Browse Source

siputils: added is_alphanumex(tval, eset) function

- check if tval contains only alphanumeric characters or those from the
  eset parameter
Daniel-Constantin Mierla 8 years ago
parent
commit
76c5da5ec0

+ 28 - 7
src/modules/siputils/checks.c

@@ -808,18 +808,26 @@ int is_numeric(sip_msg_t *msg, char *_sp, char* _s2)
 }
 
 /*
- * Check if the parameter contains alphanumeric characters
+ * Check if the parameter contains alphanumeric characters or are part of
+ * the second parameter
  */
-int is_alphanum(sip_msg_t *msg, char *_sp, char* _s2)
+int is_alphanumex(sip_msg_t *msg, char *_sp, char* _se)
 {
 	str tval = {0, 0};
+	str eset = {0, 0};
 	int i;
+	int j;
+	int found;
 
-	if(fixup_get_svalue(msg, (gparam_t*)_sp, &tval)!=0)
-	{
-		LM_ERR("cannot get parameter value\n");
+	if(fixup_get_svalue(msg, (gparam_t*)_sp, &tval)!=0) {
+		LM_ERR("cannot get tval parameter value\n");
 		return -1;
 	}
+	if(fixup_get_svalue(msg, (gparam_t*)_se, &eset)!=0) {
+		LM_ERR("cannot get eset parameter value\n");
+		return -1;
+	}
+
 	if(tval.len<=0)
 		return -2;
 
@@ -827,8 +835,21 @@ int is_alphanum(sip_msg_t *msg, char *_sp, char* _s2)
 	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;
+				|| (tval.s[i]>='z' && tval.s[i]<='z')) ) {
+			if(eset.len<=0) {
+				return -3;
+			}
+			found = 0;
+			for(j=0; j<eset.len; j++) {
+				if(tval.s[i]==eset.s[j]) {
+					found = 1;
+					break;
+				}
+			}
+			if(found==0) {
+				return -3;
+			}
+		}
 	}
 
 	return 1;

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

@@ -129,4 +129,10 @@ int is_numeric(sip_msg_t *msg, char *_sp, char* _s2);
  */
 int is_alphanum(sip_msg_t *msg, char *_sp, char* _s2);
 
+/*
+ * Check if the parameter contains alphanumeric characters or are part of
+ * the second parameter
+ */
+int is_alphanumex(sip_msg_t *msg, char *_sp, char* _se);
+
 #endif /* CHECKS_H */

+ 24 - 0
src/modules/siputils/doc/siputils_admin.xml

@@ -681,6 +681,30 @@ if (is_alphanum("$rU")) {
 		</example>
 	</section>
 
+	<section id="siputils.f.is_alphanumex">
+		<title>
+		<function moreinfo="none">is_alphanumex(tval, eset)</function>
+		</title>
+		<para>
+			Checks if the value of parameter 'tval' consists solely of decimal
+			digits, alphabetic ASCII characters and the characters in the
+			second parameter 'eset'. The parameters can include variables.
+		</para>
+		<para>
+		This function can be used from ANY_ROUTE.
+		</para>
+		<example>
+		<title><function>is_alphanumex</function> usage</title>
+		<programlisting format="linespecific">
+...
+if (is_alphanumex("$rU", "+.-_")) {
+   ...
+}
+...
+</programlisting>
+		</example>
+	</section>
+
 	<section id="siputils.f.encode_contact">
 		<title>
 		<function moreinfo="none">encode_contact(encoding_prefix,hostpart)</function>

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

@@ -181,6 +181,8 @@ static cmd_export_t cmds[]={
 		0, ANY_ROUTE},
 	{"is_alphanum", (cmd_function)is_alphanum,               1, fixup_spve_null,
 		0, ANY_ROUTE},
+	{"is_alphanumex", (cmd_function)is_alphanumex,           2, fixup_spve_spve,
+		0, ANY_ROUTE},
 	{"sip_p_charging_vector", (cmd_function)sip_handle_pcv,  1, fixup_spve_null,
 		fixup_free_spve_null, ANY_ROUTE},
 	{0,0,0,0,0,0}