Project

General

Profile

Docs ModGeoip » mod_geoip_for_1.5.c

mod_geoip for lighttpd 1.5 - tobsn, 2009-01-09 19:52

 
1
#include <GeoIP.h>
2
#include <GeoIPCity.h>
3

    
4
#include <ctype.h>
5
#include <stdlib.h>
6
#include <string.h>
7

    
8
#include "base.h"
9
#include "log.h"
10
#include "buffer.h"
11

    
12
#include "plugin.h"
13

    
14
#ifdef HAVE_CONFIG_H
15
#include "config.h"
16
#endif
17

    
18
/**
19
 * 
20
 * $mod_geoip.c (v2.0) (13.09.2006 00:29:11)
21
 *
22
 * Name:
23
 *	  mod_geoip.c
24
 *
25
 * Description:
26
 *	  GeoIP module (plugin) for lighttpd.
27
 *	  the module loads a geoip database of type "country" or "city" and
28
 *	  sets new ENV vars based on ip record lookups.
29
 *
30
 *	  country db env's:
31
 *			  GEOIP_COUNTRY_CODE
32
 *			  GEOIP_COUNTRY_CODE3
33
 *			  GEOIP_COUNTRY_NAME
34
 *
35
 *	  city db env's:
36
 *			  GEOIP_COUNTRY_CODE
37
 *			  GEOIP_COUNTRY_CODE3
38
 *			  GEOIP_COUNTRY_NAME
39
 *			  GEOIP_CITY_NAME
40
 *			  GEOIP_CITY_POSTAL_CODE
41
 *			  GEOIP_CITY_LATITUDE
42
 *			  GEOIP_CITY_LONG_LATITUDE
43
 *			  GEOIP_CITY_DMA_CODE
44
 *			  GEOIP_CITY_AREA_CODE
45
 * 
46
 * Usage (configuration options):
47
 *	  geoip.db-filename = <path to the geoip or geocity database>
48
 *	  geoip.memory-cache = <enable|disable> : default disabled
49
 *			  if enabled, mod_geoip will load the database binary file to
50
 *			  memory for very fast lookups. the only penalty is memory usage.
51
 *
52
 * Author:
53
 *	  Ami E. Bizamcher (amix)
54
 *	  duke.amix@gmail.com
55
 * 
56
 * Note:
57
 *	  GeoIP Library and API must be installed!
58
 */
