16 * $mod_geoip.c (v2.0) (13.09.2006 00:29:11)
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.
36 * GEOIP_CITY_POSTAL_CODE
38 * GEOIP_CITY_LONG_LATITUDE
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.
49 * Ami E. Bizamcher (amix)
53 * GeoIP Library and API must be installed!
57 /* plugin config for all request/connections */
60 unsigned short mem_cache
;
68 plugin_config
**config_storage
;
73 /* init the plugin data */
74 INIT_FUNC(mod_geoip_init
) {
77 p
= calloc(1, sizeof(*p
));
82 /* destroy the plugin data */
83 FREE_FUNC(mod_geoip_free
) {
88 if (!p
) return HANDLER_GO_ON
;
90 if (p
->config_storage
) {
93 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
94 plugin_config
*s
= p
->config_storage
[i
];
98 buffer_free(s
->db_name
);
101 if (s
->gi
) GeoIP_delete(s
->gi
);
105 free(p
->config_storage
);
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
;
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
];
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 */
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
;
180 static int mod_geoip_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
182 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
204 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("geoip.memory-cache"))) {
214 URIHANDLER_FUNC(mod_geoip_subrequest
) {
215 plugin_data
*p
= p_d
;
217 mod_geoip_patch_connection(srv
, con
, p
);
219 if (!buffer_is_empty(p
->conf
.db_name
)) {
220 const char *remote_ip
;
223 const char *returnedCountry
;
225 remote_ip
= con
->dst_addr_buf
->ptr
;
227 if (p
->conf
.gi
->databaseType
== GEOIP_COUNTRY_EDITION
) {
228 /* get the country code 2 chars */
229 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_CODE"))) {
230 if (NULL
!= (returnedCountry
= GeoIP_country_code_by_addr(p
->conf
.gi
, remote_ip
))) {
231 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
232 ds
= data_string_init();
235 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_CODE");
236 buffer_copy_string(ds
->value
, returnedCountry
);
237 array_insert_unique(con
->environment
, (data_unset
*)ds
);
241 /* get the country code 3 chars */
242 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_CODE3"))) {
243 if (NULL
!= (returnedCountry
= GeoIP_country_code3_by_addr(p
->conf
.gi
, remote_ip
))) {
244 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
245 ds
= data_string_init();
248 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_CODE3");
249 buffer_copy_string(ds
->value
, returnedCountry
);
250 array_insert_unique(con
->environment
, (data_unset
*)ds
);
254 /* get the country name */
255 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_NAME"))) {
256 if (NULL
!= (returnedCountry
= GeoIP_country_name_by_addr(p
->conf
.gi
, remote_ip
))) {
257 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
258 ds
= data_string_init();
261 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_NAME");
262 buffer_copy_string(ds
->value
, returnedCountry
);
263 array_insert_unique(con
->environment
, (data_unset
*)ds
);
268 return HANDLER_GO_ON
;
271 /* if we are here, geo city is in use */
273 if (NULL
!= (gir
= GeoIP_record_by_addr(p
->conf
.gi
, remote_ip
))) {
274 /* get the country code 2 chars */
275 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_CODE"))) {
276 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
277 ds
= data_string_init();
280 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_CODE");
281 buffer_copy_string(ds
->value
, gir
->country_code
);
282 array_insert_unique(con
->environment
, (data_unset
*)ds
);
285 /* get the country code 3 chars */
286 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_CODE3"))) {
287 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
288 ds
= data_string_init();
291 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_CODE3");
292 buffer_copy_string(ds
->value
, gir
->country_code3
);
293 array_insert_unique(con
->environment
, (data_unset
*)ds
);
296 /* get the country name */
297 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_COUNTRY_NAME"))) {
298 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
299 ds
= data_string_init();
302 buffer_copy_string(ds
->key
, "GEOIP_COUNTRY_NAME");
303 buffer_copy_string(ds
->value
, gir
->country_name
);
304 array_insert_unique(con
->environment
, (data_unset
*)ds
);
307 /* get the city region */
308 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_REGION"))) {
309 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
310 ds
= data_string_init();
313 buffer_copy_string(ds
->key
, "GEOIP_CITY_REGION");
314 buffer_copy_string(ds
->value
, gir
->region
);
315 array_insert_unique(con
->environment
, (data_unset
*)ds
);
319 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_NAME"))) {
320 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
321 ds
= data_string_init();
324 buffer_copy_string(ds
->key
, "GEOIP_CITY_NAME");
325 buffer_copy_string(ds
->value
, gir
->city
);
326 array_insert_unique(con
->environment
, (data_unset
*)ds
);
329 /* get the postal code */
330 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_POSTAL_CODE"))) {
331 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
332 ds
= data_string_init();
335 buffer_copy_string(ds
->key
, "GEOIP_CITY_POSTAL_CODE");
336 buffer_copy_string(ds
->value
, gir
->postal_code
);
337 array_insert_unique(con
->environment
, (data_unset
*)ds
);
340 /* get the latitude */
341 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_LATITUDE"))) {
343 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
344 ds
= data_string_init();
347 snprintf(latitude
, sizeof(latitude
), "%f", gir
->latitude
);
348 buffer_copy_string(ds
->key
, "GEOIP_CITY_LATITUDE");
349 buffer_copy_string(ds
->value
, latitude
);
350 array_insert_unique(con
->environment
, (data_unset
*)ds
);
353 /* get the long latitude */
354 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_LONG_LATITUDE"))) {
355 char long_latitude
[32];
356 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
357 ds
= data_string_init();
360 snprintf(long_latitude
, sizeof(long_latitude
), "%f", gir
->longitude
);
361 buffer_copy_string(ds
->key
, "GEOIP_CITY_LONG_LATITUDE");
362 buffer_copy_string(ds
->value
, long_latitude
);
363 array_insert_unique(con
->environment
, (data_unset
*)ds
);
366 /* get the dma code */
367 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_DMA_CODE"))) {
369 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
370 ds
= data_string_init();
373 snprintf(dc
, sizeof(dc
), "%i", gir
->dma_code
);
374 buffer_copy_string(ds
->key
, "GEOIP_CITY_DMA_CODE");
375 buffer_copy_string(ds
->value
, dc
);
376 array_insert_unique(con
->environment
, (data_unset
*)ds
);
379 /* get the area code */
380 if (NULL
== (ds
= (data_string
*)array_get_element(con
->environment
, "GEOIP_CITY_AREA_CODE"))) {
382 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
383 ds
= data_string_init();
386 snprintf(ac
, sizeof(ac
), "%i", gir
->area_code
);
387 buffer_copy_string(ds
->key
, "GEOIP_CITY_AREA_CODE");
388 buffer_copy_string(ds
->value
, ac
);
389 array_insert_unique(con
->environment
, (data_unset
*)ds
);
392 GeoIPRecord_delete(gir
);
396 /* keep walking... (johnnie walker style ;) */
397 return HANDLER_GO_ON
;
400 /* this function is called at dlopen() time and inits the callbacks */
402 int mod_geoip_plugin_init(plugin
*p
);
403 int mod_geoip_plugin_init(plugin
*p
) {
404 p
->version
= LIGHTTPD_VERSION_ID
;
405 p
->name
= buffer_init_string("geoip");
407 p
->init
= mod_geoip_init
;
408 p
->handle_subrequest_start
= mod_geoip_subrequest
;
409 p
->set_defaults
= mod_geoip_set_defaults
;
410 p
->cleanup
= mod_geoip_free
;