comparison src/http/modules/ngx_http_geoip_module.c @ 3915:e3645e338937

geoip_org patch by Alexander Uskov
author Igor Sysoev <igor@sysoev.ru>
date Wed, 11 May 2011 09:34:21 +0000
parents 2b92090d8a43
children 0dceaa117e0d
comparison
equal deleted inserted replaced
3914:2b92090d8a43 3915:e3645e338937
12 #include <GeoIPCity.h> 12 #include <GeoIPCity.h>
13 13
14 14
15 typedef struct { 15 typedef struct {
16 GeoIP *country; 16 GeoIP *country;
17 GeoIP *org;
17 GeoIP *city; 18 GeoIP *city;
18 } ngx_http_geoip_conf_t; 19 } ngx_http_geoip_conf_t;
19 20
20 21
21 typedef struct { 22 typedef struct {
25 26
26 27
27 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr); 28 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr);
28 29
29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, 30 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r,
31 ngx_http_variable_value_t *v, uintptr_t data);
32 static ngx_int_t ngx_http_geoip_org_variable(ngx_http_request_t *r,
30 ngx_http_variable_value_t *v, uintptr_t data); 33 ngx_http_variable_value_t *v, uintptr_t data);
31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r, 34 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r,
32 ngx_http_variable_value_t *v, uintptr_t data); 35 ngx_http_variable_value_t *v, uintptr_t data);
33 static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r, 36 static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r,
34 ngx_http_variable_value_t *v, uintptr_t data); 37 ngx_http_variable_value_t *v, uintptr_t data);
40 43
41 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf); 44 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf);
42 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf); 45 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf);
43 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, 46 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
44 void *conf); 47 void *conf);
48 static char *ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd,
49 void *conf);
45 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, 50 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
46 void *conf); 51 void *conf);
47 static void ngx_http_geoip_cleanup(void *data); 52 static void ngx_http_geoip_cleanup(void *data);
48 53
49 54
50 static ngx_command_t ngx_http_geoip_commands[] = { 55 static ngx_command_t ngx_http_geoip_commands[] = {
51 56
52 { ngx_string("geoip_country"), 57 { ngx_string("geoip_country"),
53 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, 58 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
54 ngx_http_geoip_country, 59 ngx_http_geoip_country,
60 NGX_HTTP_MAIN_CONF_OFFSET,
61 0,
62 NULL },
63
64 { ngx_string("geoip_org"),
65 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
66 ngx_http_geoip_org,
55 NGX_HTTP_MAIN_CONF_OFFSET, 67 NGX_HTTP_MAIN_CONF_OFFSET,
56 0, 68 0,
57 NULL }, 69 NULL },
58 70
59 { ngx_string("geoip_city"), 71 { ngx_string("geoip_city"),
110 122
111 { ngx_string("geoip_country_name"), NULL, 123 { ngx_string("geoip_country_name"), NULL,
112 ngx_http_geoip_country_variable, 124 ngx_http_geoip_country_variable,
113 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 }, 125 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 },
114 126
127 { ngx_string("geoip_org"), NULL,
128 ngx_http_geoip_org_variable,
129 (uintptr_t) GeoIP_name_by_ipnum, 0, 0 },
130
115 { ngx_string("geoip_city_continent_code"), NULL, 131 { ngx_string("geoip_city_continent_code"), NULL,
116 ngx_http_geoip_city_variable, 132 ngx_http_geoip_city_variable,
117 offsetof(GeoIPRecord, continent_code), 0, 0 }, 133 offsetof(GeoIPRecord, continent_code), 0, 0 },
118 134
119 { ngx_string("geoip_city_country_code"), NULL, 135 { ngx_string("geoip_city_country_code"), NULL,
210 return NGX_OK; 226 return NGX_OK;
211 } 227 }
212 228
213 229
214 static ngx_int_t 230 static ngx_int_t
231 ngx_http_geoip_org_variable(ngx_http_request_t *r,
232 ngx_http_variable_value_t *v, uintptr_t data)
233 {
234 ngx_http_geoip_variable_handler_pt handler =
235 (ngx_http_geoip_variable_handler_pt) data;
236
237 u_long addr;
238 const char *val;
239 struct sockaddr_in *sin;
240 ngx_http_geoip_conf_t *gcf;
241
242 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
243
244 if (gcf->org == NULL) {
245 goto not_found;
246 }
247
248 if (r->connection->sockaddr->sa_family != AF_INET) {
249 goto not_found;
250 }
251
252 sin = (struct sockaddr_in *) r->connection->sockaddr;
253 addr = ntohl(sin->sin_addr.s_addr);
254
255 val = handler(gcf->org, addr);
256
257 if (val == NULL) {
258 goto not_found;
259 }
260
261 v->len = ngx_strlen(val);
262 v->valid = 1;
263 v->no_cacheable = 0;
264 v->not_found = 0;
265 v->data = (u_char *) val;
266
267 return NGX_OK;
268
269 not_found:
270
271 v->not_found = 1;
272
273 return NGX_OK;
274 }
275
276
277 static ngx_int_t
215 ngx_http_geoip_city_variable(ngx_http_request_t *r, 278 ngx_http_geoip_city_variable(ngx_http_request_t *r,
216 ngx_http_variable_value_t *v, uintptr_t data) 279 ngx_http_variable_value_t *v, uintptr_t data)
217 { 280 {
218 char *val; 281 char *val;
219 size_t len; 282 size_t len;
473 } 536 }
474 } 537 }
475 538
476 539
477 static char * 540 static char *
478 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 541 ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
479 { 542 {
480 ngx_http_geoip_conf_t *gcf = conf; 543 ngx_http_geoip_conf_t *gcf = conf;
481 544
482 ngx_str_t *value; 545 ngx_str_t *value;
483 546
484 if (gcf->city) { 547 if (gcf->org) {
485 return "is duplicate"; 548 return "is duplicate";
486 } 549 }
487 550
488 value = cf->args->elts; 551 value = cf->args->elts;
489 552
490 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); 553 gcf->org = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
491 554
492 if (gcf->city == NULL) { 555 if (gcf->org == NULL) {
493 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 556 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
494 "GeoIP_open(\"%V\") failed", &value[1]); 557 "GeoIP_open(\"%V\") failed", &value[1]);
495 558
496 return NGX_CONF_ERROR; 559 return NGX_CONF_ERROR;
497 } 560 }
498 561
499 if (cf->args->nelts == 3) { 562 if (cf->args->nelts == 3) {
500 if (ngx_strcmp(value[2].data, "utf8") == 0) { 563 if (ngx_strcmp(value[2].data, "utf8") == 0) {
501 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8); 564 GeoIP_set_charset (gcf->org, GEOIP_CHARSET_UTF8);
502 565
503 } else { 566 } else {
504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 567 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
505 "invalid parameter \"%V\"", &value[2]); 568 "invalid parameter \"%V\"", &value[2]);
506 return NGX_CONF_ERROR; 569 return NGX_CONF_ERROR;
507 } 570 }
508 } 571 }
509 572
573 switch (gcf->org->databaseType) {
574
575 case GEOIP_ISP_EDITION:
576 case GEOIP_ORG_EDITION:
577 case GEOIP_DOMAIN_EDITION:
578 case GEOIP_ASNUM_EDITION:
579
580 return NGX_CONF_OK;
581
582 default:
583 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
584 "invalid GeoIP database \"%V\" type:%d",
585 &value[1], gcf->org->databaseType);
586 return NGX_CONF_ERROR;
587 }
588 }
589
590
591 static char *
592 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
593 {
594 ngx_http_geoip_conf_t *gcf = conf;
595
596 ngx_str_t *value;
597
598 if (gcf->city) {
599 return "is duplicate";
600 }
601
602 value = cf->args->elts;
603
604 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
605
606 if (gcf->city == NULL) {
607 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
608 "GeoIP_open(\"%V\") failed", &value[1]);
609
610 return NGX_CONF_ERROR;
611 }
612
613 if (cf->args->nelts == 3) {
614 if (ngx_strcmp(value[2].data, "utf8") == 0) {
615 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8);
616
617 } else {
618 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
619 "invalid parameter \"%V\"", &value[2]);
620 return NGX_CONF_ERROR;
621 }
622 }
623
510 switch (gcf->city->databaseType) { 624 switch (gcf->city->databaseType) {
511 625
512 case GEOIP_CITY_EDITION_REV0: 626 case GEOIP_CITY_EDITION_REV0:
513 case GEOIP_CITY_EDITION_REV1: 627 case GEOIP_CITY_EDITION_REV1:
514 628
530 644
531 if (gcf->country) { 645 if (gcf->country) {
532 GeoIP_delete(gcf->country); 646 GeoIP_delete(gcf->country);
533 } 647 }
534 648
649 if (gcf->org) {
650 GeoIP_delete(gcf->org);
651 }
652
535 if (gcf->city) { 653 if (gcf->city) {
536 GeoIP_delete(gcf->city); 654 GeoIP_delete(gcf->city);
537 } 655 }
538 } 656 }