浏览代码

modules_k/siputils: tel2sip() replaced by tel2sip(uri, hostpart, result)

Juha Heinanen 14 年之前
父节点
当前提交
5ad55516eb

+ 19 - 15
modules_k/siputils/README

@@ -78,7 +78,7 @@ Gabriel Vasile
               4.5. uri_param(param)
               4.5. uri_param(param)
               4.6. uri_param(param,value)
               4.6. uri_param(param,value)
               4.7. add_uri_param(param)
               4.7. add_uri_param(param)
-              4.8. tel2sip()
+              4.8. tel2sip(uri, hostpart, result)
               4.9. is_e164(pseudo-variable)
               4.9. is_e164(pseudo-variable)
               4.10. is_uri_user_e164(pseudo-variable)
               4.10. is_uri_user_e164(pseudo-variable)
               4.11. encode_contact(encoding_prefix,hostpart)
               4.11. encode_contact(encoding_prefix,hostpart)
@@ -159,7 +159,7 @@ Chapter 1. Admin Guide
         4.5. uri_param(param)
         4.5. uri_param(param)
         4.6. uri_param(param,value)
         4.6. uri_param(param,value)
         4.7. add_uri_param(param)
         4.7. add_uri_param(param)
-        4.8. tel2sip()
+        4.8. tel2sip(uri, hostpart, result)
         4.9. is_e164(pseudo-variable)
         4.9. is_e164(pseudo-variable)
         4.10. is_uri_user_e164(pseudo-variable)
         4.10. is_uri_user_e164(pseudo-variable)
         4.11. encode_contact(encoding_prefix,hostpart)
         4.11. encode_contact(encoding_prefix,hostpart)
@@ -355,7 +355,7 @@ modparam("auth", "rpid_avp", "$avp(myrpid)")
    4.5. uri_param(param)
    4.5. uri_param(param)
    4.6. uri_param(param,value)
    4.6. uri_param(param,value)
    4.7. add_uri_param(param)
    4.7. add_uri_param(param)
-   4.8. tel2sip()
+   4.8. tel2sip(uri, hostpart, result)
    4.9. is_e164(pseudo-variable)
    4.9. is_e164(pseudo-variable)
    4.10. is_uri_user_e164(pseudo-variable)
    4.10. is_uri_user_e164(pseudo-variable)
    4.11. encode_contact(encoding_prefix,hostpart)
    4.11. encode_contact(encoding_prefix,hostpart)
