Browse code

geoip: removed unneeded lib kmi from Makefile

- bits of formatting and removed snv ids

Daniel-Constantin Mierla authored on 02/01/2017 08:05:46
Showing 1 changed files
... ...
@@ -1,5 +1,4 @@
1 1
 /**
2
- * $Id$
3 2
  *
4 3
  * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
5 4
  *
... ...
@@ -174,14 +173,14 @@ int pv_parse_geoip_name(pv_spec_p sp, str *in)
174 173
 
175 174
 	switch(pvs.len)
176 175
 	{
177
-		case 2: 
176
+		case 2:
178 177
 			if(strncmp(pvs.s, "cc", 2)==0)
179 178
 				gpv->type = 0;
180 179
 			else if(strncmp(pvs.s, "tz", 2)==0)
181 180
 				gpv->type = 1;
182 181
 			else goto error;
183 182
 		break;
184
-		case 3: 
183
+		case 3:
185 184
 			if(strncmp(pvs.s, "zip", 3)==0)
186 185
 				gpv->type = 2;
187 186
 			else if(strncmp(pvs.s, "lat", 3)==0)
... ...
@@ -196,7 +195,7 @@ int pv_parse_geoip_name(pv_spec_p sp, str *in)
196 195
 				gpv->type = 7;
197 196
 			else goto error;
198 197
 		break;
199
-		case 4: 
198
+		case 4:
200 199
 			if(strncmp(pvs.s, "city", 4)==0)
201 200
 				gpv->type = 8;
202 201
 			else if(strncmp(pvs.s, "area", 4)==0)
... ...
@@ -207,7 +206,7 @@ int pv_parse_geoip_name(pv_spec_p sp, str *in)
207 206
 				gpv->type = 11;
208 207
 			else goto error;
209 208
 		break;
210
-		case 5: 
209
+		case 5:
211 210
 			if(strncmp(pvs.s, "metro", 5)==0)
212 211
 				gpv->type = 12;
213 212
 			else if(strncmp(pvs.s, "contc", 5)==0)
... ...
@@ -367,7 +366,7 @@ int pv_get_geoip(struct sip_msg *msg, pv_param_t *param,
367 366
 int geoip_init_pv(char *path)
368 367
 {
369 368
 	_handle_GeoIP = GeoIP_open(path, GEOIP_MMAP_CACHE);
370
-	
369
+
371 370
 	if(_handle_GeoIP==NULL)
372 371
 	{
373 372
 		LM_ERR("cannot open GeoIP database file at: %s\n", path);
... ...
@@ -392,7 +391,7 @@ void geoip_destroy_pv(void)
392 391
 void geoip_pv_reset(str *name)
393 392
 {
394 393
 	sr_geoip_record_t *gr = NULL;
395
-	
394
+
396 395
 	gr = sr_geoip_get_record(name);
397 396
 
398 397
 	if(gr==NULL)
... ...
@@ -407,13 +406,13 @@ void geoip_pv_reset(str *name)
407 406
 int geoip_update_pv(str *tomatch, str *name)
408 407
 {
409 408
 	sr_geoip_record_t *gr = NULL;
410
-	
409
+
411 410
 	if(tomatch->len>255)
412 411
 	{
413 412
 		LM_DBG("target too long (max 255): %s\n", tomatch->s);
414 413
 		return -3;
415 414
 	}
416
-	
415
+
417 416
 	gr = sr_geoip_get_record(name);
418 417
 	if(gr==NULL)
419 418
 	{
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -29,9 +29,9 @@
29 29
 #include <stdlib.h>
30 30
 #include <time.h>
31 31
 
32
-#include "../../dprint.h"
33
-#include "../../hashes.h"
34
-#include "../../pvar.h"
32
+#include "../../core/dprint.h"
33
+#include "../../core/hashes.h"
34
+#include "../../core/pvar.h"
35 35
 
36 36
 #include "geoip_pv.h"
37 37
 
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,438 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
5
+ *
6
+ * This file is part of Kamailio, a free SIP server.
7
+ *
8
+ * This file is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ *
14
+ * This file is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
+ *
23
+ */
24
+
25
+
26
+#include <assert.h>
27
+#include <ctype.h>
28
+#include <string.h>
29
+#include <stdlib.h>
30
+#include <time.h>
31
+
32
+#include "../../dprint.h"
33
+#include "../../hashes.h"
34
+#include "../../pvar.h"
35
+
36
+#include "geoip_pv.h"
37
+
38
+typedef struct _sr_geoip_record {
39
+	GeoIPRecord *record;
40
+	char *time_zone;
41
+	char *region_name;
42
+	char **range;
43
+	char latitude[16];
44
+	char longitude[16];
45
+	char tomatch[256];
46
+	int flags;
47
+} sr_geoip_record_t;
48
+
49
+typedef struct _sr_geoip_item {
50
+	str pvclass;
51
+	unsigned int hashid;
52
+	sr_geoip_record_t r;
53
+	struct _sr_geoip_item *next;
54
+} sr_geoip_item_t;
55
+
56
+typedef struct _geoip_pv {
57
+	sr_geoip_item_t *item;
58
+	int type;
59
+} geoip_pv_t;
60
+
61
+static GeoIP *_handle_GeoIP = NULL;
62
+
63
+static sr_geoip_item_t *_sr_geoip_list = NULL;
64
+
65
+sr_geoip_record_t *sr_geoip_get_record(str *name)
66
+{
67
+	sr_geoip_item_t *it = NULL;
68
+	unsigned int hashid = 0;
69
+
70
+	hashid =  get_hash1_raw(name->s, name->len);
71
+
72
+	it = _sr_geoip_list;
73
+	while(it!=NULL)
74
+	{
75
+		if(it->hashid==hashid && it->pvclass.len == name->len
76
+				&& strncmp(it->pvclass.s, name->s, name->len)==0)
77
+			return &it->r;
78
+		it = it->next;
79
+	}
80
+	return NULL;
81
+}
82
+
83
+sr_geoip_item_t *sr_geoip_add_item(str *name)
84
+{
85
+	sr_geoip_item_t *it = NULL;
86
+	unsigned int hashid = 0;
87
+
88
+	hashid =  get_hash1_raw(name->s, name->len);
89
+
90
+	it = _sr_geoip_list;
91
+	while(it!=NULL)
92
+	{
93
+		if(it->hashid==hashid && it->pvclass.len == name->len
94
+				&& strncmp(it->pvclass.s, name->s, name->len)==0)
95
+			return it;
96
+		it = it->next;
97
+	}
98
+	/* add new */
99
+	it = (sr_geoip_item_t*)pkg_malloc(sizeof(sr_geoip_item_t));
100
+	if(it==NULL)
101
+	{
102
+		LM_ERR("no more pkg\n");
103
+		return NULL;
104
+	}
105
+	memset(it, 0, sizeof(sr_geoip_item_t));
106
+	it->pvclass.s = (char*)pkg_malloc(name->len+1);
107
+	if(it->pvclass.s==NULL)
108
+	{
109
+		LM_ERR("no more pkg.\n");
110
+		pkg_free(it);
111
+		return NULL;
112
+	}
113
+	memcpy(it->pvclass.s, name->s, name->len);
114
+	it->pvclass.s[name->len] = '\0';
115
+	it->pvclass.len = name->len;
116
+	it->hashid = hashid;
117
+	it->next = _sr_geoip_list;
118
+	_sr_geoip_list = it;
119
+	return it;
120
+}
121
+
122
+
123
+int pv_parse_geoip_name(pv_spec_p sp, str *in)
124
+{
125
+	geoip_pv_t *gpv=NULL;
126
+	char *p;
127
+	str pvc;
128
+	str pvs;
129
+	if(sp==NULL || in==NULL || in->len<=0)
130
+		return -1;
131
+
132
+	gpv = (geoip_pv_t*)pkg_malloc(sizeof(geoip_pv_t));
133
+	if(gpv==NULL)
134
+		return -1;
135
+
136
+	memset(gpv, 0, sizeof(geoip_pv_t));
137
+
138
+	p = in->s;
139
+
140
+	while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
141
+		p++;
142
+	if(p>in->s+in->len || *p=='\0')
143
+		goto error;
144
+	pvc.s = p;
145
+	while(p < in->s + in->len)
146
+	{
147
+		if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
148
+			break;
149
+		p++;
150
+	}
151
+	if(p>in->s+in->len || *p=='\0')
152
+		goto error;
153
+	pvc.len = p - pvc.s;
154
+	if(*p!='=')
155
+	{
156
+		while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
157
+			p++;
158
+		if(p>in->s+in->len || *p=='\0' || *p!='=')
159
+			goto error;
160
+	}
161
+	p++;
162
+	if(*p!='>')
163
+		goto error;
164
+	p++;
165
+
166
+	pvs.len = in->len - (int)(p - in->s);
167
+	pvs.s = p;
168
+	LM_DBG("geoip [%.*s] - key [%.*s]\n", pvc.len, pvc.s,
169
+			pvs.len, pvs.s);
170
+
171
+	gpv->item = sr_geoip_add_item(&pvc);
172
+	if(gpv->item==NULL)
173
+		goto error;
174
+
175
+	switch(pvs.len)
176
+	{
177
+		case 2: 
178
+			if(strncmp(pvs.s, "cc", 2)==0)
179
+				gpv->type = 0;
180
+			else if(strncmp(pvs.s, "tz", 2)==0)
181
+				gpv->type = 1;
182
+			else goto error;
183
+		break;
184
+		case 3: 
185
+			if(strncmp(pvs.s, "zip", 3)==0)
186
+				gpv->type = 2;
187
+			else if(strncmp(pvs.s, "lat", 3)==0)
188
+				gpv->type = 3;
189
+			else if(strncmp(pvs.s, "lon", 3)==0)
190
+				gpv->type = 4;
191
+			else if(strncmp(pvs.s, "dma", 3)==0)
192
+				gpv->type = 5;
193
+			else if(strncmp(pvs.s, "ips", 3)==0)
194
+				gpv->type = 6;
195
+			else if(strncmp(pvs.s, "ipe", 3)==0)
196
+				gpv->type = 7;
197
+			else goto error;
198
+		break;
199
+		case 4: 
200
+			if(strncmp(pvs.s, "city", 4)==0)
201
+				gpv->type = 8;
202
+			else if(strncmp(pvs.s, "area", 4)==0)
203
+				gpv->type = 9;
204
+			else if(strncmp(pvs.s, "regc", 4)==0)
205
+				gpv->type = 10;
206
+			else if(strncmp(pvs.s, "regn", 4)==0)
207
+				gpv->type = 11;
208
+			else goto error;
209
+		break;
210
+		case 5: 
211
+			if(strncmp(pvs.s, "metro", 5)==0)
212
+				gpv->type = 12;
213
+			else if(strncmp(pvs.s, "contc", 5)==0)
214
+				gpv->type = 13;
215
+			else goto error;
216
+		break;
217
+		default:
218
+			goto error;
219
+	}
220
+	sp->pvp.pvn.u.dname = (void*)gpv;
221
+	sp->pvp.pvn.type = PV_NAME_OTHER;
222
+
223
+	return 0;
224
+
225
+error:
226
+	if(gpv!=NULL)
227
+		pkg_free(gpv);
228
+
229
+	LM_ERR("error at PV geoip name: %.*s\n", in->len, in->s);
230
+	return -1;
231
+}
232
+
233
+int pv_geoip_get_strzval(struct sip_msg *msg, pv_param_t *param,
234
+		pv_value_t *res, char *sval)
235
+{
236
+	str s;
237
+	if(sval==NULL)
238
+		return pv_get_null(msg, param, res);
239
+
240
+	s.s = sval;
241
+	s.len = strlen(s.s);
242
+	return pv_get_strval(msg, param, res, &s);
243
+}
244
+
245
+int pv_get_geoip(struct sip_msg *msg, pv_param_t *param,
246
+		pv_value_t *res)
247
+{
248
+	geoip_pv_t *gpv;
249
+
250
+	if(msg==NULL || param==NULL)
251
+		return -1;
252
+
253
+	gpv = (geoip_pv_t*)param->pvn.u.dname;
254
+	if(gpv==NULL)
255
+		return -1;
256
+	if(gpv->item==NULL)
257
+		return pv_get_null(msg, param, res);
258
+
259
+	switch(gpv->type)
260
+	{
261
+		case 1: /* tz */
262
+			if(gpv->item->r.time_zone==NULL)
263
+			{
264
+				if(gpv->item->r.flags&1)
265
+					return pv_get_null(msg, param, res);
266
+				if(gpv->item->r.record==NULL)
267
+					return pv_get_null(msg, param, res);
268
+				gpv->item->r.time_zone
269
+					= (char*)GeoIP_time_zone_by_country_and_region(
270
+						gpv->item->r.record->country_code,
271
+						gpv->item->r.record->region);
272
+				gpv->item->r.flags |= 1;
273
+			}
274
+			return pv_geoip_get_strzval(msg, param, res,
275
+					gpv->item->r.time_zone);
276
+		case 2: /* zip */
277
+			if(gpv->item->r.record==NULL)
278
+				return pv_get_null(msg, param, res);
279
+			return pv_geoip_get_strzval(msg, param, res,
280
+					gpv->item->r.record->postal_code);
281
+		case 3: /* lat */
282
+			if((gpv->item->r.flags&2)==0)
283
+			{
284
+				if(gpv->item->r.record==NULL)
285
+					return pv_get_null(msg, param, res);
286
+				snprintf(gpv->item->r.latitude, 15, "%f",
287
+						gpv->item->r.record->latitude);
288
+				gpv->item->r.flags |= 2;
289
+			}
290
+			return pv_geoip_get_strzval(msg, param, res,
291
+					gpv->item->r.latitude);
292
+		case 4: /* lon */
293
+			if((gpv->item->r.flags&4)==0)
294
+			{
295
+				if(gpv->item->r.record==NULL)
296
+					return pv_get_null(msg, param, res);
297
+				snprintf(gpv->item->r.longitude, 15, "%f",
298
+						gpv->item->r.record->longitude);
299
+				gpv->item->r.flags |= 4;
300
+			}
301
+			return pv_geoip_get_strzval(msg, param, res,
302
+					gpv->item->r.longitude);
303
+		case 5: /* dma */
304
+			if(gpv->item->r.record==NULL)
305
+				return pv_get_null(msg, param, res);
306
+			return pv_get_sintval(msg, param, res,
307
+					gpv->item->r.record->dma_code);
308
+		case 6: /* ips */
309
+		case 7: /* ipe */
310
+			if((gpv->item->r.flags&8)==0)
311
+			{
312
+				gpv->item->r.range = GeoIP_range_by_ip(_handle_GeoIP,
313
+					gpv->item->r.tomatch);
314
+				gpv->item->r.flags |= 8;
315
+			}
316
+			if(gpv->item->r.range==NULL)
317
+				return pv_get_null(msg, param, res);
318
+			if(gpv->type==6)
319
+				return pv_geoip_get_strzval(msg, param, res,
320
+						gpv->item->r.range[0]);
321
+			return pv_geoip_get_strzval(msg, param, res,
322
+					gpv->item->r.range[1]);
323
+		case 8: /* city */
324
+			return pv_geoip_get_strzval(msg, param, res,
325
+					gpv->item->r.record->city);
326
+		case 9: /* area */
327
+			if(gpv->item->r.record==NULL)
328
+				return pv_get_null(msg, param, res);
329
+			return pv_get_sintval(msg, param, res,
330
+					gpv->item->r.record->area_code);
331
+		case 10: /* regc */
332
+			if(gpv->item->r.record==NULL)
333
+				return pv_get_null(msg, param, res);
334
+			return pv_geoip_get_strzval(msg, param, res,
335
+					gpv->item->r.record->region);
336
+		case 11: /* regn */
337
+			if((gpv->item->r.flags&16)==0)
338
+			{
339
+				if(gpv->item->r.record==NULL)
340
+					return pv_get_null(msg, param, res);
341
+				gpv->item->r.region_name
342
+						= (char*)GeoIP_region_name_by_code(
343
+							gpv->item->r.record->country_code,
344
+							gpv->item->r.record->region);
345
+				gpv->item->r.flags |= 16;
346
+			}
347
+			return pv_geoip_get_strzval(msg, param, res,
348
+					gpv->item->r.region_name);
349
+		case 12: /* metro */
350
+			if(gpv->item->r.record==NULL)
351
+				return pv_get_null(msg, param, res);
352
+			return pv_get_sintval(msg, param, res,
353
+					gpv->item->r.record->metro_code);
354
+		case 13: /* contc */
355
+			if(gpv->item->r.record==NULL)
356
+				return pv_get_null(msg, param, res);
357
+			return pv_geoip_get_strzval(msg, param, res,
358
+					gpv->item->r.record->continent_code);
359
+		default: /* cc */
360
+			if(gpv->item->r.record==NULL)
361
+				return pv_get_null(msg, param, res);
362
+			return pv_geoip_get_strzval(msg, param, res,
363
+					gpv->item->r.record->country_code);
364
+	}
365
+}
366
+
367
+int geoip_init_pv(char *path)
368
+{
369
+	_handle_GeoIP = GeoIP_open(path, GEOIP_MMAP_CACHE);
370
+	
371
+	if(_handle_GeoIP==NULL)
372
+	{
373
+		LM_ERR("cannot open GeoIP database file at: %s\n", path);
374
+		return -1;
375
+	}
376
+	return 0;
377
+}
378
+
379
+void geoip_destroy_list(void)
380
+{
381
+}
382
+
383
+void geoip_destroy_pv(void)
384
+{
385
+	if(_handle_GeoIP!=NULL)
386
+	{
387
+		GeoIP_delete(_handle_GeoIP);
388
+		_handle_GeoIP=NULL;
389
+	}
390
+}
391
+
392
+void geoip_pv_reset(str *name)
393
+{
394
+	sr_geoip_record_t *gr = NULL;
395
+	
396
+	gr = sr_geoip_get_record(name);
397
+
398
+	if(gr==NULL)
399
+		return;
400
+	if(gr->range!=NULL)
401
+		GeoIP_range_by_ip_delete(gr->range);
402
+	if(gr->record!=NULL)
403
+		GeoIPRecord_delete(gr->record);
404
+	memset(gr, 0, sizeof(struct _sr_geoip_record));
405
+}
406
+
407
+int geoip_update_pv(str *tomatch, str *name)
408
+{
409
+	sr_geoip_record_t *gr = NULL;
410
+	
411
+	if(tomatch->len>255)
412
+	{
413
+		LM_DBG("target too long (max 255): %s\n", tomatch->s);
414
+		return -3;
415
+	}
416
+	
417
+	gr = sr_geoip_get_record(name);
418
+	if(gr==NULL)
419
+	{
420
+		LM_DBG("container not found: %s\n", tomatch->s);
421
+		return - 4;
422
+	}
423
+
424
+	strncpy(gr->tomatch, tomatch->s, tomatch->len);
425
+	gr->tomatch[tomatch->len] = '\0';
426
+	gr->record = GeoIP_record_by_name(_handle_GeoIP,
427
+			(const char*)gr->tomatch);
428
+	LM_DBG("attempt to match: %s\n", gr->tomatch);
429
+	if (gr->record == NULL)
430
+	{
431
+		LM_DBG("no match for: %s\n", gr->tomatch);
432
+		return -2;
433
+	}
434
+	LM_DBG("geoip PV updated for: %s\n", gr->tomatch);
435
+
436
+	return 1;
437
+}
438
+