diff -NurpP nginx-1.2.2.orig//auto/lib/geoip/conf nginx-1.2.2/auto/lib/geoip/conf --- nginx-1.2.2.orig//auto/lib/geoip/conf 2012-01-18 15:07:43.000000000 +0000 +++ nginx-1.2.2/auto/lib/geoip/conf 2012-07-03 12:05:59.125459805 +0000 @@ -6,7 +6,7 @@ ngx_feature="GeoIP library" ngx_feature_name= ngx_feature_run=no - ngx_feature_incs= + ngx_feature_incs="#include " ngx_feature_path= ngx_feature_libs="-lGeoIP" ngx_feature_test="GeoIP_open(NULL, 0)" @@ -18,6 +18,7 @@ if [ $ngx_found = no ]; then # FreeBSD port ngx_feature="GeoIP library in /usr/local/" + ngx_feature_path="/usr/local/include" if [ $NGX_RPATH = YES ]; then ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lGeoIP" @@ -34,7 +35,7 @@ if [ $ngx_found = no ]; then # NetBSD port ngx_feature="GeoIP library in /usr/pkg/" - ngx_feature_path="/usr/pkg/include/" + ngx_feature_path="/usr/pkg/include" if [ $NGX_RPATH = YES ]; then ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lGeoIP" @@ -64,8 +65,20 @@ fi if [ $ngx_found = yes ]; then + CORE_INCS="$CORE_INCS $ngx_feature_path" CORE_LIBS="$CORE_LIBS $ngx_feature_libs" + if [ $NGX_IPV6 = YES ]; then + ngx_feature="GeoIP IPV6 support" + ngx_feature_name="NGX_HAVE_GEOIP_IPV6" + ngx_feature_run=no + ngx_feature_incs="#include + #include " + #ngx_feature_path= + #ngx_feature_libs= + ngx_feature_test="printf(\"%d\", GEOIP_CITY_EDITION_REV0_V6);" + . auto/feature + fi else cat << END diff -NurpP nginx-1.2.2.orig//auto/lib/libgd/conf nginx-1.2.2/auto/lib/libgd/conf --- nginx-1.2.2.orig//auto/lib/libgd/conf 2012-01-18 15:07:43.000000000 +0000 +++ nginx-1.2.2/auto/lib/libgd/conf 2012-07-03 12:05:59.125459805 +0000 @@ -35,7 +35,7 @@ if [ $ngx_found = no ]; then # NetBSD port ngx_feature="GD library in /usr/pkg/" - ngx_feature_path="/usr/pkg/include/" + ngx_feature_path="/usr/pkg/include" if [ $NGX_RPATH = YES ]; then ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lgd" diff -NurpP nginx-1.2.2.orig//src/http/modules/ngx_http_geoip_module.c nginx-1.2.2/src/http/modules/ngx_http_geoip_module.c --- nginx-1.2.2.orig//src/http/modules/ngx_http_geoip_module.c 2012-07-02 15:41:31.000000000 +0000 +++ nginx-1.2.2/src/http/modules/ngx_http_geoip_module.c 2012-07-03 12:07:33.266465344 +0000 @@ -12,11 +12,17 @@ #include #include +#define NGX_GEOIP_COUNTRY_CODE 0 +#define NGX_GEOIP_COUNTRY_CODE3 1 +#define NGX_GEOIP_COUNTRY_NAME 2 typedef struct { GeoIP *country; GeoIP *org; GeoIP *city; + unsigned country_ipv6:1; + unsigned org_ipv6:1; + unsigned city_ipv6:1; ngx_array_t *proxies; /* array of ngx_cidr_t */ ngx_flag_t proxy_recursive; } ngx_http_geoip_conf_t; @@ -30,6 +36,26 @@ typedef struct { typedef char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr); + +ngx_http_geoip_variable_handler_pt ngx_http_geoip_country_functions[] = { + GeoIP_country_code_by_ipnum, + GeoIP_country_code3_by_ipnum, + GeoIP_country_name_by_ipnum, +}; + + +#if (NGX_HAVE_GEOIP_IPV6) +typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *, geoipv6_t addr); + + +ngx_http_geoip_variable_handler_v6_pt ngx_http_geoip_country_ipv6_functions[] = { + GeoIP_country_code_by_ipnum_v6, + GeoIP_country_code3_by_ipnum_v6, + GeoIP_country_name_by_ipnum_v6, +}; +#endif + + static u_long ngx_http_geoip_addr(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf); static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, @@ -138,19 +164,19 @@ static ngx_http_variable_t ngx_http_geo { ngx_string("geoip_country_code"), NULL, ngx_http_geoip_country_variable, - (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 }, + NGX_GEOIP_COUNTRY_CODE, 0, 0 }, { ngx_string("geoip_country_code3"), NULL, ngx_http_geoip_country_variable, - (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 }, + NGX_GEOIP_COUNTRY_CODE3, 0, 0 }, { ngx_string("geoip_country_name"), NULL, ngx_http_geoip_country_variable, - (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 }, + NGX_GEOIP_COUNTRY_NAME, 0, 0 }, { ngx_string("geoip_org"), NULL, ngx_http_geoip_org_variable, - (uintptr_t) GeoIP_name_by_ipnum, 0, 0 }, + 0, 0, 0 }, { ngx_string("geoip_city_continent_code"), NULL, ngx_http_geoip_city_variable, @@ -246,15 +272,69 @@ ngx_http_geoip_addr(ngx_http_request_t * } +#if (NGX_HAVE_GEOIP_IPV6) +static geoipv6_t +ngx_http_geoip_addr_v6(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf) +{ + ngx_addr_t addr; + ngx_table_elt_t *xfwd; + struct sockaddr_in6 *sin6; + struct sockaddr_in *sin; + struct in6_addr addr6; + u_long laddr; + + addr.sockaddr = r->connection->sockaddr; + addr.socklen = r->connection->socklen; + /* addr.name = r->connection->addr_text; */ + + xfwd = r->headers_in.x_forwarded_for; + + if (xfwd != NULL && gcf->proxies != NULL) { + (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data, + xfwd->value.len, gcf->proxies, + gcf->proxy_recursive); + } + + switch (addr.sockaddr->sa_family) { + + case AF_INET: + /* Produce IPv4 mapped IPv6 address */ + sin = (struct sockaddr_in *) addr.sockaddr; + laddr = ntohl(sin->sin_addr.s_addr); + + ngx_memzero(&addr6, sizeof(struct in6_addr)); + addr6.s6_addr[10] = 0xFF; + addr6.s6_addr[11] = 0xFF; + addr6.s6_addr[12] = laddr >> 24; + addr6.s6_addr[13] = laddr >> 16; + addr6.s6_addr[14] = laddr >> 8; + addr6.s6_addr[15] = laddr; + return addr6; + + case AF_INET6: + sin6 = (struct sockaddr_in6 *) addr.sockaddr; + return sin6->sin6_addr; + + } + + return in6addr_any; +} +#endif + + static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_http_geoip_variable_handler_pt handler = - (ngx_http_geoip_variable_handler_pt) data; - - const char *val; - ngx_http_geoip_conf_t *gcf; +#if (NGX_HAVE_GEOIP_IPV6) + ngx_http_geoip_variable_handler_v6_pt handler_v6 = + ngx_http_geoip_country_ipv6_functions[data]; +#endif + ngx_http_geoip_variable_handler_pt handler = + ngx_http_geoip_country_functions[data]; + + const char *val; + ngx_http_geoip_conf_t *gcf; gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); @@ -262,7 +342,13 @@ ngx_http_geoip_country_variable(ngx_http goto not_found; } +#if (NGX_HAVE_GEOIP_IPV6) + val = gcf->country_ipv6 + ? handler_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf)) + : handler(gcf->country, ngx_http_geoip_addr(r, gcf)); +#else val = handler(gcf->country, ngx_http_geoip_addr(r, gcf)); +#endif if (val == NULL) { goto not_found; @@ -288,9 +374,6 @@ static ngx_int_t ngx_http_geoip_org_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_http_geoip_variable_handler_pt handler = - (ngx_http_geoip_variable_handler_pt) data; - size_t len; char *val; ngx_http_geoip_conf_t *gcf; @@ -301,7 +384,13 @@ ngx_http_geoip_org_variable(ngx_http_req goto not_found; } - val = handler(gcf->org, ngx_http_geoip_addr(r, gcf)); +#if (NGX_HAVE_GEOIP_IPV6) + val = gcf->country_ipv6 + ? GeoIP_name_by_ipnum_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf)) + : GeoIP_name_by_ipnum(gcf->country, ngx_http_geoip_addr(r, gcf)); +#else + val = GeoIP_name_by_ipnum(gcf->org, ngx_http_geoip_addr(r, gcf)); +#endif if (val == NULL) { goto not_found; @@ -491,7 +580,13 @@ ngx_http_geoip_get_city_record(ngx_http_ gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); if (gcf->city) { +#if (NGX_HAVE_GEOIP_IPV6) + return gcf->city_ipv6 + ? GeoIP_record_by_ipnum_v6(gcf->city, ngx_http_geoip_addr_v6(r, gcf)) + : GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); +#else return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); +#endif } return NULL; @@ -592,8 +687,16 @@ ngx_http_geoip_country(ngx_conf_t *cf, n case GEOIP_PROXY_EDITION: case GEOIP_NETSPEED_EDITION: + gcf->country_ipv6 = 0; return NGX_CONF_OK; +#if (NGX_HAVE_GEOIP_IPV6) + case GEOIP_COUNTRY_EDITION_V6: + + gcf->country_ipv6 = 1; + return NGX_CONF_OK; +#endif + default: ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid GeoIP database \"%V\" type:%d", @@ -643,7 +746,18 @@ ngx_http_geoip_org(ngx_conf_t *cf, ngx_c case GEOIP_DOMAIN_EDITION: case GEOIP_ASNUM_EDITION: + gcf->org_ipv6 = 0; + return NGX_CONF_OK; + +#if (NGX_HAVE_GEOIP_IPV6) + case GEOIP_ISP_EDITION_V6: + case GEOIP_ORG_EDITION_V6: + case GEOIP_DOMAIN_EDITION_V6: + case GEOIP_ASNUM_EDITION_V6: + + gcf->org_ipv6 = 1; return NGX_CONF_OK; +#endif default: ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, @@ -692,8 +806,17 @@ ngx_http_geoip_city(ngx_conf_t *cf, ngx_ case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: + gcf->city_ipv6 = 0; return NGX_CONF_OK; +#if (NGX_HAVE_GEOIP_IPV6) + case GEOIP_CITY_EDITION_REV0_V6: + case GEOIP_CITY_EDITION_REV1_V6: + + gcf->city_ipv6 = 1; + return NGX_CONF_OK; +#endif + default: ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid GeoIP City database \"%V\" type:%d",