comparison src/http/modules/ngx_http_geoip_module.c @ 626:a7a5fa2e395b NGINX_1_0_3

nginx 1.0.3 *) Feature: the "auth_basic_user_file" directive supports "$apr1", "{PLAIN}", and "{SSHA}" password encryption methods. Thanks to Maxim Dounin. *) Feature: the "geoip_org" directive and $geoip_org variable. Thanks to Alexander Uskov, Arnaud Granal, and Denis F. Latypoff. *) Feature: ngx_http_geo_module and ngx_http_geoip_module support IPv4 addresses mapped to IPv6 addresses. *) Bugfix: a segmentation fault occurred in a worker process during testing IPv4 address mapped to IPv6 address, if access or deny rules were defined only for IPv6; the bug had appeared in 0.8.22. *) Bugfix: a cached reponse may be broken if proxy/fastcgi/scgi/ uwsgi_cache_bypass and proxy/fastcgi/scgi/uwsgi_no_cache directive values were different; the bug had appeared in 0.8.46.
author Igor Sysoev <http://sysoev.ru>
date Wed, 25 May 2011 00:00:00 +0400
parents bb20316269e4
children d0f7a625f27c
comparison
equal deleted inserted replaced
625:30f948276abe 626:a7a5fa2e395b
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,
162 178
163 { ngx_null_string, NULL, NULL, 0, 0, 0 } 179 { ngx_null_string, NULL, NULL, 0, 0, 0 }
164 }; 180 };
165 181
166 182
183 static u_long
184 ngx_http_geoip_addr(ngx_http_request_t *r)
185 {
186 struct sockaddr_in *sin;
187 #if (NGX_HAVE_INET6)
188 u_char *p;
189 u_long addr;
190 struct sockaddr_in6 *sin6;
191 #endif
192
193 switch (r->connection->sockaddr->sa_family) {
194
195 case AF_INET:
196 sin = (struct sockaddr_in *) r->connection->sockaddr;
197 return ntohl(sin->sin_addr.s_addr);
198
199 #if (NGX_HAVE_INET6)
200
201 case AF_INET6:
202 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
203
204 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
205 p = sin6->sin6_addr.s6_addr;
206 addr = p[12] << 24;
207 addr += p[13] << 16;
208 addr += p[14] << 8;
209 addr += p[15];
210
211 return addr;
212 }
213
214 #endif
215 }
216
217 return INADDR_NONE;
218 }
219
220
167 static ngx_int_t 221 static ngx_int_t
168 ngx_http_geoip_country_variable(ngx_http_request_t *r, 222 ngx_http_geoip_country_variable(ngx_http_request_t *r,
169 ngx_http_variable_value_t *v, uintptr_t data) 223 ngx_http_variable_value_t *v, uintptr_t data)
170 { 224 {
171 ngx_http_geoip_variable_handler_pt handler = 225 ngx_http_geoip_variable_handler_pt handler =
172 (ngx_http_geoip_variable_handler_pt) data; 226 (ngx_http_geoip_variable_handler_pt) data;
173 227
174 u_long addr;
175 const char *val; 228 const char *val;
176 struct sockaddr_in *sin;
177 ngx_http_geoip_conf_t *gcf; 229 ngx_http_geoip_conf_t *gcf;
178 230
179 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); 231 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
180 232
181 if (gcf->country == NULL) { 233 if (gcf->country == NULL) {
182 goto not_found; 234 goto not_found;
183 } 235 }
184 236
185 if (r->connection->sockaddr->sa_family != AF_INET) { 237 val = handler(gcf->country, ngx_http_geoip_addr(r));
186 goto not_found;
187 }
188
189 sin = (struct sockaddr_in *) r->connection->sockaddr;
190 addr = ntohl(sin->sin_addr.s_addr);
191
192 val = handler(gcf->country, addr);
193 238
194 if (val == NULL) { 239 if (val == NULL) {
195 goto not_found; 240 goto not_found;
196 } 241 }
197 242
210 return NGX_OK; 255 return NGX_OK;
211 } 256 }
212 257
213 258
214 static ngx_int_t 259 static ngx_int_t
260 ngx_http_geoip_org_variable(ngx_http_request_t *r,
261 ngx_http_variable_value_t *v, uintptr_t data)
262 {
263 ngx_http_geoip_variable_handler_pt handler =
264 (ngx_http_geoip_variable_handler_pt) data;
265
266 const char *val;
267 ngx_http_geoip_conf_t *gcf;
268
269 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
270
271 if (gcf->org == NULL) {
272 goto not_found;
273 }
274
275 val = handler(gcf->org, ngx_http_geoip_addr(r));
276
277 if (val == NULL) {
278 goto not_found;
279 }
280
281 v->len = ngx_strlen(val);
282 v->valid = 1;
283 v->no_cacheable = 0;
284 v->not_found = 0;
285 v->data = (u_char *) val;
286
287 return NGX_OK;
288
289 not_found:
290
291 v->not_found = 1;
292
293 return NGX_OK;
294 }
295
296
297 static ngx_int_t
215 ngx_http_geoip_city_variable(ngx_http_request_t *r, 298 ngx_http_geoip_city_variable(ngx_http_request_t *r,
216 ngx_http_variable_value_t *v, uintptr_t data) 299 ngx_http_variable_value_t *v, uintptr_t data)
217 { 300 {
218 char *val; 301 char *val;
219 size_t len; 302 size_t len;
362 445
363 446
364 static GeoIPRecord * 447 static GeoIPRecord *
365 ngx_http_geoip_get_city_record(ngx_http_request_t *r) 448 ngx_http_geoip_get_city_record(ngx_http_request_t *r)
366 { 449 {
367 u_long addr;
368 struct sockaddr_in *sin;
369 ngx_http_geoip_conf_t *gcf; 450 ngx_http_geoip_conf_t *gcf;
370 451
371 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); 452 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
372 453
373 if (gcf->city && r->connection->sockaddr->sa_family == AF_INET) { 454 if (gcf->city) {
374 455 return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r));
375 sin = (struct sockaddr_in *) r->connection->sockaddr;
376 addr = ntohl(sin->sin_addr.s_addr);
377
378 return GeoIP_record_by_ipnum(gcf->city, addr);
379 } 456 }
380 457
381 return NULL; 458 return NULL;
382 } 459 }
383 460
439 516
440 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); 517 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
441 518
442 if (gcf->country == NULL) { 519 if (gcf->country == NULL) {
443 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 520 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
444 "GeoIO_open(\"%V\") failed", &value[1]); 521 "GeoIP_open(\"%V\") failed", &value[1]);
445 522
446 return NGX_CONF_ERROR; 523 return NGX_CONF_ERROR;
447 } 524 }
448 525
449 if (cf->args->nelts == 3) { 526 if (cf->args->nelts == 3) {
473 } 550 }
474 } 551 }
475 552
476 553
477 static char * 554 static char *
478 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 555 ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
479 { 556 {
480 ngx_http_geoip_conf_t *gcf = conf; 557 ngx_http_geoip_conf_t *gcf = conf;
481 558
482 ngx_str_t *value; 559 ngx_str_t *value;
483 560
484 if (gcf->city) { 561 if (gcf->org) {
485 return "is duplicate"; 562 return "is duplicate";
486 } 563 }
487 564
488 value = cf->args->elts; 565 value = cf->args->elts;
489 566
490 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); 567 gcf->org = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
491 568
492 if (gcf->city == NULL) { 569 if (gcf->org == NULL) {
493 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 570 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
494 "GeoIO_open(\"%V\") failed", &value[1]); 571 "GeoIP_open(\"%V\") failed", &value[1]);
495 572
496 return NGX_CONF_ERROR; 573 return NGX_CONF_ERROR;
497 } 574 }
498 575
499 if (cf->args->nelts == 3) { 576 if (cf->args->nelts == 3) {
500 if (ngx_strcmp(value[2].data, "utf8") == 0) { 577 if (ngx_strcmp(value[2].data, "utf8") == 0) {
501 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8); 578 GeoIP_set_charset (gcf->org, GEOIP_CHARSET_UTF8);
502 579
503 } else { 580 } else {
504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 581 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
505 "invalid parameter \"%V\"", &value[2]); 582 "invalid parameter \"%V\"", &value[2]);
506 return NGX_CONF_ERROR; 583 return NGX_CONF_ERROR;
507 } 584 }
508 } 585 }
509 586
587 switch (gcf->org->databaseType) {
588
589 case GEOIP_ISP_EDITION:
590 case GEOIP_ORG_EDITION:
591 case GEOIP_DOMAIN_EDITION:
592 case GEOIP_ASNUM_EDITION:
593
594 return NGX_CONF_OK;
595
596 default:
597 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
598 "invalid GeoIP database \"%V\" type:%d",
599 &value[1], gcf->org->databaseType);
600 return NGX_CONF_ERROR;
601 }
602 }
603
604
605 static char *
606 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
607 {
608 ngx_http_geoip_conf_t *gcf = conf;
609
610 ngx_str_t *value;
611
612 if (gcf->city) {
613 return "is duplicate";
614 }
615
616 value = cf->args->elts;
617
618 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
619
620 if (gcf->city == NULL) {
621 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
622 "GeoIP_open(\"%V\") failed", &value[1]);
623
624 return NGX_CONF_ERROR;
625 }
626
627 if (cf->args->nelts == 3) {
628 if (ngx_strcmp(value[2].data, "utf8") == 0) {
629 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8);
630
631 } else {
632 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
633 "invalid parameter \"%V\"", &value[2]);
634 return NGX_CONF_ERROR;
635 }
636 }
637
510 switch (gcf->city->databaseType) { 638 switch (gcf->city->databaseType) {
511 639
512 case GEOIP_CITY_EDITION_REV0: 640 case GEOIP_CITY_EDITION_REV0:
513 case GEOIP_CITY_EDITION_REV1: 641 case GEOIP_CITY_EDITION_REV1:
514 642
530 658
531 if (gcf->country) { 659 if (gcf->country) {
532 GeoIP_delete(gcf->country); 660 GeoIP_delete(gcf->country);
533 } 661 }
534 662
663 if (gcf->org) {
664 GeoIP_delete(gcf->org);
665 }
666
535 if (gcf->city) { 667 if (gcf->city) {
536 GeoIP_delete(gcf->city); 668 GeoIP_delete(gcf->city);
537 } 669 }
538 } 670 }