Procházet zdrojové kódy

modules_k/siputils: added set_uri_user(uri, user) function.

Juha Heinanen před 15 roky
rodič
revize
dbfd450d4e

+ 40 - 0
modules_k/siputils/README

@@ -85,6 +85,8 @@ Gabriel Vasile
               4.16. is_rpid_user_e164()
               4.16. is_rpid_user_e164()
               4.17. append_rpid_hf()
               4.17. append_rpid_hf()
               4.18. append_rpid_hf(prefix, suffix)
               4.18. append_rpid_hf(prefix, suffix)
+              4.19. is_rpid_user_e164()
+              4.20. set_uri_user(uri, user)
 
 
    List of Examples
    List of Examples
 
 
@@ -115,6 +117,8 @@ Gabriel Vasile
    1.25. is_rpid_user_e164 usage
    1.25. is_rpid_user_e164 usage
    1.26. append_rpid_hf usage
    1.26. append_rpid_hf usage
    1.27. append_rpid_hf(prefix, suffix) usage
    1.27. append_rpid_hf(prefix, suffix) usage
+   1.28. is_rpid_user_e164 usage
+   1.29. set_uri_user usage
 
 
 Chapter 1. Admin Guide
 Chapter 1. Admin Guide
 
 
@@ -158,6 +162,8 @@ Chapter 1. Admin Guide
         4.16. is_rpid_user_e164()
         4.16. is_rpid_user_e164()
         4.17. append_rpid_hf()
         4.17. append_rpid_hf()
         4.18. append_rpid_hf(prefix, suffix)
         4.18. append_rpid_hf(prefix, suffix)
+        4.19. is_rpid_user_e164()
+        4.20. set_uri_user(uri, user)
 
 
 1. Overview
 1. Overview
 
 
@@ -350,6 +356,8 @@ modparam("auth", "rpid_avp", "$avp(myrpid)")
    4.16. is_rpid_user_e164()
    4.16. is_rpid_user_e164()
    4.17. append_rpid_hf()
    4.17. append_rpid_hf()
    4.18. append_rpid_hf(prefix, suffix)
    4.18. append_rpid_hf(prefix, suffix)
+   4.19. is_rpid_user_e164()
+   4.20. set_uri_user(uri, user)
 
 
 4.1.  ring_insert_callid()
 4.1.  ring_insert_callid()
 
 
@@ -689,3 +697,35 @@ append_rpid_hf();  # Append Remote-Party-ID header field
 # Append Remote-Party-ID header field
 # Append Remote-Party-ID header field
 append_rpid_hf("", ";party=calling;id-type=subscriber;screen=yes");
 append_rpid_hf("", ";party=calling;id-type=subscriber;screen=yes");
 ...
 ...
