forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalltable.h
489 lines (418 loc) · 12.7 KB
/
calltable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* Martin Vit [email protected]
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2.
*/
/* Calls are stored into indexed array.
* Into one calltable is stored SIP call-id and IP-port of SDP session
*/
#ifndef CALLTABLE_H
#define CALLTABLE_H
#include <queue>
#include <arpa/inet.h>
#include <time.h>
#include <pcap.h>
#include <mysql++.h>
#include <string>
#include "rtp.h"
#define MAX_IP_PER_CALL 30 //!< total maxumum of SDP sessions for one call-id
#define MAX_SSRC_PER_CALL 30 //!< total maxumum of SDP sessions for one call-id
#define MAX_CALL_ID 32 //!< max len of stored call-id
#define MAX_FNAME 256 //!< max len of stored call-id
#define MAX_RTPMAP 30 //!< max rtpmap records
#define RTPTIMEOUT 300
#define MAXNODE 150000
#define INVITE 1
#define BYE 2
#define CANCEL 3
#define RES2XX 4
#define RES3XX 5
#define RES4XX 6
#define RES5XX 7
#define RES6XX 8
#define RES18X 9
#define REGISTER 10
#define FLAG_SAVERTP (1 << 0)
#define FLAG_SAVESIP (1 << 1)
#define FLAG_SAVEREGISTER (1 << 2)
#define FLAG_SAVEWAV (1 << 3)
#define FLAG_SAVEGRAPH (1 << 4)
/**
* This class implements operations on call
*/
class Call {
public:
int type; //!< type of call, INVITE or REGISTER
RTP *rtp[MAX_SSRC_PER_CALL]; //!< array of RTP streams
unsigned long call_id_len; //!< length of call-id
char call_id[MAX_CALL_ID]; //!< call-id from SIP session
char fbasename[MAX_FNAME]; //!< basename of file
char fbasename_safe[MAX_FNAME]; //!< basename of file
char callername[256]; //!< callerid name from SIP header
char caller[256]; //!< From: xxx
char caller_domain[256]; //!< From: xxx
char called[256]; //!< To: xxx
char called_domain[256]; //!< To: xxx
char byecseq[32];
char invitecseq[32];
char custom_header1[256]; //!< Custom SIP header
bool seeninvite; //!< true if we see SIP INVITE within the Call
bool seeninviteok; //!< true if we see SIP INVITE within the Call
bool seenbye; //!< true if we see SIP BYE within the Call
bool seenbyeandok; //!< true if we see SIP OK TO BYE OR TO CANEL within the Call
bool sighup; //!< true if call is saving during sighup
char *dirname(); //!< name of the directory to store files for the Call
char a_ua[1024]; //!< caller user agent
char b_ua[1024]; //!< callee user agent
int rtpmap[MAX_IP_PER_CALL][20]; //!< rtpmap for every rtp stream
RTP tmprtp; //!< temporary structure used to decode information from frame
RTP *lastcallerrtp; //!< last RTP stream from caller
RTP *lastcalledrtp; //!< last RTP stream from called
void *calltable; //!< reference to calltable
u_int32_t saddr; //!< source IP address of first INVITE
unsigned short sport; //!< source port of first INVITE
int whohanged; //!< who hanged up. 0 -> caller, 1-> callee, -1 -> unknown
int recordstopped; //!< flag holding if call was stopped to avoid double free
int dtmfflag; //!< used for holding dtmf states
time_t progress_time; //!< time in seconds of 18X response
time_t first_rtp_time; //!< time in seconds of first RTP packet
time_t connect_time; //!< time in seconds of 200 OK
time_t last_packet_time;
time_t first_packet_time;
time_t destroy_call_at;
void *rtp_cur[2]; //!< last RTP structure in direction 0 and 1 (iscaller = 1)
void *rtp_prev[2]; //!< previouse RTP structure in direction 0 and 1 (iscaller = 1)
u_int32_t sipcallerip; //!< SIP signalling source IP address
u_int32_t sipcalledip; //!< SIP signalling destination IP address
char lastSIPresponse[128];
int lastSIPresponseNum;
char pcapfilename[512];
int last_callercodec; //!< Last caller codec
int last_calledcodec; //!< Last called codec
int fifo1;
int fifo2;
int codec_caller;
int codec_called;
unsigned int flags; //!< structure holding FLAGS*
int *listening_worker_run;
int thread_num;
void *listening_worker_args;
int ssrc_n; //!< last index of rtp array
/**
* constructor
*
* @param call_id unique identification of call parsed from packet
* @param call_id_len lenght of the call_id buffer
* @param time time of the first packet
* @param ct reference to calltable
*
*/
Call(char *call_id, unsigned long call_id_len, time_t time, void *ct);
/**
* destructor
*
*/
~Call();
/**
* @brief find Call by IP adress and port.
*
* This function is applied for every incoming UDP packet
*
* @param addr IP address of the packet
* @param port port number of the packet
*
* @return reference to the finded Call or NULL if not found.
*/
Call *find_by_ip_port(in_addr_t addr, unsigned short port, int *iscaller);
int get_index_by_ip_port(in_addr_t addr, unsigned short port);
/**
* @brief close all rtp[].gfileRAW
*
* close all RTP[].gfileRAW to flush writes
*
* @return nothing
*/
void closeRawFiles();
/**
* @brief read RTP packet
*
* Used for reading RTP packet
*
* @param data pointer to the packet buffer
* @param datalen lenght of the buffer
* @param header header structure of the packet
* @param saddr source IP adress of the packet
*
*/
void read_rtp( unsigned char *data, int datalen, struct pcap_pkthdr *header, u_int32_t saddr, unsigned short port, int iscaller);
/**
* @brief read RTCP packet
*
* Used for reading RTCP packet
*
* @param data pointer to the packet buffer
* @param datalen lenght of the buffer
* @param header header structure of the packet
* @param saddr source IP adress of the packet
*
*/
void read_rtcp(unsigned char*, int, pcap_pkthdr*, u_int32_t, short unsigned int, int);
/**
* @brief adds RTP stream to the this Call
*
* Adds RTP stream to the this Call which is identified by IP address and port number
*
* @param addr IP address of the RTP stream
* @param port port number of the RTP stream
*
* @return return 0 on success, 1 if IP and port is duplicated and -1 on failure
*/
int add_ip_port(in_addr_t addr, unsigned short port, char *ua, unsigned long ua_len, bool iscaller, int *rtpmap);
/**
* @brief get file descriptor of the writing pcap file
*
* @return file descriptor of the writing pcap file
*/
pcap_dumper_t *get_f_pcap() { return f_pcap; };
/**
* @brief set file descriptor of the writing pcap file
*
* @param file descriptor
*/
void set_f_pcap(pcap_dumper_t *f_pcap) { this->f_pcap = f_pcap; };
/**
* @brief get time of the last seen packet which belongs to this call
*
* @param f_pcap file descriptor
*
* @return time of the last packet in seconds from UNIX epoch
*/
time_t get_last_packet_time() { return last_packet_time; };
/**
* @brief set time of the last seen packet which belongs to this call
*
* this time is used for calculating lenght of the call
*
* @param timestamp in seconds from UNIX epoch
*
*/
void set_last_packet_time(time_t mtime) { last_packet_time = mtime; };
/**
* @brief get first time of the the packet which belongs to this call
*
* this time is used as start of the call in CDR record
*
* @return time of the first packet in seconds from UNIX epoch
*/
time_t get_first_packet_time() { return first_packet_time; };
/**
* @brief set first time of the the packet which belongs to this call
*
* @param timestamp in seconds from UNIX epoch
*
*/
void set_first_packet_time(time_t mtime) { first_packet_time = mtime; };
/**
* @brief convert raw files to one WAV
*
*/
int convertRawToWav();
/**
* @brief build query
*
*/
int buildQuery(stringstream *query);
/**
* @brief prepare for escape string - connect if need
*
*/
bool prepareForEscapeString();
/**
* @brief execute query
*
*/
int doQuery(string &queryStr);
/**
* @brief save call to database
*
*/
int saveToDb();
/**
* @brief save register msgs to database
*
*/
int saveRegisterToDb();
/**
* @brief calculate duration of the call
*
* @return lenght of the call in seconds
*/
int duration() { return last_packet_time - first_packet_time; };
/**
* @brief return start of the call which is first seen packet
*
* @param timestamp in seconds from UNIX epoch
*/
int calltime() { return first_packet_time; };
/**
* @brief remove call from hash table
*
*/
void hashRemove();
/**
* @brief stop recording packets to pcap file
*
*/
void stoprecording();
/**
* @brief substitute all nonalphanum string to "_" (except for @)
*
*/
char *get_fbasename_safe();
/**
* @brief print debug information for the call to stdout
*
*/
void addtocachequeue(string file);
void dump();
private:
in_addr_t addr[MAX_IP_PER_CALL]; //!< IP address from SDP (indexed together with port)
unsigned short port[MAX_IP_PER_CALL]; //!< port number from SDP (indexed together with IP)
bool iscaller[MAX_IP_PER_CALL]; //!< is that RTP stream from CALLER party?
int ipport_n; //!< last index of addr and port array
pcap_dumper_t *f_pcap;
char sdirname[255];
};
/**
* This class implements operations on Call list
*/
class Calltable {
public:
queue<Call*> calls_queue; //!< this queue is used for asynchronous storing CDR by the worker thread
queue<Call*> calls_deletequeue; //!< this queue is used for asynchronous storing CDR by the worker thread
queue<string> files_queue; //!< this queue is used for asynchronous storing CDR by the worker thread
list<Call*> calls_list; //!<
list<Call*>::iterator call;
map<string, Call*> calls_listMAP; //!<
map<string, Call*>::iterator callMAPIT; //!<
/**
* @brief constructor
*
*/
Calltable();
/*
Calltable() {
pthread_mutex_init(&qlock, NULL);
printf("SS:%d\n", sizeof(calls_hash));
printf("SS:%s\n", 1);
memset(calls_hash, 0x0, sizeof(calls_hash) * MAXNODE);
};
*/
/**
* destructor
*
*/
~Calltable();
/**
* @brief lock calls_queue structure
*
*/
void lock_calls_queue() { pthread_mutex_lock(&qlock); };
void lock_calls_deletequeue() { pthread_mutex_lock(&qdellock); };
void lock_files_queue() { pthread_mutex_lock(&flock); };
/**
* @brief unlock calls_queue structure
*
*/
void unlock_calls_queue() { pthread_mutex_unlock(&qlock); };
void unlock_calls_deletequeue() { pthread_mutex_unlock(&qdellock); };
void unlock_files_queue() { pthread_mutex_unlock(&flock); };
/**
* @brief lock files_queue structure
*
*/
/**
* @brief add Call to Calltable
*
* @param call_id unique identifier of the Call which is parsed from the SIP packets
* @param call_id_len lenght of the call_id buffer
* @param time timestamp of arrivel packet in seconds from UNIX epoch
*
* @return reference of the new Call class
*/
Call *add(char *call_id, unsigned long call_id_len, time_t time, u_int32_t saddr, unsigned short port);
/**
* @brief find Call by call_id
*
* @param call_id unique identifier of the Call which is parsed from the SIP packets
* @param call_id_len lenght of the call_id buffer
*
* @return reference of the Call if found, otherwise return NULL
*/
Call *find_by_call_id(char *call_id, unsigned long call_id_len);
/**
* @brief find Call by IP adress and port number
*
* @param addr IP address of the packet
* @param port port number of the packet
*
* @return reference of the Call if found, otherwise return NULL
*/
Call *find_by_ip_port(in_addr_t addr, unsigned short port, int *iscaller);
/**
* @brief Save inactive calls to MySQL and delete it from list
*
*
* walk this list of Calls and if any of the call is inactive more
* than 5 minutes, save it to MySQL and delete it from the list
*
* @param cuutime current time
*
* @return reference of the Call if found, otherwise return NULL
*/
int cleanup( time_t currtime );
/**
* @brief add call to hash table
*
*/
void hashAdd(in_addr_t addr, unsigned short port, Call* call, int iscaller, int isrtcp);
/**
* @brief add call to hash table
*
*/
Call *hashfind_by_ip_port(in_addr_t addr, unsigned short port, int *iscaller, int *isrtcp);
/**
* @brief remove call from hash
*
*/
void hashRemove(in_addr_t addr, unsigned short port);
private:
pthread_mutex_t qlock; //!< mutex locking calls_queue
pthread_mutex_t qdellock; //!< mutex locking calls_deletequeue
pthread_mutex_t flock; //!< mutex locking calls_queue
struct hash_node {
Call *call;
hash_node *next;
int iscaller;
u_int32_t addr;
u_int16_t port;
u_int16_t is_rtcp;
};
void *calls_hash[MAXNODE];
unsigned int tuplehash(u_int32_t addr, u_int16_t port) {
unsigned int key;
key = (unsigned int)(addr * port);
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += ~(key << 11);
key ^= (key >> 16);
return key % MAXNODE;
}
};
string sqlDateTimeString(time_t unixTime);
string sqlEscapeString(const char *inputStr, char borderChar = '\'');
string reverseString(const char *str);
bool isSqlDriver(const char *sqlDriver);
bool isTypeDb(const char *typeDb);
#endif