forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap_queue.cpp
4953 lines (4721 loc) · 159 KB
/
pcap_queue.cpp
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <errno.h>
#include <fcntl.h>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <iostream>
#include <sstream>
#include <sys/syscall.h>
#include <vector>
#include <malloc.h>
#include <snappy-c.h>
#ifdef HAVE_LIBLZ4
#include <lz4.h>
#endif //HAVE_LIBLZ4
#include "pcap_queue_block.h"
#include "pcap_queue.h"
#include "hash.h"
#include "mirrorip.h"
#include "ipaccount.h"
#include "filter_mysql.h"
#include "tcpreassembly.h"
#include "sniff.h"
#include "rrd.h"
#include "cleanspool.h"
#include "ssldata.h"
#include "tar.h"
#define TEST_DEBUG_PARAMS 0
#if TEST_DEBUG_PARAMS == 1
#define OPT_PCAP_BLOCK_STORE_MAX_ITEMS 2000
#define OPT_PCAP_FILE_STORE_MAX_BLOCKS 1000
#define OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_MEMORY 500
#define OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_DISK 40000
#define OPT_PCAP_QUEUE_BYPASS_MAX_ITEMS 500
#else
#define OPT_PCAP_BLOCK_STORE_MAX_ITEMS 2000 // 500 kB
#define OPT_PCAP_FILE_STORE_MAX_BLOCKS 1000 // 500 MB
#define OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_MEMORY 500 // 250 MB
#define OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_DISK 40000 // 20 GB
#define OPT_PCAP_QUEUE_BYPASS_MAX_ITEMS 500 // 500 MB
#endif
#define AVG_PACKET_SIZE 250
#define VERBOSE (verbosity > 0)
#define DEBUG_VERBOSE (VERBOSE && false)
#define DEBUG_SYNC (DEBUG_VERBOSE && false)
#define DEBUG_SLEEP (DEBUG_VERBOSE && true)
#define DEBUG_ALL_PACKETS (DEBUG_VERBOSE && false)
#define TEST_PACKETS (DEBUG_VERBOSE && false)
#define VERBOSE_TEST_PACKETS (TEST_PACKETS && false)
#define TERMINATING ((is_terminating() && this->enableAutoTerminate) || this->threadDoTerminate)
#define MAX_TCPSTREAMS 1024
#define FILE_BUFFER_SIZE 1000000
using namespace std;
extern Call *process_packet(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,
int istcp, int *was_rtp, struct iphdr2 *header_ip, int *voippacket, int forceSip,
pcap_block_store *block_store, int block_store_index, int dlt, int sensor_id,
bool mainProcess = true, int sipOffset = 0,
PreProcessPacket::packet_parse_s *parsePacket = NULL);
extern int check_sip20(char *data, unsigned long len);
void daemonizeOutput(string error);
extern int verbosity;
extern int verbosityE;
extern int opt_rrd;
extern char opt_chdir[1024];
extern int opt_udpfrag;
extern int opt_skinny;
extern int opt_ipaccount;
extern int opt_pcapdump;
extern int opt_pcapdump_all;
extern int opt_dup_check;
extern int opt_dup_check_ipheader;
extern int opt_mirrorip;
extern char opt_mirrorip_src[20];
extern char opt_mirrorip_dst[20];
extern int opt_enable_http;
extern int opt_enable_webrtc;
extern int opt_enable_ssl;
extern int opt_tcpreassembly_pb_lock;
extern int opt_fork;
extern int opt_id_sensor;
extern char opt_name_sensor[256];
extern int opt_mysqlstore_max_threads_cdr;
extern int opt_mysqlstore_max_threads_message;
extern int opt_mysqlstore_max_threads_register;
extern int opt_mysqlstore_max_threads_http;
extern int opt_mysqlstore_max_threads_ipacc_base;
extern int opt_mysqlstore_max_threads_ipacc_agreg2;
extern pcap_t *global_pcap_handle;
extern char *sipportmatrix;
extern char *httpportmatrix;
extern char *webrtcportmatrix;
extern unsigned int duplicate_counter;
extern struct tcp_stream2_t *tcp_streams_hashed[MAX_TCPSTREAMS];
extern MirrorIP *mirrorip;
extern char user_filter[10*2048];
extern Calltable *calltable;
extern volatile int calls_counter;
extern PreProcessPacket *preProcessPacket;
extern ProcessRtpPacket *processRtpPacketHash;
extern ProcessRtpPacket *processRtpPacketDistribute[MAX_PROCESS_RTP_PACKET_THREADS];
extern TcpReassembly *tcpReassemblyHttp;
extern TcpReassembly *tcpReassemblyWebrtc;
extern TcpReassembly *tcpReassemblySsl;
extern char opt_pb_read_from_file[256];
extern int opt_pb_read_from_file_speed;
extern int opt_pb_read_from_file_acttime;
extern char opt_scanpcapdir[2048];
extern int global_pcap_dlink;
extern char opt_cachedir[1024];
extern unsigned long long cachedirtransfered;
unsigned long long lastcachedirtransfered = 0;
extern char opt_cachedir[1024];
extern char cloud_host[256];
extern int opt_pcap_dump_tar;
extern volatile unsigned int glob_tar_queued_files;
extern cBuffersControl buffersControl;
vm_atomic<string> pbStatString;
vm_atomic<u_long> pbCountPacketDrop;
void *_PcapQueue_threadFunction(void *arg);
void *_PcapQueue_writeThreadFunction(void *arg);
void *_PcapQueue_readFromInterfaceThread_threadFunction(void *arg);
void *_PcapQueue_readFromFifo_socketServerThreadFunction(void *arg);
void *_PcapQueue_readFromFifo_connectionThreadFunction(void *arg);
static bool __config_BYPASS_FIFO = true;
static bool __config_USE_PCAP_FOR_FIFO = false;
static bool __config_ENABLE_TOGETHER_READ_WRITE_FILE = false;
#if TEST_DEBUG_PARAMS > 0
u_int opt_pcap_queue_block_max_time_ms = 500;
size_t opt_pcap_queue_block_max_size = OPT_PCAP_BLOCK_STORE_MAX_ITEMS * AVG_PACKET_SIZE;
u_int opt_pcap_queue_file_store_max_time_ms = 5000;
size_t opt_pcap_queue_file_store_max_size = opt_pcap_queue_block_max_size * OPT_PCAP_FILE_STORE_MAX_BLOCKS;
uint64_t opt_pcap_queue_store_queue_max_memory_size = opt_pcap_queue_block_max_size * OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_MEMORY;
uint64_t opt_pcap_queue_store_queue_max_disk_size = opt_pcap_queue_block_max_size * OPT_PCAP_STORE_QUEUE_MAX_BLOCKS_IN_DISK;
uint64_t opt_pcap_queue_bypass_max_size = opt_pcap_queue_block_max_size * OPT_PCAP_QUEUE_BYPASS_MAX_ITEMS;
#else
u_int opt_pcap_queue_block_max_time_ms = 500;
size_t opt_pcap_queue_block_max_size = 1024 * 1024;
u_int opt_pcap_queue_file_store_max_time_ms = 2000;
size_t opt_pcap_queue_file_store_max_size = 200 * 1024 * 1024;
uint64_t opt_pcap_queue_store_queue_max_memory_size = 200 * 1024 * 1024; //default is 200MB
uint64_t opt_pcap_queue_store_queue_max_disk_size = 0;
uint64_t opt_pcap_queue_bypass_max_size = 256 * 1024 * 1024;
#endif
int opt_pcap_queue_compress = -1;
pcap_block_store::compress_method opt_pcap_queue_compress_method
= pcap_block_store::snappy;
string opt_pcap_queue_disk_folder;
ip_port opt_pcap_queue_send_to_ip_port;
ip_port opt_pcap_queue_receive_from_ip_port;
int opt_pcap_queue_receive_from_port;
int opt_pcap_queue_receive_dlt = DLT_EN10MB;
int opt_pcap_queue_iface_separate_threads = 0;
int opt_pcap_queue_iface_dedup_separate_threads = 0;
int opt_pcap_queue_iface_dedup_separate_threads_extend = 0;
int opt_pcap_queue_iface_qring_size = 5000;
int opt_pcap_queue_dequeu_window_length = -1;
int opt_pcap_queue_dequeu_method = 2;
int opt_pcap_dispatch = 0;
int opt_pcap_queue_iface_dedup_separate_threads_extend__ext_mode
= 1;
size_t _opt_pcap_queue_block_offset_inc_size = opt_pcap_queue_block_max_size / AVG_PACKET_SIZE / 4;
size_t _opt_pcap_queue_block_restore_buffer_inc_size = opt_pcap_queue_block_max_size / 4;
int pcap_drop_flag = 0;
int enable_bad_packet_order_warning = 0;
bool exists_thread_delete = 0;
static pcap_block_store_queue *blockStoreBypassQueue;
static unsigned long sumPacketsCounterIn[2];
static unsigned long sumPacketsCounterOut[2];
static unsigned long sumBlocksCounterIn[2];
static unsigned long sumBlocksCounterOut[2];
static unsigned long long sumPacketsSize[2];
static unsigned long long sumPacketsSizeCompress[2];
static unsigned long maxBypassBufferItems;
static unsigned long maxBypassBufferSize;
static unsigned long countBypassBufferSizeExceeded;
static double heapPerc = 0;
extern MySqlStore *sqlStore;
extern MySqlStore *loadFromQFiles;
extern unsigned int glob_ssl_calls;
bool packetbuffer_memory_is_full = false;
#include "sniff_inline.h"
bool pcap_block_store::add(pcap_pkthdr *header, u_char *packet, int offset, int dlink) {
if(this->full) {
return(false);
}
if((this->size + sizeof(pcap_pkthdr_plus) + header->caplen) > opt_pcap_queue_block_max_size ||
(this->size && (getTimeMS() - this->timestampMS) >= opt_pcap_queue_block_max_time_ms)) {
this->full = true;
return(false);
}
if(!this->block) {
while(true) {
this->block = new FILE_LINE u_char[opt_pcap_queue_block_max_size];
if(this->block) {
break;
}
syslog(LOG_ERR, "not enough memory for alloc packetbuffer block");
sleep(1);
}
}
if(!this->offsets_size) {
this->offsets_size = _opt_pcap_queue_block_offset_inc_size;
this->offsets = new FILE_LINE uint32_t[this->offsets_size];
}
if(this->count == this->offsets_size) {
uint32_t *offsets_old = this->offsets;
size_t offsets_size_old = this->offsets_size;
this->offsets_size += _opt_pcap_queue_block_offset_inc_size;
this->offsets = new FILE_LINE uint32_t[this->offsets_size];
memcpy_heapsafe(this->offsets, offsets_old, sizeof(uint32_t) * offsets_size_old,
__FILE__, __LINE__);
delete [] offsets_old;
}
this->offsets[this->count] = this->size;
pcap_pkthdr_plus header_plus = pcap_pkthdr_plus(*header, offset, dlink);
memcpy_heapsafe(this->block + this->size, this->block,
&header_plus, NULL,
sizeof(pcap_pkthdr_plus),
__FILE__, __LINE__);
this->size += sizeof(pcap_pkthdr_plus);
memcpy_heapsafe(this->block + this->size, this->block,
packet, NULL,
header->caplen,
__FILE__, __LINE__);
this->size += header->caplen;
++this->count;
return(true);
}
bool pcap_block_store::add(pcap_pkthdr_plus *header, u_char *packet) {
return(this->add((pcap_pkthdr*)header, packet, header->offset, header->dlink));
}
bool pcap_block_store::isFull_checkTimout() {
if(this->full) {
return(true);
}
if(this->size && (getTimeMS() - this->timestampMS) >= opt_pcap_queue_block_max_time_ms) {
this->full = true;
return(true);
}
return(false);
}
void pcap_block_store::destroy() {
if(this->offsets) {
delete [] this->offsets;
this->offsets = NULL;
}
if(this->block) {
delete [] this->block;
this->block = NULL;
}
this->size = 0;
this->size_compress = 0;
this->count = 0;
this->offsets_size = 0;
this->full = false;
this->dlink = global_pcap_dlink;
this->sensor_id = opt_id_sensor;
memset(this->ifname, 0, sizeof(this->ifname));
}
void pcap_block_store::destroyRestoreBuffer() {
if(this->restoreBuffer) {
delete [] this->restoreBuffer;
this->restoreBuffer = NULL;
}
this->restoreBufferSize = 0;
this->restoreBufferAllocSize = 0;
}
bool pcap_block_store::isEmptyRestoreBuffer() {
return(!this->restoreBuffer);
}
void pcap_block_store::freeBlock() {
if(this->block) {
delete [] this->block;
this->block = NULL;
}
}
u_char* pcap_block_store::getSaveBuffer() {
size_t sizeSaveBuffer = this->getSizeSaveBuffer();
u_char *saveBuffer = new FILE_LINE u_char[sizeSaveBuffer];
pcap_block_store_header header;
header.size = this->size;
header.size_compress = this->size_compress;
header.count = this->count;
header.dlink = this->dlink;
header.sensor_id = this->sensor_id;
strcpy(header.ifname, this->ifname);
memcpy_heapsafe(saveBuffer, saveBuffer,
&header, NULL,
sizeof(header),
__FILE__, __LINE__);
memcpy_heapsafe(saveBuffer + sizeof(header), saveBuffer,
this->offsets, this->offsets,
sizeof(uint32_t) * this->count,
__FILE__, __LINE__);
memcpy_heapsafe(saveBuffer + sizeof(pcap_block_store_header) + this->count * sizeof(uint32_t), saveBuffer,
this->block, this->block,
this->getUseSize(),
__FILE__, __LINE__);
return(saveBuffer);
}
void pcap_block_store::restoreFromSaveBuffer(u_char *saveBuffer) {
pcap_block_store_header *header = (pcap_block_store_header*)saveBuffer;
this->size = header->size;
this->size_compress = header->size_compress;
this->count = header->count;
this->dlink = header->dlink;
this->sensor_id = header->sensor_id;
strcpy(this->ifname, header->ifname);
if(this->offsets) {
delete [] this->offsets;
}
if(this->block) {
delete [] this->block;
}
this->offsets_size = this->count;
this->offsets = new FILE_LINE uint32_t[this->offsets_size];
memcpy_heapsafe(this->offsets, this->offsets,
saveBuffer + sizeof(pcap_block_store_header), saveBuffer,
sizeof(uint32_t) * this->count,
__FILE__, __LINE__);
size_t sizeBlock = this->getUseSize();
this->block = new FILE_LINE u_char[sizeBlock];
memcpy_heapsafe(this->block, this->block,
saveBuffer + sizeof(pcap_block_store_header) + this->count * sizeof(uint32_t), saveBuffer,
sizeBlock,
__FILE__, __LINE__);
this->full = true;
}
int pcap_block_store::addRestoreChunk(u_char *buffer, size_t size, size_t *offset, bool autoRestore) {
u_char *_buffer = buffer + (offset ? *offset : 0);
size_t _size = size - (offset ? *offset : 0);
if(_size <= 0) {
return(-1);
}
if(this->restoreBufferAllocSize < this->restoreBufferSize + _size) {
this->restoreBufferAllocSize = this->restoreBufferSize + _size + _opt_pcap_queue_block_restore_buffer_inc_size;
u_char *restoreBufferNew = new FILE_LINE u_char[this->restoreBufferAllocSize];
if(this->restoreBuffer) {
memcpy_heapsafe(restoreBufferNew, this->restoreBuffer, this->restoreBufferSize,
__FILE__, __LINE__);
delete [] this->restoreBuffer;
}
this->restoreBuffer = restoreBufferNew;
}
memcpy_heapsafe(this->restoreBuffer + this->restoreBufferSize, this->restoreBuffer,
_buffer, _buffer,
_size,
__FILE__, __LINE__);
this->restoreBufferSize += _size;
int sizeRestoreBuffer = this->getSizeSaveBufferFromRestoreBuffer();
if(sizeRestoreBuffer < 0 ||
this->restoreBufferSize < (size_t)sizeRestoreBuffer) {
return(0);
}
if(offset) {
*offset = size - (this->restoreBufferSize - sizeRestoreBuffer);
}
if(autoRestore) {
this->restoreFromSaveBuffer(this->restoreBuffer);
this->destroyRestoreBuffer();
}
return(1);
}
bool pcap_block_store::compress() {
if(!opt_pcap_queue_compress ||
this->size_compress) {
return(true);
}
switch(opt_pcap_queue_compress_method) {
case lz4:
#ifdef HAVE_LIBLZ4
return(this->compress_lz4());
#endif //HAVE_LIBLZ4
case snappy:
default:
return(this->compress_snappy());
}
return(true);
}
bool pcap_block_store::compress_snappy() {
size_t snappyBuffSize = snappy_max_compressed_length(this->size);
u_char *snappyBuff = new FILE_LINE u_char[snappyBuffSize];
if(!snappyBuff) {
syslog(LOG_ERR, "packetbuffer: snappy_compress: snappy buffer allocation failed - PACKETBUFFER BLOCK DROPPED!");
return(false);
}
snappy_status snappyRslt = snappy_compress((char*)this->block, this->size, (char*)snappyBuff, &snappyBuffSize);
switch(snappyRslt) {
case SNAPPY_OK:
delete [] this->block;
this->block = new FILE_LINE u_char[snappyBuffSize];
memcpy_heapsafe(this->block, snappyBuff, snappyBuffSize,
__FILE__, __LINE__);
delete [] snappyBuff;
this->size_compress = snappyBuffSize;
sumPacketsSizeCompress[0] += this->size_compress;
return(true);
case SNAPPY_INVALID_INPUT:
syslog(LOG_ERR, "packetbuffer: snappy_compress: invalid input");
break;
case SNAPPY_BUFFER_TOO_SMALL:
syslog(LOG_ERR, "packetbuffer: snappy_compress: buffer is too small");
break;
default:
syslog(LOG_ERR, "packetbuffer: snappy_compress: unknown error");
break;
}
delete [] snappyBuff;
return(false);
}
bool pcap_block_store::compress_lz4() {
#ifdef HAVE_LIBLZ4
size_t lz4BuffSize = LZ4_compressBound(this->size);
u_char *lz4Buff = new FILE_LINE u_char[lz4BuffSize];
if(!lz4Buff) {
syslog(LOG_ERR, "packetbuffer: lz4_compress: lz4 buffer allocation failed - PACKETBUFFER BLOCK DROPPED!");
return(false);
}
int lz4_size = LZ4_compress((char*)this->block, (char*)lz4Buff, this->size);
if(lz4_size > 0) {
delete [] this->block;
this->block = new FILE_LINE u_char[lz4_size];
memcpy_heapsafe(this->block, lz4Buff, lz4_size,
__FILE__, __LINE__);
delete [] lz4Buff;
this->size_compress = lz4_size;
sumPacketsSizeCompress[0] += this->size_compress;
return(true);
} else {
syslog(LOG_ERR, "packetbuffer: lz4_compress: error");
}
delete [] lz4Buff;
#endif //HAVE_LIBLZ4
return(false);
}
bool pcap_block_store::uncompress(compress_method method) {
if(!this->size_compress) {
return(true);
}
switch(method == compress_method_default ? opt_pcap_queue_compress_method : method) {
case lz4:
#ifdef HAVE_LIBLZ4
return(this->uncompress_lz4());
#endif //HAVE_LIBLZ4
case snappy:
default:
return(this->uncompress_snappy());
}
return(true);
}
bool pcap_block_store::uncompress_snappy() {
if(!this->size_compress) {
return(true);
}
size_t snappyBuffSize = this->size;
u_char *snappyBuff = new FILE_LINE u_char[snappyBuffSize];
snappy_status snappyRslt = snappy_uncompress((char*)this->block, this->size_compress, (char*)snappyBuff, &snappyBuffSize);
switch(snappyRslt) {
case SNAPPY_OK:
delete [] this->block;
this->block = snappyBuff;
this->size_compress = 0;
return(true);
case SNAPPY_INVALID_INPUT:
syslog(LOG_ERR, "packetbuffer: snappy_uncompress: invalid input");
break;
case SNAPPY_BUFFER_TOO_SMALL:
syslog(LOG_ERR, "packetbuffer: snappy_uncompress: buffer is too small");
break;
default:
syslog(LOG_ERR, "packetbuffer: snappy_uncompress: unknown error");
break;
}
delete [] snappyBuff;
return(false);
}
bool pcap_block_store::uncompress_lz4() {
#ifdef HAVE_LIBLZ4
if(!this->size_compress) {
return(true);
}
size_t lz4BuffSize = this->size;
u_char *lz4Buff = new FILE_LINE u_char[lz4BuffSize];
if(LZ4_decompress_fast((char*)this->block, (char*)lz4Buff, this->size) >= 0) {
delete [] this->block;
this->block = lz4Buff;
this->size_compress = 0;
return(true);
} else {
syslog(LOG_ERR, "packetbuffer: lz4_uncompress: error");
}
delete [] lz4Buff;
#endif //HAVE_LIBLZ4
return(false);
}
pcap_block_store_queue::pcap_block_store_queue() {
this->countOfBlocks = 0;
this->sizeOfBlocks = 0;
this->_sync_queue = 0;
}
pcap_block_store_queue::~pcap_block_store_queue() {
this->lock_queue();
while(this->queueBlock.size()) {
delete this->queueBlock.front();
this->queueBlock.pop_front();
}
this->unlock_queue();
}
pcap_file_store::pcap_file_store(u_int id, const char *folder) {
this->id = id;
this->folder = folder;
this->fileHandlePush = NULL;
this->fileHandlePop = NULL;
this->fileBufferPush = NULL;
this->fileBufferPop = NULL;
this->fileSize = 0;
this->fileSizeFlushed = 0;
this->countPush = 0;
this->countPop = 0;
this->full = false;
this->timestampMS = getTimeMS();
this->_sync_flush_file = 0;
}
pcap_file_store::~pcap_file_store() {
this->destroy();
}
bool pcap_file_store::push(pcap_block_store *blockStore) {
if(!this->fileHandlePush && !this->open(typeHandlePush)) {
return(false);
}
size_t oldFileSize = this->fileSize;
size_t sizeSaveBuffer = blockStore->getSizeSaveBuffer();
u_char *saveBuffer = blockStore->getSaveBuffer();
this->lock_sync_flush_file();
unsigned long long timeBeforeWrite = getTimeNS();
size_t rsltWrite = fwrite(saveBuffer, 1, sizeSaveBuffer, this->fileHandlePush);
unsigned long long timeAfterWrite = getTimeNS();
double diffTimeS = (timeAfterWrite - timeBeforeWrite) / 1e9;
if(diffTimeS > 0.1) {
syslog(LOG_NOTICE, "packetbuffer: slow write %luB - %.3lfs", sizeSaveBuffer, diffTimeS);
}
if(rsltWrite == sizeSaveBuffer) {
this->fileSize += rsltWrite;
} else {
syslog(LOG_ERR, "packetbuffer: write to %s failed", this->getFilePathName().c_str());
}
this->unlock_sync_flush_file();
delete [] saveBuffer;
if(rsltWrite == sizeSaveBuffer) {
blockStore->freeBlock();
blockStore->idFileStore = this->id;
blockStore->filePosition = oldFileSize;
++this->countPush;
return(true);
}
fseek(this->fileHandlePush, oldFileSize, SEEK_SET);
return(false);
}
bool pcap_file_store::pop(pcap_block_store *blockStore) {
if(!blockStore->idFileStore) {
syslog(LOG_ERR, "packetbuffer: invalid file store id");
return(false);
}
if(!this->fileHandlePop && !this->open(typeHandlePop)) {
return(false);
}
this->lock_sync_flush_file();
if(this->fileSizeFlushed <= blockStore->filePosition) {
this->fileSizeFlushed = this->fileSize;
if(this->fileHandlePush) {
fflush(this->fileHandlePush);
}
}
this->unlock_sync_flush_file();
fseek(this->fileHandlePop, blockStore->filePosition, SEEK_SET);
blockStore->destroyRestoreBuffer();
size_t readBuffSize = 1000;
u_char *readBuff = new FILE_LINE u_char[readBuffSize];
size_t readed;
while((readed = fread(readBuff, 1, readBuffSize, this->fileHandlePop)) > 0) {
if(blockStore->addRestoreChunk(readBuff, readed) > 0) {
break;
}
}
delete [] readBuff;
++this->countPop;
blockStore->destroyRestoreBuffer();
if(this->countPop == this->countPush && this->isFull()) {
this->close(typeHandlePop);
}
return(true);
}
bool pcap_file_store::open(eTypeHandle typeHandle) {
if((!(typeHandle & typeHandlePush) || this->fileHandlePush) &&
(!(typeHandle & typeHandlePop) || this->fileHandlePop)) {
return(true);
}
bool rslt = true;
string filePathName = this->getFilePathName();
if(typeHandle & typeHandlePush) {
remove(filePathName.c_str());
this->fileHandlePush = fopen(filePathName.c_str(), "wb");
if(this->fileHandlePush) {
if(VERBOSE || DEBUG_VERBOSE) {
ostringstream outStr;
outStr << "create packet buffer store: " << filePathName
<< " write handle: " << this->fileHandlePush
<< endl;
if(DEBUG_VERBOSE) {
cout << outStr.str();
} else {
syslog(LOG_ERR, "packetbuffer: %s", outStr.str().c_str());
}
}
this->fileBufferPush = new FILE_LINE u_char[FILE_BUFFER_SIZE];
setbuffer(this->fileHandlePush, (char*)this->fileBufferPush, FILE_BUFFER_SIZE);
} else {
syslog(LOG_ERR, "packetbuffer: open %s for write failed", filePathName.c_str());
rslt = false;
}
}
if(typeHandle & typeHandlePop) {
this->fileHandlePop = fopen(filePathName.c_str(), "rb");
if(this->fileHandlePop) {
if(VERBOSE || DEBUG_VERBOSE) {
ostringstream outStr;
outStr << "open file pcap store: " << filePathName
<< " read handle: " << this->fileHandlePop
<< endl;
if(DEBUG_VERBOSE) {
cout << outStr.str();
} else {
syslog(LOG_ERR, "packetbuffer: %s", outStr.str().c_str());
}
}
this->fileBufferPop = new FILE_LINE u_char[FILE_BUFFER_SIZE];
setbuffer(this->fileHandlePop, (char*)this->fileBufferPop, FILE_BUFFER_SIZE);
} else {
syslog(LOG_ERR, "packetbuffer: open %s for read failed", filePathName.c_str());
rslt = false;
}
}
return(rslt);
}
bool pcap_file_store::close(eTypeHandle typeHandle) {
if(typeHandle & typeHandlePush &&
this->fileHandlePush != NULL) {
this->lock_sync_flush_file();
if(this->fileHandlePush) {
fclose(this->fileHandlePush);
this->fileHandlePush = NULL;
delete [] this->fileBufferPush;
this->fileBufferPush = NULL;
}
this->unlock_sync_flush_file();
}
if(typeHandle & typeHandlePop &&
this->fileHandlePop != NULL) {
fclose(this->fileHandlePop);
this->fileHandlePop = NULL;
delete [] this->fileBufferPop;
this->fileBufferPop = NULL;
}
return(true);
}
bool pcap_file_store::destroy() {
this->close(typeHandleAll);
string filePathName = this->getFilePathName();
remove(filePathName.c_str());
return(true);
}
string pcap_file_store::getFilePathName() {
char filePathName[this->folder.length() + 100];
sprintf(filePathName, TEST_DEBUG_PARAMS ? "%s/pcap_store_mx_%010u" : "%s/pcap_store_%010u", this->folder.c_str(), this->id);
return(filePathName);
}
pcap_store_queue::pcap_store_queue(const char *fileStoreFolder) {
this->fileStoreFolder = fileStoreFolder;
this->lastFileStoreId = 0;
this->_sync_queue = 0;
this->_sync_fileStore = 0;
this->cleanupFileStoreCounter = 0;
this->lastTimeLogErrDiskIsFull = 0;
this->lastTimeLogErrMemoryIsFull = 0;
if(!access(fileStoreFolder, F_OK )) {
mkdir(fileStoreFolder, 0700);
}
}
pcap_store_queue::~pcap_store_queue() {
pcap_file_store *fileStore;
while(this->fileStore.size()) {
fileStore = this->fileStore.front();
delete fileStore;
this->fileStore.pop_front();
}
pcap_block_store *blockStore;
while(this->queueStore.size()) {
blockStore = this->queueStore.front();
delete blockStore;
this->queueStore.pop_front();
}
}
bool pcap_store_queue::push(pcap_block_store *blockStore, bool deleteBlockStoreIfFail) {
if(opt_scanpcapdir[0]) {
while(!is_terminating() && buffersControl.getPercUsePB() > 20) {
usleep(100);
}
if(is_terminating()) {
return(false);
}
}
bool saveToFileStore = false;
bool locked_fileStore = false;
if(opt_pcap_queue_store_queue_max_disk_size &&
this->fileStoreFolder.length()) {
if(!buffersControl.check__pcap_store_queue__push()) {
saveToFileStore = true;
} else if(!__config_ENABLE_TOGETHER_READ_WRITE_FILE) {
this->lock_fileStore();
locked_fileStore = true;
if(this->fileStore.size() &&
!this->fileStore[this->fileStore.size() - 1]->isFull(buffersControl.get__pcap_store_queue__sizeOfBlocksInMemory() == 0)) {
saveToFileStore = true;
} else {
this->unlock_fileStore();
locked_fileStore = false;
}
}
}
if(saveToFileStore) {
pcap_file_store *fileStore;
if(!locked_fileStore) {
this->lock_fileStore();
}
if(this->getFileStoreUseSize(false) > opt_pcap_queue_store_queue_max_disk_size) {
u_long actTime = getTimeMS();
if(actTime - 1000 > this->lastTimeLogErrDiskIsFull) {
syslog(LOG_ERR, "packetbuffer: DISK IS FULL");
this->lastTimeLogErrDiskIsFull = actTime;
}
if(deleteBlockStoreIfFail) {
delete blockStore;
}
this->unlock_fileStore();
return(false);
}
if(!this->fileStore.size() ||
this->fileStore[this->fileStore.size() - 1]->isFull()) {
++this->lastFileStoreId;
if(!this->lastFileStoreId) {
++this->lastFileStoreId;
}
fileStore = new FILE_LINE pcap_file_store(this->lastFileStoreId, this->fileStoreFolder.c_str());
this->fileStore.push_back(fileStore);
} else {
fileStore = this->fileStore[this->fileStore.size() - 1];
}
this->unlock_fileStore();
if(!fileStore->push(blockStore)) {
if(deleteBlockStoreIfFail) {
delete blockStore;
}
return(false);
}
} else {
if(locked_fileStore) {
this->unlock_fileStore();
}
if(!buffersControl.check__pcap_store_queue__push()) {
u_long actTime = getTimeMS();
if(actTime - 1000 > this->lastTimeLogErrMemoryIsFull) {
syslog(LOG_ERR, "packetbuffer: MEMORY IS FULL");
this->lastTimeLogErrMemoryIsFull = actTime;
}
if(deleteBlockStoreIfFail) {
delete blockStore;
}
packetbuffer_memory_is_full = true;
return(false);
} else {
this->add_sizeOfBlocksInMemory(blockStore->getUseSize());
packetbuffer_memory_is_full = false;
}
}
this->lock_queue();
this->queueStore.push_back(blockStore);
this->unlock_queue();
return(true);
}
bool pcap_store_queue::pop(pcap_block_store **blockStore) {
*blockStore = NULL;
this->lock_queue();
if(this->queueStore.size()) {
*blockStore = this->queueStore.front();
this->queueStore.pop_front();
}
this->unlock_queue();
if(*blockStore) {
if((*blockStore)->idFileStore) {
pcap_file_store *_fileStore = this->findFileStoreById((*blockStore)->idFileStore);
if(!_fileStore) {
delete *blockStore;
return(false);
}
while(!__config_ENABLE_TOGETHER_READ_WRITE_FILE && !_fileStore->full) {
usleep(100);
}
if(!_fileStore->pop(*blockStore)) {
delete *blockStore;
return(false);
}
} else {
this->sub_sizeOfBlocksInMemory((*blockStore)->getUseSize());
}
++this->cleanupFileStoreCounter;
if(!(this->cleanupFileStoreCounter % 100)) {
this->cleanupFileStore();
}
}
return(true);
}
pcap_file_store *pcap_store_queue::findFileStoreById(u_int id) {
pcap_file_store *fileStore = NULL;
this->lock_fileStore();
for(size_t i = 0; i < this->fileStore.size(); i++) {
if(this->fileStore[i]->id == id) {
fileStore = this->fileStore[i];
break;
}
}
this->unlock_fileStore();
return(fileStore);
}
void pcap_store_queue::cleanupFileStore() {
this->lock_fileStore();
while(this->fileStore.size()) {
pcap_file_store *fileStore = this->fileStore.front();
if(fileStore->isForDestroy()) {
delete fileStore;
this->fileStore.pop_front();
} else {
break;
}
}
this->unlock_fileStore();
}
uint64_t pcap_store_queue::getFileStoreUseSize(bool lock) {
if(lock) {
this->lock_fileStore();
}
uint64_t size = 0;
size_t itemsInFileStore = this->fileStore.size();
for(size_t i = 0; i < itemsInFileStore; i++) {
size += this->fileStore[i]->fileSize;
}
if(lock) {
this->unlock_fileStore();
}
return(size);
}
PcapQueue::PcapQueue(eTypeQueue typeQueue, const char *nameQueue) {
this->typeQueue = typeQueue;
this->nameQueue = nameQueue;
this->threadHandle = 0;
this->writeThreadHandle = 0;
this->enableWriteThread = false;
this->enableAutoTerminate = true;
this->fifoReadHandle = -1;
this->fifoWriteHandle = -1;
this->threadInitOk = false;
this->threadInitFailed = false;
this->writeThreadInitOk = false;
this->threadTerminated = false;
this->writeThreadTerminated = false;
this->threadDoTerminate = false;
this->mainThreadId = 0;
this->writeThreadId = 0;
for(int i = 0; i < PCAP_QUEUE_NEXT_THREADS_MAX; i++) {
this->nextThreadsId[i] = 0;
}
memset(this->mainThreadPstatData, 0, sizeof(this->mainThreadPstatData));
memset(this->writeThreadPstatData, 0, sizeof(this->writeThreadPstatData));
for(int i = 0; i < PCAP_QUEUE_NEXT_THREADS_MAX; i++) {
memset(this->nextThreadsPstatData[i], 0, sizeof(this->nextThreadsPstatData[i]));
}
memset(this->procPstatData, 0, sizeof(this->procPstatData));
this->packetBuffer = NULL;
this->instancePcapHandle = NULL;
this->initAllReadThreadsFinished = false;
this->counter_calls_old = 0;
this->counter_sip_packets_old[0] = 0;
this->counter_sip_packets_old[1] = 0;
this->counter_sip_register_packets_old = 0;
this->counter_sip_message_packets_old = 0;
this->counter_rtp_packets_old = 0;
this->counter_all_packets_old = 0;
}
PcapQueue::~PcapQueue() {
if(this->fifoReadHandle >= 0) {
close(this->fifoReadHandle);
}
if(this->fifoWriteHandle >= 0) {
close(this->fifoWriteHandle);
syslog(LOG_NOTICE, "packetbuffer terminating (%s): close fifoWriteHandle", nameQueue.c_str());
}
if(this->packetBuffer) {
delete [] this->packetBuffer;
syslog(LOG_NOTICE, "packetbuffer terminating (%s): free packetBuffer", nameQueue.c_str());
}
}
void PcapQueue::setFifoFileForRead(const char *fifoFileForRead) {
this->fifoFileForRead = fifoFileForRead;
}
void PcapQueue::setFifoFileForWrite(const char *fifoFileForWrite) {
this->fifoFileForWrite = fifoFileForWrite;
}
void PcapQueue::setFifoReadHandle(int fifoReadHandle) {
this->fifoReadHandle = fifoReadHandle;
}
void PcapQueue::setFifoWriteHandle(int fifoWriteHandle) {