Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geo_module.c @ 4785:f8767b8032ba stable-1.2
Merge of r4713: map hostnames fix (ticket #182).
The final dot wasn't stripped before looking up in a map of hostnames.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 06 Aug 2012 14:04:06 +0000 |
parents | 47936d1c98e9 |
children | 2ccaaf838bc2 |
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) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
236 struct in6_addr *inaddr6; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
237 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
238 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; |
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 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
241 return ntohl(*(in_addr_t *) &inaddr6->s6_addr[12]); |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
242 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
243 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
244 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
245 #endif |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
246 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
247 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
|
248 return INADDR_NONE; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
249 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
250 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
251 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
|
252 return ntohl(sin->sin_addr.s_addr); |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
253 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
254 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
255 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
256 static ngx_int_t |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
257 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
|
258 ngx_addr_t *addr) |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
259 { |
2399 | 260 ngx_http_variable_value_t *v; |
261 | |
262 if (ctx->index == -1) { | |
263 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
264 "http geo started: %V", &r->connection->addr_text); | |
265 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
266 addr->sockaddr = r->connection->sockaddr; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
267 addr->socklen = r->connection->socklen; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
268 /* 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
|
269 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
270 return NGX_OK; |
2399 | 271 } |
272 | |
273 v = ngx_http_get_flushed_variable(r, ctx->index); | |
274 | |
275 if (v == NULL || v->not_found) { | |
276 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
277 "http geo not found"); | |
278 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
279 return NGX_ERROR; |
2399 | 280 } |
281 | |
282 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
283 "http geo started: %v", v); | |
284 | |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
285 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
|
286 return NGX_OK; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
287 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
288 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
289 return NGX_ERROR; |
2399 | 290 } |
291 | |
292 | |
489 | 293 static char * |
294 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 295 { |
589 | 296 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
|
297 void **p; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
298 size_t len; |
2399 | 299 ngx_str_t *value, name; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
300 ngx_uint_t i; |
589 | 301 ngx_conf_t save; |
302 ngx_pool_t *pool; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
303 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
|
304 ngx_http_variable_t *var; |
2399 | 305 ngx_http_geo_ctx_t *geo; |
589 | 306 ngx_http_geo_conf_ctx_t ctx; |
485 | 307 |
501 | 308 value = cf->args->elts; |
309 | |
2399 | 310 geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t)); |
311 if (geo == NULL) { | |
312 return NGX_CONF_ERROR; | |
313 } | |
314 | |
315 name = value[1]; | |
316 name.len--; | |
317 name.data++; | |
318 | |
319 if (cf->args->nelts == 3) { | |
320 | |
321 geo->index = ngx_http_get_variable_index(cf, &name); | |
322 if (geo->index == NGX_ERROR) { | |
323 return NGX_CONF_ERROR; | |
324 } | |
325 | |
326 name = value[2]; | |
327 name.len--; | |
328 name.data++; | |
329 | |
330 } else { | |
331 geo->index = -1; | |
332 } | |
333 | |
334 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
501 | 335 if (var == NULL) { |
485 | 336 return NGX_CONF_ERROR; |
337 } | |
338 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
339 pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
340 if (pool == NULL) { |
485 | 341 return NGX_CONF_ERROR; |
342 } | |
343 | |
3652 | 344 ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t)); |
345 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
346 ctx.temp_pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
347 if (ctx.temp_pool == NULL) { |
485 | 348 return NGX_CONF_ERROR; |
349 } | |
350 | |
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
|
351 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); |
485 | 352 |
589 | 353 ctx.pool = cf->pool; |
3652 | 354 ctx.data_size = sizeof(ngx_http_geo_header_t) |
355 + sizeof(ngx_http_variable_value_t) | |
356 + 0x10000 * sizeof(ngx_http_geo_range_t *); | |
357 ctx.allow_binary_include = 1; | |
485 | 358 |
359 save = *cf; | |
360 cf->pool = pool; | |
589 | 361 cf->ctx = &ctx; |
485 | 362 cf->handler = ngx_http_geo; |
363 cf->handler_conf = conf; | |
364 | |
365 rv = ngx_conf_parse(cf, NULL); | |
366 | |
367 *cf = save; | |
368 | |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
369 geo->proxies = ctx.proxies; |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
370 geo->proxy_recursive = ctx.proxy_recursive; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
371 |
3652 | 372 if (ctx.high.low) { |
373 | |
374 if (!ctx.binary_include) { | |
375 for (i = 0; i < 0x10000; i++) { | |
376 a = (ngx_array_t *) ctx.high.low[i]; | |
377 | |
378 if (a == NULL || a->nelts == 0) { | |
379 continue; | |
380 } | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
381 |
3652 | 382 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
|
383 |
3652 | 384 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); |
385 if (ctx.high.low[i] == NULL) { | |
386 return NGX_CONF_ERROR; | |
387 } | |
388 | |
389 p = (void **) ngx_cpymem(ctx.high.low[i], a->elts, len); | |
390 *p = NULL; | |
391 ctx.data_size += len + sizeof(void *); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
392 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
393 |
3652 | 394 if (ctx.allow_binary_include |
395 && !ctx.outside_entries | |
396 && ctx.entries > 100000 | |
397 && ctx.includes == 1) | |
398 { | |
399 ngx_http_geo_create_binary_base(&ctx); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
400 } |
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 |
2399 | 403 geo->u.high = ctx.high; |
404 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
405 var->get_handler = ngx_http_geo_range_variable; |
2399 | 406 var->data = (uintptr_t) geo; |
485 | 407 |
3652 | 408 if (ctx.high.default_value == NULL) { |
409 ctx.high.default_value = &ngx_http_variable_null_value; | |
410 } | |
411 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
412 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
413 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
414 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
415 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
416 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
417 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
|
418 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
419 return NGX_CONF_ERROR; |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
420 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
421 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
422 |
2399 | 423 geo->u.tree = ctx.tree; |
424 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
425 var->get_handler = ngx_http_geo_cidr_variable; |
2399 | 426 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
427 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
428 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
429 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
430 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
431 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
|
432 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
433 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
434 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
435 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
436 (uintptr_t) &ngx_http_variable_null_value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
437 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
438 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
439 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
440 } |
485 | 441 } |
442 | |
443 return rv; | |
444 } | |
445 | |
446 | |
489 | 447 static char * |
448 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
485 | 449 { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
450 char *rv; |
3652 | 451 ngx_str_t *value; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
452 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
453 ngx_http_geo_conf_ctx_t *ctx; |
485 | 454 |
589 | 455 ctx = cf->ctx; |
485 | 456 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
457 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
458 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
459 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
460 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
461 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
462 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
463 if (ctx->tree) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
464 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
465 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
466 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
467 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
468 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
469 |
3652 | 470 ctx->ranges = 1; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
471 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
472 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
473 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
474 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
475 } |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
476 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
477 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
|
478 ctx->proxy_recursive = 1; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
479 rv = NGX_CONF_OK; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
480 goto done; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
481 } |
2340
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 |
485 | 484 if (cf->args->nelts != 2) { |
485 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
486 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
487 goto failed; |
485 | 488 } |
489 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
490 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
491 |
3652 | 492 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
|
493 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
494 goto done; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
495 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
496 } else if (ngx_strcmp(value[0].data, "proxy") == 0) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
497 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
498 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
|
499 goto failed; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
500 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
501 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
502 rv = ngx_http_geo_add_proxy(cf, ctx, &cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
503 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
504 goto done; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
505 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
506 |
3652 | 507 if (ctx->ranges) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
508 rv = ngx_http_geo_range(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
509 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
510 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
511 rv = ngx_http_geo_cidr(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
512 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
513 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
514 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
515 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
516 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
517 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
518 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
519 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
520 failed: |
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 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
523 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
524 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
525 } |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
528 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
529 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
|
530 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
531 { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
532 u_char *p, *last; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
533 in_addr_t start, end; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
534 ngx_str_t *net; |
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
535 ngx_uint_t del; |
2340
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 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
538 |
3652 | 539 if (ctx->high.default_value) { |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
540 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
|
541 "duplicate default geo range value: \"%V\", old value: \"%v\"", |
3652 | 542 &value[1], ctx->high.default_value); |
3650
c12b0dd5bd1c
change duplicate default geo range processing
Igor Sysoev <igor@sysoev.ru>
parents:
3645
diff
changeset
|
543 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
544 |
3652 | 545 ctx->high.default_value = ngx_http_geo_value(cf, ctx, &value[1]); |
546 if (ctx->high.default_value == NULL) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
547 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
548 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
549 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
550 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
551 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
552 |
3652 | 553 if (ctx->binary_include) { |
554 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
|
555 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 556 ctx->include_name.data); |
557 return NGX_CONF_ERROR; | |
558 } | |
559 | |
560 if (ctx->high.low == NULL) { | |
561 ctx->high.low = ngx_pcalloc(ctx->pool, | |
562 0x10000 * sizeof(ngx_http_geo_range_t *)); | |
563 if (ctx->high.low == NULL) { | |
564 return NGX_CONF_ERROR; | |
565 } | |
566 } | |
567 | |
568 ctx->entries++; | |
569 ctx->outside_entries = 1; | |
570 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
571 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
572 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
573 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
574 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
575 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
576 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
577 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
579 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
580 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
581 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 p = ngx_strlchr(net->data, last, '-'); |
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 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
585 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
588 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
589 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
590 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
594 start = ntohl(start); |
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 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
597 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
598 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
599 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
600 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
601 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
604 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
605 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
606 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
607 goto invalid; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
610 if (del) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
611 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
|
612 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
613 "no address range \"%V\" to delete", net); |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
616 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
617 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
618 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
619 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
|
620 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
621 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
622 return NGX_CONF_ERROR; |
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 ctx->net = net; |
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 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
|
628 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
629 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
630 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
631 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
|
632 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
633 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
634 } |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
637 /* 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
|
638 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
639 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
640 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
|
641 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
642 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
643 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
644 ngx_uint_t h, i, s, e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
645 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
646 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
647 |
3456
91cff7f97a50
fix a geo range if the range includes two or more /16 networks
Igor Sysoev <igor@sysoev.ru>
parents:
3115
diff
changeset
|
648 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
|
649 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
650 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
|
651 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
652 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
|
653 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
|
654 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
655 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
656 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
657 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
658 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
659 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
660 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
661 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
662 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
663 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
664 |
3652 | 665 a = (ngx_array_t *) ctx->high.low[h]; |
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 (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
668 a = ngx_array_create(ctx->temp_pool, 64, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
669 sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
670 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
671 return NGX_CONF_ERROR; |
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 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
|
675 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
676 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
679 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
680 while (i) { |
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 i--; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
683 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
684 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
685 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
686 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
687 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
689 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
690 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
692 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
693 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 return NGX_CONF_ERROR; |
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 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
699 ngx_memmove(&range[i + 2], &range[i + 1], |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
700 (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
|
701 |
2442 | 702 range[i + 1].start = (u_short) s; |
703 range[i + 1].end = (u_short) e; | |
704 range[i + 1].value = ctx->value; | |
2340
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 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
708 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
711 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
712 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
713 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 ctx->net, ctx->value, range[i].value); |
2442 | 715 |
716 range[i].value = ctx->value; | |
717 | |
718 goto next; | |
719 } | |
720 | |
721 if (s > (ngx_uint_t) range[i].start | |
722 && e < (ngx_uint_t) range[i].end) | |
723 { | |
724 /* split the range and insert the new one */ | |
725 | |
726 range = ngx_array_push(a); | |
727 if (range == NULL) { | |
728 return NGX_CONF_ERROR; | |
729 } | |
730 | |
731 range = ngx_array_push(a); | |
732 if (range == NULL) { | |
733 return NGX_CONF_ERROR; | |
734 } | |
735 | |
736 range = a->elts; | |
737 | |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
738 ngx_memmove(&range[i + 3], &range[i + 1], |
2442 | 739 (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t)); |
740 | |
741 range[i + 2].start = (u_short) (e + 1); | |
742 range[i + 2].end = range[i].end; | |
743 range[i + 2].value = range[i].value; | |
744 | |
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
|
745 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
|
746 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
|
747 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
|
748 |
2442 | 749 range[i].end = (u_short) (s - 1); |
750 | |
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
|
751 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
|
752 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
753 |
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 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
|
755 && 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
|
756 { |
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 /* 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
|
758 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
759 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
|
760 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
|
761 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
|
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 |
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 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
|
765 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
766 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
|
767 (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
|
768 |
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 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
|
770 |
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 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
|
772 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
|
773 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
|
774 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
775 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
|
776 } |
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 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
|
779 && 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
|
780 { |
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 /* 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
|
782 |
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 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
|
784 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
|
785 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
|
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 |
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 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
|
789 |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
790 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
|
791 (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
|
792 |
2442 | 793 range[i + 1].start = (u_short) s; |
794 range[i + 1].end = (u_short) e; | |
795 range[i + 1].value = ctx->value; | |
796 | |
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
|
797 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
|
798 |
2442 | 799 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
800 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
801 |
2448 | 802 s = (ngx_uint_t) range[i].start; |
803 e = (ngx_uint_t) range[i].end; | |
804 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
805 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 806 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
807 ctx->net, | |
808 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
809 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
810 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
811 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
812 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
813 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
814 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
815 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
816 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
817 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
818 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
819 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
820 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
821 range->start = (u_short) s; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
822 range->end = (u_short) e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
823 range->value = ctx->value; |
2442 | 824 |
825 next: | |
826 | |
827 continue; | |
2340
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 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
831 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
832 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
833 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
834 static ngx_uint_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
835 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
|
836 in_addr_t start, in_addr_t end) |
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 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
839 ngx_uint_t h, i, s, e, warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
840 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
841 ngx_http_geo_range_t *range; |
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 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
844 |
2451 | 845 for (n = start; n <= end; n += 0x10000) { |
2340
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 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
848 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
849 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
850 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
851 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
852 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
853 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
854 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
855 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
856 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
857 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
858 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
859 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
860 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
861 |
3652 | 862 a = (ngx_array_t *) ctx->high.low[h]; |
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 (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
865 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
866 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
867 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
868 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
869 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
870 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
871 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
872 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
873 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
874 { |
3887
e7798b5e990a
use memmove() in appropriate places
Igor Sysoev <igor@sysoev.ru>
parents:
3660
diff
changeset
|
875 ngx_memmove(&range[i], &range[i + 1], |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
876 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2450 | 877 |
878 a->nelts--; | |
879 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
880 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
881 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
882 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
883 if (s != (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
884 && e != (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
885 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
886 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
887 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
888 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
889 warn = 1; |
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 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
893 return warn; |
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 |
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 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
898 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
|
899 ngx_str_t *value) |
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 ngx_int_t rc, del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
902 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
903 ngx_uint_t i; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
904 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
905 ngx_http_variable_value_t *val, *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
906 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
908 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
910 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
911 } |
485 | 912 } |
913 | |
914 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
|
915 /* cidr.family = AF_INET; */ |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
916 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
917 cidr.u.in.mask = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
918 net = &value[0]; |
485 | 919 |
920 } else { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
921 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
922 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
923 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
924 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
925 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
926 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
927 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
928 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
929 |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
930 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
|
931 return NGX_CONF_ERROR; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
932 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
933 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
934 if (cidr.family != AF_INET) { |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
935 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
|
936 "\"geo\" supports IPv4 only"); |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
937 return NGX_CONF_ERROR; |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
938 } |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
939 |
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
940 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
|
941 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
|
942 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
943 if (del) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
944 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
|
945 cidr.u.in.mask) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
946 != NGX_OK) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
947 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
948 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
949 "no network \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
950 } |
2365 | 951 |
952 return NGX_CONF_OK; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
953 } |
485 | 954 } |
955 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
956 val = ngx_http_geo_value(cf, ctx, &value[1]); |
485 | 957 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
958 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
959 return NGX_CONF_ERROR; |
485 | 960 } |
961 | |
553 | 962 for (i = 2; i; i--) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
963 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
|
964 (uintptr_t) val); |
553 | 965 if (rc == NGX_OK) { |
966 return NGX_CONF_OK; | |
967 } | |
968 | |
969 if (rc == NGX_ERROR) { | |
970 return NGX_CONF_ERROR; | |
971 } | |
972 | |
973 /* rc == NGX_BUSY */ | |
974 | |
3115 | 975 old = (ngx_http_variable_value_t *) |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
976 ngx_radix32tree_find(ctx->tree, cidr.u.in.addr & cidr.u.in.mask); |
553 | 977 |
978 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
|
979 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
980 net, val, old); |
553 | 981 |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
982 rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, cidr.u.in.mask); |
553 | 983 |
984 if (rc == NGX_ERROR) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
985 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
553 | 986 return NGX_CONF_ERROR; |
987 } | |
485 | 988 } |
989 | |
553 | 990 return NGX_CONF_ERROR; |
485 | 991 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
992 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
993 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
994 static ngx_http_variable_value_t * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
995 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
|
996 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
997 { |
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
|
998 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
|
999 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
|
1000 ngx_http_geo_variable_value_node_t *gvvn; |
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 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1003 |
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
|
1004 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
|
1005 ngx_str_rbtree_lookup(&ctx->rbtree, value, hash); |
2340
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 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
|
1008 return gvvn->value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1009 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1010 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1011 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
|
1012 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1013 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1014 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1015 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1016 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1017 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1018 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1019 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1020 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1021 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1022 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1023 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1024 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1025 |
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
|
1026 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
|
1027 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
|
1028 if (gvvn == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1029 return NULL; |
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 |
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
|
1032 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
|
1033 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
|
1034 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
|
1035 gvvn->value = val; |
3652 | 1036 gvvn->offset = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1037 |
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
|
1038 ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1039 |
3652 | 1040 ctx->data_size += ngx_align(sizeof(ngx_http_variable_value_t) + value->len, |
1041 sizeof(void *)); | |
1042 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1043 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
1044 } |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1045 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1046 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1047 static char * |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1048 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
|
1049 ngx_cidr_t *cidr) |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1050 { |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1051 ngx_cidr_t *c; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1052 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1053 if (ctx->proxies == NULL) { |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1054 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
|
1055 if (ctx->proxies == NULL) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1056 return NGX_CONF_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1057 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1058 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1059 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1060 c = ngx_array_push(ctx->proxies); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1061 if (c == NULL) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1062 return NGX_CONF_ERROR; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1063 } |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1064 |
4668
ba2c7463ce18
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4593
diff
changeset
|
1065 *c = *cidr; |
2992
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 return NGX_CONF_OK; |
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 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1070 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1071 static ngx_int_t |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1072 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
|
1073 { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1074 ngx_int_t rc; |
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 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
|
1077 cidr->family = AF_INET; |
2992
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1078 cidr->u.in.addr = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1079 cidr->u.in.mask = 0xffffffff; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1080 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1081 return NGX_OK; |
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 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1084 rc = ngx_ptocidr(net, cidr); |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1085 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1086 if (rc == NGX_ERROR) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1087 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
|
1088 return NGX_ERROR; |
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 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1091 if (rc == NGX_DONE) { |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1092 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1093 "low address bits of %V are meaningless", net); |
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 |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1096 return NGX_OK; |
2465ff3da161
geo module supports trusted proxies
Igor Sysoev <igor@sysoev.ru>
parents:
2537
diff
changeset
|
1097 } |
3652 | 1098 |
1099 | |
1100 static char * | |
1101 ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, | |
1102 ngx_str_t *name) | |
1103 { | |
1104 char *rv; | |
1105 ngx_str_t file; | |
1106 | |
1107 file.len = name->len + 4; | |
1108 file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5); | |
1109 if (file.data == NULL) { | |
1110 return NGX_CONF_ERROR; | |
1111 } | |
1112 | |
1113 ngx_sprintf(file.data, "%V.bin%Z", name); | |
1114 | |
1115 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { | |
1116 return NGX_CONF_ERROR; | |
1117 } | |
1118 | |
3654
b56935e34273
test binary geo ranges base only for ranges
Igor Sysoev <igor@sysoev.ru>
parents:
3653
diff
changeset
|
1119 if (ctx->ranges) { |
3660 | 1120 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); |
3652 | 1121 |
3660 | 1122 switch (ngx_http_geo_include_binary_base(cf, ctx, &file)) { |
1123 case NGX_OK: | |
1124 return NGX_CONF_OK; | |
1125 case NGX_ERROR: | |
1126 return NGX_CONF_ERROR; | |
1127 default: | |
1128 break; | |
1129 } | |
3652 | 1130 } |
1131 | |
1132 file.len -= 4; | |
1133 file.data[file.len] = '\0'; | |
1134 | |
1135 ctx->include_name = file; | |
1136 | |
1137 if (ctx->outside_entries) { | |
1138 ctx->allow_binary_include = 0; | |
1139 } | |
1140 | |
1141 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
1142 | |
1143 rv = ngx_conf_parse(cf, &file); | |
1144 | |
1145 ctx->includes++; | |
1146 ctx->outside_entries = 0; | |
1147 | |
1148 return rv; | |
1149 } | |
1150 | |
1151 | |
1152 static ngx_int_t | |
1153 ngx_http_geo_include_binary_base(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, | |
1154 ngx_str_t *name) | |
1155 { | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1156 u_char *base, ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1157 time_t mtime; |
3652 | 1158 size_t size, len; |
1159 ssize_t n; | |
1160 uint32_t crc32; | |
1161 ngx_err_t err; | |
1162 ngx_int_t rc; | |
1163 ngx_uint_t i; | |
1164 ngx_file_t file; | |
1165 ngx_file_info_t fi; | |
1166 ngx_http_geo_range_t *range, **ranges; | |
1167 ngx_http_geo_header_t *header; | |
1168 ngx_http_variable_value_t *vv; | |
1169 | |
1170 ngx_memzero(&file, sizeof(ngx_file_t)); | |
1171 file.name = *name; | |
1172 file.log = cf->log; | |
1173 | |
1174 file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0); | |
1175 if (file.fd == NGX_INVALID_FILE) { | |
1176 err = ngx_errno; | |
1177 if (err != NGX_ENOENT) { | |
1178 ngx_conf_log_error(NGX_LOG_CRIT, cf, err, | |
1179 ngx_open_file_n " \"%s\" failed", name->data); | |
1180 } | |
1181 return NGX_DECLINED; | |
1182 } | |
1183 | |
1184 if (ctx->outside_entries) { | |
1185 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
|
1186 "binary geo range base \"%s\" cannot be mixed with usual entries", |
3652 | 1187 name->data); |
1188 rc = NGX_ERROR; | |
1189 goto done; | |
1190 } | |
1191 | |
3655
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1192 if (ctx->binary_include) { |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1193 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
|
1194 "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
|
1195 name->data, ctx->include_name.data); |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1196 rc = NGX_ERROR; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1197 goto done; |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1198 } |
ec20f1f93aea
test the second binary geo range base existence
Igor Sysoev <igor@sysoev.ru>
parents:
3654
diff
changeset
|
1199 |
3652 | 1200 if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) { |
1201 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1202 ngx_fd_info_n " \"%s\" failed", name->data); | |
1203 goto failed; | |
1204 } | |
1205 | |
1206 size = (size_t) ngx_file_size(&fi); | |
3653
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1207 mtime = ngx_file_mtime(&fi); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1208 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1209 ch = name->data[name->len - 4]; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1210 name->data[name->len - 4] = '\0'; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1211 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1212 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
|
1213 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
|
1214 ngx_file_info_n " \"%s\" failed", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1215 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1216 } |
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 name->data[name->len - 4] = ch; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1219 |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1220 if (mtime < ngx_file_mtime(&fi)) { |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1221 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
|
1222 "stale binary geo range base \"%s\"", name->data); |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1223 goto failed; |
be3f716ba546
test binary gep range base mtime
Igor Sysoev <igor@sysoev.ru>
parents:
3652
diff
changeset
|
1224 } |
3652 | 1225 |
1226 base = ngx_palloc(ctx->pool, size); | |
1227 if (base == NULL) { | |
1228 goto failed; | |
1229 } | |
1230 | |
1231 n = ngx_read_file(&file, base, size, 0); | |
1232 | |
1233 if (n == NGX_ERROR) { | |
1234 ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno, | |
1235 ngx_read_file_n " \"%s\" failed", name->data); | |
1236 goto failed; | |
1237 } | |
1238 | |
1239 if ((size_t) n != size) { | |
1240 ngx_conf_log_error(NGX_LOG_CRIT, cf, 0, | |
1241 ngx_read_file_n " \"%s\" returned only %z bytes instead of %z", | |
1242 name->data, n, size); | |
1243 goto failed; | |
1244 } | |
1245 | |
1246 header = (ngx_http_geo_header_t *) base; | |
1247 | |
3656
3d61601f3171
test binary geo range base size
Igor Sysoev <igor@sysoev.ru>
parents:
3655
diff
changeset
|
1248 if (size < 16 || ngx_memcmp(&ngx_http_geo_header, header, 12) != 0) { |
3652 | 1249 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
1250 "incompatible binary geo range base \"%s\"", name->data); | |
1251 goto failed; | |
1252 } | |
1253 | |
1254 ngx_crc32_init(crc32); | |
1255 | |
1256 vv = (ngx_http_variable_value_t *) (base + sizeof(ngx_http_geo_header_t)); | |
1257 | |
1258 while(vv->data) { | |
1259 len = ngx_align(sizeof(ngx_http_variable_value_t) + vv->len, | |
1260 sizeof(void *)); | |
1261 ngx_crc32_update(&crc32, (u_char *) vv, len); | |
1262 vv->data += (size_t) base; | |
1263 vv = (ngx_http_variable_value_t *) ((u_char *) vv + len); | |
1264 } | |
1265 ngx_crc32_update(&crc32, (u_char *) vv, sizeof(ngx_http_variable_value_t)); | |
1266 vv++; | |
1267 | |
1268 ranges = (ngx_http_geo_range_t **) vv; | |
1269 | |
1270 for (i = 0; i < 0x10000; i++) { | |
1271 ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *)); | |
1272 if (ranges[i]) { | |
1273 ranges[i] = (ngx_http_geo_range_t *) | |
1274 ((u_char *) ranges[i] + (size_t) base); | |
1275 } | |
1276 } | |
1277 | |
1278 range = (ngx_http_geo_range_t *) &ranges[0x10000]; | |
1279 | |
1280 while ((u_char *) range < base + size) { | |
1281 while (range->value) { | |
1282 ngx_crc32_update(&crc32, (u_char *) range, | |
1283 sizeof(ngx_http_geo_range_t)); | |
1284 range->value = (ngx_http_variable_value_t *) | |
1285 ((u_char *) range->value + (size_t) base); | |
1286 range++; | |
1287 } | |
1288 ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *)); | |
1289 range = (ngx_http_geo_range_t *) ((u_char *) range + sizeof(void *)); | |
1290 } | |
1291 | |
1292 ngx_crc32_final(crc32); | |
1293 | |
1294 if (crc32 != header->crc32) { | |
1295 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1296 "CRC32 mismatch in binary geo range base \"%s\"", name->data); | |
1297 goto failed; | |
1298 } | |
1299 | |
1300 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, | |
1301 "using binary geo range base \"%s\"", name->data); | |
1302 | |
1303 ctx->include_name = *name; | |
1304 ctx->binary_include = 1; | |
1305 ctx->high.low = ranges; | |
1306 rc = NGX_OK; | |
1307 | |
1308 goto done; | |
1309 | |
1310 failed: | |
1311 | |
1312 rc = NGX_DECLINED; | |
1313 | |
1314 done: | |
1315 | |
1316 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
1317 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, | |
1318 ngx_close_file_n " \"%s\" failed", name->data); | |
1319 } | |
1320 | |
1321 return rc; | |
1322 } | |
1323 | |
1324 | |
1325 static void | |
1326 ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx) | |
1327 { | |
1328 u_char *p; | |
1329 uint32_t hash; | |
1330 ngx_str_t s; | |
1331 ngx_uint_t i; | |
1332 ngx_file_mapping_t fm; | |
1333 ngx_http_geo_range_t *r, *range, **ranges; | |
1334 ngx_http_geo_header_t *header; | |
1335 ngx_http_geo_variable_value_node_t *gvvn; | |
1336 | |
1337 fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5); | |
1338 if (fm.name == NULL) { | |
1339 return; | |
1340 } | |
1341 | |
1342 ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name); | |
1343 | |
1344 fm.size = ctx->data_size; | |
1345 fm.log = ctx->pool->log; | |
1346 | |
1347 ngx_log_error(NGX_LOG_NOTICE, fm.log, 0, | |
1348 "creating binary geo range base \"%s\"", fm.name); | |
1349 | |
1350 if (ngx_create_file_mapping(&fm) != NGX_OK) { | |
1351 return; | |
1352 } | |
1353 | |
1354 p = ngx_cpymem(fm.addr, &ngx_http_geo_header, | |
1355 sizeof(ngx_http_geo_header_t)); | |
1356 | |
1357 p = ngx_http_geo_copy_values(fm.addr, p, ctx->rbtree.root, | |
1358 ctx->rbtree.sentinel); | |
1359 | |
1360 p += sizeof(ngx_http_variable_value_t); | |
1361 | |
1362 ranges = (ngx_http_geo_range_t **) p; | |
1363 | |
1364 p += 0x10000 * sizeof(ngx_http_geo_range_t *); | |
1365 | |
1366 for (i = 0; i < 0x10000; i++) { | |
1367 r = ctx->high.low[i]; | |
1368 if (r == NULL) { | |
1369 continue; | |
1370 } | |
1371 | |
1372 range = (ngx_http_geo_range_t *) p; | |
1373 ranges[i] = (ngx_http_geo_range_t *) (p - (u_char *) fm.addr); | |
1374 | |
1375 do { | |
1376 s.len = r->value->len; | |
1377 s.data = r->value->data; | |
1378 hash = ngx_crc32_long(s.data, s.len); | |
1379 gvvn = (ngx_http_geo_variable_value_node_t *) | |
1380 ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash); | |
1381 | |
1382 range->value = (ngx_http_variable_value_t *) gvvn->offset; | |
1383 range->start = r->start; | |
1384 range->end = r->end; | |
1385 range++; | |
1386 | |
1387 } while ((++r)->value); | |
1388 | |
1389 range->value = NULL; | |
1390 | |
1391 p = (u_char *) range + sizeof(void *); | |
1392 } | |
1393 | |
1394 header = fm.addr; | |
1395 header->crc32 = ngx_crc32_long((u_char *) fm.addr | |
1396 + sizeof(ngx_http_geo_header_t), | |
1397 fm.size - sizeof(ngx_http_geo_header_t)); | |
1398 | |
1399 ngx_close_file_mapping(&fm); | |
1400 } | |
1401 | |
1402 | |
1403 static u_char * | |
1404 ngx_http_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node, | |
1405 ngx_rbtree_node_t *sentinel) | |
1406 { | |
1407 ngx_http_variable_value_t *vv; | |
1408 ngx_http_geo_variable_value_node_t *gvvn; | |
1409 | |
1410 if (node == sentinel) { | |
1411 return p; | |
1412 } | |
1413 | |
1414 gvvn = (ngx_http_geo_variable_value_node_t *) node; | |
1415 gvvn->offset = p - base; | |
1416 | |
1417 vv = (ngx_http_variable_value_t *) p; | |
1418 *vv = *gvvn->value; | |
1419 p += sizeof(ngx_http_variable_value_t); | |
1420 vv->data = (u_char *) (p - base); | |
1421 | |
1422 p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len); | |
1423 | |
1424 p = ngx_align_ptr(p, sizeof(void *)); | |
1425 | |
1426 p = ngx_http_geo_copy_values(base, p, node->left, sentinel); | |
1427 | |
1428 return ngx_http_geo_copy_values(base, p, node->right, sentinel); | |
1429 } |