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 53
    1.10. sdp_print usage
54 54
    1.11. sdp_get usage
55 55
    1.12. sdp_content usage
56
+   1.13. sdp_get_line_startswith usage
56 57
 
57 58
 Chapter 1. Admin Guide
58 59
 
... ...
@@ -79,6 +81,7 @@ Chapter 1. Admin Guide
79 79
         4.10. sdp_print(level)
80 80
         4.11. sdp_get(avpvar)
81 81
         4.12. sdp_content()
82
+        4.13. sdp_get_line_startswith(avpvar, string)
82 83
 
83 84
 1. Overview
84 85
 
... ...
@@ -124,6 +127,7 @@ Chapter 1. Admin Guide
124 124
    4.10. sdp_print(level)
125 125
    4.11. sdp_get(avpvar)
126 126
    4.12. sdp_content()
127
+   4.13. sdp_get_line_startswith(avpvar, string)
127 128
 
128 129
 4.1.  sdp_remove_codecs_by_id(list)
129 130
 
... ...
@@ -319,3 +323,18 @@ if(sdp_content()) {
319 319
     ...
320 320
 }
321 321
 ...
322
+
323
+4.13.  sdp_get_line_startswith(avpvar, string)
324
+
325
+   Store the search part of SDP body message with line beginning with
326
+   'string' in an AVP. Return 1 if 'string' is found in SDP, -1 on error
327
+   and -2 if there is no SDP part in the message body.
328
+
329
+   This function can be used from ANY_ROUTE.
330
+
331
+   Example 1.13. sdp_get_line_startswith usage
332
+...
333
+if(sdp_get_line_startswith("$avp(mline)", "m=")) {
334
+        xlog("m-line: $avp(mline)\n");
335
+}
336
+...
... ...
@@ -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 52
 static int w_sdp_print(sip_msg_t* msg, char* level, char *bar);
53 53
 static int w_sdp_get(sip_msg_t* msg, char *bar);
54 54
 static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar);
55
+static int w_sdp_get_line_startswith(sip_msg_t* msg, char *foo, char *bar);
56
+
55 57
 
56 58
 static int mod_init(void);
57 59
 
... ...
@@ -84,6 +87,8 @@ static cmd_export_t cmds[] = {
84 84
 		1, 0,  0, ANY_ROUTE},
85 85
 	{"sdp_content",                (cmd_function)w_sdp_content,
86 86
 		0, 0,  0, ANY_ROUTE},
87
+	{"sdp_get_line_startswith", (cmd_function)w_sdp_get_line_startswith,
88
+		2, 0,  0, ANY_ROUTE},
87 89
 	{"bind_sdpops",                (cmd_function)bind_sdpops,
88 90
 		1, 0, 0, 0},
89 91
 	{0, 0, 0, 0, 0, 0}
... ...
@@ -1122,6 +1127,123 @@ static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar)
1122 1122
 /**
1123 1123
  *
1124 1124
  */
1125
+static int w_sdp_get_line_startswith(sip_msg_t *msg, char *avp, char *s_line)
1126
+{
1127
+	sdp_info_t *sdp = NULL;
1128
+	str body = {NULL, 0};
1129
+	str line = {NULL, 0};
1130
+	char* p = NULL;
1131
+	str s;
1132
+	str sline;
1133
+        int_str avp_val;
1134
+        int_str avp_name;
1135
+        pv_spec_t *avp_spec = NULL;
1136
+        static unsigned short avp_type = 0;
1137
+	int sdp_missing=1;
1138
+
1139
+	if (s_line == NULL || strlen(s_line) <= 0)
1140
+	{
1141
+		LM_ERR("Search string is null or empty\n");
1142
+		    return -1;
1143
+	}
1144
+	sline.s = s_line;
1145
+	sline.len = strlen(s_line);
1146
+
1147
+	sdp_missing = parse_sdp(msg);
1148
+
1149
+	if(sdp_missing < 0) {
1150
+		LM_ERR("Unable to parse sdp\n");
1151
+		return -1;
1152
+	}
1153
+
1154
+	sdp = (sdp_info_t *)msg->body;
1155
+
1156
+        if (sdp_missing || sdp == NULL)
1157
+	{
1158
+                LM_DBG("No SDP\n");
1159
+                return -2;
1160
+	}
1161
+
1162
+	body.s = sdp->raw_sdp.s;
1163
+	body.len = sdp->raw_sdp.len;
1164
+
1165
+	if (body.s==NULL) {
1166
+		LM_ERR("failed to get the message body\n");
1167
+		return -1;
1168
+	}
1169
+
1170
+	body.len = msg->len - (body.s - msg->buf);
1171
+	if (body.len==0) {
1172
+		LM_DBG("message body has zero length\n");
1173
+		return -1;
1174
+	}
1175
+
1176
+	if (avp == NULL || strlen(avp) <= 0)
1177
+	{
1178
+		LM_ERR("avp variable is null or empty\n");
1179
+		    return -1;
1180
+	}
1181
+
1182
+	s.s = avp;
1183
+	s.len = strlen(s.s);
1184
+
1185
+	if (pv_locate_name(&s) != s.len)
1186
+        {
1187
+                LM_ERR("invalid parameter\n");
1188
+                return -1;
1189
+        }
1190
+
1191
+        if (((avp_spec = pv_cache_get(&s)) == NULL)
1192
+                        || avp_spec->type!=PVT_AVP) {
1193
+                LM_ERR("malformed or non AVP %s AVP definition\n", avp);
1194
+                return -1;
1195
+        }
1196
+
1197
+        if(pv_get_avp_name(0, &avp_spec->pvp, &avp_name, &avp_type)!=0)
1198
+        {
1199
+                LM_ERR("[%s]- invalid AVP definition\n", avp);
1200
+                return -1;
1201
+        }
1202
+
1203
+	p = find_sdp_line(body.s, body.s+body.len, sline.s[0]);
1204
+	while (p != NULL)
1205
+	{
1206
+		if (sdp_locate_line(msg, p, &line) != 0)
1207
+		{
1208
+			LM_ERR("sdp_locate_line fail\n");
1209
+			return -1;
1210
+		}
1211
+
1212
+		if (strncmp(line.s, sline.s, sline.len) == 0)
1213
+		{
1214
+            		avp_val.s.s = line.s;
1215
+            		avp_val.s.len = line.len;
1216
+
1217
+			// remove ending \r\n if exists
1218
+			if (avp_val.s.s[line.len-2] == '\r' && avp_val.s.s[line.len-1] == '\n')
1219
+			{
1220
+			    avp_val.s.s[line.len-2] = '\0';
1221
+			    avp_val.s.len -= 2;
1222
+			}
1223
+
1224
+    			if (add_avp(AVP_VAL_STR | avp_type, avp_name, avp_val) != 0)
1225
+    			{
1226
+        		    LM_ERR("Failed to add SDP line avp");
1227
+        		    return -1;
1228
+    			}
1229
+
1230
+			return 1;
1231
+		}
1232
+
1233
+		p = find_sdp_line(line.s + line.len, body.s + body.len, sline.s[0]);
1234
+	}
1235
+
1236
+	return 0;
1237
+}
1238
+
1239
+/**
1240
+ *
1241
+ */
1125 1242
 int bind_sdpops(struct sdpops_binds *sob){
1126 1243
 	if (sob == NULL) {
1127 1244
 		LM_WARN("bind_sdpops: Cannot load sdpops API into a NULL pointer\n");