Module: sip-router
Branch: master
Commit: 6d894662c42d976d011a79947b677497a41fe337
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6d89466…
Author: Juha Heinanen <jh(a)tutpro.com>
Committer: Juha Heinanen <jh(a)tutpro.com>
Date: Tue May 10 09:50:14 2011 +0300
modules_k/siputils: added set_uri_host script function
---
modules_k/siputils/README | 49 ++++++++++++------
modules_k/siputils/checks.c | 81 +++++++++++++++++++++++++++++
modules_k/siputils/checks.h | 5 ++
modules_k/siputils/doc/siputils_admin.xml | 26 +++++++++-
modules_k/siputils/siputils.c | 2 +
5 files changed, 145 insertions(+), 18 deletions(-)
diff --git a/modules_k/siputils/README b/modules_k/siputils/README
index 8dd3963..ccb849e 100644
--- a/modules_k/siputils/README
+++ b/modules_k/siputils/README
@@ -91,8 +91,9 @@ Gabriel Vasile
4.18. append_rpid_hf(prefix, suffix)
4.19. is_rpid_user_e164()
4.20. set_uri_user(uri, user)
- 4.21. is_request()
- 4.22. is_reply()
+ 4.21. set_uri_host(uri, host)
+ 4.22. is_request()
+ 4.23. is_reply()
List of Examples
@@ -125,8 +126,9 @@ Gabriel Vasile
1.27. append_rpid_hf(prefix, suffix) usage
1.28. is_rpid_user_e164 usage
1.29. set_uri_user usage
- 1.30. is_request usage
- 1.31. is_reply usage
+ 1.30. set_uri_host usage
+ 1.31. is_request usage
+ 1.32. is_reply usage
Chapter 1. Admin Guide
@@ -172,8 +174,9 @@ Chapter 1. Admin Guide
4.18. append_rpid_hf(prefix, suffix)
4.19. is_rpid_user_e164()
4.20. set_uri_user(uri, user)
- 4.21. is_request()
- 4.22. is_reply()
+ 4.21. set_uri_host(uri, host)
+ 4.22. is_request()
+ 4.23. is_reply()
1. Overview
@@ -368,8 +371,9 @@ modparam("auth", "rpid_avp",
"$avp(myrpid)")
4.18. append_rpid_hf(prefix, suffix)
4.19. is_rpid_user_e164()
4.20. set_uri_user(uri, user)
- 4.21. is_request()
- 4.22. is_reply()
+ 4.21. set_uri_host(uri, host)
+ 4.22. is_request()
+ 4.23. is_reply()
4.1. ring_insert_callid()
@@ -732,11 +736,10 @@ if (is_rpid_user_e164()) {
4.20. set_uri_user(uri, user)
- Sets userpart of SIP URI stored in writable pseudo variable uri to
- value of pseudo variable user.
+ Sets userpart of SIP URI stored in writable pseudo variable 'uri' to
+ value of pseudo variable 'user'.
- This function can be used from REQUEST_ROUTE, BRANCH_ROUTE,
- FAILURE_ROUTE, and ONREPLY_ROUTE.
+ This function can be used from ANY_ROUTE.
Example 1.29. set_uri_user usage
...
@@ -745,26 +748,40 @@ $var(user) = "new_user";
set_uri_user("$var(uri)", "$var(user)");
...
-4.21. is_request()
+4.21. set_uri_host(uri, host)
+
+ Sets hostpart of SIP URI stored in writable pseudo variable 'uri' to
+ value of pseudo variable 'host'.
+
+ This function can be used from ANY_ROUTE.
+
+ Example 1.30. set_uri_host usage
+...
+$var(uri) = "sip:user@host";
+$var(host) = "new_host";
+set_uri_host("$var(uri)", "$var(host)");
+...
+
+4.22. is_request()
Return true if the SIP message is a request.
This function can be used from ANY_ROUTE.
- Example 1.30. is_request usage
+ Example 1.31. is_request usage
...
if (is_request()) {
...
}
...
-4.22. is_reply()
+4.23. is_reply()
Return true if the SIP message is a reply.
This function can be used from ANY_ROUTE.
- Example 1.31. is_reply usage
+ Example 1.32. is_reply usage
...
if (is_reply()) {
...
diff --git a/modules_k/siputils/checks.c b/modules_k/siputils/checks.c
index 83341a4..6ccac04 100644
--- a/modules_k/siputils/checks.c
+++ b/modules_k/siputils/checks.c
@@ -583,3 +583,84 @@ int set_uri_user(struct sip_msg* _m, char* _uri, char* _value)
return 1;
}
+
+/*
+ * Set hostpart of URI
+ */
+int set_uri_host(struct sip_msg* _m, char* _uri, char* _value)
+{
+ pv_spec_t *uri_pv, *value_pv;
+ pv_value_t uri_val, value_val, res_val;
+ str uri, value;
+ char *at, *colon, *c, *next;
+ unsigned int host_len;
+ char new_uri[MAX_URI_SIZE + 1];
+
+ uri_pv = (pv_spec_t *)_uri;
+ if (uri_pv && (pv_get_spec_value(_m, uri_pv, &uri_val) == 0)) {
+ if (uri_val.flags & PV_VAL_STR) {
+ if (uri_val.rs.len == 0 || uri_val.rs.s == NULL) {
+ LM_ERR("missing uri value\n");
+ return -1;
+ }
+ } else {
+ LM_ERR("uri value is not string\n");
+ return -1;
+ }
+ } else {
+ LM_ERR("failed to get uri value\n");
+ return -1;
+ }
+ uri = uri_val.rs;
+
+ value_pv = (pv_spec_t *)_value;
+ if (value_pv && (pv_get_spec_value(_m, value_pv, &value_val) == 0)) {
+ if (value_val.flags & PV_VAL_STR) {
+ if (value_val.rs.s == NULL) {
+ LM_ERR("missing uri value\n");
+ return -1;
+ }
+ } else {
+ LM_ERR("uri value is not string\n");
+ return -1;
+ }
+ } else {
+ LM_ERR("failed to get uri value\n");
+ return -1;
+ }
+ value = value_val.rs;
+
+ if (value.len == 0) {
+ LM_ERR("hostpart of uri cannot be empty\n");
+ return -1;
+ }
+ if (uri.len + value.len > MAX_URI_SIZE) {
+ LM_ERR("resulting uri would be too large\n");
+ return -1;
+ }
+
+ colon = strchr(uri.s, ':');
+ if (colon == NULL) {
+ LM_ERR("uri does not contain ':' character\n");
+ return -1;
+ }
+ c = &(new_uri[0]);
+ at = strchr(colon + 1, '@');
+ if (at == NULL) {
+ next = colon + 1;
+ } else {
+ next = at + 1;
+ }
+ append_str(c, uri.s, next - uri.s);
+ host_len = strcspn(next, ":;?");
+ append_str(c, value.s, value.len);
+ strcpy(c, next + host_len);
+ res_val.rs.len = uri.len + value.len - host_len;
+ res_val.rs.s = &(new_uri[0]);
+
+ LM_DBG("resulting uri: %.*s\n", res_val.rs.len, res_val.rs.s);
+ res_val.flags = PV_VAL_STR;
+ uri_pv->setf(_m, &uri_pv->pvp, (int)EQ_T, &res_val);
+
+ return 1;
+}
diff --git a/modules_k/siputils/checks.h b/modules_k/siputils/checks.h
index 42a79e3..706e54b 100644
--- a/modules_k/siputils/checks.h
+++ b/modules_k/siputils/checks.h
@@ -97,6 +97,11 @@ int is_e164(struct sip_msg* _m, char* _sp, char* _s2);
int set_uri_user(struct sip_msg* _m, char* _uri, char* _value);
/*
+ * Set hostpart of URI
+ */
+int set_uri_host(struct sip_msg* _m, char* _uri, char* _value);
+
+/*
* Return true (1) if SIP message is request, otherwise false (-1)
*/
int w_is_request(struct sip_msg* msg, char *foo, char *bar);
diff --git a/modules_k/siputils/doc/siputils_admin.xml
b/modules_k/siputils/doc/siputils_admin.xml
index 323a55f..f479d66 100644
--- a/modules_k/siputils/doc/siputils_admin.xml
+++ b/modules_k/siputils/doc/siputils_admin.xml
@@ -850,10 +850,10 @@ if (is_rpid_user_e164()) {
</title>
<para>
Sets userpart of SIP URI stored in writable pseudo variable
- uri to value of pseudo variable user.
+ 'uri' to value of pseudo variable 'user'.
</para>
<para>
- This function can be used from REQUEST_ROUTE, BRANCH_ROUTE, FAILURE_ROUTE, and
ONREPLY_ROUTE.
+ This function can be used from ANY_ROUTE.
</para>
<example>
<title>set_uri_user usage</title>
@@ -868,6 +868,28 @@ set_uri_user("$var(uri)", "$var(user)");
</section>
<section>
<title>
+ <function moreinfo="none">set_uri_host(uri, host)</function>
+ </title>
+ <para>
+ Sets hostpart of SIP URI stored in writable pseudo variable
+ 'uri' to value of pseudo variable 'host'.
+ </para>
+ <para>
+ This function can be used from ANY_ROUTE.
+ </para>
+ <example>
+ <title>set_uri_host usage</title>
+ <programlisting format="linespecific">
+...
+$var(uri) = "sip:user@host";
+$var(host) = "new_host";
+set_uri_host("$var(uri)", "$var(host)");
+...
+</programlisting>
+ </example>
+ </section>
+ <section>
+ <title>
<function moreinfo="none">is_request()</function>
</title>
<para>
diff --git a/modules_k/siputils/siputils.c b/modules_k/siputils/siputils.c
index e018b14..e50fa3d 100644
--- a/modules_k/siputils/siputils.c
+++ b/modules_k/siputils/siputils.c
@@ -152,6 +152,8 @@ static cmd_export_t cmds[]={
0, REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
{"set_uri_user", (cmd_function)set_uri_user, 2, fixup_set_uri,
fixup_free_set_uri, ANY_ROUTE},
+ {"set_uri_host", (cmd_function)set_uri_host, 2, fixup_set_uri,
+ fixup_free_set_uri, ANY_ROUTE},
{"bind_siputils", (cmd_function)bind_siputils, 0, 0,
0, 0},
{"is_request", (cmd_function)w_is_request, 0, 0,