Module: sip-router
Branch: master
Commit: 203bbc38cb7bca36878b561ea6e6f58d17f07c71
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=203bbc3…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Thu Aug 12 15:47:12 2010 +0200
pv: more To hdr attributes allowed in assignment
- values of username, domain and display name of To header can be set
via PV assignmet ($tU, $td, $tn)
- use carefully, don't do many assignments to these variables for same
message - it can results in strange output due to lumps system used
behind. However, you can do msg_apply_changes() after one assignment
---
modules_k/pv/pv.c | 12 ++--
modules_k/pv/pv_core.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++--
modules_k/pv/pv_core.h | 9 ++++
3 files changed, 133 insertions(+), 10 deletions(-)
diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index 2878c77..dece906 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -328,13 +328,13 @@ static pv_export_t mod_pvs[] = {
PVT_OTHER, pv_get_srcport, 0,
0, 0, 0, 0},
{{"td", (sizeof("td")-1)}, /* */
- PVT_OTHER, pv_get_to_attr, 0,
+ PVT_OTHER, pv_get_to_attr, pv_set_to_domain,
0, 0, pv_init_iname, 3},
{{"to.domain", (sizeof("to.domain")-1)}, /* */
- PVT_OTHER, pv_get_to_attr, 0,
+ PVT_OTHER, pv_get_to_attr, pv_set_to_domain,
0, 0, pv_init_iname, 3},
{{"tn", (sizeof("tn")-1)}, /* */
- PVT_OTHER, pv_get_to_attr, 0,
+ PVT_OTHER, pv_get_to_attr, pv_set_to_display,
0, 0, pv_init_iname, 5},
{{"tt", (sizeof("tt")-1)}, /* */
PVT_OTHER, pv_get_to_attr, 0,
@@ -343,13 +343,13 @@ static pv_export_t mod_pvs[] = {
PVT_TO, pv_get_to_attr, pv_set_to_uri,
0, 0, pv_init_iname, 1},
{{"to", (sizeof("to")-1)}, /* */
- PVT_TO, pv_get_to_attr, 0,
+ PVT_TO, pv_get_to_attr, pv_set_to_uri,
0, 0, pv_init_iname, 1},
{{"tU", (sizeof("tU")-1)}, /* */
- PVT_OTHER, pv_get_to_attr, 0,
+ PVT_OTHER, pv_get_to_attr, pv_set_to_username,
0, 0, pv_init_iname, 2},
{{"to.user", (sizeof("to.user")-1)}, /* */
- PVT_OTHER, pv_get_to_attr, 0,
+ PVT_OTHER, pv_get_to_attr, pv_set_to_username,
0, 0, pv_init_iname, 2},
{{"true", (sizeof("true")-1)}, /* */
PVT_OTHER, pv_get_true, 0,
diff --git a/modules_k/pv/pv_core.c b/modules_k/pv/pv_core.c
index 2b1eede..d434d70 100644
--- a/modules_k/pv/pv_core.c
+++ b/modules_k/pv/pv_core.c
@@ -2057,15 +2057,108 @@ int pv_set_xto_attr(struct sip_msg* msg, pv_param_t *param,
goto error;
}
buf.len = val->rs.len;
- memcpy(buf.s, val->rs.s, val->rs.len);
+ memcpy(buf.s, val->rs.s, val->rs.len);
loffset = tb->uri.s - msg->buf;
llen = tb->uri.len;
break;
case 1: /* username */
+ if(val == NULL || (val->flags&PV_VAL_NULL))
+ {
+ if(tb->parsed_uri.user.len==0)
+ return 0; /* nothing to delete */
+ /* delete username */
+ loffset = tb->parsed_uri.user.s - msg->buf;
+ llen = tb->parsed_uri.user.len;
+ /* delete '@' after */
+ if(tb->parsed_uri.user.s[tb->parsed_uri.user.len]=='@')
+ llen++;
+ break;
+ }
+ if(!(val->flags&PV_VAL_STR))
+ {
+ LM_ERR("attempt to assign non-str value to To header"
+ " display name\n");
+ return -1;
+ }
+ buf.s = pkg_malloc(val->rs.len+1);
+ if (buf.s==0)
+ {
+ LM_ERR("no more pkg mem\n");
+ goto error;
+ }
+ buf.len = val->rs.len;
+ memcpy(buf.s, val->rs.s, val->rs.len);
+ if(tb->parsed_uri.user.len==0)
+ {
+ l = anchor_lump(msg, tb->parsed_uri.host.s - msg->buf, 0, 0);
+ buf.s[buf.len] = '@';
+ buf.len++;
+ } else {
+ /* delete username */
+ loffset = tb->parsed_uri.user.s - msg->buf;
+ llen = tb->parsed_uri.user.len;
+ }
break;
case 2: /* domain */
+ if(val == NULL || (val->flags&PV_VAL_NULL))
+ {
+ LM_WARN("To header URI domain cannot be deleted\n");
+ return 0;
+ }
+ if(!(val->flags&PV_VAL_STR))
+ {
+ LM_ERR("attempt to assign non-str value to To header"
+ " URI domain\n");
+ return -1;
+ }
+ buf.s = pkg_malloc(val->rs.len);
+ if (buf.s==0)
+ {
+ LM_ERR("no more pkg mem\n");
+ goto error;
+ }
+ buf.len = val->rs.len;
+ memcpy(buf.s, val->rs.s, val->rs.len);
+ loffset = tb->parsed_uri.host.s - msg->buf;
+ llen = tb->parsed_uri.host.len;
break;
case 3: /* display */
+ if(val == NULL || (val->flags&PV_VAL_NULL))
+ {
+ if(tb->display.len==0)
+ return 0; /* nothing to delete */
+ /* delete display */
+ loffset = tb->display.s - msg->buf;
+ llen = tb->display.len;
+ /* delete whitespace after */
+ if(tb->display.s[tb->display.len]==' ')
+ llen++;
+ break;
+ }
+ if(!(val->flags&PV_VAL_STR))
+ {
+ LM_ERR("attempt to assign non-str value to To header"
+ " display name\n");
+ return -1;
+ }
+ buf.s = pkg_malloc(val->rs.len+1);
+ if (buf.s==0)
+ {
+ LM_ERR("no more pkg mem\n");
+ goto error;
+ }
+ buf.len = val->rs.len;
+ memcpy(buf.s, val->rs.s, val->rs.len);
+ if(tb->display.len==0)
+ {
+ l = anchor_lump(msg, tb->body.s - msg->buf, 0, 0);
+ buf.s[buf.len] = ' ';
+ buf.len++;
+ } else {
+ /* delete display */
+ loffset = tb->display.s - msg->buf;
+ llen = tb->display.len;
+ }
break;
}
@@ -2079,7 +2172,7 @@ int pv_set_xto_attr(struct sip_msg* msg, pv_param_t *param,
}
}
/* set new value when given */
- if(buf.len>0)
+ if(l!=NULL && buf.len>0)
{
if (insert_new_lump_after(l, buf.s, buf.len, 0)==0)
{
@@ -2101,8 +2194,7 @@ int pv_set_to_attr(struct sip_msg* msg, pv_param_t *param,
if(msg==NULL)
return -1;
- if(msg->to==NULL && parse_headers(msg, HDR_TO_F, 0)==-1)
- {
+ if(msg->to==NULL && parse_headers(msg, HDR_TO_F, 0)==-1) {
LM_ERR("cannot parse To header\n");
return -1;
}
@@ -2110,6 +2202,10 @@ int pv_set_to_attr(struct sip_msg* msg, pv_param_t *param,
LM_DBG("no To header\n");
return -1;
}
+ if(parse_to_uri(msg)==NULL) {
+ LM_ERR("cannot parse To header\n");
+ return -1;
+ }
return pv_set_xto_attr(msg, param, op, val, get_to(msg), type);
}
@@ -2119,6 +2215,24 @@ int pv_set_to_uri(struct sip_msg* msg, pv_param_t *param,
return pv_set_to_attr(msg, param, op, val, 0);
}
+int pv_set_to_username(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val)
+{
+ return pv_set_to_attr(msg, param, op, val, 1);
+}
+
+int pv_set_to_domain(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val)
+{
+ return pv_set_to_attr(msg, param, op, val, 2);
+}
+
+int pv_set_to_display(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val)
+{
+ return pv_set_to_attr(msg, param, op, val, 3);
+}
+
/********* end PV set functions *********/
int pv_parse_scriptvar_name(pv_spec_p sp, str *in)
diff --git a/modules_k/pv/pv_core.h b/modules_k/pv/pv_core.h
index 0c5ce4c..418632a 100644
--- a/modules_k/pv/pv_core.h
+++ b/modules_k/pv/pv_core.h
@@ -235,6 +235,15 @@ int pv_set_bflags(struct sip_msg* msg, pv_param_t *param,
int pv_set_to_uri(struct sip_msg* msg, pv_param_t *param,
int op, pv_value_t *val);
+int pv_set_to_username(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val);
+
+int pv_set_to_domain(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val);
+
+int pv_set_to_display(struct sip_msg* msg, pv_param_t *param,
+ int op, pv_value_t *val);
+
/********* end PV set functions *********/
int pv_parse_scriptvar_name(pv_spec_p sp, str *in);