@@ -492,11 +492,12 @@ if (uri_param("param1","value1")) {
 add_uri_param("nat=yes");
 add_uri_param("nat=yes");
 ...
 ...
 
 
-4.8.  tel2sip()
+4.8.  tel2sip(uri, hostpart, result)
 
 
-   Converts RURI, if it is tel URI, to SIP URI. Returns true only if
-   conversion succeeded or if no conversion was needed (like RURI was not
-   tel URI).
+   Converts URI in first param (pseudo variable or string) to SIP URI, if
+   it is a tel URI. If conversion was done, writes resulting SIP URI to
+   third param (pseudo variable). Returns 1 if conversion succeeded or if
+   no conversion was needed.
 
 
    The conversion follows the rules in RFC 3261 section 19.1.6:
    The conversion follows the rules in RFC 3261 section 19.1.6:
      * Visual separators ( "-", ".", "(", ")" ) are removed from tel URI
      * Visual separators ( "-", ".", "(", ")" ) are removed from tel URI
@@ -504,19 +505,22 @@ add_uri_param("nat=yes");
      * tel URI parameters are downcased before appending them to SIP URI
      * tel URI parameters are downcased before appending them to SIP URI
        userinfo
        userinfo
 
 
-   The SIP URI host is formed using the From URI host.
+   The SIP URI hostpart is taken from second param (pseudo variable or
+   string).
 
 
-   This function can be used from REQUEST_ROUTE.
+   This function can be used from REQUEST_ROUTE or ONREPLY_ROUTE.
 
 
    Example 1.17. tel2sip usage
    Example 1.17. tel2sip usage
 ...
 ...
-# RURI:  tel:+(34)-999-888-777
-tel2sip();
-# RURI:  sip:[email protected];user=phone
+# $ru: tel:+(34)-999-888-777
+# $fu: sip:[email protected]
+tel2sip("$ru", $fd", "$ru");
+# $ru:  sip:[email protected];user=phone
 
 
-# RURI:  tel:+12-(34)-56-78;Ext=200;ISUB=+123-456
-tel2sip();
-# RURI:  sip:+12345678;ext=200;[email protected];user=phone
+# $ru: tel:+12-(34)-56-78;Ext=200;ISUB=+123-456
+# $fu: sip:[email protected]
+tel2sip("$ru", $fd", "$ru");
+# $ru:  sip:+12345678;ext=200;[email protected];user=phone
 ...
 ...
 
 
 4.9.  is_e164(pseudo-variable)
 4.9.  is_e164(pseudo-variable)

+ 76 - 65
modules_k/siputils/checks.c

@@ -52,6 +52,7 @@
 #include "../../dset.h"
 #include "../../dset.h"
 #include "../../pvar.h"
 #include "../../pvar.h"
 #include "../../lvalue.h"
 #include "../../lvalue.h"
+#include "../../sr_module.h"
 #include "checks.h"
 #include "checks.h"
 
 
 /**
 /**
@@ -329,80 +330,90 @@ ok:
 
 
 
 
 /*
 /*
- * Converts Request-URI, if it is tel URI, to SIP URI.  Returns 1, if
- * conversion succeeded or if no conversion was needed, i.e., Request-URI
- * was not tel URI.  Returns -1, if conversion failed.
+ * Converts URI, if it is tel URI, to SIP URI.  Returns 1, if
+ * conversion succeeded or if no conversion was needed, i.e., URI was not
+ * tel URI.  Returns -1, if conversion failed.  Takes SIP URI hostpart from
+ * second parameter and (if needed) writes the result to third paramater.
  */
  */
-int tel2sip(struct sip_msg* _msg, char* _s1, char* _s2)
+int tel2sip(struct sip_msg* _msg, char* _uri, char* _hostpart, char* _res)
 {
 {
-	str *ruri;
-	str tel_uri;
-	struct sip_uri *pfuri;
-	str suri;
-	char* at;
-	int i, j;
-	int in_tel_parameters = 0;
-
-	ruri = GET_RURI(_msg);
-	
-	if (ruri->len < 4) return 1;
+    str uri, hostpart, tel_uri, sip_uri;
+    char *at;
+    int i, j, in_tel_parameters = 0;
+    pv_spec_t *res;
+    pv_value_t res_val;
+
+    /* get parameters */
+    if (get_str_fparam(&uri, _msg, (fparam_t*)_uri) < 0) {
+	LM_ERR("failed to get uri value\n");
+    }
+    if (get_str_fparam(&hostpart, _msg, (fparam_t*)_hostpart) < 0) {
+	LM_ERR("failed to get hostpart value\n");
+    }
+    res = (pv_spec_t *)_res;
 
 
-	if (strncasecmp(ruri->s, "tel:", 4) != 0) return 1;
+    /* check if anything needs to be done */
+    if (uri.len < 4) return 1;
+    if (strncasecmp(uri.s, "tel:", 4) != 0) return 1;
+    
+    /* reserve memory for clean tel uri */
+    tel_uri.s = pkg_malloc(uri.len);
+    if (tel_uri.s == 0) {
+	LM_ERR("no more pkg memory\n");
+	return -1;
+    }
 	
 	
-	tel_uri.s = pkg_malloc(ruri->len);
-	if (tel_uri.s == 0) {
-		LM_ERR("no more pkg memory\n");
-		return -1;
+    /* Remove visual separators before converting to SIP URI. Don't remove 
+       visual separators in TEL URI parameters (after the first ";") */
+    for (i=0, j=0; i < uri.len; i++) {
+	if (in_tel_parameters == 0) {
+	    if (uri.s[i] == ';')
+		in_tel_parameters = 1;
 	}
 	}
-	
-	/* Remove visual separators before converting to SIP URI. Don't remove 
-	visual separators in TEL URI parameters (after the first ";") */
-	for(i=0,j=0; i < ruri->len; i++) {
-		if (in_tel_parameters == 0) {
-			if (ruri->s[i] == ';')
-				in_tel_parameters = 1;
-		}
-		if (in_tel_parameters == 0) {
-			if (ruri->s[i] != '-' && ruri->s[i] != '.' && ruri->s[i] != '(' && ruri->s[i] != ')')
-				tel_uri.s[j++] = tolower(ruri->s[i]);
-		} else {
-			tel_uri.s[j++] = tolower(ruri->s[i]);
-		}
+	if (in_tel_parameters == 0) {
+	    if ((uri.s[i] != '-') && (uri.s[i] != '.') && 
+		(uri.s[i] != '(') && (uri.s[i] != ')'))
+		tel_uri.s[j++] = tolower(uri.s[i]);
+	} else {
+	    tel_uri.s[j++] = tolower(uri.s[i]);
 	}
 	}
-	tel_uri.s[j] = '\0';
-	tel_uri.len = strlen(tel_uri.s);
+    }
+    tel_uri.s[j] = '\0';
+    tel_uri.len = strlen(tel_uri.s);
+
+    /* reserve memory for resulting sip uri */
+    sip_uri.len = 4 + tel_uri.len - 4 + 1 + hostpart.len + 1 + 10;
+    sip_uri.s = pkg_malloc(sip_uri.len);
+    if (sip_uri.s == 0) {
+	LM_ERR("no more pkg memory\n");
+	pkg_free(tel_uri.s);
+	return -1;
+    }
 
 
-	if ((pfuri=parse_from_uri(_msg))==NULL) {
-		LM_ERR("parsing From header failed\n");
-		return -1;
-	}
+    /* create resulting sip uri */
+    at = sip_uri.s;
+    append_str(at, "sip:", 4);
+    append_str(at, tel_uri.s + 4, tel_uri.len - 4);
+    append_chr(at, '@');
+    append_str(at, hostpart.s, hostpart.len);
+    append_chr(at, ';');
+    append_str(at, "user=phone", 10);
 
 
-	suri.len = 4 + tel_uri.len - 4 + 1 + pfuri->host.len + 1 + 10;
-	suri.s = pkg_malloc(suri.len);
-	if (suri.s == 0) {
-		LM_ERR("no more pkg memory\n");
-		return -1;
-	}
-	at = suri.s;
-	memcpy(at, "sip:", 4);
-	at = at + 4;
-	memcpy(at, tel_uri.s + 4, tel_uri.len - 4);
-	at = at + tel_uri.len - 4;
-	*at = '@';
-	at = at + 1;
-	memcpy(at, pfuri->host.s, pfuri->host.len);
-	at = at + pfuri->host.len;
-	*at = ';';
-	at = at + 1;
-	memcpy(at, "user=phone", 10);
+    /* tel_uri is not needed anymore */
+    pkg_free(tel_uri.s);
 
 
-	if (rewrite_uri(_msg, &suri) == 1) {
-		pkg_free(suri.s);
-		return 1;
-	} else {
-		pkg_free(suri.s);
-		return -1;
-	}
+    /* set result pv value and write sip uri to result pv */
+    res_val.rs = sip_uri;
+    res_val.flags = PV_VAL_STR;
+    if (res->setf(_msg, &res->pvp, (int)EQ_T, &res_val) != 0) {
+	LM_ERR("failed to set result pvar\n");
+	pkg_free(sip_uri.s);
+	return -1;
+    }
+
+    /* free allocated pkg memory and return */
+    pkg_free(sip_uri.s);
+    return 1;
 }
 }
 
 
 
 

+ 5 - 4
modules_k/siputils/checks.h

@@ -73,11 +73,12 @@ int add_uri_param(struct sip_msg* _msg, char* _param, char* _s2);
 
 
 
 
 /*
 /*
- * Converts Request-URI, if it is tel URI, to SIP URI.  Returns 1, if
- * conversion succeeded or if no conversion was needed, i.e., Request-URI
- * was not tel URI.  Returns -1, if conversion failed.
+ * Converts URI, if it is tel URI, to SIP URI.  Returns 1, if
+ * conversion succeeded or if no conversion was needed, i.e., URI was not
+ * tel URI.  Returns -1, if conversion failed.  Takes SIP URI hostpart from
+ * second parameter and (if needed) writes the result to third paramater.
  */
  */
-int tel2sip(struct sip_msg* _msg, char* _s1, char* _s2);
+int tel2sip(struct sip_msg* _msg, char* _uri, char* _hostpart, char* _res);
 
 
 
 
 /*
 /*

+ 17 - 12
modules_k/siputils/doc/siputils_admin.xml

@@ -477,12 +477,14 @@ add_uri_param("nat=yes");
 	</section>
 	</section>
 	<section>
 	<section>
 		<title>
 		<title>
-		<function moreinfo="none">tel2sip()</function>
+		<function moreinfo="none">tel2sip(uri, hostpart, result)</function>
 		</title>
 		</title>
 		<para>
 		<para>
-		Converts RURI, if it is tel URI, to SIP URI.  Returns true only if
-		conversion succeeded or if no conversion was needed (like RURI
-		was not tel URI).
+		Converts URI in first param (pseudo variable or string) to
+		SIP URI, if it is a tel URI.  If conversion was done,
+		writes resulting SIP URI to third param (pseudo variable).
+		Returns 1 if conversion succeeded or if no conversion
+		was needed.
 		</para>
 		</para>
 		<para>
 		<para>
 		The conversion follows the rules in RFC 3261 section 19.1.6:
 		The conversion follows the rules in RFC 3261 section 19.1.6:
@@ -496,22 +498,25 @@ add_uri_param("nat=yes");
                 </itemizedlist>
                 </itemizedlist>
 		</para>
 		</para>
 		<para>
 		<para>
-		The SIP URI host is formed using the From URI host.
+		The SIP URI hostpart is taken from second param
+		(pseudo variable or string).
 		</para>
 		</para>
 		<para>
 		<para>
-		This function can be used from REQUEST_ROUTE.
+		This function can be used from REQUEST_ROUTE or ONREPLY_ROUTE.
 		</para>
 		</para>
 		<example>
 		<example>
 		<title><function>tel2sip</function> usage</title>
 		<title><function>tel2sip</function> usage</title>
 		<programlisting format="linespecific">
 		<programlisting format="linespecific">
 ...
 ...
-# RURI:  tel:+(34)-999-888-777
-tel2sip();
-# RURI:  sip:[email protected];user=phone
+# $ru: tel:+(34)-999-888-777
+# $fu: sip:[email protected]
+tel2sip("$ru", $fd", "$ru");
+# $ru:  sip:[email protected];user=phone
 
 
-# RURI:  tel:+12-(34)-56-78;Ext=200;ISUB=+123-456
-tel2sip();
-# RURI:  sip:+12345678;ext=200;[email protected];user=phone
+# $ru: tel:+12-(34)-56-78;Ext=200;ISUB=+123-456
+# $fu: sip:[email protected]
+tel2sip("$ru", $fd", "$ru");
+# $ru:  sip:+12345678;ext=200;[email protected];user=phone
 ...
 ...
 </programlisting>
 </programlisting>
 		</example>
 		</example>

+ 34 - 2
modules_k/siputils/siputils.c

@@ -109,6 +109,7 @@ static void mod_destroy(void);
 /* Fixup functions to be defined later */
 /* Fixup functions to be defined later */
 static int fixup_set_uri(void** param, int param_no);
 static int fixup_set_uri(void** param, int param_no);
 static int fixup_free_set_uri(void** param, int param_no);
 static int fixup_free_set_uri(void** param, int param_no);
+static int fixup_tel2sip(void** param, int param_no);
 
 
 char *contact_flds_separator = DEFAULT_SEPARATOR;
 char *contact_flds_separator = DEFAULT_SEPARATOR;
 
 
@@ -127,8 +128,8 @@ static cmd_export_t cmds[]={
 		0, REQUEST_ROUTE|LOCAL_ROUTE},
 		0, REQUEST_ROUTE|LOCAL_ROUTE},
 	{"add_uri_param",      (cmd_function)add_uri_param,     1, fixup_str_null,
 	{"add_uri_param",      (cmd_function)add_uri_param,     1, fixup_str_null,
 		0, REQUEST_ROUTE},
 		0, REQUEST_ROUTE},
-	{"tel2sip",            (cmd_function)tel2sip,           0, 0,
-		0, REQUEST_ROUTE},
+	{"tel2sip", (cmd_function)tel2sip, 3, fixup_tel2sip, 0,
+	 REQUEST_ROUTE|ONREPLY_ROUTE},
 	{"is_e164",            (cmd_function)is_e164,           1, fixup_pvar_null,
 	{"is_e164",            (cmd_function)is_e164,           1, fixup_pvar_null,
 		fixup_free_pvar_null, REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
 		fixup_free_pvar_null, REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
 	{"is_uri_user_e164",   (cmd_function)is_uri_user_e164,  1, fixup_pvar_null,
 	{"is_uri_user_e164",   (cmd_function)is_uri_user_e164,  1, fixup_pvar_null,
@@ -296,3 +297,34 @@ static int fixup_free_set_uri(void** param, int param_no)
 {
 {
     return fixup_free_pvar_null(param, 1);
     return fixup_free_pvar_null(param, 1);
 }
 }
+
+
+/*
+ * Fix tel2sip function params: uri and hostpart pvars and
+ * result writable pvar.
+ */
+static int fixup_tel2sip(void** param, int param_no)
+{
+    if ((param_no == 1) || (param_no == 2)) {
+	if (fixup_var_str_12(param, 1) < 0) {
+	    LM_ERR("failed to fixup uri or hostpart pvar\n");
+	    return -1;
+	}
+	return 0;
+    }
+
+    if (param_no == 3) {
+	if (fixup_pvar_null(param, 1) != 0) {
+	    LM_ERR("failed to fixup result pvar\n");
+	    return -1;
+	}
+	if (((pv_spec_t *)(*param))->setf == NULL) {
+	    LM_ERR("result pvar is not writeble\n");
+	    return -1;
+	}
+	return 0;
+    }
+
+    LM_ERR("invalid parameter number <%d>\n", param_no);
+    return -1;
+}