Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geo_module.c @ 4858:5e789d8b009d stable-1.2
Merge of r4833: limit req: fix of rbtree node insertion.
Limit req: fix of rbtree node insertion on hash collisions.
The rbtree used in ngx_http_limit_req_module has two level of keys, the top is
hash, and the next is the value string itself. However, when inserting a new
node, only hash has been set, while the value string has been left empty.
The bug was introduced in r4419 (1.1.14).
Found by Charles Chen.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 24 Sep 2012 19:11:45 +0000 |
parents | 880dedfa4008 |
children | 9f4cdc7a8578 05beaa2d87b3 |
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 { |
3652 | 21 ngx_http_geo_range_t **low; |
2399 | 22 ngx_http_variable_value_t *default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
23 } ngx_http_geo_high_ranges_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
24 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
25 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
26 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
|
27 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
|
28 ngx_http_variable_value_t *value; |
3652 | 29 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
|
30 } 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
|
31 |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
32 |
6802ba529ec4
change ngx_http_variable_value_node_t to more generic ngx_str_node_t
Igor Sysoev <igor@sysoev.ru>
parents:
3456
diff
changeset
|
33 typedef struct { |
2399 | 34 ngx_http_variable_value_t *value; |
35 ngx_str_t *net; | |
3652 | 36 ngx_http_geo_high_ranges_t high; |
2399 | 37 ngx_radix_tree_t *tree; |
38 ngx_rbtree_t rbtree; | |
39 ngx_rbtree_node_t sentinel; | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
40 ngx_array_t *proxies; |
2399 | 41 ngx_pool_t *pool; |
42 ngx_pool_t *temp_pool; | |
3652 | 43 |
44 size_t data_size; | |
45 | |
46 ngx_str_t include_name; | |
47 ngx_uint_t includes; | |
48 ngx_uint_t entries; | |
49 | |
50 unsigned ranges:1; | |
51 unsigned outside_entries:1; | |
52 unsigned allow_binary_include:1; | |
53 unsigned binary_include:1; | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
54 unsigned proxy_recursive:1; |
589 | 55 } ngx_http_geo_conf_ctx_t; |
485 | 56 |
57 | |
2399 | 58 typedef struct { |
59 union { | |
60 ngx_radix_tree_t *tree; | |
3652 | 61 ngx_http_geo_high_ranges_t high; |
2399 | 62 } u; |
63 | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
64 ngx_array_t *proxies; |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
65 unsigned proxy_recursive:1; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
66 |
2399 | 67 ngx_int_t index; |
68 } ngx_http_geo_ctx_t; | |
69 | |
70 | |
71 static in_addr_t ngx_http_geo_addr(ngx_http_request_t *r, | |
72 ngx_http_geo_ctx_t *ctx); | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
73 static ngx_int_t ngx_http_geo_real_addr(ngx_http_request_t *r, |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
74 ngx_http_geo_ctx_t *ctx, ngx_addr_t *addr); |
485 | 75 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
76 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
|
77 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
|
78 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 ngx_cidr_t *cidr); |
3652 | 91 static char *ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
92 ngx_str_t *name); | |
93 static ngx_int_t ngx_http_geo_include_binary_base(ngx_conf_t *cf, | |
94 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *name); | |
95 static void ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx); | |
96 static u_char *ngx_http_geo_copy_values(u_char *base, u_char *p, | |
97 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); | |
485 | 98 |
99 | |
100 static ngx_command_t ngx_http_geo_commands[] = { | |
101 | |
102 { ngx_string("geo"), | |
2399 | 103 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, |
485 | 104 ngx_http_geo_block, |
105 NGX_HTTP_MAIN_CONF_OFFSET, | |
106 0, | |
107 NULL }, | |
108 | |
109 ngx_null_command | |
110 }; | |
111 | |
112 | |
113 static ngx_http_module_t ngx_http_geo_module_ctx = { | |
509 | 114 NULL, /* preconfiguration */ |
115 NULL, /* postconfiguration */ | |
485 | 116 |
117 NULL, /* create main configuration */ | |
118 NULL, /* init main configuration */ | |
119 | |
120 NULL, /* create server configuration */ | |
121 NULL, /* merge server configuration */ | |
122 | |
123 NULL, /* create location configuration */ | |
124 NULL /* merge location configuration */ | |
125 }; | |
126 | |
127 | |
128 ngx_module_t ngx_http_geo_module = { | |
509 | 129 NGX_MODULE_V1, |
485 | 130 &ngx_http_geo_module_ctx, /* module context */ |
131 ngx_http_geo_commands, /* module directives */ | |
132 NGX_HTTP_MODULE, /* module type */ | |
541 | 133 NULL, /* init master */ |
485 | 134 NULL, /* init module */ |
541 | 135 NULL, /* init process */ |
136 NULL, /* init thread */ | |
137 NULL, /* exit thread */ | |
138 NULL, /* exit process */ | |
139 NULL, /* exit master */ | |
140 NGX_MODULE_V1_PADDING | |
485 | 141 }; |
142 | |
143 | |
3652 | 144 typedef struct { |
145 u_char GEORNG[6]; | |
146 u_char version; | |
147 u_char ptr_size; | |
4725
47936d1c98e9
Merge of r4682, r4694, r4699, r4704, r4705: minor nits.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4668
diff
changeset
|
148 uint32_t endianness; |
3652 | 149 uint32_t crc32; |
150 } ngx_http_geo_header_t; | |
151 | |
152 | |
153 static ngx_http_geo_header_t ngx_http_geo_header = { | |
154 { 'G', 'E', 'O', 'R', 'N', 'G' }, 0, sizeof(void *), 0x12345678, 0 | |
155 }; | |
156 | |
157 | |
485 | 158 /* AF_INET only */ |
159 | |
573 | 160 static ngx_int_t |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
161 ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
573 | 162 uintptr_t data) |
485 | 163 { |
2399 | 164 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data; |
485 | 165 |
573 | 166 ngx_http_variable_value_t *vv; |
485 | 167 |
573 | 168 vv = (ngx_http_variable_value_t *) |
2399 | 169 ngx_radix32tree_find(ctx->u.tree, ngx_http_geo_addr(r, ctx)); |
489 | 170 |
573 | 171 *v = *vv; |
172 | |
2399 | 173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
174 "http geo: %v", v); | |
489 | 175 |
573 | 176 return NGX_OK; |
485 | 177 } |
178 | |
179 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
180 static ngx_int_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
181 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
|
182 uintptr_t data) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
183 { |
2399 | 184 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
|
185 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
186 in_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
|
187 ngx_uint_t n; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
188 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
189 |
3652 | 190 *v = *ctx->u.high.default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
191 |
2399 | 192 addr = ngx_http_geo_addr(r, ctx); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
193 |
3652 | 194 range = ctx->u.high.low[addr >> 16]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
195 |
3645
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
196 if (range) { |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
197 n = addr & 0xffff; |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
198 do { |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
199 if (n >= (ngx_uint_t) range->start && n <= (ngx_uint_t) range->end) |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
200 { |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
201 *v = *range->value; |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
202 break; |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
203 } |
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
204 } while ((++range)->value); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
205 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
206 |
2399 | 207 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
208 "http geo: %v", v); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
209 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
210 return NGX_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
211 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
212 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
213 |
2399 | 214 static in_addr_t |
215 ngx_http_geo_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx) | |
216 { | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
217 ngx_addr_t addr; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
218 ngx_table_elt_t *xfwd; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
219 struct sockaddr_in *sin; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
220 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
221 if (ngx_http_geo_real_addr(r, ctx, &addr) != NGX_OK) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
222 return INADDR_NONE; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
223 } |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
224 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
225 xfwd = r->headers_in.x_forwarded_for; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
226 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
227 if (xfwd != NULL && ctx->proxies != NULL) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
228 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data, |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
229 xfwd->value.len, ctx->proxies, |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
230 ctx->proxy_recursive); |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
231 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
232 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
233 #if (NGX_HAVE_INET6) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
234 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
235 if (addr.sockaddr->sa_family == AF_INET6) { |
4857
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
236 u_char *p; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
237 in_addr_t inaddr; |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
238 struct in6_addr *inaddr6; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
239 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
240 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
241 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
242 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { |
4857
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
243 p = inaddr6->s6_addr; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
244 |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
245 inaddr = p[12] << 24; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
246 inaddr += p[13] << 16; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
247 inaddr += p[14] << 8; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
248 inaddr += p[15]; |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
249 |
880dedfa4008
Merge of r4829: fixed strict aliasing with ipv6 (ticket #201).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4855
diff
changeset
|
250 return inaddr; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
251 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
252 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
253 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
254 #endif |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
255 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
256 if (addr.sockaddr->sa_family != AF_INET) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
257 return INADDR_NONE; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
258 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
259 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
260 sin = (struct sockaddr_in *) addr.sockaddr; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
261 return ntohl(sin->sin_addr.s_addr); |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
262 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
263 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
264 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
265 static ngx_int_t |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
266 ngx_http_geo_real_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx, |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
267 ngx_addr_t *addr) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
268 { |
2399 | 269 ngx_http_variable_value_t *v; |
270 | |
271 if (ctx->index == -1) { | |
272 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
273 "http geo started: %V", &r->connection->addr_text); | |
274 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
275 addr->sockaddr = r->connection->sockaddr; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
276 addr->socklen = r->connection->socklen; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
277 /* 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
|
278 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
279 return NGX_OK; |
2399 | 280 } |
281 | |
282 v = ngx_http_get_flushed_variable(r, ctx->index); | |
283 | |
284 if (v == NULL || v->not_found) { | |
285 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
286 "http geo not found"); | |
287 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
288 return NGX_ERROR; |
2399 | 289 } |
290 | |
291 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
292 "http geo started: %v", v); | |
293 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
294 if (ngx_parse_addr(r->pool, addr, v->data, v->len) == NGX_OK) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
295 return NGX_OK; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
296 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
297 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
298 return NGX_ERROR; |
2399 | 299 } |
300 | |
301 | |
489 | 302 static char * |
303 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 304 { |
589 | 305 char *rv; |
3645
9f22c780d574
eliminate a number of ranges: about 18,000 /16 networks are empty,
Igor Sysoev <igor@sysoev.ru>
parents:
3643
diff
changeset
|
306 void **p; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
307 size_t len; |
2399 | 308 ngx_str_t *value, name; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
309 ngx_uint_t i; |
589 | 310 ngx_conf_t save; |
311 ngx_pool_t *pool; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
312 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
|
313 ngx_http_variable_t *var; |
2399 | 314 ngx_http_geo_ctx_t *geo; |
589 | 315 ngx_http_geo_conf_ctx_t ctx; |
485 | 316 |
501 | 317 value = cf->args->elts; |
318 | |
2399 | 319 geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t)); |
320 if (geo == NULL) { | |
321 return NGX_CONF_ERROR; | |
322 } | |
323 | |
324 name = value[1]; | |
325 name.len--; | |
326 name.data++; | |
327 | |
328 if (cf->args->nelts == 3) { | |
329 | |
330 geo->index = ngx_http_get_variable_index(cf, &name); | |
331 if (geo->index == NGX_ERROR) { | |
332 return NGX_CONF_ERROR; | |
333 } | |
334 | |
335 name = value[2]; | |
336 name.len--; | |
337 name.data++; | |
338 | |
339 } else { | |
340 geo->index = -1; | |
341 } | |
342 | |
343 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
501 | 344 if (var == NULL) { |
485 | 345 return NGX_CONF_ERROR; |
346 } | |
347 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
348 pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
349 if (pool == NULL) { |
485 | 350 return NGX_CONF_ERROR; |
351 } | |
352 | |
3652 | 353 ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t)); |
354 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
355 ctx.temp_pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
356 if (ctx.temp_pool == NULL) { |
485 | 357 return NGX_CONF_ERROR; |
358 } | |
359 | |
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
|
360 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); |
485 | 361 |
589 | 362 ctx.pool = cf->pool; |
3652 | 363 ctx.data_size = sizeof(ngx_http_geo_header_t) |
364 + sizeof(ngx_http_variable_value_t) | |
365 + 0x10000 * sizeof(ngx_http_geo_range_t *); | |
366 ctx.allow_binary_include = 1; | |
485 | 367 |
368 save = *cf; | |
369 cf->pool = pool; | |
589 | 370 cf->ctx = &ctx; |
485 | 371 cf->handler = ngx_http_geo; |
372 cf->handler_conf = conf; | |
373 | |
374 rv = ngx_conf_parse(cf, NULL); | |
375 | |
376 *cf = save; | |
377 | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
378 geo->proxies = ctx.proxies; |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
379 geo->proxy_recursive = ctx.proxy_recursive; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
380 |
3652 | 381 if (ctx.high.low) { |
382 | |
383 if (!ctx.binary_include) { | |
384 for (i = 0; i < 0x10000; i++) { | |
385 a = (ngx_array_t *) ctx.high.low[i]; | |
386 | |
387 if (a == NULL || a->nelts == 0) { | |
388 continue; | |
389 } | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
390 |
3652 | 391 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
|
392 |
3652 | 393 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); |
394 if (ctx.high.low[i] == NULL) { | |
395 return NGX_CONF_ERROR; | |
396 } | |
397 | |
398 p = (void **) ngx_cpymem(ctx.high.low[i], a->elts, len); | |
399 *p = NULL; | |
400 ctx.data_size += len + sizeof(void *); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
401 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
402 |
3652 | 403 if (ctx.allow_binary_include |
404 && !ctx.outside_entries | |
405 && ctx.entries > 100000 | |
406 && ctx.includes == 1) | |
407 { | |
408 ngx_http_geo_create_binary_base(&ctx); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
409 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
410 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
411 |
4855
f66d0a6d7fdd
Merge of r4817: geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4725
diff
changeset
|
412 if (ctx.high.default_value == NULL) { |
f66d0a6d7fdd
Merge of r4817: geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4725
diff
changeset
|
413 ctx.high.default_value = &ngx_http_variable_null_value; |
f66d0a6d7fdd
Merge of r4817: geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4725
diff
changeset
|
414 } |
f66d0a6d7fdd
Merge of r4817: geo: fixed handling of ranges without default set.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4725
diff
changeset
|
415 |
2399 | 416 geo->u.high = ctx.high; |
417 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
418 var->get_handler = ngx_http_geo_range_variable; |
2399 | 419 var->data = (uintptr_t) geo; |
485 | 420 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
421 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
422 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
423 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
424 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
425 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
426 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
|
427 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
428 return NGX_CONF_ERROR; |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
429 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
430 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
431 |
2399 | 432 geo->u.tree = ctx.tree; |
433 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
434 var->get_handler = ngx_http_geo_cidr_variable; |
2399 | 435 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
436 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
437 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
438 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
439 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
440 if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
441 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
442 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
443 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
444 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
445 (uintptr_t) &ngx_http_variable_null_value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
446 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
447 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
448 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
449 } |
485 | 450 } |
451 | |
452 return rv; | |
453 } | |
454 | |
455 | |
489 | 456 static char * |
457 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
485 | 458 { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
459 char *rv; |
3652 | 460 ngx_str_t *value; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
461 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
462 ngx_http_geo_conf_ctx_t *ctx; |
485 | 463 |
589 | 464 ctx = cf->ctx; |
485 | 465 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
466 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
467 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
468 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
469 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
470 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
471 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
472 if (ctx->tree) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
473 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
474 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
475 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
476 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
477 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
478 |
3652 | 479 ctx->ranges = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
480 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
481 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
482 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
483 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
484 } |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
485 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
486 else if (ngx_strcmp(value[0].data, "proxy_recursive") == 0) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
487 ctx->proxy_recursive = 1; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
488 rv = NGX_CONF_OK; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
489 goto done; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
490 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
491 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
492 |
485 | 493 if (cf->args->nelts != 2) { |
494 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
495 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
496 goto failed; |
485 | 497 } |
498 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
499 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
500 |
3652 | 501 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
|
502 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
503 goto done; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
504 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
505 } else if (ngx_strcmp(value[0].data, "proxy") == 0) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
506 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
507 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
|
508 goto failed; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
509 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
510 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
511 rv = ngx_http_geo_add_proxy(cf, ctx, &cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
512 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
513 goto done; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
514 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
515 |
3652 | 516 if (ctx->ranges) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
517 rv = ngx_http_geo_range(cf, ctx, value); |
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 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
520 rv = ngx_http_geo_cidr(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
521 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
522 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
523 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
524 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
525 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
526 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
527 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
528 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
529 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
530 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
531 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
532 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
533 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
534 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
535 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
536 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
537 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
538 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
|
539 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
540 { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
541 u_char *p, *last; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
542 in_addr_t start, end; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
543 ngx_str_t *net; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
544 ngx_uint_t del; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
545 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
546 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
547 |
3652 | 548 if (ctx->high.default_value) { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
549 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
|
550 "duplicate default geo range value: \"%V\", old value: \"%v\"", |
3652 | 551 &value[1], ctx->high.default_value); |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
552 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
553 |
3652 | 554 ctx->high.default_value = ngx_http_geo_value(cf, ctx, &value[1]); |
555 if (ctx->high.default_value == NULL) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
556 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
557 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
558 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
559 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
560 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
561 |
3652 | 562 if (ctx->binary_include) { |
563 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
|
564 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 565 ctx->include_name.data); |
566 return NGX_CONF_ERROR; | |
567 } | |
568 | |
569 if (ctx->high.low == NULL) { | |
570 ctx->high.low = ngx_pcalloc(ctx->pool, | |
571 0x10000 * sizeof(ngx_http_geo_range_t *)); | |
572 if (ctx->high.low == NULL) { | |
573 return NGX_CONF_ERROR; | |
574 } | |
575 } | |
576 | |
577 ctx->entries++; | |
578 ctx->outside_entries = 1; | |
579 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
580 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
581 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
583 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
584 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
585 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
588 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
589 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
590 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 p = ngx_strlchr(net->data, last, '-'); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
592 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
593 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
594 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
595 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
596 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
597 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
598 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
599 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
600 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
601 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
602 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
603 start = ntohl(start); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
604 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
605 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
606 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
607 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
608 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
609 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
610 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
611 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
612 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
613 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
614 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
615 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
616 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
619 if (del) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
620 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
|
621 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
622 "no address range \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
623 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
624 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
625 return NGX_CONF_OK; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
628 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
|
629 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
630 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
631 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
632 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
633 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
634 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
635 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
636 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
|
637 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
638 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
639 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
640 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
|
641 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
642 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
643 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
644 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
645 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
646 /* 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
|
647 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
648 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
649 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
|
650 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
651 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
652 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
653 ngx_uint_t h, i, s, e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
654 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
655 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
656 |
3456
91cff7f97a50
fix a geo range if the range includes two or more /16 networks
Igor Sysoev <igor@sysoev.ru>
parents:
3115
diff
changeset
|
657 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
|
658 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
659 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
|
660 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
661 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
|
662 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
|
663 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
664 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
665 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
666 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
667 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
668 e = end & 0xffff; |
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 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
671 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
672 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
673 |
3652 | 674 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
675 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
676 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 a = ngx_array_create(ctx->temp_pool, 64, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
679 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
680 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
681 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
682 |
3652 | 683 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
|
684 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
686 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
687 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
689 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
690 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 i--; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
692 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
693 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
695 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
696 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
697 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
699 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
700 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
701 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
702 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
703 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
704 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
705 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
706 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
708 ngx_memmove(&range[i + 2], &range[i + 1], |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 |
2442 | 711 range[i + 1].start = (u_short) s; |
712 range[i + 1].end = (u_short) e; | |
713 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
715 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
716 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
717 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
718 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
719 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
720 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
721 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
722 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
723 ctx->net, ctx->value, range[i].value); |
2442 | 724 |
725 range[i].value = ctx->value; | |
726 | |
727 goto next; | |
728 } | |
729 | |
730 if (s > (ngx_uint_t) range[i].start | |
731 && e < (ngx_uint_t) range[i].end) | |
732 { | |
733 /* split the range and insert the new one */ | |
734 | |
735 range = ngx_array_push(a); | |
736 if (range == NULL) { | |
737 return NGX_CONF_ERROR; | |
738 } | |
739 | |
740 range = ngx_array_push(a); | |
741 if (range == NULL) { | |
742 return NGX_CONF_ERROR; | |
743 } | |
744 | |
745 range = a->elts; | |
746 | |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
747 ngx_memmove(&range[i + 3], &range[i + 1], |
2442 | 748 (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t)); |
749 | |
750 range[i + 2].start = (u_short) (e + 1); | |
751 range[i + 2].end = range[i].end; | |
752 range[i + 2].value = range[i].value; | |
753 | |
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
|
754 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
|
755 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
|
756 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
|
757 |
2442 | 758 range[i].end = (u_short) (s - 1); |
759 | |
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
|
760 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
|
761 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
762 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
763 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
|
764 && 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
|
765 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
766 /* 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
|
767 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
768 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
|
769 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
|
770 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
|
771 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
772 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
773 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
|
774 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
775 ngx_memmove(&range[i + 1], &range[i], |
2457
1aff348d9aa8
fix segfault when geo range replaces starting part of another range
Igor Sysoev <igor@sysoev.ru>
parents:
2452
diff
changeset
|
776 (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
|
777 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
778 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
|
779 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
780 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
|
781 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
|
782 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
|
783 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
784 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
|
785 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
786 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
787 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
|
788 && 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
|
789 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
790 /* 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
|
791 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
792 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
|
793 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
|
794 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
|
795 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
796 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
797 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
|
798 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
799 ngx_memmove(&range[i + 2], &range[i + 1], |
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
|
800 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
801 |
2442 | 802 range[i + 1].start = (u_short) s; |
803 range[i + 1].end = (u_short) e; | |
804 range[i + 1].value = ctx->value; | |
805 | |
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
|
806 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
|
807 |
2442 | 808 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
809 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
810 |
2448 | 811 s = (ngx_uint_t) range[i].start; |
812 e = (ngx_uint_t) range[i].end; | |
813 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
814 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 815 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
816 ctx->net, | |
817 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
818 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
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 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
821 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
822 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
823 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
824 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
825 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
826 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
827 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
828 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
829 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
830 range->start = (u_short) s; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
831 range->end = (u_short) e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
832 range->value = ctx->value; |
2442 | 833 |
834 next: | |
835 | |
836 continue; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
837 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
838 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
839 return NGX_CONF_OK; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
842 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
843 static ngx_uint_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
844 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
|
845 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
846 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
847 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
848 ngx_uint_t h, i, s, e, warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
849 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
850 ngx_http_geo_range_t *range; |
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 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
853 |
2451 | 854 for (n = start; n <= end; n += 0x10000) { |
2340
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 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
857 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
858 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
859 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
860 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
861 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
862 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
863 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
864 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
865 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
866 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
867 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
868 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
869 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
870 |
3652 | 871 a = (ngx_array_t *) ctx->high.low[h]; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
872 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
873 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
874 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
875 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
876 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
877 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
878 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
879 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
880 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
881 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
882 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
883 { |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
884 ngx_memmove(&range[i], &range[i + 1], |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
885 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2450 | 886 |
887 a->nelts--; | |
888 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
889 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
890 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
891 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
892 if (s != (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
893 && e != (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
894 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
895 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
896 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
897 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
898 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
899 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
900 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
901 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
902 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
903 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
904 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
905 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
906 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 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
|
908 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
910 ngx_int_t rc, del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
911 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
912 ngx_uint_t i; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
913 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
914 ngx_http_variable_value_t *val, *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
915 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
916 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
917 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
918 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
919 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
920 } |
485 | 921 } |
922 | |
923 if (ngx_strcmp(value[0].data, "default") == 0) { | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
924 /* cidr.family = AF_INET; */ |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
925 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
926 cidr.u.in.mask = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
927 net = &value[0]; |
485 | 928 |
929 } else { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
930 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
931 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
932 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
933 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
934 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
935 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
936 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
937 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
938 |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
939 if (ngx_http_geo_cidr_value(cf, net, &cidr) != NGX_OK) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
940 return NGX_CONF_ERROR; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
941 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
942 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
943 if (cidr.family != AF_INET) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
944 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
945 "\"geo\" supports IPv4 only"); |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
946 return NGX_CONF_ERROR; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
947 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
948 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
949 cidr.u.in.addr = ntohl(cidr.u.in.addr); |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
950 cidr.u.in.mask = ntohl(cidr.u.in.mask); |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
951 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
952 if (del) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
953 if (ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
954 cidr.u.in.mask) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
955 != NGX_OK) |
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 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
958 "no network \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
959 } |
2365 | 960 |
961 return NGX_CONF_OK; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
962 } |
485 | 963 } |
964 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
965 val = ngx_http_geo_value(cf, ctx, &value[1]); |
485 | 966 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
967 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
968 return NGX_CONF_ERROR; |
485 | 969 } |
970 | |
553 | 971 for (i = 2; i; i--) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
972 rc = ngx_radix32tree_insert(ctx->tree, cidr.u.in.addr, cidr.u.in.mask, |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
973 (uintptr_t) val); |
553 | 974 if (rc == NGX_OK) { |
975 return NGX_CONF_OK; | |
976 } | |
977 | |
978 if (rc == NGX_ERROR) { | |
979 return NGX_CONF_ERROR; | |
980 } | |
981 | |
982 /* rc == NGX_BUSY */ | |
983 | |
3115 | 984 old = (ngx_http_variable_value_t *) |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
985 ngx_radix32tree_find(ctx->tree, cidr.u.in.addr & cidr.u.in.mask); |
553 | 986 |
987 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
988 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
989 net, val, old); |
553 | 990 |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
991 rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, cidr.u.in.mask); |
553 | 992 |
993 if (rc == NGX_ERROR) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
994 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
553 | 995 return NGX_CONF_ERROR; |
996 } | |
485 | 997 } |
998 | |
553 | 999 return NGX_CONF_ERROR; |
485 | 1000 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1001 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1002 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1003 static ngx_http_variable_value_t * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1004 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
|
1005 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1006 { |
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
|
1007 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
|
1008 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
|
1009 ngx_http_geo_variable_value_node_t *gvvn; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1010 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1011 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1012 |
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
|
1013 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
|
1014 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1015 |
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
|
1016 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
|
1017 return gvvn->value; |
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 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
|
1021 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1022 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1023 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1024 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1025 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1026 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1027 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1028 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1029 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1030 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1031 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1032 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1033 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1034 |
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
|
1035 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
|
1036 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
|
1037 if (gvvn == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1038 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1039 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1040 |
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
|
1041 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
|
1042 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
|
1043 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
|
1044 gvvn->value = val; |
3652 | 1045 gvvn->offset = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1046 |
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
|
1047 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1048 |
3652 | 1049 ctx->data_size += ngx_align(sizeof(ngx_http_variable_value_t) + value->len, |
1050 sizeof(void *)); | |
1051 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1052 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1053 } |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1054 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1055 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1056 static char * |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1057 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
|
1058 ngx_cidr_t *cidr) |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1059 { |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1060 ngx_cidr_t *c; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1061 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1062 if (ctx->proxies == NULL) { |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1063 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
|
1064 if (ctx->proxies == NULL) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1065 return NGX_CONF_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1066 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1067 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1068 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1069 c = ngx_array_push(ctx->proxies); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1070 if (c == NULL) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1071 return NGX_CONF_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1072 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1073 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1074 *c = *cidr; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1075 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1076 return NGX_CONF_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1077 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1078 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1079 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1080 static ngx_int_t |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1081 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
|
1082 { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1083 ngx_int_t rc; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1084 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1085 if (ngx_strcmp(net->data, "255.255.255.255") == 0) { |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1086 cidr->family = AF_INET; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1087 cidr->u.in.addr = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1088 cidr->u.in.mask = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1089 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1090 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1091 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1092 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1093 rc = ngx_ptocidr(net, cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1094 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1095 if (rc == NGX_ERROR) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1096 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
|
1097 return NGX_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1098 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1099 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1100 if (rc == NGX_DONE) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1101 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1102 "low address bits of %V are meaningless", net); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1103 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1104 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1105 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1106 } |
3652 | 1107 |
1108 | |
1109 static char * | |
1110 ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, | |
1111 ngx_str_t *name) | |
1112 { | |
1113 char *rv; | |
1114 ngx_str_t file; | |
1115 | |
1116 file.len = name->len + 4; | |
1117 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5); | |
1118 if (file.data == NULL) { | |
1119 return NGX_CONF_ERROR; | |
1120 } | |
1121 | |
1122 ngx_sprintf(file.data, "%V.bin%Z", name); | |
1123 | |
1124 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { | |
1125 return NGX_CONF_ERROR; | |
1126 } | |
1127 | |
3654
b56935e34273
test binary geo ranges base only for ranges
Igor Sysoev <igor@sysoev.ru>
parents:
3653
diff
changeset
|
1128 if (ctx->ranges) { |
3660 | 1129 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); |
3652 | 1130 |
3660 | 1131 switch (ngx_http_geo_include_binary_base(cf, ctx, &file)) { |
1132 case NGX_OK: | |
1133 return NGX_CONF_OK; | |
1134 case NGX_ERROR: | |
1135 return NGX_CONF_ERROR; | |
1136 default: | |
1137 break; | |
1138 } | |
3652 | 1139 } |
1140 | |
1141 file.len -= 4; | |
1142 file.data[file.len] = '\0'; | |
1143 | |
1144 ctx->include_name = file; | |
1145 | |
1146 if (ctx->outside_entries) { | |
1147 ctx->allow_binary_include = 0; | |
1148 } | |
1149 | |
1150 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
1151 | |
1152 rv = ngx_conf_parse(cf, &file); | |
1153 | |
1154 ctx->includes++; | |
1155 ctx->outside_entries = 0; | |
1156 | |
1157 return rv; | |
1158 } | |
1159 | |
1160 | |
1161 static ngx_int_t | |
1162 ngx_http_geo_include_binary_base(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, | |
1163 ngx_str_t *name) | |
1164 { | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1165 u_char *base, ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1166 time_t mtime; |
3652 | 1167 size_t size, len; |
1168 ssize_t n; | |
1169 uint32_t crc32; | |
1170 ngx_err_t err; | |
1171 ngx_int_t rc; | |
1172 ngx_uint_t i; | |
1173 ngx_file_t file; | |
1174 ngx_file_info_t fi; | |
1175 ngx_http_geo_range_t *range, **ranges; | |
1176 ngx_http_geo_header_t *header; | |
1177 ngx_http_variable_value_t *vv; | |
1178 | |
1179 ngx_memzero(&file, sizeof(ngx_file_t)); | |
1180 file.name = *name; | |
1181 file.log = cf->log; | |
1182 | |
1183 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0); | |
1184 if (file.fd == NGX_INVALID_FILE) { | |
1185 err = ngx_errno; | |
1186 if (err != NGX_ENOENT) { | |
1187 ngx_conf_log_error(NGX_LOG_CRIT, cf, err, | |
1188 ngx_open_file_n " \"%s\" failed", name->data); | |
1189 } | |
1190 return NGX_DECLINED; | |
1191 } | |
1192 | |
1193 if (ctx->outside_entries) { | |
1194 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
|
1195 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 1196 name->data); |
1197 rc = NGX_ERROR; | |
1198 goto done; | |
1199 } | |
1200 | |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1201 if (ctx->binary_include) { |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1202 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
|
1203 "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
|
1204 name->data, ctx->include_name.data); |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1205 rc = NGX_ERROR; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1206 goto done; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1207 } |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1208 |
3652 | 1209 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) { |
1210 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1211 ngx_fd_info_n " \"%s\" failed", name->data); | |
1212 goto failed; | |
1213 } | |
1214 | |
1215 size = (size_t) ngx_file_size(&fi); | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1216 mtime = ngx_file_mtime(&fi); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1217 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1218 ch = name->data[name->len - 4]; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1219 name->data[name->len - 4] = '\0'; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1220 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1221 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
|
1222 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
|
1223 ngx_file_info_n " \"%s\" failed", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1224 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1225 } |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1226 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1227 name->data[name->len - 4] = ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1228 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1229 if (mtime < ngx_file_mtime(&fi)) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1230 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
|
1231 "stale binary geo range base \"%s\"", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1232 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1233 } |
3652 | 1234 |
1235 base = ngx_palloc(ctx->pool, size); | |
1236 if (base == NULL) { | |
1237 goto failed; | |
1238 } | |
1239 | |
1240 n = ngx_read_file(&file, base, size, 0); | |
1241 | |
1242 if (n == NGX_ERROR) { | |
1243 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1244 ngx_read_file_n " \"%s\" failed", name->data); | |
1245 goto failed; | |
1246 } | |
1247 | |
1248 if ((size_t) n != size) { | |
1249 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0, | |
1250 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z", | |
1251 name->data, n, size); | |
1252 goto failed; | |
1253 } | |
1254 | |
1255 header = (ngx_http_geo_header_t *) base; | |
1256 | |
3656
3d61601f3171
test binary geo range base size
Igor Sysoev <igor@sysoev.ru>
parents:
3655
diff
changeset
|
1257 if (size < 16 || ngx_memcmp(&ngx_http_geo_header, header, 12) != 0) { |
3652 | 1258 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
1259 "incompatible binary geo range base \"%s\"", name->data); | |
1260 goto failed; | |
1261 } | |
1262 | |
1263 ngx_crc32_init(crc32); | |
1264 | |
1265 vv = (ngx_http_variable_value_t *) (base + sizeof(ngx_http_geo_header_t)); | |
1266 | |
1267 while(vv->data) { | |
1268 len = ngx_align(sizeof(ngx_http_variable_value_t) + vv->len, | |
1269 sizeof(void *)); | |
1270 ngx_crc32_update(&crc32, (u_char *) vv, len); | |
1271 vv->data += (size_t) base; | |
1272 vv = (ngx_http_variable_value_t *) ((u_char *) vv + len); | |
1273 } | |
1274 ngx_crc32_update(&crc32, (u_char *) vv, sizeof(ngx_http_variable_value_t)); | |
1275 vv++; | |
1276 | |
1277 ranges = (ngx_http_geo_range_t **) vv; | |
1278 | |
1279 for (i = 0; i < 0x10000; i++) { | |
1280 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *)); | |
1281 if (ranges[i]) { | |
1282 ranges[i] = (ngx_http_geo_range_t *) | |
1283 ((u_char *) ranges[i] + (size_t) base); | |
1284 } | |
1285 } | |
1286 | |
1287 range = (ngx_http_geo_range_t *) &ranges[0x10000]; | |
1288 | |
1289 while ((u_char *) range < base + size) { | |
1290 while (range->value) { | |
1291 ngx_crc32_update(&crc32, (u_char *) range, | |
1292 sizeof(ngx_http_geo_range_t)); | |
1293 range->value = (ngx_http_variable_value_t *) | |
1294 ((u_char *) range->value + (size_t) base); | |
1295 range++; | |
1296 } | |
1297 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *)); | |
1298 range = (ngx_http_geo_range_t *) ((u_char *) range + sizeof(void *)); | |
1299 } | |
1300 | |
1301 ngx_crc32_final(crc32); | |
1302 | |
1303 if (crc32 != header->crc32) { | |
1304 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1305 "CRC32 mismatch in binary geo range base \"%s\"", name->data); | |
1306 goto failed; | |
1307 } | |
1308 | |
1309 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, | |
1310 "using binary geo range base \"%s\"", name->data); | |
1311 | |
1312 ctx->include_name = *name; | |
1313 ctx->binary_include = 1; | |
1314 ctx->high.low = ranges; | |
1315 rc = NGX_OK; | |
1316 | |
1317 goto done; | |
1318 | |
1319 failed: | |
1320 | |
1321 rc = NGX_DECLINED; | |
1322 | |
1323 done: | |
1324 | |
1325 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
1326 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, | |
1327 ngx_close_file_n " \"%s\" failed", name->data); | |
1328 } | |
1329 | |
1330 return rc; | |
1331 } | |
1332 | |
1333 | |
1334 static void | |
1335 ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx) | |
1336 { | |
1337 u_char *p; | |
1338 uint32_t hash; | |
1339 ngx_str_t s; | |
1340 ngx_uint_t i; | |
1341 ngx_file_mapping_t fm; | |
1342 ngx_http_geo_range_t *r, *range, **ranges; | |
1343 ngx_http_geo_header_t *header; | |
1344 ngx_http_geo_variable_value_node_t *gvvn; | |
1345 | |
1346 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5); | |
1347 if (fm.name == NULL) { | |
1348 return; | |
1349 } | |
1350 | |
1351 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name); | |
1352 | |
1353 fm.size = ctx->data_size; | |
1354 fm.log = ctx->pool->log; | |
1355 | |
1356 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0, | |
1357 "creating binary geo range base \"%s\"", fm.name); | |
1358 | |
1359 if (ngx_create_file_mapping(&fm) != NGX_OK) { | |
1360 return; | |
1361 } | |
1362 | |
1363 p = ngx_cpymem(fm.addr, &ngx_http_geo_header, | |
1364 sizeof(ngx_http_geo_header_t)); | |
1365 | |
1366 p = ngx_http_geo_copy_values(fm.addr, p, ctx->rbtree.root, | |
1367 ctx->rbtree.sentinel); | |
1368 | |
1369 p += sizeof(ngx_http_variable_value_t); | |
1370 | |
1371 ranges = (ngx_http_geo_range_t **) p; | |
1372 | |
1373 p += 0x10000 * sizeof(ngx_http_geo_range_t *); | |
1374 | |
1375 for (i = 0; i < 0x10000; i++) { | |
1376 r = ctx->high.low[i]; | |
1377 if (r == NULL) { | |
1378 continue; | |
1379 } | |
1380 | |
1381 range = (ngx_http_geo_range_t *) p; | |
1382 ranges[i] = (ngx_http_geo_range_t *) (p - (u_char *) fm.addr); | |
1383 | |
1384 do { | |
1385 s.len = r->value->len; | |
1386 s.data = r->value->data; | |
1387 hash = ngx_crc32_long(s.data, s.len); | |
1388 gvvn = (ngx_http_geo_variable_value_node_t *) | |
1389 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash); | |
1390 | |
1391 range->value = (ngx_http_variable_value_t *) gvvn->offset; | |
1392 range->start = r->start; | |
1393 range->end = r->end; | |
1394 range++; | |
1395 | |
1396 } while ((++r)->value); | |
1397 | |
1398 range->value = NULL; | |
1399 | |
1400 p = (u_char *) range + sizeof(void *); | |
1401 } | |
1402 | |
1403 header = fm.addr; | |
1404 header->crc32 = ngx_crc32_long((u_char *) fm.addr | |
1405 + sizeof(ngx_http_geo_header_t), | |
1406 fm.size - sizeof(ngx_http_geo_header_t)); | |
1407 | |
1408 ngx_close_file_mapping(&fm); | |
1409 } | |
1410 | |
1411 | |
1412 static u_char * | |
1413 ngx_http_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node, | |
1414 ngx_rbtree_node_t *sentinel) | |
1415 { | |
1416 ngx_http_variable_value_t *vv; | |
1417 ngx_http_geo_variable_value_node_t *gvvn; | |
1418 | |
1419 if (node == sentinel) { | |
1420 return p; | |
1421 } | |
1422 | |
1423 gvvn = (ngx_http_geo_variable_value_node_t *) node; | |
1424 gvvn->offset = p - base; | |
1425 | |
1426 vv = (ngx_http_variable_value_t *) p; | |
1427 *vv = *gvvn->value; | |
1428 p += sizeof(ngx_http_variable_value_t); | |
1429 vv->data = (u_char *) (p - base); | |
1430 | |
1431 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len); | |
1432 | |
1433 p = ngx_align_ptr(p, sizeof(void *)); | |
1434 | |
1435 p = ngx_http_geo_copy_values(base, p, node->left, sentinel); | |
1436 | |
1437 return ngx_http_geo_copy_values(base, p, node->right, sentinel); | |
1438 } |