59

    
60

    
61
/* plugin config for all request/connections */
62

    
63
typedef struct {
64
		unsigned short mem_cache;
65
		buffer  *db_name;
66
		GeoIP   *gi;
67
} plugin_config;
68

    
69
typedef struct {
70
		PLUGIN_DATA;
71

    
72
		plugin_config **config_storage;
73

    
74
		plugin_config conf; 
75
} plugin_data;
76

    
77
/* init the plugin data */
78
INIT_FUNC(mod_geoip_init) {
79
		plugin_data *p;
80

    
81
		p = calloc(1, sizeof(*p));
82

    
83
		return p;
84
}
85

    
86
/* destroy the plugin data */
87
FREE_FUNC(mod_geoip_free) {
88
		plugin_data *p = p_d;
89

    
90
		UNUSED(srv);
91

    
92
		if (!p) return HANDLER_GO_ON;
93

    
94
		if (p->config_storage) {
95
				size_t i;
96

    
97
				for (i = 0; i < srv->config_context->used; i++) {
98
						plugin_config *s = p->config_storage[i];
99

    
100
						if (!s) continue;
101

    
102
						buffer_free(s->db_name);
103

    
104
						/* clean up */
105
						if (s->gi) GeoIP_delete(s->gi);
106

    
107
						free(s);
108
				}
109
				free(p->config_storage);
110
		}
111

    
112
		free(p);
113

    
114
		return HANDLER_GO_ON;
115
}
116

    
117
/* handle plugin config and check values */
118

    
119
SETDEFAULTS_FUNC(mod_geoip_set_defaults) {
120
		plugin_data *p = p_d;
121
		size_t i = 0;
122

    
123
		config_values_t cv[] = { 
124
				{ "geoip.db-filename",  NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
125
				{ "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },	/* 1 */
126
				{ NULL,				 NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
127
		};
128

    
129
		if (!p) return HANDLER_ERROR;
130

    
131
		p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
132

    
133
		for (i = 0; i < srv->config_context->used; i++) {
134
				plugin_config *s;
135
				int mode;
136

    
137
				s = calloc(1, sizeof(plugin_config));
138

    
139
				s->db_name = buffer_init();
140
				s->mem_cache = 0; /* default: do not load db to cache */
141
				s->gi = NULL;
142

    
143
				cv[0].destination = s->db_name;
144
				cv[1].destination = &(s->mem_cache);
145

    
146
				p->config_storage[i] = s;
147

    
148
				if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
149
						return HANDLER_ERROR;
150
				}
151

    
152
				mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
153

    
154
				/* country db filename is requeried! */
155
				if (!buffer_is_empty(s->db_name)) {
156

    
157
						/* let's start cooking */
158
						if (s->mem_cache != 0)
159
								mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
160

    
161
						if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) {
162
								log_error_write(srv, __FILE__, __LINE__, "s",
163
										"failed to open GeoIP database!!!");
164

    
165
								return HANDLER_ERROR;
166
						}
167

    
168
						/* is the db supported ? */
169
						if (s->gi->databaseType != GEOIP_COUNTRY_EDITION &&
170
								s->gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
171
								s->gi->databaseType != GEOIP_CITY_EDITION_REV1) {
172
								log_error_write(srv, __FILE__, __LINE__, "s",
173
										"GeoIP database is of unsupported type!!!");
174
						}
175
				}
176
		}
177

    
178
		return HANDLER_GO_ON;
179
}
180

    
181
#define PATCH(x) \
182
		p->conf.x = s->x;
183
static int mod_geoip_patch_connection(server *srv, connection *con, plugin_data *p) {
184
		size_t i, j;
185
		plugin_config *s = p->config_storage[0];
186

    
187
		PATCH(db_name);
188
		PATCH(mem_cache);
189
		PATCH(gi);
190

    
191
		/* skip the first, the global context */
192
		for (i = 1; i < srv->config_context->used; i++) {
193
				data_config *dc = (data_config *)srv->config_context->data[i];
194
				s = p->config_storage[i];
195

    
196
				/* condition didn't match */
197
				if (!config_check_cond(srv, con, dc)) continue;
198

    
199
				/* merge config */
200
				for (j = 0; j < dc->value->used; j++) {
201
						data_unset *du = dc->value->data[j];
202

    
203
						if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.db-filename"))) {
204
								PATCH(db_name);
205
						}
206

    
207
						if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) {
208
								PATCH(mem_cache);
209
						}
210
				}
211
		}
212

    
213
		return 0;
214
}
215
#undef PATCH
216

    
217
URIHANDLER_FUNC(mod_geoip_subrequest) {
218
		plugin_data *p = p_d;
219

    
220
		mod_geoip_patch_connection(srv, con, p);
221

    
222
		if (!buffer_is_empty(p->conf.db_name)) {
223
				const char *remote_ip;
224
				GeoIP *gi;
225
				data_string *ds;
226
				const char *returnedCountry;
227

    
228
				remote_ip = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
229

    
230
				if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
231
						/* get the country code 2 chars */
232
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE")))) {
233
								if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) {
234
										if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
235
												ds = data_string_init();
236
										}
237

    
238
										buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
239
										buffer_copy_string(ds->value, returnedCountry);
240
										array_insert_unique(con->environment, (data_unset *)ds);
241
								}
242
						}
243

    
244
						/* get the country code 3 chars */
245
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE3")))) {
246
								if (NULL != (returnedCountry = GeoIP_country_code3_by_addr(p->conf.gi, remote_ip))) {
247
										if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
248
												ds = data_string_init();
249
										}
250

    
251
										buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
252
										buffer_copy_string(ds->value, returnedCountry);
253
										array_insert_unique(con->environment, (data_unset *)ds);
254
								}
255
						}
256

    
257
						/* get the country name */
258
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_NAME")))) {
259
								if (NULL != (returnedCountry = GeoIP_country_name_by_addr(p->conf.gi, remote_ip))) {
260
										if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
261
												ds = data_string_init();
262
										}
263

    
264
										buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
265
										buffer_copy_string(ds->value, returnedCountry);
266
										array_insert_unique(con->environment, (data_unset *)ds);
267
								}
268
						}
269

    
270
						/* go on... */
271
						return HANDLER_GO_ON;
272
				}
273

    
274
				/* if we are here, geo city is in use */
275
				GeoIPRecord * gir;
276

    
277
				if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
278
						/* get the country code 2 chars */
