Browse code

modules/sdpops: Added new function sdp_get_line_startswith(avpvar, string).

Konstantin Mosesov authored on 17/05/2013 18:35:59
Showing 3 changed files
... ...
@@ -10,7 +10,7 @@ Daniel-Constantin Mierla
10 10
 
11 11
    <miconda@gmail.com>
12 12
 
13
-   Copyright � 2011 asipto.com
13
+   Copyright © 2011 asipto.com
14 14
      __________________________________________________________________
15 15
 
16 16
    Table of Contents
... ...
@@ -38,6 +38,7 @@ Daniel-Constantin Mierla
38 38
               4.10. sdp_print(level)
39 39
               4.11. sdp_get(avpvar)
40 40
               4.12. sdp_content()
41
+              4.13. sdp_get_line_startswith(avpvar, string)
41 42
 
42 43
    List of Examples
43 44
 
... ...
@@ -53,6 +54,7 @@ Daniel-Constantin Mierla
53 54
    1.10. sdp_print usage
54 55
    1.11. sdp_get usage
55 56
    1.12. sdp_content usage
57
+   1.13. sdp_get_line_startswith usage
56 58
 
57 59
 Chapter 1. Admin Guide
58 60
 
... ...
@@ -79,6 +81,7 @@ Chapter 1. Admin Guide
79 81
         4.10. sdp_print(level)
80 82
         4.11. sdp_get(avpvar)
81 83
         4.12. sdp_content()
84
+        4.13. sdp_get_line_startswith(avpvar, string)
82 85
 
83 86
 1. Overview
84 87
 
... ...
@@ -124,6 +127,7 @@ Chapter 1. Admin Guide
124 127
    4.10. sdp_print(level)
125 128
    4.11. sdp_get(avpvar)
126 129
    4.12. sdp_content()
130
+   4.13. sdp_get_line_startswith(avpvar, string)
127 131
 
128 132
 4.1.  sdp_remove_codecs_by_id(list)
129 133
 
... ...
@@ -319,3 +323,18 @@ if(sdp_content()) {
319 323
     ...
320 324
 }
321 325
 ...
326
+
327
+4.13.  sdp_get_line_startswith(avpvar, string)
328
+
329
+   Store the search part of SDP body message with line beginning with
330
+   'string' in an AVP. Return 1 if 'string' is found in SDP, -1 on error
331
+   and -2 if there is no SDP part in the message body.
332
+
333
+   This function can be used from ANY_ROUTE.
334
+
335
+   Example 1.13. sdp_get_line_startswith usage
336
+...
337
+if(sdp_get_line_startswith("$avp(mline)", "m=")) {
338
+        xlog("m-line: $avp(mline)\n");
339
+}
340
+...
... ...
@@ -366,6 +366,28 @@ if(sdp_content()) {
366 366
 </programlisting>
367 367
 	    </example>
368 368
 	</section>
369
+	<section>
370
+		<title>
371
+			<function moreinfo="none">sdp_get_line_startswith(avpvar, string)</function>
372
+		</title>
373
+		<para>
374
+			Store the search part of SDP body message with line beginning with 'string' in an AVP. 
375
+			Return 1 if 'string' is found in SDP, -1 on error and -2 if there is no SDP part in the message body.
376
+		</para>
377
+		<para>
378
+			This function can be used from ANY_ROUTE.
379
+		</para>
380
+		<example>
381
+			<title><function>sdp_get_line_startswith</function> usage</title>
382
+			<programlisting format="linespecific">
383
+...
384
+if(sdp_get_line_startswith("$avp(mline)", "m=")) {
385
+	xlog("m-line: $avp(mline)\n");
386
+}
387
+...
388
+</programlisting>
389
+		</example>
390
+	</section>
369 391
 	</section>
370 392
 </chapter>
371 393
 
... ...
@@ -30,6 +30,7 @@
30 30
 #include "../../dprint.h"
31 31
 #include "../../mod_fix.h"
32 32
 #include "../../pvar.h"
33
+#include "../../usr_avp.h"
33 34
 #include "../../parser/sdp/sdp.h"
34 35
 #include "../../parser/sdp/sdp_helpr_funcs.h"
35 36
 #include "../../trim.h"
... ...
@@ -52,6 +53,8 @@ static int w_sdp_remove_media(sip_msg_t* msg, char* media, char *bar);
52 53
 static int w_sdp_print(sip_msg_t* msg, char* level, char *bar);
53 54
 static int w_sdp_get(sip_msg_t* msg, char *bar);
54 55
 static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar);
56
+static int w_sdp_get_line_startswith(sip_msg_t* msg, char *foo, char *bar);
57
+
55 58
 
56 59
 static int mod_init(void);
57 60
 
... ...
@@ -84,6 +87,8 @@ static cmd_export_t cmds[] = {
84 87
 		1, 0,  0, ANY_ROUTE},
85 88
 	{"sdp_content",                (cmd_function)w_sdp_content,
86 89
 		0, 0,  0, ANY_ROUTE},
