Browse code

phonenum: set record after phone number lookup and return pv attributes

Daniel-Constantin Mierla authored on 08/11/2017 10:59:31
Showing 2 changed files
... ...
@@ -39,19 +39,19 @@ MODULE_VERSION
39 39
 
40 40
 static int phonenum_smode = 0;
41 41
 
42
-static int  mod_init(void);
42
+static int mod_init(void);
43 43
 static void mod_destroy(void);
44 44
 
45
-static int w_phonenum_match(struct sip_msg* msg, char* str1, char* str2);
45
+static int w_phonenum_match(struct sip_msg *msg, char *str1, char *str2);
46 46
 static int phonenum_match(sip_msg_t *msg, str *tomatch, str *pvclass);
47 47
 
48
+/* clang-format off */
48 49
 static pv_export_t mod_pvs[] = {
49 50
 	{ {"phn", sizeof("phn")-1}, PVT_OTHER, pv_get_phonenum, 0,
50 51
 		pv_parse_phonenum_name, 0, 0, 0 },
51 52
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
52 53
 };
53 54
 
54
-
55 55
 static cmd_export_t cmds[]={
56 56
 	{"phonenum_match", (cmd_function)w_phonenum_match, 2, fixup_spve_spve,
57 57
 		0, ANY_ROUTE},
... ...
@@ -66,8 +66,8 @@ static param_export_t params[]={
66 66
 struct module_exports exports = {
67 67
 	"phonenum",
68 68
 	DEFAULT_DLFLAGS, /* dlopen flags */
69
-	cmds,
70
-	params,
69
+	cmds,            /* exported config functions */
70
+	params,          /* exported config parameters */
71 71
 	0,
72 72
 	0,              /* exported MI functions */
73 73
 	mod_pvs,        /* exported pseudo-variables */
... ...
@@ -77,7 +77,7 @@ struct module_exports exports = {
77 77
 	mod_destroy,    /* destroy function */
78 78
 	0               /* per child init function */
79 79
 };
80
-
80
+/* clang-format on */
81 81
 
82 82
 /**
83 83
  * init module function
... ...
@@ -85,8 +85,7 @@ struct module_exports exports = {
85 85
 static int mod_init(void)
86 86
 {
87 87
 
88
-	if(phonenum_init_pv(phonenum_smode)!=0)
89
-	{
88
+	if(phonenum_init_pv(phonenum_smode) != 0) {
90 89
 		LM_ERR("cannot do init\n");
91 90
 		return -1;
92 91
 	}
... ...
@@ -109,21 +108,21 @@ static int phonenum_match(sip_msg_t *msg, str *tomatch, str *pvclass)
109 108
 	return phonenum_update_pv(tomatch, pvclass);
110 109
 }
111 110
 
112
-static int w_phonenum_match(sip_msg_t* msg, char* target, char* pvname)
111
+static int w_phonenum_match(sip_msg_t *msg, char *target, char *pvname)
113 112
 {
114 113
 	str tomatch = STR_NULL;
115 114
 	str pvclass = STR_NULL;
116 115
 
117
-	if(msg==NULL) {
116
+	if(msg == NULL) {
118 117
 		LM_ERR("received null msg\n");
119 118
 		return -1;
120 119
 	}
121 120
 
122
-	if(fixup_get_svalue(msg, (gparam_t*)target, &tomatch)<0) {
121
+	if(fixup_get_svalue(msg, (gparam_t *)target, &tomatch) < 0) {
123 122
 		LM_ERR("cannot get the address\n");
124 123
 		return -1;
125 124
 	}
126
-	if(fixup_get_svalue(msg, (gparam_t*)pvname, &pvclass)<0) {
125
+	if(fixup_get_svalue(msg, (gparam_t *)pvname, &pvclass) < 0) {
127 126
 		LM_ERR("cannot get the pv class\n");
128 127
 		return -1;
129 128
 	}
... ...
@@ -134,17 +133,20 @@ static int w_phonenum_match(sip_msg_t* msg, char* target, char* pvname)
134 133
 /**
135 134
  *
136 135
  */
136
+/* clang-format off */
137 137
 static sr_kemi_t sr_kemi_phonenum_exports[] = {
138
-    { str_init("phonenum"), str_init("match"),
139
-        SR_KEMIP_INT, phonenum_match,
140
-        { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
141
-            SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
142
-    },
138
+	{ str_init("phonenum"), str_init("match"),
139
+		SR_KEMIP_INT, phonenum_match,
140
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
141
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
142
+	},
143 143
 
144
-    { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
144
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
145 145
 };
146
+/* clang-format on */
146 147
 
147
-int mod_register(char *path, int *dlflags, void *p1, void *p2) {
148
-    sr_kemi_modules_add(sr_kemi_phonenum_exports);
149
-    return 0;
148
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
149
+{
150
+	sr_kemi_modules_add(sr_kemi_phonenum_exports);
151
+	return 0;
150 152
 }
... ...
@@ -35,14 +35,11 @@
35 35
 #include "cphonenumber.h"
36 36
 #include "phonenum_pv.h"
37 37
 
38
+/* clang-format off */
38 39
 typedef struct _sr_phonenum_record {
39 40
 	telnum_t *record;
40
-	char *number;
41
-	char *country;
42
-	char *region;
43
-	char *operator;
44 41
 	char tomatch[256];
45
-	int flags;
42
+	long flags;
46 43
 } sr_phonenum_record_t;
47 44
 
48 45
 typedef struct _sr_phonenum_item {
... ...
@@ -56,6 +53,7 @@ typedef struct _phonenum_pv {
56 53
 	sr_phonenum_item_t *item;
57 54
 	int type;
58 55
 } phonenum_pv_t;
56
+/* clang-format on */
59 57
 
60 58
 
61 59
 static sr_phonenum_item_t *_sr_phonenum_list = NULL;
... ...
@@ -65,13 +63,12 @@ sr_phonenum_record_t *sr_phonenum_get_record(str *name)
65 63
 	sr_phonenum_item_t *it = NULL;
66 64
 	unsigned int hashid = 0;
67 65
 
68
-	hashid =  get_hash1_raw(name->s, name->len);
66
+	hashid = get_hash1_raw(name->s, name->len);
69 67
 
70 68
 	it = _sr_phonenum_list;
71
-	while(it!=NULL)
72
-	{
73
-		if(it->hashid==hashid && it->pvclass.len == name->len
74
-				&& strncmp(it->pvclass.s, name->s, name->len)==0)
69
+	while(it != NULL) {
70
+		if(it->hashid == hashid && it->pvclass.len == name->len
71
+				&& strncmp(it->pvclass.s, name->s, name->len) == 0)
75 72
 			return &it->r;
76 73
 		it = it->next;
77 74
 	}
... ...
@@ -83,27 +80,24 @@ sr_phonenum_item_t *sr_phonenum_add_item(str *name)
83 80
 	sr_phonenum_item_t *it = NULL;
84 81
 	unsigned int hashid = 0;
85 82
 
86
-	hashid =  get_hash1_raw(name->s, name->len);
83
+	hashid = get_hash1_raw(name->s, name->len);
87 84
 
88 85
 	it = _sr_phonenum_list;
89
-	while(it!=NULL)
90
-	{
91
-		if(it->hashid==hashid && it->pvclass.len == name->len
92
-				&& strncmp(it->pvclass.s, name->s, name->len)==0)
86
+	while(it != NULL) {
87
+		if(it->hashid == hashid && it->pvclass.len == name->len
88
+				&& strncmp(it->pvclass.s, name->s, name->len) == 0)
93 89
 			return it;
94 90
 		it = it->next;
95 91
 	}
96 92
 	/* add new */
97
-	it = (sr_phonenum_item_t*)pkg_malloc(sizeof(sr_phonenum_item_t));
98
-	if(it==NULL)
99
-	{
93
+	it = (sr_phonenum_item_t *)pkg_malloc(sizeof(sr_phonenum_item_t));
94
+	if(it == NULL) {
100 95
 		LM_ERR("no more pkg\n");
101 96
 		return NULL;
102 97
 	}
103 98
 	memset(it, 0, sizeof(sr_phonenum_item_t));
104
-	it->pvclass.s = (char*)pkg_malloc(name->len+1);
105
-	if(it->pvclass.s==NULL)
106
-	{
99
+	it->pvclass.s = (char *)pkg_malloc(name->len + 1);
100
+	if(it->pvclass.s == NULL) {
107 101
 		LM_ERR("no more pkg.\n");
108 102
 		pkg_free(it);
109 103
 		return NULL;
... ...
@@ -120,96 +114,103 @@ sr_phonenum_item_t *sr_phonenum_add_item(str *name)
120 114
 
121 115
 int pv_parse_phonenum_name(pv_spec_p sp, str *in)
122 116
 {
123
-	phonenum_pv_t *gpv=NULL;
117
+	phonenum_pv_t *gpv = NULL;
124 118
 	char *p;
125 119
 	str pvc;
126 120
 	str pvs;
127
-	if(sp==NULL || in==NULL || in->len<=0)
121
+	if(sp == NULL || in == NULL || in->len <= 0)
128 122
 		return -1;
129 123
 
130
-	gpv = (phonenum_pv_t*)pkg_malloc(sizeof(phonenum_pv_t));
131
-	if(gpv==NULL)
124
+	gpv = (phonenum_pv_t *)pkg_malloc(sizeof(phonenum_pv_t));
125
+	if(gpv == NULL)
132 126
 		return -1;
133 127
 
134 128
 	memset(gpv, 0, sizeof(phonenum_pv_t));
135 129
 
136 130
 	p = in->s;
137 131
 
138
-	while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
132
+	while(p < in->s + in->len
133
+			&& (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
139 134
 		p++;
140
-	if(p>in->s+in->len || *p=='\0')
135
+	if(p > in->s + in->len || *p == '\0')
141 136
 		goto error;
142 137
 	pvc.s = p;
143
-	while(p < in->s + in->len)
144
-	{
145
-		if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
138
+	while(p < in->s + in->len) {
139
+		if(*p == '=' || *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
146 140
 			break;
147 141
 		p++;
148 142
 	}
149
-	if(p>in->s+in->len || *p=='\0')
143
+	if(p > in->s + in->len || *p == '\0')
150 144
 		goto error;
151 145
 	pvc.len = p - pvc.s;
152
-	if(*p!='=')
153
-	{
154
-		while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
146
+	if(*p != '=') {
147
+		while(p < in->s + in->len
148
+				&& (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
155 149
 			p++;
156
-		if(p>in->s+in->len || *p=='\0' || *p!='=')
150
+		if(p > in->s + in->len || *p == '\0' || *p != '=')
157 151
 			goto error;
158 152
 	}
159 153
 	p++;
160
-	if(*p!='>')
154
+	if(*p != '>')
161 155
 		goto error;
162 156
 	p++;
163 157
 
164 158
 	pvs.len = in->len - (int)(p - in->s);
165 159
 	pvs.s = p;
166
-	LM_DBG("phonenum [%.*s] - key [%.*s]\n", pvc.len, pvc.s,
167
-			pvs.len, pvs.s);
160
+	LM_DBG("phonenum [%.*s] - key [%.*s]\n", pvc.len, pvc.s, pvs.len, pvs.s);
168 161
 
169 162
 	gpv->item = sr_phonenum_add_item(&pvc);
170
-	if(gpv->item==NULL)
163
+	if(gpv->item == NULL)
171 164
 		goto error;
172 165
 
173
-	switch(pvs.len)
174
-	{
166
+	switch(pvs.len) {
167
+		case 5:
168
+			if(strncmp(pvs.s, "ltype", 6) == 0)
169
+				gpv->type = 2;
170
+			else if(strncmp(pvs.s, "ndesc", 6) == 0)
171
+				gpv->type = 3;
172
+			else if(strncmp(pvs.s, "error", 6) == 0)
173
+				gpv->type = 4;
174
+			else if(strncmp(pvs.s, "cctel", 6) == 0)
175
+				gpv->type = 5;
176
+			else if(strncmp(pvs.s, "valid", 6) == 0)
177
+				gpv->type = 6;
178
+			else
179
+				goto error;
180
+			break;		
175 181
 		case 6:
176
-			if(strncmp(pvs.s, "number", 6)==0)
182
+			if(strncmp(pvs.s, "number", 6) == 0)
177 183
 				gpv->type = 0;
178
-			else if(strncmp(pvs.s, "region", 6)==0)
179
-				gpv->type = 2;
180
-			else goto error;
181
-		break;
182
-		case 7:
183
-			if(strncmp(pvs.s, "country", 7)==0)
184
+			else
185
+				goto error;
186
+			break;
187
+		case 10:
188
+			if(strncmp(pvs.s, "normalized", 10) == 0)
184 189
 				gpv->type = 1;
185
-			else goto error;
186
-		break;
187
-		case 8:
188
-			if(strncmp(pvs.s, "operator", 8)==0)
189
-				gpv->type = 3;
190
-			else goto error;
191
-		break;
190
+			else
191
+				goto error;
192
+			break;
192 193
 		default:
193 194
 			goto error;
194 195
 	}
195
-	sp->pvp.pvn.u.dname = (void*)gpv;
196
+	sp->pvp.pvn.u.dname = (void *)gpv;
196 197
 	sp->pvp.pvn.type = PV_NAME_OTHER;
197 198
 
198 199
 	return 0;
199 200
 
200 201
 error:
201
-	if(gpv!=NULL)
202
+	if(gpv != NULL)
202 203
 		pkg_free(gpv);
203 204
 
204 205
 	LM_ERR("error at PV phonenum name: %.*s\n", in->len, in->s);
205 206
 	return -1;
206 207
 }
207 208
 
208
-int pv_phonenum_get_strzval(struct sip_msg *msg, pv_param_t *param,
209
-		pv_value_t *res, char *sval)
209
+int pv_phonenum_get_strzval(
210
+		struct sip_msg *msg, pv_param_t *param, pv_value_t *res, char *sval)
210 211
 {
211 212
 	str s;
212
-	if(sval==NULL)
213
+	if(sval == NULL)
213 214
 		return pv_get_null(msg, param, res);
214 215
 
215 216
 	s.s = sval;
... ...
@@ -217,30 +218,47 @@ int pv_phonenum_get_strzval(struct sip_msg *msg, pv_param_t *param,
217 218
 	return pv_get_strval(msg, param, res, &s);
218 219
 }
219 220
 
220
-int pv_get_phonenum(struct sip_msg *msg, pv_param_t *param,
221
-		pv_value_t *res)
221
+int pv_get_phonenum(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
222 222
 {
223 223
 	phonenum_pv_t *gpv;
224 224
 
225
-	if(msg==NULL || param==NULL)
225
+	if(msg == NULL || param == NULL)
226 226
 		return -1;
227 227
 
228
-	gpv = (phonenum_pv_t*)param->pvn.u.dname;
229
-	if(gpv==NULL)
228
+	gpv = (phonenum_pv_t *)param->pvn.u.dname;
229
+	if(gpv == NULL)
230 230
 		return -1;
231
-	if(gpv->item==NULL)
231
+	if(gpv->item == NULL)
232
+		return pv_get_null(msg, param, res);
233
+	if(gpv->item->r.record==NULL)
232 234
 		return pv_get_null(msg, param, res);
233 235
 
234
-	switch(gpv->type)
235
-	{
236
-		case 1: /* country */
237
-			return pv_get_null(msg, param, res);
238
-		case 2: /* region */
239
-			return pv_get_null(msg, param, res);
240
-		case 3: /* operator */
241
-			return pv_get_null(msg, param, res);
236
+	switch(gpv->type) {
237
+		case 1: /* normalized */
238
+			if(gpv->item->r.record->normalized==NULL)
239
+				return pv_get_null(msg, param, res);
240
+			return pv_get_strzval(msg, param, res,
241
+						gpv->item->r.record->normalized);
242
+		case 2: /* ltype */
243
+			if(gpv->item->r.record->ltype==NULL)
244
+				return pv_get_null(msg, param, res);
245
+			return pv_get_strzval(msg, param, res, gpv->item->r.record->ltype);
246
+		case 3: /* ndesc */
247
+			if(gpv->item->r.record->ndesc==NULL)
248
+				return pv_get_null(msg, param, res);
249
+			return pv_get_strzval(msg, param, res, gpv->item->r.record->ndesc);
250
+		case 4: /* error */
251
+			if(gpv->item->r.record->error==NULL)
252
+				return pv_get_null(msg, param, res);
253
+			return pv_get_strzval(msg, param, res, gpv->item->r.record->error);
254
+		case 5: /* cctel */
255
+			return pv_get_sintval(msg, param, res, gpv->item->r.record->cctel);
256
+		case 6: /* valid */
257
+			return pv_get_sintval(msg, param, res, gpv->item->r.record->valid);
242 258
 		default: /* number */
243
-			return pv_get_null(msg, param, res);
259
+			if(gpv->item->r.record->number==NULL)
260
+				return pv_get_null(msg, param, res);
261
+			return pv_get_strzval(msg, param, res, gpv->item->r.record->number);
244 262
 	}
245 263
 }
246 264
 
... ...
@@ -263,8 +281,11 @@ void phonenum_pv_reset(str *name)
263 281
 
264 282
 	gr = sr_phonenum_get_record(name);
265 283
 
266
-	if(gr==NULL)
284
+	if(gr == NULL)
267 285
 		return;
286
+	if(gr->record != NULL) {
287
+		telnum_free(gr->record);
288
+	}
268 289
 	memset(gr, 0, sizeof(sr_phonenum_record_t));
269 290
 }
270 291
 
... ...
@@ -272,24 +293,25 @@ int phonenum_update_pv(str *tomatch, str *name)
272 293
 {
273 294
 	sr_phonenum_record_t *gr = NULL;
274 295
 
275
-	if(tomatch->len>255)
276
-	{
296
+	if(tomatch->len > 255) {
277 297
 		LM_DBG("target too long (max 255): %s\n", tomatch->s);
278 298
 		return -3;
279 299
 	}
280 300
 
281 301
 	gr = sr_phonenum_get_record(name);
282
-	if(gr==NULL)
283
-	{
302
+	if(gr == NULL) {
284 303
 		LM_DBG("container not found: %s\n", tomatch->s);
285
-		return - 4;
304
+		return -4;
305
+	}
306
+	if(gr->record != NULL) {
307
+		telnum_free(gr->record);
286 308
 	}
287 309
 
288 310
 	strncpy(gr->tomatch, tomatch->s, tomatch->len);
289 311
 	gr->tomatch[tomatch->len] = '\0';
290 312
 	LM_DBG("attempt to match: %s\n", gr->tomatch);
291
-	if (gr->record == NULL)
292
-	{
313
+	gr->record = telnum_parse(gr->tomatch, "ZZ");
314
+	if(gr->record == NULL) {
293 315
 		LM_DBG("no match for: %s\n", gr->tomatch);
294 316
 		return -2;
295 317
 	}