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