[autobuild] allow sendfile() in cross-compile (fixes #2836)
[lighttpd.git] / src / mod_geoip.c
blob694f2de2520495e46e731e3c7591c9ddd986e57c
1 #include "first.h"
3 #include <GeoIP.h>
4 #include <GeoIPCity.h>
6 #include "base.h"
7 #include "log.h"
8 #include "buffer.h"
9 #include "plugin.h"
11 #include <stdlib.h>
12 #include <string.h>
14 /**
16 * $mod_geoip.c (v2.0) (13.09.2006 00:29:11)
18 * Name:
19 * mod_geoip.c
21 * Description:
22 * GeoIP module (plugin) for lighttpd.
23 * the module loads a geoip database of type "country" or "city" and
24 * sets new ENV vars based on ip record lookups.
26 * country db env's:
27 * GEOIP_COUNTRY_CODE
28 * GEOIP_COUNTRY_CODE3
29 * GEOIP_COUNTRY_NAME
31 * city db env's:
32 * GEOIP_COUNTRY_CODE
33 * GEOIP_COUNTRY_CODE3
34 * GEOIP_COUNTRY_NAME
35 * GEOIP_CITY_NAME
36 * GEOIP_CITY_POSTAL_CODE
37 * GEOIP_CITY_LATITUDE
38 * GEOIP_CITY_LONG_LATITUDE
39 * GEOIP_CITY_DMA_CODE
40 * GEOIP_CITY_AREA_CODE
42 * Usage (configuration options):
43 * geoip.db-filename = <path to the geoip or geocity database>
44 * geoip.memory-cache = <enable|disable> : default disabled
45 * if enabled, mod_geoip will load the database binary file to
46 * memory for very fast lookups. the only penalty is memory usage.
48 * Author:
49 * Ami E. Bizamcher (amix)
50 * duke.amix@gmail.com
52 * Note:
53 * GeoIP Library and API must be installed!
57 /* plugin config for all request/connections */
59 typedef struct {
60 unsigned short mem_cache;
61 buffer *db_name;
62 GeoIP *gi;
63 } plugin_config;
65 typedef struct {
66 PLUGIN_DATA;
68 plugin_config **config_storage;
70 plugin_config conf;
71 } plugin_data;
73 /* init the plugin data */
74 INIT_FUNC(mod_geoip_init) {
75 plugin_data *p;
77 p = calloc(1, sizeof(*p));
79 return p;
82 /* destroy the plugin data */
83 FREE_FUNC(mod_geoip_free) {
84 plugin_data *p = p_d;
86 UNUSED(srv);
88 if (!p) return HANDLER_GO_ON;
90 if (p->config_storage) {
91 size_t i;
93 for (i = 0; i < srv->config_context->used; i++) {
94 plugin_config *s = p->config_storage[i];
96 if (!s) continue;
98 buffer_free(s->db_name);
100 /* clean up */
101 if (s->gi) GeoIP_delete(s->gi);
103 free(s);
105 free(p->config_storage);
108 free(p);
110 return HANDLER_GO_ON;
113 /* handle plugin config and check values */
115 SETDEFAULTS_FUNC(mod_geoip_set_defaults) {
116 plugin_data *p = p_d;
117 size_t i = 0;
119 config_values_t cv[] = {
120 { "geoip.db-filename", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
121 { "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
122 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
125 if (!p) return HANDLER_ERROR;
127 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
129 for (i = 0; i < srv->config_context->used; i++) {
130 data_config const* config = (data_config const*)srv->config_context->data[i];
131 plugin_config *s;
132 int mode;
134 s = calloc(1, sizeof(plugin_config));
136 s->db_name = buffer_init();
137 s->mem_cache = 0; /* default: do not load db to cache */
138 s->gi = NULL;
140 cv[0].destination = s->db_name;
141 cv[1].destination = &(s->mem_cache);
143 p->config_storage[i] = s;
145 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
146 return HANDLER_ERROR;
149 mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
151 /* country db filename is requeried! */
152 if (!buffer_is_empty(s->db_name)) {
154 /* let's start cooking */
155 if (s->mem_cache != 0)
156 mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
158 if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) {
159 log_error_write(srv, __FILE__, __LINE__, "s",
160 "failed to open GeoIP database!!!");
162 return HANDLER_ERROR;
165 /* is the db supported ? */
166 if (s->gi->databaseType != GEOIP_COUNTRY_EDITION &&
167 s->gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
168 s->gi->databaseType != GEOIP_CITY_EDITION_REV1) {
169 log_error_write(srv, __FILE__, __LINE__, "s",
170 "GeoIP database is of unsupported type!!!");
175 return HANDLER_GO_ON;
178 #define PATCH(x) \
179 p->conf.x = s->x;
180 static int mod_geoip_patch_connection(server *srv, connection *con, plugin_data *p) {
181 size_t i, j;
182 plugin_config *s = p->config_storage[0];
184 PATCH(db_name);
185 PATCH(mem_cache);
186 PATCH(gi);
188 /* skip the first, the global context */
189 for (i = 1; i < srv->config_context->used; i++) {
190 data_config *dc = (data_config *)srv->config_context->data[i];
191 s = p->config_storage[i];
193 /* condition didn't match */
194 if (!config_check_cond(srv, con, dc)) continue;
196 /* merge config */
197 for (j = 0; j < dc->value->used; j++) {
198 data_unset *du = dc->value->data[j];
200 if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.db-filename"))) {
201 PATCH(db_name);
204 if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) {
205 PATCH(mem_cache);
210 return 0;
212 #undef PATCH
214 static handler_t mod_geoip_query (connection *con, plugin_data *p) {
215 const char *remote_ip;
216 data_string *ds;
217 GeoIPRecord *gir;
218 const char *returnedCountry;
220 remote_ip = con->dst_addr_buf->ptr;
222 if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
223 /* get the country code 2 chars */
224 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
225 if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) {
226 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
227 ds = data_string_init();
230 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
231 buffer_copy_string(ds->value, returnedCountry);
232 array_insert_unique(con->environment, (data_unset *)ds);
236 /* get the country code 3 chars */
237 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
238 if (NULL != (returnedCountry = GeoIP_country_code3_by_addr(p->conf.gi, remote_ip))) {
239 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
240 ds = data_string_init();
243 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
244 buffer_copy_string(ds->value, returnedCountry);
245 array_insert_unique(con->environment, (data_unset *)ds);
249 /* get the country name */
250 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
251 if (NULL != (returnedCountry = GeoIP_country_name_by_addr(p->conf.gi, remote_ip))) {
252 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
253 ds = data_string_init();
256 buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
257 buffer_copy_string(ds->value, returnedCountry);
258 array_insert_unique(con->environment, (data_unset *)ds);
262 /* go on... */
263 return HANDLER_GO_ON;
266 /* if we are here, geo city is in use */
268 if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
269 /* get the country code 2 chars */
270 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
271 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
272 ds = data_string_init();
275 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
276 buffer_copy_string(ds->value, gir->country_code);
277 array_insert_unique(con->environment, (data_unset *)ds);
280 /* get the country code 3 chars */
281 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
282 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
283 ds = data_string_init();
286 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
287 buffer_copy_string(ds->value, gir->country_code3);
288 array_insert_unique(con->environment, (data_unset *)ds);
291 /* get the country name */
292 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
293 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
294 ds = data_string_init();
297 buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
298 buffer_copy_string(ds->value, gir->country_name);
299 array_insert_unique(con->environment, (data_unset *)ds);
302 /* get the city region */
303 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_REGION"))) {
304 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
305 ds = data_string_init();
308 buffer_copy_string(ds->key, "GEOIP_CITY_REGION");
309 buffer_copy_string(ds->value, gir->region);
310 array_insert_unique(con->environment, (data_unset *)ds);
313 /* get the city */
314 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_NAME"))) {
315 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
316 ds = data_string_init();
319 buffer_copy_string(ds->key, "GEOIP_CITY_NAME");
320 buffer_copy_string(ds->value, gir->city);
321 array_insert_unique(con->environment, (data_unset *)ds);
324 /* get the postal code */
325 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_POSTAL_CODE"))) {
326 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
327 ds = data_string_init();
330 buffer_copy_string(ds->key, "GEOIP_CITY_POSTAL_CODE");
331 buffer_copy_string(ds->value, gir->postal_code);
332 array_insert_unique(con->environment, (data_unset *)ds);
335 /* get the latitude */
336 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LATITUDE"))) {
337 char latitude[32];
338 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
339 ds = data_string_init();
342 snprintf(latitude, sizeof(latitude), "%f", gir->latitude);
343 buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
344 buffer_copy_string(ds->value, latitude);
345 array_insert_unique(con->environment, (data_unset *)ds);
348 /* get the long latitude */
349 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LONG_LATITUDE"))) {
350 char long_latitude[32];
351 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
352 ds = data_string_init();
355 snprintf(long_latitude, sizeof(long_latitude), "%f", gir->longitude);
356 buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
357 buffer_copy_string(ds->value, long_latitude);
358 array_insert_unique(con->environment, (data_unset *)ds);
361 /* get the dma code */
362 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_DMA_CODE"))) {
363 char dc[5];
364 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
365 ds = data_string_init();
368 snprintf(dc, sizeof(dc), "%i", gir->dma_code);
369 buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
370 buffer_copy_string(ds->value, dc);
371 array_insert_unique(con->environment, (data_unset *)ds);
374 /* get the area code */
375 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_AREA_CODE"))) {
376 char ac[5];
377 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
378 ds = data_string_init();
381 snprintf(ac, sizeof(ac), "%i", gir->area_code);
382 buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
383 buffer_copy_string(ds->value, ac);
384 array_insert_unique(con->environment, (data_unset *)ds);
387 GeoIPRecord_delete(gir);
390 return HANDLER_GO_ON;
393 CONNECTION_FUNC(mod_geoip_handle_request_env) {
394 plugin_data *p = p_d;
395 mod_geoip_patch_connection(srv, con, p);
396 if (buffer_is_empty(p->conf.db_name)) return HANDLER_GO_ON;
398 return mod_geoip_query(con, p);
401 /* this function is called at dlopen() time and inits the callbacks */
403 int mod_geoip_plugin_init(plugin *p);
404 int mod_geoip_plugin_init(plugin *p) {
405 p->version = LIGHTTPD_VERSION_ID;
406 p->name = buffer_init_string("geoip");
408 p->init = mod_geoip_init;
409 p->handle_request_env = mod_geoip_handle_request_env;
410 p->set_defaults = mod_geoip_set_defaults;
411 p->cleanup = mod_geoip_free;
413 p->data = NULL;
415 return 0;