forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniff.h
554 lines (520 loc) · 14.4 KB
/
sniff.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* Martin Vit support@voipmonitor.org
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2.
*/
#ifndef SNIFF_H
#define SNIFF_H
#include <queue>
#include <map>
#include "rqueue.h"
#include "voipmonitor.h"
#include "calltable.h"
#include "pcap_queue_block.h"
#ifdef FREEBSD
#include <machine/endian.h>
#else
#include "asm/byteorder.h"
#endif
#define MAXPACKETLENQRING 1600
#define RTP_FIXED_HEADERLEN 12
#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
#define IP_MF 0x2000 /* Flag: "More Fragments" */
#define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
struct iphdr2 {
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
/*The options start here. */
#ifdef PACKED
} __attribute__((packed));
#else
};
#endif
void *rtp_read_thread_func(void *arg);
void readdump_libnids(pcap_t *handle);
void readdump_libpcap(pcap_t *handle);
void save_packet(Call *call, struct pcap_pkthdr *header, const u_char *packet,
unsigned int saddr, int source, unsigned int daddr, int dest,
int istcp, iphdr2 *header_ip, char *data, unsigned int datalen, unsigned int dataoffset, int type,
int forceSip, int dlt, int sensor_id);
typedef std::map<in_addr_t, in_addr_t> nat_aliases_t; //!<
/* this is copied from libpcap sll.h header file, which is not included in debian distribution */
#define SLL_ADDRLEN 8 /* length of address field */
struct sll_header {
u_int16_t sll_pkttype; /* packet type */
u_int16_t sll_hatype; /* link-layer address type */
u_int16_t sll_halen; /* link-layer address length */
u_int8_t sll_addr[SLL_ADDRLEN]; /* link-layer address */
u_int16_t sll_protocol; /* protocol */
};
struct udphdr2 {
uint16_t source;
uint16_t dest;
uint16_t len;
uint16_t check;
};
typedef struct {
Call *call;
unsigned char data[MAXPACKETLENQRING];
int datalen;
int dataoffset;
u_int32_t saddr;
u_int32_t daddr;
unsigned short sport;
unsigned short dport;
char iscaller;
char is_rtcp;
int dlt;
int sensor_id;
char save_packet;
const u_char *packet;
char istcp;
struct pcap_pkthdr header;
struct iphdr2 header_ip;
volatile char free;
} rtp_packet;
typedef struct {
Call *call;
u_int32_t saddr;
u_int32_t daddr;
unsigned short sport;
unsigned short dport;
char iscaller;
char is_rtcp;
int dlt;
int sensor_id;
char save_packet;
const u_char *packet;
char istcp;
u_char *data;
int datalen;
int dataoffset;
struct pcap_pkthdr header;
pcap_block_store *block_store;
int block_store_index;
} rtp_packet_pcap_queue;
struct rtp_read_thread {
rtp_read_thread() {
this->rtpp_queue = NULL;
this->rtpp_queue_quick = NULL;
this->rtpp_queue_quick_boost = NULL;
}
pthread_t thread; // ID of worker storing CDR thread
rqueue<rtp_packet_pcap_queue> *rtpp_queue;
rqueue_quick<rtp_packet_pcap_queue> *rtpp_queue_quick;
rqueue_quick_boost<rtp_packet_pcap_queue> *rtpp_queue_quick_boost;
};
typedef struct {
struct pcap_pkthdr header;
u_char packet[MAXPACKETLENQRING];
u_char *packet2;
int offset;
volatile char free;
} pcap_packet;
#define MAXLIVEFILTERS 10
#define MAXLIVEFILTERSCHARS 32
typedef struct livesnifferfilter_s {
struct state_s {
bool all_saddr;
bool all_daddr;
bool all_bothaddr;
bool all_addr;
bool all_srcnum;
bool all_dstnum;
bool all_bothnum;
bool all_num;
bool all_siptypes;
bool all_all;
};
unsigned int lv_saddr[MAXLIVEFILTERS];
unsigned int lv_daddr[MAXLIVEFILTERS];
unsigned int lv_bothaddr[MAXLIVEFILTERS];
char lv_srcnum[MAXLIVEFILTERS][MAXLIVEFILTERSCHARS];
char lv_dstnum[MAXLIVEFILTERS][MAXLIVEFILTERSCHARS];
char lv_bothnum[MAXLIVEFILTERS][MAXLIVEFILTERSCHARS];
unsigned char lv_siptypes[MAXLIVEFILTERS];
int uid;
time_t created_at;
state_s state;
void updateState();
string getStringState();
} livesnifferfilter_t;
struct livesnifferfilter_use_siptypes_s {
bool u_invite;
bool u_register;
bool u_options;
bool u_subscribe;
bool u_message;
bool u_notify;
};
struct gre_hdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
#ifdef FREEBSD
u_int16_t rec:3,
#else
__u16 rec:3,
#endif
srr:1,
seq:1,
key:1,
routing:1,
csum:1,
version:3,
reserved:4,
ack:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
#ifdef FREEBSD
u_int16_t csum:1,
#else
__u16 csum:1,
#endif
routing:1,
key:1,
seq:1,
srr:1,
rec:3,
ack:1,
reserved:4,
version:3;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
#ifdef FREEBSD
u_int16_t protocol;
#else
__be16 protocol;
#endif
};
#define MAX_TCPSTREAMS 1024
class TcpReassemblySip {
public:
struct tcp_stream2_s {
u_int64_t packet_number;
u_int32_t saddr;
u_int16_t source;
u_int32_t daddr;
u_int16_t dest;
char *data;
int datalen;
int dataoffset;
pcap_pkthdr header;
u_char *packet;
iphdr2 *header_ip;
pcap_t *handle;
int dlt;
int sensor_id;
time_t ts;
u_int32_t seq;
u_int32_t next_seq;
u_int32_t ack_seq;
tcp_stream2_s *next;
int lastpsh;
};
struct stream_id {
stream_id(u_int32_t saddr = 0, u_int16_t source = 0,
u_int32_t daddr = 0, u_int16_t dest = 0) {
this->saddr = saddr;
this->source = source;
this->daddr = daddr;
this->dest = dest;
}
u_int32_t saddr;
u_int16_t source;
u_int32_t daddr;
u_int16_t dest;
bool operator < (const stream_id& other) const {
return((this->saddr < other.saddr) ? 1 : (this->saddr > other.saddr) ? 0 :
(this->source < other.source) ? 1 : (this->source > other.source) ? 0 :
(this->daddr < other.daddr) ? 1 : (this->daddr > other.daddr) ? 0 :
(this->dest < other.dest));
}
};
public:
void processPacket(
u_int64_t packet_number,
unsigned int saddr, int source, unsigned int daddr, int dest, char *data, int datalen, int dataoffset,
pcap_t *handle, pcap_pkthdr header, const u_char *packet, struct iphdr2 *header_ip,
int dlt, int sensor_id,
bool issip);
void clean(time_t ts = 0);
private:
tcp_stream2_s *addPacket(
tcp_stream2_s *stream,
u_int64_t packet_number,
unsigned int saddr, int source, unsigned int daddr, int dest, char *data, int datalen, int dataoffset,
pcap_t *handle, pcap_pkthdr header, const u_char *packet, struct iphdr2 *header_ip,
int dlt, int sensor_id);
void complete(
tcp_stream2_s *stream, stream_id id);
tcp_stream2_s *getLastStreamItem(tcp_stream2_s *stream) {
while(stream->next) {
stream = stream->next;
}
return(stream);
}
bool isCompleteStream(tcp_stream2_s *stream) {
bool streamOneItems = false;
tcp_stream2_s *lastStreamItem = NULL;
if(stream->next) {
lastStreamItem = getLastStreamItem(stream);
} else {
lastStreamItem = stream;
streamOneItems = true;
}
if(lastStreamItem->datalen >= 2 &&
lastStreamItem->data[lastStreamItem->datalen - 2] == 0x0d && lastStreamItem->data[lastStreamItem->datalen - 1] == 0x0a) {
return(true);
}
if(streamOneItems) {
char *endHeaderSepPos = (char*)memmem(stream->data, stream->datalen, "\r\n\r\n", 4);
if(endHeaderSepPos) {
*endHeaderSepPos = 0;
char *contentLengthPos = strcasestr(stream->data, "Content-Length: ");
bool okLength = false;
if(contentLengthPos) {
unsigned int contentLength = atol(contentLengthPos + 16);
if((endHeaderSepPos - stream->data) + 4 + contentLength == stream->datalen) {
okLength = true;
}
}
*endHeaderSepPos = '\r';
if(okLength) {
return(true);
}
}
}
return(false);
}
private:
map<stream_id, tcp_stream2_s*> tcp_streams;
};
class PreProcessPacket {
public:
struct packet_s {
u_int64_t packet_number;
unsigned int saddr;
int source;
unsigned int daddr;
int dest;
char *data;
int datalen;
int dataoffset;
pcap_t *handle;
pcap_pkthdr header;
const u_char *packet;
bool packetDelete;
int istcp;
struct iphdr2 *header_ip;
int forceSip;
pcap_block_store *block_store;
int block_store_index;
int dlt;
int sensor_id;
bool is_ssl;
};
struct packet_parse_s {
packet_parse_s() {
init();
}
void init() {
sip_method = -1;
sip_response = false;
lastSIPresponseNum = -1;
call_cancel_lsr487 = false;
call = NULL;
call_created = NULL;
detectUserAgent = false;
_getCallID_reassembly = false;
_getSipMethod = false;
_getLastSipResponse = false;
_findCall = false;
_createCall = false;
}
packet_s packet;
ParsePacket parse;
u_int32_t sipDataLen;
bool isSip;
string callid;
int sip_method;
bool sip_response;
int lastSIPresponseNum;
string lastSIPresponse;
bool call_cancel_lsr487;
Call *call;
Call *call_created;
bool detectUserAgent;
bool _getCallID_reassembly;
bool _getSipMethod;
bool _getLastSipResponse;
bool _findCall;
bool _createCall;
unsigned int hash[2];
volatile int used;
};
public:
PreProcessPacket();
~PreProcessPacket();
void push(bool is_ssl, u_int64_t packet_number,
unsigned int saddr, int source, unsigned int daddr, int dest,
char *data, int datalen, int dataoffset,
pcap_t *handle, pcap_pkthdr *header, const u_char *packet, bool packetDelete,
int istcp, struct iphdr2 *header_ip, int forceSip,
pcap_block_store *block_store, int block_store_index, int dlt, int sensor_id,
bool disableLock = false);
void preparePstatData();
double getCpuUsagePerc(bool preparePstatData);
void terminate();
private:
bool sipProcess(packet_parse_s *parse_packet);
bool sipProcess_getCallID(packet_parse_s *parse_packet);
bool sipProcess_reassembly(packet_parse_s *parse_packet);
void sipProcess_getSipMethod(packet_parse_s *parse_packet);
void sipProcess_getLastSipResponse(packet_parse_s *parse_packet);
void sipProcess_findCall(packet_parse_s *parse_packet);
void sipProcess_createCall(packet_parse_s *parse_packet);
void *outThreadFunction();
void lock_push() {
while(__sync_lock_test_and_set(&this->_sync_push, 1)) {
usleep(10);
}
}
void unlock_push() {
__sync_lock_release(&this->_sync_push);
}
private:
packet_parse_s **qring;
unsigned int qringmax;
volatile unsigned int readit;
volatile unsigned int writeit;
pthread_t out_thread_handle;
pstat_data threadPstatData[2];
int outThreadId;
volatile int _sync_push;
bool term_preProcess;
friend inline void *_PreProcessPacket_outThreadFunction(void *arg);
};
class ProcessRtpPacket {
public:
enum eType {
hash,
distribute
};
public:
struct rtp_call_info {
Call *call;
bool iscaller;
bool is_rtcp;
s_sdp_flags sdp_flags;
bool use_sync;
};
struct packet_s {
unsigned int saddr;
int source;
unsigned int daddr;
int dest;
char *data;
int datalen;
int dataoffset;
pcap_t *handle;
pcap_pkthdr header;
const u_char *packet;
int istcp;
struct iphdr2 *header_ip;
pcap_block_store *block_store;
int block_store_index;
int dlt;
int sensor_id;
unsigned int hash_s;
unsigned int hash_d;
rtp_call_info call_info[20];
int call_info_length;
bool call_info_find_by_dest;
volatile int used;
};
public:
ProcessRtpPacket(eType type, int indexThread);
~ProcessRtpPacket();
void push(unsigned int saddr, int source, unsigned int daddr, int dest,
char *data, int datalen, int dataoffset,
pcap_t *handle, pcap_pkthdr *header, const u_char *packet, int istcp, struct iphdr2 *header_ip,
pcap_block_store *block_store, int block_store_index, int dlt, int sensor_id,
unsigned int hash_s, unsigned int hash_d);
void push(packet_s *_packet);
void preparePstatData(bool nextThread = false);
double getCpuUsagePerc(bool preparePstatData, bool nextThread = false);
void terminate();
private:
void *outThreadFunction();
void *nextThreadFunction();
void rtp(packet_s *_packet);
void find_hash(packet_s *_packet, bool lock = true);
packet_s *qring_item(size_t index) {
index += this->readit;
if(index >= qringmax) {
index -= qringmax;
}
return(&qring[index]);
}
size_t qring_size() {
unsigned int _writeit = writeit;
unsigned int _readit = readit;
return(_writeit > _readit ?
_writeit - _readit:
_writeit < _readit ?
_writeit - 1 + qringmax - _readit :
(qring[_readit].used ? qringmax : 0));
}
public:
#if RTP_PROF
volatile unsigned long long __prof__ProcessRtpPacket_outThreadFunction_begin;
volatile unsigned long long __prof__ProcessRtpPacket_outThreadFunction;
volatile unsigned long long __prof__ProcessRtpPacket_outThreadFunction__usleep;
volatile unsigned long long __prof__ProcessRtpPacket_rtp;
volatile unsigned long long __prof__ProcessRtpPacket_rtp__hashfind;
volatile unsigned long long __prof__ProcessRtpPacket_rtp__fill_call_array;
volatile unsigned long long __prof__process_packet__rtp;
volatile unsigned long long __prof__add_to_rtp_thread_queue;
#endif
eType type;
int indexThread;
int outThreadId;
int nextThreadId;
private:
packet_s *qring;
unsigned int qringmax;
unsigned int hash_blob_limit;
volatile unsigned int readit;
volatile unsigned int writeit;
pthread_t out_thread_handle;
pthread_t next_thread_handle;
pstat_data threadPstatData[2][2];
bool term_processRtp;
volatile int hash_buffer_next_thread_process;
volatile unsigned int hash_blob_size;
friend inline void *_ProcessRtpPacket_outThreadFunction(void *arg);
friend inline void *_ProcessRtpPacket_nextThreadFunction(void *arg);
};
#define enable_save_sip(call) (call->flags & FLAG_SAVESIP)
#define enable_save_register(call) (call->flags & FLAG_SAVEREGISTER)
#define enable_save_rtp(call) ((call->flags & (FLAG_SAVERTP | FLAG_SAVERTPHEADER)) || (call->isfax && opt_saveudptl) || opt_saverfc2833)
#define enable_save_sip_rtp(call) (enable_save_sip(call) || enable_save_rtp(call))
#define enable_save_packet(call) (enable_save_sip(call) || enable_save_register(call) || enable_save_rtp(call))
#define enable_save_audio(call) ((call->flags & FLAG_SAVEAUDIO) || opt_savewav_force)
#define enable_save_sip_rtp_audio(call) (enable_save_sip_rtp(call) || enable_save_audio(call))
#define enable_save_any(call) (enable_save_packet(call) || enable_save_audio(call))
#endif