forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calltable.cpp
3893 lines (3555 loc) · 121 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 <net/if.h>
#include <iostream>
#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 "tar.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_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 int opt_audio_format; // define format for audio writing (if -W option)
extern int opt_mos_g729;
extern int opt_nocdr;
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 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 int num_threads;
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_silencedmtfseq[16];
extern bool opt_cdr_sipport;
extern bool opt_cdr_rtpport;
extern char get_customers_pn_query[1024];
extern int opt_saverfc2833;
extern int opt_dbdtmf;
extern int opt_dscp;
extern int opt_cdrproxy;
extern int opt_pcap_dump_tar;
extern struct pcap_stat pcapstat;
extern int opt_filesclean;
extern int opt_allow_zerossrc;
extern int opt_cdr_ua_enable;
extern unsigned int graph_delimiter;
extern unsigned int graph_version;
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_rtpsave_threaded;
extern int opt_last_rtp_from_end;
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;
volatile int calls_counter = 0;
volatile int calls_cdr_save_counter = 0;
volatile int calls_message_save_counter = 0;
extern char mac[32];
unsigned int last_register_clean = 0;
extern CustPhoneNumberCache *custPnCache;
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 int opt_skinny;
extern int opt_enable_fraud;
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];
SqlDb *sqlDbSaveCall = NULL;
bool existsColumnCalldateInCdrNext = true;
bool existsColumnCalldateInCdrRtp = true;
bool existsColumnCalldateInCdrDtmf = true;
bool existsColumnCalldateInCdrTarPart = true;
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;
/* constructor */
Call::Call(char *call_id, unsigned long call_id_len, time_t time) :
tmprtp(-1),
pcap(PcapDumper::na, this),
pcapSip(PcapDumper::sip, this),
pcapRtp(PcapDumper::rtp, this) {
//increaseTartimemap(time);
isfax = 0;
seenudptl = 0;
last_callercodec = -1;
ipport_n = 0;
ssrc_n = 0;
first_packet_time = time;
first_packet_usec = 0;
last_packet_time = time;
last_rtp_a_packet_time = 0;
last_rtp_b_packet_time = 0;
this->call_id = string(call_id, call_id_len);
this->call_id_len = call_id_len;
whohanged = -1;
seeninvite = false;
seeninviteok = false;
seenbye = false;
seenbyeandok = false;
seenRES2XX = 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;
byecseq[0] = '\0';
invitecseq[0] = '\0';
cancelcseq[0] = '\0';
sighup = false;
progress_time = 0;
set_progress_time_via_2XX_or18X = false;
first_rtp_time = 0;
connect_time = 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;
msgcount = 0;
regcount = 0;
reg401count = 0;
regstate = 0;
regresponse = false;
for(int i = 0; i < MAX_SSRC_PER_CALL; i++) {
rtp[i] = NULL;
}
rtplock = 0;
audiobuffer1 = NULL;
last_seq_audiobuffer1 = 0;
audiobuffer2 = NULL;
last_seq_audiobuffer2 = 0;
listening_worker_run = NULL;
tmprtp.call_owner = this;
flags = 0;
lastcallerrtp = NULL;
lastcalledrtp = NULL;
destroy_call_at = 0;
destroy_call_at_bye = 0;
custom_header1[0] = '\0';
match_header[0] = '\0';
thread_num = num_threads > 0 ? gthread_num % num_threads : 0;
gthread_num++;
recordstopped = 0;
dtmfflag = 0;
dtmfflag2 = 0;
silencerecording = 0;
flags1 = 0;
#if SYNC_CALL_RTP
rtppcaketsinqueue = 0;
#else
rtppcaketsinqueue_p = 0;
rtppcaketsinqueue_m = 0;
#endif
message = NULL;
contenttype = NULL;
content_length = 0;
unrepliedinvite = 0;
for(int i = 0; i < MAX_SIPCALLERDIP; i++) {
sipcallerip[i] = 0;
sipcalledip[i] = 0;
}
lastsipcallerip = 0;
sipcallerport = 0;
sipcalledport = 0;
fname2 = 0;
skinny_partyid = 0;
pthread_mutex_init(&buflock, NULL);
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());
}
forcemark[0] = forcemark[1] = 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;
pcap_drop = 0;
onCall_2XX = false;
onCall_18X = false;
updateDstnumOnAnswer = false;
useSensorId = opt_id_sensor;
useDlt = global_pcap_dlink;
useHandle = global_pcap_handle;
force_close = false;
first_codec = -1;
chantype = 0;
chunkBuffersCount = 0;
}
void
Call::mapRemove() {
int i;
Calltable *ct = (Calltable *)calltable;
for(i = 0; i < ipport_n; i++) {
ct->mapRemove(this->ip_port[i].addr, this->ip_port[i].port);
if(opt_rtcp) {
ct->mapRemove(this->ip_port[i].addr, this->ip_port[i].port + 1);
}
}
}
void
Call::hashRemove() {
int i;
Calltable *ct = (Calltable *)calltable;
for(i = 0; i < ipport_n; i++) {
ct->hashRemove(this, this->ip_port[i].addr, this->ip_port[i].port);
if(opt_rtcp) {
ct->hashRemove(this, this->ip_port[i].addr, this->ip_port[i].port + 1);
}
}
}
void
Call::addtofilesqueue(string file, string column, long long writeBytes) {
_addtofilesqueue(file, column, dirnamesqlfiles(), writeBytes);
}
void
Call::_addtofilesqueue(string file, string column, string dirnamesqlfiles, long long writeBytes) {
if(!opt_filesclean or opt_nocdr or file == "" or !isSqlDriver("mysql") or
!isSetCleanspoolParameters()) return;
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);
}
if(!size && fileCacheExists) {
size = GetFileSizeDU(fileCache);
}
if(writeBytes) {
writeBytes = GetDU(writeBytes);
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;
}
ostringstream query;
int id_sensor = opt_id_sensor_cleanspool == -1 ? 0 : opt_id_sensor_cleanspool;
query << "INSERT INTO files SET files.datehour = " << dirnamesqlfiles << ", id_sensor = " << id_sensor << ", "
<< column << " = " << size << " ON DUPLICATE KEY UPDATE " << column << " = " << column << " + " << size;
sqlStore->lock(STORE_PROC_ID_CLEANSPOOL);
sqlStore->query(query.str().c_str(), STORE_PROC_ID_CLEANSPOOL);
ostringstream fname;
fname << "filesindex/" << column << "/" << dirnamesqlfiles;
ofstream myfile(fname.str().c_str(), ios::app | ios::out);
if(!myfile.is_open()) {
syslog(LOG_ERR,"error write to [%s]", fname.str().c_str());
}
myfile << file << ":" << size << "\n";
myfile.close();
sqlStore->unlock(STORE_PROC_ID_CLEANSPOOL);
}
void
Call::addtocachequeue(string file) {
_addtocachequeue(file);
}
void
Call::_addtocachequeue(string file) {
calltable->lock_files_queue();
calltable->files_queue.push(file);
calltable->unlock_files_queue();
}
void
Call::removeRTP() {
while(rtplock) {
//wait until the lock is released
usleep(100);
}
rtplock = 1;
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;
}
}
lastcallerrtp = NULL;
lastcalledrtp = NULL;
rtplock = 0;
}
/* destructor */
Call::~Call(){
if(opt_skinny) {
if(skinny_partyid) {
((Calltable *)calltable)->skinny_partyID.erase(skinny_partyid);
}
/*
stringstream tmp[2];
tmp[0] << this->sipcallerip << '|' << this->sipcalledip;
tmp[1] << this->sipcallerip << '|' << this->sipcalledip;
*/
for (((Calltable *)calltable)->skinny_ipTuplesIT = ((Calltable *)calltable)->skinny_ipTuples.begin(); ((Calltable *)calltable)->skinny_ipTuplesIT != ((Calltable *)calltable)->skinny_ipTuples.end();) {
if(((Calltable *)calltable)->skinny_ipTuplesIT->second == this) {
((Calltable *)calltable)->skinny_ipTuples.erase(((Calltable *)calltable)->skinny_ipTuplesIT++);
} else {
++((Calltable *)calltable)->skinny_ipTuplesIT;
}
}
}
if(opt_callidmerge_header[0] != '\0') {
((Calltable*)calltable)->lock_calls_mergeMAP();
for(std::vector<string>::iterator it = mergecalls.begin(); it != mergecalls.end(); ++it) {
((Calltable*)calltable)->calls_mergeMAP.erase(*it);
}
((Calltable*)calltable)->unlock_calls_mergeMAP();
}
if(contenttype) free(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;
}
pthread_mutex_lock(&listening_worker_run_lock);
if(audiobuffer1) delete audiobuffer1;
if(audiobuffer2) delete audiobuffer2;
if(this->message) {
free(message);
}
pthread_mutex_destroy(&buflock);
pthread_mutex_unlock(&listening_worker_run_lock);
pthread_mutex_destroy(&listening_worker_run_lock);
//decreaseTartimemap(this->first_packet_time);
}
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()) {
rtp[i]->graph.close();
}
}
}
}
/* returns name of the directory in format YYYY-MM-DD */
string
Call::dirname() {
char sdirname[18];
struct tm t = localtime_r((const time_t*)(&first_packet_time));
if(opt_newdir) {
snprintf(sdirname, 17, "%04d-%02d-%02d/%02d/%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min);
} else {
snprintf(sdirname, 17, "%04d-%02d-%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
}
sdirname[17] = 0;
string s(sdirname);
return s;
}
/* returns name of the directory in format YYYY-MM-DD */
string
Call::dirnamesqlfiles() {
char sdirname[12];
struct tm t = localtime_r((const time_t*)(&first_packet_time));
snprintf(sdirname, 11, "%04d%02d%02d%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour);
sdirname[11] = 0;
string s(sdirname);
return s;
}
/* add ip adress and port to this call */
int
Call::add_ip_port(in_addr_t sip_src_addr, in_addr_t addr, unsigned short port, char *sessid, char *ua, unsigned long ua_len, bool iscaller, int *rtpmap, bool fax) {
if(verbosity >= 4) {
struct in_addr in;
in.s_addr = addr;
printf("call:[%p] ip:[%s] port:[%d] iscaller:[%d]\n", this, inet_ntoa(in), port, iscaller);
}
if(ipport_n > 0) {
if(this->refresh_data_ip_port(addr, port, iscaller, rtpmap, fax)) {
return 1;
}
}
if(sverb.process_rtp) {
cout << "RTP - add_ip_port: " << inet_ntostring(htonl(addr)) << " / " << port << " " << (iscaller ? "caller" : "called") << endl;
}
if(ipport_n == MAX_IP_PER_CALL){
char tmp[18];
struct in_addr in;
in.s_addr = addr;
strcpy(tmp, inet_ntoa(in));
syslog(LOG_ERR,"callid [%s]: to much INVITEs in this call [%s:%d], raise MAX_IP_PER_CALL and recompile sniffer", call_id.c_str(), tmp, port);
}
// add ip and port
if(ipport_n >= MAX_IP_PER_CALL){
return -1;
}
if(ua_len && ua_len < 1024) {
char *tmp = iscaller ? this->b_ua : this->a_ua;
memcpy(tmp, ua, MIN(ua_len, 1024));
tmp[MIN(ua_len, 1023)] = '\0';
}
this->ip_port[ipport_n].sip_src_addr = sip_src_addr;
this->ip_port[ipport_n].addr = addr;
this->ip_port[ipport_n].port = port;
this->ip_port[ipport_n].iscaller = iscaller;
this->ip_port[ipport_n].fax = fax;
if(sessid) {
memcpy(this->ip_port[ipport_n].sessid, sessid, MAXLEN_SDP_SESSID);
} else {
memset(this->ip_port[ipport_n].sessid, 0, MAXLEN_SDP_SESSID);
}
memcpy(this->rtpmap[RTPMAP_BY_CALLERD ? iscaller : ipport_n], rtpmap, MAX_RTPMAP * sizeof(int));
ipport_n++;
return 0;
}
bool
Call::refresh_data_ip_port(in_addr_t addr, unsigned short port, bool iscaller, int *rtpmap, bool fax) {
for(int i = 0; i < ipport_n; i++) {
if(this->ip_port[i].addr == addr && this->ip_port[i].port == port) {
// reinit rtpmap
memcpy(this->rtpmap[RTPMAP_BY_CALLERD ? iscaller : i], rtpmap, MAX_RTPMAP * sizeof(int));
// force mark bit for reinvite
forcemark[iscaller] = true;
if(fax && !this->ip_port[i].fax) {
this->ip_port[i].fax = fax;
calltable->lock_calls_hash();
hash_node_call *calls = calltable->hashfind_by_ip_port(addr, port, 0, false);
if(calls) {
for(hash_node_call *node_call = calls; node_call != NULL; node_call = node_call->next) {
node_call->is_fax = fax;
}
}
calltable->unlock_calls_hash();
}
return true;
}
}
return false;
}
void
Call::add_ip_port_hash(in_addr_t sip_src_addr, in_addr_t addr, unsigned short port, char *sessid, char *ua, unsigned long ua_len, bool iscaller, int *rtpmap, bool fax, int allowrelation) {
if(sessid) {
int sessidIndex = get_index_by_sessid(sessid, sip_src_addr);
if(sessidIndex >= 0) {
if(this->ip_port[sessidIndex].sip_src_addr == sip_src_addr &&
(this->ip_port[sessidIndex].addr != addr ||
this->ip_port[sessidIndex].port != port ||
this->ip_port[sessidIndex].iscaller != iscaller)) {
((Calltable*)calltable)->hashRemove(this, ip_port[sessidIndex].addr, ip_port[sessidIndex].port);
((Calltable*)calltable)->hashAdd(addr, port, this, iscaller, 0, fax, allowrelation);
if(opt_rtcp) {
((Calltable*)calltable)->hashRemove(this, ip_port[sessidIndex].addr, ip_port[sessidIndex].port + 1);
((Calltable*)calltable)->hashAdd(addr, port + 1, this, iscaller, 1, fax);
}
//cout << "change ip/port for sessid " << sessid << " ip:" << inet_ntostring(htonl(addr)) << "/" << inet_ntostring(htonl(this->ip_port[sessidIndex].addr)) << " port:" << port << "/" << this->ip_port[sessidIndex].port << endl;
this->ip_port[sessidIndex].addr = addr;
this->ip_port[sessidIndex].port = port;
this->ip_port[sessidIndex].iscaller = iscaller;
}
this->refresh_data_ip_port(addr, port, iscaller, rtpmap, fax);
return;
}
}
if(this->add_ip_port(sip_src_addr, addr, port, sessid, ua, ua_len, iscaller, rtpmap, fax) != -1) {
((Calltable*)calltable)->hashAdd(addr, port, this, iscaller, 0, fax, allowrelation);
if(opt_rtcp) {
((Calltable*)calltable)->hashAdd(addr, port + 1, this, iscaller, 1, fax);
}
}
}
/* Return reference to Call if IP:port was found, otherwise return NULL */
Call*
Call::find_by_ip_port(in_addr_t addr, unsigned short port, int *iscaller){
for(int i = 0; i < ipport_n; i++) {
if(this->ip_port[i].addr == addr && this->ip_port[i].port == port) {
// we have found it
*iscaller = this->ip_port[i].iscaller;
return this;
}
}
// not found
return NULL;
}
int
Call::get_index_by_ip_port(in_addr_t addr, unsigned short port){
for(int i = 0; i < ipport_n; i++) {
if(this->ip_port[i].addr == addr && this->ip_port[i].port == port) {
// we have found it
return i;
}
}
// not found
return -1;
}
Call*
Call::find_by_sessid(char *sessid){
for(int i = 0; i < ipport_n; i++) {
if(!memcmp(this->ip_port[i].sessid, sessid, MAXLEN_SDP_SESSID)) {
// we have found it
return this;
}
}
// not found
return NULL;
}
int
Call::get_index_by_sessid(char *sessid, in_addr_t sip_src_addr){
for(int i = 0; i < ipport_n; i++) {
if(!memcmp(this->ip_port[i].sessid, sessid, MAXLEN_SDP_SESSID) &&
(!sip_src_addr || sip_src_addr == this->ip_port[i].sip_src_addr)) {
// we have found it
return i;
}
}
// not found
return -1;
}
/* analyze rtcp packet */
void
Call::read_rtcp(unsigned char* data, int datalen, int dataoffset, struct pcap_pkthdr *header, u_int32_t saddr, u_int32_t daddr, unsigned short sport, unsigned short dport, int iscaller,
char enable_save_packet, const u_char *packet, char istcp, int dlt, int sensor_id) {
parse_rtcp((char*)data, datalen, this);
if(enable_save_packet && opt_rtpsave_threaded) {
save_packet(this, header, packet, saddr, sport, daddr, dport, istcp, NULL, (char*)data, datalen, dataoffset, TYPE_RTP,
false, dlt, sensor_id);
}
}
/* analyze rtp packet */
void
Call::read_rtp(unsigned char* data, int datalen, int dataoffset, struct pcap_pkthdr *header, struct iphdr2 *header_ip, u_int32_t saddr, u_int32_t daddr, unsigned short sport, unsigned short dport, int iscaller, int *record,
char enable_save_packet, const u_char *packet, char istcp, int dlt, int sensor_id, char *ifname) {
*record = 0;
if(first_rtp_time == 0) {
first_rtp_time = header->ts.tv_sec;
}
//RTP tmprtp; moved to Call structure to avoid creating and destroying class which is not neccessary
tmprtp.fill(data, datalen, header, saddr, daddr, sport, dport);
int curpayload = tmprtp.getPayload();
unsigned int curSSRC = tmprtp.getSSRC();
if((!opt_allow_zerossrc and curSSRC == 0) || tmprtp.getVersion() != 2) {
// invalid ssrc
goto end;
}
if(opt_dscp && !header_ip) {
header_ip = (struct iphdr2 *)(data - sizeof(struct iphdr2) - sizeof(udphdr2));
}
if(iscaller) {
last_rtp_a_packet_time = header->ts.tv_sec;
} else {
last_rtp_b_packet_time = header->ts.tv_sec;
}
for(int i = 0; i < ssrc_n; i++) {
if(rtp[i]->ssrc2 == curSSRC
#if RTP_BY_SRC_IP
&& rtp[i]->saddr == saddr
#endif
&& rtp[i]->dport == dport
) {
// found
if(opt_dscp) {
rtp[i]->dscp = header_ip->tos >> 2;
if(sverb.dscp) {
cout << "rtpdscp " << (int)(header_ip->tos>>2) << endl;
}
}
// chekc if packet is DTMF and saverfc2833 is enabled
if(opt_saverfc2833 and rtp[i]->codec == PAYLOAD_TELEVENT) {
*record = 1;
}
// check if codec did not changed but ignore payload 13 and 19 which is CNG and 101 which is DTMF
if(curpayload == 13 or curpayload == 19 or rtp[i]->codec == PAYLOAD_TELEVENT or rtp[i]->payload2 == curpayload) {
goto read;
} else {
//codec changed, check if it is not DTMF
if(curpayload >= 96 && curpayload <= 127) {
for(int j = 0; j < MAX_RTPMAP; j++) {
if(rtp[i]->rtpmap[j] != 0 && curpayload == rtp[i]->rtpmap[j] / 1000) {
rtp[i]->codec = rtp[i]->rtpmap[j] - curpayload * 1000;
}
}
} else {
rtp[i]->codec = curpayload;
}
if(rtp[i]->codec == PAYLOAD_TELEVENT) {
read:
rtp[i]->read(data, datalen, header, saddr, daddr, sport, dport, seeninviteok, sensor_id, ifname);
if(iscaller) {
lastcallerrtp = rtp[i];
} else {
lastcalledrtp = rtp[i];
}
goto end;
} else {
//codec changed and it is not DTMF, reset ssrc so the stream will not match and new one is used
rtp[i]->ssrc2 = 0;
}
}
}
}
// adding new RTP source
if(ssrc_n < MAX_SSRC_PER_CALL) {
// close previouse graph files to save RAM (but only if > 10
if(flags & FLAG_SAVEGRAPH && ssrc_n > 6) {
if(iscaller) {
if(lastcallerrtp && lastcallerrtp->graph.isOpen()) {
lastcallerrtp->graph.close();
}
} else {
if(lastcalledrtp && lastcalledrtp->graph.isOpen()) {
lastcalledrtp->graph.close();
}
}
}
// if previouse RTP streams are present it should be filled by silence to keep it in sync
if(iscaller) {
last_seq_audiobuffer1 = 0;
if(lastcallerrtp) {
lastcallerrtp->jt_tail(header);
}
} else {
last_seq_audiobuffer2 = 0;
if(lastcalledrtp) {
lastcalledrtp->jt_tail(header);
}
}
while(rtplock) {
//wait until the lock is released
usleep(100);
}
rtplock = 1;
rtp[ssrc_n] = new RTP(sensor_id);
rtp[ssrc_n]->call_owner = this;
rtp[ssrc_n]->ssrc_index = ssrc_n;
rtp[ssrc_n]->iscaller = iscaller;
if(rtp_cur[iscaller]) {
rtp_prev[iscaller] = rtp_cur[iscaller];
}
rtp_cur[iscaller] = rtp[ssrc_n];
if(opt_dscp) {
rtp[ssrc_n]->dscp = header_ip->tos >> 2;
if(sverb.dscp) {
cout << "rtpdscp " << (int)(header_ip->tos>>2) << endl;
}
}
char graphFilePath_spool_relative[1024];
char graphFilePath[1024];
snprintf(graphFilePath_spool_relative, 1023, "%s/%s/%s.%d.graph%s", dirname().c_str(), opt_newdir ? "GRAPH" : "", get_fbasename_safe(), ssrc_n,
opt_gzipGRAPH == FileZipHandler::gzip ? ".gz" : "");
graphFilePath_spool_relative[1023] = 0;
if(opt_cachedir[0] != '\0') {
snprintf(graphFilePath, 1023, "%s/%s", opt_cachedir, graphFilePath_spool_relative);
graphFilePath[1023] = 0;
} else {
strcpy(graphFilePath, graphFilePath_spool_relative);
}
strcpy(rtp[ssrc_n]->gfilename, graphFilePath);
if(flags & FLAG_SAVEGRAPH) {
if(rtp[ssrc_n]->graph.open(graphFilePath, graphFilePath_spool_relative)) {
rtp[ssrc_n]->graph.write((char*)&graph_version, 4); //every graph starts with graph_version
}
}
rtp[ssrc_n]->gfileRAW = NULL;
snprintf(rtp[ssrc_n]->basefilename, 1023, "%s/%s/%s.i%d", dirname().c_str(), opt_newdir ? "AUDIO" : "", get_fbasename_safe(), !iscaller);
rtp[ssrc_n]->basefilename[1023] = 0;
if(RTPMAP_BY_CALLERD) {
memcpy(this->rtp[ssrc_n]->rtpmap, rtpmap[isFillRtpMap(iscaller) ? iscaller : !iscaller], MAX_RTPMAP * sizeof(int));
} else {
int i = get_index_by_ip_port(saddr, sport);
if(i >= 0 && isFillRtpMap(i)) {
memcpy(this->rtp[ssrc_n]->rtpmap, rtpmap[i], MAX_RTPMAP * sizeof(int));
} else {
for(int j = 0; j < 2; j++) {
i = getFillRtpMapByCallerd(j ? !iscaller : iscaller);
if(i >= 0) {
memcpy(this->rtp[ssrc_n]->rtpmap, rtpmap[i], MAX_RTPMAP * sizeof(int));
break;
}
}
}
}
rtp[ssrc_n]->read(data, datalen, header, saddr, daddr, sport, dport, seeninviteok, sensor_id, ifname);
this->rtp[ssrc_n]->ssrc = this->rtp[ssrc_n]->ssrc2 = curSSRC;
this->rtp[ssrc_n]->payload2 = curpayload;
//set codec
if(curpayload >= 96 && curpayload <= 127) {
for(int i = 0; i < MAX_RTPMAP; i++) {
if(this->rtp[ssrc_n]->rtpmap[i] != 0 && curpayload == this->rtp[ssrc_n]->rtpmap[i] / 1000) {
this->rtp[ssrc_n]->codec = this->rtp[ssrc_n]->rtpmap[i] - curpayload * 1000;
}
}
} else {
this->rtp[ssrc_n]->codec = curpayload;
}
if(iscaller) {
lastcallerrtp = rtp[ssrc_n];
} else {
lastcalledrtp = rtp[ssrc_n];
}
rtplock = 0;
ssrc_n++;
}
end:
if(enable_save_packet && opt_rtpsave_threaded) {
if((this->silencerecording || (opt_onlyRTPheader && !(this->flags & FLAG_SAVERTP))) && !this->isfax) {
if(datalen >= RTP_FIXED_HEADERLEN &&
header->caplen > (unsigned)(datalen - RTP_FIXED_HEADERLEN)) {
unsigned int tmp_u32 = header->caplen;
header->caplen = header->caplen - (datalen - RTP_FIXED_HEADERLEN);
save_packet(this, header, packet, saddr, sport, daddr, dport, istcp, NULL, (char*)data, datalen, dataoffset, TYPE_RTP,
false, dlt, sensor_id);
header->caplen = tmp_u32;
}
} else {
save_packet(this, header, packet, saddr, sport, daddr, dport, istcp, NULL, (char*)data, datalen, dataoffset, TYPE_RTP,
false, dlt, sensor_id);
}
}
}
void Call::stoprecording() {
if(recordstopped == 0) {
this->flags = 0;
this->pcap.remove();
this->pcapSip.remove();
this->pcapRtp.remove();
/****
if(this->get_fsip_pcap() != NULL) {
pcap_dump_flush(this->get_fsip_pcap());
pcap_dump_close(this->get_fsip_pcap());
this->set_fsip_pcap(NULL);
if(opt_cachedir[0] != '\0') {
snprintf(str2, 2047, "%s/%s.pcap", opt_cachedir, sip_pcapfilename.c_str());
} else {
snprintf(str2, 2047, "%s.pcap", sip_pcapfilename.c_str());
}
str2[2047] = 0;
unlink(str2);
}
if(this->get_frtp_pcap() != NULL) {
pcap_dump_flush(this->get_frtp_pcap());
pcap_dump_close(this->get_frtp_pcap());
this->set_frtp_pcap(NULL);
if(opt_cachedir[0] != '\0') {
snprintf(str2, 2047, "%s/%s.pcap", opt_cachedir, rtp_pcapfilename.c_str());
} else {
snprintf(str2, 2047, "%s.pcap", rtp_pcapfilename.c_str());
}
str2[2047] = 0;
unlink(str2);
}
if(this->get_f_pcap() != NULL) {
pcap_dump_flush(this->get_f_pcap());
pcap_dump_close(this->get_f_pcap());
this->set_f_pcap(NULL);
if(opt_cachedir[0] != '\0') {
snprintf(str2, 2047, "%s/%s.pcap", opt_cachedir, pcapfilename.c_str());
} else {
snprintf(str2, 2047, "%s.pcap", pcapfilename.c_str());
}
str2[2047] = 0;
unlink(str2);
}
****/
this->recordstopped = 1;
if(verbosity >= 1) {
syslog(LOG_ERR,"Call %s/%s.pcap was stopped due to dtmf or norecord sip header. ", this->dirname().c_str(), this->get_fbasename_safe());
}
} else {
if(verbosity >= 1) {
syslog(LOG_ERR,"Call %s/%s.pcap was stopped before. Ignoring now. ", this->dirname().c_str(), this->get_fbasename_safe());
}
}
}
double calculate_mos_g711(double ppl, double burstr, int version) {
double r;
double bpl = 8.47627; //mos = -4.23836 + 0.29873 * r - 0.00416744 * r * r + 0.0000209855 * r * r * r;
double mos;
if(ppl == 0 or burstr == 0) {
return 4.5;
}
if(ppl > 0.5) {
return 1;
}
switch(version) {
case 1:
case 2:
default:
// this mos is calculated for G.711 and PLC
bpl = 17.2647;
r = 93.2062077233 - 95.0 * (ppl*100/(ppl*100/burstr + bpl));
mos = 2.06405 + 0.031738 * r - 0.000356641 * r * r + 2.93143 * pow(10,-6) * r * r * r;
if(mos < 1)
return 1;
if(mos > 4.5)
return 4.5;
}