+
+4.19.  is_rpid_user_e164()
+
+   The function checks if the SIP URI received from the database or radius
+   server and will potentially be used in Remote-Party-ID header field
+   contains an E164 number (+followed by up to 15 decimal digits) in its
+   user part. Check fails, if no such SIP URI exists (i.e. radius server
+   or database didn't provide this information).
+
+   This function can be used from REQUEST_ROUTE.
+
+   Example 1.28. is_rpid_user_e164 usage
+...
+if (is_rpid_user_e164()) {
+    # do something here
+};
+...
+
+4.20.  set_uri_user(uri, 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.
+
+   Example 1.29. set_uri_user usage
+...
+$var(uri) = "sip:user@host";
+$var(user) = "new_user";
+set_uri_user("$var(uri)", "$var(user)");
+...

+ 92 - 1
modules_k/siputils/checks.c

@@ -1,10 +1,12 @@
 /*
 /*
  * $Id$
  * $Id$
  *
  *
- * Various URI checks and Request URI manipulation
+ * Various URI checks and URI manipulation
  *
  *
  * Copyright (C) 2001-2003 FhG Fokus
  * Copyright (C) 2001-2003 FhG Fokus
  *
  *
+ * Copyright (C) 2004-2010 Juha Heinanen
+ *
  * This file is part of SIP-router, a free SIP server.
  * This file is part of SIP-router, a free SIP server.
  *
  *
  * SIP-router is free software; you can redistribute it and/or modify
  * SIP-router is free software; you can redistribute it and/or modify
@@ -49,6 +51,7 @@
 #include "../../lib/kcore/parser_helpers.h"
 #include "../../lib/kcore/parser_helpers.h"
 #include "../../dset.h"
 #include "../../dset.h"
 #include "../../pvar.h"
 #include "../../pvar.h"
+#include "../../lvalue.h"
 #include "checks.h"
 #include "checks.h"
 
 
 
 
@@ -454,3 +457,91 @@ int is_uri_user_e164(struct sip_msg* _m, char* _sp, char* _s2)
 	return -1;
 	return -1;
     }
     }
 }
 }
+
+/*
+ * Set userpart of URI
+ */
+int set_uri_user(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;
+    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 uriuser value\n");
+		return -1;
+	    }
+	} else {
+	    LM_ERR("uriuser value is not string\n");
+	    return -1;
+	}
+    } else {
+	LM_ERR("failed to get uriuser value\n");
+	return -1;
+    }
+    value = value_val.rs;
+
+    colon = strchr(uri.s, ':');
+    if (colon == NULL) {
+	LM_ERR("uri does not contain ':' character\n");
+	return -1;
+    }
+    at = strchr(uri.s, '@');
+    c = &(new_uri[0]);
+    if (at == NULL) {
+	if (value.len == 0) return 1;
+	if (uri.len + value.len > MAX_URI_SIZE) {
+	    LM_ERR("resulting uri would be too large\n");
+	    return -1;
+	}
+	append_str(c, uri.s, colon - uri.s + 1);
+	append_str(c, value.s, value.len);
+	append_chr(c, '@');
+	append_str(c, colon + 1, uri.len - (colon - uri.s + 1));
+	res_val.rs.len = uri.len + value.len + 1;
+    } else {
+	if (value.len == 0) {
+	    append_str(c, uri.s, colon - uri.s + 1);
+	    append_str(c, at + 1, uri.len - (at - uri.s + 1));
+	    res_val.rs.len = uri.len - (at - colon);
+	} else {
+	    if (uri.len + value.len - (at - colon - 1) > MAX_URI_SIZE) {
+		LM_ERR("resulting uri would be too large\n");
+		return -1;
+	    }
+	    append_str(c, uri.s, colon - uri.s + 1);
+	    append_str(c, value.s, value.len);
+	    append_str(c, at, uri.len - (at - uri.s));
+	    res_val.rs.len = uri.len + value.len - (at - colon - 1);
+	}
+    }
+
+    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;
+}

+ 5 - 0
modules_k/siputils/checks.h

@@ -90,4 +90,9 @@ int is_uri_user_e164(struct sip_msg* _m, char* _sp, char* _s2);
  */
  */
 int is_e164(struct sip_msg* _m, char* _sp, char* _s2);
 int is_e164(struct sip_msg* _m, char* _sp, char* _s2);
 
 
+/*
+ * Set userpart of URI
+ */
+int set_uri_user(struct sip_msg* _m, char* _uri, char* _value);
+
 #endif /* CHECKS_H */
 #endif /* CHECKS_H */

+ 9 - 0
modules_k/siputils/doc/siputils.xml

@@ -62,6 +62,15 @@
 		</address>
 		</address>
 	    	</author>
 	    	</author>
 
 
+		<author>
+		<firstname>Juha</firstname>
+		<surname>Heinanen</surname>
+		<affiliation><orgname>TutPro Inc.</orgname></affiliation>
+		<address>
+		    <email>[email protected]</email>
+		</address>
+	    	</author>
+
 		<editor>
 		<editor>
 		<firstname>Jan</firstname>
 		<firstname>Jan</firstname>
 		<surname>Janak</surname>
 		<surname>Janak</surname>

+ 47 - 0
modules_k/siputils/doc/siputils_admin.xml

@@ -812,6 +812,53 @@ append_rpid_hf();  # Append Remote-Party-ID header field
 # Append Remote-Party-ID header field
 # Append Remote-Party-ID header field
 append_rpid_hf("", ";party=calling;id-type=subscriber;screen=yes");
 append_rpid_hf("", ";party=calling;id-type=subscriber;screen=yes");
 ...
 ...
