Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_geo_module.c @ 8531:41f4bd4c51f1
Disabled control characters and space in header names.
Control characters (0x00-0x1f, 0x7f), space, and colon were never allowed in
header names. The only somewhat valid use is header continuation which nginx
never supported and which is explicitly obsolete by RFC 7230.
Previously, such headers were considered invalid and were ignored by default
(as per ignore_invalid_headers directive). With this change, such headers
are unconditionally rejected.
It is expected to make nginx more resilient to various attacks, in particular,
with ignore_invalid_headers switched off (which is inherently unsecure, though
nevertheless sometimes used in the wild).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 28 Jun 2021 18:01:18 +0300 |
parents | 286ae954009d |
children |
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 | |
7430
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
209 #if (NGX_HAVE_UNIX_DOMAIN) |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
210 case AF_UNIX: |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
211 vv = (ngx_stream_variable_value_t *) |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
212 ngx_radix32tree_find(ctx->u.trees.tree, INADDR_NONE); |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
213 break; |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
214 #endif |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
215 |
4992 | 216 default: /* AF_INET */ |
217 sin = (struct sockaddr_in *) addr.sockaddr; | |
218 inaddr = ntohl(sin->sin_addr.s_addr); | |
219 | |
6631 | 220 vv = (ngx_stream_variable_value_t *) |
4992 | 221 ngx_radix32tree_find(ctx->u.trees.tree, inaddr); |
222 | |
223 break; | |
224 } | |
225 | |
226 done: | |
489 | 227 |
573 | 228 *v = *vv; |
229 | |
6631 | 230 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
231 "stream geo: %v", v); | |
489 | 232 |
573 | 233 return NGX_OK; |
485 | 234 } |
235 | |
236 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
237 static ngx_int_t |
6631 | 238 ngx_stream_geo_range_variable(ngx_stream_session_t *s, |
239 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
|
240 { |
6631 | 241 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
|
242 |
6631 | 243 in_addr_t inaddr; |
244 ngx_addr_t addr; | |
245 ngx_uint_t n; | |
246 struct sockaddr_in *sin; | |
247 ngx_stream_geo_range_t *range; | |
4992 | 248 #if (NGX_HAVE_INET6) |
6631 | 249 u_char *p; |
250 struct in6_addr *inaddr6; | |
4992 | 251 #endif |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
252 |
3652 | 253 *v = *ctx->u.high.default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
254 |
6631 | 255 if (ngx_stream_geo_addr(s, ctx, &addr) == NGX_OK) { |
4992 | 256 |
257 switch (addr.sockaddr->sa_family) { | |
258 | |
259 #if (NGX_HAVE_INET6) | |
260 case AF_INET6: | |
261 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; | |
262 | |
263 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { | |
264 p = inaddr6->s6_addr; | |
265 | |
266 inaddr = p[12] << 24; | |
267 inaddr += p[13] << 16; | |
268 inaddr += p[14] << 8; | |
269 inaddr += p[15]; | |
270 | |
271 } else { | |
272 inaddr = INADDR_NONE; | |
273 } | |
274 | |
275 break; | |
276 #endif | |
277 | |
7430
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
278 #if (NGX_HAVE_UNIX_DOMAIN) |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
279 case AF_UNIX: |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
280 inaddr = INADDR_NONE; |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
281 break; |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
282 #endif |
286ae954009d
Geo: fixed handling of AF_UNIX client addresses (ticket #1684).
Maxim Dounin <mdounin@mdounin.ru>
parents:
7215
diff
changeset
|
283 |
4992 | 284 default: /* AF_INET */ |
285 sin = (struct sockaddr_in *) addr.sockaddr; | |
286 inaddr = ntohl(sin->sin_addr.s_addr); | |
287 break; | |
288 } | |
289 | |
290 } else { | |
291 inaddr = INADDR_NONE; | |
292 } | |
293 | |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
294 if (ctx->u.high.low) { |
4992 | 295 range = ctx->u.high.low[inaddr >> 16]; |
4989 | 296 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
297 if (range) { |
4992 | 298 n = inaddr & 0xffff; |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
299 do { |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
300 if (n >= (ngx_uint_t) range->start |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
301 && n <= (ngx_uint_t) range->end) |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
302 { |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
303 *v = *range->value; |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
304 break; |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
305 } |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
306 } while ((++range)->value); |
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
307 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
308 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
309 |
6631 | 310 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
311 "stream geo: %v", v); | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
312 |
4992 | 313 return NGX_OK; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
314 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
315 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
316 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
317 static ngx_int_t |
6631 | 318 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
|
319 ngx_addr_t *addr) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
320 { |
6631 | 321 ngx_stream_variable_value_t *v; |
2399 | 322 |
323 if (ctx->index == -1) { | |
6631 | 324 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
325 "stream geo started: %V", &s->connection->addr_text); | |
2399 | 326 |
6631 | 327 addr->sockaddr = s->connection->sockaddr; |
328 addr->socklen = s->connection->socklen; | |
329 /* 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
|
330 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
331 return NGX_OK; |
2399 | 332 } |
333 | |
6631 | 334 v = ngx_stream_get_flushed_variable(s, ctx->index); |
2399 | 335 |
336 if (v == NULL || v->not_found) { | |
6631 | 337 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
338 "stream geo not found"); | |
2399 | 339 |
4626
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
340 return NGX_ERROR; |
2399 | 341 } |
342 | |
6631 | 343 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
344 "stream geo started: %v", v); | |
2399 | 345 |
6631 | 346 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
|
347 return NGX_OK; |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
348 } |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
349 |
d0db70f4c13b
geo: chains of trusted proxies and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4593
diff
changeset
|
350 return NGX_ERROR; |
2399 | 351 } |
352 | |
353 | |
489 | 354 static char * |
6631 | 355 ngx_stream_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
485 | 356 { |
7215
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
357 char *rv; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
358 size_t len; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
359 ngx_str_t *value, name; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
360 ngx_uint_t i; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
361 ngx_conf_t save; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
362 ngx_pool_t *pool; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
363 ngx_array_t *a; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
364 ngx_stream_variable_t *var; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
365 ngx_stream_geo_ctx_t *geo; |
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
366 ngx_stream_geo_conf_ctx_t ctx; |
4992 | 367 #if (NGX_HAVE_INET6) |
7215
2dc837d16099
Geo: fixed indentation.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7214
diff
changeset
|
368 static struct in6_addr zero; |
4992 | 369 #endif |
485 | 370 |
501 | 371 value = cf->args->elts; |
372 | |
6631 | 373 geo = ngx_palloc(cf->pool, sizeof(ngx_stream_geo_ctx_t)); |
2399 | 374 if (geo == NULL) { |
375 return NGX_CONF_ERROR; | |
376 } | |
377 | |
378 name = value[1]; | |
4963
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
379 |
4972
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
380 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
|
381 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
|
382 "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
|
383 return NGX_CONF_ERROR; |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
384 } |
9f4cdc7a8578
Fixed variable syntax checking in "set", "geo", "limit_conn_zone",
Ruslan Ermilov <ru@nginx.com>
parents:
4828
diff
changeset
|
385 |
2399 | 386 name.len--; |
387 name.data++; | |
388 | |
389 if (cf->args->nelts == 3) { | |
390 | |
6631 | 391 geo->index = ngx_stream_get_variable_index(cf, &name); |
2399 | 392 if (geo->index == NGX_ERROR) { |
393 return NGX_CONF_ERROR; | |
394 } | |
395 | |
396 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
|
397 |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
398 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
|
399 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
|
400 "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
|
401 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
|
402 } |
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4968
diff
changeset
|
403 |
2399 | 404 name.len--; |
405 name.data++; | |
406 | |
407 } else { | |
408 geo->index = -1; | |
409 } | |
410 | |
6631 | 411 var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE); |
501 | 412 if (var == NULL) { |
485 | 413 return NGX_CONF_ERROR; |
414 } | |
415 | |
5124
5482671df278
Use NGX_DEFAULT_POOL_SIZE macro where appropriate.
Ruslan Ermilov <ru@nginx.com>
parents:
5084
diff
changeset
|
416 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
|
417 if (pool == NULL) { |
485 | 418 return NGX_CONF_ERROR; |
419 } | |
420 | |
6631 | 421 ngx_memzero(&ctx, sizeof(ngx_stream_geo_conf_ctx_t)); |
3652 | 422 |
5124
5482671df278
Use NGX_DEFAULT_POOL_SIZE macro where appropriate.
Ruslan Ermilov <ru@nginx.com>
parents:
5084
diff
changeset
|
423 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
|
424 if (ctx.temp_pool == NULL) { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
425 ngx_destroy_pool(pool); |
485 | 426 return NGX_CONF_ERROR; |
427 } | |
428 | |
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
|
429 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); |
485 | 430 |
589 | 431 ctx.pool = cf->pool; |
6631 | 432 ctx.data_size = sizeof(ngx_stream_geo_header_t) |
433 + sizeof(ngx_stream_variable_value_t) | |
434 + 0x10000 * sizeof(ngx_stream_geo_range_t *); | |
3652 | 435 ctx.allow_binary_include = 1; |
485 | 436 |
437 save = *cf; | |
438 cf->pool = pool; | |
589 | 439 cf->ctx = &ctx; |
6631 | 440 cf->handler = ngx_stream_geo; |
485 | 441 cf->handler_conf = conf; |
442 | |
443 rv = ngx_conf_parse(cf, NULL); | |
444 | |
445 *cf = save; | |
446 | |
7214
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
447 if (rv != NGX_CONF_OK) { |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
448 goto failed; |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
449 } |
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
450 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
451 if (ctx.ranges) { |
3652 | 452 |
4968
90d8c3400769
Geo: fixed the "ranges" without ranges case.
Ruslan Ermilov <ru@nginx.com>
parents:
4967
diff
changeset
|
453 if (ctx.high.low && !ctx.binary_include) { |
3652 | 454 for (i = 0; i < 0x10000; i++) { |
455 a = (ngx_array_t *) ctx.high.low[i]; | |
456 | |
6655
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
457 if (a == NULL) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
458 continue; |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
459 } |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
460 |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
461 if (a->nelts == 0) { |
873d7053efb9
Geo: fixed access to already freed memory.
Valentin Bartenev <vbart@nginx.com>
parents:
6631
diff
changeset
|
462 ctx.high.low[i] = NULL; |
3652 | 463 continue; |
464 } | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
465 |
6631 | 466 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
|
467 |
3652 | 468 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); |
469 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
|
470 goto failed; |
3652 | 471 } |
472 | |
4967
a9d60fafaa85
Geo: improved ngx_http_geo_block() code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
473 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
|
474 ctx.high.low[i][a->nelts].value = NULL; |
3652 | 475 ctx.data_size += len + sizeof(void *); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
476 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
477 |
3652 | 478 if (ctx.allow_binary_include |
479 && !ctx.outside_entries | |
480 && ctx.entries > 100000 | |
481 && ctx.includes == 1) | |
482 { | |
6631 | 483 ngx_stream_geo_create_binary_base(&ctx); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
484 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
485 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
486 |
4816
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
487 if (ctx.high.default_value == NULL) { |
6631 | 488 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
|
489 } |
2ccaaf838bc2
Geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4681
diff
changeset
|
490 |
2399 | 491 geo->u.high = ctx.high; |
492 | |
6631 | 493 var->get_handler = ngx_stream_geo_range_variable; |
2399 | 494 var->data = (uintptr_t) geo; |
485 | 495 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
496 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
497 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
498 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
|
499 if (ctx.tree == NULL) { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
500 goto failed; |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
501 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
502 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
503 |
4992 | 504 geo->u.trees.tree = ctx.tree; |
505 | |
506 #if (NGX_HAVE_INET6) | |
507 if (ctx.tree6 == NULL) { | |
508 ctx.tree6 = ngx_radix_tree_create(cf->pool, -1); | |
509 if (ctx.tree6 == NULL) { | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
510 goto failed; |
4992 | 511 } |
512 } | |
513 | |
514 geo->u.trees.tree6 = ctx.tree6; | |
515 #endif | |
2399 | 516 |
6631 | 517 var->get_handler = ngx_stream_geo_cidr_variable; |
2399 | 518 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
519 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
520 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
6631 | 521 (uintptr_t) &ngx_stream_variable_null_value) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
522 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
523 { |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
524 goto failed; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
525 } |
4980
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
526 |
f091722acab4
Geo: ensure that default entry is always present.
Ruslan Ermilov <ru@nginx.com>
parents:
4979
diff
changeset
|
527 /* NGX_BUSY is okay (default was set explicitly) */ |
4992 | 528 |
529 #if (NGX_HAVE_INET6) | |
530 if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr, | |
6631 | 531 (uintptr_t) &ngx_stream_variable_null_value) |
4992 | 532 == NGX_ERROR) |
533 { | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
534 goto failed; |
4992 | 535 } |
536 #endif | |
485 | 537 } |
538 | |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
539 ngx_destroy_pool(ctx.temp_pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
540 ngx_destroy_pool(pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
541 |
7214
88aad69eccef
Geo: optimized configuration parser.
Ruslan Ermilov <ru@nginx.com>
parents:
7213
diff
changeset
|
542 return NGX_CONF_OK; |
7213
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
543 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
544 failed: |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
545 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
546 ngx_destroy_pool(ctx.temp_pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
547 ngx_destroy_pool(pool); |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
548 |
c69c13f10502
Geo: fixed memory allocation error handling (closes #1482).
Ruslan Ermilov <ru@nginx.com>
parents:
7087
diff
changeset
|
549 return NGX_CONF_ERROR; |
485 | 550 } |
551 | |
552 | |
489 | 553 static char * |
6631 | 554 ngx_stream_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) |
485 | 555 { |
6631 | 556 char *rv; |
557 ngx_str_t *value; | |
558 ngx_stream_geo_conf_ctx_t *ctx; | |
485 | 559 |
589 | 560 ctx = cf->ctx; |
485 | 561 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
562 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
563 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
564 if (cf->args->nelts == 1) { |
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 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
567 |
4992 | 568 if (ctx->tree |
569 #if (NGX_HAVE_INET6) | |
570 || ctx->tree6 | |
571 #endif | |
572 ) | |
573 { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
574 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
575 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
576 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
577 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
579 |
3652 | 580 ctx->ranges = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
581 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
583 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
584 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
585 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 |
485 | 588 if (cf->args->nelts != 2) { |
589 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
590 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 goto failed; |
485 | 592 } |
593 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
594 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
595 |
6631 | 596 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
|
597 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
598 goto done; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
599 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
600 |
3652 | 601 if (ctx->ranges) { |
6631 | 602 rv = ngx_stream_geo_range(cf, ctx, value); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
603 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
604 } else { |
6631 | 605 rv = ngx_stream_geo_cidr(cf, ctx, value); |
2340
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 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
609 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
610 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
611 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
612 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
613 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
614 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
615 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
616 ngx_reset_pool(cf->pool); |
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 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
619 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
620 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
621 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
622 static char * |
6631 | 623 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
|
624 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
625 { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
626 u_char *p, *last; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
627 in_addr_t start, end; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
628 ngx_str_t *net; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
629 ngx_uint_t del; |
2340
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 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
632 |
3652 | 633 if (ctx->high.default_value) { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
634 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
|
635 "duplicate default geo range value: \"%V\", old value: \"%v\"", |
3652 | 636 &value[1], ctx->high.default_value); |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
637 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
638 |
6631 | 639 ctx->high.default_value = ngx_stream_geo_value(cf, ctx, &value[1]); |
3652 | 640 if (ctx->high.default_value == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
641 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
642 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
643 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
644 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
645 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
646 |
3652 | 647 if (ctx->binary_include) { |
648 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
|
649 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 650 ctx->include_name.data); |
651 return NGX_CONF_ERROR; | |
652 } | |
653 | |
654 if (ctx->high.low == NULL) { | |
655 ctx->high.low = ngx_pcalloc(ctx->pool, | |
6631 | 656 0x10000 * sizeof(ngx_stream_geo_range_t *)); |
3652 | 657 if (ctx->high.low == NULL) { |
658 return NGX_CONF_ERROR; | |
659 } | |
660 } | |
661 | |
662 ctx->entries++; | |
663 ctx->outside_entries = 1; | |
664 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
665 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
666 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
667 del = 1; |
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 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
670 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
671 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
672 } |
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 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
675 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
676 p = ngx_strlchr(net->data, last, '-'); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
679 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
680 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
681 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
682 start = ngx_inet_addr(net->data, p - net->data); |
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 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
686 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
687 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 start = ntohl(start); |
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 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
692 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
693 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
695 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
699 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
700 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
701 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
702 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
703 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
704 if (del) { |
6631 | 705 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
|
706 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 "no address range \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
708 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 return NGX_CONF_OK; |
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 |
6631 | 713 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
|
714 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
715 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
716 return NGX_CONF_ERROR; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
719 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
720 |
6631 | 721 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
|
722 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
723 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
724 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
725 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
|
726 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
727 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
728 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
729 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
730 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
731 /* 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
|
732 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
733 static char * |
6631 | 734 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
|
735 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
736 { |
6631 | 737 in_addr_t n; |
738 ngx_uint_t h, i, s, e; | |
739 ngx_array_t *a; | |
740 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
741 |
3456
91cff7f97a50
fix a geo range if the range includes two or more /16 networks
Igor Sysoev <igor@sysoev.ru>
parents:
3115
diff
changeset
|
742 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
|
743 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
744 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
|
745 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
746 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
|
747 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
|
748 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
749 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
750 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
751 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
752 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
753 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
754 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
755 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
756 e = 0xffff; |
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 |
3652 | 759 a = (ngx_array_t *) ctx->high.low[h]; |
2340
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 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
762 a = ngx_array_create(ctx->temp_pool, 64, |
6631 | 763 sizeof(ngx_stream_geo_range_t)); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
764 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
765 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
766 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
767 |
6631 | 768 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
|
769 } |
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 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
772 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
773 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
774 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
775 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
776 i--; |
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 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
779 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
780 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
781 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
782 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
783 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
784 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
785 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
786 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
787 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
788 return NGX_CONF_ERROR; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
791 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
792 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
793 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 794 (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
|
795 |
2442 | 796 range[i + 1].start = (u_short) s; |
797 range[i + 1].end = (u_short) e; | |
798 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
799 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
800 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
801 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
802 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
803 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
804 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
805 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
806 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
807 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
808 ctx->net, ctx->value, range[i].value); |
2442 | 809 |
810 range[i].value = ctx->value; | |
811 | |
812 goto next; | |
813 } | |
814 | |
815 if (s > (ngx_uint_t) range[i].start | |
816 && e < (ngx_uint_t) range[i].end) | |
817 { | |
818 /* split the range and insert the new one */ | |
819 | |
820 range = ngx_array_push(a); | |
821 if (range == NULL) { | |
822 return NGX_CONF_ERROR; | |
823 } | |
824 | |
825 range = ngx_array_push(a); | |
826 if (range == NULL) { | |
827 return NGX_CONF_ERROR; | |
828 } | |
829 | |
830 range = a->elts; | |
831 | |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
832 ngx_memmove(&range[i + 3], &range[i + 1], |
6631 | 833 (a->nelts - 3 - i) * sizeof(ngx_stream_geo_range_t)); |
2442 | 834 |
835 range[i + 2].start = (u_short) (e + 1); | |
836 range[i + 2].end = range[i].end; | |
837 range[i + 2].value = range[i].value; | |
838 | |
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
|
839 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
|
840 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
|
841 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
|
842 |
2442 | 843 range[i].end = (u_short) (s - 1); |
844 | |
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
|
845 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
|
846 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
847 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
848 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
|
849 && 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
|
850 { |
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 /* 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
|
852 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
853 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
|
854 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
|
855 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
|
856 } |
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 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
|
859 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
860 ngx_memmove(&range[i + 1], &range[i], |
6631 | 861 (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
|
862 |
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 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
|
864 |
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 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
|
866 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
|
867 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
|
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 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
|
870 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
871 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
872 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
|
873 && 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
|
874 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
875 /* 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
|
876 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
877 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
|
878 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
|
879 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
|
880 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
881 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
882 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
|
883 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
884 ngx_memmove(&range[i + 2], &range[i + 1], |
6631 | 885 (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
|
886 |
2442 | 887 range[i + 1].start = (u_short) s; |
888 range[i + 1].end = (u_short) e; | |
889 range[i + 1].value = ctx->value; | |
890 | |
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
|
891 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
|
892 |
2442 | 893 goto next; |
2340
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 |
2448 | 896 s = (ngx_uint_t) range[i].start; |
897 e = (ngx_uint_t) range[i].end; | |
898 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
899 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 900 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
901 ctx->net, | |
902 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
903 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
904 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
905 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
906 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
908 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
910 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
911 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
912 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
913 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
914 |
6663
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
915 range = a->elts; |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
916 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
917 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
|
918 (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
|
919 |
53198d9bf84f
Geo: fixed insertion of ranges specified in descending order.
Ruslan Ermilov <ru@nginx.com>
parents:
6662
diff
changeset
|
920 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
|
921 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
|
922 range[0].value = ctx->value; |
2442 | 923 |
924 next: | |
925 | |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
926 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
927 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
928 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
929 } |
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 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
932 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
933 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
934 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
935 static ngx_uint_t |
6631 | 936 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
|
937 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
938 { |
6631 | 939 in_addr_t n; |
940 ngx_uint_t h, i, s, e, warn; | |
941 ngx_array_t *a; | |
942 ngx_stream_geo_range_t *range; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
943 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
944 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
945 |
6662
1301a58b5dac
Geo: fixed removing a range in certain cases.
Ruslan Ermilov <ru@nginx.com>
parents:
6661
diff
changeset
|
946 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
|
947 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
948 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
949 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
950 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
951 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
952 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
953 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
954 } |
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 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
957 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
958 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
959 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
960 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
961 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
962 |
3652 | 963 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
964 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
965 if (a == NULL || a->nelts == 0) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
966 warn = 1; |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
967 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
968 } |
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 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
971 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
972 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
973 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
974 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
975 { |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
976 ngx_memmove(&range[i], &range[i + 1], |
6631 | 977 (a->nelts - 1 - i) * sizeof(ngx_stream_geo_range_t)); |
2450 | 978 |
979 a->nelts--; | |
980 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
981 break; |
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 |
6664
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
984 if (i == a->nelts - 1) { |
0ee6f023ef92
Geo: fixed warnings when removing nonexistent ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6663
diff
changeset
|
985 warn = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
986 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
987 } |
6661
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
988 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
989 next: |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
990 |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
991 if (h == 0xffff) { |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
992 break; |
ec141a44641b
Geo: fixed overflow when iterating over ranges.
Ruslan Ermilov <ru@nginx.com>
parents:
6655
diff
changeset
|
993 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
994 } |
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 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
997 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
998 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
999 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1000 static char * |
6631 | 1001 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
|
1002 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1003 { |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1004 char *rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1005 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
|
1006 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
|
1007 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1008 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1009 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1010 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1011 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1012 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1013 } |
485 | 1014 } |
1015 | |
4992 | 1016 #if (NGX_HAVE_INET6) |
1017 if (ctx->tree6 == NULL) { | |
1018 ctx->tree6 = ngx_radix_tree_create(ctx->pool, -1); | |
1019 if (ctx->tree6 == NULL) { | |
1020 return NGX_CONF_ERROR; | |
1021 } | |
1022 } | |
1023 #endif | |
1024 | |
485 | 1025 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
|
1026 cidr.family = AF_INET; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1027 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1028 cidr.u.in.mask = 0; |
485 | 1029 |
6631 | 1030 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
|
1031 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1032 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1033 return rv; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1034 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
1035 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1036 #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
|
1037 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
|
1038 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
|
1039 |
6631 | 1040 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
|
1041 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1042 if (rv != NGX_CONF_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1043 return rv; |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1044 } |
4992 | 1045 #endif |
1046 | |
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_OK; |
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 (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
|
1051 net = &value[1]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1052 del = 1; |
4992 | 1053 |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1054 } else { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1055 net = &value[0]; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1056 del = 0; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1057 } |
2365 | 1058 |
6631 | 1059 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
|
1060 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
|
1061 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1062 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1063 if (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
|
1064 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
|
1065 cidr.u.in.mask = ntohl(cidr.u.in.mask); |
485 | 1066 } |
1067 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1068 if (del) { |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1069 switch (cidr.family) { |
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 #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
|
1072 case AF_INET6: |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1073 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
|
1074 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
|
1075 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
|
1076 break; |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1077 #endif |
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 default: /* AF_INET */ |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1080 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
|
1081 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
|
1082 break; |
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 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1085 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
|
1086 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
|
1087 "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
|
1088 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1089 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1090 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
|
1091 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1092 |
6631 | 1093 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
|
1094 } |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1095 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1096 |
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1097 static char * |
6631 | 1098 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
|
1099 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
|
1100 { |
6631 | 1101 ngx_int_t rc; |
1102 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
|
1103 |
6631 | 1104 val = ngx_stream_geo_value(cf, ctx, value); |
485 | 1105 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
1106 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1107 return NGX_CONF_ERROR; |
485 | 1108 } |
1109 | |
4996
f95b3772d832
Geo: made "default" affect both IPv4 and IPv6 when using prefixes.
Ruslan Ermilov <ru@nginx.com>
parents:
4993
diff
changeset
|
1110 switch (cidr->family) { |
4992 | 1111 |
1112 #if (NGX_HAVE_INET6) | |
1113 case AF_INET6: | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1114 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
|
1115 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1116 (uintptr_t) val); |
4992 | 1117 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1118 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1119 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1120 } |
4992 | 1121 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1122 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1123 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1124 } |
553 | 1125 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1126 /* rc == NGX_BUSY */ |
4992 | 1127 |
6631 | 1128 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1129 ngx_radix128tree_find(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1130 cidr->u.in6.addr.s6_addr); |
4992 | 1131 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1132 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1133 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1134 net, val, old); |
4992 | 1135 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1136 rc = ngx_radix128tree_delete(ctx->tree6, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1137 cidr->u.in6.addr.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1138 cidr->u.in6.mask.s6_addr); |
4992 | 1139 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1140 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1141 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
|
1142 return NGX_CONF_ERROR; |
553 | 1143 } |
1144 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1145 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
|
1146 cidr->u.in6.mask.s6_addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1147 (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1148 |
4992 | 1149 break; |
1150 #endif | |
1151 | |
1152 default: /* AF_INET */ | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1153 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1154 cidr->u.in.mask, (uintptr_t) val); |
553 | 1155 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1156 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1157 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1158 } |
553 | 1159 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1160 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1161 return NGX_CONF_ERROR; |
553 | 1162 } |
4992 | 1163 |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1164 /* rc == NGX_BUSY */ |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1165 |
6631 | 1166 old = (ngx_stream_variable_value_t *) |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1167 ngx_radix32tree_find(ctx->tree, cidr->u.in.addr); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1168 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1169 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1170 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1171 net, val, old); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1172 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1173 rc = ngx_radix32tree_delete(ctx->tree, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1174 cidr->u.in.addr, cidr->u.in.mask); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1175 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1176 if (rc == NGX_ERROR) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1177 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
|
1178 return NGX_CONF_ERROR; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1179 } |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1180 |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1181 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1182 cidr->u.in.mask, (uintptr_t) val); |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1183 |
4992 | 1184 break; |
485 | 1185 } |
1186 | |
4999
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1187 if (rc == NGX_OK) { |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1188 return NGX_CONF_OK; |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1189 } |
3f776d65c5b9
Geo: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
4996
diff
changeset
|
1190 |
553 | 1191 return NGX_CONF_ERROR; |
485 | 1192 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1193 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1194 |
6631 | 1195 static ngx_stream_variable_value_t * |
1196 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
|
1197 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1198 { |
6631 | 1199 uint32_t hash; |
1200 ngx_stream_variable_value_t *val; | |
1201 ngx_stream_geo_variable_value_node_t *gvvn; | |
2340
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 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1204 |
6631 | 1205 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
|
1206 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1207 |
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
|
1208 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
|
1209 return gvvn->value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1210 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1211 |
6631 | 1212 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
|
1213 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1214 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1215 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1216 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1217 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1218 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1219 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1220 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1221 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1222 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1223 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1224 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1225 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1226 |
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
|
1227 gvvn = ngx_palloc(ctx->temp_pool, |
6631 | 1228 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
|
1229 if (gvvn == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1230 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1231 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1232 |
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
|
1233 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
|
1234 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
|
1235 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
|
1236 gvvn->value = val; |
3652 | 1237 gvvn->offset = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1238 |
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
|
1239 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1240 |
6631 | 1241 ctx->data_size += ngx_align(sizeof(ngx_stream_variable_value_t) |
1242 + value->len, sizeof(void *)); | |
3652 | 1243 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1244 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1245 } |
2992
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 static ngx_int_t |
6631 | 1249 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
|
1250 { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1251 ngx_int_t rc; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1252 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1253 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
|
1254 cidr->family = AF_INET; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1255 cidr->u.in.addr = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1256 cidr->u.in.mask = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1257 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1258 return NGX_OK; |
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 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1261 rc = ngx_ptocidr(net, cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1262 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1263 if (rc == NGX_ERROR) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1264 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
|
1265 return NGX_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1266 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1267 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1268 if (rc == NGX_DONE) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1269 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1270 "low address bits of %V are meaningless", net); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1271 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1272 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1273 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1274 } |
3652 | 1275 |
1276 | |
1277 static char * | |
6631 | 1278 ngx_stream_geo_include(ngx_conf_t *cf, ngx_stream_geo_conf_ctx_t *ctx, |
3652 | 1279 ngx_str_t *name) |
1280 { | |
1281 char *rv; | |
1282 ngx_str_t file; | |
1283 | |
1284 file.len = name->len + 4; | |
1285 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5); | |
1286 if (file.data == NULL) { | |
1287 return NGX_CONF_ERROR; | |
1288 } | |
1289 | |
1290 ngx_sprintf(file.data, "%V.bin%Z", name); | |
1291 | |
5330
314c3d7cc3a5
Backed out f1a91825730a and 7094bd12c1ff.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5317
diff
changeset
|
1292 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { |
3652 | 1293 return NGX_CONF_ERROR; |
1294 } | |
1295 | |
3654
b56935e34273
test binary geo ranges base only for ranges
Igor Sysoev <igor@sysoev.ru>
parents:
3653
diff
changeset
|
1296 if (ctx->ranges) { |
3660 | 1297 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); |
3652 | 1298 |
6631 | 1299 switch (ngx_stream_geo_include_binary_base(cf, ctx, &file)) { |
3660 | 1300 case NGX_OK: |
1301 return NGX_CONF_OK; | |
1302 case NGX_ERROR: | |
1303 return NGX_CONF_ERROR; | |
1304 default: | |
1305 break; | |
1306 } | |
3652 | 1307 } |
1308 | |
1309 file.len -= 4; | |
1310 file.data[file.len] = '\0'; | |
1311 | |
1312 ctx->include_name = file; | |
1313 | |
1314 if (ctx->outside_entries) { | |
1315 ctx->allow_binary_include = 0; | |
1316 } | |
1317 | |
1318 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
1319 | |
1320 rv = ngx_conf_parse(cf, &file); | |
1321 | |
1322 ctx->includes++; | |
1323 ctx->outside_entries = 0; | |
1324 | |
1325 return rv; | |
1326 } | |
1327 | |
1328 | |
1329 static ngx_int_t | |
6631 | 1330 ngx_stream_geo_include_binary_base(ngx_conf_t *cf, |
1331 ngx_stream_geo_conf_ctx_t *ctx, ngx_str_t *name) | |
3652 | 1332 { |
6631 | 1333 u_char *base, ch; |
1334 time_t mtime; | |
1335 size_t size, len; | |
1336 ssize_t n; | |
1337 uint32_t crc32; | |
1338 ngx_err_t err; | |
1339 ngx_int_t rc; | |
1340 ngx_uint_t i; | |
1341 ngx_file_t file; | |
1342 ngx_file_info_t fi; | |
1343 ngx_stream_geo_range_t *range, **ranges; | |
1344 ngx_stream_geo_header_t *header; | |
1345 ngx_stream_variable_value_t *vv; | |
3652 | 1346 |
1347 ngx_memzero(&file, sizeof(ngx_file_t)); | |
1348 file.name = *name; | |
1349 file.log = cf->log; | |
1350 | |
7087
47b7ffc3339d
Fixed calls to ngx_open_file() in certain places.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7086
diff
changeset
|
1351 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
7086 | 1352 |
3652 | 1353 if (file.fd == NGX_INVALID_FILE) { |
1354 err = ngx_errno; | |
1355 if (err != NGX_ENOENT) { | |
1356 ngx_conf_log_error(NGX_LOG_CRIT, cf, err, | |
1357 ngx_open_file_n " \"%s\" failed", name->data); | |
1358 } | |
1359 return NGX_DECLINED; | |
1360 } | |
1361 | |
1362 if (ctx->outside_entries) { | |
1363 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
|
1364 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 1365 name->data); |
1366 rc = NGX_ERROR; | |
1367 goto done; | |
1368 } | |
1369 | |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1370 if (ctx->binary_include) { |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1371 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
|
1372 "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
|
1373 name->data, ctx->include_name.data); |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1374 rc = NGX_ERROR; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1375 goto done; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1376 } |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1377 |
3652 | 1378 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) { |
1379 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1380 ngx_fd_info_n " \"%s\" failed", name->data); | |
1381 goto failed; | |
1382 } | |
1383 | |
1384 size = (size_t) ngx_file_size(&fi); | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1385 mtime = ngx_file_mtime(&fi); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1386 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1387 ch = name->data[name->len - 4]; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1388 name->data[name->len - 4] = '\0'; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1389 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1390 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
|
1391 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
|
1392 ngx_file_info_n " \"%s\" failed", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1393 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1394 } |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1395 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1396 name->data[name->len - 4] = ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1397 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1398 if (mtime < ngx_file_mtime(&fi)) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1399 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
|
1400 "stale binary geo range base \"%s\"", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1401 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1402 } |
3652 | 1403 |
1404 base = ngx_palloc(ctx->pool, size); | |
1405 if (base == NULL) { | |
1406 goto failed; | |
1407 } | |
1408 | |
1409 n = ngx_read_file(&file, base, size, 0); | |
1410 | |
1411 if (n == NGX_ERROR) { | |
1412 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1413 ngx_read_file_n " \"%s\" failed", name->data); | |
1414 goto failed; | |
1415 } | |
1416 | |
1417 if ((size_t) n != size) { | |
1418 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0, | |
1419 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z", | |
1420 name->data, n, size); | |
1421 goto failed; | |
1422 } | |
1423 | |
6631 | 1424 header = (ngx_stream_geo_header_t *) base; |
3652 | 1425 |
6631 | 1426 if (size < 16 || ngx_memcmp(&ngx_stream_geo_header, header, 12) != 0) { |
3652 | 1427 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
1428 "incompatible binary geo range base \"%s\"", name->data); | |
1429 goto failed; | |
1430 } | |
1431 | |
1432 ngx_crc32_init(crc32); | |
1433 | |
6631 | 1434 vv = (ngx_stream_variable_value_t *) |
1435 (base + sizeof(ngx_stream_geo_header_t)); | |
3652 | 1436 |
5757
c5ec6944de98
Style: add whitespace between control statement and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5330
diff
changeset
|
1437 while (vv->data) { |
6631 | 1438 len = ngx_align(sizeof(ngx_stream_variable_value_t) + vv->len, |
3652 | 1439 sizeof(void *)); |
1440 ngx_crc32_update(&crc32, (u_char *) vv, len); | |
1441 vv->data += (size_t) base; | |
6631 | 1442 vv = (ngx_stream_variable_value_t *) ((u_char *) vv + len); |
3652 | 1443 } |
6631 | 1444 ngx_crc32_update(&crc32, (u_char *) vv, |
1445 sizeof(ngx_stream_variable_value_t)); | |
3652 | 1446 vv++; |
1447 | |
6631 | 1448 ranges = (ngx_stream_geo_range_t **) vv; |
3652 | 1449 |
1450 for (i = 0; i < 0x10000; i++) { | |
1451 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *)); | |
1452 if (ranges[i]) { | |
6631 | 1453 ranges[i] = (ngx_stream_geo_range_t *) |
3652 | 1454 ((u_char *) ranges[i] + (size_t) base); |
1455 } | |
1456 } | |
1457 | |
6631 | 1458 range = (ngx_stream_geo_range_t *) &ranges[0x10000]; |
3652 | 1459 |
1460 while ((u_char *) range < base + size) { | |
1461 while (range->value) { | |
1462 ngx_crc32_update(&crc32, (u_char *) range, | |
6631 | 1463 sizeof(ngx_stream_geo_range_t)); |
1464 range->value = (ngx_stream_variable_value_t *) | |
3652 | 1465 ((u_char *) range->value + (size_t) base); |
1466 range++; | |
1467 } | |
1468 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *)); | |
6631 | 1469 range = (ngx_stream_geo_range_t *) ((u_char *) range + sizeof(void *)); |
3652 | 1470 } |
1471 | |
1472 ngx_crc32_final(crc32); | |
1473 | |
1474 if (crc32 != header->crc32) { | |
1475 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1476 "CRC32 mismatch in binary geo range base \"%s\"", name->data); | |
1477 goto failed; | |
1478 } | |
1479 | |
1480 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, | |
1481 "using binary geo range base \"%s\"", name->data); | |
1482 | |
1483 ctx->include_name = *name; | |
1484 ctx->binary_include = 1; | |
1485 ctx->high.low = ranges; | |
1486 rc = NGX_OK; | |
1487 | |
1488 goto done; | |
1489 | |
1490 failed: | |
1491 | |
1492 rc = NGX_DECLINED; | |
1493 | |
1494 done: | |
1495 | |
1496 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
1497 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, | |
1498 ngx_close_file_n " \"%s\" failed", name->data); | |
1499 } | |
1500 | |
1501 return rc; | |
1502 } | |
1503 | |
1504 | |
1505 static void | |
6631 | 1506 ngx_stream_geo_create_binary_base(ngx_stream_geo_conf_ctx_t *ctx) |
3652 | 1507 { |
6631 | 1508 u_char *p; |
1509 uint32_t hash; | |
1510 ngx_str_t s; | |
1511 ngx_uint_t i; | |
1512 ngx_file_mapping_t fm; | |
1513 ngx_stream_geo_range_t *r, *range, **ranges; | |
1514 ngx_stream_geo_header_t *header; | |
1515 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1516 |
1517 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5); | |
1518 if (fm.name == NULL) { | |
1519 return; | |
1520 } | |
1521 | |
1522 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name); | |
1523 | |
1524 fm.size = ctx->data_size; | |
1525 fm.log = ctx->pool->log; | |
1526 | |
1527 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0, | |
1528 "creating binary geo range base \"%s\"", fm.name); | |
1529 | |
1530 if (ngx_create_file_mapping(&fm) != NGX_OK) { | |
1531 return; | |
1532 } | |
1533 | |
6631 | 1534 p = ngx_cpymem(fm.addr, &ngx_stream_geo_header, |
1535 sizeof(ngx_stream_geo_header_t)); | |
3652 | 1536 |
6631 | 1537 p = ngx_stream_geo_copy_values(fm.addr, p, ctx->rbtree.root, |
1538 ctx->rbtree.sentinel); | |
3652 | 1539 |
6631 | 1540 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1541 |
6631 | 1542 ranges = (ngx_stream_geo_range_t **) p; |
3652 | 1543 |
6631 | 1544 p += 0x10000 * sizeof(ngx_stream_geo_range_t *); |
3652 | 1545 |
1546 for (i = 0; i < 0x10000; i++) { | |
1547 r = ctx->high.low[i]; | |
1548 if (r == NULL) { | |
1549 continue; | |
1550 } | |
1551 | |
6631 | 1552 range = (ngx_stream_geo_range_t *) p; |
1553 ranges[i] = (ngx_stream_geo_range_t *) (p - (u_char *) fm.addr); | |
3652 | 1554 |
1555 do { | |
1556 s.len = r->value->len; | |
1557 s.data = r->value->data; | |
1558 hash = ngx_crc32_long(s.data, s.len); | |
6631 | 1559 gvvn = (ngx_stream_geo_variable_value_node_t *) |
3652 | 1560 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash); |
1561 | |
6631 | 1562 range->value = (ngx_stream_variable_value_t *) gvvn->offset; |
3652 | 1563 range->start = r->start; |
1564 range->end = r->end; | |
1565 range++; | |
1566 | |
1567 } while ((++r)->value); | |
1568 | |
1569 range->value = NULL; | |
1570 | |
1571 p = (u_char *) range + sizeof(void *); | |
1572 } | |
1573 | |
1574 header = fm.addr; | |
1575 header->crc32 = ngx_crc32_long((u_char *) fm.addr | |
6631 | 1576 + sizeof(ngx_stream_geo_header_t), |
1577 fm.size - sizeof(ngx_stream_geo_header_t)); | |
3652 | 1578 |
1579 ngx_close_file_mapping(&fm); | |
1580 } | |
1581 | |
1582 | |
1583 static u_char * | |
6631 | 1584 ngx_stream_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node, |
3652 | 1585 ngx_rbtree_node_t *sentinel) |
1586 { | |
6631 | 1587 ngx_stream_variable_value_t *vv; |
1588 ngx_stream_geo_variable_value_node_t *gvvn; | |
3652 | 1589 |
1590 if (node == sentinel) { | |
1591 return p; | |
1592 } | |
1593 | |
6631 | 1594 gvvn = (ngx_stream_geo_variable_value_node_t *) node; |
3652 | 1595 gvvn->offset = p - base; |
1596 | |
6631 | 1597 vv = (ngx_stream_variable_value_t *) p; |
3652 | 1598 *vv = *gvvn->value; |
6631 | 1599 p += sizeof(ngx_stream_variable_value_t); |
3652 | 1600 vv->data = (u_char *) (p - base); |
1601 | |
1602 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len); | |
1603 | |
1604 p = ngx_align_ptr(p, sizeof(void *)); | |
1605 | |
6631 | 1606 p = ngx_stream_geo_copy_values(base, p, node->left, sentinel); |
3652 | 1607 |
6631 | 1608 return ngx_stream_geo_copy_values(base, p, node->right, sentinel); |
3652 | 1609 } |