瀏覽代碼

siputils: added is_alphanum(v) function

- helper function to check if a string value contains only digits or
  alphabetic characters
Daniel-Constantin Mierla 8 年之前
父節點
當前提交
41c4319de8

+ 27 - 0
src/modules/siputils/checks.c

@@ -806,3 +806,30 @@ int is_numeric(sip_msg_t *msg, char *_sp, char* _s2)
 
 
 	return 1;
 	return 1;
 }
 }
+
+/*
+ * Check if the parameter contains alphanumeric characters
+ */
+int is_alphanum(sip_msg_t *msg, char *_sp, char* _s2)
+{
+	str tval = {0, 0};
+	int i;
+
+	if(fixup_get_svalue(msg, (gparam_t*)_sp, &tval)!=0)
+	{
+		LM_ERR("cannot get parameter value\n");
+		return -1;
+	}
+	if(tval.len<=0)
+		return -2;
+
+	i = 0;
+	for(; i<tval.len; i++) {
+		if( !((tval.s[i]>='0' && tval.s[i]<='9')
+				|| (tval.s[i]>='A' && tval.s[i]<='Z')
+				|| (tval.s[i]>='z' && tval.s[i]<='z')) )
+			return -3;
+	}
+
+	return 1;
+}

+ 5 - 0
src/modules/siputils/checks.h

@@ -124,4 +124,9 @@ int is_tel_number(sip_msg_t *msg, char *_sp, char* _s2);
  */
  */
 int is_numeric(sip_msg_t *msg, char *_sp, char* _s2);
 int is_numeric(sip_msg_t *msg, char *_sp, char* _s2);
 
 
+/*
+ * Check if the parameter contains alphanumeric characters
+ */
+int is_alphanum(sip_msg_t *msg, char *_sp, char* _s2);
+
 #endif /* CHECKS_H */
 #endif /* CHECKS_H */

+ 38 - 14
src/modules/siputils/doc/siputils_admin.xml

@@ -10,9 +10,9 @@
 <!-- Module User's Guide -->
 <!-- Module User's Guide -->
 
 
 <chapter>
 <chapter>
-	
+
 	<title>&adminguide;</title>
 	<title>&adminguide;</title>
-	
+
 	<section>
 	<section>
 	<title>Overview</title>
 	<title>Overview</title>
 	<para>
 	<para>
@@ -26,16 +26,16 @@
 	So its necessary to drop the 183 in this cases and send a 180 instead.
 	So its necessary to drop the 183 in this cases and send a 180 instead.
 	</para>
 	</para>
 	<para>
 	<para>
-	This module also provides a function to answer OPTIONS requests which 
-	are directed to the server itself. This means an OPTIONS request 
-	which has the address of the server in the request URI, and no 
-	username in the URI. The request will be answered with a 200 OK 
+	This module also provides a function to answer OPTIONS requests which
+	are directed to the server itself. This means an OPTIONS request
+	which has the address of the server in the request URI, and no
+	username in the URI. The request will be answered with a 200 OK
 	with the capabilities of the server.
 	with the capabilities of the server.
 	</para>
 	</para>
 	<para>
 	<para>
 	To answer OPTIONS request directed to your server is the easiest
 	To answer OPTIONS request directed to your server is the easiest
-	way for is-alive-tests on the SIP (application) layer from remote 
-	(similar to ICMP echo requests, also known as <quote>ping</quote>, 
+	way for is-alive-tests on the SIP (application) layer from remote
+	(similar to ICMP echo requests, also known as <quote>ping</quote>,
 	on the network layer).
 	on the network layer).
 	</para>
 	</para>
 	</section>
 	</section>
@@ -57,7 +57,7 @@
 	<section>
 	<section>
 		<title>External Libraries or Applications</title>
 		<title>External Libraries or Applications</title>
 		<para>
 		<para>
-		The following libraries or applications must be installed before 
+		The following libraries or applications must be installed before
 		running &kamailio; with this module loaded:
 		running &kamailio; with this module loaded:
 			<itemizedlist>
 			<itemizedlist>
 			<listitem>
 			<listitem>
@@ -69,7 +69,7 @@
 		</para>
 		</para>
 	</section>
 	</section>
 	</section>
 	</section>
-	
+
 	<section>
 	<section>
 		<title>Parameters</title>
 		<title>Parameters</title>
 		<section>
 		<section>
