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