Repository navigation
Expand file tree
/
Copy pathfilter_mysql.cpp
More file actions
1384 lines (1230 loc) · 49.7 KB
/
Copy pathfilter_mysql.cpp
File metadata and controls
1384 lines (1230 loc) · 49.7 KB
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 <syslog.h>
#include <string.h>
#include "voipmonitor.h"
#include "filter_mysql.h"
#include "calltable.h"
#include "odbc.h"
#include "sql_db.h"
#include <math.h>
#include <vector>
extern bool selectSensorsContainSensorId(const string &select_sensors, int sensor_id);
using namespace std;
extern int opt_nocdr;
extern int opt_id_sensor;
extern SensorsMap sensorsMap;
bool isStringNull(string &str) {
return(str == "" || str == "\\N");
}
/* filter_base class */
void filter_base::freeFilterMap(std::map<int, filter_base*> *&filter_map) {
if(filter_map) {
for(std::map<int, filter_base*>::iterator iter = filter_map->begin(); iter != filter_map->end(); iter++) {
delete iter->second;
}
delete filter_map;
filter_map = NULL;
}
}
void filter_base::load(std::map<int, filter_base*> *&filter_map, u_int32_t *global_flags, SqlDb *sqlDb) {
freeFilterMap(filter_map);
// the rules from the file (if configured) must precede the db rules (a duplicate rule overwrites the item)
loadFile(global_flags);
if(!cFilters::useDbRules()) {
return;
}
loadDbBySensors(filter_map, global_flags, sqlDb);
if(sverb.capture_filter) {
syslog(LOG_NOTICE, "%s: load", getDbTable());
}
}
// one select of the enabled rules of the table - with the sensors_id column (group_concat of the <table>_sensors
// rows) if that table exists; every row is parsed once and distributed to the sets
void filter_base::loadDbBySensors(std::map<int, filter_base*> *&filter_map, u_int32_t *global_flags, SqlDb *sqlDb) {
bool _createSqlObject = false;
if(!sqlDb) {
sqlDb = createSqlObject();
_createSqlObject = true;
}
string table = getDbTable();
string sensors_table = table + "_sensors";
bool existsSensorsTable = sqlDb->existsTable(sensors_table);
sqlDb->query("SELECT " + table + ".*" +
(existsSensorsTable ?
",(select group_concat(coalesce(sensor_id, -2)) from " + sensors_table +
" where " + table + "_id = " + table + ".id) as sensors_id" :
string()) +
" FROM " + table + " where enabled = 1" + getDbOrderBy());
SqlDb_rows rows;
sqlDb->fetchRows(&rows);
if(is_server() && existsSensorsTable) {
createFilterMapBySensors(&rows, filter_map);
}
SqlDb_row row;
while((row = rows.fetchRow())) {
filter_db_row_base *dbRow = parseDbRow(&row);
addDbRowBySensors(filter_map, row["sensors_id"], dbRow, global_flags);
delete dbRow;
}
if(_createSqlObject) {
delete sqlDb;
}
}
// the per-sensor sets exist only for the sensors referenced by the sensors_id of a row which does not pass for the
// local sensor (the sensors.id of the <table>_sensors rows, -1: all sensors, -2: null sensor_id); a sensor without
// a pinned rule and the local sensor use only the default set
void filter_base::createFilterMapBySensors(SqlDb_rows *rows, std::map<int, filter_base*> *&filter_map) {
std::set<int> sensorsTableId;
SqlDb_row row;
while((row = rows->fetchRow())) {
string sensors_id = row["sensors_id"];
if(!selectSensorsContainSensorId(sensors_id, 0)) {
vector<int> tableIds = split2int(sensors_id, ',');
sensorsTableId.insert(tableIds.begin(), tableIds.end());
}
}
rows->initFetch();
std::set<int> sensors;
sensorsMap.getSensorsIdByTableId(sensorsTableId, &sensors);
sensors.erase(opt_id_sensor);
if(sensors.empty()) {
return;
}
filter_map = new FILE_LINE(0) std::map<int, filter_base*>;
for(std::set<int>::iterator iter = sensors.begin(); iter != sensors.end(); iter++) {
(*filter_map)[*iter] = createInstance();
}
}
// hand one parsed db row to the sets: a row which passes for the local sensor (sensor_id 0; an empty sensors_id
// means all sensors) goes to the default set only, any other row to the per-sensor sets of its sensors -
// add_call_flags evaluates the per-sensor set (only if cFilters::isClientPacketOnServer) and then the default set;
// the caller holds lock() or lock_reload()
void filter_base::addDbRowBySensors(std::map<int, filter_base*> *filter_map, const string &sensors_id, filter_db_row_base *dbRow, u_int32_t *global_flags) {
if(selectSensorsContainSensorId(sensors_id, 0)) {
add_db_row(dbRow, global_flags);
} else if(filter_map) {
for(std::map<int, filter_base*>::iterator iter = filter_map->begin(); iter != filter_map->end(); iter++) {
if(selectSensorsContainSensorId(sensors_id, iter->first)) {
iter->second->add_db_row(dbRow, global_flags);
}
}
}
}
filter_base *filter_base::selectFilterBySensor(std::map<int, filter_base*> *filter_map, int sensor_id) {
if(filter_map) {
std::map<int, filter_base*>::iterator iter = filter_map->find(sensor_id);
if(iter != filter_map->end()) {
return(iter->second);
}
}
return(NULL);
}
void filter_base::dumpFilterMapBySensor(std::map<int, filter_base*> *filter_map, ostringstream &oss) {
if(filter_map) {
for(std::map<int, filter_base*>::iterator iter = filter_map->begin(); iter != filter_map->end(); iter++) {
oss << dec << "sensor " << iter->first << ":" << endl;
iter->second->_dump2man(oss);
}
}
}
string filter_base::_string(SqlDb_row *sqlRow, map<string, string> *row, const char *column) {
return(sqlRow ?
(*sqlRow)[column].c_str() :
(*row)[column].c_str());
}
bool filter_base::_value_is_null(SqlDb_row *sqlRow, map<string, string> *row, const char *column) {
return(sqlRow ?
sqlRow->isNull(column) :
isStringNull((*row)[column]));
}
int filter_base::_value(SqlDb_row *sqlRow, map<string, string> *row, const char *column) {
return(atoi(sqlRow ?
(*sqlRow)[column].c_str() :
(*row)[column].c_str()));
}
void filter_base::_loadBaseDataRow(SqlDb_row *sqlRow, map<string, string> *row, filter_db_row_base *baseRow) {
baseRow->direction = _value_is_null(sqlRow, row, "direction") ? 0 : _value(sqlRow, row, "direction");
baseRow->rtp = _value_is_null(sqlRow, row, "rtp") ? -1 : _value(sqlRow, row, "rtp");
baseRow->rtp_video = _value_is_null(sqlRow, row, "rtp_video") ? -1 : _value(sqlRow, row, "rtp_video");
baseRow->mrcp = _value_is_null(sqlRow, row, "mrcp") ? -1 : _value(sqlRow, row, "mrcp");
baseRow->bfcp = _value_is_null(sqlRow, row, "bfcp") ? -1 : _value(sqlRow, row, "bfcp");
baseRow->rtcp = _value_is_null(sqlRow, row, "rtcp") ? -1 : _value(sqlRow, row, "rtcp");
baseRow->sip = _value_is_null(sqlRow, row, "sip") ? -1 : _value(sqlRow, row, "sip");
baseRow->reg = _value_is_null(sqlRow, row, "register") ? -1 : _value(sqlRow, row, "register");
baseRow->dtmf = _value_is_null(sqlRow, row, "dtmf") ? -1 : _value(sqlRow, row, "dtmf");
baseRow->graph = _value_is_null(sqlRow, row, "graph") ? -1 : _value(sqlRow, row, "graph");
baseRow->wav = _value_is_null(sqlRow, row, "wav") ? -1 : _value(sqlRow, row, "wav");
baseRow->audio_transcribe = _value_is_null(sqlRow, row, "audio_transcribe") ? -1 : _value(sqlRow, row, "audio_transcribe");
baseRow->audiograph = _value_is_null(sqlRow, row, "audiograph") ? -1 : _value(sqlRow, row, "audiograph");
baseRow->skip = _value_is_null(sqlRow, row, "skip") ? -1 : _value(sqlRow, row, "skip");
baseRow->script = _value_is_null(sqlRow, row, "script") ? -1 : _value(sqlRow, row, "script");
baseRow->mos_lqo = _value_is_null(sqlRow, row, "mos_lqo") ? -1 : _value(sqlRow, row, "mos_lqo");
baseRow->hide_message = _value_is_null(sqlRow, row, "hide_message") ? -1 : _value(sqlRow, row, "hide_message");
baseRow->spool_2 = _value_is_null(sqlRow, row, "spool_2") ? -1 : _value(sqlRow, row, "spool_2");
baseRow->options = _value_is_null(sqlRow, row, "options") ? -1 : _value(sqlRow, row, "options");
baseRow->notify = _value_is_null(sqlRow, row, "notify") ? -1 : _value(sqlRow, row, "notify");
baseRow->subscribe = _value_is_null(sqlRow, row, "subscribe") ? -1 : _value(sqlRow, row, "subscribe");
baseRow->natalias = _string(sqlRow, row, "natalias");
baseRow->natalias_inheritance = _value(sqlRow, row, "natalias_inheritance");
}
void filter_base::loadBaseDataRow(SqlDb_row *sqlRow, filter_db_row_base *baseRow) {
_loadBaseDataRow(sqlRow, NULL, baseRow);
}
void filter_base::loadBaseDataRow(map<string, string> *row, filter_db_row_base *baseRow) {
_loadBaseDataRow(NULL, row, baseRow);
}
u_int64_t filter_base::getFlagsFromBaseData(filter_db_row_base *baseRow, u_int32_t *global_flags) {
u_int64_t flags = 0;
if(baseRow->rtp == 1) flags |= CAPT_FLAG(_CAPT_BIT_RTP_ALL);
else if(baseRow->rtp == 2) flags |= CAPT_FLAG(_CAPT_BIT_RTP_HEADER);
else if(baseRow->rtp == 0) flags |= CAPT_FLAG(_CAPT_BIT_NORTP);
if(baseRow->rtp_video == 1) flags |= CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_ALL);
else if(baseRow->rtp_video == 2) flags |= CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_HEADER);
else if(baseRow->rtp_video == 3) flags |= CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_CDR_ONLY);
else if(baseRow->rtp_video == 0) flags |= CAPT_FLAG(_CAPT_BIT_NORTP_VIDEO);
if(baseRow->mrcp == 1) { flags |= CAPT_FLAG(_CAPT_BIT_MRCP); *global_flags |= cFilters::_gf_mrcp; }
else if(baseRow->mrcp == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOMRCP);
if(baseRow->bfcp == 1) { flags |= CAPT_FLAG(_CAPT_BIT_BFCP); *global_flags |= cFilters::_gf_bfcp; }
else if(baseRow->bfcp == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOBFCP);
if(baseRow->rtcp == 1) flags |= CAPT_FLAG(_CAPT_BIT_RTCP);
else if(baseRow->rtcp == 0) flags |= CAPT_FLAG(_CAPT_BIT_NORTCP);
if(baseRow->sip == 1) flags |= CAPT_FLAG(_CAPT_BIT_SIP);
else if(baseRow->sip == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOSIP);
if (baseRow->reg == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOREGISTER_DB) | CAPT_FLAG(_CAPT_BIT_NOREGISTER_PCAP);
else if (baseRow->reg == 1) flags |= CAPT_FLAG(_CAPT_BIT_REGISTER_DB) | CAPT_FLAG(_CAPT_BIT_REGISTER_PCAP);
else if (baseRow->reg == 2) flags |= CAPT_FLAG(_CAPT_BIT_REGISTER_DB) | CAPT_FLAG(_CAPT_BIT_NOREGISTER_PCAP);
else if (baseRow->reg == 3) flags |= CAPT_FLAG(_CAPT_BIT_NOREGISTER_DB) | CAPT_FLAG(_CAPT_BIT_REGISTER_PCAP);
if(baseRow->dtmf == 1) flags |= CAPT_FLAG(_CAPT_BIT_DTMF_DB) | CAPT_FLAG(_CAPT_BIT_DTMF_PCAP);
else if(baseRow->dtmf == 0) flags |= CAPT_FLAG(_CAPT_BIT_NODTMF_DB) | CAPT_FLAG(_CAPT_BIT_NODTMF_PCAP);
else if(baseRow->dtmf == 2) flags |= CAPT_FLAG(_CAPT_BIT_DTMF_DB) | CAPT_FLAG(_CAPT_BIT_NODTMF_PCAP);
else if(baseRow->dtmf == 3) flags |= CAPT_FLAG(_CAPT_BIT_DTMF_PCAP) | CAPT_FLAG(_CAPT_BIT_NODTMF_DB);
if(baseRow->graph == 1) flags |= CAPT_FLAG(_CAPT_BIT_GRAPH);
else if(baseRow->graph == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOGRAPH);
if(baseRow->wav == 1) flags |= CAPT_FLAG(_CAPT_BIT_AUDIO);
else if(baseRow->wav == 2) flags |= CAPT_FLAG(_CAPT_BIT_AUDIO_WAV);
else if(baseRow->wav == 3) flags |= CAPT_FLAG(_CAPT_BIT_AUDIO_OGG);
else if(baseRow->wav == 4) flags |= CAPT_FLAG(
#if HAVE_LIBLAME && HAVE_LIBMPG123
_CAPT_BIT_AUDIO_MP3
#else
_CAPT_BIT_AUDIO_OGG
#endif
);
else if(baseRow->wav == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOWAV);
if(baseRow->audio_transcribe == 1) flags |= CAPT_FLAG(_CAPT_BIT_AUDIO_TRANSCRIBE);
else if(baseRow->audio_transcribe == 0) flags |= CAPT_FLAG(_CAPT_BIT_NO_AUDIO_TRANSCRIBE);
if(baseRow->audiograph == 1) flags |= CAPT_FLAG(_CAPT_BIT_AUDIOGRAPH);
else if(baseRow->audiograph == 0) flags |= CAPT_FLAG(_CAPT_BIT_NO_AUDIOGRAPH);
if(baseRow->skip == 1) flags |= CAPT_FLAG(_CAPT_BIT_SKIP);
else if(baseRow->skip == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOSKIP);
if(baseRow->script == 1) flags |= CAPT_FLAG(_CAPT_BIT_SCRIPT);
else if(baseRow->script == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOSCRIPT);
if(baseRow->mos_lqo == 1) flags |= CAPT_FLAG(_CAPT_BIT_AMOSLQO);
else if(baseRow->mos_lqo == 2) flags |= CAPT_FLAG(_CAPT_BIT_BMOSLQO);
else if(baseRow->mos_lqo == 3) flags |= CAPT_FLAG(_CAPT_BIT_ABMOSLQO);
else if(baseRow->mos_lqo == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOMOSLQO);
if(baseRow->hide_message == 1) flags |= CAPT_FLAG(_CAPT_BIT_HIDEMSG);
else if(baseRow->hide_message == 0) flags |= CAPT_FLAG(_CAPT_BIT_SHOWMSG);
if(baseRow->spool_2 == 1) flags |= CAPT_FLAG(_CAPT_BIT_SPOOL_2_SET);
else if(baseRow->spool_2 == 0) flags |= CAPT_FLAG(_CAPT_BIT_SPOOL_2_UNSET);
if (baseRow->options == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOOPTIONS_DB) | CAPT_FLAG(_CAPT_BIT_NOOPTIONS_PCAP);
else if (baseRow->options == 1) flags |= CAPT_FLAG(_CAPT_BIT_OPTIONS_DB) | CAPT_FLAG(_CAPT_BIT_OPTIONS_PCAP);
else if (baseRow->options == 2) flags |= CAPT_FLAG(_CAPT_BIT_OPTIONS_DB) | CAPT_FLAG(_CAPT_BIT_NOOPTIONS_PCAP);
else if (baseRow->options == 3) flags |= CAPT_FLAG(_CAPT_BIT_NOOPTIONS_DB) | CAPT_FLAG(_CAPT_BIT_OPTIONS_PCAP);
if (baseRow->notify == 0) flags |= CAPT_FLAG(_CAPT_BIT_NONOTIFY_DB) | CAPT_FLAG(_CAPT_BIT_NONOTIFY_PCAP);
else if (baseRow->notify == 1) flags |= CAPT_FLAG(_CAPT_BIT_NOTIFY_DB) | CAPT_FLAG(_CAPT_BIT_NOTIFY_PCAP);
else if (baseRow->notify == 2) flags |= CAPT_FLAG(_CAPT_BIT_NOTIFY_DB) | CAPT_FLAG(_CAPT_BIT_NONOTIFY_PCAP);
else if (baseRow->notify == 3) flags |= CAPT_FLAG(_CAPT_BIT_NONOTIFY_DB) | CAPT_FLAG(_CAPT_BIT_NOTIFY_PCAP);
if (baseRow->subscribe == 0) flags |= CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_DB) | CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_PCAP);
else if (baseRow->subscribe == 1) flags |= CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_DB) | CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_PCAP);
else if (baseRow->subscribe == 2) flags |= CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_DB) | CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_PCAP);
else if (baseRow->subscribe == 3) flags |= CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_DB) | CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_PCAP);
return(flags);
}
void filter_base::parseNatAliases(filter_db_row_base *baseRow, sNatAliases **nat_aliases) {
if(!baseRow->natalias.empty()) {
*nat_aliases = new FILE_LINE(0) sNatAliases;
vector<string> nat_aliases_str = split(baseRow->natalias, '\n');
for(unsigned i = 0; i < nat_aliases_str.size(); i++) {
vmIP ip_nat[2];
const char *ip_nat_2_str;
if(ip_nat[0].setFromString(nat_aliases_str[i].c_str(), &ip_nat_2_str)) {
while(*ip_nat_2_str == ' ' || *ip_nat_2_str == '\t' || *ip_nat_2_str == ':' || *ip_nat_2_str == '=') {
++ip_nat_2_str;
}
if(ip_nat[1].setFromString(ip_nat_2_str, NULL)) {
(*nat_aliases)->add(ip_nat[0], ip_nat[1]);
}
}
}
if(!(*nat_aliases)->isSet()) {
delete *nat_aliases;
*nat_aliases = NULL;
}
}
}
void filter_base::setCallFlagsFromFilterFlags(volatile unsigned long int *callFlags, u_int64_t filterFlags, bool reconfigure) {
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SIP)) *callFlags |= FLAG_SAVESIP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOSIP)) *callFlags &= ~FLAG_SAVESIP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTP_ALL)) {*callFlags |= FLAG_SAVERTP; *callFlags &= ~FLAG_SAVERTPHEADER;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTP_HEADER)) {*callFlags |= FLAG_SAVERTPHEADER; *callFlags &= ~FLAG_SAVERTP;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NORTP)) {*callFlags &= ~FLAG_SAVERTP; *callFlags &= ~FLAG_SAVERTPHEADER;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_ALL)) {*callFlags |= (FLAG_SAVERTP_VIDEO | FLAG_PROCESSING_RTP_VIDEO); *callFlags &= ~FLAG_SAVERTP_VIDEO_HEADER;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_HEADER)) {*callFlags |= (FLAG_SAVERTP_VIDEO_HEADER | FLAG_PROCESSING_RTP_VIDEO); *callFlags &= ~FLAG_SAVERTP_VIDEO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTP_VIDEO_CDR_ONLY)) {*callFlags |= FLAG_PROCESSING_RTP_VIDEO; *callFlags &= ~(FLAG_SAVERTP_VIDEO | FLAG_SAVERTP_VIDEO_HEADER);}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NORTP_VIDEO)) {*callFlags &= ~(FLAG_SAVERTP_VIDEO | FLAG_SAVERTP_VIDEO_HEADER | FLAG_PROCESSING_RTP_VIDEO);}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_MRCP)) *callFlags |= FLAG_SAVEMRCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOMRCP)) *callFlags &= ~FLAG_SAVEMRCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_BFCP)) *callFlags |= FLAG_SAVEBFCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOBFCP)) *callFlags &= ~FLAG_SAVEBFCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_RTCP)) *callFlags |= FLAG_SAVERTCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NORTCP)) *callFlags &= ~FLAG_SAVERTCP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_REGISTER_DB)) *callFlags |= FLAG_SAVEREGISTERDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOREGISTER_DB)) *callFlags &= ~FLAG_SAVEREGISTERDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_REGISTER_PCAP)) *callFlags |= FLAG_SAVEREGISTERPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOREGISTER_PCAP)) *callFlags &= ~FLAG_SAVEREGISTERPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_DTMF_DB)) *callFlags |= FLAG_SAVEDTMFDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NODTMF_DB)) *callFlags &= ~FLAG_SAVEDTMFDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_DTMF_PCAP)) *callFlags |= FLAG_SAVEDTMFPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NODTMF_PCAP)) *callFlags &= ~FLAG_SAVEDTMFPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIO)) *callFlags |= FLAG_SAVEAUDIO;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIO_WAV)) {*callFlags &= ~(FLAG_FORMATAUDIO_OGG|FLAG_SAVEAUDIO_MP3); *callFlags |= FLAG_SAVEAUDIO_WAV|FLAG_SAVEAUDIO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIO_OGG)) {*callFlags &= ~(FLAG_FORMATAUDIO_WAV|FLAG_SAVEAUDIO_MP3); *callFlags |= FLAG_SAVEAUDIO_OGG|FLAG_SAVEAUDIO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIO_MP3)) {*callFlags &= ~(FLAG_FORMATAUDIO_WAV|FLAG_SAVEAUDIO_OGG); *callFlags |= FLAG_SAVEAUDIO_MP3|FLAG_SAVEAUDIO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOWAV)) *callFlags &= ~FLAG_SAVEAUDIO;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIO_TRANSCRIBE)) *callFlags |= FLAG_AUDIOTRANSCRIBE;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NO_AUDIO_TRANSCRIBE)) *callFlags &= ~FLAG_AUDIOTRANSCRIBE;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AUDIOGRAPH)) *callFlags |= FLAG_SAVEAUDIOGRAPH;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NO_AUDIOGRAPH)) *callFlags &= ~FLAG_SAVEAUDIOGRAPH;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_GRAPH)) *callFlags |= FLAG_SAVEGRAPH;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOGRAPH)) *callFlags &= ~FLAG_SAVEGRAPH;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SKIP)) *callFlags |= FLAG_SKIPCDR;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOSKIP)) *callFlags &= ~FLAG_SKIPCDR;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SCRIPT)) *callFlags |= FLAG_RUNSCRIPT;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOSCRIPT)) *callFlags &= ~FLAG_RUNSCRIPT;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_AMOSLQO)) {*callFlags |= FLAG_RUNAMOSLQO; *callFlags &= ~FLAG_RUNBMOSLQO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_BMOSLQO)) {*callFlags |= FLAG_RUNBMOSLQO; *callFlags &= ~FLAG_RUNAMOSLQO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_ABMOSLQO)) {*callFlags |= FLAG_RUNAMOSLQO|FLAG_RUNBMOSLQO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOMOSLQO)) {*callFlags &= ~FLAG_RUNAMOSLQO; *callFlags &= ~FLAG_RUNBMOSLQO;}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_HIDEMSG)) *callFlags |= FLAG_HIDEMESSAGE;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SHOWMSG)) *callFlags &= ~FLAG_HIDEMESSAGE;
if(!reconfigure) {
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SPOOL_2_SET)) *callFlags |= FLAG_USE_SPOOL_2;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SPOOL_2_UNSET)) *callFlags &= ~FLAG_USE_SPOOL_2;
}
if(filterFlags & CAPT_FLAG(_CAPT_BIT_OPTIONS_DB)) *callFlags |= FLAG_SAVEOPTIONSDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOOPTIONS_DB)) *callFlags &= ~FLAG_SAVEOPTIONSDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_OPTIONS_PCAP)) *callFlags |= FLAG_SAVEOPTIONSPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOOPTIONS_PCAP)) *callFlags &= ~FLAG_SAVEOPTIONSPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOTIFY_DB)) *callFlags |= FLAG_SAVENOTIFYDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NONOTIFY_DB)) *callFlags &= ~FLAG_SAVENOTIFYDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOTIFY_PCAP)) *callFlags |= FLAG_SAVENOTIFYPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NONOTIFY_PCAP)) *callFlags &= ~FLAG_SAVENOTIFYPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_DB)) *callFlags |= FLAG_SAVESUBSCRIBEDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_DB)) *callFlags &= ~FLAG_SAVESUBSCRIBEDB;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_SUBSCRIBE_PCAP)) *callFlags |= FLAG_SAVESUBSCRIBEPCAP;
if(filterFlags & CAPT_FLAG(_CAPT_BIT_NOSUBSCRIBE_PCAP)) *callFlags &= ~FLAG_SAVESUBSCRIBEPCAP;
}
/* IPfilter class */
// constructor
IPfilter::IPfilter() {
first_node = NULL;
count = 0;
};
// destructor
IPfilter::~IPfilter() {
t_node *node = first_node;
while(node != NULL) {
t_node *node_next = node->next;
delete node;
node = node_next;
}
};
filter_db_row_base *IPfilter::parseDbRow(SqlDb_row *row) {
db_row *dbRow = new FILE_LINE(0) db_row;
dbRow->ip.setIP(row, "ip");
dbRow->mask = atoi((*row)["mask"].c_str());
loadBaseDataRow(row, dbRow);
return(dbRow);
}
void IPfilter::add_db_row(filter_db_row_base *dbRow_, u_int32_t *global_flags) {
db_row *dbRow = (db_row*)dbRow_;
count++;
t_node *node = new FILE_LINE(0) t_node;
node->direction = dbRow->direction;
node->network = dbRow->ip.network(dbRow->mask);
node->mask = dbRow->mask;
node->flags = this->getFlagsFromBaseData(dbRow, global_flags);
this->parseNatAliases(dbRow, &node->nat_aliases);
node->nat_aliases_inheritance = dbRow->natalias_inheritance;
// add node to the first position
node->next = first_node;
first_node = node;
}
int IPfilter::_add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, vmIP saddr, vmIP daddr, bool reconfigure) {
if (this->count == 0) {
// no filters, return
return 0;
}
int last_mask = 0;
char found = 0;
map<int, t_node*> nat_aliases_node_mask;
t_node *node;
for(node = first_node; node != NULL; node = node->next) {
if((!node->network.isSet() && !node->mask) ||
((node->direction == 0 or node->direction == 2) and (daddr.network(node->mask) == node->network)) ||
((node->direction == 0 or node->direction == 1) and (saddr.network(node->mask) == node->network))) {
int mask = node->network.isSet() && !node->mask ? node->network.bits() : node->mask;
if(mask < last_mask) {
if(node->nat_aliases) {
nat_aliases_node_mask[node->mask] = node;
}
continue;
}
last_mask = mask;
this->setCallFlagsFromFilterFlags(flags, node->flags, reconfigure);
if(node->nat_aliases) {
nat_aliases_node_mask[mask] = node;
}
found = 1;
}
}
if(nat_aliases_node_mask.size()) {
unsigned counter = 0;
for(map<int, t_node*>::reverse_iterator iter = nat_aliases_node_mask.rbegin(); iter != nat_aliases_node_mask.rend(); iter++) {
if((!counter || iter->second->nat_aliases_inheritance) && nat_aliases) {
if(!*nat_aliases) {
*nat_aliases = new FILE_LINE(0) sNatAliases;
}
(*nat_aliases)->add(iter->second->nat_aliases);
}
++counter;
}
}
return found;
}
void IPfilter::_dump2man(ostringstream &oss) {
t_node *node;
for(node = first_node; node != NULL; node = node->next) {
oss << "ip[" << node->network.getString() << "/" << node->mask << "] direction[" << node->direction << "] flags[0x" << hex << node->flags << "]" << endl;
}
}
void IPfilter::dump2man(ostringstream &oss) {
lock();
if(filter_active) {
filter_active->_dump2man(oss);
}
dumpFilterMapBySensor(filter_active_by_sensor, oss);
unlock();
}
int IPfilter::add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, vmIP saddr, vmIP daddr, int sensor_id, bool reconfigure) {
int rslt = 0;
lock();
if(cFilters::isClientPacketOnServer(sensor_id)) {
IPfilter *filter_sensor = (IPfilter*)selectFilterBySensor(filter_active_by_sensor, sensor_id);
if(filter_sensor) {
rslt = filter_sensor->_add_call_flags(flags, nat_aliases, saddr, daddr, reconfigure);
}
}
if(filter_active) {
rslt |= filter_active->_add_call_flags(flags, nat_aliases, saddr, daddr, reconfigure);
}
unlock();
return(rslt);
}
void IPfilter::loadActive(u_int32_t *global_flags, SqlDb *sqlDb) {
lock();
filter_active = new FILE_LINE(4002) IPfilter();
filter_active->load(filter_active_by_sensor, global_flags, sqlDb);
unlock();
}
void IPfilter::freeActive() {
lock();
if(filter_active) {
delete filter_active;
filter_active = NULL;
}
freeFilterMap(filter_active_by_sensor);
unlock();
}
void IPfilter::prepareReload(u_int32_t *global_flags, SqlDb *sqlDb) {
lock_reload();
if(filter_reload) {
delete filter_reload;
}
filter_reload = new FILE_LINE(4003) IPfilter;
filter_reload->load(filter_reload_by_sensor, global_flags, sqlDb);
reload_do = true;
syslog(LOG_NOTICE, "IPfilter::prepareReload");
unlock_reload();
}
void IPfilter::applyReload() {
if(reload_do) {
lock_reload();
if(reload_do) {
lock();
IPfilter *filter_active_old = filter_active;
std::map<int, filter_base*> *filter_active_by_sensor_old = filter_active_by_sensor;
filter_active = filter_reload;
filter_active_by_sensor = filter_reload_by_sensor;
unlock();
// free the previous sets outside the lock of the packet path
delete filter_active_old;
freeFilterMap(filter_active_by_sensor_old);
filter_reload = NULL;
filter_reload_by_sensor = NULL;
reload_do = false;
syslog(LOG_NOTICE, "IPfilter::applyReload");
}
unlock_reload();
}
}
IPfilter *IPfilter::filter_active = NULL;
IPfilter *IPfilter::filter_reload = NULL;
std::map<int, filter_base*> *IPfilter::filter_active_by_sensor = NULL;
std::map<int, filter_base*> *IPfilter::filter_reload_by_sensor = NULL;
volatile bool IPfilter::reload_do = 0;
volatile int IPfilter::_sync = 0;
volatile int IPfilter::_sync_reload = 0;
/* TELNUMfilter class */
// constructor
TELNUMfilter::TELNUMfilter() {
first_node = new FILE_LINE(0) t_node_tel;
first_node->payload = NULL;
for(int i = 0; i < 256; i++) {
first_node->nodes[i] = NULL;
}
count = 0;
};
// destructor
TELNUMfilter::~TELNUMfilter() {
// je nutne uvolnit t_payload strukturu a t_node
// algoritmus musi projit strom a postupne odmazavat za pomoci fronty
// (prohledavani do sirky)
//
// do fronty se prida first_node
// DO WHILE (dokud neni prazdna fronta )
// pro kazdy prvek se projdou naslednici a zaradi se nakonec fronty
// zpracovavany t_node se odstrani ze predu fronty a uvolni se pamet
// ENDWHILE
deque<t_node_tel*> fronta;
fronta.push_back(first_node);
t_node_tel *node;
while( !fronta.empty() ) {
node = fronta.front();
if(node->payload) {
// nektera cisla nemaji payload
delete(node->payload);
}
for(int i = 0; i < 256; i++) {
if(node->nodes[i]) {
// vsichni nenulovi naslednici zaradime na konec fronty
fronta.push_back(node->nodes[i]);
}
}
//otce muzeme vymazat z fronty a nasledne dealokovat pamet
fronta.pop_front();
delete(node);
}
for(size_t i = 0; i < wildcard_payloads.size(); i++) {
delete wildcard_payloads[i];
}
wildcard_payloads.clear();
};
void TELNUMfilter::add_payload(t_payload *payload) {
if(strpbrk(payload->prefix, "%_")) {
payload->number_matcher = new FILE_LINE(0) PhoneNumber(payload->prefix, PhoneNumber::_tn_prefix);
// score by literal-character count so it interoperates with the
// trie's found_length (also a literal-character count)
for(const char *p = payload->prefix; *p; p++) {
if(*p != '%') payload->specificity++;
}
wildcard_payloads.push_back(payload);
return;
}
t_node_tel *tmp = first_node;
for(unsigned int i = 0; i < strlen(payload->prefix); i++) {
if(!tmp->nodes[(int)payload->prefix[i]]) {
t_node_tel *node = new FILE_LINE(0) t_node_tel;
node->payload = NULL;
for(int j = 0; j < 256; j++) {
node->nodes[j] = NULL;
}
tmp->nodes[(int)payload->prefix[i]] = node;
}
tmp = tmp->nodes[(int)payload->prefix[i]]; // shift
}
tmp->payload = payload;
};
filter_db_row_base *TELNUMfilter::parseDbRow(SqlDb_row *row) {
db_row *dbRow = new FILE_LINE(0) db_row;
strcpy_null_term(dbRow->prefix, trim_str((*row)["prefix"]).c_str());
loadBaseDataRow(row, dbRow);
return(dbRow);
}
void TELNUMfilter::add_db_row(filter_db_row_base *dbRow_, u_int32_t *global_flags) {
db_row *dbRow = (db_row*)dbRow_;
count++;
t_payload *np = new FILE_LINE(0) t_payload;
np->direction = dbRow->direction;
strcpy_null_term(np->prefix, dbRow->prefix);
np->flags = this->getFlagsFromBaseData(dbRow, global_flags);
this->parseNatAliases(dbRow, &np->nat_aliases);
add_payload(np);
}
void TELNUMfilter::loadFile(u_int32_t *global_flags) {
extern char opt_capture_rules_telnum_file[1024];
if(is_sender() || is_client_packetbuffer_sender() || !opt_capture_rules_telnum_file[0]) {
return;
}
cCsv csv;
csv.setFirstRowContainFieldNames();
csv.load(opt_capture_rules_telnum_file);
unsigned rowsCount = csv.getRowsCount();
for(unsigned i = 1; i <= rowsCount; i++) {
map<string, string> row;
csv.getRow(i, &row);
db_row dbRow;
strcpy_null_term(dbRow.prefix, trim_str(row["prefix"]).c_str());
loadBaseDataRow(&row, &dbRow);
add_db_row(&dbRow, global_flags);
}
}
int TELNUMfilter::_add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, const char *telnum_src, const char *telnum_dst, bool reconfigure) {
if (this->count == 0) {
// no filters, return
return 0;
}
const char *telnum_in[2] = { telnum_src, telnum_dst };
const char *telnum_p[2];
string decoded[2];
unsigned telnum_len[2];
for(int k = 0; k < 2; k++) {
if(strchr(telnum_in[k], '%')) {
unsigned in_len = strlen(telnum_in[k]);
decoded[k].reserve(in_len);
for(unsigned int i = 0; i < in_len; i++) {
if(telnum_in[k][i] == '%' && !strncmp(telnum_in[k] + i, "%23", 3)) {
decoded[k] += '#';
i += 2;
} else {
decoded[k] += telnum_in[k][i];
}
}
telnum_p[k] = decoded[k].c_str();
telnum_len[k] = decoded[k].length();
} else {
telnum_p[k] = telnum_in[k];
telnum_len[k] = strlen(telnum_in[k]);
}
}
unsigned found_length = 0;
u_int64_t found_flags = 0;
sNatAliases *found_nat_aliases = NULL;
for(int src_dst = 1; src_dst <= 2; src_dst++) {
const char *telnum = telnum_p[src_dst - 1];
unsigned telnum_length = telnum_len[src_dst - 1];
t_node_tel *node = first_node;
for(unsigned int i = 0; i < telnum_length; i++) {
unsigned char checkChar = telnum[i];
if(!node->nodes[checkChar]) {
break;
}
node = node->nodes[checkChar];
if(node && node->payload &&
(node->payload->direction == 0 ||
node->payload->direction == src_dst) &&
(i + 1) > found_length) {
found_length = i + 1;
found_flags = node->payload->flags;
found_nat_aliases = node->payload->nat_aliases;
}
}
for(size_t k = 0; k < wildcard_payloads.size(); k++) {
t_payload *p = wildcard_payloads[k];
if(p->direction != 0 && p->direction != src_dst) {
continue;
}
if(p->number_matcher->checkNumber(telnum)) {
if(p->specificity > found_length) {
found_length = p->specificity;
found_flags = p->flags;
found_nat_aliases = p->nat_aliases;
}
}
}
}
if(found_length > 0) {
this->setCallFlagsFromFilterFlags(flags, found_flags, reconfigure);
if(found_nat_aliases && nat_aliases) {
if(!*nat_aliases) {
*nat_aliases = new FILE_LINE(0) sNatAliases;
}
(*nat_aliases)->add(found_nat_aliases);
}
}
return(found_length > 0);
}
void TELNUMfilter::_dump2man(ostringstream &oss) {
dump_trie_node(oss, first_node);
for(size_t i = 0; i < wildcard_payloads.size(); i++) {
dump_payload_line(oss, wildcard_payloads[i], true);
}
}
void TELNUMfilter::dump2man(ostringstream &oss) {
lock();
if(filter_active) {
filter_active->_dump2man(oss);
}
dumpFilterMapBySensor(filter_active_by_sensor, oss);
unlock();
}
void TELNUMfilter::dump_trie_node(ostringstream &oss, t_node_tel *node) {
if(node->payload) {
dump_payload_line(oss, node->payload, false);
}
for(int i = 0; i < 256; i++) {
if(node->nodes[i]) {
dump_trie_node(oss, node->nodes[i]);
}
}
}
void TELNUMfilter::dump_payload_line(ostringstream &oss, t_payload *p, bool wildcard) {
oss << "prefix[" << p->prefix << "] direction[" << p->direction << "] flags[0x" << hex << p->flags << "]";
if(wildcard) {
oss << " wildcard";
}
oss << endl;
}
int TELNUMfilter::add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, const char *telnum_src, const char *telnum_dst, int sensor_id, bool reconfigure) {
int rslt = 0;
lock();
if(cFilters::isClientPacketOnServer(sensor_id)) {
TELNUMfilter *filter_sensor = (TELNUMfilter*)selectFilterBySensor(filter_active_by_sensor, sensor_id);
if(filter_sensor) {
rslt = filter_sensor->_add_call_flags(flags, nat_aliases, telnum_src, telnum_dst, reconfigure);
}
}
if(filter_active) {
rslt |= filter_active->_add_call_flags(flags, nat_aliases, telnum_src, telnum_dst, reconfigure);
}
unlock();
return(rslt);
}
void TELNUMfilter::loadActive(u_int32_t *global_flags, SqlDb *sqlDb) {
lock();
filter_active = new FILE_LINE(4004) TELNUMfilter();
filter_active->load(filter_active_by_sensor, global_flags, sqlDb);
unlock();
}
void TELNUMfilter::freeActive() {
lock();
if(filter_active) {
delete filter_active;
filter_active = NULL;
}
freeFilterMap(filter_active_by_sensor);
unlock();
}
void TELNUMfilter::prepareReload(u_int32_t *global_flags, SqlDb *sqlDb) {
lock_reload();
if(filter_reload) {
delete filter_reload;
}
filter_reload = new FILE_LINE(4005) TELNUMfilter;
filter_reload->load(filter_reload_by_sensor, global_flags, sqlDb);
reload_do = true;
syslog(LOG_NOTICE, "TELNUMfilter::prepareReload");
unlock_reload();
}
void TELNUMfilter::applyReload() {
if(reload_do) {
lock_reload();
if(reload_do) {
lock();
TELNUMfilter *filter_active_old = filter_active;
std::map<int, filter_base*> *filter_active_by_sensor_old = filter_active_by_sensor;
filter_active = filter_reload;
filter_active_by_sensor = filter_reload_by_sensor;
unlock();
// free the previous sets outside the lock of the packet path
delete filter_active_old;
freeFilterMap(filter_active_by_sensor_old);
filter_reload = NULL;
filter_reload_by_sensor = NULL;
reload_do = false;
syslog(LOG_NOTICE, "TELNUMfilter::applyReload");
}
unlock_reload();
}
}
TELNUMfilter *TELNUMfilter::filter_active = NULL;
TELNUMfilter *TELNUMfilter::filter_reload = NULL;
std::map<int, filter_base*> *TELNUMfilter::filter_active_by_sensor = NULL;
std::map<int, filter_base*> *TELNUMfilter::filter_reload_by_sensor = NULL;
volatile bool TELNUMfilter::reload_do = 0;
volatile int TELNUMfilter::_sync = 0;
volatile int TELNUMfilter::_sync_reload = 0;
/* DOMAINfilter class */
// constructor
DOMAINfilter::DOMAINfilter() {
first_node = NULL;
count = 0;
};
// destructor
DOMAINfilter::~DOMAINfilter() {
t_node *node = first_node;
while(node != NULL) {
t_node *node_next = node->next;
delete node;
node = node_next;
}
};
filter_db_row_base *DOMAINfilter::parseDbRow(SqlDb_row *row) {
db_row *dbRow = new FILE_LINE(0) db_row;
dbRow->domain = trim_str((*row)["domain"]);
loadBaseDataRow(row, dbRow);
return(dbRow);
}
void DOMAINfilter::add_db_row(filter_db_row_base *dbRow_, u_int32_t *global_flags) {
db_row *dbRow = (db_row*)dbRow_;
count++;
t_node *node = new FILE_LINE(0) t_node;
node->direction = dbRow->direction;
node->domain = dbRow->domain;
node->flags = this->getFlagsFromBaseData(dbRow, global_flags);
this->parseNatAliases(dbRow, &node->nat_aliases);
// add node to the first position
node->next = first_node;
first_node = node;
}
int
DOMAINfilter::_add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, const char *domain_src, const char *domain_dst, bool reconfigure) {
if (this->count == 0) {
// no filters, return
return 0;
}
t_node *node;
for(node = first_node; node != NULL; node = node->next) {
if(((node->direction == 0 or node->direction == 2) and (domain_dst == node->domain)) ||
((node->direction == 0 or node->direction == 1) and (domain_src == node->domain))) {
this->setCallFlagsFromFilterFlags(flags, node->flags, reconfigure);
if(node->nat_aliases && nat_aliases) {
if(!*nat_aliases) {
*nat_aliases = new FILE_LINE(0) sNatAliases;
}
(*nat_aliases)->add(node->nat_aliases);
}
return 1;
}
}
return 0;
}
void DOMAINfilter::_dump2man(ostringstream &oss) {
t_node *node;
for(node = first_node; node != NULL; node = node->next) {
oss << "domain[" << node->domain << "] direction[" << node->direction << "] flags[0x" << hex << node->flags << "]" << endl;
}
}
void DOMAINfilter::dump2man(ostringstream &oss) {
lock();
if(filter_active) {
filter_active->_dump2man(oss);
}
dumpFilterMapBySensor(filter_active_by_sensor, oss);
unlock();
}
int DOMAINfilter::add_call_flags(volatile unsigned long int *flags, sNatAliases **nat_aliases, const char *domain_src, const char *domain_dst, int sensor_id, bool reconfigure) {
int rslt = 0;
lock();
if(cFilters::isClientPacketOnServer(sensor_id)) {
DOMAINfilter *filter_sensor = (DOMAINfilter*)selectFilterBySensor(filter_active_by_sensor, sensor_id);
if(filter_sensor) {
rslt = filter_sensor->_add_call_flags(flags, nat_aliases, domain_src, domain_dst, reconfigure);
}
}
if(filter_active) {
rslt |= filter_active->_add_call_flags(flags, nat_aliases, domain_src, domain_dst, reconfigure);
}
unlock();
return(rslt);
}
void DOMAINfilter::loadActive(u_int32_t *global_flags, SqlDb *sqlDb) {
lock();
filter_active = new FILE_LINE(4007) DOMAINfilter();
filter_active->load(filter_active_by_sensor, global_flags, sqlDb);
unlock();
}
void DOMAINfilter::freeActive() {
lock();
if(filter_active) {
delete filter_active;
filter_active = NULL;
}
freeFilterMap(filter_active_by_sensor);
unlock();
}
void DOMAINfilter::prepareReload(u_int32_t *global_flags, SqlDb *sqlDb) {
lock_reload();
if(filter_reload) {
delete filter_reload;
}
filter_reload = new FILE_LINE(4008) DOMAINfilter;
filter_reload->load(filter_reload_by_sensor, global_flags, sqlDb);
reload_do = true;
syslog(LOG_NOTICE, "DOMAINfilter::prepareReload");