279
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE")))) {
280
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
281
										ds = data_string_init();
282
								}
283

    
284
								buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
285
								buffer_copy_string(ds->value, gir->country_code);
286
								array_insert_unique(con->environment, (data_unset *)ds);
287
						}
288

    
289
						/* get the country code 3 chars */
290
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE3")))) {
291
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
292
										ds = data_string_init();
293
								}
294

    
295
								buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
296
								buffer_copy_string(ds->value, gir->country_code3);
297
								array_insert_unique(con->environment, (data_unset *)ds);
298
						}
299

    
300
						/* get the country name */
301
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_NAME")))) {
302
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
303
										ds = data_string_init();
304
								}
305

    
306
								buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
307
								buffer_copy_string(ds->value, gir->country_name);
308
								array_insert_unique(con->environment, (data_unset *)ds);
309
						}
310

    
311
						/* get the city region */
312
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_REGION")))) {
313
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
314
										ds = data_string_init();
315
								}
316

    
317
								buffer_copy_string(ds->key, "GEOIP_CITY_REGION");
318
								buffer_copy_string(ds->value, gir->region);
319
								array_insert_unique(con->environment, (data_unset *)ds);
320
						}
321

    
322
						/* get the city */
323
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_NAME")))) {
324
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
325
										ds = data_string_init();
326
								}
327

    
328
								buffer_copy_string(ds->key, "GEOIP_CITY_NAME");
329
								buffer_copy_string(ds->value, gir->city);
330
								array_insert_unique(con->environment, (data_unset *)ds);
331
						}
332

    
333
						/* get the postal code */
334
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_POSTAL_CODE")))) {
335
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
336
										ds = data_string_init();
337
								}
338

    
339
								buffer_copy_string(ds->key, "GEOIP_CITY_POSTAL_CODE");
340
								buffer_copy_string(ds->value, gir->postal_code);
341
								array_insert_unique(con->environment, (data_unset *)ds);
342
						}
343

    
344
						/* get the latitude */
345
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_LATITUDE")))) {
346
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
347
										ds = data_string_init();
348
								}
349

    
350
								char latitude[32];
351
								sprintf(&latitude, "%f", gir->latitude);
352
								buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
353
								buffer_copy_string(ds->value, latitude);
354
								array_insert_unique(con->environment, (data_unset *)ds);
355
						}
356

    
357
						/* get the long latitude */
358
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_LONG_LATITUDE")))) {
359
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
360
										ds = data_string_init();
361
								}
362

    
363
								char long_latitude[32];
364
								sprintf(&long_latitude, "%f", gir->longitude);
365
								buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
366
								buffer_copy_string(ds->value, long_latitude);
367
								array_insert_unique(con->environment, (data_unset *)ds);
368
						}
369

    
370
						/* get the dma code */
371
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_DMA_CODE")))) {
372
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
373
										ds = data_string_init();
374
								}
375

    
376
								char dc[5];
377
								sprintf(&dc, "%i", gir->dma_code);
378
								buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
379
								buffer_copy_string(ds->value, dc);
380
								array_insert_unique(con->environment, (data_unset *)ds);
381
						}
382

    
383
						/* get the area code */
384
						if (NULL == (ds = (data_string *)array_get_element(con->environment, CONST_STR_LEN("GEOIP_CITY_AREA_CODE")))) {
385
								if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
386
										ds = data_string_init();
387
								}
388

    
389
								char ac[5];
390
								sprintf(&ac, "%i", gir->area_code);
391
								buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
392
								array_insert_unique(con->environment, (data_unset *)ds);
393
								buffer_copy_string(ds->value, ac);
394
						}
395
				}
396
		}
397

    
398
		/* keep walking... (johnnie walker style ;) */
399
		return HANDLER_GO_ON;
400
}
401
	
402
/* this function is called at dlopen() time and inits the callbacks */
403

    
404
int mod_geoip_plugin_init(plugin *p) {
405
	p->version      = LIGHTTPD_VERSION_ID;
406
	p->name         = buffer_init_string("geoip");
407
	
408
	p->init         = mod_geoip_init;
409
	p->handle_start_backend = mod_geoip_subrequest;
410
	p->set_defaults = mod_geoip_set_defaults;
411
	p->cleanup      = mod_geoip_free;
412
	
413
	p->data         = NULL;
414
	
415
	return 0;
416
}
(2-2/5)