-
Notifications
You must be signed in to change notification settings - Fork 1
/
calltable.cpp
12052 lines (11369 loc) · 393 KB
/
calltable.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
/* Martin Vit [email protected]
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2.
*/
/**
* This file implements Calltable and Call class. Calltable implements operations
* on Call list. Call class implements operations on one call.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include <math.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <net/if.h>
#include <dirent.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <list>
#include <set>
#include <iterator>
//#include <.h>
#include "voipmonitor.h"
#include "calltable.h"
#include "format_wav.h"
#include "format_ogg.h"
#include "codecs.h"
#include "codec_alaw.h"
#include "codec_ulaw.h"
#include "mos_g729.h"
#include "jitterbuffer/asterisk/time.h"
#include "odbc.h"
#include "sql_db.h"
#include "rtcp.h"
#include "ipaccount.h"
#include "cleanspool.h"
#include "regcache.h"
#include "fraud.h"
#include "billing.h"
#include "tar.h"
#include "filter_mysql.h"
#include "sniff_inline.h"
#include "register.h"
#include "manager.h"
#include "srtp.h"
#include "dtls.h"
#include "filter_call.h"
#include "options.h"
#include "sniff_proc_class.h"
#include "charts.h"
#define MIN(x,y) ((x) < (y) ? (x) : (y))
using namespace std;
extern int verbosity;
extern int verbosityE;
extern int opt_sip_register;
extern int opt_saveRTP;
extern int opt_onlyRTPheader;
extern int opt_saveSIP;
extern int opt_use_libsrtp;
extern int opt_rtcp;
extern int opt_saveRAW; // save RTP payload RAW data?
extern int opt_saveWAV; // save RTP payload RAW data?
extern int opt_saveGRAPH; // save GRAPH data to graph file?
extern FileZipHandler::eTypeCompress opt_gzipGRAPH; // compress GRAPH data to graph file?
extern bool opt_srtp_rtp_decrypt;
extern bool opt_srtp_rtp_audio_decrypt;
extern bool opt_srtp_rtcp_decrypt;
extern int opt_savewav_force;
extern int opt_save_sdp_ipport;
extern int opt_mos_g729;
extern int opt_nocdr;
extern NoStoreCdrRules nocdr_rules;
extern int opt_only_cdr_next;
extern char opt_cachedir[1024];
extern char sql_cdr_table[256];
extern char sql_cdr_table_last30d[256];
extern char sql_cdr_table_last7d[256];
extern char sql_cdr_table_last1d[256];
extern char sql_cdr_next_table[256];
extern char sql_cdr_ua_table[256];
extern char sql_cdr_sip_response_table[256];
extern char sql_cdr_sip_request_table[256];
extern char sql_cdr_reason_table[256];
extern int opt_callend;
extern int opt_id_sensor;
extern int opt_id_sensor_cleanspool;
extern int rtptimeout;
extern int sipwithoutrtptimeout;
extern int absolute_timeout;
extern unsigned int gthread_num;
extern volatile int num_threads_active;
extern int opt_printinsertid;
extern int opt_cdronlyanswered;
extern int opt_cdronlyrtp;
extern int opt_newdir;
extern char opt_keycheck[1024];
extern char opt_convert_char[256];
extern int opt_norecord_dtmf;
extern char opt_silencedtmfseq[16];
extern int opt_pauserecordingdtmf_timeout;
extern char get_customers_pn_query[1024];
extern int opt_saverfc2833;
extern int opt_dscp;
extern int opt_cdrproxy;
extern int opt_messageproxy;
extern int opt_cdr_country_code;
extern int opt_message_country_code;
extern int opt_pcap_dump_tar;
extern struct pcap_stat pcapstat;
extern int opt_filesclean;
extern int opt_allow_zerossrc;
extern int opt_cdr_sip_response_number_max_length;
extern vector<string> opt_cdr_sip_response_reg_remove;
extern int opt_cdr_ua_enable;
extern vector<string> opt_cdr_ua_reg_remove;
extern vector<string> opt_cdr_ua_reg_whitelist;
extern unsigned int graph_delimiter;
extern int opt_mosmin_f2;
extern char opt_mos_lqo_bin[1024];
extern char opt_mos_lqo_ref[1024];
extern char opt_mos_lqo_ref16[1024];
extern int opt_mos_lqo;
extern regcache *regfailedcache;
extern MySqlStore *sqlStore;
extern int global_pcap_dlink;
extern pcap_t *global_pcap_handle;
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_limit_queue_register;
extern Calltable *calltable;
extern int opt_silencedetect;
extern int opt_clippingdetect;
extern CustomHeaders *custom_headers_cdr;
extern CustomHeaders *custom_headers_message;
extern int opt_custom_headers_last_value;
extern bool _save_sip_history;
extern int opt_saveudptl;
extern int opt_rtpip_find_endpoints;
extern rtp_read_thread *rtp_threads;
extern bool opt_rtpmap_by_callerd;
extern bool opt_rtpmap_combination;
extern int opt_register_timeout_disable_save_failed;
extern int opt_rtpfromsdp_onlysip;
extern int opt_rtpfromsdp_onlysip_skinny;
extern int opt_rtp_check_both_sides_by_sdp;
extern int opt_hash_modify_queue_length_ms;
extern int opt_mysql_enable_multiple_rows_insert;
extern int opt_mysql_max_multiple_rows_insert;
extern PreProcessPacket *preProcessPacketCallX[];
extern bool opt_disable_sdp_multiplication_warning;
volatile int calls_counter = 0;
/* probably not used any more */
volatile int registers_counter = 0;
extern char mac[32];
unsigned int last_register_clean = 0;
extern int opt_onewaytimeout;
extern int opt_saveaudio_reversestereo;
extern int opt_saveaudio_stereo;
extern int opt_saveaudio_reversestereo;
extern float opt_saveaudio_oggquality;
extern bool opt_saveaudio_filteripbysipip;
extern bool opt_saveaudio_filter_ext;
extern bool opt_saveaudio_wav_mix;
extern bool opt_saveaudio_from_first_invite;
extern bool opt_saveaudio_afterconnect;
extern int opt_skinny;
extern int opt_enable_fraud;
extern char opt_call_id_alternative[256];
extern char opt_callidmerge_header[128];
extern int opt_sdp_multiplication;
extern int opt_hide_message_content;
extern char opt_hide_message_content_secret[1024];
extern vector<string> opt_message_body_url_reg;
SqlDb *sqlDbSaveCall = NULL;
SqlDb *sqlDbSaveSs7 = NULL;
extern sExistsColumns existsColumns;
extern int opt_pcap_dump_tar_sip_use_pos;
extern int opt_pcap_dump_tar_rtp_use_pos;
extern int opt_pcap_dump_tar_graph_use_pos;
extern unsigned int glob_ssl_calls;
extern bool opt_cdr_partition;
extern int opt_t2_boost;
extern bool opt_time_precision_in_ms;
extern cBilling *billing;
extern cSqlDbData *dbData;
extern char *opt_rtp_stream_analysis_params;
extern bool opt_charts_cache;
extern bool opt_charts_cache_store;
extern bool opt_charts_cache_ip_boost;
extern int terminating_charts_cache;
extern volatile int terminating;
sCallField callFields[] = {
{ cf_callreference, "callreference" },
{ cf_callid, "callid" },
{ cf_calldate, "calldate" },
{ cf_calldate_num, "calldate_num" },
{ cf_lastpackettime, "lastpackettime" },
{ cf_duration, "duration" },
{ cf_connect_duration, "connect_duration" },
{ cf_caller, "caller" },
{ cf_called, "called" },
{ cf_caller_country, "caller_country" },
{ cf_called_country, "called_country" },
{ cf_caller_international, "caller_international" },
{ cf_called_international, "called_international" },
{ cf_callername, "callername" },
{ cf_callerdomain, "callerdomain" },
{ cf_calleddomain, "calleddomain" },
{ cf_calleragent, "calleragent" },
{ cf_calledagent, "calledagent" },
{ cf_callerip, "callerip" },
{ cf_calledip, "calledip" },
{ cf_callerip_country, "callerip_country" },
{ cf_calledip_country, "calledip_country" },
{ cf_sipproxies, "sipproxies" },
{ cf_lastSIPresponseNum, "lastSIPresponseNum" },
{ cf_rtp_src, "rtp_src" },
{ cf_rtp_dst, "rtp_dst" },
{ cf_rtp_src_country, "rtp_src_country" },
{ cf_rtp_dst_country, "rtp_dst_country" },
{ cf_callercodec, "callercodec" },
{ cf_calledcodec, "calledcodec" },
{ cf_src_mosf1, "src_mosf1" },
{ cf_src_mosf2, "src_mosf2" },
{ cf_src_mosAD, "src_mosAD" },
{ cf_dst_mosf1, "dst_mosf1" },
{ cf_dst_mosf2, "dst_mosf2" },
{ cf_dst_mosAD, "dst_mosAD" },
{ cf_src_jitter, "src_jitter" },
{ cf_dst_jitter, "dst_jitter" },
{ cf_src_loss, "src_loss" },
{ cf_dst_loss, "dst_loss" },
{ cf_src_loss_last10sec, "src_loss_last10sec" },
{ cf_dst_loss_last10sec, "dst_loss_last10sec" },
{ cf_id_sensor, "id_sensor" },
{ cf_vlan, "vlan" }
};
Call_abstract::Call_abstract(int call_type, u_int64_t time_us) {
alloc_flag = 1;
type_base = call_type;
type_next = 0;
first_packet_time_us = time_us;
fbasename[0] = 0;
fbasename_safe[0] = 0;
fname_register = 0;
useSensorId = opt_id_sensor;
useDlt = global_pcap_dlink;
useHandle = global_pcap_handle;
flags = 0;
user_data = NULL;
user_data_type = 0;
chunkBuffersCount = 0;
}
bool
Call_abstract::addNextType(int type) {
if(!type_next &&
((type_base == INVITE && type == MESSAGE) ||
(type_base == MESSAGE && type == INVITE))) {
type_next = type;
((Call*)this)->setRtpThreadNum();
return(true);
}
return(false);
}
string
Call_abstract::get_sensordir() {
string sensorDir;
extern int opt_spooldir_by_sensor;
extern int opt_spooldir_by_sensorname;
if((opt_spooldir_by_sensor && useSensorId > 0) ||
opt_spooldir_by_sensorname) {
if(opt_spooldir_by_sensorname) {
extern SensorsMap sensorsMap;
sensorDir = sensorsMap.getSensorNameFile(useSensorId);
} else if(opt_spooldir_by_sensor) {
sensorDir = intToString(useSensorId);
}
}
return(sensorDir);
}
string
Call_abstract::get_pathname(eTypeSpoolFile typeSpoolFile, const char *substSpoolDir) {
if(!force_spool_path.empty()) {
return(force_spool_path);
}
string spoolDir;
string sensorDir;
string typeDir;
spoolDir = substSpoolDir ?
substSpoolDir :
(opt_cachedir[0] ? opt_cachedir : getSpoolDir(typeSpoolFile));
sensorDir = get_sensordir();
struct tm t = time_r(first_packet_time_us);
char timeDir_buffer[100];
if(opt_newdir) {
static volatile int timeDirCache_sync = 0;
static volatile u_int32_t timeDirCache_index[60];
static volatile char timeDirCache_buffer[60][100];
timeDir_buffer[0] = 0;
u_int8_t _time_index_arr[4];
_time_index_arr[0] = t.tm_mon;
_time_index_arr[1] = t.tm_mday;
_time_index_arr[2] = t.tm_hour;
_time_index_arr[3] = t.tm_min;
u_int32_t *_time_index = (u_int32_t*)_time_index_arr;
while(__sync_lock_test_and_set(&timeDirCache_sync, 1));
if(*_time_index == timeDirCache_index[t.tm_min]) {
strcpy_null_term(timeDir_buffer, (const char*)timeDirCache_buffer[t.tm_min]);
}
__sync_lock_release(&timeDirCache_sync);
if(!timeDir_buffer[0]) {
snprintf(timeDir_buffer, sizeof(timeDir_buffer),
"%04d-%02d-%02d/%02d/%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min);
while(__sync_lock_test_and_set(&timeDirCache_sync, 1));
timeDirCache_index[t.tm_min] = *_time_index;
strncpy_null_term((char*)timeDirCache_buffer[t.tm_min], timeDir_buffer, sizeof(timeDir_buffer));
__sync_lock_release(&timeDirCache_sync);
}
} else {
snprintf(timeDir_buffer, sizeof(timeDir_buffer),
"%04d-%02d-%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
}
typeDir = opt_newdir ? getSpoolTypeDir(typeSpoolFile) : "";
return(spoolDir + (spoolDir.length() ? "/" : "") +
sensorDir + (sensorDir.length() ? "/" : "") +
timeDir_buffer + "/" +
typeDir + (typeDir.length() ? "/" : ""));
}
string
Call_abstract::get_filename(eTypeSpoolFile typeSpoolFile, const char *fileExtension) {
string extension = fileExtension ? fileExtension : getFileTypeExtension(typeSpoolFile);
if(((typeIs(OPTIONS) && user_data_type == OPTIONS) ||
(typeIs(SUBSCRIBE) && user_data_type == SUBSCRIBE) ||
(typeIs(NOTIFY) && user_data_type == NOTIFY)) &&
user_data) {
cSipMsgRequestResponse *sipMsgRequestResponse = (cSipMsgRequestResponse*)user_data;
return(sipMsgRequestResponse->getPcapFileName() +
(extension.length() ? "." : "") + extension);
}
return((typeIs(REGISTER) ?
intToString(fname_register) :
get_fbasename_safe()) +
(extension.length() ? "." : "") + extension);
}
string
Call_abstract::get_pathfilename(eTypeSpoolFile typeSpoolFile, const char *fileExtension) {
string pathname = get_pathname(typeSpoolFile);
string filename = get_filename(typeSpoolFile, fileExtension);
return(pathname + (pathname.length() && pathname[pathname.length() - 1] != '/' ? "/" : "") +
filename);
}
/* returns name of the directory in format YYYY-MM-DD */
string
Call_abstract::dirnamesqlfiles() {
char sdirname[50];
struct tm t = time_r(first_packet_time_us);
snprintf(sdirname, sizeof(sdirname), "%04d%02d%02d%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour);
return(sdirname);
}
char *
Call_abstract::get_fbasename_safe() {
strcpy_null_term(fbasename_safe, fbasename);
prepare_string_to_filename(fbasename_safe);
return fbasename_safe;
}
void
Call_abstract::addTarPos(u_int64_t pos, int type) {
switch(type) {
case FileZipHandler::pcap_sip:
if(opt_pcap_dump_tar_sip_use_pos) {
this->tarPosSip.push_back(pos);
}
break;
case FileZipHandler::pcap_rtp:
if(opt_pcap_dump_tar_rtp_use_pos) {
this->tarPosRtp.push_back(pos);
}
break;
case FileZipHandler::graph_rtp:
if(opt_pcap_dump_tar_graph_use_pos) {
this->tarPosGraph.push_back(pos);
}
break;
}
}
/* constructor */
Call::Call(int call_type, char *call_id, unsigned long call_id_len, vector<string> *call_id_alternative, u_int64_t time_us) :
Call_abstract(call_type, time_us),
pcap(PcapDumper::na, this),
pcapSip(PcapDumper::sip, this),
pcapRtp(PcapDumper::rtp, this) {
//increaseTartimemap(time);
has_second_merged_leg = false;
isfax = NOFAX;
seenudptl = 0;
exists_udptl_data = false;
not_acceptable = false;
sip_fragmented = false;
rtp_fragmented = false;
last_callercodec = -1;
ipport_n = 0;
ssrc_n = 0;
last_signal_packet_time_us = time_us;
last_rtp_packet_time_us = 0;
last_rtp_a_packet_time_us = 0;
last_rtp_b_packet_time_us = 0;
if(call_id_len) {
this->call_id = string(call_id, call_id_len);
this->call_id_len = call_id_len;
} else {
this->call_id = string(call_id);
this->call_id_len = this->call_id.length();
}
if(opt_call_id_alternative[0]) {
this->call_id_alternative = new FILE_LINE(0) map<string, bool>;
if(call_id_alternative) {
for(unsigned i = 0; i < call_id_alternative->size(); i++) {
(*this->call_id_alternative)[(*call_id_alternative)[i]] = true;
}
}
} else {
this->call_id_alternative = NULL;
}
_call_id_alternative_lock = 0;
whohanged = -1;
seeninvite = false;
seeninviteok = false;
seenmessage = false;
seenmessageok = false;
seenbye = false;
seenbye_time_usec = 0;
seenbyeandok = false;
seenbyeandok_time_usec = 0;
unconfirmed_bye = false;
seenRES2XX = false;
seenRES2XX_no_BYE = false;
seenRES18X = false;
caller[0] = '\0';
caller_domain[0] = '\0';
callername[0] = '\0';
called[0] = '\0';
called_domain[0] = '\0';
contact_num[0] = '\0';
contact_domain[0] = '\0';
digest_username[0] = '\0';
digest_realm[0] = '\0';
register_expires = -1;
for(unsigned i = 0; i < (sizeof(byecseq) / sizeof(byecseq[0])); i++) {
byecseq[i].null();
}
invitecseq.null();
messagecseq.null();
registercseq.null();
cancelcseq.null();
updatecseq.null();
sighup = false;
progress_time_us = 0;
first_rtp_time_us = 0;
connect_time_us = 0;
first_invite_time_us = 0;
first_response_100_time_us = 0;
first_response_xxx_time_us = 0;
first_message_time_us = 0;
first_response_200_time_us = 0;
a_ua[0] = '\0';
b_ua[0] = '\0';
memset(rtpmap, 0, sizeof(rtpmap));
rtp_cur[0] = NULL;
rtp_cur[1] = NULL;
rtp_prev[0] = NULL;
rtp_prev[1] = NULL;
lastSIPresponse[0] = '\0';
lastSIPresponseNum = 0;
new_invite_after_lsr487 = false;
cancel_lsr487 = false;
reason_sip_cause = 0;
reason_q850_cause = 0;
hold_status = false;
is_fas_detected = false;
is_zerossrc_detected = false;
is_sipalg_detected = false;
msgcount = 0;
regcount = 0;
regcount_after_4xx = 0;
reg401count = 0;
reg401count_all = 0;
reg403count = 0;
reg404count = 0;
reg200count = 0;
regstate = 0;
regresponse = false;
regrrddiff = -1;
//regsrcmac = 0;
last_sip_method = 0;
for(int i = 0; i < MAX_SSRC_PER_CALL; i++) {
rtp[i] = NULL;
}
rtpab[0] = NULL;
rtpab[1] = NULL;
rtplock = 0;
listening_worker_run = NULL;
lastcallerrtp = NULL;
lastcalledrtp = NULL;
saddr.clear();
sport.clear();
daddr.clear();
dport.clear();
destroy_call_at = 0;
destroy_call_at_bye = 0;
destroy_call_at_bye_confirmed = 0;
custom_header1[0] = '\0';
match_header[0] = '\0';
thread_num = 0;
thread_num_rd = 0;
setRtpThreadNum();
recordstopped = 0;
dtmfflag = 0;
for(unsigned int i = 0; i < sizeof(dtmfflag2) / sizeof(dtmfflag2[0]); i++) {
dtmfflag2[i] = 0;
}
silencerecording = 0;
recordingpausedby182 = 0;
rtppacketsinqueue = 0;
end_call_rtp = 0;
end_call_hash_removed = 0;
push_call_to_calls_queue = 0;
push_register_to_registers_queue = 0;
message = NULL;
message_info = NULL;
contenttype = NULL;
content_length = 0;
dcs = 0;
voicemail = voicemail_na;
max_length_sip_data = 0;
max_length_sip_packet = 0;
for(int i = 0; i < MAX_SIPCALLERDIP; i++) {
sipcallerip[i].clear();
sipcalledip[i].clear();
sipcallerport[i].clear();
sipcalledport[i].clear();
}
sipcalledip_mod.clear();
sipcalledport_mod.clear();
lastsipcallerip.clear();
sipcallerdip_reverse = false;
skinny_partyid = 0;
pthread_mutex_init(&listening_worker_run_lock, NULL);
caller_sipdscp = 0;
called_sipdscp = 0;
ps_ifdrop = pcapstat.ps_ifdrop;
ps_drop = pcapstat.ps_drop;
if(verbosity && verbosityE > 1) {
syslog(LOG_NOTICE, "CREATE CALL %s", this->call_id.c_str());
}
_custom_headers_content_sync = 0;
_forcemark_lock = 0;
_proxies_lock = 0;
a_mos_lqo = -1;
b_mos_lqo = -1;
oneway = 1;
absolute_timeout_exceeded = 0;
zombie_timeout_exceeded = 0;
bye_timeout_exceeded = 0;
rtp_timeout_exceeded = 0;
sipwithoutrtp_timeout_exceeded = 0;
oneway_timeout_exceeded = 0;
force_terminate = 0;
pcap_drop = 0;
onInvite = false;
onCall_2XX = false;
onCall_18X = false;
updateDstnumOnAnswer = false;
updateDstnumFromMessage = false;
force_close = false;
first_codec = -1;
caller_silence = 0;
called_silence = 0;
caller_noise = 0;
called_noise = 0;
caller_lastsilence = 0;
called_lastsilence = 0;
caller_clipping_8k = 0;
called_clipping_8k = 0;
vlan = VLAN_UNSET;
_mergecalls_lock = 0;
exists_crypto_suite_key = false;
for(int i = 0; i < 2; i++) {
callerd_confirm_rtp_by_both_sides_sdp[i] = 0;
}
log_srtp_callid = false;
error_negative_payload_length = false;
use_removeRtp = false;
rtp_ip_port_counter = 0;
hash_queue_counter = 0;
attemptsClose = 0;
use_rtcp_mux = false;
use_sdp_sendonly = false;
rtp_from_multiple_sensors = false;
is_ssl = false;
rtp_zeropackets_stored = 0;
last_udptl_seq = 0;
lastraw[0] = NULL;
lastraw[1] = NULL;
iscaller_consecutive[0] = 0;
iscaller_consecutive[1] = 0;
last_mgcp_connect_packet_time_us = 0;
_hash_add_lock = 0;
counter = ++counter_s;
syslog_sdp_multiplication = false;
_txt_lock = 0;
televent_exists_request = false;
televent_exists_response = false;
}
u_int64_t Call::counter_s = 0;
void
Call::hashRemove(struct timeval *ts, bool useHashQueueCounter) {
for(int i = 0; i < ipport_n; i++) {
calltable->hashRemove(this, this->ip_port[i].addr, this->ip_port[i].port, ts, false, useHashQueueCounter);
if(opt_rtcp) {
calltable->hashRemove(this, this->ip_port[i].addr, this->ip_port[i].port.inc(), ts, true, useHashQueueCounter);
}
this->evDestroyIpPortRtpStream(i);
}
if(!opt_hash_modify_queue_length_ms && this->rtp_ip_port_counter) {
syslog(LOG_WARNING, "WARNING: rest before hash cleanup for callid: %s: %i", this->fbasename, this->rtp_ip_port_counter);
if(this->rtp_ip_port_counter > 0) {
calltable->hashRemove(this, ts, useHashQueueCounter);
if(this->rtp_ip_port_counter) {
syslog(LOG_WARNING, "WARNING: rest after hash cleanup for callid: %s: %i", this->fbasename, this->rtp_ip_port_counter);
}
}
}
}
void
Call::skinnyTablesRemove() {
if(opt_skinny) {
calltable->lock_skinny_maps();
if(skinny_partyid) {
calltable->skinny_partyID.erase(skinny_partyid);
skinny_partyid = 0;
}
for (map<d_item<vmIP>, Call*>::iterator skinny_ipTuplesIT = calltable->skinny_ipTuples.begin(); skinny_ipTuplesIT != calltable->skinny_ipTuples.end();) {
if(skinny_ipTuplesIT->second == this) {
calltable->skinny_ipTuples.erase(skinny_ipTuplesIT++);
} else {
++skinny_ipTuplesIT;
}
}
calltable->unlock_skinny_maps();
}
}
void
Call::removeFindTables(struct timeval *ts, bool set_end_call, bool destroy) {
if(set_end_call) {
hash_add_lock();
this->end_call_rtp = 1;
if(!(opt_hash_modify_queue_length_ms && this->end_call_hash_removed)) {
this->hashRemove(ts, true);
this->end_call_hash_removed = 1;
}
hash_add_unlock();
} else if(destroy) {
if(opt_hash_modify_queue_length_ms && this->rtp_ip_port_counter) {
calltable->hashRemoveForce(this);
}
this->hashRemove(ts);
} else {
this->hashRemove(ts, true);
}
this->skinnyTablesRemove();
}
void
Call::destroyCall() {
this->removeFindTables(NULL, false, true);
this->atFinish();
this->calls_counter_dec();
}
void
Call::addtofilesqueue(eTypeSpoolFile typeSpoolFile, string file, long long writeBytes) {
_addtofilesqueue(typeSpoolFile, file, dirnamesqlfiles(), writeBytes, getSpoolIndex());
}
void
Call::_addtofilesqueue(eTypeSpoolFile typeSpoolFile, string file, string dirnamesqlfiles, long long writeBytes, int spoolIndex) {
if(!opt_filesclean or opt_nocdr or file == "" or !isSqlDriver("mysql") or
!CleanSpool::isSetCleanspool(spoolIndex) or
!CleanSpool::check_datehour(dirnamesqlfiles.c_str())) {
return;
}
string dst_file_cachedir;
if(opt_cachedir[0] != '\0') {
int cachedir_length = strlen(opt_cachedir);
if(!strncmp(file.c_str(), opt_cachedir, cachedir_length)) {
while(file[cachedir_length] == '/') {
++cachedir_length;
}
dst_file_cachedir = string(::getSpoolDir(typeSpoolFile, spoolIndex)) + '/' + file.substr(cachedir_length);
}
}
bool fileExists = file_exists((char*)file.c_str());
bool fileCacheExists = false;
string fileCache;
if(opt_cachedir[0] != '\0') {
fileCache = string(opt_cachedir) + "/" + file;
fileCacheExists = file_exists((char*)fileCache.c_str());
}
if(!fileExists && !fileCacheExists) return;
long long size = 0;
if(fileExists) {
size = GetFileSizeDU(file, typeSpoolFile, spoolIndex);
}
if(!size && fileCacheExists) {
size = GetFileSizeDU(fileCache, typeSpoolFile, spoolIndex);
}
if(writeBytes) {
writeBytes = GetDU(writeBytes, typeSpoolFile, spoolIndex);
if(writeBytes > size) {
size = writeBytes;
}
}
if(size == (long long)-1) {
//error or file does not exists
char buf[4092];
buf[0] = '\0';
strerror_r(errno, buf, 4092);
syslog(LOG_ERR, "addtofilesqueue ERROR file[%s] - error[%d][%s]", file.c_str(), errno, buf);
return;
}
if(size == 0) {
// if the file has 0 size we still need to add it to cleaning procedure
size = 1;
}
extern CleanSpool *cleanSpool[2];
if(cleanSpool[spoolIndex]) {
cleanSpool[spoolIndex]->addFile(dirnamesqlfiles.c_str(), typeSpoolFile, dst_file_cachedir.empty() ? file.c_str() : dst_file_cachedir.c_str(), size);
}
}
void
Call::evStartRtpStream(int /*index_ip_port*/, vmIP saddr, vmPort sport, vmIP daddr, vmPort dport, time_t time) {
/*cout << "start rtp stream : "
<< saddr.getString() << ":" << sport << " -> "
<< daddr.getString() << ":" << dport << endl;*/
if(opt_enable_fraud) {
fraudBeginRtpStream(saddr, sport, daddr, dport, this, time);
}
}
void
Call::evEndRtpStream(int /*index_ip_port*/, vmIP saddr, vmPort sport, vmIP daddr, vmPort dport, time_t time) {
/*cout << "stop rtp stream : "
<< saddr.getString() << ":" << sport << " -> "
<< daddr.getString() << ":" << dport << endl;*/
if(opt_enable_fraud) {
fraudEndRtpStream(saddr, sport, daddr, dport, this, time);
}
}
void
Call::addtocachequeue(string file) {
_addtocachequeue(file);
}
void
Call::_addtocachequeue(string file) {
int cachedir_length = strlen(opt_cachedir);
if(!strncmp(file.c_str(), opt_cachedir, cachedir_length)) {
while(file[cachedir_length] == '/') {
++cachedir_length;
}
file = file.substr(cachedir_length);
}
calltable->lock_files_queue();
calltable->files_queue.push(file);
calltable->unlock_files_queue();
}
void
Call::removeRTP() {
while(this->rtppacketsinqueue > 0) {
if(!opt_t2_boost && rtp_threads) {
extern int num_threads_max;
for(int i = 0; i < num_threads_max; i++) {
if(rtp_threads[i].threadId) {
rtp_threads[i].push_batch();
}
}
}
USLEEP(100);
}
while(__sync_lock_test_and_set(&rtplock, 1)) {
USLEEP(100);
}
closeRawFiles();
ssrc_n = 0;
for(int i = 0; i < MAX_SSRC_PER_CALL; i++) {
// lets check whole array as there can be holes due rtp[0] <=> rtp[1] swaps in mysql rutine
if(rtp[i]) {
delete rtp[i];
rtp[i] = NULL;
}
}
for(int i = 0; i < 2; i++) {
rtp_cur[i] = NULL;
rtp_prev[i] = NULL;
}
lastcallerrtp = NULL;
lastcalledrtp = NULL;
__sync_lock_release(&rtplock);
use_removeRtp = true;
}
/* destructor */
Call::~Call(){
alloc_flag = 0;
if(opt_call_id_alternative[0] && call_id_alternative) {
delete call_id_alternative;
}
removeMergeCalls();
if(is_ssl) {
glob_ssl_calls--;
}
if(contenttype) delete [] contenttype;
for(int i = 0; i < MAX_SSRC_PER_CALL; i++) {
// lets check whole array as there can be holes due rtp[0] <=> rtp[1] swaps in mysql rutine
if(rtp[i]) {
delete rtp[i];
}
}
// tell listening_worker to stop listening
if(listening_worker_run) {
*listening_worker_run = 0;
}
destroyListeningBuffers();
listening_remove_worker(this);
pthread_mutex_destroy(&listening_worker_run_lock);
if(this->message) {
delete [] message;
}
if(this->message_info) {
delete [] message_info;
}
//decreaseTartimemap(this->first_packet_time);
//printf("caller s[%u] n[%u] ls[%u] called s[%u] n[%u] ls[%u]\n", caller_silence, caller_noise, caller_lastsilence, called_silence, called_noise, called_lastsilence);
//printf("caller_clipping_8k [%u] [%u]\n", caller_clipping_8k, called_clipping_8k);
if(typeIs(INVITE) && is_enable_rtp_threads() && num_threads_active > 0 && rtp_threads) {
extern void lock_add_remove_rtp_threads();
extern void unlock_add_remove_rtp_threads();
lock_add_remove_rtp_threads();
if(rtp_threads[thread_num].calls > 0) {
__sync_sub_and_fetch(&rtp_threads[thread_num].calls, 1);
}
unlock_add_remove_rtp_threads();
}
for(map<sStreamId, sUdptlDumper*>::iterator iter = udptlDumpers.begin(); iter != udptlDumpers.end(); iter++) {
delete iter->second;
}
for(map<int, class RTPsecure*>::iterator iter = rtp_secure_map.begin(); iter != rtp_secure_map.end(); iter++) {
delete iter->second;
}
}
void
Call::closeRawFiles() {
for(int i = 0; i < ssrc_n; i++) {
// close RAW files
if(rtp[i]->gfileRAW) {
FILE *tmp;
rtp[i]->jitterbuffer_fixed_flush(rtp[i]->channel_record);
/* preventing race condition as gfileRAW is checking for NULL pointer in rtp classes */
tmp = rtp[i]->gfileRAW;
rtp[i]->gfileRAW = NULL;
fclose(tmp);
}
// close GRAPH files
if(opt_saveGRAPH || (flags & FLAG_SAVEGRAPH)) {
if(rtp[i]->graph.isOpen()) {
if(!rtp[i]->mos_processed or (rtp[i]->last_mos_time + 1 < rtp[i]->_last_ts.tv_sec)) {
rtp[i]->save_mos_graph(true);
}
rtp[i]->graph.close();
} else {
rtp[i]->graph.clearAutoOpen();
}
}
}
}
/* add ip adress and port to this call */
int
Call::add_ip_port(vmIP sip_src_addr, vmIP addr, ip_port_call_info::eTypeAddr type_addr, vmPort port, pcap_pkthdr *header,
char *sessid, char *sdp_label, list<rtp_crypto_config> *rtp_crypto_config_list, char *to, char *branch, int iscaller, RTPMAP *rtpmap, s_sdp_flags sdp_flags) {
if(this->end_call_rtp) {
return(-1);
}
if(verbosity >= 4) {
printf("call:[%p] ip:[%s] port:[%d] iscaller:[%d]\n", this, addr.getString().c_str(), port.getPort(), iscaller);
}
if(ipport_n > 0) {
if(this->refresh_data_ip_port(addr, port, header,
rtp_crypto_config_list, iscaller, rtpmap, sdp_flags)) {
return 1;
}
}
if(sverb.process_rtp) {
cout << "RTP - add_ip_port: " << addr.getString() << " / " << port << " " << iscaller_description(iscaller) << endl;