Ticket #250: nginx-geoipv6.patch

File nginx-geoipv6.patch, 9.5 KB (added by Gregor Kališnik, 14 years ago)

Updated patch for GeoIPv6 module

  • 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  
    66    ngx_feature="GeoIP library"
    77    ngx_feature_name=
    88    ngx_feature_run=no
    9     ngx_feature_incs=
     9    ngx_feature_incs="#include <GeoIP.h>"
    1010    ngx_feature_path=
    1111    ngx_feature_libs="-lGeoIP"
    1212    ngx_feature_test="GeoIP_open(NULL, 0)"
     
    1818    # FreeBSD port
    1919
    2020    ngx_feature="GeoIP library in /usr/local/"
     21    ngx_feature_path="/usr/local/include"
    2122
    2223    if [ $NGX_RPATH = YES ]; then
    2324        ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lGeoIP"
     
    3435    # NetBSD port
    3536
    3637    ngx_feature="GeoIP library in /usr/pkg/"
    37     ngx_feature_path="/usr/pkg/include/"
     38    ngx_feature_path="/usr/pkg/include"
    3839
    3940    if [ $NGX_RPATH = YES ]; then
    4041        ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lGeoIP"
     
    6465
    6566
    6667if [ $ngx_found = yes ]; then
     68
     69    CORE_INCS="$CORE_INCS $ngx_feature_path"
    6770    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
    6871
     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
    6984else
    7085
    7186cat << 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  
    3535    # NetBSD port
    3636
    3737    ngx_feature="GD library in /usr/pkg/"
    38     ngx_feature_path="/usr/pkg/include/"
     38    ngx_feature_path="/usr/pkg/include"
    3939
    4040    if [ $NGX_RPATH = YES ]; then
    4141        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  
    1313#include <GeoIPCity.h>
    1414
    1515
     16#define NGX_GEOIP_COUNTRY_CODE  0
     17#define NGX_GEOIP_COUNTRY_CODE3 1
     18#define NGX_GEOIP_COUNTRY_NAME  2
     19
     20
    1621typedef struct {
    1722    GeoIP        *country;
    1823    GeoIP        *org;
    1924    GeoIP        *city;
    2025    ngx_array_t  *proxies;    /* array of ngx_cidr_t */
    2126    ngx_flag_t    proxy_recursive;
     27    unsigned      country_ipv6:1;
     28    unsigned      org_ipv6:1;
     29    unsigned      city_ipv6:1;
    2230} ngx_http_geoip_conf_t;
    2331
    2432
     
    2836} ngx_http_geoip_var_t;
    2937
    3038
    31 typedef char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr);
     39typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr);
     40
     41
     42ngx_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)
     50typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *, geoipv6_t addr);
     51
     52
     53ngx_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
    3260
    3361static u_long ngx_http_geoip_addr(ngx_http_request_t *r,
    3462    ngx_http_geoip_conf_t *gcf);
     
    138166
    139167    { ngx_string("geoip_country_code"), NULL,
    140168      ngx_http_geoip_country_variable,
    141       (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 },
     169      NGX_GEOIP_COUNTRY_CODE, 0, 0 },
    142170
    143171    { ngx_string("geoip_country_code3"), NULL,
    144172      ngx_http_geoip_country_variable,
    145       (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 },
     173      NGX_GEOIP_COUNTRY_CODE3, 0, 0 },
    146174
    147175    { ngx_string("geoip_country_name"), NULL,
    148176      ngx_http_geoip_country_variable,
    149       (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 },
     177      NGX_GEOIP_COUNTRY_NAME, 0, 0 },
    150178
    151179    { ngx_string("geoip_org"), NULL,
    152180      ngx_http_geoip_org_variable,
    153       (uintptr_t) GeoIP_name_by_ipnum, 0, 0 },
     181      0, 0, 0 },
    154182
    155183    { ngx_string("geoip_city_continent_code"), NULL,
    156184      ngx_http_geoip_city_variable,
     
    255283}
    256284
    257285
     286#if (NGX_HAVE_GEOIP_IPV6)
     287static geoipv6_t
     288ngx_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
    258336static ngx_int_t
    259337ngx_http_geoip_country_variable(ngx_http_request_t *r,
    260338    ngx_http_variable_value_t *v, uintptr_t data)
    261339{
    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];
    264346
    265     const char             *val;
    266     ngx_http_geoip_conf_t  *gcf;
     347    const char                           *val;
     348    ngx_http_geoip_conf_t                *gcf;
    267349
    268350    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
    269351
     
    271353        goto not_found;
    272354    }
    273355
     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
    274361    val = handler(gcf->country, ngx_http_geoip_addr(r, gcf));
     362#endif
    275363
    276364    if (val == NULL) {
    277365        goto not_found;
     
    297385ngx_http_geoip_org_variable(ngx_http_request_t *r,
    298386    ngx_http_variable_value_t *v, uintptr_t data)
    299387{
    300     ngx_http_geoip_variable_handler_pt  handler =
    301         (ngx_http_geoip_variable_handler_pt) data;
    302 
    303388    size_t                  len;
    304389    char                   *val;
    305390    ngx_http_geoip_conf_t  *gcf;
     
    310395        goto not_found;
    311396    }
    312397
    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
    314405
    315406    if (val == NULL) {
    316407        goto not_found;
     
    500591    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
    501592
    502593    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
    503599        return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf));
     600#endif
    504601    }
    505602
    506603    return NULL;
     
    601698    case GEOIP_PROXY_EDITION:
    602699    case GEOIP_NETSPEED_EDITION:
    603700
     701        gcf->country_ipv6 = 0;
    604702        return NGX_CONF_OK;
    605703
     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
    606711    default:
    607712        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    608713                           "invalid GeoIP database \"%V\" type:%d",
     
    652757    case GEOIP_DOMAIN_EDITION:
    653758    case GEOIP_ASNUM_EDITION:
    654759
     760        gcf->org_ipv6 = 0;
    655761        return NGX_CONF_OK;
    656762
     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
    657773    default:
    658774        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    659775                           "invalid GeoIP database \"%V\" type:%d",
     
    701817    case GEOIP_CITY_EDITION_REV0:
    702818    case GEOIP_CITY_EDITION_REV1:
    703819
     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;
    704828        return NGX_CONF_OK;
     829#endif
    705830
    706831    default:
    707832        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,