@@ -640,7 +640,8 @@ if (is_tel_number("+24242424") {
 		<function moreinfo="none">is_numeric(tval)</function>
 		<function moreinfo="none">is_numeric(tval)</function>
 		</title>
 		</title>
 		<para>
 		<para>
-		  Checks if the parameter value consists solely of decimal digits. The parameter can include variables.
+			Checks if the parameter value consists solely of decimal digits.
+			The parameter can include variables.
 		</para>
 		</para>
 		<para>
 		<para>
 		This function can be used from ANY_ROUTE.
 		This function can be used from ANY_ROUTE.
@@ -649,7 +650,30 @@ if (is_tel_number("+24242424") {
 		<title><function>is_numeric</function> usage</title>
 		<title><function>is_numeric</function> usage</title>
 		<programlisting format="linespecific">
 		<programlisting format="linespecific">
 ...
 ...
-if (is_numeric($rU)) {  # Test if R-URI user consists of decimal digits
+if (is_numeric("$rU")) {  # Test if R-URI user consists of decimal digits
+   ...
+}
+...
+</programlisting>
+		</example>
+	</section>
+
+	<section id="siputils.f.is_alphanum">
+		<title>
+		<function moreinfo="none">is_alphanum(tval)</function>
+		</title>
+		<para>
+			Checks if the parameter value consists solely of decimal digits
+			or alphabetic ASCII characters. The parameter can include variables.
+		</para>
+		<para>
+		This function can be used from ANY_ROUTE.
+		</para>
+		<example>
+		<title><function>is_alphanum</function> usage</title>
+		<programlisting format="linespecific">
+...
+if (is_alphanum("$rU")) {
    ...
    ...
 }
 }
 ...
 ...
@@ -662,13 +686,13 @@ if (is_numeric($rU)) {  # Test if R-URI user consists of decimal digits
 		<function moreinfo="none">encode_contact(encoding_prefix,hostpart)</function>
 		<function moreinfo="none">encode_contact(encoding_prefix,hostpart)</function>
 		</title>
 		</title>
 		<para>
 		<para>
-		This function will encode uri-s inside Contact header in the following 
+		This function will encode uri-s inside Contact header in the following
 		manner
 		manner
 		sip:username:password@ip:port;transport=protocol goes
 		sip:username:password@ip:port;transport=protocol goes
 		sip:encoding_prefix*username*ip*port*protocol@hostpart.
 		sip:encoding_prefix*username*ip*port*protocol@hostpart.
 		</para>
 		</para>
 		<para>
 		<para>
-		* is the default separator and can be changed by setting the contact_flds_separator 
+		* is the default separator and can be changed by setting the contact_flds_separator
 		module parameter.
 		module parameter.
 		</para>
 		</para>
 		<para>
 		<para>

+ 4 - 2
src/modules/siputils/siputils.c

@@ -171,14 +171,16 @@ static cmd_export_t cmds[]={
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
 	{"is_gruu",  (cmd_function)w_is_gruu,                    1, fixup_spve_null,
 	{"is_gruu",  (cmd_function)w_is_gruu,                    1, fixup_spve_null,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
-	{"is_supported",  (cmd_function)w_is_supported,                    1, fixup_option,
+	{"is_supported",  (cmd_function)w_is_supported,          1, fixup_option,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
-	{"is_first_hop",  (cmd_function)w_is_first_hop,                    0, 0,
+	{"is_first_hop",  (cmd_function)w_is_first_hop,          0, 0,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
 	{"is_tel_number", (cmd_function)is_tel_number,           1, fixup_spve_null,
 	{"is_tel_number", (cmd_function)is_tel_number,           1, fixup_spve_null,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
 	{"is_numeric", (cmd_function)is_numeric,                 1, fixup_spve_null,
 	{"is_numeric", (cmd_function)is_numeric,                 1, fixup_spve_null,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
+	{"is_alphanum", (cmd_function)is_alphanum,               1, fixup_spve_null,
+		0, ANY_ROUTE},
 	{"sip_p_charging_vector", (cmd_function)sip_handle_pcv,  1, fixup_spve_null,
 	{"sip_p_charging_vector", (cmd_function)sip_handle_pcv,  1, fixup_spve_null,
 		fixup_free_spve_null, ANY_ROUTE},
 		fixup_free_spve_null, ANY_ROUTE},
 	{0,0,0,0,0,0}
 	{0,0,0,0,0,0}