90
+	{"sdp_get_line_startswith", (cmd_function)w_sdp_get_line_startswith,
91
+		2, 0,  0, ANY_ROUTE},
87 92
 	{"bind_sdpops",                (cmd_function)bind_sdpops,
88 93
 		1, 0, 0, 0},
89 94
 	{0, 0, 0, 0, 0, 0}
... ...
@@ -1119,6 +1124,123 @@ static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar)
1119 1124
 	return -1;
1120 1125
 }
1121 1126
 
1127
+/**
1128
+ *
1129
+ */
1130
+static int w_sdp_get_line_startswith(sip_msg_t *msg, char *avp, char *s_line)
1131
+{
1132
+	sdp_info_t *sdp = NULL;
1133
+	str body = {NULL, 0};
1134
+	str line = {NULL, 0};
1135
+	char* p = NULL;
1136
+	str s;
1137
+	str sline;
1138
+        int_str avp_val;
1139
+        int_str avp_name;
1140
+        pv_spec_t *avp_spec = NULL;
1141
+        static unsigned short avp_type = 0;
1142
+	int sdp_missing=1;
1143
+
1144
+	if (s_line == NULL || strlen(s_line) <= 0)
1145
+	{
1146
+		LM_ERR("Search string is null or empty\n");
1147
+		    return -1;
1148
+	}
1149
+	sline.s = s_line;
1150
+	sline.len = strlen(s_line);
1151
+
1152
+	sdp_missing = parse_sdp(msg);
1153
+
1154
+	if(sdp_missing < 0) {
1155
+		LM_ERR("Unable to parse sdp\n");
1156
+		return -1;
1157
+	}
1158
+
1159
+	sdp = (sdp_info_t *)msg->body;
1160
+
1161
+        if (sdp_missing || sdp == NULL)
1162
+	{
1163
+                LM_DBG("No SDP\n");
1164
+                return -2;
1165
+	}
1166
+
1167
+	body.s = sdp->raw_sdp.s;
1168
+	body.len = sdp->raw_sdp.len;
1169
+
1170
+	if (body.s==NULL) {
1171
+		LM_ERR("failed to get the message body\n");
1172
+		return -1;
1173
+	}
1174
+
1175
+	body.len = msg->len - (body.s - msg->buf);
1176
+	if (body.len==0) {
1177
+		LM_DBG("message body has zero length\n");
1178
+		return -1;
1179
+	}
1180
+
1181
+	if (avp == NULL || strlen(avp) <= 0)
1182
+	{
1183
+		LM_ERR("avp variable is null or empty\n");
1184
+		    return -1;
1185
+	}
1186
+
1187
+	s.s = avp;
1188
+	s.len = strlen(s.s);
1189
+
1190
+	if (pv_locate_name(&s) != s.len)
1191
+        {
1192
+                LM_ERR("invalid parameter\n");
1193
+                return -1;
1194
+        }
1195
+
1196
+        if (((avp_spec = pv_cache_get(&s)) == NULL)
1197
+                        || avp_spec->type!=PVT_AVP) {
1198
+                LM_ERR("malformed or non AVP %s AVP definition\n", avp);
1199
+                return -1;
1200
+        }
1201
+
1202
+        if(pv_get_avp_name(0, &avp_spec->pvp, &avp_name, &avp_type)!=0)
1203
+        {
1204
+                LM_ERR("[%s]- invalid AVP definition\n", avp);
1205
+                return -1;
1206
+        }
1207
+
1208
+	p = find_sdp_line(body.s, body.s+body.len, sline.s[0]);
1209
+	while (p != NULL)
1210
+	{
1211
+		if (sdp_locate_line(msg, p, &line) != 0)
1212
+		{
1213
+			LM_ERR("sdp_locate_line fail\n");
1214
+			return -1;
1215
+		}
1216
+
1217
+		if (strncmp(line.s, sline.s, sline.len) == 0)
1218
+		{
1219
+            		avp_val.s.s = line.s;
1220
+            		avp_val.s.len = line.len;
1221
+
1222
+			// remove ending \r\n if exists
1223
+			if (avp_val.s.s[line.len-2] == '\r' && avp_val.s.s[line.len-1] == '\n')
1224
+			{
1225
+			    avp_val.s.s[line.len-2] = '\0';
1226
+			    avp_val.s.len -= 2;
1227
+			}
1228
+
1229
+    			if (add_avp(AVP_VAL_STR | avp_type, avp_name, avp_val) != 0)
1230
+    			{
1231
+        		    LM_ERR("Failed to add SDP line avp");
1232
+        		    return -1;
1233
+    			}
1234
+
1235
+			return 1;
1236
+		}
1237
+
1238
+		p = find_sdp_line(line.s + line.len, body.s + body.len, sline.s[0]);
1239
+	}
1240
+
1241
+	return 0;
1242
+}
1243
+
1122 1244
 /**
1123 1245
  *
1124 1246
  */