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