-
Notifications
You must be signed in to change notification settings - Fork 564
/
mediaproxy.c
2051 lines (1658 loc) · 57.3 KB
/
mediaproxy.c
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
/*
* Copyright (C) 2004-2008 Dan Pascu
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/un.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../str.h"
#include "../../pvar.h"
#include "../../error.h"
#include "../../data_lump.h"
#include "../../mem/mem.h"
#include "../../ut.h"
#include "../../trim.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_to.h"
#include "../../msg_translator.h"
#include "../dialog/dlg_load.h"
#include "../dialog/dlg_hash.h"
#include "../tm/tm_load.h"
#include "../tm/t_msgbuilder.h"
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define INLINE inline
#else
# define INLINE
#endif
#define SIGNALING_IP_AVP_SPEC "$avp(mp_signaling_ip)"
#define MEDIA_RELAY_AVP_SPEC "$avp(mp_media_relay)"
#define ICE_CANDIDATE_AVP_SPEC "$avp(mp_ice_candidate)"
#define NO_CANDIDATE -1
// Although `AF_LOCAL' is mandated by POSIX.1g, `AF_UNIX' is portable to
// more systems. `AF_UNIX' was the traditional name stemming from BSD, so
// even most POSIX systems support it. It is also the name of choice in
// the Unix98 specification. So if there's no AF_LOCAL fallback to AF_UNIX
#ifndef AF_LOCAL
# define AF_LOCAL AF_UNIX
#endif
// Solaris does not have the MSG_NOSIGNAL flag for the send(2) syscall
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
#define isnulladdr(adr) ((adr).len==7 && memcmp("0.0.0.0", (adr).s, 7)==0)
#define isnullport(port) ((port).len==1 && (port).s[0]=='0')
#define STR_MATCH(str, buf) ((str).len==strlen(buf) && memcmp(buf, (str).s, (str).len)==0)
#define STR_IMATCH(str, buf) ((str).len==strlen(buf) && strncasecmp(buf, (str).s, (str).len)==0)
#define STR_HAS_PREFIX(str, prefix) ((str).len>=(prefix).len && memcmp((prefix).s, (str).s, (prefix).len)==0)
#define STR_HAS_IPREFIX(str, prefix) ((str).len>=(prefix).len && strncasecmp((prefix).s, (str).s, (prefix).len)==0)
typedef int Bool;
#define True 1
#define False 0
typedef Bool (*NatTestFunction)(struct sip_msg *msg);
typedef enum {
TNone=0,
TSupported,
TUnsupported
} TransportType;
#define RETRY_INTERVAL 10
#define BUFFER_SIZE 8192
typedef struct MediaproxySocket {
char *name; // name
int sock; // socket
int timeout; // how many milliseconds to wait for an answer
time_t last_failure; // time of the last failure
char data[BUFFER_SIZE]; // buffer for the answer data
} MediaproxySocket;
typedef struct {
const char *name;
uint32_t address;
uint32_t mask;
} NetInfo;
typedef struct {
str type; // stream type (`audio', `video', `image', ...)
str ip;
str port;
str rtcp_ip; // pointer to the rtcp IP if explicitly specified by stream
str rtcp_port; // pointer to the rtcp port if explicitly specified by stream
str direction;
Bool local_ip; // true if the IP is locally defined inside this media stream
Bool has_ice;
Bool has_rtcp_ice;
TransportType transport;
char *start_line;
char *next_line;
char *first_ice_candidate;
} StreamInfo;
#define MAX_STREAMS 32
typedef struct SessionInfo {
str ip;
str ip_line; // pointer to the whole session level ip line
str direction;
str separator;
StreamInfo streams[MAX_STREAMS];
unsigned int stream_count;
unsigned int supported_count;
} SessionInfo;
typedef struct AVP_Param {
str spec;
int name;
unsigned short type;
} AVP_Param;
typedef struct ice_candidate_data {
unsigned int priority;
Bool skip_next_reply;
} ice_candidate_data;
// Function prototypes
//
static int EngageMediaProxy(struct sip_msg *msg);
static int UseMediaProxy(struct sip_msg *msg);
static int EndMediaSession(struct sip_msg *msg);
static int mod_init(void);
static int child_init(int rank);
// Module global variables and state
//
static int mediaproxy_disabled = False;
static str ice_candidate = str_init("none");
static MediaproxySocket mediaproxy_socket = {
"/var/run/mediaproxy/dispatcher.sock", // name
-1, // sock
500, // timeout in 500 milliseconds if there is no answer
0, // time of the last failure
"" // data
};
struct tm_binds tm_api;
struct dlg_binds dlg_api;
Bool have_dlg_api = False;
// The AVP where the caller signaling IP is stored (if defined)
static AVP_Param signaling_ip_avp = {str_init(SIGNALING_IP_AVP_SPEC), -1, 0};
// The AVP where the application-defined media relay IP is stored
static AVP_Param media_relay_avp = {str_init(MEDIA_RELAY_AVP_SPEC), -1, 0};
// The AVP where the ICE candidate priority is stored (if defined)
static AVP_Param ice_candidate_avp = {str_init(ICE_CANDIDATE_AVP_SPEC), -1, 0};
static cmd_export_t commands[] = {
{"engage_media_proxy", (cmd_function)EngageMediaProxy, 0, 0, 0, REQUEST_ROUTE},
{"use_media_proxy", (cmd_function)UseMediaProxy, 0, 0, 0, REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | LOCAL_ROUTE},
{"end_media_session", (cmd_function)EndMediaSession, 0, 0, 0, REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | LOCAL_ROUTE},
{0, 0, 0, 0, 0, 0}
};
static param_export_t parameters[] = {
{"disable", INT_PARAM, &mediaproxy_disabled},
{"mediaproxy_socket", STR_PARAM, &(mediaproxy_socket.name)},
{"mediaproxy_timeout", INT_PARAM, &(mediaproxy_socket.timeout)},
{"signaling_ip_avp", STR_PARAM, &(signaling_ip_avp.spec.s)},
{"media_relay_avp", STR_PARAM, &(media_relay_avp.spec.s)},
{"ice_candidate", STR_PARAM, &(ice_candidate.s)},
{"ice_candidate_avp", STR_PARAM, &(ice_candidate_avp.spec.s)},
{0, 0, 0}
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tm", DEP_SILENT },
{ MOD_TYPE_DEFAULT, "dialog", DEP_SILENT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ NULL, NULL },
},
};
struct module_exports exports = {
"mediaproxy", // module name
MOD_TYPE_DEFAULT,// class of this module
MODULE_VERSION, // module name
DEFAULT_DLFLAGS, // dlopen flags
0, // load function
&deps, // OpenSIPS module dependencies
commands, // exported functions
NULL, // exported async functions
parameters, // exported parameters
NULL, // exported statistics
NULL, // exported MI functions
NULL, // exported pseudo-variables
NULL, // exported transformations
NULL, // extra processes
NULL, // pre-init function
mod_init, // module init function (before fork. kids will inherit)
NULL, // reply processing function
NULL, // destroy function
child_init // child init function
};
// String processing functions
//
// strfind() finds the start of the first occurrence of the substring needle
// of length nlen in the memory area haystack of length len.
static void*
strfind(const void *haystack, size_t len, const void *needle, size_t nlen)
{
char *sp;
// Sanity check
if(!(haystack && needle && nlen && len>=nlen))
return NULL;
for (sp = (char*)haystack; sp <= (char*)haystack + len - nlen; sp++) {
if (*sp == *(char*)needle && memcmp(sp, needle, nlen)==0) {
return sp;
}
}
return NULL;
}
// strcasefind() finds the start of the first occurrence of the substring
// needle of length nlen in the memory area haystack of length len by doing
// a case insensitive search
static void*
strcasefind(const char *haystack, size_t len, const char *needle, size_t nlen)
{
char *sp;
// Sanity check
if(!(haystack && needle && nlen && len>=nlen))
return NULL;
for (sp = (char*)haystack; sp <= (char*)haystack + len - nlen; sp++) {
if (tolower(*sp) == tolower(*(char*)needle) &&
strncasecmp(sp, needle, nlen)==0) {
return sp;
}
}
return NULL;
}
// returns a pointer to first CR or LF char found or the end of string
static char*
findendline(char *string, int len)
{
char *ptr = string;
while(ptr - string < len && *ptr != '\n' && *ptr != '\r')
ptr++;
return ptr;
}
static int
strtoint(str *data)
{
long int result;
char c;
// hack to avoid copying the string
c = data->s[data->len];
data->s[data->len] = 0;
result = strtol(data->s, NULL, 10);
data->s[data->len] = c;
return (int)result;
}
// find a line in str `block' that starts with `start'.
static char*
find_line_starting_with(str *block, char *start, int ignoreCase)
{
char *ptr, *bend;
str zone;
int tlen;
bend = block->s + block->len;
tlen = strlen(start);
ptr = NULL;
for (zone = *block; zone.len > 0; zone.len = bend - zone.s) {
if (ignoreCase)
ptr = strcasefind(zone.s, zone.len, start, tlen);
else
ptr = strfind(zone.s, zone.len, start, tlen);
if (!ptr || ptr==block->s || ptr[-1]=='\n' || ptr[-1]=='\r')
break;
zone.s = ptr + tlen;
}
return ptr;
}
// count all lines in str `block' that starts with `start'.
static unsigned int
count_lines_starting_with(str *block, char *start, int ignoreCase)
{
char *ptr, *bend;
str zone;
int tlen;
unsigned count;
bend = block->s + block->len;
tlen = strlen(start);
count = 0;
for (zone = *block; zone.len > 0; zone.len = bend - zone.s) {
if (ignoreCase)
ptr = strcasefind(zone.s, zone.len, start, tlen);
else
ptr = strfind(zone.s, zone.len, start, tlen);
if (!ptr)
break;
if (ptr==block->s || ptr[-1]=='\n' || ptr[-1]=='\r')
count++;
zone.s = ptr + tlen;
}
return count;
}
// get up to `limit' whitespace separated tokens from `char *string'
static int
get_tokens(char *string, str *tokens, int limit)
{
int i, len, size;
char *ptr;
if (!string) {
return 0;
}
len = strlen(string);
for (ptr=string, i=0; i<limit && len>0; i++) {
size = strspn(ptr, " \t\n\r");
ptr += size;
len -= size;
if (len <= 0)
break;
size = strcspn(ptr, " \t\n\r");
if (size==0)
break;
tokens[i].s = ptr;
tokens[i].len = size;
ptr += size;
len -= size;
}
return i;
}
// get up to `limit' whitespace separated tokens from `str *string'
static int
get_str_tokens(str *string, str *tokens, int limit)
{
int count;
char c;
if (!string || !string->s) {
return 0;
}
c = string->s[string->len];
string->s[string->len] = 0;
count = get_tokens(string->s, tokens, limit);
string->s[string->len] = c;
return count;
}
// Functions to extract the info we need from the SIP/SDP message
//
static Bool
get_callid(struct sip_msg* msg, str *cid)
{
if (msg->callid == NULL) {
if (parse_headers(msg, HDR_CALLID_F, 0) == -1) {
LM_ERR("cannot parse Call-ID header\n");
return False;
}
if (msg->callid == NULL) {
LM_ERR("missing Call-ID header\n");
return False;
}
}
*cid = msg->callid->body;
trim(cid);
return True;
}
static Bool
get_cseq_number(struct sip_msg *msg, str *cseq)
{
struct cell *trans = tm_api.t_gett();
if (msg->first_line.type == SIP_REPLY && trans != NULL && trans != T_UNDEFINED &&
trans->uas.request!=NULL ) {
*cseq = get_cseq(trans->uas.request)->number;
} else {
if (msg->cseq == NULL) {
if (parse_headers(msg, HDR_CSEQ_F, 0)==-1) {
LM_ERR("cannot parse CSeq header\n");
return False;
}
if (msg->cseq == NULL) {
LM_ERR("missing CSeq header\n");
return False;
}
}
*cseq = get_cseq(msg)->number;
}
return True;
}
static str
get_from_uri(struct sip_msg *msg)
{
static str unknown = str_init("unknown");
str uri;
char *ptr;
if (parse_from_header(msg) < 0) {
LM_ERR("cannot parse the From header\n");
return unknown;
}
uri = get_from(msg)->uri;
if (uri.len == 0)
return unknown;
if (strncasecmp(uri.s, "sip:", 4)==0) {
uri.s += 4;
uri.len -= 4;
}
if ((ptr = strfind(uri.s, uri.len, ";", 1))!=NULL) {
uri.len = ptr - uri.s;
}
return uri;
}
static str
get_to_uri(struct sip_msg *msg)
{
static str unknown = str_init("unknown");
str uri;
char *ptr;
if (parse_headers(msg, HDR_TO_F, 0) == -1) {
LM_ERR("failed to parse To header\n");
return unknown;
}
if (!msg->to) {
LM_ERR("missing To header\n");
return unknown;
}
uri = get_to(msg)->uri;
if (uri.len == 0)
return unknown;
if (strncasecmp(uri.s, "sip:", 4)==0) {
uri.s += 4;
uri.len -= 4;
}
if ((ptr = strfind(uri.s, uri.len, ";", 1))!=NULL) {
uri.len = ptr - uri.s;
}
return uri;
}
static str
get_from_tag(struct sip_msg *msg)
{
static str undefined = str_init("");
str tag;
if (parse_from_header(msg) < 0) {
LM_ERR("cannot parse the From header\n");
return undefined;
}
tag = get_from(msg)->tag_value;
if (tag.len == 0)
return undefined;
return tag;
}
static str
get_to_tag(struct sip_msg *msg)
{
static str undefined = str_init("");
str tag;
if (msg->first_line.type==SIP_REPLY && msg->REPLY_STATUS<200) {
// Ignore the To tag for provisional replies
return undefined;
}
if (parse_headers(msg, HDR_TO_F, 0) == -1) {
LM_ERR("failed to parse To header\n");
return undefined;
}
if (!msg->to) {
LM_ERR("missing To header\n");
return undefined;
}
tag = get_to(msg)->tag_value;
if (tag.len == 0)
return undefined;
return tag;
}
static str
get_user_agent(struct sip_msg* msg)
{
static str unknown = str_init("unknown agent");
str block, server;
char *ptr;
if (parse_headers(msg, HDR_USERAGENT_F, 0)==0 && msg->user_agent &&
msg->user_agent->body.s && msg->user_agent->body.len>0) {
return msg->user_agent->body;
}
// If we can't find user-agent, look after the `Server' header
// This is a temporary hack. Normally it should be extracted by opensips.
block.s = msg->buf;
block.len = msg->len;
ptr = find_line_starting_with(&block, "Server:", True);
if (!ptr)
return unknown;
server.s = ptr + 7;
server.len = findendline(server.s, block.s+block.len-server.s) - server.s;
trim(&server);
if (server.len == 0)
return unknown;
return server;
}
// Get caller signaling IP
static str
get_signaling_ip(struct sip_msg* msg)
{
int_str value;
if (!search_first_avp(signaling_ip_avp.type | AVP_VAL_STR,
signaling_ip_avp.name, &value, NULL) ||
value.s.s==NULL || value.s.len==0) {
value.s.s = ip_addr2a(&msg->rcv.src_ip);
value.s.len = strlen(value.s.s);
}
return value.s;
}
// Get the application-defined media_relay if defined
static str
get_media_relay(struct sip_msg* msg)
{
static str undefined = str_init("");
int_str value;
if (!search_first_avp(media_relay_avp.type | AVP_VAL_STR,
media_relay_avp.name, &value, NULL) || value.s.s==NULL || value.s.len==0) {
return undefined;
}
return value.s;
}
// Functions to manipulate the SDP message body
//
static Bool
check_content_type(struct sip_msg *msg)
{
str type;
if (!msg->content_type) {
LM_WARN("the Content-Type header is missing! Assume the content type is text/plain\n");
return True;
}
type = msg->content_type->body;
trim(&type);
if (strncasecmp(type.s, "application/sdp", 15) != 0)
return False;
if (!(isspace((int)type.s[15]) || type.s[15] == ';' || type.s[15] == 0)) {
LM_ERR("invalid character after Content-Type: `%c'\n", type.s[15]);
return False;
}
return True;
}
// Get the SDP message from SIP message and check it's Content-Type
// Return values:
// 1 - success
// -1 - error in getting body or invalid content type
// -2 - empty message
static int
get_sdp_message(struct sip_msg *msg, str *sdp)
{
if (get_body(msg, sdp)!=0 || sdp->len==0)
return -2;
if (!check_content_type(msg))
return -1;
return 1;
}
// Return a str containing the line separator used in the SDP body
static str
get_sdp_line_separator(str *sdp)
{
char *ptr, *end_ptr, *sdp_end;
str separator;
sdp_end = sdp->s + sdp->len;
ptr = find_line_starting_with(sdp, "v=", False);
end_ptr = findendline(ptr, sdp_end-ptr);
separator.s = ptr = end_ptr;
while ((*ptr=='\n' || *ptr=='\r') && ptr<sdp_end)
ptr++;
separator.len = ptr - separator.s;
if (separator.len > 2)
separator.len = 2; // safety check
return separator;
}
// will return the direction attribute defined in the given block.
// if missing, default is used if provided, else `sendrecv' is used.
static str
get_direction_attribute(str *block, str *default_direction)
{
str direction, zone, line;
char *ptr;
for (zone=*block;;) {
ptr = find_line_starting_with(&zone, "a=", False);
if (!ptr) {
if (default_direction)
return *default_direction;
direction.s = "sendrecv";
direction.len = 8;
return direction;
}
line.s = ptr + 2;
line.len = findendline(line.s, zone.s + zone.len - line.s) - line.s;
if (line.len==8) {
if (strncasecmp(line.s, "sendrecv", 8)==0 || strncasecmp(line.s, "sendonly", 8)==0 ||
strncasecmp(line.s, "recvonly", 8)==0 || strncasecmp(line.s, "inactive", 8)==0) {
return line;
}
}
zone.s = line.s + line.len;
zone.len = block->s + block->len - zone.s;
}
}
// will return the rtcp port of the stream in the given block
// if defined by the stream, otherwise will return {NULL, 0}.
static str
get_rtcp_port_attribute(str *block)
{
str zone, rtcp_port, undefined = {NULL, 0};
char *ptr;
int count;
ptr = find_line_starting_with(block, "a=rtcp:", False);
if (!ptr)
return undefined;
zone.s = ptr + 7;
zone.len = findendline(zone.s, block->s + block->len - zone.s) - zone.s;
count = get_str_tokens(&zone, &rtcp_port, 1);
if (count != 1) {
LM_ERR("invalid `a=rtcp' line in SDP body\n");
return undefined;
}
return rtcp_port;
}
// will return the rtcp IP of the stream in the given block
// if defined by the stream, otherwise will return {NULL, 0}.
static str
get_rtcp_ip_attribute(str *block)
{
str zone, tokens[4], undefined = {NULL, 0};
char *ptr;
int count;
ptr = find_line_starting_with(block, "a=rtcp:", False);
if (!ptr)
return undefined;
zone.s = ptr + 7;
zone.len = findendline(zone.s, block->s + block->len - zone.s) - zone.s;
count = get_str_tokens(&zone, tokens, 4);
if (count != 4) {
return undefined;
}
return tokens[3];
}
// will return true if the given block has both
// a=ice-pwd and a=ice-ufrag attributes.
static Bool
has_ice_attributes(str *block)
{
char *ptr;
ptr = find_line_starting_with(block, "a=ice-pwd:", False);
if (ptr) {
ptr = find_line_starting_with(block, "a=ice-ufrag:", False);
if (ptr) {
return True;
}
}
return False;
}
// will return true if the given SDP has both
// a=ice-pwd and a=ice-ufrag attributes at the
// session level.
static Bool
has_session_ice_attributes(str *sdp)
{
str block;
char *ptr;
// session level ICE attributes can be found from the beginning up to the first media block
ptr = find_line_starting_with(sdp, "m=", False);
if (ptr) {
block.s = sdp->s;
block.len = ptr - block.s;
} else {
block = *sdp;
}
return has_ice_attributes(&block);
}
// will return true if the given block contains
// a a=candidate attribute. This should be called
// for a stream, as a=candidate attribute is not
// allowed at the session level
static Bool
has_ice_candidates(str *block)
{
char *ptr;
ptr = find_line_starting_with(block, "a=candidate:", False);
if (ptr) {
return True;
}
return False;
}
// will return true if given block contains an ICE
// candidate with the given component ID
static Bool
has_ice_candidate_component(str *block, int id)
{
char *ptr, *block_end;
int i, components, count;
str chunk, zone, tokens[2];
block_end = block->s + block->len;
components = count_lines_starting_with(block, "a=candidate:", False);
for (i=0, chunk=*block; i<components; i++) {
ptr = find_line_starting_with(&chunk, "a=candidate:", False);
if (!ptr)
break;
zone.s = ptr + 12;
zone.len = findendline(zone.s, block_end - zone.s) - zone.s;
count = get_str_tokens(&zone, tokens, 2);
if (count == 2) {
if (strtoint(&tokens[1]) == id) {
return True;
}
}
chunk.s = zone.s + zone.len;
chunk.len = block_end - chunk.s;
}
return False;
}
// will return the priority (string value) that will be used
// for the candidate(s) inserted
static str
get_ice_candidate(void)
{
int_str value;
if (!search_first_avp(ice_candidate_avp.type | AVP_VAL_STR,
ice_candidate_avp.name, &value, NULL) || value.s.s==NULL || value.s.len==0) {
// if AVP is not set use global module parameter
return ice_candidate;
} else {
return value.s;
}
}
// will return the priority (integer value) that will be used
// for the candidate(s) inserted
static unsigned int
get_ice_candidate_priority(str priority)
{
int type_pref;
if (STR_IMATCH(priority, "high-priority")) {
// Use type preference even higher than host candidates
type_pref = 130;
} else if (STR_IMATCH(priority, "low-priority")) {
type_pref = 0;
} else {
return NO_CANDIDATE;
}
// This will return the priority for the RTP component, the RTCP
// component is RTP - 1
return ((type_pref << 24) + 16777215);
}
// will return the ip address present in a `c=' line in the given block
// returns: -1 on error, 0 if not found, 1 if found
static int
get_media_ip_from_block(str *block, str *mediaip)
{
str tokens[3], zone;
char *ptr;
int count;
ptr = find_line_starting_with(block, "c=", False);
if (!ptr) {
mediaip->s = NULL;
mediaip->len = 0;
return 0;
}
zone.s = ptr + 2;
zone.len = findendline(zone.s, block->s + block->len - zone.s) - zone.s;
count = get_str_tokens(&zone, tokens, 3);
if (count != 3) {
LM_ERR("invalid `c=' line in SDP body\n");
return -1;
}
// can also check if tokens[1] == 'IP4'
*mediaip = tokens[2];
return 1;
}
static Bool
get_sdp_session_ip(str *sdp, str *mediaip, str *ip_line)
{
char *ptr, *end_ptr;
str block;
// session IP can be found from the beginning up to the first media block
ptr = find_line_starting_with(sdp, "m=", False);
if (ptr) {
block.s = sdp->s;
block.len = ptr - block.s;
} else {
block = *sdp;
}
if (get_media_ip_from_block(&block, mediaip) == -1) {