forked from SIPp/sipp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall.cpp
More file actions
7158 lines (6507 loc) · 300 KB
/
Copy pathcall.cpp
File metadata and controls
7158 lines (6507 loc) · 300 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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author : Richard GAYRAUD - 04 Nov 2003
* Olivier Jacques
* From Hewlett Packard Company.
* Shriram Natarajan
* Peter Higginson
* Eric Miller
* Venkatesh
* Enrico Hartung
* Nasir Khan
* Lee Ballard
* Guillaume Teissier from FTR&D
* Wolfgang Beck
* Venkatesh
* Vlad Troyanker
* Charles P Wright from IBM Research
* Amit On from Followap
* Jan Andres from Freenet
* Ben Evans from Open Cloud
* Marc Van Diest from Belgacom
* Michael Dwyer from Cibation
* Roland Meub
* Andy Aicken
* Martin H. VanLeeuwen
*/
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <assert.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string>
#ifdef PCAPPLAY
#include "send_packets.h"
#endif
#include "sipp.hpp"
#include "auth.hpp"
#include "urlcoder.hpp"
#include "deadcall.hpp"
#include "config.h"
#include "version.h"
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
std::string join(const std::vector<std::string> &s, const char* delim) {
std::ostringstream imploded;
std::copy(s.begin(), s.end(), std::ostream_iterator<std::string>(imploded, delim));
std::string ret = imploded.str();
if (ret.length()) {
ret.resize(ret.length() - strlen(delim));
}
return ret;
}
std::string trim(const std::string &s) {
size_t first = s.find_first_not_of(' ');
if (first == std::string::npos) {
return s;
}
size_t last = s.find_last_not_of(' ');
return s.substr(first, (last - first + 1));
}
#define callDebug(...) do { if (useCallDebugf) { _callDebug( __VA_ARGS__ ); } } while (0)
extern std::map<std::string, SIPpSocket *> map_perip_fd;
#ifdef PCAPPLAY
/* send_packets pthread wrapper */
void *send_wrapper(void *);
#endif
int call::dynamicId = 0;
int call::maxDynamicId = 10000+2000*4; // FIXME both param to be in command line !!!!
int call::startDynamicId = 10000; // FIXME both param to be in command line !!!!
int call::stepDynamicId = 4; // FIXME both param to be in command line !!!!
/************** Call map and management routines **************/
static const int SM_UNUSED = -1;
static unsigned int next_number = 1;
class CallIdBuilder {
public:
explicit CallIdBuilder(unsigned int call_number)
: call_number(call_number)
{
}
void build(char *call_id)
{
switch (call_id_mode) {
case CID_MODE_UUID:
build_uuid(false);
break;
case CID_MODE_UUID_COMPACT:
build_uuid(true);
break;
case CID_MODE_RANDOM:
build_random();
break;
case CID_MODE_TIMESTAMP:
build_timestamp();
break;
case CID_MODE_FORMAT:
default:
build_format();
break;
}
copy_to(call_id);
}
private:
void build_format()
{
const char *src = call_id_string;
while (*src) {
if (*src != '%') {
output << *src++;
continue;
}
++src;
if (*src == '\0') {
output << '%';
break;
}
switch (*src++) {
case 'u':
output << call_number;
break;
case 'p':
output << pid;
break;
case 's':
output << local_ip;
break;
case 'r':
output << rand();
break;
default:
output << '%';
break;
}
}
}
void build_uuid(bool compact)
{
unsigned char bytes[16];
fill_bytes(bytes, sizeof(bytes));
bytes[6] = static_cast<unsigned char>((bytes[6] & 0x0f) | 0x40);
bytes[8] = static_cast<unsigned char>((bytes[8] & 0x3f) | 0x80);
append_hex_bytes(bytes, 4);
if (!compact) {
output << '-';
}
append_hex_bytes(bytes + 4, 2);
if (!compact) {
output << '-';
}
append_hex_bytes(bytes + 6, 2);
if (!compact) {
output << '-';
}
append_hex_bytes(bytes + 8, 2);
if (!compact) {
output << '-';
}
append_hex_bytes(bytes + 10, 6);
output << '@' << local_ip;
}
void build_random()
{
unsigned char bytes[16];
fill_bytes(bytes, sizeof(bytes));
output << call_number << '-';
append_hex_bytes(bytes, sizeof(bytes));
output << '@' << local_ip;
}
void build_timestamp()
{
output << timestamp_micros() << '-' << call_number << '-' << pid << '@' << local_ip;
}
void fill_bytes(unsigned char *bytes, size_t size) const
{
uint64_t seed = timestamp_micros();
seed ^= static_cast<uint64_t>(call_number) << 32;
seed ^= static_cast<uint64_t>(pid) << 8;
for (size_t i = 0; i < size; ++i) {
seed ^= static_cast<uint64_t>(rand()) << ((i % 4) * 8);
seed = seed * 2862933555777941757ULL + 3037000493ULL + i;
bytes[i] = static_cast<unsigned char>((seed >> ((i % 8) * 8)) & 0xff);
}
}
void append_hex_bytes(const unsigned char *bytes, size_t size)
{
for (size_t i = 0; i < size; ++i) {
output << std::hex << std::nouppercase << std::setw(2) << std::setfill('0')
<< static_cast<unsigned int>(bytes[i]);
}
output << std::dec << std::setfill(' ');
}
static uint64_t timestamp_micros()
{
using clock = std::chrono::system_clock;
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::microseconds>(
clock::now().time_since_epoch()).count());
}
void copy_to(char *call_id) const
{
std::string value = output.str();
size_t length = std::min(value.size(), static_cast<size_t>(MAX_HEADER_LEN - 1));
memcpy(call_id, value.data(), length);
call_id[length] = '\0';
}
unsigned int call_number;
std::ostringstream output;
};
static void build_call_id(char *call_id, unsigned int call_number)
{
CallIdBuilder(call_number).build(call_id);
}
static unsigned int get_tdm_map_number()
{
unsigned int nb = 0;
unsigned int i=0;
unsigned int interval=0;
unsigned int random=0;
bool found = false;
/* Find a number in the tdm_map which is not in use */
interval = (tdm_map_a+1) * (tdm_map_b+1) * (tdm_map_c+1);
random = rand() % interval;
while ((i<interval) && (!found)) {
if (tdm_map[(random + i - 1) % interval] == false) {
nb = (random + i - 1) % interval;
found = true;
}
i++;
}
if (!found) {
return 0;
} else {
return nb+1;
}
}
/* When should this call wake up? */
unsigned int call::wake()
{
unsigned int wake = 0;
if (zombie) {
return wake;
}
if (paused_until) {
wake = paused_until;
}
if (next_retrans && (!wake || (next_retrans < wake))) {
wake = next_retrans;
}
if (recv_timeout && (!wake || (recv_timeout < wake))) {
wake = recv_timeout;
}
return wake;
}
static std::string find_in_sdp(std::string const &pattern, std::string const &msg)
{
std::string::size_type begin, end;
begin = msg.find(pattern);
if (begin == std::string::npos) {
return "";
}
begin += pattern.size();
end = msg.find_first_of(" \r\n", begin);
if (end == std::string::npos || begin == end) {
return "";
}
return msg.substr(begin, end - begin);
}
#ifdef PCAPPLAY
void call::get_remote_media_addr(std::string const &msg)
{
std::string host = find_in_sdp(media_ip_is_ipv6 ? "c=IN IP6 " : "c=IN IP4 ", msg);
if (host.empty()) {
return;
}
hasMediaInformation = 1;
const int family = media_ip_is_ipv6 ? AF_INET6 : AF_INET;
std::string port = find_in_sdp("m=audio ", msg);
if (!port.empty()) {
gai_getsockaddr(&play_args_a.to, host.c_str(), port.c_str(),
AI_NUMERICHOST | AI_NUMERICSERV, family);
}
port = find_in_sdp("m=image ", msg);
if (!port.empty()) {
gai_getsockaddr(&play_args_i.to, host.c_str(), port.c_str(),
AI_NUMERICHOST | AI_NUMERICSERV, family);
}
port = find_in_sdp("m=video ", msg);
if (!port.empty()) {
gai_getsockaddr(&play_args_v.to, host.c_str(), port.c_str(),
AI_NUMERICHOST | AI_NUMERICSERV, family);
}
}
#endif
/******* Extract RTP remote media infomartion from SDP *******/
/***** Similar to the routines used by the PCAP play code *****/
#define SDP_AUDIOPORT_PREFIX "\nm=audio"
#define SDP_IMAGEPORT_PREFIX "\nm=image"
#define SDP_VIDEOPORT_PREFIX "\nm=video"
std::string call::extract_rtp_remote_addr(const char* msg, int &ip_ver, int &audio_port, int &video_port)
{
const char* search;
int image_port = 0;
std::size_t pos1 = 0;
std::size_t pos2 = 0;
std::string msgstr;
std::string sub;
std::string host;
if (msg) {
msgstr = msg;
}
/* Look for start of message body */
search = strstr(msg, "\r\n\r\n");
if (!search) {
ERROR("extract_rtp_remote_addr: SDP message body not found");
}
msg = search + 2; /* skip past header. point to blank line before body */
/* Now search for IP address field */
host = find_in_sdp("c=IN IP4 ", msg);
if (host.empty()) {
host = find_in_sdp("c=IN IP6 ", msg);
if (host.empty()) {
ERROR("extract_rtp_remote_addr: invalid IP version in SDP message body");
}
ip_ver = 6;
} else {
ip_ver = 4;
}
/* Find the port number for the image stream */
pos1 = msgstr.find(SDP_IMAGEPORT_PREFIX, 0, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_IMAGEPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &image_port); /* parse port substring as integer */
}
}
/* Now try to find the port number for the audio stream */
pos1 = msgstr.find(SDP_AUDIOPORT_PREFIX, 0, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_AUDIOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &audio_port); /* parse port substring as integer */
}
}
/* first audio m-line had port of ZERO -- look for second audio m-line */
if (audio_port == 0)
{
pos1 = msgstr.find(SDP_AUDIOPORT_PREFIX, pos2, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_AUDIOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &audio_port);
}
}
}
/* And find the port number for the video stream */
pos1 = msgstr.find(SDP_VIDEOPORT_PREFIX, 0, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_VIDEOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &video_port); /* parse port substring as integer */
}
}
/* first video m-line had port of ZERO -- look for second video m-line */
if (video_port == 0)
{
pos1 = msgstr.find(SDP_VIDEOPORT_PREFIX, pos2, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_VIDEOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &video_port);
}
}
}
return host;
}
int call::check_audio_ciphersuite_match(SrtpInfoParams &pA)
{
int audio_cs_len = 0;
int audio_ciphersuite_match = 0;
logSrtpInfo("call::check_audio_ciphersuite_match(): Preferred AUDIO cryptosuite: [%s]\n", _pref_audio_cs_out);
if (pA.found)
{
audio_cs_len = strlen(_pref_audio_cs_out);
if (!strncmp(_pref_audio_cs_out, "AES_CM_128_HMAC_SHA1_80", audio_cs_len) ||
!strncmp(_pref_audio_cs_out, "AES_CM_128_HMAC_SHA1_32", audio_cs_len) ||
!strncmp(_pref_audio_cs_out, "NULL_HMAC_SHA1_80", audio_cs_len) ||
!strncmp(_pref_audio_cs_out, "NULL_HMAC_SHA1_32", audio_cs_len))
{
if (!strncmp(pA.primary_cryptosuite, _pref_audio_cs_out, audio_cs_len))
{
// PRIMARY AUDIO cryptosuite matches preferred AUDIO cryptosuite
logSrtpInfo("call::check_audio_ciphersuite_match(): PRIMARY AUDIO cryptosuite matches preferred AUDIO cryptosuite...\n");
audio_ciphersuite_match = 1;
}
else
{
// PRIMARY AUDIO cryptosuite does NOT match preferred AUDIO cryptosuite
logSrtpInfo("call::check_audio_ciphersuite_match(): PRIMARY AUDIO cryptosuite [%s] does NOT match preferred AUDIO cryptosuite [%s]...\n", pA.primary_cryptosuite, _pref_audio_cs_out);
audio_ciphersuite_match = 0;
}
}
}
return audio_ciphersuite_match;
}
int call::check_video_ciphersuite_match(SrtpInfoParams &pV)
{
int video_cs_len = 0;
int video_ciphersuite_match = 0;
logSrtpInfo("call::check_video_ciphersuite_match(): Preferred VIDEO cryptosuite: [%s]\n", _pref_video_cs_out);
if (pV.found)
{
video_cs_len = strlen(_pref_video_cs_out);
if (!strncmp(_pref_video_cs_out, "AES_CM_128_HMAC_SHA1_80", video_cs_len) ||
!strncmp(_pref_video_cs_out, "AES_CM_128_HMAC_SHA1_32", video_cs_len) ||
!strncmp(_pref_video_cs_out, "NULL_HMAC_SHA1_80", video_cs_len) ||
!strncmp(_pref_video_cs_out, "NULL_HMAC_SHA1_32", video_cs_len))
{
if (!strncmp(pV.primary_cryptosuite, _pref_video_cs_out, video_cs_len))
{
// PRIMARY VIDEO cryptosuite matches preferred VIDEO cryptosuite
logSrtpInfo("call::check_video_ciphersuite_match(): PRIMARY VIDEO cryptosuite matches preferred VIDEO cryptosuite...\n");
video_ciphersuite_match = 1;
}
else
{
// PRIMARY VIDEO cryptosuite does NOT match preferred VIDEO cryptosuite
logSrtpInfo("call::check_video_ciphersuite_match(): PRIMARY VIDEO cryptosuite [%s] does NOT match preferred VIDEO cryptosuite [%s]...\n", pV.primary_cryptosuite, _pref_video_cs_out);
video_ciphersuite_match = 0;
}
}
}
return video_ciphersuite_match;
}
/******* Extract SRTP remote media infomartion from SDP *******/
#define SDP_AUDIOCRYPTO_PREFIX "\na=crypto:"
#define SDP_VIDEOCRYPTO_PREFIX "\na=crypto:"
int call::extract_srtp_remote_info(const char * msg, SrtpInfoParams &pA, SrtpInfoParams &pV)
{
pA.found = false;
pV.found = false;
pA.primary_cryptotag = 0;
pV.primary_cryptotag = 0;
*pA.primary_cryptosuite = 0;
*pV.primary_cryptosuite = 0;
*pA.primary_cryptokeyparams = 0;
*pV.primary_cryptokeyparams = 0;
pA.primary_unencrypted_srtp = false;
pV.primary_unencrypted_srtp = false;
pA.secondary_cryptotag = 0;
pV.secondary_cryptotag = 0;
*pA.secondary_cryptosuite = 0;
*pV.secondary_cryptosuite = 0;
*pA.secondary_cryptokeyparams = 0;
*pV.secondary_cryptokeyparams = 0;
pA.secondary_unencrypted_srtp = false;
pV.secondary_unencrypted_srtp = false;
std::size_t mline_sol = 0; /* Start of m-line line */
std::size_t mline_eol = 0; /* End of m-line line */
std::string mline_contents; /* Actual m-line contents */
std::size_t msection_limit = 0; /* m-line media section limit */
std::string msgstr; /* std::string representation of SDP body */
char crypto_audio_sessionparams[64];
char crypto_video_sessionparams[64];
char* checkUESRTP = nullptr;
bool audioExists = false;
bool videoExists = false;
std::size_t cur_pos = 0;
int audio_port = 0;
int video_port = 0;
std::size_t pos1 = 0;
std::size_t pos2 = 0;
std::string sub;
std::size_t amsection_limit = 0;
std::size_t vmsection_limit = 0;
*crypto_audio_sessionparams = 0;
*crypto_video_sessionparams = 0;
// skip past header - point to blank line before body
// Try CRLF and if not found, try LF (the RFC requires CRLF)
if (const char *body_crlf = strstr(msg, "\r\n\r\n")) {
msgstr = body_crlf + 4;
} else if (const char *body_lf = strstr(msg, "\n\n")) {
msgstr = body_lf + 2;
}
if (msgstr.empty())
return -1; /* FAILURE -- No SDP body found */
/* --------------------------------------------------------------
* Determine SDP m-line structure
* -------------------------------------------------------------- */
amsection_limit = msgstr.find("\nm=audio", 0, msgstr.size());
vmsection_limit = msgstr.find("\nm=video", 0, msgstr.size());
/* --------------------------------------------------------------
* Try to find an AUDIO MLINE
* -------------------------------------------------------------- */
pos1 = msgstr.find(SDP_AUDIOPORT_PREFIX, 0, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_AUDIOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &audio_port); /* parse port substring as integer */
if (audio_port != 0)
{
logSrtpInfo("found first ACTIVE audio m-line with NON-ZERO port [%d]...\n", audio_port);
audioExists = true;
}
else
{
logSrtpInfo("found first INACTIVE audio m-line (e.g. with ZERO port)...\n");
pos1 = msgstr.find(SDP_AUDIOPORT_PREFIX, pos2, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_AUDIOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &audio_port);
if (audio_port != 0)
{
logSrtpInfo("found second ACTIVE audio m-line with NON-ZERO port [%d]...\n", audio_port);
audioExists = true;
}
else
{
logSrtpInfo("found second INACTIVE audio m-line (e.g. with ZERO port)...\n");
audioExists = false;
}
}
else
{
logSrtpInfo("invalid formatting encountered: missing whitespace after second audio m-line port...\n");
audioExists = false;
}
}
else
{
logSrtpInfo("NO second audio m-line found...\n");
audioExists = false;
}
}
}
else
{
logSrtpInfo("invalid formatting encountered: missing whitespace after first audio m-line port...\n");
audioExists = false;
}
}
else
{
logSrtpInfo("NO first audio m-line found...\n");
audioExists = false;
}
cur_pos = pos2;
if (audioExists &&
(((amsection_limit != std::string::npos) && (cur_pos != std::string::npos) && (cur_pos < amsection_limit)) ||
((amsection_limit == std::string::npos) && (vmsection_limit == std::string::npos) && (cur_pos != std::string::npos))))
{
// AUDIO "m=audio" prefix found...
pA.found = true;
mline_sol = msgstr.find(SDP_AUDIOCRYPTO_PREFIX, cur_pos/*0*/, 10);
if (mline_sol != std::string::npos) {
// PRIMARY AUDIO "a:crypto:" crypto prefix found
mline_eol = msgstr.find("\n", mline_sol, 1);
if (mline_eol != std::string::npos) {
mline_contents = msgstr.substr(mline_sol, mline_eol);
// %*1[ ] is to skip a single space after the "inline:...." field.
// as opposed to literal space, which matches zero or more spaces.
sscanf(mline_contents.c_str(), "\na=crypto:%d %24[^ ] inline:%40[^ ]%*1[ ]%63s",
&pA.primary_cryptotag,
pA.primary_cryptosuite,
pA.primary_cryptokeyparams,
crypto_audio_sessionparams);
checkUESRTP = strstr(crypto_audio_sessionparams, "UNENCRYPTED_SRTP");
if (checkUESRTP) {
logSrtpInfo("call::extract_srtp_remote_info(): Detected UNENCRYPTED_SRTP token for PRIMARY AUDIO\n");
pA.primary_unencrypted_srtp = true;
} else {
logSrtpInfo("call::extract_srtp_remote_info(): No UNENCRYPTED_SRTP token detected for PRIMARY AUDIO\n");
pA.primary_unencrypted_srtp = false;
}
}
}
// Look for end-of-audio-media section
msection_limit = msgstr.find("\nm=", mline_eol+1, 3);
mline_sol = msgstr.find(SDP_AUDIOCRYPTO_PREFIX, mline_eol+1, 10);
if (((msection_limit != std::string::npos) && (mline_sol != std::string::npos) && (mline_sol < msection_limit)) ||
((msection_limit == std::string::npos) && (mline_sol != std::string::npos))) {
// SECONDARY AUDIO "a:crypto:" crypto prefix found
mline_eol = msgstr.find("\n", mline_sol, 1);
if (mline_eol != std::string::npos) {
mline_contents = msgstr.substr(mline_sol, mline_eol);
sscanf(mline_contents.c_str(), "\na=crypto:%d %24[^ ] inline:%40[^ ]%*1[ ]%63s",
&pA.secondary_cryptotag,
pA.secondary_cryptosuite,
pA.secondary_cryptokeyparams,
crypto_audio_sessionparams);
checkUESRTP = strstr(crypto_audio_sessionparams, "UNENCRYPTED_SRTP");
if (checkUESRTP) {
logSrtpInfo("call::extract_srtp_remote_info(): Detected UNENCRYPTED_SRTP token for SECONDARY AUDIO\n");
pA.secondary_unencrypted_srtp = true;
} else {
logSrtpInfo("call::extract_srtp_remote_info(): No UNENCRYPTED_SRTP token detected for SECONDARY AUDIO\n");
pA.secondary_unencrypted_srtp = false;
}
}
}
}
/* --------------------------------------------------------------
* Try to find a VIDEO MLINE
* -------------------------------------------------------------- */
pos1 = msgstr.find(SDP_VIDEOPORT_PREFIX, 0, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_VIDEOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &video_port); /* parse port substring as integer */
if (video_port != 0)
{
logSrtpInfo("found first ACTIVE video m-line with NON-ZERO port [%d]...\n", video_port);
videoExists = true;
}
else
{
logSrtpInfo("found first INACTIVE video m-line (e.g. with ZERO port)...\n");
pos1 = msgstr.find(SDP_VIDEOPORT_PREFIX, pos2, 8);
if (pos1 != std::string::npos)
{
pos1 += 8; /* skip SDP_VIDEOPORT_PREFIX */
pos1 += 1; /* skip first whitespace */
pos2 = msgstr.find(" ", pos1); /* find second whitespace AFTER port */
if (pos2 != std::string::npos)
{
sub = msgstr.substr(pos1, pos2-pos1); /* extract port substring */
sscanf(sub.c_str(), "%d", &video_port);
if (video_port != 0)
{
logSrtpInfo("found second ACTIVE video m-line with NON-ZERO port [%d]...\n", video_port);
videoExists = true;
}
else
{
logSrtpInfo("found second INACTIVE video m-line (e.g. with ZERO port)...\n");
videoExists = false;
}
}
else
{
logSrtpInfo("invalid formatting encountered: missing whitespace after second video m-line port...\n");
videoExists = false;
}
}
else
{
logSrtpInfo("NO second video m-line found...\n");
videoExists = false;
}
}
}
else
{
logSrtpInfo("invalid formatting encountered: missing whitespace after first video m-line port...\n");
videoExists = false;
}
}
else
{
logSrtpInfo("NO first video m-line found...\n");
videoExists = false;
}
cur_pos = pos2;
if (videoExists &&
(((vmsection_limit != std::string::npos) && (cur_pos != std::string::npos) && (cur_pos < vmsection_limit)) ||
((vmsection_limit == std::string::npos) && (amsection_limit == std::string::npos) && (cur_pos != std::string::npos))))
{
// VIDEO "m=video" prefix found...
pV.found = true;
mline_sol = msgstr.find(SDP_VIDEOCRYPTO_PREFIX, cur_pos/*mline_eol+1*/, 10);
if (mline_sol != std::string::npos) {
// PRIMARY VIDEO "a:crypto:" crypto prefix found
mline_eol = msgstr.find("\n", mline_sol, 1);
if (mline_eol != std::string::npos) {
mline_contents = msgstr.substr(mline_sol, mline_eol);
sscanf(mline_contents.c_str(), "\na=crypto:%d %24[^ ] inline:%40[^ ]%*1[ ]%63s",
&pV.primary_cryptotag,
pV.primary_cryptosuite,
pV.primary_cryptokeyparams,
crypto_video_sessionparams);
checkUESRTP = strstr(crypto_video_sessionparams, "UNENCRYPTED_SRTP");
if (checkUESRTP) {
logSrtpInfo("call::extract_srtp_remote_info(): Detected UNENCRYPTED_SRTP token for PRIMARY VIDEO\n");
pV.primary_unencrypted_srtp = true;
} else {
logSrtpInfo("call::extract_srtp_remote_info(): No UNENCRYPTED_SRTP token detected for PRIMARY VIDEO\n");
pV.primary_unencrypted_srtp = false;
}
}
}
// Look for end-of-video-media section
msection_limit = msgstr.find("\nm=", mline_eol+1, 3);
mline_sol = msgstr.find(SDP_VIDEOCRYPTO_PREFIX, mline_eol+1, 10);
if (((msection_limit != std::string::npos) && (mline_sol != std::string::npos) && (mline_sol < msection_limit)) ||
((msection_limit == std::string::npos) && (mline_sol != std::string::npos))) {
// SECONDARY VIDEO "a:crypto:" crypto prefix found
mline_eol = msgstr.find("\n", mline_sol, 1);
if (mline_eol != std::string::npos) {
mline_contents = msgstr.substr(mline_sol, mline_eol);
sscanf(mline_contents.c_str(), "\na=crypto:%d %24[^ ] inline:%40[^ ]%*1[ ]%63s",
&pV.secondary_cryptotag,
pV.secondary_cryptosuite,
pV.secondary_cryptokeyparams,
crypto_video_sessionparams);
checkUESRTP = strstr(crypto_video_sessionparams, "UNENCRYPTED_SRTP");
if (checkUESRTP) {
logSrtpInfo("call::extract_srtp_remote_info(): Detected UNENCRYPTED_SRTP token for SECONDARY VIDEO\n");
pV.secondary_unencrypted_srtp = true;
} else {
logSrtpInfo("call::extract_srtp_remote_info(): No UNENCRYPTED_SRTP token detected for SECONDARY VIDEO\n");
pV.secondary_unencrypted_srtp = false;
}
}
}
}
return 0; /* SUCCESS -- parsed SDP SRTP INFO */
}
/******* Very simple hash for retransmission detection *******/
unsigned long call::hash(const char * msg)
{
unsigned long hash = 0;
int c;
if (rtcheck == RTCHECK_FULL) {
while ((c = *msg++))
hash = c + (hash << 6) + (hash << 16) - hash;
} else if (rtcheck == RTCHECK_LOOSE) {
/* Based on section 11.5 (bullet 2) of RFC2543 we only take into account
* the To, From, Call-ID, and CSeq values. */
const char *hdr = get_header_content(msg, "To:");
while ((c = *hdr++))
hash = c + (hash << 6) + (hash << 16) - hash;
hdr = get_header_content(msg, "From:");
while ((c = *hdr++))
hash = c + (hash << 6) + (hash << 16) - hash;
hdr = get_header_content(msg, "Call-ID:");
while ((c = *hdr++))
hash = c + (hash << 6) + (hash << 16) - hash;
hdr = get_header_content(msg, "CSeq:");
while ((c = *hdr++))
hash = c + (hash << 6) + (hash << 16) - hash;
/* For responses, we should also consider the code and body (if any),
* because they are not nearly as well defined as the request retransmission. */
if (!strncmp(msg, "SIP/2.0", strlen("SIP/2.0"))) {
/* Add the first line into the hash. */
hdr = msg + strlen("SIP/2.0");
while ((c = *hdr++) && (c != '\r'))
hash = c + (hash << 6) + (hash << 16) - hash;
/* Add the body (if any) into the hash. */
hdr = strstr(msg, "\r\n\r\n");
if (hdr) {
hdr += strlen("\r\n\r\n");
while ((c = *hdr++))
hash = c + (hash << 6) + (hash << 16) - hash;
}
}
} else {
ERROR("Internal error: Invalid rtcheck %d", rtcheck);
}
return hash;
}
/******************* Call class implementation ****************/
call::call(scenario *call_scenario, const char *p_id, bool use_ipv6, int userId, struct sockaddr_storage *dest) : listener(p_id, true)
{
init(call_scenario, nullptr, dest, p_id, userId, use_ipv6, false, false);
}
call::call(scenario *call_scenario, const char *p_id, SIPpSocket *socket, struct sockaddr_storage *dest) : listener(p_id, true)
{
init(call_scenario, socket, dest, p_id, 0 /* No User. */, socket->ss_ipv6, false /* Not Auto. */, false);
}
call::call(scenario * call_scenario, SIPpSocket *socket, struct sockaddr_storage *dest, const char * p_id, int userId, bool ipv6, bool isAutomatic, bool isInitialization) : listener(p_id, true)
{
init(call_scenario, socket, dest, p_id, userId, ipv6, isAutomatic, isInitialization);
}
call *call::add_call(int userId, bool ipv6, struct sockaddr_storage *dest)
{
static char call_id[MAX_HEADER_LEN];
if(!next_number) {
next_number ++;
}
build_call_id(call_id, next_number);
return new call(main_scenario, nullptr, dest, call_id, userId, ipv6, false /* Not Auto. */, false);
}
void call::init(scenario * call_scenario, SIPpSocket *socket, struct sockaddr_storage *dest, const char * p_id, int userId, bool ipv6, bool isAutomatic, bool isInitCall)
{
_srtpctxdebugfile = nullptr;
if (srtpcheck_debug)
{
if (sendMode == MODE_CLIENT)
{
_srtpctxdebugfile = fopen("srtpctxdebugfile_uac", "w");
}
else if (sendMode == MODE_SERVER)
{
_srtpctxdebugfile = fopen("srtpctxdebugfile_uas", "w");
}
if (_srtpctxdebugfile == nullptr)
{
/* error encountered opening srtp ctx debug file */
WARNING("Error encountered opening srtp ctx debug file");
}
}
_sessionStateCurrent = eNoSession;
_sessionStateOld = eNoSession;
this->call_scenario = call_scenario;
zombie = false;
debugBuffer = nullptr;
debugLength = 0;
msg_index = 0;
last_send_index = 0;
last_send_msg = nullptr;
last_send_len = 0;