Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_stream.h>
0011 
0012 
0013 typedef struct {
0014     ngx_stream_variable_value_t       *value;
0015     u_short                            start;
0016     u_short                            end;
0017 } ngx_stream_geo_range_t;
0018 
0019 
0020 typedef struct {
0021     ngx_radix_tree_t                  *tree;
0022 #if (NGX_HAVE_INET6)
0023     ngx_radix_tree_t                  *tree6;
0024 #endif
0025 } ngx_stream_geo_trees_t;
0026 
0027 
0028 typedef struct {
0029     ngx_stream_geo_range_t           **low;
0030     ngx_stream_variable_value_t       *default_value;
0031 } ngx_stream_geo_high_ranges_t;
0032 
0033 
0034 typedef struct {
0035     ngx_str_node_t                     sn;
0036     ngx_stream_variable_value_t       *value;
0037     size_t                             offset;
0038 } ngx_stream_geo_variable_value_node_t;
0039 
0040 
0041 typedef struct {
0042     ngx_stream_variable_value_t       *value;
0043     ngx_str_t                         *net;
0044     ngx_stream_geo_high_ranges_t       high;
0045     ngx_radix_tree_t                  *tree;
0046 #if (NGX_HAVE_INET6)
0047     ngx_radix_tree_t                  *tree6;
0048 #endif
0049     ngx_rbtree_t                       rbtree;
0050     ngx_rbtree_node_t                  sentinel;
0051     ngx_pool_t                        *pool;
0052     ngx_pool_t                        *temp_pool;
0053 
0054     size_t                             data_size;
0055 
0056     ngx_str_t                          include_name;
0057     ngx_uint_t                         includes;
0058     ngx_uint_t                         entries;
0059 
0060     unsigned                           ranges:1;
0061     unsigned                           outside_entries:1;
0062     unsigned                           allow_binary_include:1;
0063     unsigned                           binary_include:1;
0064 } ngx_stream_geo_conf_ctx_t;
0065 
0066 
0067 typedef struct {
0068     union {
0069         ngx_stream_geo_trees_t         trees;
0070         ngx_stream_geo_high_ranges_t   high;
0071     } u;
0072 
0073     ngx_int_t                          index;
0074 } ngx_stream_geo_ctx_t;
0075 
0076 
0077 static ngx_int_t ngx_stream_geo_addr(ngx_stream_session_t *s,
0078     ngx_stream_geo_ctx_t *ctx, ngx_addr_t *addr);
0079 
0080 static char *ngx_stream_geo_block(ngx_conf_t *cf, ngx_command_t *cmd,
0081     void *conf);
0082 static char *ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
0083 static char *ngx_stream_geo_range(ngx_conf_t *cf,
0084     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value);
0085 static char *ngx_stream_geo_add_range(ngx_conf_t *cf,
0086     ngx_stream_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
0087 static ngx_uint_t ngx_stream_geo_delete_range(ngx_conf_t *cf,
0088     ngx_stream_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
0089 static char *ngx_stream_geo_cidr(ngx_conf_t *cf,
0090     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value);
0091 static char *ngx_stream_geo_cidr_add(ngx_conf_t *cf,
0092     ngx_stream_geo_conf_ctx_t *ctx, ngx_cidr_t *cidr, ngx_str_t *value,
0093     ngx_str_t *net);
0094 static ngx_stream_variable_value_t *ngx_stream_geo_value(ngx_conf_t *cf,
0095     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value);
0096 static ngx_int_t ngx_stream_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
0097     ngx_cidr_t *cidr);
0098 static char *ngx_stream_geo_include(ngx_conf_t *cf,
0099     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name);
0100 static ngx_int_t ngx_stream_geo_include_binary_base(ngx_conf_t *cf,
0101     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name);
0102 static void ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx);
0103 static u_char *ngx_stream_geo_copy_values(u_char *base, u_char *p,
0104     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
0105 
0106 
0107 static ngx_command_t  ngx_stream_geo_commands[] = {
0108 
0109     { ngx_string("geo"),
0110       NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
0111       ngx_stream_geo_block,
0112       0,
0113       0,
0114       NULL },
0115 
0116       ngx_null_command
0117 };
0118 
0119 
0120 static ngx_stream_module_t  ngx_stream_geo_module_ctx = {
0121     NULL,                                  /* preconfiguration */
0122     NULL,                                  /* postconfiguration */
0123 
0124     NULL,                                  /* create main configuration */
0125     NULL,                                  /* init main configuration */
0126 
0127     NULL,                                  /* create server configuration */
0128     NULL                                   /* merge server configuration */
0129 };
0130 
0131 
0132 ngx_module_t  ngx_stream_geo_module = {
0133     NGX_MODULE_V1,
0134     &ngx_stream_geo_module_ctx,            /* module context */
0135     ngx_stream_geo_commands,               /* module directives */
0136     NGX_STREAM_MODULE,                     /* module type */
0137     NULL,                                  /* init master */
0138     NULL,                                  /* init module */
0139     NULL,                                  /* init process */
0140     NULL,                                  /* init thread */
0141     NULL,                                  /* exit thread */
0142     NULL,                                  /* exit process */
0143     NULL,                                  /* exit master */
0144     NGX_MODULE_V1_PADDING
0145 };
0146 
0147 
0148 typedef struct {
0149     u_char    GEORNG[6];
0150     u_char    version;
0151     u_char    ptr_size;
0152     uint32_t  endianness;
0153     uint32_t  crc32;
0154 } ngx_stream_geo_header_t;
0155 
0156 
0157 static ngx_stream_geo_header_t  ngx_stream_geo_header = {
0158     { 'G', 'E', 'O', 'R', 'N', 'G' }, 0, sizeof(void *), 0x12345678, 0
0159 };
0160 
0161 
0162 /* geo range is AF_INET only */
0163 
0164 static ngx_int_t
0165 ngx_stream_geo_cidr_variable(ngx_stream_session_t *s,
0166     ngx_stream_variable_value_t *v, uintptr_t data)
0167 {
0168     ngx_stream_geo_ctx_t *ctx = (ngx_stream_geo_ctx_t *) data;
0169 
0170     in_addr_t                     inaddr;
0171     ngx_addr_t                    addr;
0172     struct sockaddr_in           *sin;
0173     ngx_stream_variable_value_t  *vv;
0174 #if (NGX_HAVE_INET6)
0175     u_char                       *p;
0176     struct in6_addr              *inaddr6;
0177 #endif
0178 
0179     if (ngx_stream_geo_addr(s, ctx, &addr) != NGX_OK) {
0180         vv = (ngx_stream_variable_value_t *)
0181                   ngx_radix32tree_find(ctx->u.trees.tree, INADDR_NONE);
0182         goto done;
0183     }
0184 
0185     switch (addr.sockaddr->sa_family) {
0186 
0187 #if (NGX_HAVE_INET6)
0188     case AF_INET6:
0189         inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr;
0190         p = inaddr6->s6_addr;
0191 
0192         if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
0193             inaddr = p[12] << 24;
0194             inaddr += p[13] << 16;
0195             inaddr += p[14] << 8;
0196             inaddr += p[15];
0197 
0198             vv = (ngx_stream_variable_value_t *)
0199                       ngx_radix32tree_find(ctx->u.trees.tree, inaddr);
0200 
0201         } else {
0202             vv = (ngx_stream_variable_value_t *)
0203                       ngx_radix128tree_find(ctx->u.trees.tree6, p);
0204         }
0205 
0206         break;
0207 #endif
0208 
0209     default: /* AF_INET */
0210         sin = (struct sockaddr_in *) addr.sockaddr;
0211         inaddr = ntohl(sin->sin_addr.s_addr);
0212 
0213         vv = (ngx_stream_variable_value_t *)
0214                   ngx_radix32tree_find(ctx->u.trees.tree, inaddr);
0215 
0216         break;
0217     }
0218 
0219 done:
0220 
0221     *v = *vv;
0222 
0223     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0224                    "stream geo: %v", v);
0225 
0226     return NGX_OK;
0227 }
0228 
0229 
0230 static ngx_int_t
0231 ngx_stream_geo_range_variable(ngx_stream_session_t *s,
0232     ngx_stream_variable_value_t *v, uintptr_t data)
0233 {
0234     ngx_stream_geo_ctx_t *ctx = (ngx_stream_geo_ctx_t *) data;
0235 
0236     in_addr_t                inaddr;
0237     ngx_addr_t               addr;
0238     ngx_uint_t               n;
0239     struct sockaddr_in      *sin;
0240     ngx_stream_geo_range_t  *range;
0241 #if (NGX_HAVE_INET6)
0242     u_char                  *p;
0243     struct in6_addr         *inaddr6;
0244 #endif
0245 
0246     *v = *ctx->u.high.default_value;
0247 
0248     if (ngx_stream_geo_addr(s, ctx, &addr) == NGX_OK) {
0249 
0250         switch (addr.sockaddr->sa_family) {
0251 
0252 #if (NGX_HAVE_INET6)
0253         case AF_INET6:
0254             inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr;
0255 
0256             if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
0257                 p = inaddr6->s6_addr;
0258 
0259                 inaddr = p[12] << 24;
0260                 inaddr += p[13] << 16;
0261                 inaddr += p[14] << 8;
0262                 inaddr += p[15];
0263 
0264             } else {
0265                 inaddr = INADDR_NONE;
0266             }
0267 
0268             break;
0269 #endif
0270 
0271         default: /* AF_INET */
0272             sin = (struct sockaddr_in *) addr.sockaddr;
0273             inaddr = ntohl(sin->sin_addr.s_addr);
0274             break;
0275         }
0276 
0277     } else {
0278         inaddr = INADDR_NONE;
0279     }
0280 
0281     if (ctx->u.high.low) {
0282         range = ctx->u.high.low[inaddr >> 16];
0283 
0284         if (range) {
0285             n = inaddr & 0xffff;
0286             do {
0287                 if (n >= (ngx_uint_t) range->start
0288                     && n <= (ngx_uint_t) range->end)
0289                 {
0290                     *v = *range->value;
0291                     break;
0292                 }
0293             } while ((++range)->value);
0294         }
0295     }
0296 
0297     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0298                    "stream geo: %v", v);
0299 
0300     return NGX_OK;
0301 }
0302 
0303 
0304 static ngx_int_t
0305 ngx_stream_geo_addr(ngx_stream_session_t *s, ngx_stream_geo_ctx_t *ctx,
0306     ngx_addr_t *addr)
0307 {
0308     ngx_stream_variable_value_t  *v;
0309 
0310     if (ctx->index == -1) {
0311         ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0312                        "stream geo started: %V", &s->connection->addr_text);
0313 
0314         addr->sockaddr = s->connection->sockaddr;
0315         addr->socklen = s->connection->socklen;
0316         /* addr->name = s->connection->addr_text; */
0317 
0318         return NGX_OK;
0319     }
0320 
0321     v = ngx_stream_get_flushed_variable(s, ctx->index);
0322 
0323     if (v == NULL || v->not_found) {
0324         ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0325                        "stream geo not found");
0326 
0327         return NGX_ERROR;
0328     }
0329 
0330     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0331                    "stream geo started: %v", v);
0332 
0333     if (ngx_parse_addr(s->connection->pool, addr, v->data, v->len) == NGX_OK) {
0334         return NGX_OK;
0335     }
0336 
0337     return NGX_ERROR;
0338 }
0339 
0340 
0341 static char *
0342 ngx_stream_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0343 {
0344     char                         *rv;
0345     size_t                        len;
0346     ngx_str_t                    *value, name;
0347     ngx_uint_t                    i;
0348     ngx_conf_t                    save;
0349     ngx_pool_t                   *pool;
0350     ngx_array_t                  *a;
0351     ngx_stream_variable_t        *var;
0352     ngx_stream_geo_ctx_t         *geo;
0353     ngx_stream_geo_conf_ctx_t     ctx;
0354 #if (NGX_HAVE_INET6)
0355     static struct in6_addr        zero;
0356 #endif
0357 
0358     value = cf->args->elts;
0359 
0360     geo = ngx_palloc(cf->pool, sizeof(ngx_stream_geo_ctx_t));
0361     if (geo == NULL) {
0362         return NGX_CONF_ERROR;
0363     }
0364 
0365     name = value[1];
0366 
0367     if (name.data[0] != '$') {
0368         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0369                            "invalid variable name \"%V\"", &name);
0370         return NGX_CONF_ERROR;
0371     }
0372 
0373     name.len--;
0374     name.data++;
0375 
0376     if (cf->args->nelts == 3) {
0377 
0378         geo->index = ngx_stream_get_variable_index(cf, &name);
0379         if (geo->index == NGX_ERROR) {
0380             return NGX_CONF_ERROR;
0381         }
0382 
0383         name = value[2];
0384 
0385         if (name.data[0] != '$') {
0386             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0387                                "invalid variable name \"%V\"", &name);
0388             return NGX_CONF_ERROR;
0389         }
0390 
0391         name.len--;
0392         name.data++;
0393 
0394     } else {
0395         geo->index = -1;
0396     }
0397 
0398     var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE);
0399     if (var == NULL) {
0400         return NGX_CONF_ERROR;
0401     }
0402 
0403     pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
0404     if (pool == NULL) {
0405         return NGX_CONF_ERROR;
0406     }
0407 
0408     ngx_memzero(&ctx, sizeof(ngx_stream_geo_conf_ctx_t));
0409 
0410     ctx.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
0411     if (ctx.temp_pool == NULL) {
0412         return NGX_CONF_ERROR;
0413     }
0414 
0415     ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value);
0416 
0417     ctx.pool = cf->pool;
0418     ctx.data_size = sizeof(ngx_stream_geo_header_t)
0419                   + sizeof(ngx_stream_variable_value_t)
0420                   + 0x10000 * sizeof(ngx_stream_geo_range_t *);
0421     ctx.allow_binary_include = 1;
0422 
0423     save = *cf;
0424     cf->pool = pool;
0425     cf->ctx = &ctx;
0426     cf->handler = ngx_stream_geo;
0427     cf->handler_conf = conf;
0428 
0429     rv = ngx_conf_parse(cf, NULL);
0430 
0431     *cf = save;
0432 
0433     if (ctx.ranges) {
0434 
0435         if (ctx.high.low && !ctx.binary_include) {
0436             for (i = 0; i < 0x10000; i++) {
0437                 a = (ngx_array_t *) ctx.high.low[i];
0438 
0439                 if (a == NULL) {
0440                     continue;
0441                 }
0442 
0443                 if (a->nelts == 0) {
0444                     ctx.high.low[i] = NULL;
0445                     continue;
0446                 }
0447 
0448                 len = a->nelts * sizeof(ngx_stream_geo_range_t);
0449 
0450                 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *));
0451                 if (ctx.high.low[i] == NULL) {
0452                     return NGX_CONF_ERROR;
0453                 }
0454 
0455                 ngx_memcpy(ctx.high.low[i], a->elts, len);
0456                 ctx.high.low[i][a->nelts].value = NULL;
0457                 ctx.data_size += len + sizeof(void *);
0458             }
0459 
0460             if (ctx.allow_binary_include
0461                 && !ctx.outside_entries
0462                 && ctx.entries > 100000
0463                 && ctx.includes == 1)
0464             {
0465                 ngx_stream_geo_create_binary_base(&ctx);
0466             }
0467         }
0468 
0469         if (ctx.high.default_value == NULL) {
0470             ctx.high.default_value = &ngx_stream_variable_null_value;
0471         }
0472 
0473         geo->u.high = ctx.high;
0474 
0475         var->get_handler = ngx_stream_geo_range_variable;
0476         var->data = (uintptr_t) geo;
0477 
0478         ngx_destroy_pool(ctx.temp_pool);
0479         ngx_destroy_pool(pool);
0480 
0481     } else {
0482         if (ctx.tree == NULL) {
0483             ctx.tree = ngx_radix_tree_create(cf->pool, -1);
0484             if (ctx.tree == NULL) {
0485                 return NGX_CONF_ERROR;
0486             }
0487         }
0488 
0489         geo->u.trees.tree = ctx.tree;
0490 
0491 #if (NGX_HAVE_INET6)
0492         if (ctx.tree6 == NULL) {
0493             ctx.tree6 = ngx_radix_tree_create(cf->pool, -1);
0494             if (ctx.tree6 == NULL) {
0495                 return NGX_CONF_ERROR;
0496             }
0497         }
0498 
0499         geo->u.trees.tree6 = ctx.tree6;
0500 #endif
0501 
0502         var->get_handler = ngx_stream_geo_cidr_variable;
0503         var->data = (uintptr_t) geo;
0504 
0505         ngx_destroy_pool(ctx.temp_pool);
0506         ngx_destroy_pool(pool);
0507 
0508         if (ngx_radix32tree_insert(ctx.tree, 0, 0,
0509                                    (uintptr_t) &ngx_stream_variable_null_value)
0510             == NGX_ERROR)
0511         {
0512             return NGX_CONF_ERROR;
0513         }
0514 
0515         /* NGX_BUSY is okay (default was set explicitly) */
0516 
0517 #if (NGX_HAVE_INET6)
0518         if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr,
0519                                     (uintptr_t) &ngx_stream_variable_null_value)
0520             == NGX_ERROR)
0521         {
0522             return NGX_CONF_ERROR;
0523         }
0524 #endif
0525     }
0526 
0527     return rv;
0528 }
0529 
0530 
0531 static char *
0532 ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
0533 {
0534     char                       *rv;
0535     ngx_str_t                  *value;
0536     ngx_stream_geo_conf_ctx_t  *ctx;
0537 
0538     ctx = cf->ctx;
0539 
0540     value = cf->args->elts;
0541 
0542     if (cf->args->nelts == 1) {
0543 
0544         if (ngx_strcmp(value[0].data, "ranges") == 0) {
0545 
0546             if (ctx->tree
0547 #if (NGX_HAVE_INET6)
0548                 || ctx->tree6
0549 #endif
0550                )
0551             {
0552                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0553                                    "the \"ranges\" directive must be "
0554                                    "the first directive inside \"geo\" block");
0555                 goto failed;
0556             }
0557 
0558             ctx->ranges = 1;
0559 
0560             rv = NGX_CONF_OK;
0561 
0562             goto done;
0563         }
0564     }
0565 
0566     if (cf->args->nelts != 2) {
0567         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0568                            "invalid number of the geo parameters");
0569         goto failed;
0570     }
0571 
0572     if (ngx_strcmp(value[0].data, "include") == 0) {
0573 
0574         rv = ngx_stream_geo_include(cf, ctx, &value[1]);
0575 
0576         goto done;
0577     }
0578 
0579     if (ctx->ranges) {
0580         rv = ngx_stream_geo_range(cf, ctx, value);
0581 
0582     } else {
0583         rv = ngx_stream_geo_cidr(cf, ctx, value);
0584     }
0585 
0586 done:
0587 
0588     ngx_reset_pool(cf->pool);
0589 
0590     return rv;
0591 
0592 failed:
0593 
0594     ngx_reset_pool(cf->pool);
0595 
0596     return NGX_CONF_ERROR;
0597 }
0598 
0599 
0600 static char *
0601 ngx_stream_geo_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
0602     ngx_str_t *value)
0603 {
0604     u_char      *p, *last;
0605     in_addr_t    start, end;
0606     ngx_str_t   *net;
0607     ngx_uint_t   del;
0608 
0609     if (ngx_strcmp(value[0].data, "default") == 0) {
0610 
0611         if (ctx->high.default_value) {
0612             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
0613                 "duplicate default geo range value: \"%V\", old value: \"%v\"",
0614                 &value[1], ctx->high.default_value);
0615         }
0616 
0617         ctx->high.default_value = ngx_stream_geo_value(cf, ctx, &value[1]);
0618         if (ctx->high.default_value == NULL) {
0619             return NGX_CONF_ERROR;
0620         }
0621 
0622         return NGX_CONF_OK;
0623     }
0624 
0625     if (ctx->binary_include) {
0626         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0627             "binary geo range base \"%s\" cannot be mixed with usual entries",
0628             ctx->include_name.data);
0629         return NGX_CONF_ERROR;
0630     }
0631 
0632     if (ctx->high.low == NULL) {
0633         ctx->high.low = ngx_pcalloc(ctx->pool,
0634                                     0x10000 * sizeof(ngx_stream_geo_range_t *));
0635         if (ctx->high.low == NULL) {
0636             return NGX_CONF_ERROR;
0637         }
0638     }
0639 
0640     ctx->entries++;
0641     ctx->outside_entries = 1;
0642 
0643     if (ngx_strcmp(value[0].data, "delete") == 0) {
0644         net = &value[1];
0645         del = 1;
0646 
0647     } else {
0648         net = &value[0];
0649         del = 0;
0650     }
0651 
0652     last = net->data + net->len;
0653 
0654     p = ngx_strlchr(net->data, last, '-');
0655 
0656     if (p == NULL) {
0657         goto invalid;
0658     }
0659 
0660     start = ngx_inet_addr(net->data, p - net->data);
0661 
0662     if (start == INADDR_NONE) {
0663         goto invalid;
0664     }
0665 
0666     start = ntohl(start);
0667 
0668     p++;
0669 
0670     end = ngx_inet_addr(p, last - p);
0671 
0672     if (end == INADDR_NONE) {
0673         goto invalid;
0674     }
0675 
0676     end = ntohl(end);
0677 
0678     if (start > end) {
0679         goto invalid;
0680     }
0681 
0682     if (del) {
0683         if (ngx_stream_geo_delete_range(cf, ctx, start, end)) {
0684             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
0685                                "no address range \"%V\" to delete", net);
0686         }
0687 
0688         return NGX_CONF_OK;
0689     }
0690 
0691     ctx->value = ngx_stream_geo_value(cf, ctx, &value[1]);
0692 
0693     if (ctx->value == NULL) {
0694         return NGX_CONF_ERROR;
0695     }
0696 
0697     ctx->net = net;
0698 
0699     return ngx_stream_geo_add_range(cf, ctx, start, end);
0700 
0701 invalid:
0702 
0703     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net);
0704 
0705     return NGX_CONF_ERROR;
0706 }
0707 
0708 
0709 /* the add procedure is optimized to add a growing up sequence */
0710 
0711 static char *
0712 ngx_stream_geo_add_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
0713     in_addr_t start, in_addr_t end)
0714 {
0715     in_addr_t                n;
0716     ngx_uint_t               h, i, s, e;
0717     ngx_array_t             *a;
0718     ngx_stream_geo_range_t  *range;
0719 
0720     for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
0721 
0722         h = n >> 16;
0723 
0724         if (n == start) {
0725             s = n & 0xffff;
0726         } else {
0727             s = 0;
0728         }
0729 
0730         if ((n | 0xffff) > end) {
0731             e = end & 0xffff;
0732 
0733         } else {
0734             e = 0xffff;
0735         }
0736 
0737         a = (ngx_array_t *) ctx->high.low[h];
0738 
0739         if (a == NULL) {
0740             a = ngx_array_create(ctx->temp_pool, 64,
0741                                  sizeof(ngx_stream_geo_range_t));
0742             if (a == NULL) {
0743                 return NGX_CONF_ERROR;
0744             }
0745 
0746             ctx->high.low[h] = (ngx_stream_geo_range_t *) a;
0747         }
0748 
0749         i = a->nelts;
0750         range = a->elts;
0751 
0752         while (i) {
0753 
0754             i--;
0755 
0756             if (e < (ngx_uint_t) range[i].start) {
0757                 continue;
0758             }
0759 
0760             if (s > (ngx_uint_t) range[i].end) {
0761 
0762                 /* add after the range */
0763 
0764                 range = ngx_array_push(a);
0765                 if (range == NULL) {
0766                     return NGX_CONF_ERROR;
0767                 }
0768 
0769                 range = a->elts;
0770 
0771                 ngx_memmove(&range[i + 2], &range[i + 1],
0772                            (a->nelts - 2 - i) * sizeof(ngx_stream_geo_range_t));
0773 
0774                 range[i + 1].start = (u_short) s;
0775                 range[i + 1].end = (u_short) e;
0776                 range[i + 1].value = ctx->value;
0777 
0778                 goto next;
0779             }
0780 
0781             if (s == (ngx_uint_t) range[i].start
0782                 && e == (ngx_uint_t) range[i].end)
0783             {
0784                 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
0785                     "duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
0786                     ctx->net, ctx->value, range[i].value);
0787 
0788                 range[i].value = ctx->value;
0789 
0790                 goto next;
0791             }
0792 
0793             if (s > (ngx_uint_t) range[i].start
0794                 && e < (ngx_uint_t) range[i].end)
0795             {
0796                 /* split the range and insert the new one */
0797 
0798                 range = ngx_array_push(a);
0799                 if (range == NULL) {
0800                     return NGX_CONF_ERROR;
0801                 }
0802 
0803                 range = ngx_array_push(a);
0804                 if (range == NULL) {
0805                     return NGX_CONF_ERROR;
0806                 }
0807 
0808                 range = a->elts;
0809 
0810                 ngx_memmove(&range[i + 3], &range[i + 1],
0811                            (a->nelts - 3 - i) * sizeof(ngx_stream_geo_range_t));
0812 
0813                 range[i + 2].start = (u_short) (e + 1);
0814                 range[i + 2].end = range[i].end;
0815                 range[i + 2].value = range[i].value;
0816 
0817                 range[i + 1].start = (u_short) s;
0818                 range[i + 1].end = (u_short) e;
0819                 range[i + 1].value = ctx->value;
0820 
0821                 range[i].end = (u_short) (s - 1);
0822 
0823                 goto next;
0824             }
0825 
0826             if (s == (ngx_uint_t) range[i].start
0827                 && e < (ngx_uint_t) range[i].end)
0828             {
0829                 /* shift the range start and insert the new range */
0830 
0831                 range = ngx_array_push(a);
0832                 if (range == NULL) {
0833                     return NGX_CONF_ERROR;
0834                 }
0835 
0836                 range = a->elts;
0837 
0838                 ngx_memmove(&range[i + 1], &range[i],
0839                            (a->nelts - 1 - i) * sizeof(ngx_stream_geo_range_t));
0840 
0841                 range[i + 1].start = (u_short) (e + 1);
0842 
0843                 range[i].start = (u_short) s;
0844                 range[i].end = (u_short) e;
0845                 range[i].value = ctx->value;
0846 
0847                 goto next;
0848             }
0849 
0850             if (s > (ngx_uint_t) range[i].start
0851                 && e == (ngx_uint_t) range[i].end)
0852             {
0853                 /* shift the range end and insert the new range */
0854 
0855                 range = ngx_array_push(a);
0856                 if (range == NULL) {
0857                     return NGX_CONF_ERROR;
0858                 }
0859 
0860                 range = a->elts;
0861 
0862                 ngx_memmove(&range[i + 2], &range[i + 1],
0863                            (a->nelts - 2 - i) * sizeof(ngx_stream_geo_range_t));
0864 
0865                 range[i + 1].start = (u_short) s;
0866                 range[i + 1].end = (u_short) e;
0867                 range[i + 1].value = ctx->value;
0868 
0869                 range[i].end = (u_short) (s - 1);
0870 
0871                 goto next;
0872             }
0873 
0874             s = (ngx_uint_t) range[i].start;
0875             e = (ngx_uint_t) range[i].end;
0876 
0877             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0878                          "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"",
0879                          ctx->net,
0880                          h >> 8, h & 0xff, s >> 8, s & 0xff,
0881                          h >> 8, h & 0xff, e >> 8, e & 0xff);
0882 
0883             return NGX_CONF_ERROR;
0884         }
0885 
0886         /* add the first range */
0887 
0888         range = ngx_array_push(a);
0889         if (range == NULL) {
0890             return NGX_CONF_ERROR;
0891         }
0892 
0893         range = a->elts;
0894 
0895         ngx_memmove(&range[1], &range[0],
0896                     (a->nelts - 1) * sizeof(ngx_stream_geo_range_t));
0897 
0898         range[0].start = (u_short) s;
0899         range[0].end = (u_short) e;
0900         range[0].value = ctx->value;
0901 
0902     next:
0903 
0904         if (h == 0xffff) {
0905             break;
0906         }
0907     }
0908 
0909     return NGX_CONF_OK;
0910 }
0911 
0912 
0913 static ngx_uint_t
0914 ngx_stream_geo_delete_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
0915     in_addr_t start, in_addr_t end)
0916 {
0917     in_addr_t                n;
0918     ngx_uint_t               h, i, s, e, warn;
0919     ngx_array_t             *a;
0920     ngx_stream_geo_range_t  *range;
0921 
0922     warn = 0;
0923 
0924     for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
0925 
0926         h = n >> 16;
0927 
0928         if (n == start) {
0929             s = n & 0xffff;
0930         } else {
0931             s = 0;
0932         }
0933 
0934         if ((n | 0xffff) > end) {
0935             e = end & 0xffff;
0936 
0937         } else {
0938             e = 0xffff;
0939         }
0940 
0941         a = (ngx_array_t *) ctx->high.low[h];
0942 
0943         if (a == NULL || a->nelts == 0) {
0944             warn = 1;
0945             goto next;
0946         }
0947 
0948         range = a->elts;
0949         for (i = 0; i < a->nelts; i++) {
0950 
0951             if (s == (ngx_uint_t) range[i].start
0952                 && e == (ngx_uint_t) range[i].end)
0953             {
0954                 ngx_memmove(&range[i], &range[i + 1],
0955                            (a->nelts - 1 - i) * sizeof(ngx_stream_geo_range_t));
0956 
0957                 a->nelts--;
0958 
0959                 break;
0960             }
0961 
0962             if (i == a->nelts - 1) {
0963                 warn = 1;
0964             }
0965         }
0966 
0967     next:
0968 
0969         if (h == 0xffff) {
0970             break;
0971         }
0972     }
0973 
0974     return warn;
0975 }
0976 
0977 
0978 static char *
0979 ngx_stream_geo_cidr(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
0980     ngx_str_t *value)
0981 {
0982     char        *rv;
0983     ngx_int_t    rc, del;
0984     ngx_str_t   *net;
0985     ngx_cidr_t   cidr;
0986 
0987     if (ctx->tree == NULL) {
0988         ctx->tree = ngx_radix_tree_create(ctx->pool, -1);
0989         if (ctx->tree == NULL) {
0990             return NGX_CONF_ERROR;
0991         }
0992     }
0993 
0994 #if (NGX_HAVE_INET6)
0995     if (ctx->tree6 == NULL) {
0996         ctx->tree6 = ngx_radix_tree_create(ctx->pool, -1);
0997         if (ctx->tree6 == NULL) {
0998             return NGX_CONF_ERROR;
0999         }
1000     }
1001 #endif
1002 
1003     if (ngx_strcmp(value[0].data, "default") == 0) {
1004         cidr.family = AF_INET;
1005         cidr.u.in.addr = 0;
1006         cidr.u.in.mask = 0;
1007 
1008         rv = ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]);
1009 
1010         if (rv != NGX_CONF_OK) {
1011             return rv;
1012         }
1013 
1014 #if (NGX_HAVE_INET6)
1015         cidr.family = AF_INET6;
1016         ngx_memzero(&cidr.u.in6, sizeof(ngx_in6_cidr_t));
1017 
1018         rv = ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]);
1019 
1020         if (rv != NGX_CONF_OK) {
1021             return rv;
1022         }
1023 #endif
1024 
1025         return NGX_CONF_OK;
1026     }
1027 
1028     if (ngx_strcmp(value[0].data, "delete") == 0) {
1029         net = &value[1];
1030         del = 1;
1031 
1032     } else {
1033         net = &value[0];
1034         del = 0;
1035     }
1036 
1037     if (ngx_stream_geo_cidr_value(cf, net, &cidr) != NGX_OK) {
1038         return NGX_CONF_ERROR;
1039     }
1040 
1041     if (cidr.family == AF_INET) {
1042         cidr.u.in.addr = ntohl(cidr.u.in.addr);
1043         cidr.u.in.mask = ntohl(cidr.u.in.mask);
1044     }
1045 
1046     if (del) {
1047         switch (cidr.family) {
1048 
1049 #if (NGX_HAVE_INET6)
1050         case AF_INET6:
1051             rc = ngx_radix128tree_delete(ctx->tree6,
1052                                          cidr.u.in6.addr.s6_addr,
1053                                          cidr.u.in6.mask.s6_addr);
1054             break;
1055 #endif
1056 
1057         default: /* AF_INET */
1058             rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr,
1059                                         cidr.u.in.mask);
1060             break;
1061         }
1062 
1063         if (rc != NGX_OK) {
1064             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1065                                "no network \"%V\" to delete", net);
1066         }
1067 
1068         return NGX_CONF_OK;
1069     }
1070 
1071     return ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], net);
1072 }
1073 
1074 
1075 static char *
1076 ngx_stream_geo_cidr_add(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
1077     ngx_cidr_t *cidr, ngx_str_t *value, ngx_str_t *net)
1078 {
1079     ngx_int_t                     rc;
1080     ngx_stream_variable_value_t  *val, *old;
1081 
1082     val = ngx_stream_geo_value(cf, ctx, value);
1083 
1084     if (val == NULL) {
1085         return NGX_CONF_ERROR;
1086     }
1087 
1088     switch (cidr->family) {
1089 
1090 #if (NGX_HAVE_INET6)
1091     case AF_INET6:
1092         rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr,
1093                                      cidr->u.in6.mask.s6_addr,
1094                                      (uintptr_t) val);
1095 
1096         if (rc == NGX_OK) {
1097             return NGX_CONF_OK;
1098         }
1099 
1100         if (rc == NGX_ERROR) {
1101             return NGX_CONF_ERROR;
1102         }
1103 
1104         /* rc == NGX_BUSY */
1105 
1106         old = (ngx_stream_variable_value_t *)
1107                    ngx_radix128tree_find(ctx->tree6,
1108                                          cidr->u.in6.addr.s6_addr);
1109 
1110         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1111               "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
1112               net, val, old);
1113 
1114         rc = ngx_radix128tree_delete(ctx->tree6,
1115                                      cidr->u.in6.addr.s6_addr,
1116                                      cidr->u.in6.mask.s6_addr);
1117 
1118         if (rc == NGX_ERROR) {
1119             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
1120             return NGX_CONF_ERROR;
1121         }
1122 
1123         rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr,
1124                                      cidr->u.in6.mask.s6_addr,
1125                                      (uintptr_t) val);
1126 
1127         break;
1128 #endif
1129 
1130     default: /* AF_INET */
1131         rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr,
1132                                     cidr->u.in.mask, (uintptr_t) val);
1133 
1134         if (rc == NGX_OK) {
1135             return NGX_CONF_OK;
1136         }
1137 
1138         if (rc == NGX_ERROR) {
1139             return NGX_CONF_ERROR;
1140         }
1141 
1142         /* rc == NGX_BUSY */
1143 
1144         old = (ngx_stream_variable_value_t *)
1145                    ngx_radix32tree_find(ctx->tree, cidr->u.in.addr);
1146 
1147         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1148               "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
1149               net, val, old);
1150 
1151         rc = ngx_radix32tree_delete(ctx->tree,
1152                                     cidr->u.in.addr, cidr->u.in.mask);
1153 
1154         if (rc == NGX_ERROR) {
1155             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
1156             return NGX_CONF_ERROR;
1157         }
1158 
1159         rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr,
1160                                     cidr->u.in.mask, (uintptr_t) val);
1161 
1162         break;
1163     }
1164 
1165     if (rc == NGX_OK) {
1166         return NGX_CONF_OK;
1167     }
1168 
1169     return NGX_CONF_ERROR;
1170 }
1171 
1172 
1173 static ngx_stream_variable_value_t *
1174 ngx_stream_geo_value(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
1175     ngx_str_t *value)
1176 {
1177     uint32_t                               hash;
1178     ngx_stream_variable_value_t           *val;
1179     ngx_stream_geo_variable_value_node_t  *gvvn;
1180 
1181     hash = ngx_crc32_long(value->data, value->len);
1182 
1183     gvvn = (ngx_stream_geo_variable_value_node_t *)
1184                ngx_str_rbtree_lookup(&ctx->rbtree, value, hash);
1185 
1186     if (gvvn) {
1187         return gvvn->value;
1188     }
1189 
1190     val = ngx_palloc(ctx->pool, sizeof(ngx_stream_variable_value_t));
1191     if (val == NULL) {
1192         return NULL;
1193     }
1194 
1195     val->len = value->len;
1196     val->data = ngx_pstrdup(ctx->pool, value);
1197     if (val->data == NULL) {
1198         return NULL;
1199     }
1200 
1201     val->valid = 1;
1202     val->no_cacheable = 0;
1203     val->not_found = 0;
1204 
1205     gvvn = ngx_palloc(ctx->temp_pool,
1206                       sizeof(ngx_stream_geo_variable_value_node_t));
1207     if (gvvn == NULL) {
1208         return NULL;
1209     }
1210 
1211     gvvn->sn.node.key = hash;
1212     gvvn->sn.str.len = val->len;
1213     gvvn->sn.str.data = val->data;
1214     gvvn->value = val;
1215     gvvn->offset = 0;
1216 
1217     ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node);
1218 
1219     ctx->data_size += ngx_align(sizeof(ngx_stream_variable_value_t)
1220                                 + value->len, sizeof(void *));
1221 
1222     return val;
1223 }
1224 
1225 
1226 static ngx_int_t
1227 ngx_stream_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
1228 {
1229     ngx_int_t  rc;
1230 
1231     if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
1232         cidr->family = AF_INET;
1233         cidr->u.in.addr = 0xffffffff;
1234         cidr->u.in.mask = 0xffffffff;
1235 
1236         return NGX_OK;
1237     }
1238 
1239     rc = ngx_ptocidr(net, cidr);
1240 
1241     if (rc == NGX_ERROR) {
1242         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net);
1243         return NGX_ERROR;
1244     }
1245 
1246     if (rc == NGX_DONE) {
1247         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1248                            "low address bits of %V are meaningless", net);
1249     }
1250 
1251     return NGX_OK;
1252 }
1253 
1254 
1255 static char *
1256 ngx_stream_geo_include(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx,
1257     ngx_str_t *name)
1258 {
1259     char       *rv;
1260     ngx_str_t   file;
1261 
1262     file.len = name->len + 4;
1263     file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5);
1264     if (file.data == NULL) {
1265         return NGX_CONF_ERROR;
1266     }
1267 
1268     ngx_sprintf(file.data, "%V.bin%Z", name);
1269 
1270     if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
1271         return NGX_CONF_ERROR;
1272     }
1273 
1274     if (ctx->ranges) {
1275         ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
1276 
1277         switch (ngx_stream_geo_include_binary_base(cf, ctx, &file)) {
1278         case NGX_OK:
1279             return NGX_CONF_OK;
1280         case NGX_ERROR:
1281             return NGX_CONF_ERROR;
1282         default:
1283             break;
1284         }
1285     }
1286 
1287     file.len -= 4;
1288     file.data[file.len] = '\0';
1289 
1290     ctx->include_name = file;
1291 
1292     if (ctx->outside_entries) {
1293         ctx->allow_binary_include = 0;
1294     }
1295 
1296     ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
1297 
1298     rv = ngx_conf_parse(cf, &file);
1299 
1300     ctx->includes++;
1301     ctx->outside_entries = 0;
1302 
1303     return rv;
1304 }
1305 
1306 
1307 static ngx_int_t
1308 ngx_stream_geo_include_binary_base(ngx_conf_t *cf,
1309     ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name)
1310 {
1311     u_char                       *base, ch;
1312     time_t                        mtime;
1313     size_t                        size, len;
1314     ssize_t                       n;
1315     uint32_t                      crc32;
1316     ngx_err_t                     err;
1317     ngx_int_t                     rc;
1318     ngx_uint_t                    i;
1319     ngx_file_t                    file;
1320     ngx_file_info_t               fi;
1321     ngx_stream_geo_range_t       *range, **ranges;
1322     ngx_stream_geo_header_t      *header;
1323     ngx_stream_variable_value_t  *vv;
1324 
1325     ngx_memzero(&file, sizeof(ngx_file_t));
1326     file.name = *name;
1327     file.log = cf->log;
1328 
1329     file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0);
1330     if (file.fd == NGX_INVALID_FILE) {
1331         err = ngx_errno;
1332         if (err != NGX_ENOENT) {
1333             ngx_conf_log_error(NGX_LOG_CRIT, cf, err,
1334                                ngx_open_file_n " \"%s\" failed", name->data);
1335         }
1336         return NGX_DECLINED;
1337     }
1338 
1339     if (ctx->outside_entries) {
1340         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1341             "binary geo range base \"%s\" cannot be mixed with usual entries",
1342             name->data);
1343         rc = NGX_ERROR;
1344         goto done;
1345     }
1346 
1347     if (ctx->binary_include) {
1348         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1349             "second binary geo range base \"%s\" cannot be mixed with \"%s\"",
1350             name->data, ctx->include_name.data);
1351         rc = NGX_ERROR;
1352         goto done;
1353     }
1354 
1355     if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) {
1356         ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
1357                            ngx_fd_info_n " \"%s\" failed", name->data);
1358         goto failed;
1359     }
1360 
1361     size = (size_t) ngx_file_size(&fi);
1362     mtime = ngx_file_mtime(&fi);
1363 
1364     ch = name->data[name->len - 4];
1365     name->data[name->len - 4] = '\0';
1366 
1367     if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) {
1368         ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
1369                            ngx_file_info_n " \"%s\" failed", name->data);
1370         goto failed;
1371     }
1372 
1373     name->data[name->len - 4] = ch;
1374 
1375     if (mtime < ngx_file_mtime(&fi)) {
1376         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1377                            "stale binary geo range base \"%s\"", name->data);
1378         goto failed;
1379     }
1380 
1381     base = ngx_palloc(ctx->pool, size);
1382     if (base == NULL) {
1383         goto failed;
1384     }
1385 
1386     n = ngx_read_file(&file, base, size, 0);
1387 
1388     if (n == NGX_ERROR) {
1389         ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
1390                            ngx_read_file_n " \"%s\" failed", name->data);
1391         goto failed;
1392     }
1393 
1394     if ((size_t) n != size) {
1395         ngx_conf_log_error(NGX_LOG_CRIT, cf, 0,
1396             ngx_read_file_n " \"%s\" returned only %z bytes instead of %z",
1397             name->data, n, size);
1398         goto failed;
1399     }
1400 
1401     header = (ngx_stream_geo_header_t *) base;
1402 
1403     if (size < 16 || ngx_memcmp(&ngx_stream_geo_header, header, 12) != 0) {
1404         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1405              "incompatible binary geo range base \"%s\"", name->data);
1406         goto failed;
1407     }
1408 
1409     ngx_crc32_init(crc32);
1410 
1411     vv = (ngx_stream_variable_value_t *)
1412             (base + sizeof(ngx_stream_geo_header_t));
1413 
1414     while (vv->data) {
1415         len = ngx_align(sizeof(ngx_stream_variable_value_t) + vv->len,
1416                         sizeof(void *));
1417         ngx_crc32_update(&crc32, (u_char *) vv, len);
1418         vv->data += (size_t) base;
1419         vv = (ngx_stream_variable_value_t *) ((u_char *) vv + len);
1420     }
1421     ngx_crc32_update(&crc32, (u_char *) vv,
1422                      sizeof(ngx_stream_variable_value_t));
1423     vv++;
1424 
1425     ranges = (ngx_stream_geo_range_t **) vv;
1426 
1427     for (i = 0; i < 0x10000; i++) {
1428         ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *));
1429         if (ranges[i]) {
1430             ranges[i] = (ngx_stream_geo_range_t *)
1431                             ((u_char *) ranges[i] + (size_t) base);
1432         }
1433     }
1434 
1435     range = (ngx_stream_geo_range_t *) &ranges[0x10000];
1436 
1437     while ((u_char *) range < base + size) {
1438         while (range->value) {
1439             ngx_crc32_update(&crc32, (u_char *) range,
1440                              sizeof(ngx_stream_geo_range_t));
1441             range->value = (ngx_stream_variable_value_t *)
1442                                ((u_char *) range->value + (size_t) base);
1443             range++;
1444         }
1445         ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *));
1446         range = (ngx_stream_geo_range_t *) ((u_char *) range + sizeof(void *));
1447     }
1448 
1449     ngx_crc32_final(crc32);
1450 
1451     if (crc32 != header->crc32) {
1452         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1453                   "CRC32 mismatch in binary geo range base \"%s\"", name->data);
1454         goto failed;
1455     }
1456 
1457     ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0,
1458                        "using binary geo range base \"%s\"", name->data);
1459 
1460     ctx->include_name = *name;
1461     ctx->binary_include = 1;
1462     ctx->high.low = ranges;
1463     rc = NGX_OK;
1464 
1465     goto done;
1466 
1467 failed:
1468 
1469     rc = NGX_DECLINED;
1470 
1471 done:
1472 
1473     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1474         ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
1475                       ngx_close_file_n " \"%s\" failed", name->data);
1476     }
1477 
1478     return rc;
1479 }
1480 
1481 
1482 static void
1483 ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx)
1484 {
1485     u_char                                *p;
1486     uint32_t                               hash;
1487     ngx_str_t                              s;
1488     ngx_uint_t                             i;
1489     ngx_file_mapping_t                     fm;
1490     ngx_stream_geo_range_t                *r, *range, **ranges;
1491     ngx_stream_geo_header_t               *header;
1492     ngx_stream_geo_variable_value_node_t  *gvvn;
1493 
1494     fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5);
1495     if (fm.name == NULL) {
1496         return;
1497     }
1498 
1499     ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name);
1500 
1501     fm.size = ctx->data_size;
1502     fm.log = ctx->pool->log;
1503 
1504     ngx_log_error(NGX_LOG_NOTICE, fm.log, 0,
1505                   "creating binary geo range base \"%s\"", fm.name);
1506 
1507     if (ngx_create_file_mapping(&fm) != NGX_OK) {
1508         return;
1509     }
1510 
1511     p = ngx_cpymem(fm.addr, &ngx_stream_geo_header,
1512                    sizeof(ngx_stream_geo_header_t));
1513 
1514     p = ngx_stream_geo_copy_values(fm.addr, p, ctx->rbtree.root,
1515                                    ctx->rbtree.sentinel);
1516 
1517     p += sizeof(ngx_stream_variable_value_t);
1518 
1519     ranges = (ngx_stream_geo_range_t **) p;
1520 
1521     p += 0x10000 * sizeof(ngx_stream_geo_range_t *);
1522 
1523     for (i = 0; i < 0x10000; i++) {
1524         r = ctx->high.low[i];
1525         if (r == NULL) {
1526             continue;
1527         }
1528 
1529         range = (ngx_stream_geo_range_t *) p;
1530         ranges[i] = (ngx_stream_geo_range_t *) (p - (u_char *) fm.addr);
1531 
1532         do {
1533             s.len = r->value->len;
1534             s.data = r->value->data;
1535             hash = ngx_crc32_long(s.data, s.len);
1536             gvvn = (ngx_stream_geo_variable_value_node_t *)
1537                         ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash);
1538 
1539             range->value = (ngx_stream_variable_value_t *) gvvn->offset;
1540             range->start = r->start;
1541             range->end = r->end;
1542             range++;
1543 
1544         } while ((++r)->value);
1545 
1546         range->value = NULL;
1547 
1548         p = (u_char *) range + sizeof(void *);
1549     }
1550 
1551     header = fm.addr;
1552     header->crc32 = ngx_crc32_long((u_char *) fm.addr
1553                                        + sizeof(ngx_stream_geo_header_t),
1554                                    fm.size - sizeof(ngx_stream_geo_header_t));
1555 
1556     ngx_close_file_mapping(&fm);
1557 }
1558 
1559 
1560 static u_char *
1561 ngx_stream_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node,
1562     ngx_rbtree_node_t *sentinel)
1563 {
1564     ngx_stream_variable_value_t           *vv;
1565     ngx_stream_geo_variable_value_node_t  *gvvn;
1566 
1567     if (node == sentinel) {
1568         return p;
1569     }
1570 
1571     gvvn = (ngx_stream_geo_variable_value_node_t *) node;
1572     gvvn->offset = p - base;
1573 
1574     vv = (ngx_stream_variable_value_t *) p;
1575     *vv = *gvvn->value;
1576     p += sizeof(ngx_stream_variable_value_t);
1577     vv->data = (u_char *) (p - base);
1578 
1579     p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len);
1580 
1581     p = ngx_align_ptr(p, sizeof(void *));
1582 
1583     p = ngx_stream_geo_copy_values(base, p, node->left, sentinel);
1584 
1585     return ngx_stream_geo_copy_values(base, p, node->right, sentinel);
1586 }