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