Browse code

modules/sdpops: added sloppy option to sdp_content function

Juha Heinanen authored on 13/11/2015 06:50:01
Showing 3 changed files
... ...
@@ -42,7 +42,7 @@ Daniel-Constantin Mierla
42 42
               4.14. sdp_with_codecs_by_name(list)
43 43
               4.15. sdp_print(level)
44 44
               4.16. sdp_get(avpvar)
45
-              4.17. sdp_content()
45
+              4.17. sdp_content([sloppy])
46 46
               4.18. sdp_with_ice()
47 47
               4.19. sdp_get_line_startswith(avpvar, string)
48 48
 
... ...
@@ -97,7 +97,7 @@ Chapter 1. Admin Guide
97 97
         4.14. sdp_with_codecs_by_name(list)
98 98
         4.15. sdp_print(level)
99 99
         4.16. sdp_get(avpvar)
100
-        4.17. sdp_content()
100
+        4.17. sdp_content([sloppy])
101 101
         4.18. sdp_with_ice()
102 102
         4.19. sdp_get_line_startswith(avpvar, string)
103 103
 
... ...
@@ -149,7 +149,7 @@ Chapter 1. Admin Guide
149 149
    4.14. sdp_with_codecs_by_name(list)
150 150
    4.15. sdp_print(level)
151 151
    4.16. sdp_get(avpvar)
152
-   4.17. sdp_content()
152
+   4.17. sdp_content([sloppy])
153 153
    4.18. sdp_with_ice()
154 154
    4.19. sdp_get_line_startswith(avpvar, string)
155 155
 
... ...
@@ -418,9 +418,12 @@ sdp_print("1");
418 418
 sdp_get("$avp(sdp)");
419 419
 ...
420 420
 
421
-4.17.  sdp_content()
421
+4.17.  sdp_content([sloppy])
422 422
 
423
-   Return true if the SIP message has SDP body or a SDP part in body.
423
+   Return true if the SIP message has SDP body or a SDP part in body. If
424
+   an argument is given, the function does not parse message body, but
425
+   returns true if Content-Type is "application/sdp" or if that string
426
+   exists in "multipart/mixed" body.
424 427
 
425 428
    This function can be used from ANY_ROUTE.
426 429
 
... ...
@@ -470,10 +470,13 @@ sdp_get("$avp(sdp)");
470 470
 	</section>
471 471
 	<section id="sdpops.f.sdp_content">
472 472
 	    <title>
473
-		<function moreinfo="none">sdp_content()</function>
473
+		<function moreinfo="none">sdp_content([sloppy])</function>
474 474
 	    </title>
475 475
 	    <para>
476
-		Return true if the SIP message has SDP body or a SDP part in body.
476
+		Return true if the SIP message has SDP body or a SDP part in
477
+        body.  If an argument is given, the function does not parse message
478
+        body, but returns true if Content-Type is "application/sdp" or
479
+        if that string exists in "multipart/mixed" body.
477 480
 	    </para>
478 481
 		<para>
479 482
 			This function can be used from ANY_ROUTE.
... ...
@@ -36,6 +36,7 @@
36 36
 #include "../../trim.h"
37 37
 #include "../../data_lump.h"
38 38
 #include "../../ut.h"
39
+#include "../../parser/parse_content.h"
39 40
 
40 41
 #include "api.h"
41 42
 #include "sdpops_data.h"
... ...
@@ -59,6 +60,7 @@ static int w_sdp_remove_transport(sip_msg_t* msg, char* transport, char *bar);
59 59
 static int w_sdp_print(sip_msg_t* msg, char* level, char *bar);
60 60
 static int w_sdp_get(sip_msg_t* msg, char *bar);
61 61
 static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar);
62
+static int w_sdp_content_sloppy(sip_msg_t* msg, char* foo, char *bar);
62 63
 static int w_sdp_with_ice(sip_msg_t* msg, char* foo, char *bar);
63 64
 static int w_sdp_get_line_startswith(sip_msg_t* msg, char *foo, char *bar);
64 65
 
... ...
@@ -104,6 +106,8 @@ static cmd_export_t cmds[] = {
104 104
 		1, 0,  0, ANY_ROUTE},
105 105
 	{"sdp_content",                (cmd_function)w_sdp_content,
106 106
 		0, 0,  0, ANY_ROUTE},
107
+	{"sdp_content",                (cmd_function)w_sdp_content_sloppy,
108
+		1, 0,  0, ANY_ROUTE},
107 109
 	{"sdp_with_ice",                (cmd_function)w_sdp_with_ice,
108 110
 		0, 0,  0, ANY_ROUTE},
109 111
 	{"sdp_get_line_startswith", (cmd_function)w_sdp_get_line_startswith,
... ...
@@ -1521,6 +1525,66 @@ static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar)
1521 1521
 	return -1;
1522 1522
 }
1523 1523
 
1524
+/*
1525
+ * Find the first case insensitive occurrence of find in s, where the
1526
+ * search is limited to the first slen characters of s.
1527
+ * Based on FreeBSD strnstr.
1528
+ */
1529
+char* strnistr(const char *s, const char *find, size_t slen)
1530
+{
1531
+	char c, sc;
1532
+	size_t len;
1533
+
1534
+	if ((c = *find++) != '\0') {
1535
+		len = strlen(find);
1536
+		do {
1537
+			do {
1538
+				if ((sc = *s++) == '\0' || slen-- < 1)
1539
+					return (NULL);
1540
+			} while (sc != c);
1541
+			if (len > slen)
1542
+				return (NULL);
1543
+		} while (strncasecmp(s, find, len) != 0);
1544
+		s--;
1545
+	}
1546
+	return ((char *)s);
1547
+}
1548
+
1549
+/**
1550
+ *
1551
+ */
1552
+static int w_sdp_content_sloppy(sip_msg_t* msg, char* foo, char *bar)
1553
+{
1554
+	str body;
1555
+	int mime;
1556
+
1557
+	body.s = get_body(msg);
1558
+	if (body.s == NULL) return -1;
1559
+	body.len = msg->len - (int)(body.s - msg->buf);
1560
+	if (body.len == 0) return -1;
1561
+
1562
+	mime = parse_content_type_hdr(msg);
1563
+	if (mime < 0) return -1;  /* error */
1564
+	if (mime == 0) return 1;  /* default is application/sdp */
1565
+
1566
+	switch (((unsigned int)mime) >> 16) {
1567
+	case TYPE_APPLICATION:
1568
+		if ((mime & 0x00ff) == SUBTYPE_SDP) return 1; else return -1;
1569
+	case TYPE_MULTIPART:
1570
+		if ((mime & 0x00ff) == SUBTYPE_MIXED) {
1571
+			if (strnistr(body.s, "application/sdp", body.len) == NULL) {
1572
+				return -1;
1573
+			} else {
1574
+				return 1;
1575
+			}
1576
+		} else {
1577
+			return -1;
1578
+		}
1579
+	default:
1580
+		return -1;
1581
+	}
1582
+}
1583
+
1524 1584
 /**
1525 1585
  *
1526 1586
  */