Module: sip-router Branch: master Commit: 42f431bc4ceefa32e044750ef60cf600f11fda25 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=42f431bc...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Wed Dec 10 16:12:11 2014 +0100
siputils: new function is_tel_number(val)
- returns true if the parameter is a telephone number (optional leading + followed by digits) - the parameter can contain variables
---
modules/siputils/checks.c | 29 +++++++++++++++++++++++++++++ modules/siputils/checks.h | 5 +++++ modules/siputils/siputils.c | 2 ++ 3 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/modules/siputils/checks.c b/modules/siputils/checks.c index 4cc3afd..fab644d 100644 --- a/modules/siputils/checks.c +++ b/modules/siputils/checks.c @@ -52,6 +52,7 @@ #include "../../pvar.h" #include "../../lvalue.h" #include "../../sr_module.h" +#include "../../mod_fix.h" #include "checks.h"
/** @@ -725,3 +726,31 @@ found: return 1; }
+ +/* + * Check if the parameter is a valid telephone number + * - optional leading + followed by digits only + */ +int is_tel_number(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; + if(tval.s[i]=='+') i++; + + for(; i<tval.len; i++) { + if(tval.s[i]<'0' || tval.s[i]>'9') + return -2; + } + + return 1; +} diff --git a/modules/siputils/checks.h b/modules/siputils/checks.h index bc1a99a..dcc45b8 100644 --- a/modules/siputils/checks.h +++ b/modules/siputils/checks.h @@ -117,4 +117,9 @@ int w_is_reply(struct sip_msg* msg, char *foo, char *bar); */ int get_uri_param(struct sip_msg* _msg, char* _param, char* _value);
+/* + * Check if parameter value has a telephone number format + */ +int is_tel_number(sip_msg_t *msg, char *_sp, char* _s2); + #endif /* CHECKS_H */ diff --git a/modules/siputils/siputils.c b/modules/siputils/siputils.c index 4ae73b0..076c049 100644 --- a/modules/siputils/siputils.c +++ b/modules/siputils/siputils.c @@ -175,6 +175,8 @@ static cmd_export_t cmds[]={ 0, ANY_ROUTE}, {"is_first_hop", (cmd_function)w_is_first_hop, 0, 0, 0, ANY_ROUTE}, + {"is_tel_number", (cmd_function)is_tel_number, 1, fixup_spve_null, + 0, ANY_ROUTE}, {0,0,0,0,0,0} };
to complete the story, i'll add is_numeric function telling if parameter consists solely of decimal digits. i checked my config and found that it contains more of those tests than is_tel_number tests.
regarding is_tel_number test, it currently returns true on "+" argument. i would suggest that at least one digit must follow + sign.
also, while checking the list of existing siputils functions, i found these two be redundant:
is_uri_user_e164(pseudo-variable) is_rpid_user_e164()
i suggest that they are removed.
-- juha
On 11/12/14 00:54, Juha Heinanen wrote:
to complete the story, i'll add is_numeric function telling if parameter consists solely of decimal digits. i checked my config and found that it contains more of those tests than is_tel_number tests.
regarding is_tel_number test, it currently returns true on "+" argument. i would suggest that at least one digit must follow + sign.
Pushed a patch for this, with the constraint that next digit after + must not be 0.
also, while checking the list of existing siputils functions, i found these two be redundant:
is_uri_user_e164(pseudo-variable) is_rpid_user_e164()
i suggest that they are removed.
Not sure if anyone use them, but after all they are probably just some wrappers.
Cheers, Daniel
Daniel-Constantin Mierla writes:
Pushed a patch for this, with the constraint that next digit after + must not be 0.
great.
also, while checking the list of existing siputils functions, i found these two be redundant:
is_uri_user_e164(pseudo-variable) is_rpid_user_e164()
i suggest that they are removed.
Not sure if anyone use them, but after all they are probably just some wrappers.
i think those functions (and there are other similar ones in other modules) are from the times when we didn't have pseudo variables or selects to get access to various parts of sip headers/uris.
now that we have, we could start getting rid of the no longer needed functions in order to keep kamailio smaller and cleaner. it is not a big deal if someone notices that after upgrade, some errors are printed about non-existing functions.
-- juha