Ticket #250: nginx-geoipv6.patch
| File nginx-geoipv6.patch, 9.5 KB (added by , 14 years ago) |
|---|
-
auto/lib/geoip/conf
diff -ru nginx-1.3.8/auto/lib/geoip/conf nginx-1.3.8-patched/auto/lib/geoip/conf
old new 6 6 ngx_feature="GeoIP library" 7 7 ngx_feature_name= 8 8 ngx_feature_run=no 9 ngx_feature_incs= 9 ngx_feature_incs="#include <GeoIP.h>" 10 10 ngx_feature_path= 11 11 ngx_feature_libs="-lGeoIP" 12 12 ngx_feature_test="GeoIP_open(NULL, 0)" … … 18 18 # FreeBSD port 19 19 20 20 ngx_feature="GeoIP library in /usr/local/" 21 ngx_feature_path="/usr/local/include" 21 22 22 23 if [ $NGX_RPATH = YES ]; then 23 24 ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lGeoIP" … … 34 35 # NetBSD port 35 36 36 37 ngx_feature="GeoIP library in /usr/pkg/" 37 ngx_feature_path="/usr/pkg/include /"38 ngx_feature_path="/usr/pkg/include" 38 39 39 40 if [ $NGX_RPATH = YES ]; then 40 41 ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lGeoIP" … … 64 65 65 66 66 67 if [ $ngx_found = yes ]; then 68 69 CORE_INCS="$CORE_INCS $ngx_feature_path" 67 70 CORE_LIBS="$CORE_LIBS $ngx_feature_libs" 68 71 72 if [ $NGX_IPV6 = YES ]; then 73 ngx_feature="GeoIP IPV6 support" 74 ngx_feature_name="NGX_HAVE_GEOIP_IPV6" 75 ngx_feature_run=no 76 ngx_feature_incs="#include <stdio.h> 77 #include <GeoIP.h>" 78 #ngx_feature_path= 79 #ngx_feature_libs= 80 ngx_feature_test="printf(\"%d\", GEOIP_CITY_EDITION_REV0_V6);" 81 . auto/feature 82 fi 83 69 84 else 70 85 71 86 cat << END -
auto/lib/libgd/conf
diff -ru nginx-1.3.8/auto/lib/libgd/conf nginx-1.3.8-patched/auto/lib/libgd/conf
old new 35 35 # NetBSD port 36 36 37 37 ngx_feature="GD library in /usr/pkg/" 38 ngx_feature_path="/usr/pkg/include /"38 ngx_feature_path="/usr/pkg/include" 39 39 40 40 if [ $NGX_RPATH = YES ]; then 41 41 ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lgd" -
src/http/modules/ngx_http_geoip_module.c
diff -ru nginx-1.3.8/src/http/modules/ngx_http_geoip_module.c nginx-1.3.8-patched/src/http/modules/ngx_http_geoip_module.c
old new 13 13 #include <GeoIPCity.h> 14 14 15 15 16 #define NGX_GEOIP_COUNTRY_CODE 0 17 #define NGX_GEOIP_COUNTRY_CODE3 1 18 #define NGX_GEOIP_COUNTRY_NAME 2 19 20 16 21 typedef struct { 17 22 GeoIP *country; 18 23 GeoIP *org; 19 24 GeoIP *city; 20 25 ngx_array_t *proxies; /* array of ngx_cidr_t */ 21 26 ngx_flag_t proxy_recursive; 27 unsigned country_ipv6:1; 28 unsigned org_ipv6:1; 29 unsigned city_ipv6:1; 22 30 } ngx_http_geoip_conf_t; 23 31 24 32 … … 28 36 } ngx_http_geoip_var_t; 29 37 30 38 31 typedef char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr); 39 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr); 40 41 42 ngx_http_geoip_variable_handler_pt ngx_http_geoip_country_functions[] = { 43 GeoIP_country_code_by_ipnum, 44 GeoIP_country_code3_by_ipnum, 45 GeoIP_country_name_by_ipnum, 46 }; 47 48 49 #if (NGX_HAVE_GEOIP_IPV6) 50 typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *, geoipv6_t addr); 51 52 53 ngx_http_geoip_variable_handler_v6_pt ngx_http_geoip_country_ipv6_functions[] = { 54 GeoIP_country_code_by_ipnum_v6, 55 GeoIP_country_code3_by_ipnum_v6, 56 GeoIP_country_name_by_ipnum_v6, 57 }; 58 #endif 59 32 60 33 61 static u_long ngx_http_geoip_addr(ngx_http_request_t *r, 34 62 ngx_http_geoip_conf_t *gcf); … … 138 166 139 167 { ngx_string("geoip_country_code"), NULL, 140 168 ngx_http_geoip_country_variable, 141 (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 },169 NGX_GEOIP_COUNTRY_CODE, 0, 0 }, 142 170 143 171 { ngx_string("geoip_country_code3"), NULL, 144 172 ngx_http_geoip_country_variable, 145 (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 },173 NGX_GEOIP_COUNTRY_CODE3, 0, 0 }, 146 174 147 175 { ngx_string("geoip_country_name"), NULL, 148 176 ngx_http_geoip_country_variable, 149 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 },177 NGX_GEOIP_COUNTRY_NAME, 0, 0 }, 150 178 151 179 { ngx_string("geoip_org"), NULL, 152 180 ngx_http_geoip_org_variable, 153 (uintptr_t) GeoIP_name_by_ipnum, 0, 0 },181 0, 0, 0 }, 154 182 155 183 { ngx_string("geoip_city_continent_code"), NULL, 156 184 ngx_http_geoip_city_variable, … … 255 283 } 256 284 257 285 286 #if (NGX_HAVE_GEOIP_IPV6) 287 static geoipv6_t 288 ngx_http_geoip_addr_v6(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf) 289 { 290 ngx_addr_t addr; 291 ngx_table_elt_t *xfwd; 292 struct sockaddr_in6 *sin6; 293 struct sockaddr_in *sin; 294 struct in6_addr addr6; 295 u_long addr4; 296 297 addr.sockaddr = r->connection->sockaddr; 298 addr.socklen = r->connection->socklen; 299 /* addr.name = r->connection->addr_text; */ 300 301 xfwd = r->headers_in.x_forwarded_for; 302 303 if (xfwd != NULL && gcf->proxies != NULL) { 304 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data, 305 xfwd->value.len, gcf->proxies, 306 gcf->proxy_recursive); 307 } 308 309 switch (addr.sockaddr->sa_family) { 310 311 case AF_INET: 312 /* Produce IPv4 mapped IPv6 address */ 313 sin = (struct sockaddr_in *) addr.sockaddr; 314 addr4 = ntohl(sin->sin_addr.s_addr); 315 316 ngx_memzero(&addr6, sizeof(struct in6_addr)); 317 addr6.s6_addr[10] = 0xFF; 318 addr6.s6_addr[11] = 0xFF; 319 addr6.s6_addr[12] = addr4 >> 24; 320 addr6.s6_addr[13] = addr4 >> 16; 321 addr6.s6_addr[14] = addr4 >> 8; 322 addr6.s6_addr[15] = addr4; 323 return addr6; 324 325 case AF_INET6: 326 sin6 = (struct sockaddr_in6 *) addr.sockaddr; 327 return sin6->sin6_addr; 328 329 } 330 331 return in6addr_any; 332 } 333 #endif 334 335 258 336 static ngx_int_t 259 337 ngx_http_geoip_country_variable(ngx_http_request_t *r, 260 338 ngx_http_variable_value_t *v, uintptr_t data) 261 339 { 262 ngx_http_geoip_variable_handler_pt handler = 263 (ngx_http_geoip_variable_handler_pt) data; 340 #if (NGX_HAVE_GEOIP_IPV6) 341 ngx_http_geoip_variable_handler_v6_pt handler_v6 = 342 ngx_http_geoip_country_ipv6_functions[data]; 343 #endif 344 ngx_http_geoip_variable_handler_pt handler = 345 ngx_http_geoip_country_functions[data]; 264 346 265 const char *val;266 ngx_http_geoip_conf_t *gcf;347 const char *val; 348 ngx_http_geoip_conf_t *gcf; 267 349 268 350 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); 269 351 … … 271 353 goto not_found; 272 354 } 273 355 356 #if (NGX_HAVE_GEOIP_IPV6) 357 val = gcf->country_ipv6 358 ? handler_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf)) 359 : handler(gcf->country, ngx_http_geoip_addr(r, gcf)); 360 #else 274 361 val = handler(gcf->country, ngx_http_geoip_addr(r, gcf)); 362 #endif 275 363 276 364 if (val == NULL) { 277 365 goto not_found; … … 297 385 ngx_http_geoip_org_variable(ngx_http_request_t *r, 298 386 ngx_http_variable_value_t *v, uintptr_t data) 299 387 { 300 ngx_http_geoip_variable_handler_pt handler =301 (ngx_http_geoip_variable_handler_pt) data;302 303 388 size_t len; 304 389 char *val; 305 390 ngx_http_geoip_conf_t *gcf; … … 310 395 goto not_found; 311 396 } 312 397 313 val = handler(gcf->org, ngx_http_geoip_addr(r, gcf)); 398 #if (NGX_HAVE_GEOIP_IPV6) 399 val = gcf->country_ipv6 400 ? GeoIP_name_by_ipnum_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf)) 401 : GeoIP_name_by_ipnum(gcf->country, ngx_http_geoip_addr(r, gcf)); 402 #else 403 val = GeoIP_name_by_ipnum(gcf->org, ngx_http_geoip_addr(r, gcf)); 404 #endif 314 405 315 406 if (val == NULL) { 316 407 goto not_found; … … 500 591 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); 501 592 502 593 if (gcf->city) { 594 #if (NGX_HAVE_GEOIP_IPV6) 595 return gcf->city_ipv6 596 ? GeoIP_record_by_ipnum_v6(gcf->city, ngx_http_geoip_addr_v6(r, gcf)) 597 : GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); 598 #else 503 599 return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); 600 #endif 504 601 } 505 602 506 603 return NULL; … … 601 698 case GEOIP_PROXY_EDITION: 602 699 case GEOIP_NETSPEED_EDITION: 603 700 701 gcf->country_ipv6 = 0; 604 702 return NGX_CONF_OK; 605 703 704 #if (NGX_HAVE_GEOIP_IPV6) 705 case GEOIP_COUNTRY_EDITION_V6: 706 707 gcf->country_ipv6 = 1; 708 return NGX_CONF_OK; 709 #endif 710 606 711 default: 607 712 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 608 713 "invalid GeoIP database \"%V\" type:%d", … … 652 757 case GEOIP_DOMAIN_EDITION: 653 758 case GEOIP_ASNUM_EDITION: 654 759 760 gcf->org_ipv6 = 0; 655 761 return NGX_CONF_OK; 656 762 763 #if (NGX_HAVE_GEOIP_IPV6) 764 case GEOIP_ISP_EDITION_V6: 765 case GEOIP_ORG_EDITION_V6: 766 case GEOIP_DOMAIN_EDITION_V6: 767 case GEOIP_ASNUM_EDITION_V6: 768 769 gcf->org_ipv6 = 1; 770 return NGX_CONF_OK; 771 #endif 772 657 773 default: 658 774 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 659 775 "invalid GeoIP database \"%V\" type:%d", … … 701 817 case GEOIP_CITY_EDITION_REV0: 702 818 case GEOIP_CITY_EDITION_REV1: 703 819 820 gcf->city_ipv6 = 0; 821 return NGX_CONF_OK; 822 823 #if (NGX_HAVE_GEOIP_IPV6) 824 case GEOIP_CITY_EDITION_REV0_V6: 825 case GEOIP_CITY_EDITION_REV1_V6: 826 827 gcf->city_ipv6 = 1; 704 828 return NGX_CONF_OK; 829 #endif 705 830 706 831 default: 707 832 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
