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