Mercurial > hg > nginx-quic
annotate src/core/ngx_resolver.c @ 4264:051129d893cc
Ancient incomplete ngx_http_status_module removal.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 01 Nov 2011 15:16:28 +0000 |
parents | 016352c19049 |
children | 768212ca0745 |
rev | line source |
---|---|
583 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 | |
11 | |
1649 | 12 #define NGX_RESOLVER_UDP_SIZE 4096 |
13 | |
14 | |
583 | 15 typedef struct { |
1649 | 16 u_char ident_hi; |
17 u_char ident_lo; | |
18 u_char flags_hi; | |
19 u_char flags_lo; | |
20 u_char nqs_hi; | |
21 u_char nqs_lo; | |
22 u_char nan_hi; | |
23 u_char nan_lo; | |
24 u_char nns_hi; | |
25 u_char nns_lo; | |
26 u_char nar_hi; | |
27 u_char nar_lo; | |
28 } ngx_resolver_query_t; | |
29 | |
30 | |
31 typedef struct { | |
32 u_char type_hi; | |
33 u_char type_lo; | |
34 u_char class_hi; | |
35 u_char class_lo; | |
36 } ngx_resolver_qs_t; | |
37 | |
38 | |
39 typedef struct { | |
40 u_char type_hi; | |
41 u_char type_lo; | |
42 u_char class_hi; | |
43 u_char class_lo; | |
44 u_char ttl[4]; | |
45 u_char len_hi; | |
46 u_char len_lo; | |
47 } ngx_resolver_an_t; | |
48 | |
49 | |
50 ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc); | |
51 | |
52 | |
1906 | 53 static void ngx_resolver_cleanup(void *data); |
54 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree); | |
1649 | 55 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, |
56 ngx_resolver_ctx_t *ctx); | |
57 static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, | |
58 ngx_queue_t *queue); | |
59 static ngx_int_t ngx_resolver_send_query(ngx_resolver_t *r, | |
60 ngx_resolver_node_t *rn); | |
61 static ngx_int_t ngx_resolver_create_name_query(ngx_resolver_node_t *rn, | |
62 ngx_resolver_ctx_t *ctx); | |
63 static ngx_int_t ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, | |
64 ngx_resolver_ctx_t *ctx); | |
65 static void ngx_resolver_resend_handler(ngx_event_t *ev); | |
66 static time_t ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, | |
67 ngx_queue_t *queue); | |
68 static void ngx_resolver_read_response(ngx_event_t *rev); | |
69 static void ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, | |
70 size_t n); | |
71 static void ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n, | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
72 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans); |
1649 | 73 static void ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n, |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
74 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan); |
1649 | 75 static ngx_resolver_node_t *ngx_resolver_lookup_name(ngx_resolver_t *r, |
76 ngx_str_t *name, uint32_t hash); | |
77 static ngx_resolver_node_t *ngx_resolver_lookup_addr(ngx_resolver_t *r, | |
78 in_addr_t addr); | |
79 static void ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
80 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); | |
81 static ngx_int_t ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, | |
82 u_char *buf, u_char *src, u_char *last); | |
83 static void ngx_resolver_timeout_handler(ngx_event_t *ev); | |
84 static void ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn); | |
85 static void *ngx_resolver_alloc(ngx_resolver_t *r, size_t size); | |
1903 | 86 static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size); |
1649 | 87 static void ngx_resolver_free(ngx_resolver_t *r, void *p); |
88 static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p); | |
89 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
90 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len); |
1649 | 91 |
92 | |
93 ngx_resolver_t * | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
94 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) |
1649 | 95 { |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
96 ngx_url_t u; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
97 ngx_uint_t i; |
1649 | 98 ngx_resolver_t *r; |
1906 | 99 ngx_pool_cleanup_t *cln; |
1649 | 100 ngx_udp_connection_t *uc; |
101 | |
1913
c0f873458e2b
use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents:
1906
diff
changeset
|
102 cln = ngx_pool_cleanup_add(cf->pool, 0); |
1906 | 103 if (cln == NULL) { |
104 return NULL; | |
105 } | |
106 | |
107 cln->handler = ngx_resolver_cleanup; | |
108 | |
1913
c0f873458e2b
use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents:
1906
diff
changeset
|
109 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); |
1649 | 110 if (r == NULL) { |
111 return NULL; | |
112 } | |
113 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
114 if (n) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
115 if (ngx_array_init(&r->udp_connections, cf->pool, n, |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
116 sizeof(ngx_udp_connection_t)) |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
117 != NGX_OK) |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
118 { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
119 return NULL; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
120 } |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
121 } |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
122 |
1906 | 123 cln->data = r; |
124 | |
1913
c0f873458e2b
use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents:
1906
diff
changeset
|
125 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); |
1649 | 126 if (r->event == NULL) { |
127 return NULL; | |
128 } | |
129 | |
1687 | 130 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel, |
131 ngx_resolver_rbtree_insert_value); | |
132 | |
133 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel, | |
134 ngx_rbtree_insert_value); | |
1649 | 135 |
1685 | 136 ngx_queue_init(&r->name_resend_queue); |
137 ngx_queue_init(&r->addr_resend_queue); | |
138 | |
139 ngx_queue_init(&r->name_expire_queue); | |
140 ngx_queue_init(&r->addr_expire_queue); | |
1649 | 141 |
142 r->event->handler = ngx_resolver_resend_handler; | |
143 r->event->data = r; | |
2785
d478379e51ac
*) refactor error_log processing: listen socket log might inherit built-in
Igor Sysoev <igor@sysoev.ru>
parents:
2490
diff
changeset
|
144 r->event->log = &cf->cycle->new_log; |
1649 | 145 r->ident = -1; |
146 | |
147 r->resend_timeout = 5; | |
148 r->expire = 30; | |
149 r->valid = 300; | |
150 | |
2785
d478379e51ac
*) refactor error_log processing: listen socket log might inherit built-in
Igor Sysoev <igor@sysoev.ru>
parents:
2490
diff
changeset
|
151 r->log = &cf->cycle->new_log; |
3763
beca53d6ab3c
decrease resolver errors level to error
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
152 r->log_level = NGX_LOG_ERR; |
1649 | 153 |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
154 for (i = 0; i < n; i++) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
155 ngx_memzero(&u, sizeof(ngx_url_t)); |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
156 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
157 u.host = names[i]; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
158 u.port = 53; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
159 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
160 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
161 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err); |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
162 return NULL; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
163 } |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
164 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
165 uc = ngx_array_push(&r->udp_connections); |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
166 if (uc == NULL) { |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
167 return NULL; |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
168 } |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
169 |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
170 ngx_memzero(uc, sizeof(ngx_udp_connection_t)); |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
171 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
172 uc->sockaddr = u.addrs->sockaddr; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
173 uc->socklen = u.addrs->socklen; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
174 uc->server = u.addrs->name; |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
175 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
176 uc->log = cf->cycle->new_log; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
177 uc->log.handler = ngx_resolver_log_error; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
178 uc->log.data = uc; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
179 uc->log.action = "resolving"; |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
180 } |
1649 | 181 |
182 return r; | |
183 } | |
184 | |
185 | |
1906 | 186 static void |
187 ngx_resolver_cleanup(void *data) | |
188 { | |
189 ngx_resolver_t *r = data; | |
190 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
191 ngx_uint_t i; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
192 ngx_udp_connection_t *uc; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
193 |
1906 | 194 if (r) { |
195 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
196 "cleanup resolver"); | |
197 | |
198 ngx_resolver_cleanup_tree(r, &r->name_rbtree); | |
199 | |
200 ngx_resolver_cleanup_tree(r, &r->addr_rbtree); | |
201 | |
202 if (r->event) { | |
203 ngx_free(r->event); | |
204 } | |
205 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
206 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
207 uc = r->udp_connections.elts; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
208 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
209 for (i = 0; i < r->udp_connections.nelts; i++) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
210 if (uc[i].connection) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
211 ngx_close_connection(uc[i].connection); |
1906 | 212 } |
213 } | |
214 | |
215 ngx_free(r); | |
216 } | |
217 } | |
218 | |
219 | |
220 static void | |
221 ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree) | |
222 { | |
223 ngx_resolver_ctx_t *ctx, *next; | |
224 ngx_resolver_node_t *rn; | |
225 | |
226 while (tree->root != tree->sentinel) { | |
227 | |
228 rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel); | |
229 | |
230 ngx_queue_remove(&rn->queue); | |
231 | |
232 for (ctx = rn->waiting; ctx; ctx = next) { | |
2006
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1969
diff
changeset
|
233 next = ctx->next; |
1906 | 234 |
235 if (ctx->event) { | |
236 ngx_resolver_free(r, ctx->event); | |
237 } | |
238 | |
239 ngx_resolver_free(r, ctx); | |
240 } | |
241 | |
242 ngx_rbtree_delete(tree, &rn->node); | |
243 | |
244 ngx_resolver_free_node(r, rn); | |
245 } | |
246 } | |
247 | |
248 | |
1649 | 249 ngx_resolver_ctx_t * |
250 ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp) | |
251 { | |
252 in_addr_t addr; | |
253 ngx_resolver_ctx_t *ctx; | |
254 | |
255 if (temp) { | |
256 addr = ngx_inet_addr(temp->name.data, temp->name.len); | |
257 | |
258 if (addr != INADDR_NONE) { | |
259 temp->resolver = r; | |
260 temp->state = NGX_OK; | |
261 temp->naddrs = 1; | |
262 temp->addrs = &temp->addr; | |
263 temp->addr = addr; | |
264 temp->quick = 1; | |
265 | |
266 return temp; | |
267 } | |
268 } | |
269 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
270 if (r->udp_connections.nelts == 0) { |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
271 return NGX_NO_RESOLVER; |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
272 } |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
273 |
1649 | 274 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); |
275 | |
276 if (ctx) { | |
277 ctx->resolver = r; | |
278 } | |
279 | |
280 return ctx; | |
281 } | |
282 | |
283 | |
284 ngx_int_t | |
285 ngx_resolve_name(ngx_resolver_ctx_t *ctx) | |
286 { | |
287 ngx_int_t rc; | |
288 ngx_resolver_t *r; | |
289 | |
290 r = ctx->resolver; | |
291 | |
292 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
293 "resolve: \"%V\"", &ctx->name); | |
294 | |
295 if (ctx->quick) { | |
296 ctx->handler(ctx); | |
297 return NGX_OK; | |
298 } | |
299 | |
300 /* lock name mutex */ | |
301 | |
302 rc = ngx_resolve_name_locked(r, ctx); | |
303 | |
304 if (rc == NGX_OK) { | |
305 return NGX_OK; | |
306 } | |
307 | |
308 /* unlock name mutex */ | |
309 | |
310 if (rc == NGX_AGAIN) { | |
311 return NGX_OK; | |
312 } | |
313 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
314 /* NGX_ERROR */ |
1649 | 315 |
316 if (ctx->event) { | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
317 ngx_resolver_free(r, ctx->event); |
1649 | 318 } |
319 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
320 ngx_resolver_free(r, ctx); |
1649 | 321 |
322 return NGX_ERROR; | |
323 } | |
324 | |
325 | |
326 void | |
327 ngx_resolve_name_done(ngx_resolver_ctx_t *ctx) | |
328 { | |
329 uint32_t hash; | |
330 ngx_resolver_t *r; | |
331 ngx_resolver_ctx_t *w, **p; | |
332 ngx_resolver_node_t *rn; | |
333 | |
334 r = ctx->resolver; | |
335 | |
336 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
337 "resolve name done: %i", ctx->state); | |
338 | |
339 if (ctx->quick) { | |
340 return; | |
341 } | |
342 | |
343 if (ctx->event && ctx->event->timer_set) { | |
344 ngx_del_timer(ctx->event); | |
345 } | |
346 | |
347 /* lock name mutex */ | |
348 | |
349 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { | |
350 | |
351 hash = ngx_crc32_short(ctx->name.data, ctx->name.len); | |
352 | |
353 rn = ngx_resolver_lookup_name(r, &ctx->name, hash); | |
354 | |
355 if (rn) { | |
356 p = &rn->waiting; | |
357 w = rn->waiting; | |
358 | |
359 while (w) { | |
360 if (w == ctx) { | |
361 *p = w->next; | |
362 | |
363 goto done; | |
364 } | |
365 | |
366 p = &w->next; | |
367 w = w->next; | |
368 } | |
369 } | |
370 | |
371 ngx_log_error(NGX_LOG_ALERT, r->log, 0, | |
372 "could not cancel %V resolving", &ctx->name); | |
373 } | |
374 | |
375 done: | |
376 | |
377 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue); | |
378 | |
379 /* unlock name mutex */ | |
380 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
381 /* lock alloc mutex */ |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
382 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
383 if (ctx->event) { |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
384 ngx_resolver_free_locked(r, ctx->event); |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
385 } |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
386 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
387 ngx_resolver_free_locked(r, ctx); |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
388 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
389 /* unlock alloc mutex */ |
1649 | 390 } |
391 | |
392 | |
393 /* NGX_RESOLVE_A only */ | |
394 | |
395 static ngx_int_t | |
396 ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx) | |
397 { | |
398 uint32_t hash; | |
399 in_addr_t addr, *addrs; | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
400 ngx_int_t rc; |
1649 | 401 ngx_uint_t naddrs; |
402 ngx_resolver_ctx_t *next; | |
403 ngx_resolver_node_t *rn; | |
404 | |
405 hash = ngx_crc32_short(ctx->name.data, ctx->name.len); | |
406 | |
407 rn = ngx_resolver_lookup_name(r, &ctx->name, hash); | |
408 | |
409 if (rn) { | |
410 | |
411 if (rn->valid >= ngx_time()) { | |
412 | |
413 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached"); | |
414 | |
415 ngx_queue_remove(&rn->queue); | |
416 | |
417 rn->expire = ngx_time() + r->expire; | |
418 | |
419 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
420 | |
421 naddrs = rn->naddrs; | |
422 | |
423 if (naddrs) { | |
424 | |
425 /* NGX_RESOLVE_A answer */ | |
426 | |
427 if (naddrs != 1) { | |
428 addr = 0; | |
429 addrs = ngx_resolver_dup(r, rn->u.addrs, | |
430 naddrs * sizeof(in_addr_t)); | |
431 if (addrs == NULL) { | |
432 return NGX_ERROR; | |
433 } | |
434 | |
435 } else { | |
436 addr = rn->u.addr; | |
437 addrs = NULL; | |
438 } | |
439 | |
440 ctx->next = rn->waiting; | |
441 rn->waiting = NULL; | |
442 | |
443 /* unlock name mutex */ | |
444 | |
445 do { | |
446 ctx->state = NGX_OK; | |
447 ctx->naddrs = naddrs; | |
448 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs; | |
449 ctx->addr = addr; | |
450 next = ctx->next; | |
451 | |
452 ctx->handler(ctx); | |
453 | |
454 ctx = next; | |
455 } while (ctx); | |
456 | |
457 if (addrs) { | |
458 ngx_resolver_free(r, addrs); | |
459 } | |
460 | |
461 return NGX_OK; | |
462 } | |
463 | |
464 /* NGX_RESOLVE_CNAME */ | |
465 | |
1969 | 466 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) { |
467 | |
468 ctx->name.len = rn->cnlen; | |
469 ctx->name.data = rn->u.cname; | |
470 | |
471 return ngx_resolve_name_locked(r, ctx); | |
472 } | |
473 | |
474 ctx->next = rn->waiting; | |
475 rn->waiting = NULL; | |
476 | |
477 /* unlock name mutex */ | |
478 | |
479 do { | |
480 ctx->state = NGX_RESOLVE_NXDOMAIN; | |
481 next = ctx->next; | |
482 | |
483 ctx->handler(ctx); | |
484 | |
485 ctx = next; | |
486 } while (ctx); | |
487 | |
488 return NGX_OK; | |
1649 | 489 } |
490 | |
491 if (rn->waiting) { | |
492 | |
493 ctx->next = rn->waiting; | |
494 rn->waiting = ctx; | |
3297 | 495 ctx->state = NGX_AGAIN; |
1649 | 496 |
497 return NGX_AGAIN; | |
498 } | |
499 | |
500 ngx_queue_remove(&rn->queue); | |
501 | |
502 /* lock alloc mutex */ | |
503 | |
504 ngx_resolver_free_locked(r, rn->query); | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
505 rn->query = NULL; |
1649 | 506 |
507 if (rn->cnlen) { | |
508 ngx_resolver_free_locked(r, rn->u.cname); | |
509 } | |
510 | |
511 if (rn->naddrs > 1) { | |
512 ngx_resolver_free_locked(r, rn->u.addrs); | |
513 } | |
514 | |
515 /* unlock alloc mutex */ | |
516 | |
517 } else { | |
518 | |
519 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t)); | |
520 if (rn == NULL) { | |
521 return NGX_ERROR; | |
522 } | |
523 | |
524 rn->name = ngx_resolver_dup(r, ctx->name.data, ctx->name.len); | |
525 if (rn->name == NULL) { | |
526 ngx_resolver_free(r, rn); | |
527 return NGX_ERROR; | |
528 } | |
529 | |
530 rn->node.key = hash; | |
531 rn->nlen = (u_short) ctx->name.len; | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
532 rn->query = NULL; |
1649 | 533 |
534 ngx_rbtree_insert(&r->name_rbtree, &rn->node); | |
535 } | |
536 | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
537 rc = ngx_resolver_create_name_query(rn, ctx); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
538 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
539 if (rc == NGX_ERROR) { |
1649 | 540 goto failed; |
541 } | |
542 | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
543 if (rc == NGX_DECLINED) { |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
544 ngx_rbtree_delete(&r->name_rbtree, &rn->node); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
545 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
546 ngx_resolver_free(r, rn->query); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
547 ngx_resolver_free(r, rn->name); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
548 ngx_resolver_free(r, rn); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
549 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
550 ctx->state = NGX_RESOLVE_NXDOMAIN; |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
551 ctx->handler(ctx); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
552 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
553 return NGX_OK; |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
554 } |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
555 |
1649 | 556 if (ngx_resolver_send_query(r, rn) != NGX_OK) { |
557 goto failed; | |
558 } | |
559 | |
560 if (ctx->event == NULL) { | |
561 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); | |
562 if (ctx->event == NULL) { | |
563 goto failed; | |
564 } | |
565 | |
566 ctx->event->handler = ngx_resolver_timeout_handler; | |
567 ctx->event->data = ctx; | |
568 ctx->event->log = r->log; | |
569 ctx->ident = -1; | |
570 | |
571 ngx_add_timer(ctx->event, ctx->timeout); | |
572 } | |
573 | |
574 if (ngx_queue_empty(&r->name_resend_queue)) { | |
575 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000)); | |
576 } | |
577 | |
578 rn->expire = ngx_time() + r->resend_timeout; | |
579 | |
580 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue); | |
581 | |
582 rn->cnlen = 0; | |
583 rn->naddrs = 0; | |
584 rn->valid = 0; | |
585 rn->waiting = ctx; | |
586 | |
587 ctx->state = NGX_AGAIN; | |
588 | |
589 return NGX_AGAIN; | |
590 | |
591 failed: | |
592 | |
593 ngx_rbtree_delete(&r->name_rbtree, &rn->node); | |
594 | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
595 if (rn->query) { |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
596 ngx_resolver_free(r, rn->query); |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
597 } |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
598 |
1649 | 599 ngx_resolver_free(r, rn->name); |
600 | |
601 ngx_resolver_free(r, rn); | |
602 | |
603 return NGX_ERROR; | |
604 } | |
583 | 605 |
606 | |
607 ngx_int_t | |
1649 | 608 ngx_resolve_addr(ngx_resolver_ctx_t *ctx) |
609 { | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
610 u_char *name; |
1649 | 611 ngx_resolver_t *r; |
612 ngx_resolver_node_t *rn; | |
613 | |
614 r = ctx->resolver; | |
615 | |
616 ctx->addr = ntohl(ctx->addr); | |
617 | |
618 /* lock addr mutex */ | |
619 | |
620 rn = ngx_resolver_lookup_addr(r, ctx->addr); | |
621 | |
622 if (rn) { | |
623 | |
624 if (rn->valid >= ngx_time()) { | |
625 | |
626 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached"); | |
627 | |
628 ngx_queue_remove(&rn->queue); | |
629 | |
630 rn->expire = ngx_time() + r->expire; | |
631 | |
632 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue); | |
633 | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
634 name = ngx_resolver_dup(r, rn->name, rn->nlen); |
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
635 if (name == NULL) { |
1649 | 636 goto failed; |
637 } | |
638 | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
639 ctx->name.len = rn->nlen; |
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
640 ctx->name.data = name; |
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
641 |
1649 | 642 /* unlock addr mutex */ |
643 | |
644 ctx->state = NGX_OK; | |
645 | |
646 ctx->handler(ctx); | |
647 | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
648 ngx_resolver_free(r, name); |
1649 | 649 |
650 return NGX_OK; | |
651 } | |
652 | |
653 if (rn->waiting) { | |
654 | |
655 ctx->next = rn->waiting; | |
656 rn->waiting = ctx; | |
3297 | 657 ctx->state = NGX_AGAIN; |
1649 | 658 |
2487
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
659 /* unlock addr mutex */ |
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
660 |
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
661 return NGX_OK; |
1649 | 662 } |
663 | |
664 ngx_queue_remove(&rn->queue); | |
665 | |
666 ngx_resolver_free(r, rn->query); | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
667 rn->query = NULL; |
1649 | 668 |
669 } else { | |
670 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t)); | |
671 if (rn == NULL) { | |
672 goto failed; | |
673 } | |
674 | |
675 rn->node.key = ctx->addr; | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
676 rn->query = NULL; |
1649 | 677 |
678 ngx_rbtree_insert(&r->addr_rbtree, &rn->node); | |
679 } | |
680 | |
681 if (ngx_resolver_create_addr_query(rn, ctx) != NGX_OK) { | |
682 goto failed; | |
683 } | |
684 | |
685 if (ngx_resolver_send_query(r, rn) != NGX_OK) { | |
686 goto failed; | |
687 } | |
688 | |
689 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); | |
690 if (ctx->event == NULL) { | |
691 goto failed; | |
692 } | |
693 | |
694 ctx->event->handler = ngx_resolver_timeout_handler; | |
695 ctx->event->data = ctx; | |
696 ctx->event->log = r->log; | |
697 ctx->ident = -1; | |
698 | |
699 ngx_add_timer(ctx->event, ctx->timeout); | |
700 | |
701 if (ngx_queue_empty(&r->addr_resend_queue)) { | |
702 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000)); | |
703 } | |
704 | |
705 rn->expire = ngx_time() + r->resend_timeout; | |
706 | |
707 ngx_queue_insert_head(&r->addr_resend_queue, &rn->queue); | |
708 | |
709 rn->cnlen = 0; | |
710 rn->naddrs = 0; | |
711 rn->name = NULL; | |
712 rn->nlen = 0; | |
713 rn->valid = 0; | |
714 rn->waiting = ctx; | |
715 | |
716 /* unlock addr mutex */ | |
717 | |
718 ctx->state = NGX_AGAIN; | |
719 | |
720 return NGX_OK; | |
721 | |
722 failed: | |
723 | |
724 if (rn) { | |
725 ngx_rbtree_delete(&r->addr_rbtree, &rn->node); | |
726 | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
727 if (rn->query) { |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
728 ngx_resolver_free(r, rn->query); |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
729 } |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
730 |
1649 | 731 ngx_resolver_free(r, rn); |
732 } | |
733 | |
734 /* unlock addr mutex */ | |
735 | |
736 if (ctx->event) { | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
737 ngx_resolver_free(r, ctx->event); |
1649 | 738 } |
739 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
740 ngx_resolver_free(r, ctx); |
1649 | 741 |
742 return NGX_ERROR; | |
743 } | |
744 | |
745 | |
746 void | |
747 ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx) | |
748 { | |
749 in_addr_t addr; | |
750 ngx_resolver_t *r; | |
751 ngx_resolver_ctx_t *w, **p; | |
752 ngx_resolver_node_t *rn; | |
753 | |
754 r = ctx->resolver; | |
755 | |
756 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
757 "resolve addr done: %i", ctx->state); | |
758 | |
759 if (ctx->event && ctx->event->timer_set) { | |
760 ngx_del_timer(ctx->event); | |
761 } | |
762 | |
763 /* lock addr mutex */ | |
764 | |
765 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { | |
766 | |
767 rn = ngx_resolver_lookup_addr(r, ctx->addr); | |
768 | |
769 if (rn) { | |
770 p = &rn->waiting; | |
771 w = rn->waiting; | |
772 | |
773 while (w) { | |
774 if (w == ctx) { | |
775 *p = w->next; | |
776 | |
777 goto done; | |
778 } | |
779 | |
780 p = &w->next; | |
781 w = w->next; | |
782 } | |
783 } | |
784 | |
785 addr = ntohl(ctx->addr); | |
786 | |
787 ngx_log_error(NGX_LOG_ALERT, r->log, 0, | |
788 "could not cancel %ud.%ud.%ud.%ud resolving", | |
789 (addr >> 24) & 0xff, (addr >> 16) & 0xff, | |
790 (addr >> 8) & 0xff, addr & 0xff); | |
791 } | |
792 | |
793 done: | |
794 | |
795 ngx_resolver_expire(r, &r->addr_rbtree, &r->addr_expire_queue); | |
796 | |
797 /* unlock addr mutex */ | |
798 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
799 /* lock alloc mutex */ |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
800 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
801 if (ctx->event) { |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
802 ngx_resolver_free_locked(r, ctx->event); |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
803 } |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
804 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
805 ngx_resolver_free_locked(r, ctx); |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
806 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
807 /* unlock alloc mutex */ |
1649 | 808 } |
809 | |
810 | |
811 static void | |
812 ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue) | |
813 { | |
814 time_t now; | |
815 ngx_uint_t i; | |
816 ngx_queue_t *q; | |
817 ngx_resolver_node_t *rn; | |
818 | |
819 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire"); | |
820 | |
821 now = ngx_time(); | |
822 | |
823 for (i = 0; i < 2; i++) { | |
824 if (ngx_queue_empty(queue)) { | |
825 return; | |
826 } | |
827 | |
828 q = ngx_queue_last(queue); | |
829 | |
830 rn = ngx_queue_data(q, ngx_resolver_node_t, queue); | |
831 | |
832 if (now <= rn->expire) { | |
833 return; | |
834 } | |
835 | |
1774 | 836 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, |
837 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name); | |
1649 | 838 |
839 ngx_queue_remove(q); | |
840 | |
841 ngx_rbtree_delete(tree, &rn->node); | |
842 | |
843 ngx_resolver_free_node(r, rn); | |
844 } | |
845 } | |
846 | |
847 | |
848 static ngx_int_t | |
849 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) | |
850 { | |
851 ssize_t n; | |
852 ngx_udp_connection_t *uc; | |
853 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
854 uc = r->udp_connections.elts; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
855 |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
856 uc = &uc[r->last_connection++]; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
857 if (r->last_connection == r->udp_connections.nelts) { |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
858 r->last_connection = 0; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
859 } |
1649 | 860 |
861 if (uc->connection == NULL) { | |
862 if (ngx_udp_connect(uc) != NGX_OK) { | |
863 return NGX_ERROR; | |
864 } | |
865 | |
866 uc->connection->data = r; | |
867 uc->connection->read->handler = ngx_resolver_read_response; | |
1906 | 868 uc->connection->read->resolver = 1; |
1649 | 869 } |
870 | |
871 n = ngx_send(uc->connection, rn->query, rn->qlen); | |
872 | |
873 if (n == -1) { | |
874 return NGX_ERROR; | |
875 } | |
876 | |
877 if ((size_t) n != (size_t) rn->qlen) { | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
878 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete"); |
1649 | 879 return NGX_ERROR; |
880 } | |
881 | |
882 return NGX_OK; | |
883 } | |
884 | |
885 | |
886 static void | |
887 ngx_resolver_resend_handler(ngx_event_t *ev) | |
888 { | |
889 time_t timer, atimer, ntimer; | |
890 ngx_resolver_t *r; | |
891 | |
892 r = ev->data; | |
893 | |
894 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, | |
895 "resolver resend handler"); | |
896 | |
897 /* lock name mutex */ | |
898 | |
899 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue); | |
900 | |
901 /* unlock name mutex */ | |
902 | |
903 /* lock addr mutex */ | |
904 | |
905 atimer = ngx_resolver_resend(r, &r->addr_rbtree, &r->addr_resend_queue); | |
1679
ca317d9b5c09
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1649
diff
changeset
|
906 |
1649 | 907 /* unlock addr mutex */ |
908 | |
909 if (ntimer == 0) { | |
910 timer = atimer; | |
911 | |
912 } else if (atimer == 0) { | |
913 timer = ntimer; | |
914 | |
915 } else { | |
916 timer = (atimer < ntimer) ? atimer : ntimer; | |
917 } | |
918 | |
919 if (timer) { | |
920 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000)); | |
921 } | |
922 } | |
923 | |
924 | |
925 static time_t | |
926 ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue) | |
927 { | |
928 time_t now; | |
929 ngx_queue_t *q; | |
930 ngx_resolver_node_t *rn; | |
931 | |
932 now = ngx_time(); | |
933 | |
934 for ( ;; ) { | |
935 if (ngx_queue_empty(queue)) { | |
936 return 0; | |
937 } | |
938 | |
939 q = ngx_queue_last(queue); | |
940 | |
941 rn = ngx_queue_data(q, ngx_resolver_node_t, queue); | |
942 | |
943 if (now < rn->expire) { | |
944 return rn->expire - now; | |
945 } | |
946 | |
1774 | 947 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, |
948 "resolver resend \"%*s\" %p", | |
949 (size_t) rn->nlen, rn->name, rn->waiting); | |
1649 | 950 |
951 ngx_queue_remove(q); | |
952 | |
953 if (rn->waiting) { | |
954 | |
955 if (ngx_resolver_send_query(r, rn) == NGX_OK) { | |
956 | |
957 rn->expire = now + r->resend_timeout; | |
958 | |
959 ngx_queue_insert_head(queue, &rn->queue); | |
960 } | |
1879
cf4ee321d195
do not delete failed DNS request if there are waiting clients
Igor Sysoev <igor@sysoev.ru>
parents:
1878
diff
changeset
|
961 |
cf4ee321d195
do not delete failed DNS request if there are waiting clients
Igor Sysoev <igor@sysoev.ru>
parents:
1878
diff
changeset
|
962 continue; |
1649 | 963 } |
964 | |
965 ngx_rbtree_delete(tree, &rn->node); | |
966 | |
967 ngx_resolver_free_node(r, rn); | |
968 } | |
969 } | |
970 | |
971 | |
972 static void | |
973 ngx_resolver_read_response(ngx_event_t *rev) | |
974 { | |
975 ssize_t n; | |
976 ngx_connection_t *c; | |
977 u_char buf[NGX_RESOLVER_UDP_SIZE]; | |
978 | |
979 c = rev->data; | |
980 | |
981 do { | |
1689 | 982 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE); |
983 | |
984 if (n < 0) { | |
1649 | 985 return; |
986 } | |
987 | |
988 ngx_resolver_process_response(c->data, buf, n); | |
989 | |
990 } while (rev->ready); | |
991 } | |
992 | |
993 | |
994 static void | |
995 ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n) | |
996 { | |
997 char *err; | |
998 size_t len; | |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
999 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan, |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1000 qtype, qclass; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1001 ngx_queue_t *q; |
1649 | 1002 ngx_resolver_qs_t *qs; |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1003 ngx_resolver_node_t *rn; |
1649 | 1004 ngx_resolver_query_t *query; |
1005 | |
2281
5004229420a6
FORMERR DNS response may be equal to mininal query size
Igor Sysoev <igor@sysoev.ru>
parents:
2006
diff
changeset
|
1006 if ((size_t) n < sizeof(ngx_resolver_query_t)) { |
1649 | 1007 goto short_response; |
1008 } | |
1009 | |
1010 query = (ngx_resolver_query_t *) buf; | |
1011 | |
1012 ident = (query->ident_hi << 8) + query->ident_lo; | |
1013 flags = (query->flags_hi << 8) + query->flags_lo; | |
1014 nqs = (query->nqs_hi << 8) + query->nqs_lo; | |
1015 nan = (query->nan_hi << 8) + query->nan_lo; | |
1016 | |
1017 ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1018 "resolver DNS response %ui fl:%04Xui %ui/%ui/%ui/%ui", |
1649 | 1019 ident, flags, nqs, nan, |
1020 (query->nns_hi << 8) + query->nns_lo, | |
1021 (query->nar_hi << 8) + query->nar_lo); | |
1022 | |
1023 if (!(flags & 0x8000)) { | |
1024 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1025 "invalid DNS response %ui fl:%04Xui", ident, flags); |
1649 | 1026 return; |
1027 } | |
1028 | |
1029 code = flags & 0x7f; | |
1030 | |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1031 if (code == NGX_RESOLVE_FORMERR) { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1032 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1033 times = 0; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1034 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1035 for (q = ngx_queue_head(&r->name_resend_queue); |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1036 q != ngx_queue_sentinel(&r->name_resend_queue) || times++ < 100; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1037 q = ngx_queue_next(q)) |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1038 { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1039 rn = ngx_queue_data(q, ngx_resolver_node_t, queue); |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1040 qident = (rn->query[0] << 8) + rn->query[1]; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1041 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1042 if (qident == ident) { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1043 ngx_log_error(r->log_level, r->log, 0, |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1044 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"", |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1045 code, ngx_resolver_strerror(code), ident, |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1046 rn->nlen, rn->name); |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1047 return; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1048 } |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1049 } |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1050 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1051 goto dns_error; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1052 } |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1053 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1054 if (code > NGX_RESOLVE_REFUSED) { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1055 goto dns_error; |
1649 | 1056 } |
1057 | |
1058 if (nqs != 1) { | |
1059 err = "invalid number of questions in DNS response"; | |
1060 goto done; | |
1061 } | |
1062 | |
1063 i = sizeof(ngx_resolver_query_t); | |
1064 | |
1065 while (i < (ngx_uint_t) n) { | |
1066 if (buf[i] == '\0') { | |
1067 goto found; | |
1068 } | |
1069 | |
1070 len = buf[i]; | |
1071 i += 1 + len; | |
1072 } | |
1073 | |
1074 goto short_response; | |
1075 | |
1076 found: | |
1077 | |
1078 if (i++ == 0) { | |
1079 err = "zero-length domain name in DNS response"; | |
1080 goto done; | |
1081 } | |
1082 | |
1083 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t)) | |
1084 > (ngx_uint_t) n) | |
1085 { | |
1086 goto short_response; | |
1087 } | |
1088 | |
1089 qs = (ngx_resolver_qs_t *) &buf[i]; | |
1090 | |
1091 qtype = (qs->type_hi << 8) + qs->type_lo; | |
1092 qclass = (qs->class_hi << 8) + qs->class_lo; | |
1093 | |
1094 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1095 "resolver DNS response qt:%ui cl:%ui", qtype, qclass); |
1649 | 1096 |
1097 if (qclass != 1) { | |
1098 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1099 "unknown query class %ui in DNS response", qclass); |
1649 | 1100 return; |
1101 } | |
1102 | |
1103 switch (qtype) { | |
1104 | |
1105 case NGX_RESOLVE_A: | |
1106 | |
1107 ngx_resolver_process_a(r, buf, n, ident, code, nan, | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1108 i + sizeof(ngx_resolver_qs_t)); |
1649 | 1109 |
1110 break; | |
1111 | |
1112 case NGX_RESOLVE_PTR: | |
1113 | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1114 ngx_resolver_process_ptr(r, buf, n, ident, code, nan); |
1649 | 1115 |
1116 break; | |
1117 | |
1118 default: | |
1119 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1120 "unknown query type %ui in DNS response", qtype); |
1649 | 1121 return; |
1122 } | |
1123 | |
1124 return; | |
1125 | |
1126 short_response: | |
1127 | |
1128 err = "short dns response"; | |
1129 | |
1130 done: | |
1131 | |
1132 ngx_log_error(r->log_level, r->log, 0, err); | |
1133 | |
1134 return; | |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1135 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1136 dns_error: |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1137 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1138 ngx_log_error(r->log_level, r->log, 0, |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1139 "DNS error (%ui: %s), query id:%ui", |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1140 code, ngx_resolver_strerror(code), ident); |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1141 return; |
1649 | 1142 } |
1143 | |
1144 | |
1145 static void | |
1146 ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last, | |
1147 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans) | |
583 | 1148 { |
1649 | 1149 char *err; |
1150 u_char *cname; | |
1151 size_t len; | |
1152 uint32_t hash; | |
1153 in_addr_t addr, *addrs; | |
1154 ngx_str_t name; | |
1155 ngx_uint_t qtype, qident, naddrs, a, i, n, start; | |
1156 ngx_resolver_an_t *an; | |
1157 ngx_resolver_ctx_t *ctx, *next; | |
1158 ngx_resolver_node_t *rn; | |
1159 | |
1160 if (ngx_resolver_copy(r, &name, buf, &buf[12], &buf[last]) != NGX_OK) { | |
1161 return; | |
1162 } | |
1163 | |
1164 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name); | |
1165 | |
1166 hash = ngx_crc32_short(name.data, name.len); | |
1167 | |
1168 /* lock name mutex */ | |
1169 | |
1170 rn = ngx_resolver_lookup_name(r, &name, hash); | |
1171 | |
1172 if (rn == NULL || rn->query == NULL) { | |
1173 ngx_log_error(r->log_level, r->log, 0, | |
1174 "unexpected response for %V", &name); | |
1175 goto failed; | |
1176 } | |
1177 | |
1178 qident = (rn->query[0] << 8) + rn->query[1]; | |
1179 | |
1180 if (ident != qident) { | |
1181 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1182 "wrong ident %ui response for %V, expect %ui", |
1649 | 1183 ident, &name, qident); |
1184 goto failed; | |
1185 } | |
1186 | |
3139 | 1187 ngx_resolver_free(r, name.data); |
1188 | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1189 if (code == 0 && nan == 0) { |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1190 code = 3; /* NXDOMAIN */ |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1191 } |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1192 |
1649 | 1193 if (code) { |
1194 next = rn->waiting; | |
1195 rn->waiting = NULL; | |
1196 | |
1197 ngx_queue_remove(&rn->queue); | |
1198 | |
1199 ngx_rbtree_delete(&r->name_rbtree, &rn->node); | |
1200 | |
1201 ngx_resolver_free_node(r, rn); | |
1202 | |
1203 /* unlock name mutex */ | |
1204 | |
1205 while (next) { | |
1206 ctx = next; | |
1207 ctx->state = code; | |
1208 next = ctx->next; | |
1209 | |
1210 ctx->handler(ctx); | |
1211 } | |
1212 | |
1213 return; | |
1214 } | |
1215 | |
1216 i = ans; | |
1217 naddrs = 0; | |
1218 addr = 0; | |
1219 addrs = NULL; | |
1220 cname = NULL; | |
1221 qtype = 0; | |
1222 | |
1223 for (a = 0; a < nan; a++) { | |
1224 | |
1225 start = i; | |
1226 | |
1227 while (i < last) { | |
1228 | |
1229 if (buf[i] & 0xc0) { | |
1230 i += 2; | |
1231 goto found; | |
1232 } | |
1233 | |
1234 if (buf[i] == 0) { | |
1235 i++; | |
1236 goto test_length; | |
1237 } | |
1238 | |
1239 i += 1 + buf[i]; | |
1240 } | |
1241 | |
1242 goto short_response; | |
1243 | |
1244 test_length: | |
1245 | |
1246 if (i - start < 2) { | |
1247 err = "invalid name in dns response"; | |
1248 goto invalid; | |
1249 } | |
1250 | |
1251 found: | |
1252 | |
1253 if (i + sizeof(ngx_resolver_an_t) >= last) { | |
1254 goto short_response; | |
1255 } | |
1256 | |
1257 an = (ngx_resolver_an_t *) &buf[i]; | |
1258 | |
1259 qtype = (an->type_hi << 8) + an->type_lo; | |
1260 len = (an->len_hi << 8) + an->len_lo; | |
1261 | |
1262 if (qtype == NGX_RESOLVE_A) { | |
1263 | |
1264 i += sizeof(ngx_resolver_an_t); | |
1265 | |
1266 if (i + len > last) { | |
1267 goto short_response; | |
1268 } | |
1269 | |
1270 addr = htonl((buf[i] << 24) + (buf[i + 1] << 16) | |
1271 + (buf[i + 2] << 8) + (buf[i + 3])); | |
1272 | |
1273 naddrs++; | |
1274 | |
1275 i += len; | |
1276 | |
1277 } else if (qtype == NGX_RESOLVE_CNAME) { | |
1278 cname = &buf[i] + sizeof(ngx_resolver_an_t); | |
1279 i += sizeof(ngx_resolver_an_t) + len; | |
1965 | 1280 |
1281 } else if (qtype == NGX_RESOLVE_DNAME) { | |
1282 i += sizeof(ngx_resolver_an_t) + len; | |
1966 | 1283 |
1284 } else { | |
1285 ngx_log_error(r->log_level, r->log, 0, | |
1286 "unexpected qtype %ui", qtype); | |
1649 | 1287 } |
1288 } | |
1289 | |
1290 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, | |
1291 "resolver naddrs:%ui cname:%p", naddrs, cname); | |
1292 | |
1293 if (naddrs) { | |
1294 | |
1295 if (naddrs == 1) { | |
1296 rn->u.addr = addr; | |
1297 | |
1298 } else { | |
1299 | |
1300 addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t)); | |
1301 if (addrs == NULL) { | |
1302 return; | |
1303 } | |
1304 | |
1305 n = 0; | |
1306 i = ans; | |
1307 | |
1308 for (a = 0; a < nan; a++) { | |
1309 | |
1310 for ( ;; ) { | |
1311 | |
1312 if (buf[i] & 0xc0) { | |
1313 i += 2; | |
1314 goto ok; | |
1315 } | |
1316 | |
1317 if (buf[i] == 0) { | |
1318 i++; | |
1319 goto ok; | |
1320 } | |
1321 | |
1322 i += 1 + buf[i]; | |
1323 } | |
1324 | |
1325 ok: | |
1326 | |
1327 an = (ngx_resolver_an_t *) &buf[i]; | |
1328 | |
1329 qtype = (an->type_hi << 8) + an->type_lo; | |
1330 len = (an->len_hi << 8) + an->len_lo; | |
1331 | |
1332 i += sizeof(ngx_resolver_an_t); | |
1333 | |
1334 if (qtype == NGX_RESOLVE_A) { | |
1335 | |
1336 addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16) | |
1337 + (buf[i + 2] << 8) + (buf[i + 3])); | |
1338 | |
1339 if (n == naddrs) { | |
1340 break; | |
1341 } | |
1342 } | |
1343 | |
1344 i += len; | |
1345 } | |
1346 | |
1347 rn->u.addrs = addrs; | |
1348 | |
1349 addrs = ngx_resolver_dup(r, rn->u.addrs, | |
1350 naddrs * sizeof(in_addr_t)); | |
1351 if (addrs == NULL) { | |
1352 return; | |
1353 } | |
1354 } | |
1355 | |
1356 rn->naddrs = (u_short) naddrs; | |
1357 | |
1358 ngx_queue_remove(&rn->queue); | |
1359 | |
1360 rn->valid = ngx_time() + r->valid; | |
1361 rn->expire = ngx_time() + r->expire; | |
1362 | |
1363 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
1364 | |
1365 next = rn->waiting; | |
1366 rn->waiting = NULL; | |
1367 | |
1368 /* unlock name mutex */ | |
1369 | |
1370 while (next) { | |
1371 ctx = next; | |
1372 ctx->state = NGX_OK; | |
1373 ctx->naddrs = naddrs; | |
1374 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs; | |
1375 ctx->addr = addr; | |
1376 next = ctx->next; | |
1377 | |
1378 ctx->handler(ctx); | |
1379 } | |
1380 | |
2483
29494780d978
free addrs only it has been allocated before: non single address
Igor Sysoev <igor@sysoev.ru>
parents:
2482
diff
changeset
|
1381 if (naddrs > 1) { |
1649 | 1382 ngx_resolver_free(r, addrs); |
1383 } | |
1384 | |
1385 return; | |
1386 | |
1387 } else if (cname) { | |
1388 | |
1389 /* CNAME only */ | |
1390 | |
1391 if (ngx_resolver_copy(r, &name, buf, cname, &buf[last]) != NGX_OK) { | |
1392 return; | |
1393 } | |
1394 | |
1395 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
1396 "resolver cname:\"%V\"", &name); | |
1397 | |
1741
0829024c924d
fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents:
1689
diff
changeset
|
1398 ngx_queue_remove(&rn->queue); |
0829024c924d
fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents:
1689
diff
changeset
|
1399 |
1649 | 1400 rn->cnlen = (u_short) name.len; |
1401 rn->u.cname = name.data; | |
1402 rn->valid = ngx_time() + r->valid; | |
1403 rn->expire = ngx_time() + r->expire; | |
1404 | |
1405 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
1406 | |
1407 ctx = rn->waiting; | |
1408 rn->waiting = NULL; | |
1409 | |
1410 if (ctx) { | |
1411 ctx->name = name; | |
1412 | |
1413 (void) ngx_resolve_name_locked(r, ctx); | |
1414 } | |
1415 | |
1416 return; | |
1417 } | |
1418 | |
1419 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1420 "no A or CNAME types in DNS responses, unknown query type: %ui", |
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1421 qtype); |
1649 | 1422 return; |
1423 | |
1424 short_response: | |
1425 | |
1426 err = "short dns response"; | |
1427 | |
1428 invalid: | |
1429 | |
1430 /* unlock name mutex */ | |
1431 | |
1432 ngx_log_error(r->log_level, r->log, 0, err); | |
1433 | |
1434 return; | |
1435 | |
1436 failed: | |
1437 | |
1438 /* unlock name mutex */ | |
1439 | |
3139 | 1440 ngx_resolver_free(r, name.data); |
1441 | |
1649 | 1442 return; |
1443 } | |
1444 | |
1445 | |
1446 static void | |
1447 ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n, | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1448 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan) |
1649 | 1449 { |
1450 char *err; | |
1451 size_t len; | |
1452 in_addr_t addr; | |
1453 ngx_int_t digit; | |
1454 ngx_str_t name; | |
3902
159b58f9c0bd
fix building by gcc 4.6 without --with-debug
Igor Sysoev <igor@sysoev.ru>
parents:
3763
diff
changeset
|
1455 ngx_uint_t i, mask, qident; |
1649 | 1456 ngx_resolver_an_t *an; |
1457 ngx_resolver_ctx_t *ctx, *next; | |
1458 ngx_resolver_node_t *rn; | |
1459 | |
1460 if (ngx_resolver_copy(r, NULL, buf, &buf[12], &buf[n]) != NGX_OK) { | |
1461 goto invalid_in_addr_arpa; | |
1462 } | |
1463 | |
1464 addr = 0; | |
1465 i = 12; | |
1466 | |
1467 for (mask = 0; mask < 32; mask += 8) { | |
1468 len = buf[i++]; | |
1469 | |
1470 digit = ngx_atoi(&buf[i], len); | |
1471 if (digit == NGX_ERROR || digit > 255) { | |
1472 goto invalid_in_addr_arpa; | |
1473 } | |
1474 | |
1475 addr += digit << mask; | |
1476 i += len; | |
1477 } | |
1478 | |
1479 if (ngx_strcmp(&buf[i], "\7in-addr\4arpa") != 0) { | |
1480 goto invalid_in_addr_arpa; | |
1481 } | |
1482 | |
1483 /* lock addr mutex */ | |
1484 | |
1485 rn = ngx_resolver_lookup_addr(r, addr); | |
1486 | |
1487 if (rn == NULL || rn->query == NULL) { | |
1488 ngx_log_error(r->log_level, r->log, 0, | |
1489 "unexpected response for %ud.%ud.%ud.%ud", | |
1490 (addr >> 24) & 0xff, (addr >> 16) & 0xff, | |
1491 (addr >> 8) & 0xff, addr & 0xff); | |
1492 goto failed; | |
1493 } | |
1494 | |
1495 qident = (rn->query[0] << 8) + rn->query[1]; | |
1496 | |
1497 if (ident != qident) { | |
1498 ngx_log_error(r->log_level, r->log, 0, | |
1967
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1499 "wrong ident %ui response for %ud.%ud.%ud.%ud, expect %ui", |
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1500 ident, (addr >> 24) & 0xff, (addr >> 16) & 0xff, |
4a4b15c9c474
use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents:
1966
diff
changeset
|
1501 (addr >> 8) & 0xff, addr & 0xff, qident); |
1649 | 1502 goto failed; |
1503 } | |
1504 | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1505 if (code == 0 && nan == 0) { |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1506 code = 3; /* NXDOMAIN */ |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1507 } |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1508 |
1649 | 1509 if (code) { |
1510 next = rn->waiting; | |
1511 rn->waiting = NULL; | |
1512 | |
1513 ngx_queue_remove(&rn->queue); | |
1514 | |
1515 ngx_rbtree_delete(&r->addr_rbtree, &rn->node); | |
1516 | |
1517 ngx_resolver_free_node(r, rn); | |
1518 | |
1519 /* unlock addr mutex */ | |
1520 | |
1521 while (next) { | |
1522 ctx = next; | |
1523 ctx->state = code; | |
1524 next = ctx->next; | |
1525 | |
1526 ctx->handler(ctx); | |
1527 } | |
1528 | |
1529 return; | |
1530 } | |
1531 | |
1532 i += sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t); | |
1533 | |
1534 if (i + 2 + sizeof(ngx_resolver_an_t) > (ngx_uint_t) n) { | |
1535 goto short_response; | |
1536 } | |
1537 | |
1538 /* compression pointer to "XX.XX.XX.XX.in-addr.arpa */ | |
1539 | |
1540 if (buf[i] != 0xc0 || buf[i + 1] != 0x0c) { | |
1541 err = "invalid in-addr.arpa name in DNS response"; | |
1542 goto invalid; | |
1543 } | |
1544 | |
1545 an = (ngx_resolver_an_t *) &buf[i + 2]; | |
1546 | |
1547 len = (an->len_hi << 8) + an->len_lo; | |
1548 | |
1549 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, | |
3902
159b58f9c0bd
fix building by gcc 4.6 without --with-debug
Igor Sysoev <igor@sysoev.ru>
parents:
3763
diff
changeset
|
1550 "resolver qt:%ui cl:%ui len:%uz", |
159b58f9c0bd
fix building by gcc 4.6 without --with-debug
Igor Sysoev <igor@sysoev.ru>
parents:
3763
diff
changeset
|
1551 (an->type_hi << 8) + an->type_lo, |
159b58f9c0bd
fix building by gcc 4.6 without --with-debug
Igor Sysoev <igor@sysoev.ru>
parents:
3763
diff
changeset
|
1552 (an->class_hi << 8) + an->class_lo, len); |
1649 | 1553 |
1554 i += 2 + sizeof(ngx_resolver_an_t); | |
1555 | |
1556 if (i + len > (ngx_uint_t) n) { | |
1557 goto short_response; | |
1558 } | |
1559 | |
1560 if (ngx_resolver_copy(r, &name, buf, &buf[i], &buf[n]) != NGX_OK) { | |
1561 return; | |
1562 } | |
1563 | |
1564 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name); | |
1565 | |
2486
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
1566 if (name.len != (size_t) rn->nlen |
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
1567 || ngx_strncmp(name.data, rn->name, name.len) != 0) |
1649 | 1568 { |
2482
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
1569 if (rn->nlen) { |
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
1570 ngx_resolver_free(r, rn->name); |
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
1571 } |
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
1572 |
2490
1c87647b7ca5
fix building by msvc, introduced in r2487
Igor Sysoev <igor@sysoev.ru>
parents:
2487
diff
changeset
|
1573 rn->nlen = (u_short) name.len; |
1649 | 1574 rn->name = name.data; |
1575 | |
2486
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
1576 name.data = ngx_resolver_dup(r, rn->name, name.len); |
1649 | 1577 if (name.data == NULL) { |
1578 goto failed; | |
1579 } | |
1580 } | |
1581 | |
1582 ngx_queue_remove(&rn->queue); | |
1583 | |
1584 rn->valid = ngx_time() + r->valid; | |
1585 rn->expire = ngx_time() + r->expire; | |
1586 | |
1587 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue); | |
1588 | |
1589 next = rn->waiting; | |
1590 rn->waiting = NULL; | |
1591 | |
1592 /* unlock addr mutex */ | |
1593 | |
1594 while (next) { | |
1595 ctx = next; | |
1596 ctx->state = NGX_OK; | |
1597 ctx->name = name; | |
1598 next = ctx->next; | |
1599 | |
1600 ctx->handler(ctx); | |
1601 } | |
1602 | |
1603 ngx_resolver_free(r, name.data); | |
1604 | |
1605 return; | |
1606 | |
1607 invalid_in_addr_arpa: | |
1608 | |
1609 ngx_log_error(r->log_level, r->log, 0, | |
1610 "invalid in-addr.arpa name in DNS response"); | |
1611 return; | |
1612 | |
1613 short_response: | |
1614 | |
1615 err = "short DNS response"; | |
1616 | |
1617 invalid: | |
1618 | |
1619 /* unlock addr mutex */ | |
1620 | |
1621 ngx_log_error(r->log_level, r->log, 0, err); | |
1622 | |
1623 return; | |
1624 | |
1625 failed: | |
1626 | |
1627 /* unlock addr mutex */ | |
1628 | |
1629 return; | |
1630 } | |
1631 | |
1632 | |
1633 static ngx_resolver_node_t * | |
1634 ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash) | |
1635 { | |
1636 ngx_int_t rc; | |
1637 ngx_rbtree_node_t *node, *sentinel; | |
1638 ngx_resolver_node_t *rn; | |
1639 | |
1640 node = r->name_rbtree.root; | |
1641 sentinel = r->name_rbtree.sentinel; | |
1642 | |
1643 while (node != sentinel) { | |
1644 | |
1645 if (hash < node->key) { | |
1646 node = node->left; | |
1647 continue; | |
1648 } | |
1649 | |
1650 if (hash > node->key) { | |
1651 node = node->right; | |
1652 continue; | |
1653 } | |
1654 | |
1655 /* hash == node->key */ | |
1656 | |
1657 do { | |
1658 rn = (ngx_resolver_node_t *) node; | |
1659 | |
3143
ab6258e18099
fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents:
3139
diff
changeset
|
1660 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen); |
1649 | 1661 |
1662 if (rc == 0) { | |
1663 return rn; | |
1664 } | |
1665 | |
1666 node = (rc < 0) ? node->left : node->right; | |
1667 | |
1668 } while (node != sentinel && hash == node->key); | |
1669 | |
1670 break; | |
1671 } | |
1672 | |
1673 /* not found */ | |
1674 | |
1675 return NULL; | |
1676 } | |
1677 | |
1678 | |
1679 static ngx_resolver_node_t * | |
1680 ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr) | |
1681 { | |
1682 ngx_rbtree_node_t *node, *sentinel; | |
1683 | |
1684 node = r->addr_rbtree.root; | |
1685 sentinel = r->addr_rbtree.sentinel; | |
1686 | |
1687 while (node != sentinel) { | |
1688 | |
1689 if (addr < node->key) { | |
1690 node = node->left; | |
1691 continue; | |
1692 } | |
1693 | |
1694 if (addr > node->key) { | |
1695 node = node->right; | |
1696 continue; | |
1697 } | |
1698 | |
1699 /* addr == node->key */ | |
1700 | |
1701 return (ngx_resolver_node_t *) node; | |
1702 } | |
1703 | |
1704 /* not found */ | |
1705 | |
1706 return NULL; | |
1707 } | |
1708 | |
1709 | |
1710 static void | |
1711 ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
1712 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
1713 { | |
1714 ngx_rbtree_node_t **p; | |
1715 ngx_resolver_node_t *rn, *rn_temp; | |
1716 | |
1717 for ( ;; ) { | |
1718 | |
1719 if (node->key < temp->key) { | |
1720 | |
1721 p = &temp->left; | |
1722 | |
1723 } else if (node->key > temp->key) { | |
1724 | |
1725 p = &temp->right; | |
1726 | |
1727 } else { /* node->key == temp->key */ | |
1728 | |
1729 rn = (ngx_resolver_node_t *) node; | |
1730 rn_temp = (ngx_resolver_node_t *) temp; | |
1731 | |
3143
ab6258e18099
fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents:
3139
diff
changeset
|
1732 p = (ngx_memn2cmp(rn->name, rn_temp->name, rn->nlen, rn_temp->nlen) |
ab6258e18099
fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents:
3139
diff
changeset
|
1733 < 0) ? &temp->left : &temp->right; |
1649 | 1734 } |
1735 | |
1736 if (*p == sentinel) { | |
1737 break; | |
1738 } | |
1739 | |
1740 temp = *p; | |
1741 } | |
1742 | |
1743 *p = node; | |
1744 node->parent = temp; | |
1745 node->left = sentinel; | |
1746 node->right = sentinel; | |
1747 ngx_rbt_red(node); | |
1748 } | |
1749 | |
1750 | |
1751 static ngx_int_t | |
1752 ngx_resolver_create_name_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx) | |
1753 { | |
1754 u_char *p, *s; | |
3306
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
1755 size_t len, nlen; |
1649 | 1756 ngx_uint_t ident; |
1757 ngx_resolver_qs_t *qs; | |
1758 ngx_resolver_query_t *query; | |
1759 | |
3306
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
1760 nlen = ctx->name.len ? (1 + ctx->name.len + 1) : 1; |
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
1761 |
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
1762 len = sizeof(ngx_resolver_query_t) + nlen + sizeof(ngx_resolver_qs_t); |
1649 | 1763 |
3307
34e99a97fbd6
use ngx_resolver_alloc() instead of ngx_resolver_calloc()
Igor Sysoev <igor@sysoev.ru>
parents:
3306
diff
changeset
|
1764 p = ngx_resolver_alloc(ctx->resolver, len); |
1649 | 1765 if (p == NULL) { |
1766 return NGX_ERROR; | |
1767 } | |
1768 | |
1769 rn->qlen = (u_short) len; | |
1770 rn->query = p; | |
1771 | |
1772 query = (ngx_resolver_query_t *) p; | |
1773 | |
1774 ident = ngx_random(); | |
1775 | |
1776 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->resolver->log, 0, | |
1777 "resolve: \"%V\" %i", &ctx->name, ident & 0xffff); | |
1778 | |
1779 query->ident_hi = (u_char) ((ident >> 8) & 0xff); | |
1780 query->ident_lo = (u_char) (ident & 0xff); | |
1781 | |
1782 /* recursion query */ | |
1783 query->flags_hi = 1; query->flags_lo = 0; | |
1784 | |
1785 /* one question */ | |
1786 query->nqs_hi = 0; query->nqs_lo = 1; | |
1787 query->nan_hi = 0; query->nan_lo = 0; | |
1788 query->nns_hi = 0; query->nns_lo = 0; | |
1789 query->nar_hi = 0; query->nar_lo = 0; | |
1790 | |
3306
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
1791 p += sizeof(ngx_resolver_query_t) + nlen; |
1649 | 1792 |
1793 qs = (ngx_resolver_qs_t *) p; | |
1794 | |
1795 /* query type */ | |
1796 qs->type_hi = 0; qs->type_lo = (u_char) ctx->type; | |
1797 | |
1798 /* IP query class */ | |
1799 qs->class_hi = 0; qs->class_lo = 1; | |
1800 | |
1801 /* convert "www.example.com" to "\3www\7example\3com\0" */ | |
1802 | |
1803 len = 0; | |
1804 p--; | |
1805 *p-- = '\0'; | |
1806 | |
1807 for (s = ctx->name.data + ctx->name.len - 1; s >= ctx->name.data; s--) { | |
1808 if (*s != '.') { | |
1809 *p = *s; | |
1810 len++; | |
1811 | |
1812 } else { | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
1813 if (len == 0) { |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
1814 return NGX_DECLINED; |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
1815 } |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
1816 |
1649 | 1817 *p = (u_char) len; |
1818 len = 0; | |
1819 } | |
1820 | |
1821 p--; | |
1822 } | |
1823 | |
1824 *p = (u_char) len; | |
1825 | |
1826 return NGX_OK; | |
1827 } | |
1828 | |
1829 | |
1830 /* AF_INET only */ | |
1831 | |
1832 static ngx_int_t | |
1833 ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx) | |
1834 { | |
1835 u_char *p, *d; | |
1836 size_t len; | |
1837 ngx_int_t n; | |
1838 ngx_uint_t ident; | |
1839 ngx_resolver_query_t *query; | |
1840 | |
1841 len = sizeof(ngx_resolver_query_t) | |
1842 + sizeof(".255.255.255.255.in-addr.arpa.") - 1 | |
1843 + sizeof(ngx_resolver_qs_t); | |
1844 | |
3307
34e99a97fbd6
use ngx_resolver_alloc() instead of ngx_resolver_calloc()
Igor Sysoev <igor@sysoev.ru>
parents:
3306
diff
changeset
|
1845 p = ngx_resolver_alloc(ctx->resolver, len); |
1649 | 1846 if (p == NULL) { |
1847 return NGX_ERROR; | |
1848 } | |
1849 | |
1850 rn->query = p; | |
1851 query = (ngx_resolver_query_t *) p; | |
1852 | |
1853 ident = ngx_random(); | |
1854 | |
1855 query->ident_hi = (u_char) ((ident >> 8) & 0xff); | |
1856 query->ident_lo = (u_char) (ident & 0xff); | |
1857 | |
1858 /* recursion query */ | |
1859 query->flags_hi = 1; query->flags_lo = 0; | |
1860 | |
1861 /* one question */ | |
1862 query->nqs_hi = 0; query->nqs_lo = 1; | |
1863 query->nan_hi = 0; query->nan_lo = 0; | |
1864 query->nns_hi = 0; query->nns_lo = 0; | |
1865 query->nar_hi = 0; query->nar_lo = 0; | |
1866 | |
1867 p += sizeof(ngx_resolver_query_t); | |
1868 | |
3642 | 1869 for (n = 0; n < 32; n += 8) { |
1649 | 1870 d = ngx_sprintf(&p[1], "%ud", (ctx->addr >> n) & 0xff); |
1871 *p = (u_char) (d - &p[1]); | |
1872 p = d; | |
1873 } | |
1874 | |
1875 /* query type "PTR", IP query class */ | |
1876 ngx_memcpy(p, "\7in-addr\4arpa\0\0\14\0\1", 18); | |
1877 | |
1878 rn->qlen = (u_short) | |
1879 (p + sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t) | |
1880 - rn->query); | |
1881 | |
1882 return NGX_OK; | |
1883 } | |
1884 | |
1885 | |
1886 static ngx_int_t | |
1887 ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src, | |
1888 u_char *last) | |
1889 { | |
1890 char *err; | |
1891 u_char *p, *dst; | |
1892 ssize_t len; | |
1893 ngx_uint_t i, n; | |
1894 | |
1895 p = src; | |
1896 len = -1; | |
1897 | |
1898 /* | |
1899 * compression pointers allow to create endless loop, so we set limit; | |
1900 * 128 pointers should be enough to store 255-byte name | |
1901 */ | |
1902 | |
1903 for (i = 0; i < 128; i++) { | |
1904 n = *p++; | |
1905 | |
1906 if (n == 0) { | |
1907 goto done; | |
1908 } | |
1909 | |
1910 if (n & 0xc0) { | |
2314
52987a023486
fix compression pointer for big (>255) DNS responses
Igor Sysoev <igor@sysoev.ru>
parents:
2282
diff
changeset
|
1911 n = ((n & 0x3f) << 8) + *p; |
1649 | 1912 p = &buf[n]; |
1913 | |
1914 } else { | |
1915 len += 1 + n; | |
1916 p = &p[n]; | |
1917 } | |
1918 | |
1919 if (p >= last) { | |
1920 err = "name is out of response"; | |
1921 goto invalid; | |
1922 } | |
1923 } | |
1924 | |
1925 err = "compression pointers loop"; | |
1926 | |
1927 invalid: | |
1928 | |
1929 ngx_log_error(r->log_level, r->log, 0, err); | |
1930 | |
1931 return NGX_ERROR; | |
1932 | |
1933 done: | |
1934 | |
1935 if (name == NULL) { | |
583 | 1936 return NGX_OK; |
1937 } | |
1938 | |
3298
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1939 if (len == -1) { |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1940 name->len = 0; |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1941 name->data = NULL; |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1942 return NGX_OK; |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1943 } |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
1944 |
1649 | 1945 dst = ngx_resolver_alloc(r, len); |
1946 if (dst == NULL) { | |
1947 return NGX_ERROR; | |
1948 } | |
1949 | |
1950 name->data = dst; | |
1951 | |
1952 n = *src++; | |
1953 | |
1954 for ( ;; ) { | |
1955 if (n != 0xc0) { | |
1956 ngx_memcpy(dst, src, n); | |
1957 dst += n; | |
1958 src += n; | |
1959 | |
1960 n = *src++; | |
1961 | |
1962 if (n != 0) { | |
1963 *dst++ = '.'; | |
1964 } | |
1965 | |
1966 } else { | |
2314
52987a023486
fix compression pointer for big (>255) DNS responses
Igor Sysoev <igor@sysoev.ru>
parents:
2282
diff
changeset
|
1967 n = ((n & 0x3f) << 8) + *src; |
1649 | 1968 src = &buf[n]; |
1969 | |
1970 n = *src++; | |
1971 } | |
1972 | |
1973 if (n == 0) { | |
1974 name->len = dst - name->data; | |
1975 return NGX_OK; | |
1976 } | |
1977 } | |
1978 } | |
1979 | |
1980 | |
1981 static void | |
1982 ngx_resolver_timeout_handler(ngx_event_t *ev) | |
1983 { | |
1984 ngx_resolver_ctx_t *ctx; | |
1985 | |
1986 ctx = ev->data; | |
1987 | |
1988 ctx->state = NGX_RESOLVE_TIMEDOUT; | |
1989 | |
1990 ctx->handler(ctx); | |
1991 } | |
1992 | |
1993 | |
1994 static void | |
1995 ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn) | |
1996 { | |
1997 /* lock alloc mutex */ | |
1998 | |
1999 if (rn->query) { | |
2000 ngx_resolver_free_locked(r, rn->query); | |
2001 } | |
2002 | |
2003 if (rn->name) { | |
2004 ngx_resolver_free_locked(r, rn->name); | |
2005 } | |
2006 | |
2007 if (rn->cnlen) { | |
2008 ngx_resolver_free_locked(r, rn->u.cname); | |
2009 } | |
2010 | |
2011 if (rn->naddrs > 1) { | |
2012 ngx_resolver_free_locked(r, rn->u.addrs); | |
2013 } | |
2014 | |
2015 ngx_resolver_free_locked(r, rn); | |
2016 | |
2017 /* unlock alloc mutex */ | |
2018 } | |
2019 | |
2020 | |
2021 static void * | |
2022 ngx_resolver_alloc(ngx_resolver_t *r, size_t size) | |
2023 { | |
2024 u_char *p; | |
2025 | |
2026 /* lock alloc mutex */ | |
2027 | |
2028 p = ngx_alloc(size, r->log); | |
2029 | |
2030 /* unlock alloc mutex */ | |
2031 | |
2032 return p; | |
2033 } | |
2034 | |
2035 | |
1903 | 2036 static void * |
1649 | 2037 ngx_resolver_calloc(ngx_resolver_t *r, size_t size) |
2038 { | |
2039 u_char *p; | |
2040 | |
2041 p = ngx_resolver_alloc(r, size); | |
2042 | |
2043 if (p) { | |
2044 ngx_memzero(p, size); | |
2045 } | |
2046 | |
2047 return p; | |
2048 } | |
2049 | |
2050 | |
2051 static void | |
2052 ngx_resolver_free(ngx_resolver_t *r, void *p) | |
2053 { | |
2054 /* lock alloc mutex */ | |
2055 | |
2056 ngx_free(p); | |
2057 | |
2058 /* unlock alloc mutex */ | |
2059 } | |
2060 | |
2061 | |
2062 static void | |
2063 ngx_resolver_free_locked(ngx_resolver_t *r, void *p) | |
2064 { | |
2065 ngx_free(p); | |
2066 } | |
2067 | |
2068 | |
2069 static void * | |
2070 ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size) | |
2071 { | |
2072 void *dst; | |
2073 | |
2074 dst = ngx_resolver_alloc(r, size); | |
2075 | |
2076 if (dst == NULL) { | |
2077 return dst; | |
2078 } | |
2079 | |
2080 ngx_memcpy(dst, src, size); | |
2081 | |
2082 return dst; | |
2083 } | |
2084 | |
2085 | |
2086 char * | |
2087 ngx_resolver_strerror(ngx_int_t err) | |
2088 { | |
2089 static char *errors[] = { | |
2090 "Format error", /* FORMERR */ | |
2091 "Server failure", /* SERVFAIL */ | |
2092 "Host not found", /* NXDOMAIN */ | |
2093 "Unimplemented", /* NOTIMP */ | |
2094 "Operation refused" /* REFUSED */ | |
2095 }; | |
2096 | |
2097 if (err > 0 && err < 6) { | |
2098 return errors[err - 1]; | |
2099 } | |
2100 | |
2101 if (err == NGX_RESOLVE_TIMEDOUT) { | |
2102 return "Operation timed out"; | |
2103 } | |
2104 | |
2105 return "Unknown error"; | |
2106 } | |
2107 | |
2108 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2109 static u_char * |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2110 ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len) |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2111 { |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2112 u_char *p; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2113 ngx_udp_connection_t *uc; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2114 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2115 p = buf; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2116 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2117 if (log->action) { |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2118 p = ngx_snprintf(buf, len, " while %s", log->action); |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2119 len -= p - buf; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2120 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2121 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2122 uc = log->data; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2123 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2124 if (uc) { |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2125 p = ngx_snprintf(p, len, ", resolver: %V", &uc->server); |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2126 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2127 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2128 return p; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2129 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2130 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2131 |
1649 | 2132 ngx_int_t |
2133 ngx_udp_connect(ngx_udp_connection_t *uc) | |
2134 { | |
2135 int rc; | |
2136 ngx_int_t event; | |
2137 ngx_event_t *rev, *wev; | |
2138 ngx_socket_t s; | |
2139 ngx_connection_t *c; | |
2140 | |
583 | 2141 s = ngx_socket(AF_INET, SOCK_DGRAM, 0); |
2142 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2143 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s); |
583 | 2144 |
2145 if (s == -1) { | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2146 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, |
583 | 2147 ngx_socket_n " failed"); |
2148 return NGX_ERROR; | |
2149 } | |
2150 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2151 c = ngx_get_connection(s, &uc->log); |
583 | 2152 |
2153 if (c == NULL) { | |
2154 if (ngx_close_socket(s) == -1) { | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2155 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, |
583 | 2156 ngx_close_socket_n "failed"); |
2157 } | |
2158 | |
2159 return NGX_ERROR; | |
2160 } | |
2161 | |
1649 | 2162 if (ngx_nonblocking(s) == -1) { |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2163 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, |
1649 | 2164 ngx_nonblocking_n " failed"); |
2165 | |
2166 ngx_free_connection(c); | |
2167 | |
2168 if (ngx_close_socket(s) == -1) { | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2169 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, |
1649 | 2170 ngx_close_socket_n " failed"); |
2171 } | |
2172 | |
2173 return NGX_ERROR; | |
2174 } | |
2175 | |
583 | 2176 rev = c->read; |
2177 wev = c->write; | |
2178 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2179 rev->log = &uc->log; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2180 wev->log = &uc->log; |
1649 | 2181 |
2182 uc->connection = c; | |
583 | 2183 |
2184 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); | |
2185 | |
2186 #if (NGX_THREADS) | |
1649 | 2187 |
2188 /* TODO: lock event when call completion handler */ | |
2189 | |
2190 rev->lock = &c->lock; | |
2191 wev->lock = &c->lock; | |
583 | 2192 rev->own_lock = &c->lock; |
2193 wev->own_lock = &c->lock; | |
1649 | 2194 |
583 | 2195 #endif |
2196 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2197 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &uc->log, 0, |
1649 | 2198 "connect to %V, fd:%d #%d", &uc->server, s, c->number); |
2199 | |
2200 rc = connect(s, uc->sockaddr, uc->socklen); | |
2201 | |
2202 /* TODO: aio, iocp */ | |
583 | 2203 |
2204 if (rc == -1) { | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2205 ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno, |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
2206 "connect() failed"); |
583 | 2207 |
2208 return NGX_ERROR; | |
2209 } | |
2210 | |
1649 | 2211 /* UDP sockets are always ready to write */ |
2212 wev->ready = 1; | |
2213 | |
2214 if (ngx_add_event) { | |
2215 | |
2216 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ? | |
2217 /* kqueue, epoll */ NGX_CLEAR_EVENT: | |
2218 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT; | |
2219 /* eventport event type has no meaning: oneshot only */ | |
2220 | |
2221 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) { | |
2222 return NGX_ERROR; | |
2223 } | |
2224 | |
2225 } else { | |
2226 /* rtsig */ | |
2227 | |
583 | 2228 if (ngx_add_conn(c) == NGX_ERROR) { |
2229 return NGX_ERROR; | |
2230 } | |
2231 } | |
2232 | |
2233 return NGX_OK; | |
2234 } |