Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_geo_module.c @ 6682:db422604ceb0
Stream: allow using the session context inside handlers.
Previously, it was not possible to use the stream context
inside ngx_stream_init_connection() handlers. Now, limit_conn,
access handlers, as well as those added later, can create
their own contexts.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 06 Sep 2016 21:28:17 +0300 |
parents | 0ee6f023ef92 |
children | 577628e6b6a6 |
rev | line source |
---|---|
485 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
485 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
6631 | 10 #include <ngx_stream.h> |
485 | 11 |
12 | |
13 typedef struct { | |
6631 | 14 ngx_stream_variable_value_t *value; |
15 u_short start; | |
16 u_short end; | |
17 } ngx_stream_geo_range_t; | |
2340
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
20 typedef struct { |
6631 | 21 ngx_radix_tree_t *tree; |
4992 | 22 #if (NGX_HAVE_INET6) |
6631 | 23 ngx_radix_tree_t *tree6; |
4992 | 24 #endif |
6631 | 25 } ngx_stream_geo_trees_t; |
4992 | 26 |
27 | |
28 typedef struct { | |
6631 | 29 ngx_stream_geo_range_t **low; |
30 ngx_stream_variable_value_t *default_value; | |
31 } ngx_stream_geo_high_ranges_t; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
32 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
33 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
34 typedef struct { |
6631 | 35 ngx_str_node_t sn; |
36 ngx_stream_variable_value_t *value; | |
37 size_t offset; | |
38 } ngx_stream_geo_variable_value_node_t; | |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
39 |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
40 |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
41 typedef struct { |
6631 | 42 ngx_stream_variable_value_t *value; |
43 ngx_str_t *net; | |
44 ngx_stream_geo_high_ranges_t high; | |
45 ngx_radix_tree_t *tree; | |
4992 | 46 #if (NGX_HAVE_INET6) |
6631 | 47 ngx_radix_tree_t *tree6; |
4992 | 48 #endif |
6631 | 49 ngx_rbtree_t rbtree; |
50 ngx_rbtree_node_t sentinel; | |
51 ngx_pool_t *pool; | |
52 ngx_pool_t *temp_pool; | |
3652 | 53 |
6631 | 54 size_t data_size; |
3652 | 55 |
6631 | 56 ngx_str_t include_name; |
57 ngx_uint_t includes; | |
58 ngx_uint_t entries; | |
3652 | 59 |
6631 | 60 unsigned ranges:1; |
61 unsigned outside_entries:1; | |
62 unsigned allow_binary_include:1; | |
63 unsigned binary_include:1; | |
64 } ngx_stream_geo_conf_ctx_t; | |
485 | 65 |
66 | |
2399 | 67 typedef struct { |
68 union { | |
6631 | 69 ngx_stream_geo_trees_t trees; |
70 ngx_stream_geo_high_ranges_t high; | |
2399 | 71 } u; |
72 | |
6631 | 73 ngx_int_t index; |
74 } ngx_stream_geo_ctx_t; | |
2399 | 75 |
76 | |
6631 | 77 static ngx_int_t ngx_stream_geo_addr(ngx_stream_session_t *s, |
78 ngx_stream_geo_ctx_t *ctx, ngx_addr_t *addr); | |
79 | |
80 static char *ngx_stream_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, | |
81 void *conf); | |
82 static char *ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf); | |
83 static char *ngx_stream_geo_range(ngx_conf_t *cf, | |
84 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value); | |
85 static char *ngx_stream_geo_add_range(ngx_conf_t *cf, | |
86 ngx_stream_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); | |
87 static ngx_uint_t ngx_stream_geo_delete_range(ngx_conf_t *cf, | |
88 ngx_stream_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); | |
89 static char *ngx_stream_geo_cidr(ngx_conf_t *cf, | |
90 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value); | |
91 static char *ngx_stream_geo_cidr_add(ngx_conf_t *cf, | |
92 ngx_stream_geo_conf_ctx_t *ctx, ngx_cidr_t *cidr, ngx_str_t *value, | |
93 ngx_str_t *net); | |
94 static ngx_stream_variable_value_t *ngx_stream_geo_value(ngx_conf_t *cf, | |
95 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *value); | |
96 static ngx_int_t ngx_stream_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
97 ngx_cidr_t *cidr); |
6631 | 98 static char *ngx_stream_geo_include(ngx_conf_t *cf, |
99 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name); | |
100 static ngx_int_t ngx_stream_geo_include_binary_base(ngx_conf_t *cf, | |
101 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name); | |
102 static void ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx); | |
103 static u_char *ngx_stream_geo_copy_values(u_char *base, u_char *p, | |
3652 | 104 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); |
485 | 105 |
106 | |
6631 | 107 static ngx_command_t ngx_stream_geo_commands[] = { |
485 | 108 |
109 { ngx_string("geo"), | |
6631 | 110 NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, |
111 ngx_stream_geo_block, | |
112 0, | |
485 | 113 0, |
114 NULL }, | |
115 | |
116 ngx_null_command | |
117 }; | |
118 | |
119 | |
6631 | 120 static ngx_stream_module_t ngx_stream_geo_module_ctx = { |
509 | 121 NULL, /* preconfiguration */ |
122 NULL, /* postconfiguration */ | |
485 | 123 |
124 NULL, /* create main configuration */ | |
125 NULL, /* init main configuration */ | |
126 | |
127 NULL, /* create server configuration */ | |
6631 | 128 NULL /* merge server configuration */ |
485 | 129 }; |
130 | |
131 | |
6631 | 132 ngx_module_t ngx_stream_geo_module = { |
509 | 133 NGX_MODULE_V1, |
6631 | 134 &ngx_stream_geo_module_ctx, /* module context */ |
135 ngx_stream_geo_commands, /* module directives */ | |
136 NGX_STREAM_MODULE, /* module type */ | |
541 | 137 NULL, /* init master */ |
485 | 138 NULL, /* init module */ |
541 | 139 NULL, /* init process */ |
140 NULL, /* init thread */ | |
141 NULL, /* exit thread */ | |
142 NULL, /* exit process */ | |
143 NULL, /* exit master */ | |
144 NGX_MODULE_V1_PADDING | |
485 | 145 }; |
146 | |
147 | |
3652 | 148 typedef struct { |
149 u_char GEORNG[6]; | |
150 u_char version; | |
151 u_char ptr_size; | |
4681
bb37a9cc08fb
Fixed spelling of "endianness", and called it "byte ordering" in the
Ruslan Ermilov <ru@nginx.com>
parents:
4626
diff
changeset
|
152 uint32_t endianness; |
3652 | 153 uint32_t crc32; |
6631 | 154 } ngx_stream_geo_header_t; |
3652 | 155 |
156 | |
6631 | 157 static ngx_stream_geo_header_t ngx_stream_geo_header = { |
3652 | 158 { 'G', 'E', 'O', 'R', 'N', 'G' }, 0, sizeof(void *), 0x12345678, 0 |
159 }; | |
160 | |
161 | |
4992 | 162 /* geo range is AF_INET only */ |
485 | 163 |
573 | 164 static ngx_int_t |
6631 | 165 ngx_stream_geo_cidr_variable(ngx_stream_session_t *s, |
166 ngx_stream_variable_value_t *v, uintptr_t data) | |
485 | 167 { |
6631 | 168 ngx_stream_geo_ctx_t *ctx = (ngx_stream_geo_ctx_t *) data; |
485 | 169 |
6631 | 170 in_addr_t inaddr; |
171 ngx_addr_t addr; | |
172 struct sockaddr_in *sin; | |
173 ngx_stream_variable_value_t *vv; | |
4992 | 174 #if (NGX_HAVE_INET6) |
6631 | 175 u_char *p; |
176 struct in6_addr *inaddr6; | |
4992 | 177 #endif |
178 | |
6631 | 179 if (ngx_stream_geo_addr(s, ctx, &addr) != NGX_OK) { |
180 vv = (ngx_stream_variable_value_t *) | |
4992 | 181 ngx_radix32tree_find(ctx->u.trees.tree, INADDR_NONE); |
182 goto done; | |
183 } | |
184 | |
185 switch (addr.sockaddr->sa_family) { | |
186 | |
187 #if (NGX_HAVE_INET6) | |
188 case AF_INET6: | |
189 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; | |
190 p = inaddr6->s6_addr; | |
485 | 191 |
4992 | 192 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { |
193 inaddr = p[12] << 24; | |
194 inaddr += p[13] << 16; | |
195 inaddr += p[14] << 8; | |
196 inaddr += p[15]; | |
197 | |
6631 | 198 vv = (ngx_stream_variable_value_t *) |
4992 | 199 ngx_radix32tree_find(ctx->u.trees.tree, inaddr); |
200 | |
201 } else { | |
6631 | 202 vv = (ngx_stream_variable_value_t *) |
4992 | 203 ngx_radix128tree_find(ctx->u.trees.tree6, p); |
204 } | |
205 | |
206 break; | |
207 #endif | |
208 | |
209 default: /* AF_INET */ | |
210 sin = (struct sockaddr_in *) addr.sockaddr; | |
211 inaddr = ntohl(sin->sin_addr.s_addr); | |
212 | |
6631 | 213 vv = (ngx_stream_variable_value_t *) |
4992 | 214 ngx_radix32tree_find(ctx->u.trees.tree, inaddr); |
215 | |
216 break; | |
217 } | |
218 | |
219 done: | |
489 | 220 |
573 | 221 *v = *vv; |
222 | |
6631 | 223 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
224 "stream geo: %v", v); | |
489 | 225 |
573 | 226 return NGX_OK; |
485 | 227 } |
228 | |
229 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
230 static ngx_int_t |
6631 | 231 ngx_stream_geo_range_variable(ngx_stream_session_t *s, |
232 ngx_stream_variable_value_t *v, uintptr_t data) | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
233 { |
6631 | 234 ngx_stream_geo_ctx_t *ctx = (ngx_stream_geo_ctx_t *) data; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
235 |
6631 | 236 in_addr_t inaddr; |
237 ngx_addr_t addr; | |
238 ngx_uint_t n; | |
239 struct sockaddr_in *sin; | |
240 ngx_stream_geo_range_t *range; | |
4992 | 241 #if (NGX_HAVE_INET6) |
6631 | 242 u_char *p; |
243 struct in6_addr *inaddr6; | |
4992 | 244 #endif |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
245 |
3652 | 246 *v = *ctx->u.high.default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
247 |
6631 | 248 if (ngx_stream_geo_addr(s, ctx, &addr) == NGX_OK) { |
4992 | 249 |
250 switch (addr.sockaddr->sa_family) { | |
251 | |
252 #if (NGX_HAVE_INET6) | |
253 case AF_INET6: | |
254 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; | |
255 | |
256 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { | |
257 p = inaddr6->s6_addr; | |
258 | |
259 inaddr = p[12] << 24; | |
260 inaddr += p[13] << 16; | |
261 inaddr += p[14] << 8; | |
262 inaddr += p[15]; | |
263 | |
264 } else { | |
265 inaddr = INADDR_NONE; | |
266 } | |
267 | |
268 break; | |
269 #endif | |
270 | |
271 default: /* AF_INET */ | |
272 sin = (struct sockaddr_in *) addr.sockaddr; | |
273 inaddr = ntohl(sin->sin_addr.s_addr); | |
274 break; | |
275 } | |
276 | |
277 } else { | |
278 inaddr = INADDR_NONE; | |
279 } | |
280 | |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
281 if (ctx->u.high.low) { |
4992 | 282 range = ctx->u.high.low[inaddr >> 16]; |
4989 | 283 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
284 if (range) { |
4992 | 285 n = inaddr & 0xffff; |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
286 do { |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
287 if (n >= (ngx_uint_t) range->start |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
288 && n <= (ngx_uint_t) range->end) |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
289 { |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
290 *v = *range->value; |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
291 break; |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
292 } |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
293 } while ((++range)->value); |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
294 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
295 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
296 |
6631 | 297 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
298 "stream geo: %v", v); | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
299 |
4992 | 300 return NGX_OK; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
301 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
302 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
303 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
304 static ngx_int_t |
6631 | 305 ngx_stream_geo_addr(ngx_stream_session_t *s, ngx_stream_geo_ctx_t *ctx, |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
306 ngx_addr_t *addr) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
307 { |
6631 | 308 ngx_stream_variable_value_t *v; |
2399 | 309 |
310 if (ctx->index == -1) { | |
6631 | 311 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
312 "stream geo started: %V", &s->connection->addr_text); | |
2399 | 313 |
6631 | 314 addr->sockaddr = s->connection->sockaddr; |
315 addr->socklen = s->connection->socklen; | |
316 /* addr->name = s->connection->addr_text; */ | |
3920
bfe4af74a4f4
support IPv4 mapped to IPv6 in geo module
Igor Sysoev <igor@sysoev.ru>
parents:
3887
diff
changeset
|
317 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
318 return NGX_OK; |
2399 | 319 } |
320 | |
6631 | 321 v = ngx_stream_get_flushed_variable(s, ctx->index); |
2399 | 322 |
323 if (v == NULL || v->not_found) { | |
6631 | 324 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
325 "stream geo not found"); | |
2399 | 326 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
327 return NGX_ERROR; |
2399 | 328 } |
329 | |
6631 | 330 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
331 "stream geo started: %v", v); | |
2399 | 332 |
6631 | 333 if (ngx_parse_addr(s->connection->pool, addr, v->data, v->len) == NGX_OK) { |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
334 return NGX_OK; |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
335 } |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
336 |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
337 return NGX_ERROR; |
2399 | 338 } |
339 | |
340 | |
489 | 341 static char * |
6631 | 342 ngx_stream_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
485 | 343 { |
6631 | 344 char *rv; |
345 size_t len; | |
346 ngx_str_t *value, name; | |
347 ngx_uint_t i; | |
348 ngx_conf_t save; | |
349 ngx_pool_t *pool; | |
350 ngx_array_t *a; | |
351 ngx_stream_variable_t *var; | |
352 ngx_stream_geo_ctx_t *geo; | |
353 ngx_stream_geo_conf_ctx_t ctx; | |
4992 | 354 #if (NGX_HAVE_INET6) |
6631 | 355 static struct in6_addr zero; |
4992 | 356 #endif |
485 | 357 |
501 | 358 value = cf->args->elts; |
359 | |
6631 | 360 geo = ngx_palloc(cf->pool, sizeof(ngx_stream_geo_ctx_t)); |
2399 | 361 if (geo == NULL) { |
362 return NGX_CONF_ERROR; | |
363 } | |
364 | |
365 name = value[1]; | |
4963
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
366 |
4972
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
367 if (name.data[0] != '$') { |
4963
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
368 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
369 "invalid variable name \"%V\"", &name); |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
370 return NGX_CONF_ERROR; |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
371 } |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
372 |
2399 | 373 name.len--; |
374 name.data++; | |
375 | |
376 if (cf->args->nelts == 3) { | |
377 | |
6631 | 378 geo->index = ngx_stream_get_variable_index(cf, &name); |
2399 | 379 if (geo->index == NGX_ERROR) { |
380 return NGX_CONF_ERROR; | |
381 } | |
382 | |
383 name = value[2]; | |
4972
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
384 |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
385 if (name.data[0] != '$') { |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
386 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
387 "invalid variable name \"%V\"", &name); |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
388 return NGX_CONF_ERROR; |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
389 } |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
390 |
2399 | 391 name.len--; |
392 name.data++; | |
393 | |
394 } else { | |
395 geo->index = -1; | |
396 } | |
397 | |
6631 | 398 var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE); |
501 | 399 if (var == NULL) { |
485 | 400 return NGX_CONF_ERROR; |
401 } | |
402 | |
5124
5482671df278
Use NGX_DEFAULT_POOL_SIZE macro where appropriate.
Ruslan Ermilov <ru@nginx.com>
parents:
5084
diff
changeset
|
403 pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
404 if (pool == NULL) { |
485 | 405 return NGX_CONF_ERROR; |
406 } | |
407 | |
6631 | 408 ngx_memzero(&ctx, sizeof(ngx_stream_geo_conf_ctx_t)); |
3652 | 409 |
5124
5482671df278
Use NGX_DEFAULT_POOL_SIZE macro where appropriate.
Ruslan Ermilov <ru@nginx.com>
parents:
5084
diff
changeset
|
410 ctx.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
411 if (ctx.temp_pool == NULL) { |
485 | 412 return NGX_CONF_ERROR; |
413 } | |
414 | |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
415 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); |
485 | 416 |
589 | 417 ctx.pool = cf->pool; |
6631 | 418 ctx.data_size = sizeof(ngx_stream_geo_header_t) |
419 + sizeof(ngx_stream_variable_value_t) | |
420 + 0x10000 * sizeof(ngx_stream_geo_range_t *); | |
3652 | 421 ctx.allow_binary_include = 1; |
485 | 422 |
423 save = *cf; | |
424 cf->pool = pool; | |
589 | 425 cf->ctx = &ctx; |
6631 | 426 cf->handler = ngx_stream_geo; |
485 | 427 cf->handler_conf = conf; |
428 | |
429 rv = ngx_conf_parse(cf, NULL); | |
430 | |
431 *cf = save; | |
432 | |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
433 if (ctx.ranges) { |
3652 | 434 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
435 if (ctx.high.low && !ctx.binary_include) { |
3652 | 436 for (i = 0; i < 0x10000; i++) { |
437 a = (ngx_array_t *) ctx.high.low[i]; | |
438 | |
6655
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
439 if (a == NULL) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
440 continue; |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
441 } |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
442 |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
443 if (a->nelts == 0) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
444 ctx.high.low[i] = NULL; |
3652 | 445 continue; |
446 } | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
447 |
6631 | 448 len = a->nelts * sizeof(ngx_stream_geo_range_t); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
449 |
3652 | 450 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); |
451 if (ctx.high.low[i] == NULL) { | |
452 return NGX_CONF_ERROR; | |
453 } | |
454 | |
4967
a9d60fafaa85
Geo: improved ngx_http_geo_block() code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
455 ngx_memcpy(ctx.high.low[i], a->elts, len); |
a9d60fafaa85
Geo: improved ngx_http_geo_block() code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
456 ctx.high.low[i][a->nelts].value = NULL; |
3652 | 457 ctx.data_size += len + sizeof(void *); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
458 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
459 |
3652 | 460 if (ctx.allow_binary_include |
461 && !ctx.outside_entries | |
462 && ctx.entries > 100000 | |
463 && ctx.includes == 1) | |
464 { | |
6631 | 465 ngx_stream_geo_create_binary_base(&ctx); |
2340
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 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
468 |
4816
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
469 if (ctx.high.default_value == NULL) { |
6631 | 470 ctx.high.default_value = &ngx_stream_variable_null_value; |
4816
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
471 } |
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
472 |
2399 | 473 geo->u.high = ctx.high; |
474 | |
6631 | 475 var->get_handler = ngx_stream_geo_range_variable; |
2399 | 476 var->data = (uintptr_t) geo; |
485 | 477 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
478 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
479 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
480 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
481 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
482 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
483 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
|
484 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
485 return NGX_CONF_ERROR; |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
486 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
487 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
488 |
4992 | 489 geo->u.trees.tree = ctx.tree; |
490 | |
491 #if (NGX_HAVE_INET6) | |
492 if (ctx.tree6 == NULL) { | |
493 ctx.tree6 = ngx_radix_tree_create(cf->pool, -1); | |
494 if (ctx.tree6 == NULL) { | |
495 return NGX_CONF_ERROR; | |
496 } | |
497 } | |
498 | |
499 geo->u.trees.tree6 = ctx.tree6; | |
500 #endif | |
2399 | 501 |
6631 | 502 var->get_handler = ngx_stream_geo_cidr_variable; |
2399 | 503 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
504 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
505 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
506 ngx_destroy_pool(pool); |
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 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
6631 | 509 (uintptr_t) &ngx_stream_variable_null_value) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
510 == NGX_ERROR) |
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 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
513 } |
4980
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
514 |
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
515 /* NGX_BUSY is okay (default was set explicitly) */ |
4992 | 516 |
517 #if (NGX_HAVE_INET6) | |
518 if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr, | |
6631 | 519 (uintptr_t) &ngx_stream_variable_null_value) |
4992 | 520 == NGX_ERROR) |
521 { | |
522 return NGX_CONF_ERROR; | |
523 } | |
524 #endif | |
485 | 525 } |
526 | |
527 return rv; | |
528 } | |
529 | |
530 | |
489 | 531 static char * |
6631 | 532 ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) |
485 | 533 { |
6631 | 534 char *rv; |
535 ngx_str_t *value; | |
536 ngx_stream_geo_conf_ctx_t *ctx; | |
485 | 537 |
589 | 538 ctx = cf->ctx; |
485 | 539 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
540 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
541 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
542 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
543 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
544 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
545 |
4992 | 546 if (ctx->tree |
547 #if (NGX_HAVE_INET6) | |
548 || ctx->tree6 | |
549 #endif | |
550 ) | |
551 { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
552 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
553 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
554 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
555 goto failed; |
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 |
3652 | 558 ctx->ranges = 1; |
2340
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 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
561 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
562 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
563 } |
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 |
485 | 566 if (cf->args->nelts != 2) { |
567 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
568 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
569 goto failed; |
485 | 570 } |
571 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
572 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
573 |
6631 | 574 rv = ngx_stream_geo_include(cf, ctx, &value[1]); |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
575 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
576 goto done; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
577 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 |
3652 | 579 if (ctx->ranges) { |
6631 | 580 rv = ngx_stream_geo_range(cf, ctx, value); |
2340
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 } else { |
6631 | 583 rv = ngx_stream_geo_cidr(cf, ctx, value); |
2340
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
588 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
589 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
590 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
592 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
593 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
594 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
595 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
596 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
597 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
598 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
599 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
600 static char * |
6631 | 601 ngx_stream_geo_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
602 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
603 { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
604 u_char *p, *last; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
605 in_addr_t start, end; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
606 ngx_str_t *net; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
607 ngx_uint_t del; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
608 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
609 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
610 |
3652 | 611 if (ctx->high.default_value) { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
612 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
613 "duplicate default geo range value: \"%V\", old value: \"%v\"", |
3652 | 614 &value[1], ctx->high.default_value); |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
615 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
616 |
6631 | 617 ctx->high.default_value = ngx_stream_geo_value(cf, ctx, &value[1]); |
3652 | 618 if (ctx->high.default_value == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
619 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
620 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
621 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
622 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
623 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
624 |
3652 | 625 if (ctx->binary_include) { |
626 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
4593
834049edae24
Fixed grammar in error messages.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
627 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 628 ctx->include_name.data); |
629 return NGX_CONF_ERROR; | |
630 } | |
631 | |
632 if (ctx->high.low == NULL) { | |
633 ctx->high.low = ngx_pcalloc(ctx->pool, | |
6631 | 634 0x10000 * sizeof(ngx_stream_geo_range_t *)); |
3652 | 635 if (ctx->high.low == NULL) { |
636 return NGX_CONF_ERROR; | |
637 } | |
638 } | |
639 | |
640 ctx->entries++; | |
641 ctx->outside_entries = 1; | |
642 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
643 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
644 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
645 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
646 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
647 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
648 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
649 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
650 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
651 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
652 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
653 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
654 p = ngx_strlchr(net->data, last, '-'); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
655 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
656 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
657 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
658 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
659 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
660 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
661 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
662 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
663 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
664 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
665 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
666 start = ntohl(start); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
667 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
668 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
669 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
670 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
671 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
672 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
673 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
674 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
675 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
676 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
679 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
680 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
681 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
682 if (del) { |
6631 | 683 if (ngx_stream_geo_delete_range(cf, ctx, start, end)) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
684 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 "no address range \"%V\" to delete", net); |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
689 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
690 |
6631 | 691 ctx->value = ngx_stream_geo_value(cf, ctx, &value[1]); |
2340
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 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
695 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
696 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
697 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 |
6631 | 699 return ngx_stream_geo_add_range(cf, ctx, start, end); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
700 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
701 invalid: |
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 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
|
704 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
705 return NGX_CONF_ERROR; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
708 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 /* 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
|
710 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
711 static char * |
6631 | 712 ngx_stream_geo_add_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
713 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 { |
6631 | 715 in_addr_t n; |
716 ngx_uint_t h, i, s, e; | |
717 ngx_array_t *a; | |
718 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
719 |
3456
91cff7f97a50
fix a geo range if the range includes two or more /16 networks
Igor Sysoev <igor@sysoev.ru>
parents:
3115
diff
changeset
|
720 for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
721 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
722 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
|
723 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
724 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
|
725 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
|
726 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
727 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
728 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
729 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
730 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
731 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
732 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
733 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
734 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
735 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
736 |
3652 | 737 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
738 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
739 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
740 a = ngx_array_create(ctx->temp_pool, 64, |
6631 | 741 sizeof(ngx_stream_geo_range_t)); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
742 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
743 return NGX_CONF_ERROR; |
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 |
6631 | 746 ctx->high.low[h] = (ngx_stream_geo_range_t *) a; |
2340
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
749 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
750 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
751 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
752 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
753 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
754 i--; |
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 (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
757 continue; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
760 if (s > (ngx_uint_t) range[i].end) { |
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 /* add after the range */ |
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 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
765 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
766 return NGX_CONF_ERROR; |
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 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
770 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
771 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 772 (a->nelts - 2 - i) * sizeof(ngx_stream_geo_range_t)); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
773 |
2442 | 774 range[i + 1].start = (u_short) s; |
775 range[i + 1].end = (u_short) e; | |
776 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
777 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
778 goto next; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
781 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
782 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
783 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
784 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
785 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
786 ctx->net, ctx->value, range[i].value); |
2442 | 787 |
788 range[i].value = ctx->value; | |
789 | |
790 goto next; | |
791 } | |
792 | |
793 if (s > (ngx_uint_t) range[i].start | |
794 && e < (ngx_uint_t) range[i].end) | |
795 { | |
796 /* split the range and insert the new one */ | |
797 | |
798 range = ngx_array_push(a); | |
799 if (range == NULL) { | |
800 return NGX_CONF_ERROR; | |
801 } | |
802 | |
803 range = ngx_array_push(a); | |
804 if (range == NULL) { | |
805 return NGX_CONF_ERROR; | |
806 } | |
807 | |
808 range = a->elts; | |
809 | |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
810 ngx_memmove(&range[i + 3], &range[i + 1], |
6631 | 811 (a->nelts - 3 - i) * sizeof(ngx_stream_geo_range_t)); |
2442 | 812 |
813 range[i + 2].start = (u_short) (e + 1); | |
814 range[i + 2].end = range[i].end; | |
815 range[i + 2].value = range[i].value; | |
816 | |
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
|
817 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
|
818 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
|
819 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
|
820 |
2442 | 821 range[i].end = (u_short) (s - 1); |
822 | |
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
|
823 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
|
824 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
825 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
826 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
|
827 && 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
|
828 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
829 /* 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
|
830 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
831 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
|
832 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
|
833 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
|
834 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
835 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
836 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
|
837 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
838 ngx_memmove(&range[i + 1], &range[i], |
6631 | 839 (a->nelts - 1 - i) * sizeof(ngx_stream_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
|
840 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
841 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
|
842 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
843 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
|
844 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
|
845 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
|
846 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
847 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
|
848 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
849 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
850 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
|
851 && 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
|
852 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
853 /* 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
|
854 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
855 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
|
856 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
|
857 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
|
858 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
859 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
860 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
|
861 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
862 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 863 (a->nelts - 2 - i) * sizeof(ngx_stream_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
|
864 |
2442 | 865 range[i + 1].start = (u_short) s; |
866 range[i + 1].end = (u_short) e; | |
867 range[i + 1].value = ctx->value; | |
868 | |
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
|
869 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
|
870 |
2442 | 871 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
872 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
873 |
2448 | 874 s = (ngx_uint_t) range[i].start; |
875 e = (ngx_uint_t) range[i].end; | |
876 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
877 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 878 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
879 ctx->net, | |
880 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
881 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
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 return NGX_CONF_ERROR; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
886 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
887 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
888 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
889 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
890 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
891 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
892 |
6663
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
893 range = a->elts; |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
894 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
895 ngx_memmove(&range[1], &range[0], |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
896 (a->nelts - 1) * sizeof(ngx_stream_geo_range_t)); |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
897 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
898 range[0].start = (u_short) s; |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
899 range[0].end = (u_short) e; |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
900 range[0].value = ctx->value; |
2442 | 901 |
902 next: | |
903 | |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
904 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
905 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
906 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
908 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 return NGX_CONF_OK; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
912 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
913 static ngx_uint_t |
6631 | 914 ngx_stream_geo_delete_range(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
915 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
916 { |
6631 | 917 in_addr_t n; |
918 ngx_uint_t h, i, s, e, warn; | |
919 ngx_array_t *a; | |
920 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
921 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
922 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
923 |
6662
1301a58b5dac
Geo: fixed removing a range in certain cases.
Ruslan Ermilov <ru@nginx.com>
parents:
6661
diff
changeset
|
924 for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
925 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
926 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
927 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
928 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
929 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
930 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
931 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
932 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
933 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
934 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
935 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
936 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
937 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
938 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
939 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
940 |
3652 | 941 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
942 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
943 if (a == NULL || a->nelts == 0) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
944 warn = 1; |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
945 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
946 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
947 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
948 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
949 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
950 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
951 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
952 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
953 { |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
954 ngx_memmove(&range[i], &range[i + 1], |
6631 | 955 (a->nelts - 1 - i) * sizeof(ngx_stream_geo_range_t)); |
2450 | 956 |
957 a->nelts--; | |
958 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
959 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
960 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
961 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
962 if (i == a->nelts - 1) { |
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
963 warn = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
964 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
965 } |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
966 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
967 next: |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
968 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
969 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
970 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
971 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
972 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
973 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
974 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
975 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
976 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
977 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
978 static char * |
6631 | 979 ngx_stream_geo_cidr(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
980 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
981 { |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
982 char *rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
983 ngx_int_t rc, del; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
984 ngx_str_t *net; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
985 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
986 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
987 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
988 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
989 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
990 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
991 } |
485 | 992 } |
993 | |
4992 | 994 #if (NGX_HAVE_INET6) |
995 if (ctx->tree6 == NULL) { | |
996 ctx->tree6 = ngx_radix_tree_create(ctx->pool, -1); | |
997 if (ctx->tree6 == NULL) { | |
998 return NGX_CONF_ERROR; | |
999 } | |
1000 } | |
1001 #endif | |
1002 | |
485 | 1003 if (ngx_strcmp(value[0].data, "default") == 0) { |
4993
a8cc59ead621
Geo: properly initialize ngx_cidr_t when dealing with "default".
Ruslan Ermilov <ru@nginx.com>
parents:
4992
diff
changeset
|
1004 cidr.family = AF_INET; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1005 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1006 cidr.u.in.mask = 0; |
485 | 1007 |
6631 | 1008 rv = ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]); |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1009 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1010 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1011 return rv; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1012 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1013 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1014 #if (NGX_HAVE_INET6) |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1015 cidr.family = AF_INET6; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1016 ngx_memzero(&cidr.u.in6, sizeof(ngx_in6_cidr_t)); |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
1017 |
6631 | 1018 rv = ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]); |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1019 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1020 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1021 return rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1022 } |
4992 | 1023 #endif |
1024 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1025 return NGX_CONF_OK; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1026 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1027 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1028 if (ngx_strcmp(value[0].data, "delete") == 0) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1029 net = &value[1]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1030 del = 1; |
4992 | 1031 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1032 } else { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1033 net = &value[0]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1034 del = 0; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1035 } |
2365 | 1036 |
6631 | 1037 if (ngx_stream_geo_cidr_value(cf, net, &cidr) != NGX_OK) { |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1038 return NGX_CONF_ERROR; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1039 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1040 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1041 if (cidr.family == AF_INET) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1042 cidr.u.in.addr = ntohl(cidr.u.in.addr); |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1043 cidr.u.in.mask = ntohl(cidr.u.in.mask); |
485 | 1044 } |
1045 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1046 if (del) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1047 switch (cidr.family) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1048 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1049 #if (NGX_HAVE_INET6) |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1050 case AF_INET6: |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1051 rc = ngx_radix128tree_delete(ctx->tree6, |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1052 cidr.u.in6.addr.s6_addr, |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1053 cidr.u.in6.mask.s6_addr); |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1054 break; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1055 #endif |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1056 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1057 default: /* AF_INET */ |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1058 rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1059 cidr.u.in.mask); |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1060 break; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1061 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1062 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1063 if (rc != NGX_OK) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1064 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1065 "no network \"%V\" to delete", net); |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1066 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1067 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1068 return NGX_CONF_OK; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1069 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1070 |
6631 | 1071 return ngx_stream_geo_cidr_add(cf, ctx, &cidr, &value[1], net); |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1072 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1073 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1074 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1075 static char * |
6631 | 1076 ngx_stream_geo_cidr_add(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1077 ngx_cidr_t *cidr, ngx_str_t *value, ngx_str_t *net) |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1078 { |
6631 | 1079 ngx_int_t rc; |
1080 ngx_stream_variable_value_t *val, *old; | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1081 |
6631 | 1082 val = ngx_stream_geo_value(cf, ctx, value); |
485 | 1083 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
1084 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1085 return NGX_CONF_ERROR; |
485 | 1086 } |
1087 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1088 switch (cidr->family) { |
4992 | 1089 |
1090 #if (NGX_HAVE_INET6) | |
1091 case AF_INET6: | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1092 rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1093 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1094 (uintptr_t) val); |
4992 | 1095 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1096 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1097 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1098 } |
4992 | 1099 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1100 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1101 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1102 } |
553 | 1103 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1104 /* rc == NGX_BUSY */ |
4992 | 1105 |
6631 | 1106 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1107 ngx_radix128tree_find(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1108 cidr->u.in6.addr.s6_addr); |
4992 | 1109 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1110 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1111 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1112 net, val, old); |
4992 | 1113 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1114 rc = ngx_radix128tree_delete(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1115 cidr->u.in6.addr.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1116 cidr->u.in6.mask.s6_addr); |
4992 | 1117 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1118 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1119 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1120 return NGX_CONF_ERROR; |
553 | 1121 } |
1122 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1123 rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1124 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1125 (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1126 |
4992 | 1127 break; |
1128 #endif | |
1129 | |
1130 default: /* AF_INET */ | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1131 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1132 cidr->u.in.mask, (uintptr_t) val); |
553 | 1133 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1134 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1135 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1136 } |
553 | 1137 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1138 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1139 return NGX_CONF_ERROR; |
553 | 1140 } |
4992 | 1141 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1142 /* rc == NGX_BUSY */ |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1143 |
6631 | 1144 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1145 ngx_radix32tree_find(ctx->tree, cidr->u.in.addr); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1146 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1147 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1148 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1149 net, val, old); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1150 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1151 rc = ngx_radix32tree_delete(ctx->tree, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1152 cidr->u.in.addr, cidr->u.in.mask); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1153 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1154 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1155 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1156 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1157 } |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1158 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1159 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1160 cidr->u.in.mask, (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1161 |
4992 | 1162 break; |
485 | 1163 } |
1164 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1165 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1166 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1167 } |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1168 |
553 | 1169 return NGX_CONF_ERROR; |
485 | 1170 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1171 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1172 |
6631 | 1173 static ngx_stream_variable_value_t * |
1174 ngx_stream_geo_value(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1175 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1176 { |
6631 | 1177 uint32_t hash; |
1178 ngx_stream_variable_value_t *val; | |
1179 ngx_stream_geo_variable_value_node_t *gvvn; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1180 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1181 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1182 |
6631 | 1183 gvvn = (ngx_stream_geo_variable_value_node_t *) |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1184 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1185 |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1186 if (gvvn) { |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1187 return gvvn->value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1188 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1189 |
6631 | 1190 val = ngx_palloc(ctx->pool, sizeof(ngx_stream_variable_value_t)); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1191 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1192 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1193 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1194 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1195 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1196 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1197 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1198 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1199 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1200 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1201 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1202 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1203 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1204 |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1205 gvvn = ngx_palloc(ctx->temp_pool, |
6631 | 1206 sizeof(ngx_stream_geo_variable_value_node_t)); |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1207 if (gvvn == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1208 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1209 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1210 |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1211 gvvn->sn.node.key = hash; |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1212 gvvn->sn.str.len = val->len; |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1213 gvvn->sn.str.data = val->data; |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1214 gvvn->value = val; |
3652 | 1215 gvvn->offset = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1216 |
3641
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
1217 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1218 |
6631 | 1219 ctx->data_size += ngx_align(sizeof(ngx_stream_variable_value_t) |
1220 + value->len, sizeof(void *)); | |
3652 | 1221 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1222 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1223 } |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1224 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1225 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1226 static ngx_int_t |
6631 | 1227 ngx_stream_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1228 { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1229 ngx_int_t rc; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1230 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1231 if (ngx_strcmp(net->data, "255.255.255.255") == 0) { |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
1232 cidr->family = AF_INET; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1233 cidr->u.in.addr = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1234 cidr->u.in.mask = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1235 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1236 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1237 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1238 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1239 rc = ngx_ptocidr(net, cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1240 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1241 if (rc == NGX_ERROR) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1242 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1243 return NGX_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1244 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1245 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1246 if (rc == NGX_DONE) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1247 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1248 "low address bits of %V are meaningless", net); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1249 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1250 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1251 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1252 } |
3652 | 1253 |
1254 | |
1255 static char * | |
6631 | 1256 ngx_stream_geo_include(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
3652 | 1257 ngx_str_t *name) |
1258 { | |
1259 char *rv; | |
1260 ngx_str_t file; | |
1261 | |
1262 file.len = name->len + 4; | |
1263 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5); | |
1264 if (file.data == NULL) { | |
1265 return NGX_CONF_ERROR; | |
1266 } | |
1267 | |
1268 ngx_sprintf(file.data, "%V.bin%Z", name); | |
1269 | |
5330
314c3d7cc3a5
Backed out f1a91825730a and 7094bd12c1ff.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5317
diff
changeset
|
1270 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { |
3652 | 1271 return NGX_CONF_ERROR; |
1272 } | |
1273 | |
3654
b56935e34273
test binary geo ranges base only for ranges
Igor Sysoev <igor@sysoev.ru>
parents:
3653
diff
changeset
|
1274 if (ctx->ranges) { |
3660 | 1275 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); |
3652 | 1276 |
6631 | 1277 switch (ngx_stream_geo_include_binary_base(cf, ctx, &file)) { |
3660 | 1278 case NGX_OK: |
1279 return NGX_CONF_OK; | |
1280 case NGX_ERROR: | |
1281 return NGX_CONF_ERROR; | |
1282 default: | |
1283 break; | |
1284 } | |
3652 | 1285 } |
1286 | |
1287 file.len -= 4; | |
1288 file.data[file.len] = '\0'; | |
1289 | |
1290 ctx->include_name = file; | |
1291 | |
1292 if (ctx->outside_entries) { | |
1293 ctx->allow_binary_include = 0; | |
1294 } | |
1295 | |
1296 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
1297 | |
1298 rv = ngx_conf_parse(cf, &file); | |
1299 | |
1300 ctx->includes++; | |
1301 ctx->outside_entries = 0; | |
1302 | |
1303 return rv; | |
1304 } | |
1305 | |
1306 | |
1307 static ngx_int_t | |
6631 | 1308 ngx_stream_geo_include_binary_base(ngx_conf_t *cf, |
1309 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name) | |
3652 | 1310 { |
6631 | 1311 u_char *base, ch; |
1312 time_t mtime; | |
1313 size_t size, len; | |
1314 ssize_t n; | |
1315 uint32_t crc32; | |
1316 ngx_err_t err; | |
1317 ngx_int_t rc; | |
1318 ngx_uint_t i; | |
1319 ngx_file_t file; | |
1320 ngx_file_info_t fi; | |
1321 ngx_stream_geo_range_t *range, **ranges; | |
1322 ngx_stream_geo_header_t *header; | |
1323 ngx_stream_variable_value_t *vv; | |
3652 | 1324 |
1325 ngx_memzero(&file, sizeof(ngx_file_t)); | |
1326 file.name = *name; | |
1327 file.log = cf->log; | |
1328 | |
1329 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0); | |
1330 if (file.fd == NGX_INVALID_FILE) { | |
1331 err = ngx_errno; | |
1332 if (err != NGX_ENOENT) { | |
1333 ngx_conf_log_error(NGX_LOG_CRIT, cf, err, | |
1334 ngx_open_file_n " \"%s\" failed", name->data); | |
1335 } | |
1336 return NGX_DECLINED; | |
1337 } | |
1338 | |
1339 if (ctx->outside_entries) { | |
1340 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
4593
834049edae24
Fixed grammar in error messages.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
1341 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 1342 name->data); |
1343 rc = NGX_ERROR; | |
1344 goto done; | |
1345 } | |
1346 | |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1347 if (ctx->binary_include) { |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1348 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
4593
834049edae24
Fixed grammar in error messages.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
1349 "second binary geo range base \"%s\" cannot be mixed with \"%s\"", |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1350 name->data, ctx->include_name.data); |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1351 rc = NGX_ERROR; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1352 goto done; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1353 } |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1354 |
3652 | 1355 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) { |
1356 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1357 ngx_fd_info_n " \"%s\" failed", name->data); | |
1358 goto failed; | |
1359 } | |
1360 | |
1361 size = (size_t) ngx_file_size(&fi); | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1362 mtime = ngx_file_mtime(&fi); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1363 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1364 ch = name->data[name->len - 4]; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1365 name->data[name->len - 4] = '\0'; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1366 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1367 if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1368 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1369 ngx_file_info_n " \"%s\" failed", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1370 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1371 } |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1372 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1373 name->data[name->len - 4] = ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1374 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1375 if (mtime < ngx_file_mtime(&fi)) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1376 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1377 "stale binary geo range base \"%s\"", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1378 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1379 } |
3652 | 1380 |
1381 base = ngx_palloc(ctx->pool, size); | |
1382 if (base == NULL) { | |
1383 goto failed; | |
1384 } | |
1385 | |
1386 n = ngx_read_file(&file, base, size, 0); | |
1387 | |
1388 if (n == NGX_ERROR) { | |
1389 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1390 ngx_read_file_n " \"%s\" failed", name->data); | |
1391 goto failed; | |
1392 } | |
1393 | |
1394 if ((size_t) n != size) { | |
1395 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0, | |
1396 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z", | |
1397 name->data, n, size); | |
1398 goto failed; | |
1399 } | |
1400 | |
6631 | 1401 header = (ngx_stream_geo_header_t *) base; |
3652 | 1402 |
6631 | 1403 if (size < 16 || ngx_memcmp(&ngx_stream_geo_header, header, 12) != 0) { |
3652 | 1404 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
1405 "incompatible binary geo range base \"%s\"", name->data); | |
1406 goto failed; | |
1407 } | |
1408 | |
1409 ngx_crc32_init(crc32); | |
1410 | |
6631 | 1411 vv = (ngx_stream_variable_value_t *) |
1412 (base + sizeof(ngx_stream_geo_header_t)); | |
3652 | 1413 |
5757
c5ec6944de98
Style: add whitespace between control statement and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5330
diff
changeset
|
1414 while (vv->data) { |
6631 | 1415 len = ngx_align(sizeof(ngx_stream_variable_value_t) + vv->len, |
3652 | 1416 sizeof(void *)); |
1417 ngx_crc32_update(&crc32, (u_char *) vv, len); | |
1418 vv->data += (size_t) base; | |
6631 | 1419 vv = (ngx_stream_variable_value_t *) ((u_char *) vv + len); |
3652 | 1420 } |
6631 | 1421 ngx_crc32_update(&crc32, (u_char *) vv, |
1422 sizeof(ngx_stream_variable_value_t)); | |
3652 | 1423 vv++; |
1424 | |
6631 | 1425 ranges = (ngx_stream_geo_range_t **) vv; |
3652 | 1426 |
1427 for (i = 0; i < 0x10000; i++) { | |
1428 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *)); | |
1429 if (ranges[i]) { | |
6631 | 1430 ranges[i] = (ngx_stream_geo_range_t *) |
3652 | 1431 ((u_char *) ranges[i] + (size_t) base); |
1432 } | |
1433 } | |
1434 | |
6631 | 1435 range = (ngx_stream_geo_range_t *) &ranges[0x10000]; |
3652 | 1436 |
1437 while ((u_char *) range < base + size) { | |
1438 while (range->value) { | |
1439 ngx_crc32_update(&crc32, (u_char *) range, | |
6631 | 1440 sizeof(ngx_stream_geo_range_t)); |
1441 range->value = (ngx_stream_variable_value_t *) | |
3652 | 1442 ((u_char *) range->value + (size_t) base); |
1443 range++; | |
1444 } | |
1445 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *)); | |
6631 | 1446 range = (ngx_stream_geo_range_t *) ((u_char *) range + sizeof(void *)); |
3652 | 1447 } |
1448 | |
1449 ngx_crc32_final(crc32); | |
1450 | |
1451 if (crc32 != header->crc32) { | |
1452 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1453 "CRC32 mismatch in binary geo range base \"%s\"", name->data); | |
1454 goto failed; | |
1455 } | |
1456 | |
1457 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, | |
1458 "using binary geo range base \"%s\"", name->data); | |
1459 | |
1460 ctx->include_name = *name; | |
1461 ctx->binary_include = 1; | |
1462 ctx->high.low = ranges; | |
1463 rc = NGX_OK; | |
1464 | |
1465 goto done; | |
1466 | |
1467 failed: | |
1468 | |
1469 rc = NGX_DECLINED; | |
1470 | |
1471 done: | |
1472 | |
1473 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
1474 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, | |
1475 ngx_close_file_n " \"%s\" failed", name->data); | |
1476 } | |
1477 | |
1478 return rc; | |
1479 } | |
1480 | |
1481 | |
1482 static void | |
6631 | 1483 ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx) |
3652 | 1484 { |
6631 | 1485 u_char *p; |
1486 uint32_t hash; | |
1487 ngx_str_t s; | |
1488 ngx_uint_t i; | |
1489 ngx_file_mapping_t fm; | |
1490 ngx_stream_geo_range_t *r, *range, **ranges; | |
1491 ngx_stream_geo_header_t *header; | |
1492 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1493 |
1494 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5); | |
1495 if (fm.name == NULL) { | |
1496 return; | |
1497 } | |
1498 | |
1499 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name); | |
1500 | |
1501 fm.size = ctx->data_size; | |
1502 fm.log = ctx->pool->log; | |
1503 | |
1504 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0, | |
1505 "creating binary geo range base \"%s\"", fm.name); | |
1506 | |
1507 if (ngx_create_file_mapping(&fm) != NGX_OK) { | |
1508 return; | |
1509 } | |
1510 | |
6631 | 1511 p = ngx_cpymem(fm.addr, &ngx_stream_geo_header, |
1512 sizeof(ngx_stream_geo_header_t)); | |
3652 | 1513 |
6631 | 1514 p = ngx_stream_geo_copy_values(fm.addr, p, ctx->rbtree.root, |
1515 ctx->rbtree.sentinel); | |
3652 | 1516 |
6631 | 1517 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1518 |
6631 | 1519 ranges = (ngx_stream_geo_range_t **) p; |
3652 | 1520 |
6631 | 1521 p += 0x10000 * sizeof(ngx_stream_geo_range_t *); |
3652 | 1522 |
1523 for (i = 0; i < 0x10000; i++) { | |
1524 r = ctx->high.low[i]; | |
1525 if (r == NULL) { | |
1526 continue; | |
1527 } | |
1528 | |
6631 | 1529 range = (ngx_stream_geo_range_t *) p; |
1530 ranges[i] = (ngx_stream_geo_range_t *) (p - (u_char *) fm.addr); | |
3652 | 1531 |
1532 do { | |
1533 s.len = r->value->len; | |
1534 s.data = r->value->data; | |
1535 hash = ngx_crc32_long(s.data, s.len); | |
6631 | 1536 gvvn = (ngx_stream_geo_variable_value_node_t *) |
3652 | 1537 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash); |
1538 | |
6631 | 1539 range->value = (ngx_stream_variable_value_t *) gvvn->offset; |
3652 | 1540 range->start = r->start; |
1541 range->end = r->end; | |
1542 range++; | |
1543 | |
1544 } while ((++r)->value); | |
1545 | |
1546 range->value = NULL; | |
1547 | |
1548 p = (u_char *) range + sizeof(void *); | |
1549 } | |
1550 | |
1551 header = fm.addr; | |
1552 header->crc32 = ngx_crc32_long((u_char *) fm.addr | |
6631 | 1553 + sizeof(ngx_stream_geo_header_t), |
1554 fm.size - sizeof(ngx_stream_geo_header_t)); | |
3652 | 1555 |
1556 ngx_close_file_mapping(&fm); | |
1557 } | |
1558 | |
1559 | |
1560 static u_char * | |
6631 | 1561 ngx_stream_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node, |
3652 | 1562 ngx_rbtree_node_t *sentinel) |
1563 { | |
6631 | 1564 ngx_stream_variable_value_t *vv; |
1565 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1566 |
1567 if (node == sentinel) { | |
1568 return p; | |
1569 } | |
1570 | |
6631 | 1571 gvvn = (ngx_stream_geo_variable_value_node_t *) node; |
3652 | 1572 gvvn->offset = p - base; |
1573 | |
6631 | 1574 vv = (ngx_stream_variable_value_t *) p; |
3652 | 1575 *vv = *gvvn->value; |
6631 | 1576 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1577 vv->data = (u_char *) (p - base); |
1578 | |
1579 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len); | |
1580 | |
1581 p = ngx_align_ptr(p, sizeof(void *)); | |
1582 | |
6631 | 1583 p = ngx_stream_geo_copy_values(base, p, node->left, sentinel); |
3652 | 1584 |
6631 | 1585 return ngx_stream_geo_copy_values(base, p, node->right, sentinel); |
3652 | 1586 } |