Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geo_module.c @ 2511:f91c5c964e61
split ports, addresses, and server names preparation and optimization
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 18 Feb 2009 16:17:12 +0000 |
parents | 1aff348d9aa8 |
children | 2e91aecb9e57 |
rev | line source |
---|---|
485 | 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 { | |
2399 | 13 u_short start; |
14 u_short end; | |
15 ngx_http_variable_value_t *value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
16 } ngx_http_geo_range_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
17 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
18 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
19 typedef struct { |
2399 | 20 ngx_http_geo_range_t *ranges; |
21 ngx_uint_t n; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
22 } ngx_http_geo_low_ranges_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
23 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
24 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
25 typedef struct { |
2399 | 26 ngx_http_geo_low_ranges_t low[0x10000]; |
27 ngx_http_variable_value_t *default_value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
28 } ngx_http_geo_high_ranges_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
29 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
30 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
31 typedef struct { |
2399 | 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; | |
589 | 40 } ngx_http_geo_conf_ctx_t; |
485 | 41 |
42 | |
2399 | 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); | |
485 | 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); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
57 static char *ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
58 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
59 static char *ngx_http_geo_add_range(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
60 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
61 static ngx_uint_t ngx_http_geo_delete_range(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
62 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
63 static char *ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
64 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
65 static ngx_http_variable_value_t *ngx_http_geo_value(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
66 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *value); |
485 | 67 |
68 | |
69 static ngx_command_t ngx_http_geo_commands[] = { | |
70 | |
71 { ngx_string("geo"), | |
2399 | 72 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, |
485 | 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 = { | |
509 | 83 NULL, /* preconfiguration */ |
84 NULL, /* postconfiguration */ | |
485 | 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 = { | |
509 | 98 NGX_MODULE_V1, |
485 | 99 &ngx_http_geo_module_ctx, /* module context */ |
100 ngx_http_geo_commands, /* module directives */ | |
101 NGX_HTTP_MODULE, /* module type */ | |
541 | 102 NULL, /* init master */ |
485 | 103 NULL, /* init module */ |
541 | 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 | |
485 | 110 }; |
111 | |
112 | |
113 /* AF_INET only */ | |
114 | |
573 | 115 static ngx_int_t |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
116 ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
573 | 117 uintptr_t data) |
485 | 118 { |
2399 | 119 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data; |
485 | 120 |
573 | 121 ngx_http_variable_value_t *vv; |
485 | 122 |
573 | 123 vv = (ngx_http_variable_value_t *) |
2399 | 124 ngx_radix32tree_find(ctx->u.tree, ngx_http_geo_addr(r, ctx)); |
489 | 125 |
573 | 126 *v = *vv; |
127 | |
2399 | 128 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
129 "http geo: %v", v); | |
489 | 130 |
573 | 131 return NGX_OK; |
485 | 132 } |
133 | |
134 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
135 static ngx_int_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
136 ngx_http_geo_range_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
137 uintptr_t data) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
138 { |
2399 | 139 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
140 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
141 in_addr_t addr; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
142 ngx_uint_t i, n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
143 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
144 |
2399 | 145 *v = *ctx->u.high->default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
146 |
2399 | 147 addr = ngx_http_geo_addr(r, ctx); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
148 |
2399 | 149 range = ctx->u.high->low[addr >> 16].ranges; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
150 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
151 n = addr & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
152 |
2399 | 153 for (i = 0; i < ctx->u.high->low[addr >> 16].n; i++) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
154 if (n >= (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
155 && n <= (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
156 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
157 *v = *range[i].value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
158 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
159 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
160 |
2399 | 161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
162 "http geo: %v", v); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
163 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
164 return NGX_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
165 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
166 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
167 |
2399 | 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 | |
489 | 198 static char * |
199 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 200 { |
589 | 201 char *rv; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
202 size_t len; |
2399 | 203 ngx_str_t *value, name; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
204 ngx_uint_t i; |
589 | 205 ngx_conf_t save; |
206 ngx_pool_t *pool; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
207 ngx_array_t *a; |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
208 ngx_http_variable_t *var; |
2399 | 209 ngx_http_geo_ctx_t *geo; |
589 | 210 ngx_http_geo_conf_ctx_t ctx; |
485 | 211 |
501 | 212 value = cf->args->elts; |
213 | |
2399 | 214 geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t)); |
215 if (geo == NULL) { | |
216 return NGX_CONF_ERROR; | |
217 } | |
218 | |
219 name = value[1]; | |
220 name.len--; | |
221 name.data++; | |
222 | |
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]; | |
231 name.len--; | |
232 name.data++; | |
233 | |
234 } else { | |
235 geo->index = -1; | |
236 } | |
237 | |
238 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
501 | 239 if (var == NULL) { |
485 | 240 return NGX_CONF_ERROR; |
241 } | |
242 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
243 pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
244 if (pool == NULL) { |
485 | 245 return NGX_CONF_ERROR; |
246 } | |
247 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
248 ctx.temp_pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
249 if (ctx.temp_pool == NULL) { |
485 | 250 return NGX_CONF_ERROR; |
251 } | |
252 | |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
253 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, |
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
254 ngx_http_variable_value_rbtree_insert); |
485 | 255 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
256 ctx.high = NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
257 ctx.tree = NULL; |
589 | 258 ctx.pool = cf->pool; |
485 | 259 |
260 save = *cf; | |
261 cf->pool = pool; | |
589 | 262 cf->ctx = &ctx; |
485 | 263 cf->handler = ngx_http_geo; |
264 cf->handler_conf = conf; | |
265 | |
266 rv = ngx_conf_parse(cf, NULL); | |
267 | |
268 *cf = save; | |
269 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
270 if (ctx.high) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
271 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
272 for (i = 0; i < 0x10000; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
273 a = (ngx_array_t *) ctx.high->low[i].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
274 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
275 if (a == NULL || a->nelts == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
276 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
277 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
278 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
279 ctx.high->low[i].n = a->nelts; |
485 | 280 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
281 len = a->nelts * sizeof(ngx_http_geo_range_t); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
282 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
283 ctx.high->low[i].ranges = ngx_palloc(cf->pool, len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
284 if (ctx.high->low[i].ranges == NULL ){ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
285 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
286 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
287 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
288 ngx_memcpy(ctx.high->low[i].ranges, a->elts, len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
289 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
290 |
2399 | 291 geo->u.high = ctx.high; |
292 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
293 var->get_handler = ngx_http_geo_range_variable; |
2399 | 294 var->data = (uintptr_t) geo; |
485 | 295 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
296 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
297 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
298 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
299 if (ctx.high->default_value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
300 ctx.high->default_value = &ngx_http_variable_null_value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
301 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
302 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
303 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
304 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
305 ctx.tree = ngx_radix_tree_create(cf->pool, -1); |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
306 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
307 return NGX_CONF_ERROR; |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
308 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
309 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
310 |
2399 | 311 geo->u.tree = ctx.tree; |
312 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
313 var->get_handler = ngx_http_geo_cidr_variable; |
2399 | 314 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
315 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
316 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
317 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
318 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
319 if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
320 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
321 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
322 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
323 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
324 (uintptr_t) &ngx_http_variable_null_value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
325 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
326 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
327 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
328 } |
485 | 329 } |
330 | |
331 return rv; | |
332 } | |
333 | |
334 | |
489 | 335 static char * |
336 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
485 | 337 { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
338 char *rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
339 ngx_str_t *value, file; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
340 ngx_http_geo_conf_ctx_t *ctx; |
485 | 341 |
589 | 342 ctx = cf->ctx; |
485 | 343 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
344 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
345 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
346 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
347 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
348 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
349 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
350 if (ctx->tree) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
351 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
352 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
353 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
354 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
355 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
356 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
357 ctx->high = ngx_pcalloc(ctx->pool, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
358 sizeof(ngx_http_geo_high_ranges_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
359 if (ctx->high == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
360 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
361 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
362 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
363 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
364 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
365 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
366 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
367 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
368 |
485 | 369 if (cf->args->nelts != 2) { |
370 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
371 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
372 goto failed; |
485 | 373 } |
374 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
375 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
376 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
377 file.len = value[1].len++; |
485 | 378 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
379 file.data = ngx_pstrdup(ctx->temp_pool, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
380 if (file.data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
381 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
382 } |
485 | 383 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
384 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK){ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
385 goto failed; |
485 | 386 } |
387 | |
388 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
389 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
390 rv = ngx_conf_parse(cf, &file); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
391 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
392 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
393 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
394 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
395 if (ctx->high) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
396 rv = ngx_http_geo_range(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
397 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
398 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
399 rv = ngx_http_geo_cidr(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
400 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
401 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
402 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
403 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
404 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
405 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
406 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
407 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
408 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
409 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
410 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
411 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
412 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
413 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
414 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
415 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
416 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
417 ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
418 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
419 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
420 u_char *p, *last; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
421 in_addr_t start, end; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
422 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
423 ngx_uint_t del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
424 ngx_http_variable_value_t *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
425 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
426 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
427 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
428 old = ctx->high->default_value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
429 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
430 ctx->high->default_value = ngx_http_geo_value(cf, ctx, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
431 if (ctx->high->default_value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
432 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
433 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
434 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
435 if (old) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
436 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
437 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
438 &value[0], ctx->high->default_value, old); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
439 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
440 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
441 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
442 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
443 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
444 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
445 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
446 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
447 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
448 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
449 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
450 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
451 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
452 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
453 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
454 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
455 p = ngx_strlchr(net->data, last, '-'); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
456 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
457 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
458 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
459 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
460 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
461 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
462 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
463 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
464 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
465 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
466 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
467 start = ntohl(start); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
468 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
469 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
470 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
471 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
472 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
473 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
474 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
475 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
476 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
477 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
478 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
479 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
480 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
481 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
482 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
483 if (del) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
484 if (ngx_http_geo_delete_range(cf, ctx, start, end)) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
485 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
486 "no address range \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
487 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
488 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
489 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
490 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
491 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
492 ctx->value = ngx_http_geo_value(cf, ctx, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
493 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
494 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
495 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
496 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
497 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
498 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
499 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
500 return ngx_http_geo_add_range(cf, ctx, start, end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
501 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
502 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
503 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
505 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
506 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
507 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
508 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
509 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
510 /* the add procedure is optimized to add a growing up sequence */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
511 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
512 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
513 ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
514 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
515 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
516 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
517 ngx_uint_t h, i, s, e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
518 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
519 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
520 |
2451 | 521 for (n = start; n <= end; n += 0x10000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
522 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
523 h = n >> 16; |
2447
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
524 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
525 if (n == start) { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
526 s = n & 0xffff; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
527 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
528 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
529 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
530 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
531 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
532 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
533 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
534 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
535 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
536 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
537 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
538 a = (ngx_array_t *) ctx->high->low[h].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
539 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
540 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
541 a = ngx_array_create(ctx->temp_pool, 64, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
542 sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
543 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
544 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
545 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
546 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
547 ctx->high->low[h].ranges = (ngx_http_geo_range_t *) a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
548 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
549 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
550 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
551 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
552 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
553 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
554 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
555 i--; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
556 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
557 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
558 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
559 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
560 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
561 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
562 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
563 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
564 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
565 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
566 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
567 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
568 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
569 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
570 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
571 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
572 ngx_memcpy(&range[i + 2], &range[i + 1], |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
573 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
574 |
2442 | 575 range[i + 1].start = (u_short) s; |
576 range[i + 1].end = (u_short) e; | |
577 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
579 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
580 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
581 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
583 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
584 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
585 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 ctx->net, ctx->value, range[i].value); |
2442 | 588 |
589 range[i].value = ctx->value; | |
590 | |
591 goto next; | |
592 } | |
593 | |
594 if (s > (ngx_uint_t) range[i].start | |
595 && e < (ngx_uint_t) range[i].end) | |
596 { | |
597 /* split the range and insert the new one */ | |
598 | |
599 range = ngx_array_push(a); | |
600 if (range == NULL) { | |
601 return NGX_CONF_ERROR; | |
602 } | |
603 | |
604 range = ngx_array_push(a); | |
605 if (range == NULL) { | |
606 return NGX_CONF_ERROR; | |
607 } | |
608 | |
609 range = a->elts; | |
610 | |
611 ngx_memcpy(&range[i + 3], &range[i + 1], | |
612 (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t)); | |
613 | |
614 range[i + 2].start = (u_short) (e + 1); | |
615 range[i + 2].end = range[i].end; | |
616 range[i + 2].value = range[i].value; | |
617 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
618 range[i + 1].start = (u_short) s; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
619 range[i + 1].end = (u_short) e; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
620 range[i + 1].value = ctx->value; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
621 |
2442 | 622 range[i].end = (u_short) (s - 1); |
623 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
624 goto next; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
625 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
626 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
627 if (s == (ngx_uint_t) range[i].start |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
628 && e < (ngx_uint_t) range[i].end) |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
629 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
630 /* shift the range start and insert the new range */ |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
631 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
632 range = ngx_array_push(a); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
633 if (range == NULL) { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
634 return NGX_CONF_ERROR; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
635 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
636 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
637 range = a->elts; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
638 |
2457
1aff348d9aa8
fix segfault when geo range replaces starting part of another range
Igor Sysoev <igor@sysoev.ru>
parents:
2452
diff
changeset
|
639 ngx_memcpy(&range[i + 1], &range[i], |
1aff348d9aa8
fix segfault when geo range replaces starting part of another range
Igor Sysoev <igor@sysoev.ru>
parents:
2452
diff
changeset
|
640 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
641 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
642 range[i + 1].start = (u_short) (e + 1); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
643 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
644 range[i].start = (u_short) s; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
645 range[i].end = (u_short) e; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
646 range[i].value = ctx->value; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
647 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
648 goto next; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
649 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
650 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
651 if (s > (ngx_uint_t) range[i].start |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
652 && e == (ngx_uint_t) range[i].end) |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
653 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
654 /* shift the range end and insert the new range */ |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
655 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
656 range = ngx_array_push(a); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
657 if (range == NULL) { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
658 return NGX_CONF_ERROR; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
659 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
660 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
661 range = a->elts; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
662 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
663 ngx_memcpy(&range[i + 2], &range[i + 1], |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
664 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
665 |
2442 | 666 range[i + 1].start = (u_short) s; |
667 range[i + 1].end = (u_short) e; | |
668 range[i + 1].value = ctx->value; | |
669 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
670 range[i].end = (u_short) (s - 1); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
671 |
2442 | 672 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
673 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
674 |
2448 | 675 s = (ngx_uint_t) range[i].start; |
676 e = (ngx_uint_t) range[i].end; | |
677 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 679 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
680 ctx->net, | |
681 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
682 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
683 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
684 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
686 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
687 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
689 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
690 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
692 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
693 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 range->start = (u_short) s; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
695 range->end = (u_short) e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
696 range->value = ctx->value; |
2442 | 697 |
698 next: | |
699 | |
700 continue; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
701 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
702 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
703 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
704 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
705 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
706 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 static ngx_uint_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
708 ngx_http_geo_delete_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
711 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
712 ngx_uint_t h, i, s, e, warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
713 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
715 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
716 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
717 |
2451 | 718 for (n = start; n <= end; n += 0x10000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
719 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
720 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
721 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
722 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
723 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
724 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
725 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
726 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
727 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
728 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
729 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
730 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
731 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
732 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
733 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
734 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
735 a = (ngx_array_t *) ctx->high->low[h].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
736 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
737 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
738 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
739 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
740 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
741 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
742 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
743 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
744 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
745 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
746 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
747 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
748 ngx_memcpy(&range[i], &range[i + 1], |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
749 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2450 | 750 |
751 a->nelts--; | |
752 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
753 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
754 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
755 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
756 if (s != (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
757 && e != (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
758 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
759 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
760 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
761 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
762 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
763 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
764 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
765 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
766 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
767 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
768 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
769 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
770 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
771 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
772 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
773 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
774 ngx_int_t rc, del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
775 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
776 ngx_uint_t i; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
777 ngx_inet_cidr_t cidrin; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
778 ngx_http_variable_value_t *val, *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
779 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
780 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
781 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
782 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
783 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
784 } |
485 | 785 } |
786 | |
787 if (ngx_strcmp(value[0].data, "default") == 0) { | |
788 cidrin.addr = 0; | |
789 cidrin.mask = 0; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
790 net = &value[0]; |
485 | 791 |
792 } else { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
793 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
794 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
795 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
796 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
797 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
798 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
799 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
800 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
801 |
2399 | 802 if (ngx_strcmp(net->data, "255.255.255.255") == 0) { |
803 cidrin.addr = 0xffffffff; | |
804 cidrin.mask = 0xffffffff; | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
805 |
2399 | 806 } else { |
2401 | 807 rc = ngx_ptocidr(net, &cidrin); |
485 | 808 |
2401 | 809 if (rc == NGX_ERROR) { |
810 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
811 "invalid network \"%V\"", net); | |
812 return NGX_CONF_ERROR; | |
813 } | |
2399 | 814 |
2401 | 815 if (rc == NGX_DONE) { |
816 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
817 "low address bits of %V are meaningless", | |
2399 | 818 net); |
2401 | 819 } |
2399 | 820 |
2401 | 821 cidrin.addr = ntohl(cidrin.addr); |
822 cidrin.mask = ntohl(cidrin.mask); | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
823 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
824 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
825 if (del) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
826 if (ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
827 != NGX_OK) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
828 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
829 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
830 "no network \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
831 } |
2365 | 832 |
833 return NGX_CONF_OK; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
834 } |
485 | 835 } |
836 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
837 val = ngx_http_geo_value(cf, ctx, &value[1]); |
485 | 838 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
839 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
840 return NGX_CONF_ERROR; |
485 | 841 } |
842 | |
553 | 843 for (i = 2; i; i--) { |
589 | 844 rc = ngx_radix32tree_insert(ctx->tree, cidrin.addr, cidrin.mask, |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
845 (uintptr_t) val); |
553 | 846 if (rc == NGX_OK) { |
847 return NGX_CONF_OK; | |
848 } | |
849 | |
850 if (rc == NGX_ERROR) { | |
851 return NGX_CONF_ERROR; | |
852 } | |
853 | |
854 /* rc == NGX_BUSY */ | |
855 | |
856 old = (ngx_http_variable_value_t *) | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
857 ngx_radix32tree_find(ctx->tree, cidrin.addr & cidrin.mask); |
553 | 858 |
859 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
860 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
861 net, val, old); |
553 | 862 |
589 | 863 rc = ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask); |
553 | 864 |
865 if (rc == NGX_ERROR) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
866 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
553 | 867 return NGX_CONF_ERROR; |
868 } | |
485 | 869 } |
870 | |
553 | 871 return NGX_CONF_ERROR; |
485 | 872 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
873 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
874 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
875 static ngx_http_variable_value_t * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
876 ngx_http_geo_value(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
877 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
878 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
879 uint32_t hash; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
880 ngx_http_variable_value_t *val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
881 ngx_http_variable_value_node_t *vvn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
882 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
883 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
884 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
885 val = ngx_http_variable_value_lookup(&ctx->rbtree, value, hash); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
886 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
887 if (val) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
888 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
889 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
890 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
891 val = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
892 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
893 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
894 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
895 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
896 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
897 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
898 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
899 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
900 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
901 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
902 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
903 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
904 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
905 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
906 vvn = ngx_palloc(ctx->temp_pool, sizeof(ngx_http_variable_value_node_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 if (vvn == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
908 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
910 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
911 vvn->node.key = hash; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
912 vvn->len = val->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
913 vvn->value = val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
914 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
915 ngx_rbtree_insert(&ctx->rbtree, &vvn->node); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
916 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
917 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
918 } |