[sr-dev] git:ez/sdpops: modules/sdpops: added sdp_get_line_startswith(avpvar, string ) - fetch 'string' in SDP body and store line in an AVP.
Konstantin Mosesov
ez at voipgroup.org.ua
Sat May 11 20:27:34 CEST 2013
Module: sip-router
Branch: ez/sdpops
Commit: ac324a6163fecd30be24074cd34778b6d600a225
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=ac324a6163fecd30be24074cd34778b6d600a225
Author: Konstantin Mosesov <ez at voipgroup.org.ua>
Committer: Konstantin Mosesov <ez at voipgroup.org.ua>
Date: Sat May 11 20:49:41 2013 +0300
modules/sdpops: added sdp_get_line_startswith(avpvar, string) - fetch 'string' in SDP body and store line in an AVP.
---
modules/sdpops/doc/sdpops_admin.xml | 22 +++++++
modules/sdpops/sdpops_mod.c | 110 +++++++++++++++++++++++++++++++++++
2 files changed, 132 insertions(+), 0 deletions(-)
diff --git a/modules/sdpops/doc/sdpops_admin.xml b/modules/sdpops/doc/sdpops_admin.xml
index 831669e..d644625 100644
--- a/modules/sdpops/doc/sdpops_admin.xml
+++ b/modules/sdpops/doc/sdpops_admin.xml
@@ -366,6 +366,28 @@ if(sdp_content()) {
</programlisting>
</example>
</section>
+ <section>
+ <title>
+ <function moreinfo="none">sdp_get_line_startswith(avpvar, string)</function>
+ </title>
+ <para>
+ Store the search part of SDP body message with line beginning with 'string' in an AVP.
+ Return 1 if 'string' is found in SDP, -1 on error and -2 if there is no SDP part in the message body.
+ </para>
+ <para>
+ This function can be used from ANY_ROUTE.
+ </para>
+ <example>
+ <title><function>sdp_get_line_startswith</function> usage</title>
+ <programlisting format="linespecific">
+...
+if(sdp_get_line_startswith("m=", "$avp(mline)")) {
+ xlog("m-line: $avp(mline)\n");
+}
+...
+</programlisting>
+ </example>
+ </section>
</section>
</chapter>
diff --git a/modules/sdpops/sdpops_mod.c b/modules/sdpops/sdpops_mod.c
index 2d2e668..de6914b 100644
--- a/modules/sdpops/sdpops_mod.c
+++ b/modules/sdpops/sdpops_mod.c
@@ -52,6 +52,8 @@ static int w_sdp_remove_media(sip_msg_t* msg, char* media, char *bar);
static int w_sdp_print(sip_msg_t* msg, char* level, char *bar);
static int w_sdp_get(sip_msg_t* msg, char *bar);
static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar);
+static int w_sdp_get_line_startswith(sip_msg_t* msg, char *foo, char* bar);
+
static int mod_init(void);
@@ -84,6 +86,8 @@ static cmd_export_t cmds[] = {
1, 0, 0, ANY_ROUTE},
{"sdp_content", (cmd_function)w_sdp_content,
0, 0, 0, ANY_ROUTE},
+ {"sdp_get_line_startswith", (cmd_function)w_sdp_get_line_startswith,
+ 2, 0, 0, ANY_ROUTE},
{"bind_sdpops", (cmd_function)bind_sdpops,
1, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
@@ -1122,6 +1126,112 @@ static int w_sdp_content(sip_msg_t* msg, char* foo, char *bar)
/**
*
*/
+static int w_sdp_get_line_startswith(sip_msg_t *msg, char *avp, char *prefix)
+{
+ str body = {NULL, 0};
+ str line = {NULL, 0};
+ char* p = NULL;
+ str s;
+ str pprefix;
+ int_str avp_val;
+ int_str avp_name;
+ pv_spec_t *avp_spec = NULL;
+ static unsigned short avp_type = 0;
+
+ if (prefix == NULL || strlen(prefix) <= 0)
+ {
+ LM_ERR("Prefix is null or empty\n");
+ return -1;
+ }
+ pprefix.s = prefix;
+ pprefix.len = strlen(prefix);
+
+ if(parse_sdp(msg) < 0) {
+ LM_ERR("Unable to parse sdp\n");
+ return -1;
+ }
+
+ body.s = ((sdp_info_t*)msg->body)->raw_sdp.s;
+ body.len = ((sdp_info_t*)msg->body)->raw_sdp.len;
+
+ if (body.s==NULL) {
+ LM_ERR("failed to get the message body\n");
+ return -1;
+ }
+ body.len = msg->len - (body.s - msg->buf);
+ if (body.len==0) {
+ LM_DBG("message body has zero length\n");
+ return -1;
+ }
+
+ if (avp == NULL || strlen(avp) <= 0)
+ {
+ LM_ERR("avp variable is null or empty\n");
+ return -1;
+ }
+
+ s.s = avp;
+ s.len = strlen(s.s);
+
+ if (pv_locate_name(&s) != s.len)
+ {
+ LM_ERR("invalid parameter\n");
+ return -1;
+ }
+
+ if (((avp_spec = pv_cache_get(&s)) == NULL)
+ || avp_spec->type!=PVT_AVP) {
+ LM_ERR("malformed or non AVP %s AVP definition\n", avp);
+ return -1;
+ }
+
+ if(pv_get_avp_name(0, &avp_spec->pvp, &avp_name, &avp_type)!=0)
+ {
+ LM_ERR("[%s]- invalid AVP definition\n", avp);
+ return -1;
+ }
+
+ p = find_sdp_line(body.s, body.s+body.len, pprefix.s[0]);
+ while (p != NULL)
+ {
+ if (sdp_locate_line(msg, p, &line) != 0)
+ {
+ LM_ERR("sdp_locate_line fail\n");
+ return -1;
+ }
+
+ if (strncmp(line.s, pprefix.s, pprefix.len) == 0)
+ {
+ avp_val.s.s = line.s;
+ avp_val.s.len = line.len;
+
+ // removed ending \r\n if exists
+ if (avp_val.s.s[line.len-2] == '\r' && avp_val.s.s[line.len-1] == '\n')
+ {
+ avp_val.s.s[line.len-2] = '\0';
+ avp_val.s.len -= 2;
+ }
+
+
+ if (add_avp(AVP_VAL_STR | avp_type, avp_name, avp_val) != 0)
+ {
+ LM_ERR("Failed to add SDP line avp");
+ return -1;
+ }
+
+ return 1;
+ }
+
+ p = find_sdp_line(line.s + line.len, body.s + body.len, pprefix.s[0]);
+ }
+
+ return 0;
+}
+
+
+/**
+ *
+ */
int bind_sdpops(struct sdpops_binds *sob){
if (sob == NULL) {
LM_WARN("bind_sdpops: Cannot load sdpops API into a NULL pointer\n");
More information about the sr-dev
mailing list