Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_geo_module.c @ 7334:f17e313009b0
Skipping spaces in configuration files (ticket #1557).
Previously, a chunk of spaces larger than NGX_CONF_BUFFER (4096 bytes)
resulted in the "too long parameter" error during parsing such a
configuration. This was because the code only set start and start_line
on non-whitespace characters, and hence adjacent whitespace characters
were preserved when reading additional data from the configuration file.
Fix is to always move start and start_line if the last character was
a space.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 09 Aug 2018 12:15:42 +0300 |
parents | 2dc837d16099 |
children | 286ae954009d |
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 { |
7215
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
344 char *rv; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
345 size_t len; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
346 ngx_str_t *value, name; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
347 ngx_uint_t i; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
348 ngx_conf_t save; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
349 ngx_pool_t *pool; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
350 ngx_array_t *a; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
351 ngx_stream_variable_t *var; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
352 ngx_stream_geo_ctx_t *geo; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
353 ngx_stream_geo_conf_ctx_t ctx; |
4992 | 354 #if (NGX_HAVE_INET6) |
7215
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
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) { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
412 ngx_destroy_pool(pool); |
485 | 413 return NGX_CONF_ERROR; |
414 } | |
415 | |
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
|
416 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); |
485 | 417 |
589 | 418 ctx.pool = cf->pool; |
6631 | 419 ctx.data_size = sizeof(ngx_stream_geo_header_t) |
420 + sizeof(ngx_stream_variable_value_t) | |
421 + 0x10000 * sizeof(ngx_stream_geo_range_t *); | |
3652 | 422 ctx.allow_binary_include = 1; |
485 | 423 |
424 save = *cf; | |
425 cf->pool = pool; | |
589 | 426 cf->ctx = &ctx; |
6631 | 427 cf->handler = ngx_stream_geo; |
485 | 428 cf->handler_conf = conf; |
429 | |
430 rv = ngx_conf_parse(cf, NULL); | |
431 | |
432 *cf = save; | |
433 | |
7214
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
434 if (rv != NGX_CONF_OK) { |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
435 goto failed; |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
436 } |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
437 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
438 if (ctx.ranges) { |
3652 | 439 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
440 if (ctx.high.low && !ctx.binary_include) { |
3652 | 441 for (i = 0; i < 0x10000; i++) { |
442 a = (ngx_array_t *) ctx.high.low[i]; | |
443 | |
6655
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
444 if (a == NULL) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
445 continue; |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
446 } |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
447 |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
448 if (a->nelts == 0) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
449 ctx.high.low[i] = NULL; |
3652 | 450 continue; |
451 } | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
452 |
6631 | 453 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
|
454 |
3652 | 455 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); |
456 if (ctx.high.low[i] == NULL) { | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
457 goto failed; |
3652 | 458 } |
459 | |
4967
a9d60fafaa85
Geo: improved ngx_http_geo_block() code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
460 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
|
461 ctx.high.low[i][a->nelts].value = NULL; |
3652 | 462 ctx.data_size += len + sizeof(void *); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
463 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
464 |
3652 | 465 if (ctx.allow_binary_include |
466 && !ctx.outside_entries | |
467 && ctx.entries > 100000 | |
468 && ctx.includes == 1) | |
469 { | |
6631 | 470 ngx_stream_geo_create_binary_base(&ctx); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
471 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
472 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
473 |
4816
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
474 if (ctx.high.default_value == NULL) { |
6631 | 475 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
|
476 } |
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
477 |
2399 | 478 geo->u.high = ctx.high; |
479 | |
6631 | 480 var->get_handler = ngx_stream_geo_range_variable; |
2399 | 481 var->data = (uintptr_t) geo; |
485 | 482 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
483 } else { |
2358
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 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
|
486 if (ctx.tree == NULL) { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
487 goto failed; |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
488 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
489 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
490 |
4992 | 491 geo->u.trees.tree = ctx.tree; |
492 | |
493 #if (NGX_HAVE_INET6) | |
494 if (ctx.tree6 == NULL) { | |
495 ctx.tree6 = ngx_radix_tree_create(cf->pool, -1); | |
496 if (ctx.tree6 == NULL) { | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
497 goto failed; |
4992 | 498 } |
499 } | |
500 | |
501 geo->u.trees.tree6 = ctx.tree6; | |
502 #endif | |
2399 | 503 |
6631 | 504 var->get_handler = ngx_stream_geo_cidr_variable; |
2399 | 505 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
506 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
507 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
6631 | 508 (uintptr_t) &ngx_stream_variable_null_value) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
509 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
510 { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
511 goto failed; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
512 } |
4980
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
513 |
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
514 /* NGX_BUSY is okay (default was set explicitly) */ |
4992 | 515 |
516 #if (NGX_HAVE_INET6) | |
517 if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr, | |
6631 | 518 (uintptr_t) &ngx_stream_variable_null_value) |
4992 | 519 == NGX_ERROR) |
520 { | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
521 goto failed; |
4992 | 522 } |
523 #endif | |
485 | 524 } |
525 | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
526 ngx_destroy_pool(ctx.temp_pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
527 ngx_destroy_pool(pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
528 |
7214
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
529 return NGX_CONF_OK; |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
530 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
531 failed: |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
532 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
533 ngx_destroy_pool(ctx.temp_pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
534 ngx_destroy_pool(pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
535 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
536 return NGX_CONF_ERROR; |
485 | 537 } |
538 | |
539 | |
489 | 540 static char * |
6631 | 541 ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) |
485 | 542 { |
6631 | 543 char *rv; |
544 ngx_str_t *value; | |
545 ngx_stream_geo_conf_ctx_t *ctx; | |
485 | 546 |
589 | 547 ctx = cf->ctx; |
485 | 548 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
549 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
550 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
551 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
552 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
553 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
554 |
4992 | 555 if (ctx->tree |
556 #if (NGX_HAVE_INET6) | |
557 || ctx->tree6 | |
558 #endif | |
559 ) | |
560 { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
561 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
562 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
563 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
564 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
565 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
566 |
3652 | 567 ctx->ranges = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
568 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
569 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
570 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
571 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
572 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
573 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
574 |
485 | 575 if (cf->args->nelts != 2) { |
576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
577 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 goto failed; |
485 | 579 } |
580 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
581 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 |
6631 | 583 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
|
584 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
585 goto done; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 |
3652 | 588 if (ctx->ranges) { |
6631 | 589 rv = ngx_stream_geo_range(cf, ctx, value); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
590 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 } else { |
6631 | 592 rv = ngx_stream_geo_cidr(cf, ctx, value); |
2340
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
595 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
596 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
597 ngx_reset_pool(cf->pool); |
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 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
600 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
601 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
602 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
603 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
604 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
605 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
606 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
607 |
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 static char * |
6631 | 610 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
|
611 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
612 { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
613 u_char *p, *last; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
614 in_addr_t start, end; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
615 ngx_str_t *net; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
616 ngx_uint_t del; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
617 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
618 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
619 |
3652 | 620 if (ctx->high.default_value) { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
621 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
|
622 "duplicate default geo range value: \"%V\", old value: \"%v\"", |
3652 | 623 &value[1], ctx->high.default_value); |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
624 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
625 |
6631 | 626 ctx->high.default_value = ngx_stream_geo_value(cf, ctx, &value[1]); |
3652 | 627 if (ctx->high.default_value == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
628 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
629 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
630 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
631 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
632 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
633 |
3652 | 634 if (ctx->binary_include) { |
635 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
|
636 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 637 ctx->include_name.data); |
638 return NGX_CONF_ERROR; | |
639 } | |
640 | |
641 if (ctx->high.low == NULL) { | |
642 ctx->high.low = ngx_pcalloc(ctx->pool, | |
6631 | 643 0x10000 * sizeof(ngx_stream_geo_range_t *)); |
3652 | 644 if (ctx->high.low == NULL) { |
645 return NGX_CONF_ERROR; | |
646 } | |
647 } | |
648 | |
649 ctx->entries++; | |
650 ctx->outside_entries = 1; | |
651 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
652 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
653 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
654 del = 1; |
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 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
657 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
658 del = 0; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
661 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
662 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
663 p = ngx_strlchr(net->data, last, '-'); |
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 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
666 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
669 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
670 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
671 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
672 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
673 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
674 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
675 start = ntohl(start); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
676 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
679 end = ngx_inet_addr(p, last - p); |
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 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
682 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
683 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
684 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 end = ntohl(end); |
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 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 if (del) { |
6631 | 692 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
|
693 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 "no address range \"%V\" to delete", net); |
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 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
699 |
6631 | 700 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
|
701 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
702 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
703 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
704 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
705 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
706 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 |
6631 | 708 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
|
709 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
711 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
712 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
|
713 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
715 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
716 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
717 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
718 /* 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
|
719 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
720 static char * |
6631 | 721 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
|
722 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
723 { |
6631 | 724 in_addr_t n; |
725 ngx_uint_t h, i, s, e; | |
726 ngx_array_t *a; | |
727 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
728 |
3456
91cff7f97a50
fix a geo range if the range includes two or more /16 networks
Igor Sysoev <igor@sysoev.ru>
parents:
3115
diff
changeset
|
729 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
|
730 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
731 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
|
732 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
733 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
|
734 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
|
735 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
736 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
737 } |
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 ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
740 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
741 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
742 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
743 e = 0xffff; |
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 |
3652 | 746 a = (ngx_array_t *) ctx->high.low[h]; |
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 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
749 a = ngx_array_create(ctx->temp_pool, 64, |
6631 | 750 sizeof(ngx_stream_geo_range_t)); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
751 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
752 return NGX_CONF_ERROR; |
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 |
6631 | 755 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
|
756 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
757 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
758 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
759 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
760 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
761 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
762 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
763 i--; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
764 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
765 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
766 continue; |
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 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
770 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
771 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
772 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
773 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
774 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
775 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
776 } |
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 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
779 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
780 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 781 (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
|
782 |
2442 | 783 range[i + 1].start = (u_short) s; |
784 range[i + 1].end = (u_short) e; | |
785 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
786 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
787 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
788 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
789 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
790 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
791 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
792 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
793 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
794 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
795 ctx->net, ctx->value, range[i].value); |
2442 | 796 |
797 range[i].value = ctx->value; | |
798 | |
799 goto next; | |
800 } | |
801 | |
802 if (s > (ngx_uint_t) range[i].start | |
803 && e < (ngx_uint_t) range[i].end) | |
804 { | |
805 /* split the range and insert the new one */ | |
806 | |
807 range = ngx_array_push(a); | |
808 if (range == NULL) { | |
809 return NGX_CONF_ERROR; | |
810 } | |
811 | |
812 range = ngx_array_push(a); | |
813 if (range == NULL) { | |
814 return NGX_CONF_ERROR; | |
815 } | |
816 | |
817 range = a->elts; | |
818 | |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
819 ngx_memmove(&range[i + 3], &range[i + 1], |
6631 | 820 (a->nelts - 3 - i) * sizeof(ngx_stream_geo_range_t)); |
2442 | 821 |
822 range[i + 2].start = (u_short) (e + 1); | |
823 range[i + 2].end = range[i].end; | |
824 range[i + 2].value = range[i].value; | |
825 | |
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
|
826 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
|
827 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
|
828 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
|
829 |
2442 | 830 range[i].end = (u_short) (s - 1); |
831 | |
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
|
832 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
|
833 } |
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 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
|
836 && 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
|
837 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
838 /* 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
|
839 |
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 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
|
841 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
|
842 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
|
843 } |
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 |
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 = 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
|
846 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
847 ngx_memmove(&range[i + 1], &range[i], |
6631 | 848 (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
|
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 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
|
851 |
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 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
|
853 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
|
854 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
|
855 |
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 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
|
857 } |
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 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
|
860 && 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
|
861 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
862 /* 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
|
863 |
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 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
|
865 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
|
866 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
|
867 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
868 |
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 = 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
|
870 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
871 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 872 (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
|
873 |
2442 | 874 range[i + 1].start = (u_short) s; |
875 range[i + 1].end = (u_short) e; | |
876 range[i + 1].value = ctx->value; | |
877 | |
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
|
878 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
|
879 |
2442 | 880 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
881 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
882 |
2448 | 883 s = (ngx_uint_t) range[i].start; |
884 e = (ngx_uint_t) range[i].end; | |
885 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
886 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 887 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
888 ctx->net, | |
889 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
890 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
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 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
893 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
894 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
895 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
896 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
897 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
898 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
899 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
900 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
901 |
6663
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
902 range = a->elts; |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
903 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
904 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
|
905 (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
|
906 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
907 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
|
908 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
|
909 range[0].value = ctx->value; |
2442 | 910 |
911 next: | |
912 | |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
913 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
914 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
915 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
916 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
917 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
918 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
919 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
920 |
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 static ngx_uint_t |
6631 | 923 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
|
924 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
925 { |
6631 | 926 in_addr_t n; |
927 ngx_uint_t h, i, s, e, warn; | |
928 ngx_array_t *a; | |
929 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
930 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
931 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
932 |
6662
1301a58b5dac
Geo: fixed removing a range in certain cases.
Ruslan Ermilov <ru@nginx.com>
parents:
6661
diff
changeset
|
933 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
|
934 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
935 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
936 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
937 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
938 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
939 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
940 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
941 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
942 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
943 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
944 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
945 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
946 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
947 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
948 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
949 |
3652 | 950 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
951 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
952 if (a == NULL || a->nelts == 0) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
953 warn = 1; |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
954 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
955 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
956 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
957 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
958 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
959 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
960 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
961 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
962 { |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
963 ngx_memmove(&range[i], &range[i + 1], |
6631 | 964 (a->nelts - 1 - i) * sizeof(ngx_stream_geo_range_t)); |
2450 | 965 |
966 a->nelts--; | |
967 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
968 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
969 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
970 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
971 if (i == a->nelts - 1) { |
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
972 warn = 1; |
2340
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 } |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
975 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
976 next: |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
977 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
978 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
979 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
980 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
981 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
982 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
983 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
984 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
985 |
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 static char * |
6631 | 988 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
|
989 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
990 { |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
991 char *rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
992 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
|
993 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
|
994 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
995 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
996 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
997 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
998 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
999 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1000 } |
485 | 1001 } |
1002 | |
4992 | 1003 #if (NGX_HAVE_INET6) |
1004 if (ctx->tree6 == NULL) { | |
1005 ctx->tree6 = ngx_radix_tree_create(ctx->pool, -1); | |
1006 if (ctx->tree6 == NULL) { | |
1007 return NGX_CONF_ERROR; | |
1008 } | |
1009 } | |
1010 #endif | |
1011 | |
485 | 1012 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
|
1013 cidr.family = AF_INET; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1014 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1015 cidr.u.in.mask = 0; |
485 | 1016 |
6631 | 1017 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
|
1018 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1019 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1020 return rv; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1021 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1022 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1023 #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
|
1024 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
|
1025 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
|
1026 |
6631 | 1027 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
|
1028 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1029 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1030 return rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1031 } |
4992 | 1032 #endif |
1033 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1034 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
|
1035 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1036 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1037 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
|
1038 net = &value[1]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1039 del = 1; |
4992 | 1040 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1041 } else { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1042 net = &value[0]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1043 del = 0; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1044 } |
2365 | 1045 |
6631 | 1046 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
|
1047 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
|
1048 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1049 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1050 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
|
1051 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
|
1052 cidr.u.in.mask = ntohl(cidr.u.in.mask); |
485 | 1053 } |
1054 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1055 if (del) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1056 switch (cidr.family) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1057 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1058 #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
|
1059 case AF_INET6: |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1060 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
|
1061 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
|
1062 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
|
1063 break; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1064 #endif |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1065 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1066 default: /* AF_INET */ |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1067 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
|
1068 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
|
1069 break; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1070 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1071 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1072 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
|
1073 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
|
1074 "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
|
1075 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1076 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1077 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
|
1078 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1079 |
6631 | 1080 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
|
1081 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1082 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1083 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1084 static char * |
6631 | 1085 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
|
1086 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
|
1087 { |
6631 | 1088 ngx_int_t rc; |
1089 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
|
1090 |
6631 | 1091 val = ngx_stream_geo_value(cf, ctx, value); |
485 | 1092 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
1093 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1094 return NGX_CONF_ERROR; |
485 | 1095 } |
1096 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1097 switch (cidr->family) { |
4992 | 1098 |
1099 #if (NGX_HAVE_INET6) | |
1100 case AF_INET6: | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1101 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
|
1102 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1103 (uintptr_t) val); |
4992 | 1104 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1105 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1106 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1107 } |
4992 | 1108 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1109 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1110 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1111 } |
553 | 1112 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1113 /* rc == NGX_BUSY */ |
4992 | 1114 |
6631 | 1115 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1116 ngx_radix128tree_find(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1117 cidr->u.in6.addr.s6_addr); |
4992 | 1118 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1119 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1120 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1121 net, val, old); |
4992 | 1122 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1123 rc = ngx_radix128tree_delete(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1124 cidr->u.in6.addr.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1125 cidr->u.in6.mask.s6_addr); |
4992 | 1126 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1127 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1128 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
|
1129 return NGX_CONF_ERROR; |
553 | 1130 } |
1131 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1132 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
|
1133 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1134 (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1135 |
4992 | 1136 break; |
1137 #endif | |
1138 | |
1139 default: /* AF_INET */ | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1140 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1141 cidr->u.in.mask, (uintptr_t) val); |
553 | 1142 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1143 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1144 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1145 } |
553 | 1146 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1147 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1148 return NGX_CONF_ERROR; |
553 | 1149 } |
4992 | 1150 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1151 /* rc == NGX_BUSY */ |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1152 |
6631 | 1153 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1154 ngx_radix32tree_find(ctx->tree, cidr->u.in.addr); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1155 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1156 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1157 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1158 net, val, old); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1159 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1160 rc = ngx_radix32tree_delete(ctx->tree, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1161 cidr->u.in.addr, cidr->u.in.mask); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1162 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1163 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1164 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
|
1165 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1166 } |
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 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1169 cidr->u.in.mask, (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1170 |
4992 | 1171 break; |
485 | 1172 } |
1173 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1174 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1175 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1176 } |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1177 |
553 | 1178 return NGX_CONF_ERROR; |
485 | 1179 } |
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 |
6631 | 1182 static ngx_stream_variable_value_t * |
1183 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
|
1184 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1185 { |
6631 | 1186 uint32_t hash; |
1187 ngx_stream_variable_value_t *val; | |
1188 ngx_stream_geo_variable_value_node_t *gvvn; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1189 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1190 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1191 |
6631 | 1192 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
|
1193 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1194 |
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
|
1195 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
|
1196 return gvvn->value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1197 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1198 |
6631 | 1199 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
|
1200 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1201 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1202 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1203 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1204 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1205 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1206 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1207 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1208 } |
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 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1211 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1212 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1213 |
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
|
1214 gvvn = ngx_palloc(ctx->temp_pool, |
6631 | 1215 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
|
1216 if (gvvn == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1217 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1218 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1219 |
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
|
1220 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
|
1221 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
|
1222 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
|
1223 gvvn->value = val; |
3652 | 1224 gvvn->offset = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1225 |
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
|
1226 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1227 |
6631 | 1228 ctx->data_size += ngx_align(sizeof(ngx_stream_variable_value_t) |
1229 + value->len, sizeof(void *)); | |
3652 | 1230 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1231 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1232 } |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1233 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1234 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1235 static ngx_int_t |
6631 | 1236 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
|
1237 { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1238 ngx_int_t rc; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1239 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1240 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
|
1241 cidr->family = AF_INET; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1242 cidr->u.in.addr = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1243 cidr->u.in.mask = 0xffffffff; |
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 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1246 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1247 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1248 rc = ngx_ptocidr(net, cidr); |
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 if (rc == NGX_ERROR) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1251 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
|
1252 return NGX_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1253 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1254 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1255 if (rc == NGX_DONE) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1256 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1257 "low address bits of %V are meaningless", net); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1258 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1259 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1260 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1261 } |
3652 | 1262 |
1263 | |
1264 static char * | |
6631 | 1265 ngx_stream_geo_include(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
3652 | 1266 ngx_str_t *name) |
1267 { | |
1268 char *rv; | |
1269 ngx_str_t file; | |
1270 | |
1271 file.len = name->len + 4; | |
1272 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5); | |
1273 if (file.data == NULL) { | |
1274 return NGX_CONF_ERROR; | |
1275 } | |
1276 | |
1277 ngx_sprintf(file.data, "%V.bin%Z", name); | |
1278 | |
5330
314c3d7cc3a5
Backed out f1a91825730a and 7094bd12c1ff.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5317
diff
changeset
|
1279 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { |
3652 | 1280 return NGX_CONF_ERROR; |
1281 } | |
1282 | |
3654
b56935e34273
test binary geo ranges base only for ranges
Igor Sysoev <igor@sysoev.ru>
parents:
3653
diff
changeset
|
1283 if (ctx->ranges) { |
3660 | 1284 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); |
3652 | 1285 |
6631 | 1286 switch (ngx_stream_geo_include_binary_base(cf, ctx, &file)) { |
3660 | 1287 case NGX_OK: |
1288 return NGX_CONF_OK; | |
1289 case NGX_ERROR: | |
1290 return NGX_CONF_ERROR; | |
1291 default: | |
1292 break; | |
1293 } | |
3652 | 1294 } |
1295 | |
1296 file.len -= 4; | |
1297 file.data[file.len] = '\0'; | |
1298 | |
1299 ctx->include_name = file; | |
1300 | |
1301 if (ctx->outside_entries) { | |
1302 ctx->allow_binary_include = 0; | |
1303 } | |
1304 | |
1305 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
1306 | |
1307 rv = ngx_conf_parse(cf, &file); | |
1308 | |
1309 ctx->includes++; | |
1310 ctx->outside_entries = 0; | |
1311 | |
1312 return rv; | |
1313 } | |
1314 | |
1315 | |
1316 static ngx_int_t | |
6631 | 1317 ngx_stream_geo_include_binary_base(ngx_conf_t *cf, |
1318 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name) | |
3652 | 1319 { |
6631 | 1320 u_char *base, ch; |
1321 time_t mtime; | |
1322 size_t size, len; | |
1323 ssize_t n; | |
1324 uint32_t crc32; | |
1325 ngx_err_t err; | |
1326 ngx_int_t rc; | |
1327 ngx_uint_t i; | |
1328 ngx_file_t file; | |
1329 ngx_file_info_t fi; | |
1330 ngx_stream_geo_range_t *range, **ranges; | |
1331 ngx_stream_geo_header_t *header; | |
1332 ngx_stream_variable_value_t *vv; | |
3652 | 1333 |
1334 ngx_memzero(&file, sizeof(ngx_file_t)); | |
1335 file.name = *name; | |
1336 file.log = cf->log; | |
1337 | |
7087
47b7ffc3339d
Fixed calls to ngx_open_file() in certain places.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7086
diff
changeset
|
1338 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
7086 | 1339 |
3652 | 1340 if (file.fd == NGX_INVALID_FILE) { |
1341 err = ngx_errno; | |
1342 if (err != NGX_ENOENT) { | |
1343 ngx_conf_log_error(NGX_LOG_CRIT, cf, err, | |
1344 ngx_open_file_n " \"%s\" failed", name->data); | |
1345 } | |
1346 return NGX_DECLINED; | |
1347 } | |
1348 | |
1349 if (ctx->outside_entries) { | |
1350 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
|
1351 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 1352 name->data); |
1353 rc = NGX_ERROR; | |
1354 goto done; | |
1355 } | |
1356 | |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1357 if (ctx->binary_include) { |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1358 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
|
1359 "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
|
1360 name->data, ctx->include_name.data); |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1361 rc = NGX_ERROR; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1362 goto done; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1363 } |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1364 |
3652 | 1365 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) { |
1366 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1367 ngx_fd_info_n " \"%s\" failed", name->data); | |
1368 goto failed; | |
1369 } | |
1370 | |
1371 size = (size_t) ngx_file_size(&fi); | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1372 mtime = ngx_file_mtime(&fi); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1373 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1374 ch = name->data[name->len - 4]; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1375 name->data[name->len - 4] = '\0'; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1376 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1377 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
|
1378 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
|
1379 ngx_file_info_n " \"%s\" failed", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1380 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1381 } |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1382 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1383 name->data[name->len - 4] = ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1384 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1385 if (mtime < ngx_file_mtime(&fi)) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1386 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
|
1387 "stale binary geo range base \"%s\"", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1388 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1389 } |
3652 | 1390 |
1391 base = ngx_palloc(ctx->pool, size); | |
1392 if (base == NULL) { | |
1393 goto failed; | |
1394 } | |
1395 | |
1396 n = ngx_read_file(&file, base, size, 0); | |
1397 | |
1398 if (n == NGX_ERROR) { | |
1399 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1400 ngx_read_file_n " \"%s\" failed", name->data); | |
1401 goto failed; | |
1402 } | |
1403 | |
1404 if ((size_t) n != size) { | |
1405 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0, | |
1406 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z", | |
1407 name->data, n, size); | |
1408 goto failed; | |
1409 } | |
1410 | |
6631 | 1411 header = (ngx_stream_geo_header_t *) base; |
3652 | 1412 |
6631 | 1413 if (size < 16 || ngx_memcmp(&ngx_stream_geo_header, header, 12) != 0) { |
3652 | 1414 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
1415 "incompatible binary geo range base \"%s\"", name->data); | |
1416 goto failed; | |
1417 } | |
1418 | |
1419 ngx_crc32_init(crc32); | |
1420 | |
6631 | 1421 vv = (ngx_stream_variable_value_t *) |
1422 (base + sizeof(ngx_stream_geo_header_t)); | |
3652 | 1423 |
5757
c5ec6944de98
Style: add whitespace between control statement and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5330
diff
changeset
|
1424 while (vv->data) { |
6631 | 1425 len = ngx_align(sizeof(ngx_stream_variable_value_t) + vv->len, |
3652 | 1426 sizeof(void *)); |
1427 ngx_crc32_update(&crc32, (u_char *) vv, len); | |
1428 vv->data += (size_t) base; | |
6631 | 1429 vv = (ngx_stream_variable_value_t *) ((u_char *) vv + len); |
3652 | 1430 } |
6631 | 1431 ngx_crc32_update(&crc32, (u_char *) vv, |
1432 sizeof(ngx_stream_variable_value_t)); | |
3652 | 1433 vv++; |
1434 | |
6631 | 1435 ranges = (ngx_stream_geo_range_t **) vv; |
3652 | 1436 |
1437 for (i = 0; i < 0x10000; i++) { | |
1438 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *)); | |
1439 if (ranges[i]) { | |
6631 | 1440 ranges[i] = (ngx_stream_geo_range_t *) |
3652 | 1441 ((u_char *) ranges[i] + (size_t) base); |
1442 } | |
1443 } | |
1444 | |
6631 | 1445 range = (ngx_stream_geo_range_t *) &ranges[0x10000]; |
3652 | 1446 |
1447 while ((u_char *) range < base + size) { | |
1448 while (range->value) { | |
1449 ngx_crc32_update(&crc32, (u_char *) range, | |
6631 | 1450 sizeof(ngx_stream_geo_range_t)); |
1451 range->value = (ngx_stream_variable_value_t *) | |
3652 | 1452 ((u_char *) range->value + (size_t) base); |
1453 range++; | |
1454 } | |
1455 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *)); | |
6631 | 1456 range = (ngx_stream_geo_range_t *) ((u_char *) range + sizeof(void *)); |
3652 | 1457 } |
1458 | |
1459 ngx_crc32_final(crc32); | |
1460 | |
1461 if (crc32 != header->crc32) { | |
1462 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1463 "CRC32 mismatch in binary geo range base \"%s\"", name->data); | |
1464 goto failed; | |
1465 } | |
1466 | |
1467 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, | |
1468 "using binary geo range base \"%s\"", name->data); | |
1469 | |
1470 ctx->include_name = *name; | |
1471 ctx->binary_include = 1; | |
1472 ctx->high.low = ranges; | |
1473 rc = NGX_OK; | |
1474 | |
1475 goto done; | |
1476 | |
1477 failed: | |
1478 | |
1479 rc = NGX_DECLINED; | |
1480 | |
1481 done: | |
1482 | |
1483 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
1484 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, | |
1485 ngx_close_file_n " \"%s\" failed", name->data); | |
1486 } | |
1487 | |
1488 return rc; | |
1489 } | |
1490 | |
1491 | |
1492 static void | |
6631 | 1493 ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx) |
3652 | 1494 { |
6631 | 1495 u_char *p; |
1496 uint32_t hash; | |
1497 ngx_str_t s; | |
1498 ngx_uint_t i; | |
1499 ngx_file_mapping_t fm; | |
1500 ngx_stream_geo_range_t *r, *range, **ranges; | |
1501 ngx_stream_geo_header_t *header; | |
1502 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1503 |
1504 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5); | |
1505 if (fm.name == NULL) { | |
1506 return; | |
1507 } | |
1508 | |
1509 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name); | |
1510 | |
1511 fm.size = ctx->data_size; | |
1512 fm.log = ctx->pool->log; | |
1513 | |
1514 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0, | |
1515 "creating binary geo range base \"%s\"", fm.name); | |
1516 | |
1517 if (ngx_create_file_mapping(&fm) != NGX_OK) { | |
1518 return; | |
1519 } | |
1520 | |
6631 | 1521 p = ngx_cpymem(fm.addr, &ngx_stream_geo_header, |
1522 sizeof(ngx_stream_geo_header_t)); | |
3652 | 1523 |
6631 | 1524 p = ngx_stream_geo_copy_values(fm.addr, p, ctx->rbtree.root, |
1525 ctx->rbtree.sentinel); | |
3652 | 1526 |
6631 | 1527 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1528 |
6631 | 1529 ranges = (ngx_stream_geo_range_t **) p; |
3652 | 1530 |
6631 | 1531 p += 0x10000 * sizeof(ngx_stream_geo_range_t *); |
3652 | 1532 |
1533 for (i = 0; i < 0x10000; i++) { | |
1534 r = ctx->high.low[i]; | |
1535 if (r == NULL) { | |
1536 continue; | |
1537 } | |
1538 | |
6631 | 1539 range = (ngx_stream_geo_range_t *) p; |
1540 ranges[i] = (ngx_stream_geo_range_t *) (p - (u_char *) fm.addr); | |
3652 | 1541 |
1542 do { | |
1543 s.len = r->value->len; | |
1544 s.data = r->value->data; | |
1545 hash = ngx_crc32_long(s.data, s.len); | |
6631 | 1546 gvvn = (ngx_stream_geo_variable_value_node_t *) |
3652 | 1547 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash); |
1548 | |
6631 | 1549 range->value = (ngx_stream_variable_value_t *) gvvn->offset; |
3652 | 1550 range->start = r->start; |
1551 range->end = r->end; | |
1552 range++; | |
1553 | |
1554 } while ((++r)->value); | |
1555 | |
1556 range->value = NULL; | |
1557 | |
1558 p = (u_char *) range + sizeof(void *); | |
1559 } | |
1560 | |
1561 header = fm.addr; | |
1562 header->crc32 = ngx_crc32_long((u_char *) fm.addr | |
6631 | 1563 + sizeof(ngx_stream_geo_header_t), |
1564 fm.size - sizeof(ngx_stream_geo_header_t)); | |
3652 | 1565 |
1566 ngx_close_file_mapping(&fm); | |
1567 } | |
1568 | |
1569 | |
1570 static u_char * | |
6631 | 1571 ngx_stream_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node, |
3652 | 1572 ngx_rbtree_node_t *sentinel) |
1573 { | |
6631 | 1574 ngx_stream_variable_value_t *vv; |
1575 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1576 |
1577 if (node == sentinel) { | |
1578 return p; | |
1579 } | |
1580 | |
6631 | 1581 gvvn = (ngx_stream_geo_variable_value_node_t *) node; |
3652 | 1582 gvvn->offset = p - base; |
1583 | |
6631 | 1584 vv = (ngx_stream_variable_value_t *) p; |
3652 | 1585 *vv = *gvvn->value; |
6631 | 1586 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1587 vv->data = (u_char *) (p - base); |
1588 | |
1589 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len); | |
1590 | |
1591 p = ngx_align_ptr(p, sizeof(void *)); | |
1592 | |
6631 | 1593 p = ngx_stream_geo_copy_values(base, p, node->left, sentinel); |
3652 | 1594 |
6631 | 1595 return ngx_stream_geo_copy_values(base, p, node->right, sentinel); |
3652 | 1596 } |