34
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
431
|
13 u_short start;
|
|
14 u_short end;
|
|
15 ngx_http_variable_value_t *value;
|
|
16 } ngx_http_geo_range_t;
|
|
17
|
|
18
|
|
19 typedef struct {
|
|
20 ngx_http_geo_range_t *ranges;
|
|
21 ngx_uint_t n;
|
|
22 } ngx_http_geo_low_ranges_t;
|
|
23
|
|
24
|
|
25 typedef struct {
|
|
26 ngx_http_geo_low_ranges_t low[0x10000];
|
|
27 ngx_http_variable_value_t *default_value;
|
|
28 } ngx_http_geo_high_ranges_t;
|
|
29
|
|
30
|
|
31 typedef struct {
|
|
32 ngx_http_variable_value_t *value;
|
|
33 ngx_str_t *net;
|
|
34 ngx_http_geo_high_ranges_t *high;
|
|
35 ngx_radix_tree_t *tree;
|
|
36 ngx_rbtree_t rbtree;
|
|
37 ngx_rbtree_node_t sentinel;
|
|
38 ngx_pool_t *pool;
|
|
39 ngx_pool_t *temp_pool;
|
138
|
40 } ngx_http_geo_conf_ctx_t;
|
34
|
41
|
|
42
|
|
43 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
44 static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
|
431
|
45 static char *ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
46 ngx_str_t *value);
|
|
47 static char *ngx_http_geo_add_range(ngx_conf_t *cf,
|
|
48 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
49 static ngx_uint_t ngx_http_geo_delete_range(ngx_conf_t *cf,
|
|
50 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
51 static char *ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
52 ngx_str_t *value);
|
|
53 static ngx_http_variable_value_t *ngx_http_geo_value(ngx_conf_t *cf,
|
|
54 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *value);
|
34
|
55
|
|
56
|
|
57 static ngx_command_t ngx_http_geo_commands[] = {
|
|
58
|
|
59 { ngx_string("geo"),
|
|
60 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
|
|
61 ngx_http_geo_block,
|
|
62 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
63 0,
|
|
64 NULL },
|
|
65
|
|
66 ngx_null_command
|
|
67 };
|
|
68
|
|
69
|
|
70 static ngx_http_module_t ngx_http_geo_module_ctx = {
|
58
|
71 NULL, /* preconfiguration */
|
|
72 NULL, /* postconfiguration */
|
34
|
73
|
|
74 NULL, /* create main configuration */
|
|
75 NULL, /* init main configuration */
|
|
76
|
|
77 NULL, /* create server configuration */
|
|
78 NULL, /* merge server configuration */
|
|
79
|
|
80 NULL, /* create location configuration */
|
|
81 NULL /* merge location configuration */
|
|
82 };
|
|
83
|
|
84
|
|
85 ngx_module_t ngx_http_geo_module = {
|
58
|
86 NGX_MODULE_V1,
|
34
|
87 &ngx_http_geo_module_ctx, /* module context */
|
|
88 ngx_http_geo_commands, /* module directives */
|
|
89 NGX_HTTP_MODULE, /* module type */
|
90
|
90 NULL, /* init master */
|
34
|
91 NULL, /* init module */
|
90
|
92 NULL, /* init process */
|
|
93 NULL, /* init thread */
|
|
94 NULL, /* exit thread */
|
|
95 NULL, /* exit process */
|
|
96 NULL, /* exit master */
|
|
97 NGX_MODULE_V1_PADDING
|
34
|
98 };
|
|
99
|
|
100
|
|
101 /* AF_INET only */
|
|
102
|
122
|
103 static ngx_int_t
|
431
|
104 ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
122
|
105 uintptr_t data)
|
34
|
106 {
|
50
|
107 ngx_radix_tree_t *tree = (ngx_radix_tree_t *) data;
|
34
|
108
|
38
|
109 struct sockaddr_in *sin;
|
122
|
110 ngx_http_variable_value_t *vv;
|
34
|
111
|
38
|
112 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
113 "http geo started");
|
|
114
|
431
|
115 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
116
|
122
|
117 vv = (ngx_http_variable_value_t *)
|
34
|
118 ngx_radix32tree_find(tree, ntohl(sin->sin_addr.s_addr));
|
38
|
119
|
122
|
120 *v = *vv;
|
|
121
|
38
|
122 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
328
|
123 "http geo: %V %v", &r->connection->addr_text, v);
|
38
|
124
|
122
|
125 return NGX_OK;
|
34
|
126 }
|
|
127
|
|
128
|
431
|
129 static ngx_int_t
|
|
130 ngx_http_geo_range_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
131 uintptr_t data)
|
|
132 {
|
|
133 ngx_http_geo_high_ranges_t *high = (ngx_http_geo_high_ranges_t *) data;
|
|
134
|
|
135 in_addr_t addr;
|
|
136 ngx_uint_t i, n;
|
|
137 struct sockaddr_in *sin;
|
|
138 ngx_http_geo_range_t *range;
|
|
139
|
|
140 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
141 "http geo started");
|
|
142
|
|
143 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
144
|
|
145 *v = *high->default_value;
|
|
146
|
|
147 addr = ntohl(sin->sin_addr.s_addr);
|
|
148
|
|
149 range = high->low[addr >> 16].ranges;
|
|
150
|
|
151 n = addr & 0xffff;
|
|
152
|
|
153 for (i = 0; i < high->low[addr >> 16].n; i++) {
|
|
154 if (n >= (ngx_uint_t) range[i].start
|
|
155 && n <= (ngx_uint_t) range[i].end)
|
|
156 {
|
|
157 *v = *range[i].value;
|
|
158 }
|
|
159 }
|
|
160
|
|
161 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
162 "http geo: %V %v", &r->connection->addr_text, v);
|
|
163
|
|
164 return NGX_OK;
|
|
165 }
|
|
166
|
|
167
|
38
|
168 static char *
|
|
169 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
34
|
170 {
|
138
|
171 char *rv;
|
431
|
172 size_t len;
|
138
|
173 ngx_str_t *value, name;
|
431
|
174 ngx_uint_t i;
|
138
|
175 ngx_conf_t save;
|
|
176 ngx_pool_t *pool;
|
431
|
177 ngx_array_t *a;
|
328
|
178 ngx_http_variable_t *var;
|
138
|
179 ngx_http_geo_conf_ctx_t ctx;
|
34
|
180
|
50
|
181 value = cf->args->elts;
|
|
182
|
|
183 name = value[1];
|
|
184
|
|
185 if (name.data[0] != '$') {
|
|
186 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
187 "\"%V\" variable name should start with '$'",
|
|
188 &value[1]);
|
|
189 } else {
|
|
190 name.len--;
|
|
191 name.data++;
|
|
192 }
|
|
193
|
340
|
194 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
50
|
195 if (var == NULL) {
|
34
|
196 return NGX_CONF_ERROR;
|
|
197 }
|
|
198
|
132
|
199 pool = ngx_create_pool(16384, cf->log);
|
50
|
200 if (pool == NULL) {
|
34
|
201 return NGX_CONF_ERROR;
|
|
202 }
|
|
203
|
431
|
204 ctx.temp_pool = ngx_create_pool(16384, cf->log);
|
|
205 if (ctx.temp_pool == NULL) {
|
34
|
206 return NGX_CONF_ERROR;
|
|
207 }
|
|
208
|
431
|
209 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel,
|
|
210 ngx_http_variable_value_rbtree_insert);
|
|
211
|
|
212 ctx.high = NULL;
|
|
213 ctx.tree = NULL;
|
138
|
214 ctx.pool = cf->pool;
|
34
|
215
|
|
216 save = *cf;
|
|
217 cf->pool = pool;
|
138
|
218 cf->ctx = &ctx;
|
34
|
219 cf->handler = ngx_http_geo;
|
|
220 cf->handler_conf = conf;
|
|
221
|
|
222 rv = ngx_conf_parse(cf, NULL);
|
|
223
|
|
224 *cf = save;
|
|
225
|
431
|
226 if (ctx.high) {
|
|
227
|
|
228 for (i = 0; i < 0x10000; i++) {
|
|
229 a = (ngx_array_t *) ctx.high->low[i].ranges;
|
|
230
|
|
231 if (a == NULL || a->nelts == 0) {
|
|
232 continue;
|
|
233 }
|
|
234
|
|
235 ctx.high->low[i].n = a->nelts;
|
34
|
236
|
431
|
237 len = a->nelts * sizeof(ngx_http_geo_range_t);
|
|
238
|
|
239 ctx.high->low[i].ranges = ngx_palloc(cf->pool, len);
|
|
240 if (ctx.high->low[i].ranges == NULL ){
|
|
241 return NGX_CONF_ERROR;
|
|
242 }
|
|
243
|
|
244 ngx_memcpy(ctx.high->low[i].ranges, a->elts, len);
|
|
245 }
|
|
246
|
|
247 var->get_handler = ngx_http_geo_range_variable;
|
|
248 var->data = (uintptr_t) ctx.high;
|
34
|
249
|
431
|
250 ngx_destroy_pool(ctx.temp_pool);
|
|
251 ngx_destroy_pool(pool);
|
|
252
|
|
253 if (ctx.high->default_value == NULL) {
|
|
254 ctx.high->default_value = &ngx_http_variable_null_value;
|
|
255 }
|
|
256
|
|
257 } else {
|
433
|
258 if (ctx.tree == NULL) {
|
|
259 ctx.tree = ngx_radix_tree_create(cf->pool, -1);
|
|
260 if (ctx.tree == NULL) {
|
|
261 return NGX_CONF_ERROR;
|
|
262 }
|
|
263 }
|
|
264
|
431
|
265 var->get_handler = ngx_http_geo_cidr_variable;
|
|
266 var->data = (uintptr_t) ctx.tree;
|
|
267
|
|
268 ngx_destroy_pool(ctx.temp_pool);
|
|
269 ngx_destroy_pool(pool);
|
|
270
|
|
271 if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) {
|
|
272 return rv;
|
|
273 }
|
|
274
|
|
275 if (ngx_radix32tree_insert(ctx.tree, 0, 0,
|
|
276 (uintptr_t) &ngx_http_variable_null_value)
|
|
277 == NGX_ERROR)
|
|
278 {
|
|
279 return NGX_CONF_ERROR;
|
|
280 }
|
34
|
281 }
|
|
282
|
|
283 return rv;
|
|
284 }
|
|
285
|
|
286
|
38
|
287 static char *
|
|
288 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
34
|
289 {
|
431
|
290 char *rv;
|
|
291 ngx_str_t *value, file;
|
|
292 ngx_http_geo_conf_ctx_t *ctx;
|
34
|
293
|
138
|
294 ctx = cf->ctx;
|
34
|
295
|
431
|
296 value = cf->args->elts;
|
|
297
|
|
298 if (cf->args->nelts == 1) {
|
|
299
|
|
300 if (ngx_strcmp(value[0].data, "ranges") == 0) {
|
|
301
|
|
302 if (ctx->tree) {
|
|
303 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
304 "the \"ranges\" directive must be "
|
|
305 "the first directive inside \"geo\" block");
|
|
306 goto failed;
|
|
307 }
|
|
308
|
|
309 ctx->high = ngx_pcalloc(ctx->pool,
|
|
310 sizeof(ngx_http_geo_high_ranges_t));
|
|
311 if (ctx->high == NULL) {
|
|
312 goto failed;
|
|
313 }
|
|
314
|
|
315 rv = NGX_CONF_OK;
|
|
316
|
|
317 goto done;
|
|
318 }
|
|
319 }
|
|
320
|
34
|
321 if (cf->args->nelts != 2) {
|
|
322 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
323 "invalid number of the geo parameters");
|
431
|
324 goto failed;
|
34
|
325 }
|
|
326
|
431
|
327 if (ngx_strcmp(value[0].data, "include") == 0) {
|
|
328
|
|
329 file.len = value[1].len++;
|
34
|
330
|
431
|
331 file.data = ngx_pstrdup(ctx->temp_pool, &value[1]);
|
|
332 if (file.data == NULL) {
|
|
333 goto failed;
|
|
334 }
|
34
|
335
|
431
|
336 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK){
|
|
337 goto failed;
|
34
|
338 }
|
|
339
|
|
340 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
341
|
431
|
342 rv = ngx_conf_parse(cf, &file);
|
|
343
|
|
344 goto done;
|
|
345 }
|
|
346
|
|
347 if (ctx->high) {
|
|
348 rv = ngx_http_geo_range(cf, ctx, value);
|
|
349
|
|
350 } else {
|
|
351 rv = ngx_http_geo_cidr(cf, ctx, value);
|
|
352 }
|
|
353
|
|
354 done:
|
|
355
|
|
356 ngx_reset_pool(cf->pool);
|
|
357
|
|
358 return rv;
|
|
359
|
|
360 failed:
|
|
361
|
|
362 ngx_reset_pool(cf->pool);
|
|
363
|
|
364 return NGX_CONF_ERROR;
|
|
365 }
|
|
366
|
|
367
|
|
368 static char *
|
|
369 ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
370 ngx_str_t *value)
|
|
371 {
|
|
372 u_char *p, *last;
|
|
373 in_addr_t start, end;
|
|
374 ngx_str_t *net;
|
|
375 ngx_uint_t del;
|
|
376 ngx_http_variable_value_t *old;
|
|
377
|
|
378 if (ngx_strcmp(value[0].data, "default") == 0) {
|
|
379
|
|
380 old = ctx->high->default_value;
|
|
381
|
|
382 ctx->high->default_value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
383 if (ctx->high->default_value == NULL) {
|
|
384 return NGX_CONF_ERROR;
|
|
385 }
|
|
386
|
|
387 if (old) {
|
|
388 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
389 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
|
|
390 &value[0], ctx->high->default_value, old);
|
|
391 }
|
|
392
|
|
393 return NGX_CONF_OK;
|
|
394 }
|
|
395
|
|
396 if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
397 net = &value[1];
|
|
398 del = 1;
|
|
399
|
|
400 } else {
|
|
401 net = &value[0];
|
|
402 del = 0;
|
|
403 }
|
|
404
|
|
405 last = net->data + net->len;
|
|
406
|
|
407 p = ngx_strlchr(net->data, last, '-');
|
|
408
|
|
409 if (p == NULL) {
|
|
410 goto invalid;
|
|
411 }
|
|
412
|
|
413 start = ngx_inet_addr(net->data, p - net->data);
|
|
414
|
|
415 if (start == INADDR_NONE) {
|
|
416 goto invalid;
|
|
417 }
|
|
418
|
|
419 start = ntohl(start);
|
|
420
|
|
421 p++;
|
|
422
|
|
423 end = ngx_inet_addr(p, last - p);
|
|
424
|
|
425 if (end == INADDR_NONE) {
|
|
426 goto invalid;
|
|
427 }
|
|
428
|
|
429 end = ntohl(end);
|
|
430
|
|
431 if (start > end) {
|
|
432 goto invalid;
|
|
433 }
|
|
434
|
|
435 if (del) {
|
|
436 if (ngx_http_geo_delete_range(cf, ctx, start, end)) {
|
|
437 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
438 "no address range \"%V\" to delete", net);
|
|
439 }
|
|
440
|
|
441 return NGX_CONF_OK;
|
|
442 }
|
|
443
|
|
444 ctx->value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
445
|
|
446 if (ctx->value == NULL) {
|
|
447 return NGX_CONF_ERROR;
|
|
448 }
|
|
449
|
|
450 ctx->net = net;
|
|
451
|
|
452 return ngx_http_geo_add_range(cf, ctx, start, end);
|
|
453
|
|
454 invalid:
|
|
455
|
|
456 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net);
|
|
457
|
|
458 return NGX_CONF_ERROR;
|
|
459 }
|
|
460
|
|
461
|
|
462 /* the add procedure is optimized to add a growing up sequence */
|
|
463
|
|
464 static char *
|
|
465 ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
466 in_addr_t start, in_addr_t end)
|
|
467 {
|
|
468 in_addr_t n;
|
|
469 ngx_uint_t h, i, s, e;
|
|
470 ngx_array_t *a;
|
|
471 ngx_http_geo_range_t *range;
|
|
472
|
|
473 for (n = start; n < end; n += 0x10000) {
|
|
474
|
|
475 h = n >> 16;
|
|
476 s = n & 0xffff;
|
|
477
|
|
478 if ((n | 0xffff) > end) {
|
|
479 e = end & 0xffff;
|
|
480
|
|
481 } else {
|
|
482 e = 0xffff;
|
|
483 }
|
|
484
|
|
485 a = (ngx_array_t *) ctx->high->low[h].ranges;
|
|
486
|
|
487 if (a == NULL) {
|
|
488 a = ngx_array_create(ctx->temp_pool, 64,
|
|
489 sizeof(ngx_http_geo_range_t));
|
|
490 if (a == NULL) {
|
|
491 return NGX_CONF_ERROR;
|
|
492 }
|
|
493
|
|
494 ctx->high->low[h].ranges = (ngx_http_geo_range_t *) a;
|
|
495 }
|
|
496
|
|
497 i = a->nelts;
|
|
498 range = a->elts;
|
|
499
|
|
500 while (i) {
|
|
501
|
|
502 i--;
|
|
503
|
|
504 if (e < (ngx_uint_t) range[i].start) {
|
|
505 continue;
|
|
506 }
|
|
507
|
|
508 if (s > (ngx_uint_t) range[i].end) {
|
|
509
|
|
510 /* add after the range */
|
|
511
|
|
512 range = ngx_array_push(a);
|
|
513 if (range == NULL) {
|
|
514 return NGX_CONF_ERROR;
|
|
515 }
|
|
516
|
|
517 range = a->elts;
|
|
518
|
|
519 ngx_memcpy(&range[i + 2], &range[i + 1],
|
|
520 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
|
|
521
|
|
522 range = &range[i + 1];
|
|
523
|
|
524 goto next;
|
|
525 }
|
|
526
|
|
527 if (s == (ngx_uint_t) range[i].start
|
|
528 && e == (ngx_uint_t) range[i].end)
|
|
529 {
|
|
530 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
531 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
|
|
532 ctx->net, ctx->value, range[i].value);
|
|
533 continue;
|
|
534 }
|
|
535
|
|
536 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
537 "overlapped range \"%V\"", ctx->net);
|
|
538
|
|
539 return NGX_CONF_ERROR;
|
|
540 }
|
|
541
|
|
542 /* add the first range */
|
|
543
|
|
544 range = ngx_array_push(a);
|
|
545 if (range == NULL) {
|
|
546 return NGX_CONF_ERROR;
|
|
547 }
|
|
548
|
|
549 next:
|
|
550
|
|
551 range->start = (u_short) s;
|
|
552 range->end = (u_short) e;
|
|
553 range->value = ctx->value;
|
|
554 }
|
|
555
|
|
556 return NGX_CONF_OK;
|
|
557 }
|
|
558
|
|
559
|
|
560 static ngx_uint_t
|
|
561 ngx_http_geo_delete_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
562 in_addr_t start, in_addr_t end)
|
|
563 {
|
|
564 in_addr_t n;
|
|
565 ngx_uint_t h, i, s, e, warn;
|
|
566 ngx_array_t *a;
|
|
567 ngx_http_geo_range_t *range;
|
|
568
|
|
569 warn = 0;
|
|
570
|
|
571 for (n = start; n < end; n += 0x10000) {
|
|
572
|
|
573 h = n >> 16;
|
|
574 s = n & 0xffff;
|
|
575
|
|
576 if ((n | 0xffff) > end) {
|
|
577 e = end & 0xffff;
|
|
578
|
|
579 } else {
|
|
580 e = 0xffff;
|
|
581 }
|
|
582
|
|
583 a = (ngx_array_t *) ctx->high->low[h].ranges;
|
|
584
|
|
585 if (a == NULL) {
|
|
586 warn = 1;
|
|
587 continue;
|
|
588 }
|
|
589
|
|
590 range = a->elts;
|
|
591 for (i = 0; i < a->nelts; i++) {
|
|
592
|
|
593 if (s == (ngx_uint_t) range[i].start
|
|
594 && e == (ngx_uint_t) range[i].end)
|
|
595 {
|
|
596 ngx_memcpy(&range[i], &range[i + 1],
|
|
597 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
|
|
598 break;
|
|
599 }
|
|
600
|
|
601 if (s != (ngx_uint_t) range[i].start
|
|
602 && e != (ngx_uint_t) range[i].end)
|
|
603 {
|
|
604 continue;
|
|
605 }
|
|
606
|
|
607 warn = 1;
|
|
608 }
|
|
609 }
|
|
610
|
|
611 return warn;
|
|
612 }
|
|
613
|
|
614
|
|
615 static char *
|
|
616 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
617 ngx_str_t *value)
|
|
618 {
|
|
619 ngx_int_t rc, del;
|
|
620 ngx_str_t *net;
|
|
621 ngx_uint_t i;
|
|
622 ngx_inet_cidr_t cidrin;
|
|
623 ngx_http_variable_value_t *val, *old;
|
|
624
|
|
625 if (ctx->tree == NULL) {
|
|
626 ctx->tree = ngx_radix_tree_create(ctx->pool, -1);
|
|
627 if (ctx->tree == NULL) {
|
|
628 return NGX_CONF_ERROR;
|
|
629 }
|
34
|
630 }
|
|
631
|
|
632 if (ngx_strcmp(value[0].data, "default") == 0) {
|
|
633 cidrin.addr = 0;
|
|
634 cidrin.mask = 0;
|
431
|
635 net = &value[0];
|
34
|
636
|
|
637 } else {
|
431
|
638 if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
639 net = &value[1];
|
|
640 del = 1;
|
|
641
|
|
642 } else {
|
|
643 net = &value[0];
|
|
644 del = 0;
|
|
645 }
|
|
646
|
|
647 rc = ngx_ptocidr(net, &cidrin);
|
326
|
648
|
|
649 if (rc == NGX_ERROR) {
|
34
|
650 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
431
|
651 "invalid network \"%V\"", net);
|
34
|
652 return NGX_CONF_ERROR;
|
|
653 }
|
|
654
|
326
|
655 if (rc == NGX_DONE) {
|
|
656 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
431
|
657 "low address bits of %V are meaningless", net);
|
326
|
658 }
|
|
659
|
34
|
660 cidrin.addr = ntohl(cidrin.addr);
|
|
661 cidrin.mask = ntohl(cidrin.mask);
|
|
662
|
431
|
663 if (del) {
|
|
664 if (ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask)
|
|
665 != NGX_OK)
|
|
666 {
|
|
667 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
668 "no network \"%V\" to delete", net);
|
|
669 }
|
435
|
670
|
|
671 return NGX_CONF_OK;
|
34
|
672 }
|
|
673 }
|
|
674
|
431
|
675 val = ngx_http_geo_value(cf, ctx, &value[1]);
|
34
|
676
|
431
|
677 if (val == NULL) {
|
|
678 return NGX_CONF_ERROR;
|
34
|
679 }
|
|
680
|
102
|
681 for (i = 2; i; i--) {
|
138
|
682 rc = ngx_radix32tree_insert(ctx->tree, cidrin.addr, cidrin.mask,
|
431
|
683 (uintptr_t) val);
|
102
|
684 if (rc == NGX_OK) {
|
|
685 return NGX_CONF_OK;
|
|
686 }
|
|
687
|
|
688 if (rc == NGX_ERROR) {
|
|
689 return NGX_CONF_ERROR;
|
|
690 }
|
|
691
|
|
692 /* rc == NGX_BUSY */
|
|
693
|
|
694 old = (ngx_http_variable_value_t *)
|
431
|
695 ngx_radix32tree_find(ctx->tree, cidrin.addr & cidrin.mask);
|
102
|
696
|
|
697 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
431
|
698 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
|
|
699 net, val, old);
|
102
|
700
|
138
|
701 rc = ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask);
|
102
|
702
|
|
703 if (rc == NGX_ERROR) {
|
431
|
704 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
|
102
|
705 return NGX_CONF_ERROR;
|
|
706 }
|
34
|
707 }
|
|
708
|
102
|
709 return NGX_CONF_ERROR;
|
34
|
710 }
|
431
|
711
|
|
712
|
|
713 static ngx_http_variable_value_t *
|
|
714 ngx_http_geo_value(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
715 ngx_str_t *value)
|
|
716 {
|
|
717 uint32_t hash;
|
|
718 ngx_http_variable_value_t *val;
|
|
719 ngx_http_variable_value_node_t *vvn;
|
|
720
|
|
721 hash = ngx_crc32_long(value->data, value->len);
|
|
722
|
|
723 val = ngx_http_variable_value_lookup(&ctx->rbtree, value, hash);
|
|
724
|
|
725 if (val) {
|
|
726 return val;
|
|
727 }
|
|
728
|
|
729 val = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t));
|
|
730 if (val == NULL) {
|
|
731 return NULL;
|
|
732 }
|
|
733
|
|
734 val->len = value->len;
|
|
735 val->data = ngx_pstrdup(ctx->pool, value);
|
|
736 if (val->data == NULL) {
|
|
737 return NULL;
|
|
738 }
|
|
739
|
|
740 val->valid = 1;
|
|
741 val->no_cacheable = 0;
|
|
742 val->not_found = 0;
|
|
743
|
|
744 vvn = ngx_palloc(ctx->temp_pool, sizeof(ngx_http_variable_value_node_t));
|
|
745 if (vvn == NULL) {
|
|
746 return NULL;
|
|
747 }
|
|
748
|
|
749 vvn->node.key = hash;
|
|
750 vvn->len = val->len;
|
|
751 vvn->value = val;
|
|
752
|
|
753 ngx_rbtree_insert(&ctx->rbtree, &vvn->node);
|
|
754
|
|
755 return val;
|
|
756 }
|