From 103173e6258a4c2a6d8c65306320fe2e01d036f2 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Wed, 8 Nov 2017 11:59:31 +0100 Subject: [PATCH] phonenum: set record after phone number lookup and return pv attributes --- src/modules/phonenum/phonenum_mod.c | 44 +++---- src/modules/phonenum/phonenum_pv.c | 188 ++++++++++++++++------------ 2 files changed, 128 insertions(+), 104 deletions(-) diff --git a/src/modules/phonenum/phonenum_mod.c b/src/modules/phonenum/phonenum_mod.c index a8082905e10..5a051842cb9 100644 --- a/src/modules/phonenum/phonenum_mod.c +++ b/src/modules/phonenum/phonenum_mod.c @@ -39,19 +39,19 @@ MODULE_VERSION static int phonenum_smode = 0; -static int mod_init(void); +static int mod_init(void); static void mod_destroy(void); -static int w_phonenum_match(struct sip_msg* msg, char* str1, char* str2); +static int w_phonenum_match(struct sip_msg *msg, char *str1, char *str2); static int phonenum_match(sip_msg_t *msg, str *tomatch, str *pvclass); +/* clang-format off */ static pv_export_t mod_pvs[] = { { {"phn", sizeof("phn")-1}, PVT_OTHER, pv_get_phonenum, 0, pv_parse_phonenum_name, 0, 0, 0 }, { {0, 0}, 0, 0, 0, 0, 0, 0, 0 } }; - static cmd_export_t cmds[]={ {"phonenum_match", (cmd_function)w_phonenum_match, 2, fixup_spve_spve, 0, ANY_ROUTE}, @@ -66,8 +66,8 @@ static param_export_t params[]={ struct module_exports exports = { "phonenum", DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, - params, + cmds, /* exported config functions */ + params, /* exported config parameters */ 0, 0, /* exported MI functions */ mod_pvs, /* exported pseudo-variables */ @@ -77,7 +77,7 @@ struct module_exports exports = { mod_destroy, /* destroy function */ 0 /* per child init function */ }; - +/* clang-format on */ /** * init module function @@ -85,8 +85,7 @@ struct module_exports exports = { static int mod_init(void) { - if(phonenum_init_pv(phonenum_smode)!=0) - { + if(phonenum_init_pv(phonenum_smode) != 0) { LM_ERR("cannot do init\n"); return -1; } @@ -109,21 +108,21 @@ static int phonenum_match(sip_msg_t *msg, str *tomatch, str *pvclass) return phonenum_update_pv(tomatch, pvclass); } -static int w_phonenum_match(sip_msg_t* msg, char* target, char* pvname) +static int w_phonenum_match(sip_msg_t *msg, char *target, char *pvname) { str tomatch = STR_NULL; str pvclass = STR_NULL; - if(msg==NULL) { + if(msg == NULL) { LM_ERR("received null msg\n"); return -1; } - if(fixup_get_svalue(msg, (gparam_t*)target, &tomatch)<0) { + if(fixup_get_svalue(msg, (gparam_t *)target, &tomatch) < 0) { LM_ERR("cannot get the address\n"); return -1; } - if(fixup_get_svalue(msg, (gparam_t*)pvname, &pvclass)<0) { + if(fixup_get_svalue(msg, (gparam_t *)pvname, &pvclass) < 0) { LM_ERR("cannot get the pv class\n"); return -1; } @@ -134,17 +133,20 @@ static int w_phonenum_match(sip_msg_t* msg, char* target, char* pvname) /** * */ +/* clang-format off */ static sr_kemi_t sr_kemi_phonenum_exports[] = { - { str_init("phonenum"), str_init("match"), - SR_KEMIP_INT, phonenum_match, - { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE, - SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } - }, + { str_init("phonenum"), str_init("match"), + SR_KEMIP_INT, phonenum_match, + { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, - { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } } + { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } } }; +/* clang-format on */ -int mod_register(char *path, int *dlflags, void *p1, void *p2) { - sr_kemi_modules_add(sr_kemi_phonenum_exports); - return 0; +int mod_register(char *path, int *dlflags, void *p1, void *p2) +{ + sr_kemi_modules_add(sr_kemi_phonenum_exports); + return 0; } diff --git a/src/modules/phonenum/phonenum_pv.c b/src/modules/phonenum/phonenum_pv.c index 3df360716e6..9fd9ce93f41 100644 --- a/src/modules/phonenum/phonenum_pv.c +++ b/src/modules/phonenum/phonenum_pv.c @@ -35,14 +35,11 @@ #include "cphonenumber.h" #include "phonenum_pv.h" +/* clang-format off */ typedef struct _sr_phonenum_record { telnum_t *record; - char *number; - char *country; - char *region; - char *operator; char tomatch[256]; - int flags; + long flags; } sr_phonenum_record_t; typedef struct _sr_phonenum_item { @@ -56,6 +53,7 @@ typedef struct _phonenum_pv { sr_phonenum_item_t *item; int type; } phonenum_pv_t; +/* clang-format on */ static sr_phonenum_item_t *_sr_phonenum_list = NULL; @@ -65,13 +63,12 @@ sr_phonenum_record_t *sr_phonenum_get_record(str *name) sr_phonenum_item_t *it = NULL; unsigned int hashid = 0; - hashid = get_hash1_raw(name->s, name->len); + hashid = get_hash1_raw(name->s, name->len); it = _sr_phonenum_list; - while(it!=NULL) - { - if(it->hashid==hashid && it->pvclass.len == name->len - && strncmp(it->pvclass.s, name->s, name->len)==0) + while(it != NULL) { + if(it->hashid == hashid && it->pvclass.len == name->len + && strncmp(it->pvclass.s, name->s, name->len) == 0) return &it->r; it = it->next; } @@ -83,27 +80,24 @@ sr_phonenum_item_t *sr_phonenum_add_item(str *name) sr_phonenum_item_t *it = NULL; unsigned int hashid = 0; - hashid = get_hash1_raw(name->s, name->len); + hashid = get_hash1_raw(name->s, name->len); it = _sr_phonenum_list; - while(it!=NULL) - { - if(it->hashid==hashid && it->pvclass.len == name->len - && strncmp(it->pvclass.s, name->s, name->len)==0) + while(it != NULL) { + if(it->hashid == hashid && it->pvclass.len == name->len + && strncmp(it->pvclass.s, name->s, name->len) == 0) return it; it = it->next; } /* add new */ - it = (sr_phonenum_item_t*)pkg_malloc(sizeof(sr_phonenum_item_t)); - if(it==NULL) - { + it = (sr_phonenum_item_t *)pkg_malloc(sizeof(sr_phonenum_item_t)); + if(it == NULL) { LM_ERR("no more pkg\n"); return NULL; } memset(it, 0, sizeof(sr_phonenum_item_t)); - it->pvclass.s = (char*)pkg_malloc(name->len+1); - if(it->pvclass.s==NULL) - { + it->pvclass.s = (char *)pkg_malloc(name->len + 1); + if(it->pvclass.s == NULL) { LM_ERR("no more pkg.\n"); pkg_free(it); return NULL; @@ -120,96 +114,103 @@ sr_phonenum_item_t *sr_phonenum_add_item(str *name) int pv_parse_phonenum_name(pv_spec_p sp, str *in) { - phonenum_pv_t *gpv=NULL; + phonenum_pv_t *gpv = NULL; char *p; str pvc; str pvs; - if(sp==NULL || in==NULL || in->len<=0) + if(sp == NULL || in == NULL || in->len <= 0) return -1; - gpv = (phonenum_pv_t*)pkg_malloc(sizeof(phonenum_pv_t)); - if(gpv==NULL) + gpv = (phonenum_pv_t *)pkg_malloc(sizeof(phonenum_pv_t)); + if(gpv == NULL) return -1; memset(gpv, 0, sizeof(phonenum_pv_t)); p = in->s; - while(ps+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) + while(p < in->s + in->len + && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - if(p>in->s+in->len || *p=='\0') + if(p > in->s + in->len || *p == '\0') goto error; pvc.s = p; - while(p < in->s + in->len) - { - if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r') + while(p < in->s + in->len) { + if(*p == '=' || *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') break; p++; } - if(p>in->s+in->len || *p=='\0') + if(p > in->s + in->len || *p == '\0') goto error; pvc.len = p - pvc.s; - if(*p!='=') - { - while(ps+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) + if(*p != '=') { + while(p < in->s + in->len + && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - if(p>in->s+in->len || *p=='\0' || *p!='=') + if(p > in->s + in->len || *p == '\0' || *p != '=') goto error; } p++; - if(*p!='>') + if(*p != '>') goto error; p++; pvs.len = in->len - (int)(p - in->s); pvs.s = p; - LM_DBG("phonenum [%.*s] - key [%.*s]\n", pvc.len, pvc.s, - pvs.len, pvs.s); + LM_DBG("phonenum [%.*s] - key [%.*s]\n", pvc.len, pvc.s, pvs.len, pvs.s); gpv->item = sr_phonenum_add_item(&pvc); - if(gpv->item==NULL) + if(gpv->item == NULL) goto error; - switch(pvs.len) - { + switch(pvs.len) { + case 5: + if(strncmp(pvs.s, "ltype", 6) == 0) + gpv->type = 2; + else if(strncmp(pvs.s, "ndesc", 6) == 0) + gpv->type = 3; + else if(strncmp(pvs.s, "error", 6) == 0) + gpv->type = 4; + else if(strncmp(pvs.s, "cctel", 6) == 0) + gpv->type = 5; + else if(strncmp(pvs.s, "valid", 6) == 0) + gpv->type = 6; + else + goto error; + break; case 6: - if(strncmp(pvs.s, "number", 6)==0) + if(strncmp(pvs.s, "number", 6) == 0) gpv->type = 0; - else if(strncmp(pvs.s, "region", 6)==0) - gpv->type = 2; - else goto error; - break; - case 7: - if(strncmp(pvs.s, "country", 7)==0) + else + goto error; + break; + case 10: + if(strncmp(pvs.s, "normalized", 10) == 0) gpv->type = 1; - else goto error; - break; - case 8: - if(strncmp(pvs.s, "operator", 8)==0) - gpv->type = 3; - else goto error; - break; + else + goto error; + break; default: goto error; } - sp->pvp.pvn.u.dname = (void*)gpv; + sp->pvp.pvn.u.dname = (void *)gpv; sp->pvp.pvn.type = PV_NAME_OTHER; return 0; error: - if(gpv!=NULL) + if(gpv != NULL) pkg_free(gpv); LM_ERR("error at PV phonenum name: %.*s\n", in->len, in->s); return -1; } -int pv_phonenum_get_strzval(struct sip_msg *msg, pv_param_t *param, - pv_value_t *res, char *sval) +int pv_phonenum_get_strzval( + struct sip_msg *msg, pv_param_t *param, pv_value_t *res, char *sval) { str s; - if(sval==NULL) + if(sval == NULL) return pv_get_null(msg, param, res); s.s = sval; @@ -217,30 +218,47 @@ int pv_phonenum_get_strzval(struct sip_msg *msg, pv_param_t *param, return pv_get_strval(msg, param, res, &s); } -int pv_get_phonenum(struct sip_msg *msg, pv_param_t *param, - pv_value_t *res) +int pv_get_phonenum(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { phonenum_pv_t *gpv; - if(msg==NULL || param==NULL) + if(msg == NULL || param == NULL) return -1; - gpv = (phonenum_pv_t*)param->pvn.u.dname; - if(gpv==NULL) + gpv = (phonenum_pv_t *)param->pvn.u.dname; + if(gpv == NULL) return -1; - if(gpv->item==NULL) + if(gpv->item == NULL) + return pv_get_null(msg, param, res); + if(gpv->item->r.record==NULL) return pv_get_null(msg, param, res); - switch(gpv->type) - { - case 1: /* country */ - return pv_get_null(msg, param, res); - case 2: /* region */ - return pv_get_null(msg, param, res); - case 3: /* operator */ - return pv_get_null(msg, param, res); + switch(gpv->type) { + case 1: /* normalized */ + if(gpv->item->r.record->normalized==NULL) + return pv_get_null(msg, param, res); + return pv_get_strzval(msg, param, res, + gpv->item->r.record->normalized); + case 2: /* ltype */ + if(gpv->item->r.record->ltype==NULL) + return pv_get_null(msg, param, res); + return pv_get_strzval(msg, param, res, gpv->item->r.record->ltype); + case 3: /* ndesc */ + if(gpv->item->r.record->ndesc==NULL) + return pv_get_null(msg, param, res); + return pv_get_strzval(msg, param, res, gpv->item->r.record->ndesc); + case 4: /* error */ + if(gpv->item->r.record->error==NULL) + return pv_get_null(msg, param, res); + return pv_get_strzval(msg, param, res, gpv->item->r.record->error); + case 5: /* cctel */ + return pv_get_sintval(msg, param, res, gpv->item->r.record->cctel); + case 6: /* valid */ + return pv_get_sintval(msg, param, res, gpv->item->r.record->valid); default: /* number */ - return pv_get_null(msg, param, res); + if(gpv->item->r.record->number==NULL) + return pv_get_null(msg, param, res); + return pv_get_strzval(msg, param, res, gpv->item->r.record->number); } } @@ -263,8 +281,11 @@ void phonenum_pv_reset(str *name) gr = sr_phonenum_get_record(name); - if(gr==NULL) + if(gr == NULL) return; + if(gr->record != NULL) { + telnum_free(gr->record); + } memset(gr, 0, sizeof(sr_phonenum_record_t)); } @@ -272,24 +293,25 @@ int phonenum_update_pv(str *tomatch, str *name) { sr_phonenum_record_t *gr = NULL; - if(tomatch->len>255) - { + if(tomatch->len > 255) { LM_DBG("target too long (max 255): %s\n", tomatch->s); return -3; } gr = sr_phonenum_get_record(name); - if(gr==NULL) - { + if(gr == NULL) { LM_DBG("container not found: %s\n", tomatch->s); - return - 4; + return -4; + } + if(gr->record != NULL) { + telnum_free(gr->record); } strncpy(gr->tomatch, tomatch->s, tomatch->len); gr->tomatch[tomatch->len] = '\0'; LM_DBG("attempt to match: %s\n", gr->tomatch); - if (gr->record == NULL) - { + gr->record = telnum_parse(gr->tomatch, "ZZ"); + if(gr->record == NULL) { LM_DBG("no match for: %s\n", gr->tomatch); return -2; }