+</programlisting>
+		</example>
+	</section>
+	<section>
+		<title>
+			<function moreinfo="none">is_rpid_user_e164()</function>
+		</title>
+		<para>
+		The function checks if the SIP URI received from the database or 
+		radius server and will potentially be used in Remote-Party-ID header 
+		field contains an E164 number (+followed by up to 15 decimal digits) 
+		in its user part.  Check fails, if no such SIP URI exists 
+		(i.e. radius server or database didn't provide this information).
+		</para>
+		<para>
+		This function can be used from REQUEST_ROUTE.
+		</para>
+		<example>
+		<title>is_rpid_user_e164 usage</title>
+		<programlisting format="linespecific">
+...
+if (is_rpid_user_e164()) {
+    # do something here
+};
+...
+</programlisting>
+		</example>
+	</section>
+	<section>
+		<title>
+			<function moreinfo="none">set_uri_user(uri, user)</function>
+		</title>
+		<para>
+		Sets userpart of SIP URI stored in writable pseudo variable
+		uri to value of	pseudo variable user.
+		</para>
+		<para>
+		This function can be used from REQUEST_ROUTE, BRANCH_ROUTE, FAILURE_ROUTE, and ONREPLY_ROUTE.
+		</para>
+		<example>
+		<title>set_uri_user usage</title>
+		<programlisting format="linespecific">
+...
+$var(uri) = "sip:user@host";
+$var(user) = "new_user";
+set_uri_user("$var(uri)", "$var(user)");
+...
 </programlisting>
 </programlisting>
 		</example>
 		</example>
 	</section>
 	</section>

+ 40 - 0
modules_k/siputils/siputils.c

@@ -106,6 +106,10 @@ sl_api_t opt_slb;
 static int mod_init(void);
 static int mod_init(void);
 static void mod_destroy(void);
 static void mod_destroy(void);
 
 
+/* Fixup functions to be defined later */
+static int fixup_set_uri(void** param, int param_no);
+static int fixup_free_set_uri(void** param, int param_no);
+
 char *contact_flds_separator = DEFAULT_SEPARATOR;
 char *contact_flds_separator = DEFAULT_SEPARATOR;
 
 
 static cmd_export_t cmds[]={
 static cmd_export_t cmds[]={
@@ -134,6 +138,9 @@ static cmd_export_t cmds[]={
 			0, REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
 			0, REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
 	{"append_rpid_hf",      (cmd_function)append_rpid_hf_p,        2, fixup_str_str,
 	{"append_rpid_hf",      (cmd_function)append_rpid_hf_p,        2, fixup_str_str,
 			0, REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
 			0, REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
+	{"set_uri_user", (cmd_function)set_uri_user, 2, fixup_set_uri,
+	 fixup_free_set_uri,	
+	 REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE},
 	{"bind_siputils",       (cmd_function)bind_siputils,           0, 0,
 	{"bind_siputils",       (cmd_function)bind_siputils,           0, 0,
 			0, 0},
 			0, 0},
 	{0,0,0,0,0,0}
 	{0,0,0,0,0,0}
@@ -242,3 +249,36 @@ int bind_siputils(siputils_api_t* api)
 
 
 	return 0;
 	return 0;
 }
 }
+
+/*
+ * Fix set_uri_* function params: uri (writable pvar) and value (pvar)
+ */
+static int fixup_set_uri(void** param, int param_no)
+{
+    if (param_no == 1) {
+	if (fixup_pvar_null(param, 1) != 0) {
+	    LM_ERR("failed to fixup uri pvar\n");
+	    return -1;
+	}
+	if (((pv_spec_t *)(*param))->setf == NULL) {
+	    LM_ERR("uri pvar is not writeble\n");
+	    return -1;
+	}
+	return 0;
+    }
+
+    if (param_no == 2) {
+	return fixup_pvar_null(param, 1);
+    }
+
+    LM_ERR("invalid parameter number <%d>\n", param_no);
+    return -1;
+}
+
+/*
+ * Free set_uri_* params.
+ */
+static int fixup_free_set_uri(void** param, int param_no)
+{
+    return fixup_free_pvar_null(param, 1);
+}