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