34
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
34
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 typedef struct {
|
582
|
14 ngx_http_variable_value_t *value;
|
430
|
15 u_short start;
|
|
16 u_short end;
|
422
|
17 } ngx_http_geo_range_t;
|
|
18
|
|
19
|
|
20 typedef struct {
|
582
|
21 ngx_http_geo_range_t **low;
|
430
|
22 ngx_http_variable_value_t *default_value;
|
422
|
23 } ngx_http_geo_high_ranges_t;
|
|
24
|
|
25
|
|
26 typedef struct {
|
582
|
27 ngx_str_node_t sn;
|
|
28 ngx_http_variable_value_t *value;
|
|
29 size_t offset;
|
|
30 } ngx_http_geo_variable_value_node_t;
|
|
31
|
|
32
|
|
33 typedef struct {
|
430
|
34 ngx_http_variable_value_t *value;
|
|
35 ngx_str_t *net;
|
582
|
36 ngx_http_geo_high_ranges_t high;
|
430
|
37 ngx_radix_tree_t *tree;
|
|
38 ngx_rbtree_t rbtree;
|
|
39 ngx_rbtree_node_t sentinel;
|
510
|
40 ngx_array_t *proxies;
|
430
|
41 ngx_pool_t *pool;
|
|
42 ngx_pool_t *temp_pool;
|
582
|
43
|
|
44 size_t data_size;
|
|
45
|
|
46 ngx_str_t include_name;
|
|
47 ngx_uint_t includes;
|
|
48 ngx_uint_t entries;
|
|
49
|
|
50 unsigned ranges:1;
|
|
51 unsigned outside_entries:1;
|
|
52 unsigned allow_binary_include:1;
|
|
53 unsigned binary_include:1;
|
674
|
54 unsigned proxy_recursive:1;
|
138
|
55 } ngx_http_geo_conf_ctx_t;
|
34
|
56
|
|
57
|
430
|
58 typedef struct {
|
|
59 union {
|
|
60 ngx_radix_tree_t *tree;
|
582
|
61 ngx_http_geo_high_ranges_t high;
|
430
|
62 } u;
|
|
63
|
510
|
64 ngx_array_t *proxies;
|
674
|
65 unsigned proxy_recursive:1;
|
510
|
66
|
430
|
67 ngx_int_t index;
|
|
68 } ngx_http_geo_ctx_t;
|
|
69
|
|
70
|
|
71 static in_addr_t ngx_http_geo_addr(ngx_http_request_t *r,
|
|
72 ngx_http_geo_ctx_t *ctx);
|
674
|
73 static ngx_int_t ngx_http_geo_real_addr(ngx_http_request_t *r,
|
|
74 ngx_http_geo_ctx_t *ctx, ngx_addr_t *addr);
|
34
|
75 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
76 static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
|
422
|
77 static char *ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
78 ngx_str_t *value);
|
|
79 static char *ngx_http_geo_add_range(ngx_conf_t *cf,
|
|
80 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
81 static ngx_uint_t ngx_http_geo_delete_range(ngx_conf_t *cf,
|
|
82 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
83 static char *ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
84 ngx_str_t *value);
|
|
85 static ngx_http_variable_value_t *ngx_http_geo_value(ngx_conf_t *cf,
|
|
86 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *value);
|
510
|
87 static char *ngx_http_geo_add_proxy(ngx_conf_t *cf,
|
|
88 ngx_http_geo_conf_ctx_t *ctx, ngx_cidr_t *cidr);
|
|
89 static ngx_int_t ngx_http_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
|
|
90 ngx_cidr_t *cidr);
|
582
|
91 static char *ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
92 ngx_str_t *name);
|
|
93 static ngx_int_t ngx_http_geo_include_binary_base(ngx_conf_t *cf,
|
|
94 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *name);
|
|
95 static void ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx);
|
|
96 static u_char *ngx_http_geo_copy_values(u_char *base, u_char *p,
|
|
97 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
34
|
98
|
|
99
|
|
100 static ngx_command_t ngx_http_geo_commands[] = {
|
|
101
|
|
102 { ngx_string("geo"),
|
430
|
103 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
|
34
|
104 ngx_http_geo_block,
|
|
105 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
106 0,
|
|
107 NULL },
|
|
108
|
|
109 ngx_null_command
|
|
110 };
|
|
111
|
|
112
|
|
113 static ngx_http_module_t ngx_http_geo_module_ctx = {
|
58
|
114 NULL, /* preconfiguration */
|
|
115 NULL, /* postconfiguration */
|
34
|
116
|
|
117 NULL, /* create main configuration */
|
|
118 NULL, /* init main configuration */
|
|
119
|
|
120 NULL, /* create server configuration */
|
|
121 NULL, /* merge server configuration */
|
|
122
|
|
123 NULL, /* create location configuration */
|
|
124 NULL /* merge location configuration */
|
|
125 };
|
|
126
|
|
127
|
|
128 ngx_module_t ngx_http_geo_module = {
|
58
|
129 NGX_MODULE_V1,
|
34
|
130 &ngx_http_geo_module_ctx, /* module context */
|
|
131 ngx_http_geo_commands, /* module directives */
|
|
132 NGX_HTTP_MODULE, /* module type */
|
90
|
133 NULL, /* init master */
|
34
|
134 NULL, /* init module */
|
90
|
135 NULL, /* init process */
|
|
136 NULL, /* init thread */
|
|
137 NULL, /* exit thread */
|
|
138 NULL, /* exit process */
|
|
139 NULL, /* exit master */
|
|
140 NGX_MODULE_V1_PADDING
|
34
|
141 };
|
|
142
|
|
143
|
582
|
144 typedef struct {
|
|
145 u_char GEORNG[6];
|
|
146 u_char version;
|
|
147 u_char ptr_size;
|
678
|
148 uint32_t endianness;
|
582
|
149 uint32_t crc32;
|
|
150 } ngx_http_geo_header_t;
|
|
151
|
|
152
|
|
153 static ngx_http_geo_header_t ngx_http_geo_header = {
|
|
154 { 'G', 'E', 'O', 'R', 'N', 'G' }, 0, sizeof(void *), 0x12345678, 0
|
|
155 };
|
|
156
|
|
157
|
34
|
158 /* AF_INET only */
|
|
159
|
122
|
160 static ngx_int_t
|
422
|
161 ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
122
|
162 uintptr_t data)
|
34
|
163 {
|
430
|
164 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data;
|
34
|
165
|
122
|
166 ngx_http_variable_value_t *vv;
|
34
|
167
|
122
|
168 vv = (ngx_http_variable_value_t *)
|
430
|
169 ngx_radix32tree_find(ctx->u.tree, ngx_http_geo_addr(r, ctx));
|
38
|
170
|
122
|
171 *v = *vv;
|
|
172
|
430
|
173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
174 "http geo: %v", v);
|
38
|
175
|
122
|
176 return NGX_OK;
|
34
|
177 }
|
|
178
|
|
179
|
422
|
180 static ngx_int_t
|
|
181 ngx_http_geo_range_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
182 uintptr_t data)
|
|
183 {
|
430
|
184 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data;
|
422
|
185
|
|
186 in_addr_t addr;
|
582
|
187 ngx_uint_t n;
|
422
|
188 ngx_http_geo_range_t *range;
|
|
189
|
582
|
190 *v = *ctx->u.high.default_value;
|
422
|
191
|
430
|
192 addr = ngx_http_geo_addr(r, ctx);
|
422
|
193
|
582
|
194 range = ctx->u.high.low[addr >> 16];
|
422
|
195
|
582
|
196 if (range) {
|
|
197 n = addr & 0xffff;
|
|
198 do {
|
|
199 if (n >= (ngx_uint_t) range->start && n <= (ngx_uint_t) range->end)
|
|
200 {
|
|
201 *v = *range->value;
|
|
202 break;
|
|
203 }
|
|
204 } while ((++range)->value);
|
422
|
205 }
|
|
206
|
430
|
207 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
208 "http geo: %v", v);
|
422
|
209
|
|
210 return NGX_OK;
|
|
211 }
|
|
212
|
|
213
|
430
|
214 static in_addr_t
|
|
215 ngx_http_geo_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx)
|
|
216 {
|
674
|
217 ngx_addr_t addr;
|
|
218 ngx_table_elt_t *xfwd;
|
|
219 struct sockaddr_in *sin;
|
510
|
220
|
674
|
221 if (ngx_http_geo_real_addr(r, ctx, &addr) != NGX_OK) {
|
|
222 return INADDR_NONE;
|
|
223 }
|
510
|
224
|
|
225 xfwd = r->headers_in.x_forwarded_for;
|
|
226
|
674
|
227 if (xfwd != NULL && ctx->proxies != NULL) {
|
|
228 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data,
|
|
229 xfwd->value.len, ctx->proxies,
|
|
230 ctx->proxy_recursive);
|
510
|
231 }
|
|
232
|
674
|
233 #if (NGX_HAVE_INET6)
|
510
|
234
|
674
|
235 if (addr.sockaddr->sa_family == AF_INET6) {
|
|
236 struct in6_addr *inaddr6;
|
510
|
237
|
674
|
238 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr;
|
510
|
239
|
674
|
240 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
|
|
241 return ntohl(*(in_addr_t *) &inaddr6->s6_addr[12]);
|
510
|
242 }
|
|
243 }
|
|
244
|
674
|
245 #endif
|
|
246
|
|
247 if (addr.sockaddr->sa_family != AF_INET) {
|
|
248 return INADDR_NONE;
|
|
249 }
|
|
250
|
|
251 sin = (struct sockaddr_in *) addr.sockaddr;
|
|
252 return ntohl(sin->sin_addr.s_addr);
|
510
|
253 }
|
|
254
|
|
255
|
674
|
256 static ngx_int_t
|
|
257 ngx_http_geo_real_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx,
|
|
258 ngx_addr_t *addr)
|
510
|
259 {
|
430
|
260 ngx_http_variable_value_t *v;
|
|
261
|
|
262 if (ctx->index == -1) {
|
|
263 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
264 "http geo started: %V", &r->connection->addr_text);
|
|
265
|
674
|
266 addr->sockaddr = r->connection->sockaddr;
|
|
267 addr->socklen = r->connection->socklen;
|
|
268 /* addr->name = r->connection->addr_text; */
|
626
|
269
|
674
|
270 return NGX_OK;
|
430
|
271 }
|
|
272
|
|
273 v = ngx_http_get_flushed_variable(r, ctx->index);
|
|
274
|
|
275 if (v == NULL || v->not_found) {
|
|
276 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
277 "http geo not found");
|
|
278
|
674
|
279 return NGX_ERROR;
|
430
|
280 }
|
|
281
|
|
282 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
283 "http geo started: %v", v);
|
|
284
|
674
|
285 if (ngx_parse_addr(r->pool, addr, v->data, v->len) == NGX_OK) {
|
|
286 return NGX_OK;
|
|
287 }
|
|
288
|
|
289 return NGX_ERROR;
|
430
|
290 }
|
|
291
|
|
292
|
38
|
293 static char *
|
|
294 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
34
|
295 {
|
138
|
296 char *rv;
|
582
|
297 void **p;
|
422
|
298 size_t len;
|
138
|
299 ngx_str_t *value, name;
|
422
|
300 ngx_uint_t i;
|
138
|
301 ngx_conf_t save;
|
|
302 ngx_pool_t *pool;
|
422
|
303 ngx_array_t *a;
|
328
|
304 ngx_http_variable_t *var;
|
430
|
305 ngx_http_geo_ctx_t *geo;
|
138
|
306 ngx_http_geo_conf_ctx_t ctx;
|
34
|
307
|
50
|
308 value = cf->args->elts;
|
|
309
|
430
|
310 geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t));
|
|
311 if (geo == NULL) {
|
|
312 return NGX_CONF_ERROR;
|
|
313 }
|
|
314
|
50
|
315 name = value[1];
|
430
|
316 name.len--;
|
|
317 name.data++;
|
50
|
318
|
430
|
319 if (cf->args->nelts == 3) {
|
|
320
|
|
321 geo->index = ngx_http_get_variable_index(cf, &name);
|
|
322 if (geo->index == NGX_ERROR) {
|
|
323 return NGX_CONF_ERROR;
|
|
324 }
|
|
325
|
|
326 name = value[2];
|
50
|
327 name.len--;
|
|
328 name.data++;
|
430
|
329
|
|
330 } else {
|
|
331 geo->index = -1;
|
50
|
332 }
|
|
333
|
340
|
334 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
50
|
335 if (var == NULL) {
|
34
|
336 return NGX_CONF_ERROR;
|
|
337 }
|
|
338
|
132
|
339 pool = ngx_create_pool(16384, cf->log);
|
50
|
340 if (pool == NULL) {
|
34
|
341 return NGX_CONF_ERROR;
|
|
342 }
|
|
343
|
582
|
344 ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t));
|
|
345
|
422
|
346 ctx.temp_pool = ngx_create_pool(16384, cf->log);
|
|
347 if (ctx.temp_pool == NULL) {
|
34
|
348 return NGX_CONF_ERROR;
|
|
349 }
|
|
350
|
582
|
351 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value);
|
422
|
352
|
138
|
353 ctx.pool = cf->pool;
|
582
|
354 ctx.data_size = sizeof(ngx_http_geo_header_t)
|
|
355 + sizeof(ngx_http_variable_value_t)
|
|
356 + 0x10000 * sizeof(ngx_http_geo_range_t *);
|
|
357 ctx.allow_binary_include = 1;
|
34
|
358
|
|
359 save = *cf;
|
|
360 cf->pool = pool;
|
138
|
361 cf->ctx = &ctx;
|
34
|
362 cf->handler = ngx_http_geo;
|
|
363 cf->handler_conf = conf;
|
|
364
|
|
365 rv = ngx_conf_parse(cf, NULL);
|
|
366
|
|
367 *cf = save;
|
|
368
|
510
|
369 geo->proxies = ctx.proxies;
|
674
|
370 geo->proxy_recursive = ctx.proxy_recursive;
|
510
|
371
|
582
|
372 if (ctx.high.low) {
|
|
373
|
|
374 if (!ctx.binary_include) {
|
|
375 for (i = 0; i < 0x10000; i++) {
|
|
376 a = (ngx_array_t *) ctx.high.low[i];
|
|
377
|
|
378 if (a == NULL || a->nelts == 0) {
|
|
379 continue;
|
|
380 }
|
422
|
381
|
582
|
382 len = a->nelts * sizeof(ngx_http_geo_range_t);
|
422
|
383
|
582
|
384 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *));
|
|
385 if (ctx.high.low[i] == NULL) {
|
|
386 return NGX_CONF_ERROR;
|
|
387 }
|
|
388
|
|
389 p = (void **) ngx_cpymem(ctx.high.low[i], a->elts, len);
|
|
390 *p = NULL;
|
|
391 ctx.data_size += len + sizeof(void *);
|
422
|
392 }
|
|
393
|
582
|
394 if (ctx.allow_binary_include
|
|
395 && !ctx.outside_entries
|
|
396 && ctx.entries > 100000
|
|
397 && ctx.includes == 1)
|
|
398 {
|
|
399 ngx_http_geo_create_binary_base(&ctx);
|
422
|
400 }
|
|
401 }
|
|
402
|
684
|
403 if (ctx.high.default_value == NULL) {
|
|
404 ctx.high.default_value = &ngx_http_variable_null_value;
|
|
405 }
|
|
406
|
430
|
407 geo->u.high = ctx.high;
|
|
408
|
422
|
409 var->get_handler = ngx_http_geo_range_variable;
|
430
|
410 var->data = (uintptr_t) geo;
|
34
|
411
|
422
|
412 ngx_destroy_pool(ctx.temp_pool);
|
|
413 ngx_destroy_pool(pool);
|
|
414
|
|
415 } else {
|
424
|
416 if (ctx.tree == NULL) {
|
|
417 ctx.tree = ngx_radix_tree_create(cf->pool, -1);
|
|
418 if (ctx.tree == NULL) {
|
|
419 return NGX_CONF_ERROR;
|
|
420 }
|
|
421 }
|
|
422
|
430
|
423 geo->u.tree = ctx.tree;
|
|
424
|
422
|
425 var->get_handler = ngx_http_geo_cidr_variable;
|
430
|
426 var->data = (uintptr_t) geo;
|
422
|
427
|
|
428 ngx_destroy_pool(ctx.temp_pool);
|
|
429 ngx_destroy_pool(pool);
|
|
430
|
|
431 if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) {
|
|
432 return rv;
|
|
433 }
|
|
434
|
|
435 if (ngx_radix32tree_insert(ctx.tree, 0, 0,
|
|
436 (uintptr_t) &ngx_http_variable_null_value)
|
|
437 == NGX_ERROR)
|
|
438 {
|
|
439 return NGX_CONF_ERROR;
|
|
440 }
|
34
|
441 }
|
|
442
|
|
443 return rv;
|
|
444 }
|
|
445
|
|
446
|
38
|
447 static char *
|
|
448 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
34
|
449 {
|
422
|
450 char *rv;
|
582
|
451 ngx_str_t *value;
|
510
|
452 ngx_cidr_t cidr;
|
422
|
453 ngx_http_geo_conf_ctx_t *ctx;
|
34
|
454
|
138
|
455 ctx = cf->ctx;
|
34
|
456
|
422
|
457 value = cf->args->elts;
|
|
458
|
|
459 if (cf->args->nelts == 1) {
|
|
460
|
|
461 if (ngx_strcmp(value[0].data, "ranges") == 0) {
|
|
462
|
|
463 if (ctx->tree) {
|
|
464 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
465 "the \"ranges\" directive must be "
|
|
466 "the first directive inside \"geo\" block");
|
|
467 goto failed;
|
|
468 }
|
|
469
|
582
|
470 ctx->ranges = 1;
|
422
|
471
|
|
472 rv = NGX_CONF_OK;
|
|
473
|
|
474 goto done;
|
|
475 }
|
674
|
476
|
|
477 else if (ngx_strcmp(value[0].data, "proxy_recursive") == 0) {
|
|
478 ctx->proxy_recursive = 1;
|
|
479 rv = NGX_CONF_OK;
|
|
480 goto done;
|
|
481 }
|
422
|
482 }
|
|
483
|
34
|
484 if (cf->args->nelts != 2) {
|
|
485 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
486 "invalid number of the geo parameters");
|
422
|
487 goto failed;
|
34
|
488 }
|
|
489
|
422
|
490 if (ngx_strcmp(value[0].data, "include") == 0) {
|
|
491
|
582
|
492 rv = ngx_http_geo_include(cf, ctx, &value[1]);
|
422
|
493
|
|
494 goto done;
|
510
|
495
|
|
496 } else if (ngx_strcmp(value[0].data, "proxy") == 0) {
|
|
497
|
|
498 if (ngx_http_geo_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
|
|
499 goto failed;
|
|
500 }
|
|
501
|
|
502 rv = ngx_http_geo_add_proxy(cf, ctx, &cidr);
|
|
503
|
|
504 goto done;
|
422
|
505 }
|
|
506
|
582
|
507 if (ctx->ranges) {
|
422
|
508 rv = ngx_http_geo_range(cf, ctx, value);
|
|
509
|
|
510 } else {
|
|
511 rv = ngx_http_geo_cidr(cf, ctx, value);
|
|
512 }
|
|
513
|
|
514 done:
|
|
515
|
|
516 ngx_reset_pool(cf->pool);
|
|
517
|
|
518 return rv;
|
|
519
|
|
520 failed:
|
|
521
|
|
522 ngx_reset_pool(cf->pool);
|
|
523
|
|
524 return NGX_CONF_ERROR;
|
|
525 }
|
|
526
|
|
527
|
|
528 static char *
|
|
529 ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
530 ngx_str_t *value)
|
|
531 {
|
582
|
532 u_char *p, *last;
|
|
533 in_addr_t start, end;
|
|
534 ngx_str_t *net;
|
|
535 ngx_uint_t del;
|
422
|
536
|
|
537 if (ngx_strcmp(value[0].data, "default") == 0) {
|
|
538
|
582
|
539 if (ctx->high.default_value) {
|
|
540 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
541 "duplicate default geo range value: \"%V\", old value: \"%v\"",
|
|
542 &value[1], ctx->high.default_value);
|
422
|
543 }
|
|
544
|
582
|
545 ctx->high.default_value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
546 if (ctx->high.default_value == NULL) {
|
|
547 return NGX_CONF_ERROR;
|
422
|
548 }
|
|
549
|
|
550 return NGX_CONF_OK;
|
|
551 }
|
|
552
|
582
|
553 if (ctx->binary_include) {
|
|
554 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
555 "binary geo range base \"%s\" cannot be mixed with usual entries",
|
582
|
556 ctx->include_name.data);
|
|
557 return NGX_CONF_ERROR;
|
|
558 }
|
|
559
|
|
560 if (ctx->high.low == NULL) {
|
|
561 ctx->high.low = ngx_pcalloc(ctx->pool,
|
|
562 0x10000 * sizeof(ngx_http_geo_range_t *));
|
|
563 if (ctx->high.low == NULL) {
|
|
564 return NGX_CONF_ERROR;
|
|
565 }
|
|
566 }
|
|
567
|
|
568 ctx->entries++;
|
|
569 ctx->outside_entries = 1;
|
|
570
|
422
|
571 if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
572 net = &value[1];
|
|
573 del = 1;
|
|
574
|
|
575 } else {
|
|
576 net = &value[0];
|
|
577 del = 0;
|
|
578 }
|
|
579
|
|
580 last = net->data + net->len;
|
|
581
|
|
582 p = ngx_strlchr(net->data, last, '-');
|
|
583
|
|
584 if (p == NULL) {
|
|
585 goto invalid;
|
|
586 }
|
|
587
|
|
588 start = ngx_inet_addr(net->data, p - net->data);
|
|
589
|
|
590 if (start == INADDR_NONE) {
|
|
591 goto invalid;
|
|
592 }
|
|
593
|
|
594 start = ntohl(start);
|
|
595
|
|
596 p++;
|
|
597
|
|
598 end = ngx_inet_addr(p, last - p);
|
|
599
|
|
600 if (end == INADDR_NONE) {
|
|
601 goto invalid;
|
|
602 }
|
|
603
|
|
604 end = ntohl(end);
|
|
605
|
|
606 if (start > end) {
|
|
607 goto invalid;
|
|
608 }
|
|
609
|
|
610 if (del) {
|
|
611 if (ngx_http_geo_delete_range(cf, ctx, start, end)) {
|
|
612 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
613 "no address range \"%V\" to delete", net);
|
|
614 }
|
|
615
|
|
616 return NGX_CONF_OK;
|
|
617 }
|
|
618
|
|
619 ctx->value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
620
|
|
621 if (ctx->value == NULL) {
|
|
622 return NGX_CONF_ERROR;
|
|
623 }
|
|
624
|
|
625 ctx->net = net;
|
|
626
|
|
627 return ngx_http_geo_add_range(cf, ctx, start, end);
|
|
628
|
|
629 invalid:
|
|
630
|
|
631 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net);
|
|
632
|
|
633 return NGX_CONF_ERROR;
|
|
634 }
|
|
635
|
|
636
|
|
637 /* the add procedure is optimized to add a growing up sequence */
|
|
638
|
|
639 static char *
|
|
640 ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
641 in_addr_t start, in_addr_t end)
|
|
642 {
|
|
643 in_addr_t n;
|
|
644 ngx_uint_t h, i, s, e;
|
|
645 ngx_array_t *a;
|
|
646 ngx_http_geo_range_t *range;
|
|
647
|
564
|
648 for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
|
422
|
649
|
|
650 h = n >> 16;
|
438
|
651
|
|
652 if (n == start) {
|
|
653 s = n & 0xffff;
|
|
654 } else {
|
|
655 s = 0;
|
|
656 }
|
422
|
657
|
|
658 if ((n | 0xffff) > end) {
|
|
659 e = end & 0xffff;
|
|
660
|
|
661 } else {
|
|
662 e = 0xffff;
|
|
663 }
|
|
664
|
582
|
665 a = (ngx_array_t *) ctx->high.low[h];
|
422
|
666
|
|
667 if (a == NULL) {
|
|
668 a = ngx_array_create(ctx->temp_pool, 64,
|
|
669 sizeof(ngx_http_geo_range_t));
|
|
670 if (a == NULL) {
|
|
671 return NGX_CONF_ERROR;
|
|
672 }
|
|
673
|
582
|
674 ctx->high.low[h] = (ngx_http_geo_range_t *) a;
|
422
|
675 }
|
|
676
|
|
677 i = a->nelts;
|
|
678 range = a->elts;
|
|
679
|
|
680 while (i) {
|
|
681
|
|
682 i--;
|
|
683
|
|
684 if (e < (ngx_uint_t) range[i].start) {
|
|
685 continue;
|
|
686 }
|
|
687
|
|
688 if (s > (ngx_uint_t) range[i].end) {
|
|
689
|
|
690 /* add after the range */
|
|
691
|
|
692 range = ngx_array_push(a);
|
|
693 if (range == NULL) {
|
|
694 return NGX_CONF_ERROR;
|
|
695 }
|
|
696
|
|
697 range = a->elts;
|
|
698
|
620
|
699 ngx_memmove(&range[i + 2], &range[i + 1],
|
422
|
700 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
|
|
701
|
438
|
702 range[i + 1].start = (u_short) s;
|
|
703 range[i + 1].end = (u_short) e;
|
|
704 range[i + 1].value = ctx->value;
|
422
|
705
|
|
706 goto next;
|
|
707 }
|
|
708
|
|
709 if (s == (ngx_uint_t) range[i].start
|
|
710 && e == (ngx_uint_t) range[i].end)
|
|
711 {
|
|
712 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
713 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
|
|
714 ctx->net, ctx->value, range[i].value);
|
438
|
715
|
|
716 range[i].value = ctx->value;
|
|
717
|
|
718 goto next;
|
|
719 }
|
|
720
|
|
721 if (s > (ngx_uint_t) range[i].start
|
|
722 && e < (ngx_uint_t) range[i].end)
|
|
723 {
|
|
724 /* split the range and insert the new one */
|
|
725
|
|
726 range = ngx_array_push(a);
|
|
727 if (range == NULL) {
|
|
728 return NGX_CONF_ERROR;
|
|
729 }
|
|
730
|
|
731 range = ngx_array_push(a);
|
|
732 if (range == NULL) {
|
|
733 return NGX_CONF_ERROR;
|
|
734 }
|
|
735
|
|
736 range = a->elts;
|
|
737
|
620
|
738 ngx_memmove(&range[i + 3], &range[i + 1],
|
438
|
739 (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));
|
|
740
|
|
741 range[i + 2].start = (u_short) (e + 1);
|
|
742 range[i + 2].end = range[i].end;
|
|
743 range[i + 2].value = range[i].value;
|
|
744
|
|
745 range[i + 1].start = (u_short) s;
|
|
746 range[i + 1].end = (u_short) e;
|
|
747 range[i + 1].value = ctx->value;
|
|
748
|
|
749 range[i].end = (u_short) (s - 1);
|
|
750
|
|
751 goto next;
|
422
|
752 }
|
|
753
|
438
|
754 if (s == (ngx_uint_t) range[i].start
|
|
755 && e < (ngx_uint_t) range[i].end)
|
|
756 {
|
|
757 /* shift the range start and insert the new range */
|
|
758
|
|
759 range = ngx_array_push(a);
|
|
760 if (range == NULL) {
|
|
761 return NGX_CONF_ERROR;
|
|
762 }
|
|
763
|
|
764 range = a->elts;
|
|
765
|
620
|
766 ngx_memmove(&range[i + 1], &range[i],
|
440
|
767 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
|
438
|
768
|
|
769 range[i + 1].start = (u_short) (e + 1);
|
|
770
|
|
771 range[i].start = (u_short) s;
|
|
772 range[i].end = (u_short) e;
|
|
773 range[i].value = ctx->value;
|
|
774
|
|
775 goto next;
|
|
776 }
|
|
777
|
|
778 if (s > (ngx_uint_t) range[i].start
|
|
779 && e == (ngx_uint_t) range[i].end)
|
|
780 {
|
|
781 /* shift the range end and insert the new range */
|
|
782
|
|
783 range = ngx_array_push(a);
|
|
784 if (range == NULL) {
|
|
785 return NGX_CONF_ERROR;
|
|
786 }
|
|
787
|
|
788 range = a->elts;
|
|
789
|
620
|
790 ngx_memmove(&range[i + 2], &range[i + 1],
|
438
|
791 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
|
|
792
|
|
793 range[i + 1].start = (u_short) s;
|
|
794 range[i + 1].end = (u_short) e;
|
|
795 range[i + 1].value = ctx->value;
|
|
796
|
|
797 range[i].end = (u_short) (s - 1);
|
|
798
|
|
799 goto next;
|
|
800 }
|
|
801
|
|
802 s = (ngx_uint_t) range[i].start;
|
|
803 e = (ngx_uint_t) range[i].end;
|
|
804
|
422
|
805 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
438
|
806 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"",
|
|
807 ctx->net,
|
|
808 h >> 8, h & 0xff, s >> 8, s & 0xff,
|
|
809 h >> 8, h & 0xff, e >> 8, e & 0xff);
|
422
|
810
|
|
811 return NGX_CONF_ERROR;
|
|
812 }
|
|
813
|
|
814 /* add the first range */
|
|
815
|
|
816 range = ngx_array_push(a);
|
|
817 if (range == NULL) {
|
|
818 return NGX_CONF_ERROR;
|
|
819 }
|
|
820
|
|
821 range->start = (u_short) s;
|
|
822 range->end = (u_short) e;
|
|
823 range->value = ctx->value;
|
438
|
824
|
|
825 next:
|
|
826
|
|
827 continue;
|
422
|
828 }
|
|
829
|
|
830 return NGX_CONF_OK;
|
|
831 }
|
|
832
|
|
833
|
|
834 static ngx_uint_t
|
|
835 ngx_http_geo_delete_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
836 in_addr_t start, in_addr_t end)
|
|
837 {
|
|
838 in_addr_t n;
|
|
839 ngx_uint_t h, i, s, e, warn;
|
|
840 ngx_array_t *a;
|
|
841 ngx_http_geo_range_t *range;
|
|
842
|
|
843 warn = 0;
|
|
844
|
438
|
845 for (n = start; n <= end; n += 0x10000) {
|
422
|
846
|
|
847 h = n >> 16;
|
438
|
848
|
|
849 if (n == start) {
|
|
850 s = n & 0xffff;
|
|
851 } else {
|
|
852 s = 0;
|
|
853 }
|
422
|
854
|
|
855 if ((n | 0xffff) > end) {
|
|
856 e = end & 0xffff;
|
|
857
|
|
858 } else {
|
|
859 e = 0xffff;
|
|
860 }
|
|
861
|
582
|
862 a = (ngx_array_t *) ctx->high.low[h];
|
422
|
863
|
|
864 if (a == NULL) {
|
|
865 warn = 1;
|
|
866 continue;
|
|
867 }
|
|
868
|
|
869 range = a->elts;
|
|
870 for (i = 0; i < a->nelts; i++) {
|
|
871
|
|
872 if (s == (ngx_uint_t) range[i].start
|
|
873 && e == (ngx_uint_t) range[i].end)
|
|
874 {
|
620
|
875 ngx_memmove(&range[i], &range[i + 1],
|
422
|
876 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
|
438
|
877
|
|
878 a->nelts--;
|
|
879
|
422
|
880 break;
|
|
881 }
|
|
882
|
|
883 if (s != (ngx_uint_t) range[i].start
|
|
884 && e != (ngx_uint_t) range[i].end)
|
|
885 {
|
|
886 continue;
|
|
887 }
|
|
888
|
|
889 warn = 1;
|
|
890 }
|
|
891 }
|
|
892
|
|
893 return warn;
|
|
894 }
|
|
895
|
|
896
|
|
897 static char *
|
|
898 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
899 ngx_str_t *value)
|
|
900 {
|
|
901 ngx_int_t rc, del;
|
|
902 ngx_str_t *net;
|
|
903 ngx_uint_t i;
|
454
|
904 ngx_cidr_t cidr;
|
422
|
905 ngx_http_variable_value_t *val, *old;
|
|
906
|
|
907 if (ctx->tree == NULL) {
|
|
908 ctx->tree = ngx_radix_tree_create(ctx->pool, -1);
|
|
909 if (ctx->tree == NULL) {
|
|
910 return NGX_CONF_ERROR;
|
|
911 }
|
34
|
912 }
|
|
913
|
|
914 if (ngx_strcmp(value[0].data, "default") == 0) {
|
674
|
915 /* cidr.family = AF_INET; */
|
454
|
916 cidr.u.in.addr = 0;
|
|
917 cidr.u.in.mask = 0;
|
422
|
918 net = &value[0];
|
34
|
919
|
|
920 } else {
|
422
|
921 if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
922 net = &value[1];
|
|
923 del = 1;
|
|
924
|
|
925 } else {
|
|
926 net = &value[0];
|
|
927 del = 0;
|
|
928 }
|
|
929
|
510
|
930 if (ngx_http_geo_cidr_value(cf, net, &cidr) != NGX_OK) {
|
|
931 return NGX_CONF_ERROR;
|
326
|
932 }
|
|
933
|
674
|
934 if (cidr.family != AF_INET) {
|
|
935 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
936 "\"geo\" supports IPv4 only");
|
|
937 return NGX_CONF_ERROR;
|
|
938 }
|
|
939
|
|
940 cidr.u.in.addr = ntohl(cidr.u.in.addr);
|
|
941 cidr.u.in.mask = ntohl(cidr.u.in.mask);
|
|
942
|
422
|
943 if (del) {
|
454
|
944 if (ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr,
|
|
945 cidr.u.in.mask)
|
422
|
946 != NGX_OK)
|
|
947 {
|
|
948 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
949 "no network \"%V\" to delete", net);
|
|
950 }
|
426
|
951
|
|
952 return NGX_CONF_OK;
|
34
|
953 }
|
|
954 }
|
|
955
|
422
|
956 val = ngx_http_geo_value(cf, ctx, &value[1]);
|
34
|
957
|
422
|
958 if (val == NULL) {
|
|
959 return NGX_CONF_ERROR;
|
34
|
960 }
|
|
961
|
102
|
962 for (i = 2; i; i--) {
|
454
|
963 rc = ngx_radix32tree_insert(ctx->tree, cidr.u.in.addr, cidr.u.in.mask,
|
422
|
964 (uintptr_t) val);
|
102
|
965 if (rc == NGX_OK) {
|
|
966 return NGX_CONF_OK;
|
|
967 }
|
|
968
|
|
969 if (rc == NGX_ERROR) {
|
|
970 return NGX_CONF_ERROR;
|
|
971 }
|
|
972
|
|
973 /* rc == NGX_BUSY */
|
|
974
|
526
|
975 old = (ngx_http_variable_value_t *)
|
454
|
976 ngx_radix32tree_find(ctx->tree, cidr.u.in.addr & cidr.u.in.mask);
|
102
|
977
|
|
978 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
422
|
979 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
|
|
980 net, val, old);
|
102
|
981
|
454
|
982 rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, cidr.u.in.mask);
|
102
|
983
|
|
984 if (rc == NGX_ERROR) {
|
422
|
985 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
|
102
|
986 return NGX_CONF_ERROR;
|
|
987 }
|
34
|
988 }
|
|
989
|
102
|
990 return NGX_CONF_ERROR;
|
34
|
991 }
|
422
|
992
|
|
993
|
|
994 static ngx_http_variable_value_t *
|
|
995 ngx_http_geo_value(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
996 ngx_str_t *value)
|
|
997 {
|
582
|
998 uint32_t hash;
|
|
999 ngx_http_variable_value_t *val;
|
|
1000 ngx_http_geo_variable_value_node_t *gvvn;
|
422
|
1001
|
|
1002 hash = ngx_crc32_long(value->data, value->len);
|
|
1003
|
582
|
1004 gvvn = (ngx_http_geo_variable_value_node_t *)
|
|
1005 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash);
|
422
|
1006
|
582
|
1007 if (gvvn) {
|
|
1008 return gvvn->value;
|
422
|
1009 }
|
|
1010
|
|
1011 val = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t));
|
|
1012 if (val == NULL) {
|
|
1013 return NULL;
|
|
1014 }
|
|
1015
|
|
1016 val->len = value->len;
|
|
1017 val->data = ngx_pstrdup(ctx->pool, value);
|
|
1018 if (val->data == NULL) {
|
|
1019 return NULL;
|
|
1020 }
|
|
1021
|
|
1022 val->valid = 1;
|
|
1023 val->no_cacheable = 0;
|
|
1024 val->not_found = 0;
|
|
1025
|
582
|
1026 gvvn = ngx_palloc(ctx->temp_pool,
|
|
1027 sizeof(ngx_http_geo_variable_value_node_t));
|
|
1028 if (gvvn == NULL) {
|
422
|
1029 return NULL;
|
|
1030 }
|
|
1031
|
582
|
1032 gvvn->sn.node.key = hash;
|
|
1033 gvvn->sn.str.len = val->len;
|
|
1034 gvvn->sn.str.data = val->data;
|
|
1035 gvvn->value = val;
|
|
1036 gvvn->offset = 0;
|
422
|
1037
|
582
|
1038 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node);
|
|
1039
|
|
1040 ctx->data_size += ngx_align(sizeof(ngx_http_variable_value_t) + value->len,
|
|
1041 sizeof(void *));
|
422
|
1042
|
|
1043 return val;
|
|
1044 }
|
510
|
1045
|
|
1046
|
|
1047 static char *
|
|
1048 ngx_http_geo_add_proxy(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
1049 ngx_cidr_t *cidr)
|
|
1050 {
|
674
|
1051 ngx_cidr_t *c;
|
510
|
1052
|
|
1053 if (ctx->proxies == NULL) {
|
674
|
1054 ctx->proxies = ngx_array_create(ctx->pool, 4, sizeof(ngx_cidr_t));
|
510
|
1055 if (ctx->proxies == NULL) {
|
|
1056 return NGX_CONF_ERROR;
|
|
1057 }
|
|
1058 }
|
|
1059
|
|
1060 c = ngx_array_push(ctx->proxies);
|
|
1061 if (c == NULL) {
|
|
1062 return NGX_CONF_ERROR;
|
|
1063 }
|
|
1064
|
674
|
1065 *c = *cidr;
|
510
|
1066
|
|
1067 return NGX_CONF_OK;
|
|
1068 }
|
|
1069
|
|
1070
|
|
1071 static ngx_int_t
|
|
1072 ngx_http_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
|
|
1073 {
|
|
1074 ngx_int_t rc;
|
|
1075
|
|
1076 if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
|
674
|
1077 cidr->family = AF_INET;
|
510
|
1078 cidr->u.in.addr = 0xffffffff;
|
|
1079 cidr->u.in.mask = 0xffffffff;
|
|
1080
|
|
1081 return NGX_OK;
|
|
1082 }
|
|
1083
|
|
1084 rc = ngx_ptocidr(net, cidr);
|
|
1085
|
|
1086 if (rc == NGX_ERROR) {
|
|
1087 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net);
|
|
1088 return NGX_ERROR;
|
|
1089 }
|
|
1090
|
|
1091 if (rc == NGX_DONE) {
|
|
1092 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1093 "low address bits of %V are meaningless", net);
|
|
1094 }
|
|
1095
|
|
1096 return NGX_OK;
|
|
1097 }
|
582
|
1098
|
|
1099
|
|
1100 static char *
|
|
1101 ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
1102 ngx_str_t *name)
|
|
1103 {
|
|
1104 char *rv;
|
|
1105 ngx_str_t file;
|
|
1106
|
|
1107 file.len = name->len + 4;
|
|
1108 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5);
|
|
1109 if (file.data == NULL) {
|
|
1110 return NGX_CONF_ERROR;
|
|
1111 }
|
|
1112
|
|
1113 ngx_sprintf(file.data, "%V.bin%Z", name);
|
|
1114
|
|
1115 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
|
|
1116 return NGX_CONF_ERROR;
|
|
1117 }
|
|
1118
|
|
1119 if (ctx->ranges) {
|
|
1120 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
1121
|
|
1122 switch (ngx_http_geo_include_binary_base(cf, ctx, &file)) {
|
|
1123 case NGX_OK:
|
|
1124 return NGX_CONF_OK;
|
|
1125 case NGX_ERROR:
|
|
1126 return NGX_CONF_ERROR;
|
|
1127 default:
|
|
1128 break;
|
|
1129 }
|
|
1130 }
|
|
1131
|
|
1132 file.len -= 4;
|
|
1133 file.data[file.len] = '\0';
|
|
1134
|
|
1135 ctx->include_name = file;
|
|
1136
|
|
1137 if (ctx->outside_entries) {
|
|
1138 ctx->allow_binary_include = 0;
|
|
1139 }
|
|
1140
|
|
1141 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
1142
|
|
1143 rv = ngx_conf_parse(cf, &file);
|
|
1144
|
|
1145 ctx->includes++;
|
|
1146 ctx->outside_entries = 0;
|
|
1147
|
|
1148 return rv;
|
|
1149 }
|
|
1150
|
|
1151
|
|
1152 static ngx_int_t
|
|
1153 ngx_http_geo_include_binary_base(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
1154 ngx_str_t *name)
|
|
1155 {
|
|
1156 u_char *base, ch;
|
|
1157 time_t mtime;
|
|
1158 size_t size, len;
|
|
1159 ssize_t n;
|
|
1160 uint32_t crc32;
|
|
1161 ngx_err_t err;
|
|
1162 ngx_int_t rc;
|
|
1163 ngx_uint_t i;
|
|
1164 ngx_file_t file;
|
|
1165 ngx_file_info_t fi;
|
|
1166 ngx_http_geo_range_t *range, **ranges;
|
|
1167 ngx_http_geo_header_t *header;
|
|
1168 ngx_http_variable_value_t *vv;
|
|
1169
|
|
1170 ngx_memzero(&file, sizeof(ngx_file_t));
|
|
1171 file.name = *name;
|
|
1172 file.log = cf->log;
|
|
1173
|
|
1174 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0);
|
|
1175 if (file.fd == NGX_INVALID_FILE) {
|
|
1176 err = ngx_errno;
|
|
1177 if (err != NGX_ENOENT) {
|
|
1178 ngx_conf_log_error(NGX_LOG_CRIT, cf, err,
|
|
1179 ngx_open_file_n " \"%s\" failed", name->data);
|
|
1180 }
|
|
1181 return NGX_DECLINED;
|
|
1182 }
|
|
1183
|
|
1184 if (ctx->outside_entries) {
|
|
1185 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
1186 "binary geo range base \"%s\" cannot be mixed with usual entries",
|
582
|
1187 name->data);
|
|
1188 rc = NGX_ERROR;
|
|
1189 goto done;
|
|
1190 }
|
|
1191
|
|
1192 if (ctx->binary_include) {
|
|
1193 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
1194 "second binary geo range base \"%s\" cannot be mixed with \"%s\"",
|
582
|
1195 name->data, ctx->include_name.data);
|
|
1196 rc = NGX_ERROR;
|
|
1197 goto done;
|
|
1198 }
|
|
1199
|
|
1200 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) {
|
|
1201 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
1202 ngx_fd_info_n " \"%s\" failed", name->data);
|
|
1203 goto failed;
|
|
1204 }
|
|
1205
|
|
1206 size = (size_t) ngx_file_size(&fi);
|
|
1207 mtime = ngx_file_mtime(&fi);
|
|
1208
|
|
1209 ch = name->data[name->len - 4];
|
|
1210 name->data[name->len - 4] = '\0';
|
|
1211
|
|
1212 if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) {
|
|
1213 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
1214 ngx_file_info_n " \"%s\" failed", name->data);
|
|
1215 goto failed;
|
|
1216 }
|
|
1217
|
|
1218 name->data[name->len - 4] = ch;
|
|
1219
|
|
1220 if (mtime < ngx_file_mtime(&fi)) {
|
|
1221 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1222 "stale binary geo range base \"%s\"", name->data);
|
|
1223 goto failed;
|
|
1224 }
|
|
1225
|
|
1226 base = ngx_palloc(ctx->pool, size);
|
|
1227 if (base == NULL) {
|
|
1228 goto failed;
|
|
1229 }
|
|
1230
|
|
1231 n = ngx_read_file(&file, base, size, 0);
|
|
1232
|
|
1233 if (n == NGX_ERROR) {
|
|
1234 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
1235 ngx_read_file_n " \"%s\" failed", name->data);
|
|
1236 goto failed;
|
|
1237 }
|
|
1238
|
|
1239 if ((size_t) n != size) {
|
|
1240 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0,
|
|
1241 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z",
|
|
1242 name->data, n, size);
|
|
1243 goto failed;
|
|
1244 }
|
|
1245
|
|
1246 header = (ngx_http_geo_header_t *) base;
|
|
1247
|
|
1248 if (size < 16 || ngx_memcmp(&ngx_http_geo_header, header, 12) != 0) {
|
|
1249 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1250 "incompatible binary geo range base \"%s\"", name->data);
|
|
1251 goto failed;
|
|
1252 }
|
|
1253
|
|
1254 ngx_crc32_init(crc32);
|
|
1255
|
|
1256 vv = (ngx_http_variable_value_t *) (base + sizeof(ngx_http_geo_header_t));
|
|
1257
|
|
1258 while(vv->data) {
|
|
1259 len = ngx_align(sizeof(ngx_http_variable_value_t) + vv->len,
|
|
1260 sizeof(void *));
|
|
1261 ngx_crc32_update(&crc32, (u_char *) vv, len);
|
|
1262 vv->data += (size_t) base;
|
|
1263 vv = (ngx_http_variable_value_t *) ((u_char *) vv + len);
|
|
1264 }
|
|
1265 ngx_crc32_update(&crc32, (u_char *) vv, sizeof(ngx_http_variable_value_t));
|
|
1266 vv++;
|
|
1267
|
|
1268 ranges = (ngx_http_geo_range_t **) vv;
|
|
1269
|
|
1270 for (i = 0; i < 0x10000; i++) {
|
|
1271 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *));
|
|
1272 if (ranges[i]) {
|
|
1273 ranges[i] = (ngx_http_geo_range_t *)
|
|
1274 ((u_char *) ranges[i] + (size_t) base);
|
|
1275 }
|
|
1276 }
|
|
1277
|
|
1278 range = (ngx_http_geo_range_t *) &ranges[0x10000];
|
|
1279
|
|
1280 while ((u_char *) range < base + size) {
|
|
1281 while (range->value) {
|
|
1282 ngx_crc32_update(&crc32, (u_char *) range,
|
|
1283 sizeof(ngx_http_geo_range_t));
|
|
1284 range->value = (ngx_http_variable_value_t *)
|
|
1285 ((u_char *) range->value + (size_t) base);
|
|
1286 range++;
|
|
1287 }
|
|
1288 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *));
|
|
1289 range = (ngx_http_geo_range_t *) ((u_char *) range + sizeof(void *));
|
|
1290 }
|
|
1291
|
|
1292 ngx_crc32_final(crc32);
|
|
1293
|
|
1294 if (crc32 != header->crc32) {
|
|
1295 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1296 "CRC32 mismatch in binary geo range base \"%s\"", name->data);
|
|
1297 goto failed;
|
|
1298 }
|
|
1299
|
|
1300 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0,
|
|
1301 "using binary geo range base \"%s\"", name->data);
|
|
1302
|
|
1303 ctx->include_name = *name;
|
|
1304 ctx->binary_include = 1;
|
|
1305 ctx->high.low = ranges;
|
|
1306 rc = NGX_OK;
|
|
1307
|
|
1308 goto done;
|
|
1309
|
|
1310 failed:
|
|
1311
|
|
1312 rc = NGX_DECLINED;
|
|
1313
|
|
1314 done:
|
|
1315
|
|
1316 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
|
|
1317 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
1318 ngx_close_file_n " \"%s\" failed", name->data);
|
|
1319 }
|
|
1320
|
|
1321 return rc;
|
|
1322 }
|
|
1323
|
|
1324
|
|
1325 static void
|
|
1326 ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx)
|
|
1327 {
|
|
1328 u_char *p;
|
|
1329 uint32_t hash;
|
|
1330 ngx_str_t s;
|
|
1331 ngx_uint_t i;
|
|
1332 ngx_file_mapping_t fm;
|
|
1333 ngx_http_geo_range_t *r, *range, **ranges;
|
|
1334 ngx_http_geo_header_t *header;
|
|
1335 ngx_http_geo_variable_value_node_t *gvvn;
|
|
1336
|
|
1337 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5);
|
|
1338 if (fm.name == NULL) {
|
|
1339 return;
|
|
1340 }
|
|
1341
|
|
1342 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name);
|
|
1343
|
|
1344 fm.size = ctx->data_size;
|
|
1345 fm.log = ctx->pool->log;
|
|
1346
|
|
1347 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0,
|
|
1348 "creating binary geo range base \"%s\"", fm.name);
|
|
1349
|
|
1350 if (ngx_create_file_mapping(&fm) != NGX_OK) {
|
|
1351 return;
|
|
1352 }
|
|
1353
|
|
1354 p = ngx_cpymem(fm.addr, &ngx_http_geo_header,
|
|
1355 sizeof(ngx_http_geo_header_t));
|
|
1356
|
|
1357 p = ngx_http_geo_copy_values(fm.addr, p, ctx->rbtree.root,
|
|
1358 ctx->rbtree.sentinel);
|
|
1359
|
|
1360 p += sizeof(ngx_http_variable_value_t);
|
|
1361
|
|
1362 ranges = (ngx_http_geo_range_t **) p;
|
|
1363
|
|
1364 p += 0x10000 * sizeof(ngx_http_geo_range_t *);
|
|
1365
|
|
1366 for (i = 0; i < 0x10000; i++) {
|
|
1367 r = ctx->high.low[i];
|
|
1368 if (r == NULL) {
|
|
1369 continue;
|
|
1370 }
|
|
1371
|
|
1372 range = (ngx_http_geo_range_t *) p;
|
|
1373 ranges[i] = (ngx_http_geo_range_t *) (p - (u_char *) fm.addr);
|
|
1374
|
|
1375 do {
|
|
1376 s.len = r->value->len;
|
|
1377 s.data = r->value->data;
|
|
1378 hash = ngx_crc32_long(s.data, s.len);
|
|
1379 gvvn = (ngx_http_geo_variable_value_node_t *)
|
|
1380 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash);
|
|
1381
|
|
1382 range->value = (ngx_http_variable_value_t *) gvvn->offset;
|
|
1383 range->start = r->start;
|
|
1384 range->end = r->end;
|
|
1385 range++;
|
|
1386
|
|
1387 } while ((++r)->value);
|
|
1388
|
|
1389 range->value = NULL;
|
|
1390
|
|
1391 p = (u_char *) range + sizeof(void *);
|
|
1392 }
|
|
1393
|
|
1394 header = fm.addr;
|
|
1395 header->crc32 = ngx_crc32_long((u_char *) fm.addr
|
|
1396 + sizeof(ngx_http_geo_header_t),
|
|
1397 fm.size - sizeof(ngx_http_geo_header_t));
|
|
1398
|
|
1399 ngx_close_file_mapping(&fm);
|
|
1400 }
|
|
1401
|
|
1402
|
|
1403 static u_char *
|
|
1404 ngx_http_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node,
|
|
1405 ngx_rbtree_node_t *sentinel)
|
|
1406 {
|
|
1407 ngx_http_variable_value_t *vv;
|
|
1408 ngx_http_geo_variable_value_node_t *gvvn;
|
|
1409
|
|
1410 if (node == sentinel) {
|
|
1411 return p;
|
|
1412 }
|
|
1413
|
|
1414 gvvn = (ngx_http_geo_variable_value_node_t *) node;
|
|
1415 gvvn->offset = p - base;
|
|
1416
|
|
1417 vv = (ngx_http_variable_value_t *) p;
|
|
1418 *vv = *gvvn->value;
|
|
1419 p += sizeof(ngx_http_variable_value_t);
|
|
1420 vv->data = (u_char *) (p - base);
|
|
1421
|
|
1422 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len);
|
|
1423
|
|
1424 p = ngx_align_ptr(p, sizeof(void *));
|
|
1425
|
|
1426 p = ngx_http_geo_copy_values(base, p, node->left, sentinel);
|
|
1427
|
|
1428 return ngx_http_geo_copy_values(base, p, node->right, sentinel);
|
|
1429 }
|