blob: 54bd384e859667602454da9e51836fc5c6daebca [file] [log] [blame]
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001
2/*
3 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd3283ff2005-12-05 13:18:09 +00005 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11
12
Igor Sysoevcb4d5382007-11-23 17:13:26 +000013#define NGX_RESOLVER_UDP_SIZE 4096
14
15
Igor Sysoevd3283ff2005-12-05 13:18:09 +000016typedef struct {
Igor Sysoevcb4d5382007-11-23 17:13:26 +000017 u_char ident_hi;
18 u_char ident_lo;
19 u_char flags_hi;
20 u_char flags_lo;
21 u_char nqs_hi;
22 u_char nqs_lo;
23 u_char nan_hi;
24 u_char nan_lo;
25 u_char nns_hi;
26 u_char nns_lo;
27 u_char nar_hi;
28 u_char nar_lo;
29} ngx_resolver_query_t;
Igor Sysoevd3283ff2005-12-05 13:18:09 +000030
Igor Sysoevd3283ff2005-12-05 13:18:09 +000031
Igor Sysoevcb4d5382007-11-23 17:13:26 +000032typedef struct {
33 u_char type_hi;
34 u_char type_lo;
35 u_char class_hi;
36 u_char class_lo;
37} ngx_resolver_qs_t;
Igor Sysoevd3283ff2005-12-05 13:18:09 +000038
Igor Sysoevd3283ff2005-12-05 13:18:09 +000039
Igor Sysoevcb4d5382007-11-23 17:13:26 +000040typedef struct {
41 u_char type_hi;
42 u_char type_lo;
43 u_char class_hi;
44 u_char class_lo;
45 u_char ttl[4];
46 u_char len_hi;
47 u_char len_lo;
48} ngx_resolver_an_t;
49
50
51ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc);
52
53
Igor Sysoev4491dae2008-02-28 20:38:34 +000054static void ngx_resolver_cleanup(void *data);
55static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000056static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r,
57 ngx_resolver_ctx_t *ctx);
58static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree,
59 ngx_queue_t *queue);
60static ngx_int_t ngx_resolver_send_query(ngx_resolver_t *r,
61 ngx_resolver_node_t *rn);
62static ngx_int_t ngx_resolver_create_name_query(ngx_resolver_node_t *rn,
63 ngx_resolver_ctx_t *ctx);
64static ngx_int_t ngx_resolver_create_addr_query(ngx_resolver_node_t *rn,
65 ngx_resolver_ctx_t *ctx);
66static void ngx_resolver_resend_handler(ngx_event_t *ev);
67static time_t ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree,
68 ngx_queue_t *queue);
69static void ngx_resolver_read_response(ngx_event_t *rev);
70static void ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf,
71 size_t n);
72static void ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n,
Igor Sysoevc0cadf12007-12-16 20:47:55 +000073 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000074static void ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
Igor Sysoevc0cadf12007-12-16 20:47:55 +000075 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000076static ngx_resolver_node_t *ngx_resolver_lookup_name(ngx_resolver_t *r,
77 ngx_str_t *name, uint32_t hash);
78static ngx_resolver_node_t *ngx_resolver_lookup_addr(ngx_resolver_t *r,
79 in_addr_t addr);
80static void ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
81 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
82static ngx_int_t ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name,
83 u_char *buf, u_char *src, u_char *last);
84static void ngx_resolver_timeout_handler(ngx_event_t *ev);
85static void ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn);
86static void *ngx_resolver_alloc(ngx_resolver_t *r, size_t size);
Igor Sysoev8ee01f52008-02-28 15:34:53 +000087static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000088static void ngx_resolver_free(ngx_resolver_t *r, void *p);
89static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p);
90static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size);
Igor Sysoev089b2fd2010-01-11 11:01:02 +000091static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000092
93
Igor Sysoevcb4d5382007-11-23 17:13:26 +000094ngx_resolver_t *
Igor Sysoev62071812011-10-24 16:09:05 +000095ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
Igor Sysoevcb4d5382007-11-23 17:13:26 +000096{
Ruslan Ermilovbec516b2011-11-16 13:11:39 +000097 ngx_str_t s;
Igor Sysoev62071812011-10-24 16:09:05 +000098 ngx_url_t u;
99 ngx_uint_t i;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000100 ngx_resolver_t *r;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000101 ngx_pool_cleanup_t *cln;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000102 ngx_udp_connection_t *uc;
103
Igor Sysoevdfd55112008-03-04 10:42:05 +0000104 cln = ngx_pool_cleanup_add(cf->pool, 0);
Igor Sysoev4491dae2008-02-28 20:38:34 +0000105 if (cln == NULL) {
106 return NULL;
107 }
108
109 cln->handler = ngx_resolver_cleanup;
110
Igor Sysoevdfd55112008-03-04 10:42:05 +0000111 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000112 if (r == NULL) {
113 return NULL;
114 }
115
Igor Sysoev62071812011-10-24 16:09:05 +0000116 if (n) {
117 if (ngx_array_init(&r->udp_connections, cf->pool, n,
118 sizeof(ngx_udp_connection_t))
119 != NGX_OK)
120 {
121 return NULL;
122 }
123 }
124
Igor Sysoev4491dae2008-02-28 20:38:34 +0000125 cln->data = r;
126
Igor Sysoevdfd55112008-03-04 10:42:05 +0000127 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000128 if (r->event == NULL) {
129 return NULL;
130 }
131
Igor Sysoev826c02e2007-12-03 12:17:26 +0000132 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel,
133 ngx_resolver_rbtree_insert_value);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000134
Igor Sysoev826c02e2007-12-03 12:17:26 +0000135 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel,
136 ngx_rbtree_insert_value);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000137
Igor Sysoeva9d3a0d2007-12-03 11:21:19 +0000138 ngx_queue_init(&r->name_resend_queue);
139 ngx_queue_init(&r->addr_resend_queue);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000140
Igor Sysoeva9d3a0d2007-12-03 11:21:19 +0000141 ngx_queue_init(&r->name_expire_queue);
142 ngx_queue_init(&r->addr_expire_queue);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000143
144 r->event->handler = ngx_resolver_resend_handler;
145 r->event->data = r;
Igor Sysoev0cd76ea2009-04-30 13:53:42 +0000146 r->event->log = &cf->cycle->new_log;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000147 r->ident = -1;
148
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000149 r->resend_timeout = 5;
150 r->expire = 30;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000151 r->valid = 0;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000152
Igor Sysoev0cd76ea2009-04-30 13:53:42 +0000153 r->log = &cf->cycle->new_log;
Igor Sysoev49ac2b22010-09-27 11:23:45 +0000154 r->log_level = NGX_LOG_ERR;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000155
Igor Sysoev62071812011-10-24 16:09:05 +0000156 for (i = 0; i < n; i++) {
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000157 if (ngx_strncmp(names[i].data, "valid=", 6) == 0) {
158 s.len = names[i].len - 6;
159 s.data = names[i].data + 6;
160
161 r->valid = ngx_parse_time(&s, 1);
162
Maxim Dounin9f38b202012-02-13 15:41:11 +0000163 if (r->valid == (time_t) NGX_ERROR) {
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000164 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
165 "invalid parameter: %V", &names[i]);
166 return NULL;
167 }
168
169 continue;
170 }
171
Igor Sysoev62071812011-10-24 16:09:05 +0000172 ngx_memzero(&u, sizeof(ngx_url_t));
173
174 u.host = names[i];
175 u.port = 53;
176
177 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
Ruslan Ermilovd2e005c2012-05-22 13:12:14 +0000178 if (u.err) {
179 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
180 "%s in resolver \"%V\"",
181 u.err, &u.host);
182 }
183
Igor Sysoev62071812011-10-24 16:09:05 +0000184 return NULL;
185 }
186
187 uc = ngx_array_push(&r->udp_connections);
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000188 if (uc == NULL) {
189 return NULL;
190 }
191
Igor Sysoev62071812011-10-24 16:09:05 +0000192 ngx_memzero(uc, sizeof(ngx_udp_connection_t));
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000193
Igor Sysoev62071812011-10-24 16:09:05 +0000194 uc->sockaddr = u.addrs->sockaddr;
195 uc->socklen = u.addrs->socklen;
196 uc->server = u.addrs->name;
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000197 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000198
199 return r;
200}
201
202
Igor Sysoev4491dae2008-02-28 20:38:34 +0000203static void
204ngx_resolver_cleanup(void *data)
205{
206 ngx_resolver_t *r = data;
207
Igor Sysoev62071812011-10-24 16:09:05 +0000208 ngx_uint_t i;
209 ngx_udp_connection_t *uc;
210
Igor Sysoev4491dae2008-02-28 20:38:34 +0000211 if (r) {
212 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
213 "cleanup resolver");
214
215 ngx_resolver_cleanup_tree(r, &r->name_rbtree);
216
217 ngx_resolver_cleanup_tree(r, &r->addr_rbtree);
218
219 if (r->event) {
220 ngx_free(r->event);
221 }
222
Igor Sysoev4491dae2008-02-28 20:38:34 +0000223
Igor Sysoev62071812011-10-24 16:09:05 +0000224 uc = r->udp_connections.elts;
225
226 for (i = 0; i < r->udp_connections.nelts; i++) {
227 if (uc[i].connection) {
228 ngx_close_connection(uc[i].connection);
229 }
Igor Sysoev4491dae2008-02-28 20:38:34 +0000230 }
231
232 ngx_free(r);
233 }
234}
235
236
237static void
238ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree)
239{
240 ngx_resolver_ctx_t *ctx, *next;
241 ngx_resolver_node_t *rn;
242
243 while (tree->root != tree->sentinel) {
244
245 rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel);
246
247 ngx_queue_remove(&rn->queue);
248
249 for (ctx = rn->waiting; ctx; ctx = next) {
Igor Sysoev433608c2008-05-14 07:54:52 +0000250 next = ctx->next;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000251
252 if (ctx->event) {
253 ngx_resolver_free(r, ctx->event);
254 }
255
256 ngx_resolver_free(r, ctx);
257 }
258
259 ngx_rbtree_delete(tree, &rn->node);
260
261 ngx_resolver_free_node(r, rn);
262 }
263}
264
265
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000266ngx_resolver_ctx_t *
267ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp)
268{
269 in_addr_t addr;
270 ngx_resolver_ctx_t *ctx;
271
272 if (temp) {
273 addr = ngx_inet_addr(temp->name.data, temp->name.len);
274
275 if (addr != INADDR_NONE) {
276 temp->resolver = r;
277 temp->state = NGX_OK;
278 temp->naddrs = 1;
279 temp->addrs = &temp->addr;
280 temp->addr = addr;
281 temp->quick = 1;
282
283 return temp;
284 }
285 }
286
Igor Sysoev62071812011-10-24 16:09:05 +0000287 if (r->udp_connections.nelts == 0) {
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000288 return NGX_NO_RESOLVER;
289 }
290
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000291 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t));
292
293 if (ctx) {
294 ctx->resolver = r;
295 }
296
297 return ctx;
298}
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000299
300
301ngx_int_t
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000302ngx_resolve_name(ngx_resolver_ctx_t *ctx)
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000303{
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000304 ngx_int_t rc;
305 ngx_resolver_t *r;
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000306
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000307 r = ctx->resolver;
308
309 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
310 "resolve: \"%V\"", &ctx->name);
311
312 if (ctx->quick) {
313 ctx->handler(ctx);
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000314 return NGX_OK;
315 }
316
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000317 /* lock name mutex */
318
319 rc = ngx_resolve_name_locked(r, ctx);
320
321 if (rc == NGX_OK) {
322 return NGX_OK;
323 }
324
325 /* unlock name mutex */
326
327 if (rc == NGX_AGAIN) {
328 return NGX_OK;
329 }
330
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000331 /* NGX_ERROR */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000332
333 if (ctx->event) {
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000334 ngx_resolver_free(r, ctx->event);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000335 }
336
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000337 ngx_resolver_free(r, ctx);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000338
339 return NGX_ERROR;
340}
341
342
343void
344ngx_resolve_name_done(ngx_resolver_ctx_t *ctx)
345{
346 uint32_t hash;
347 ngx_resolver_t *r;
348 ngx_resolver_ctx_t *w, **p;
349 ngx_resolver_node_t *rn;
350
351 r = ctx->resolver;
352
353 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
354 "resolve name done: %i", ctx->state);
355
356 if (ctx->quick) {
357 return;
358 }
359
360 if (ctx->event && ctx->event->timer_set) {
361 ngx_del_timer(ctx->event);
362 }
363
364 /* lock name mutex */
365
366 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
367
368 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
369
370 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
371
372 if (rn) {
373 p = &rn->waiting;
374 w = rn->waiting;
375
376 while (w) {
377 if (w == ctx) {
378 *p = w->next;
379
380 goto done;
381 }
382
383 p = &w->next;
384 w = w->next;
385 }
386 }
387
388 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
389 "could not cancel %V resolving", &ctx->name);
390 }
391
392done:
393
394 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue);
395
396 /* unlock name mutex */
397
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000398 /* lock alloc mutex */
399
400 if (ctx->event) {
401 ngx_resolver_free_locked(r, ctx->event);
402 }
403
404 ngx_resolver_free_locked(r, ctx);
405
406 /* unlock alloc mutex */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000407}
408
409
410/* NGX_RESOLVE_A only */
411
412static ngx_int_t
413ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
414{
415 uint32_t hash;
416 in_addr_t addr, *addrs;
Igor Sysoev09b199c2008-04-09 14:45:39 +0000417 ngx_int_t rc;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000418 ngx_uint_t naddrs;
419 ngx_resolver_ctx_t *next;
420 ngx_resolver_node_t *rn;
421
422 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
423
424 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
425
426 if (rn) {
427
428 if (rn->valid >= ngx_time()) {
429
430 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
431
432 ngx_queue_remove(&rn->queue);
433
434 rn->expire = ngx_time() + r->expire;
435
436 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
437
438 naddrs = rn->naddrs;
439
440 if (naddrs) {
441
442 /* NGX_RESOLVE_A answer */
443
444 if (naddrs != 1) {
445 addr = 0;
446 addrs = ngx_resolver_dup(r, rn->u.addrs,
447 naddrs * sizeof(in_addr_t));
448 if (addrs == NULL) {
449 return NGX_ERROR;
450 }
451
452 } else {
453 addr = rn->u.addr;
454 addrs = NULL;
455 }
456
457 ctx->next = rn->waiting;
458 rn->waiting = NULL;
459
460 /* unlock name mutex */
461
462 do {
463 ctx->state = NGX_OK;
464 ctx->naddrs = naddrs;
465 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
466 ctx->addr = addr;
467 next = ctx->next;
468
469 ctx->handler(ctx);
470
471 ctx = next;
472 } while (ctx);
473
474 if (addrs) {
475 ngx_resolver_free(r, addrs);
476 }
477
478 return NGX_OK;
479 }
480
481 /* NGX_RESOLVE_CNAME */
482
Igor Sysoeva8372d82008-04-12 07:29:20 +0000483 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000484
Igor Sysoeva8372d82008-04-12 07:29:20 +0000485 ctx->name.len = rn->cnlen;
486 ctx->name.data = rn->u.cname;
487
488 return ngx_resolve_name_locked(r, ctx);
489 }
490
491 ctx->next = rn->waiting;
492 rn->waiting = NULL;
493
494 /* unlock name mutex */
495
496 do {
497 ctx->state = NGX_RESOLVE_NXDOMAIN;
498 next = ctx->next;
499
500 ctx->handler(ctx);
501
502 ctx = next;
503 } while (ctx);
504
505 return NGX_OK;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000506 }
507
508 if (rn->waiting) {
509
510 ctx->next = rn->waiting;
511 rn->waiting = ctx;
Igor Sysoev64bfa872009-11-09 17:45:56 +0000512 ctx->state = NGX_AGAIN;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000513
514 return NGX_AGAIN;
515 }
516
517 ngx_queue_remove(&rn->queue);
518
519 /* lock alloc mutex */
520
Maxim Dounin7d863c02012-05-14 09:13:45 +0000521 if (rn->query) {
522 ngx_resolver_free_locked(r, rn->query);
523 rn->query = NULL;
524 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000525
526 if (rn->cnlen) {
527 ngx_resolver_free_locked(r, rn->u.cname);
528 }
529
530 if (rn->naddrs > 1) {
531 ngx_resolver_free_locked(r, rn->u.addrs);
532 }
533
534 /* unlock alloc mutex */
535
536 } else {
537
538 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
539 if (rn == NULL) {
540 return NGX_ERROR;
541 }
542
543 rn->name = ngx_resolver_dup(r, ctx->name.data, ctx->name.len);
544 if (rn->name == NULL) {
545 ngx_resolver_free(r, rn);
546 return NGX_ERROR;
547 }
548
549 rn->node.key = hash;
550 rn->nlen = (u_short) ctx->name.len;
Igor Sysoev949aea42008-04-09 14:26:08 +0000551 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000552
553 ngx_rbtree_insert(&r->name_rbtree, &rn->node);
554 }
555
Igor Sysoev09b199c2008-04-09 14:45:39 +0000556 rc = ngx_resolver_create_name_query(rn, ctx);
557
558 if (rc == NGX_ERROR) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000559 goto failed;
560 }
561
Igor Sysoev09b199c2008-04-09 14:45:39 +0000562 if (rc == NGX_DECLINED) {
563 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
564
565 ngx_resolver_free(r, rn->query);
566 ngx_resolver_free(r, rn->name);
567 ngx_resolver_free(r, rn);
568
569 ctx->state = NGX_RESOLVE_NXDOMAIN;
570 ctx->handler(ctx);
571
572 return NGX_OK;
573 }
574
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000575 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
576 goto failed;
577 }
578
579 if (ctx->event == NULL) {
580 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
581 if (ctx->event == NULL) {
582 goto failed;
583 }
584
585 ctx->event->handler = ngx_resolver_timeout_handler;
586 ctx->event->data = ctx;
587 ctx->event->log = r->log;
588 ctx->ident = -1;
589
590 ngx_add_timer(ctx->event, ctx->timeout);
591 }
592
593 if (ngx_queue_empty(&r->name_resend_queue)) {
594 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
595 }
596
597 rn->expire = ngx_time() + r->resend_timeout;
598
599 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue);
600
601 rn->cnlen = 0;
602 rn->naddrs = 0;
603 rn->valid = 0;
604 rn->waiting = ctx;
605
606 ctx->state = NGX_AGAIN;
607
608 return NGX_AGAIN;
609
610failed:
611
612 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
613
Igor Sysoev949aea42008-04-09 14:26:08 +0000614 if (rn->query) {
615 ngx_resolver_free(r, rn->query);
616 }
617
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000618 ngx_resolver_free(r, rn->name);
619
620 ngx_resolver_free(r, rn);
621
622 return NGX_ERROR;
623}
624
625
626ngx_int_t
627ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
628{
Igor Sysoev92588722009-01-29 14:35:23 +0000629 u_char *name;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000630 ngx_resolver_t *r;
631 ngx_resolver_node_t *rn;
632
633 r = ctx->resolver;
634
635 ctx->addr = ntohl(ctx->addr);
636
637 /* lock addr mutex */
638
639 rn = ngx_resolver_lookup_addr(r, ctx->addr);
640
641 if (rn) {
642
643 if (rn->valid >= ngx_time()) {
644
645 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
646
647 ngx_queue_remove(&rn->queue);
648
649 rn->expire = ngx_time() + r->expire;
650
651 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
652
Igor Sysoev92588722009-01-29 14:35:23 +0000653 name = ngx_resolver_dup(r, rn->name, rn->nlen);
654 if (name == NULL) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000655 goto failed;
656 }
657
Igor Sysoev92588722009-01-29 14:35:23 +0000658 ctx->name.len = rn->nlen;
659 ctx->name.data = name;
660
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000661 /* unlock addr mutex */
662
663 ctx->state = NGX_OK;
664
665 ctx->handler(ctx);
666
Igor Sysoev92588722009-01-29 14:35:23 +0000667 ngx_resolver_free(r, name);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000668
669 return NGX_OK;
670 }
671
672 if (rn->waiting) {
673
674 ctx->next = rn->waiting;
675 rn->waiting = ctx;
Igor Sysoev64bfa872009-11-09 17:45:56 +0000676 ctx->state = NGX_AGAIN;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000677
Igor Sysoev8ef386e2009-01-31 20:33:01 +0000678 /* unlock addr mutex */
679
680 return NGX_OK;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000681 }
682
683 ngx_queue_remove(&rn->queue);
684
685 ngx_resolver_free(r, rn->query);
Igor Sysoev949aea42008-04-09 14:26:08 +0000686 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000687
688 } else {
689 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
690 if (rn == NULL) {
691 goto failed;
692 }
693
694 rn->node.key = ctx->addr;
Igor Sysoev949aea42008-04-09 14:26:08 +0000695 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000696
697 ngx_rbtree_insert(&r->addr_rbtree, &rn->node);
698 }
699
700 if (ngx_resolver_create_addr_query(rn, ctx) != NGX_OK) {
701 goto failed;
702 }
703
704 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
705 goto failed;
706 }
707
708 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
709 if (ctx->event == NULL) {
710 goto failed;
711 }
712
713 ctx->event->handler = ngx_resolver_timeout_handler;
714 ctx->event->data = ctx;
715 ctx->event->log = r->log;
716 ctx->ident = -1;
717
718 ngx_add_timer(ctx->event, ctx->timeout);
719
720 if (ngx_queue_empty(&r->addr_resend_queue)) {
721 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
722 }
723
724 rn->expire = ngx_time() + r->resend_timeout;
725
726 ngx_queue_insert_head(&r->addr_resend_queue, &rn->queue);
727
728 rn->cnlen = 0;
729 rn->naddrs = 0;
730 rn->name = NULL;
731 rn->nlen = 0;
732 rn->valid = 0;
733 rn->waiting = ctx;
734
735 /* unlock addr mutex */
736
737 ctx->state = NGX_AGAIN;
738
739 return NGX_OK;
740
741failed:
742
743 if (rn) {
744 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
745
Igor Sysoev949aea42008-04-09 14:26:08 +0000746 if (rn->query) {
747 ngx_resolver_free(r, rn->query);
748 }
749
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000750 ngx_resolver_free(r, rn);
751 }
752
753 /* unlock addr mutex */
754
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000755 if (ctx->event) {
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000756 ngx_resolver_free(r, ctx->event);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000757 }
758
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000759 ngx_resolver_free(r, ctx);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000760
761 return NGX_ERROR;
762}
763
764
765void
766ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
767{
768 in_addr_t addr;
769 ngx_resolver_t *r;
770 ngx_resolver_ctx_t *w, **p;
771 ngx_resolver_node_t *rn;
772
773 r = ctx->resolver;
774
775 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
776 "resolve addr done: %i", ctx->state);
777
778 if (ctx->event && ctx->event->timer_set) {
779 ngx_del_timer(ctx->event);
780 }
781
782 /* lock addr mutex */
783
784 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
785
786 rn = ngx_resolver_lookup_addr(r, ctx->addr);
787
788 if (rn) {
789 p = &rn->waiting;
790 w = rn->waiting;
791
792 while (w) {
793 if (w == ctx) {
794 *p = w->next;
795
796 goto done;
797 }
798
799 p = &w->next;
800 w = w->next;
801 }
802 }
803
804 addr = ntohl(ctx->addr);
805
806 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
807 "could not cancel %ud.%ud.%ud.%ud resolving",
808 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
809 (addr >> 8) & 0xff, addr & 0xff);
810 }
811
812done:
813
814 ngx_resolver_expire(r, &r->addr_rbtree, &r->addr_expire_queue);
815
816 /* unlock addr mutex */
817
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000818 /* lock alloc mutex */
819
820 if (ctx->event) {
821 ngx_resolver_free_locked(r, ctx->event);
822 }
823
824 ngx_resolver_free_locked(r, ctx);
825
826 /* unlock alloc mutex */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000827}
828
829
830static void
831ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
832{
833 time_t now;
834 ngx_uint_t i;
835 ngx_queue_t *q;
836 ngx_resolver_node_t *rn;
837
838 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire");
839
840 now = ngx_time();
841
842 for (i = 0; i < 2; i++) {
843 if (ngx_queue_empty(queue)) {
844 return;
845 }
846
847 q = ngx_queue_last(queue);
848
849 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
850
851 if (now <= rn->expire) {
852 return;
853 }
854
Igor Sysoevd02661a2007-12-24 17:05:31 +0000855 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
856 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000857
858 ngx_queue_remove(q);
859
860 ngx_rbtree_delete(tree, &rn->node);
861
862 ngx_resolver_free_node(r, rn);
863 }
864}
865
866
867static ngx_int_t
868ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn)
869{
870 ssize_t n;
871 ngx_udp_connection_t *uc;
872
Igor Sysoev62071812011-10-24 16:09:05 +0000873 uc = r->udp_connections.elts;
874
875 uc = &uc[r->last_connection++];
876 if (r->last_connection == r->udp_connections.nelts) {
877 r->last_connection = 0;
878 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000879
880 if (uc->connection == NULL) {
Maxim Dounin53d96772012-02-27 16:57:02 +0000881
882 uc->log = *r->log;
883 uc->log.handler = ngx_resolver_log_error;
884 uc->log.data = uc;
885 uc->log.action = "resolving";
886
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000887 if (ngx_udp_connect(uc) != NGX_OK) {
888 return NGX_ERROR;
889 }
890
891 uc->connection->data = r;
892 uc->connection->read->handler = ngx_resolver_read_response;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000893 uc->connection->read->resolver = 1;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000894 }
895
896 n = ngx_send(uc->connection, rn->query, rn->qlen);
897
898 if (n == -1) {
899 return NGX_ERROR;
900 }
901
902 if ((size_t) n != (size_t) rn->qlen) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +0000903 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete");
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000904 return NGX_ERROR;
905 }
906
907 return NGX_OK;
908}
909
910
911static void
912ngx_resolver_resend_handler(ngx_event_t *ev)
913{
914 time_t timer, atimer, ntimer;
915 ngx_resolver_t *r;
916
917 r = ev->data;
918
919 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0,
920 "resolver resend handler");
921
922 /* lock name mutex */
923
924 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue);
925
926 /* unlock name mutex */
927
928 /* lock addr mutex */
929
930 atimer = ngx_resolver_resend(r, &r->addr_rbtree, &r->addr_resend_queue);
Igor Sysoevb4e5b4c2007-12-01 19:57:37 +0000931
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000932 /* unlock addr mutex */
933
934 if (ntimer == 0) {
935 timer = atimer;
936
937 } else if (atimer == 0) {
938 timer = ntimer;
939
940 } else {
941 timer = (atimer < ntimer) ? atimer : ntimer;
942 }
943
944 if (timer) {
945 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000));
946 }
947}
948
949
950static time_t
951ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
952{
953 time_t now;
954 ngx_queue_t *q;
955 ngx_resolver_node_t *rn;
956
957 now = ngx_time();
958
959 for ( ;; ) {
960 if (ngx_queue_empty(queue)) {
961 return 0;
962 }
963
964 q = ngx_queue_last(queue);
965
966 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
967
968 if (now < rn->expire) {
969 return rn->expire - now;
970 }
971
Igor Sysoevd02661a2007-12-24 17:05:31 +0000972 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
973 "resolver resend \"%*s\" %p",
974 (size_t) rn->nlen, rn->name, rn->waiting);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000975
976 ngx_queue_remove(q);
977
978 if (rn->waiting) {
979
980 if (ngx_resolver_send_query(r, rn) == NGX_OK) {
981
982 rn->expire = now + r->resend_timeout;
983
984 ngx_queue_insert_head(queue, &rn->queue);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000985 }
Igor Sysoev61adfb22008-02-06 16:08:52 +0000986
987 continue;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000988 }
989
990 ngx_rbtree_delete(tree, &rn->node);
991
992 ngx_resolver_free_node(r, rn);
993 }
994}
995
996
997static void
998ngx_resolver_read_response(ngx_event_t *rev)
999{
1000 ssize_t n;
1001 ngx_connection_t *c;
1002 u_char buf[NGX_RESOLVER_UDP_SIZE];
1003
1004 c = rev->data;
1005
1006 do {
Igor Sysoeve67d4612007-12-03 16:46:46 +00001007 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001008
Igor Sysoeve67d4612007-12-03 16:46:46 +00001009 if (n < 0) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001010 return;
1011 }
1012
1013 ngx_resolver_process_response(c->data, buf, n);
1014
1015 } while (rev->ready);
1016}
1017
1018
1019static void
1020ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n)
1021{
1022 char *err;
1023 size_t len;
Igor Sysoev571a5e12008-10-24 14:38:09 +00001024 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan,
1025 qtype, qclass;
1026 ngx_queue_t *q;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001027 ngx_resolver_qs_t *qs;
Igor Sysoev571a5e12008-10-24 14:38:09 +00001028 ngx_resolver_node_t *rn;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001029 ngx_resolver_query_t *query;
1030
Igor Sysoev1c2bb212008-10-24 14:34:10 +00001031 if ((size_t) n < sizeof(ngx_resolver_query_t)) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001032 goto short_response;
1033 }
1034
1035 query = (ngx_resolver_query_t *) buf;
1036
1037 ident = (query->ident_hi << 8) + query->ident_lo;
1038 flags = (query->flags_hi << 8) + query->flags_lo;
1039 nqs = (query->nqs_hi << 8) + query->nqs_lo;
1040 nan = (query->nan_hi << 8) + query->nan_lo;
1041
1042 ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001043 "resolver DNS response %ui fl:%04Xui %ui/%ui/%ui/%ui",
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001044 ident, flags, nqs, nan,
1045 (query->nns_hi << 8) + query->nns_lo,
1046 (query->nar_hi << 8) + query->nar_lo);
1047
1048 if (!(flags & 0x8000)) {
1049 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001050 "invalid DNS response %ui fl:%04Xui", ident, flags);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001051 return;
1052 }
1053
1054 code = flags & 0x7f;
1055
Igor Sysoev571a5e12008-10-24 14:38:09 +00001056 if (code == NGX_RESOLVE_FORMERR) {
1057
1058 times = 0;
1059
1060 for (q = ngx_queue_head(&r->name_resend_queue);
1061 q != ngx_queue_sentinel(&r->name_resend_queue) || times++ < 100;
1062 q = ngx_queue_next(q))
1063 {
1064 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
1065 qident = (rn->query[0] << 8) + rn->query[1];
1066
1067 if (qident == ident) {
1068 ngx_log_error(r->log_level, r->log, 0,
1069 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"",
1070 code, ngx_resolver_strerror(code), ident,
1071 rn->nlen, rn->name);
1072 return;
1073 }
1074 }
1075
1076 goto dns_error;
1077 }
1078
1079 if (code > NGX_RESOLVE_REFUSED) {
1080 goto dns_error;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001081 }
1082
1083 if (nqs != 1) {
1084 err = "invalid number of questions in DNS response";
1085 goto done;
1086 }
1087
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001088 i = sizeof(ngx_resolver_query_t);
1089
1090 while (i < (ngx_uint_t) n) {
1091 if (buf[i] == '\0') {
1092 goto found;
1093 }
1094
1095 len = buf[i];
1096 i += 1 + len;
1097 }
1098
1099 goto short_response;
1100
1101found:
1102
1103 if (i++ == 0) {
1104 err = "zero-length domain name in DNS response";
1105 goto done;
1106 }
1107
1108 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t))
1109 > (ngx_uint_t) n)
1110 {
1111 goto short_response;
1112 }
1113
1114 qs = (ngx_resolver_qs_t *) &buf[i];
1115
1116 qtype = (qs->type_hi << 8) + qs->type_lo;
1117 qclass = (qs->class_hi << 8) + qs->class_lo;
1118
1119 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001120 "resolver DNS response qt:%ui cl:%ui", qtype, qclass);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001121
1122 if (qclass != 1) {
1123 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001124 "unknown query class %ui in DNS response", qclass);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001125 return;
1126 }
1127
1128 switch (qtype) {
1129
1130 case NGX_RESOLVE_A:
1131
1132 ngx_resolver_process_a(r, buf, n, ident, code, nan,
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001133 i + sizeof(ngx_resolver_qs_t));
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001134
1135 break;
1136
1137 case NGX_RESOLVE_PTR:
1138
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001139 ngx_resolver_process_ptr(r, buf, n, ident, code, nan);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001140
1141 break;
1142
1143 default:
1144 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001145 "unknown query type %ui in DNS response", qtype);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001146 return;
1147 }
1148
1149 return;
1150
1151short_response:
1152
1153 err = "short dns response";
1154
1155done:
1156
1157 ngx_log_error(r->log_level, r->log, 0, err);
1158
1159 return;
Igor Sysoev571a5e12008-10-24 14:38:09 +00001160
1161dns_error:
1162
1163 ngx_log_error(r->log_level, r->log, 0,
1164 "DNS error (%ui: %s), query id:%ui",
1165 code, ngx_resolver_strerror(code), ident);
1166 return;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001167}
1168
1169
1170static void
1171ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
1172 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans)
1173{
1174 char *err;
1175 u_char *cname;
1176 size_t len;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001177 int32_t ttl;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001178 uint32_t hash;
1179 in_addr_t addr, *addrs;
1180 ngx_str_t name;
1181 ngx_uint_t qtype, qident, naddrs, a, i, n, start;
1182 ngx_resolver_an_t *an;
1183 ngx_resolver_ctx_t *ctx, *next;
1184 ngx_resolver_node_t *rn;
1185
1186 if (ngx_resolver_copy(r, &name, buf, &buf[12], &buf[last]) != NGX_OK) {
1187 return;
1188 }
1189
1190 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
1191
1192 hash = ngx_crc32_short(name.data, name.len);
1193
1194 /* lock name mutex */
1195
1196 rn = ngx_resolver_lookup_name(r, &name, hash);
1197
1198 if (rn == NULL || rn->query == NULL) {
1199 ngx_log_error(r->log_level, r->log, 0,
1200 "unexpected response for %V", &name);
1201 goto failed;
1202 }
1203
1204 qident = (rn->query[0] << 8) + rn->query[1];
1205
1206 if (ident != qident) {
1207 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001208 "wrong ident %ui response for %V, expect %ui",
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001209 ident, &name, qident);
1210 goto failed;
1211 }
1212
Igor Sysoev99651522009-09-16 13:48:11 +00001213 ngx_resolver_free(r, name.data);
1214
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001215 if (code == 0 && nan == 0) {
1216 code = 3; /* NXDOMAIN */
1217 }
1218
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001219 if (code) {
1220 next = rn->waiting;
1221 rn->waiting = NULL;
1222
1223 ngx_queue_remove(&rn->queue);
1224
1225 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
1226
1227 ngx_resolver_free_node(r, rn);
1228
1229 /* unlock name mutex */
1230
1231 while (next) {
1232 ctx = next;
1233 ctx->state = code;
1234 next = ctx->next;
1235
1236 ctx->handler(ctx);
1237 }
1238
1239 return;
1240 }
1241
1242 i = ans;
1243 naddrs = 0;
1244 addr = 0;
1245 addrs = NULL;
1246 cname = NULL;
1247 qtype = 0;
Ruslan Ermilov777879c2011-11-16 13:27:33 +00001248 ttl = 0;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001249
1250 for (a = 0; a < nan; a++) {
1251
1252 start = i;
1253
1254 while (i < last) {
1255
1256 if (buf[i] & 0xc0) {
1257 i += 2;
1258 goto found;
1259 }
1260
1261 if (buf[i] == 0) {
1262 i++;
1263 goto test_length;
1264 }
1265
1266 i += 1 + buf[i];
1267 }
1268
1269 goto short_response;
1270
1271 test_length:
1272
1273 if (i - start < 2) {
1274 err = "invalid name in dns response";
1275 goto invalid;
1276 }
1277
1278 found:
1279
1280 if (i + sizeof(ngx_resolver_an_t) >= last) {
1281 goto short_response;
1282 }
1283
1284 an = (ngx_resolver_an_t *) &buf[i];
1285
1286 qtype = (an->type_hi << 8) + an->type_lo;
1287 len = (an->len_hi << 8) + an->len_lo;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001288 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
1289 + (an->ttl[2] << 8) + (an->ttl[3]);
1290
1291 if (ttl < 0) {
1292 ttl = 0;
1293 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001294
1295 if (qtype == NGX_RESOLVE_A) {
1296
1297 i += sizeof(ngx_resolver_an_t);
1298
1299 if (i + len > last) {
1300 goto short_response;
1301 }
1302
1303 addr = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1304 + (buf[i + 2] << 8) + (buf[i + 3]));
1305
1306 naddrs++;
1307
1308 i += len;
1309
1310 } else if (qtype == NGX_RESOLVE_CNAME) {
1311 cname = &buf[i] + sizeof(ngx_resolver_an_t);
1312 i += sizeof(ngx_resolver_an_t) + len;
Igor Sysoev19540cd2008-04-10 17:12:21 +00001313
1314 } else if (qtype == NGX_RESOLVE_DNAME) {
1315 i += sizeof(ngx_resolver_an_t) + len;
Igor Sysoev4ad5be62008-04-10 17:26:14 +00001316
1317 } else {
1318 ngx_log_error(r->log_level, r->log, 0,
1319 "unexpected qtype %ui", qtype);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001320 }
1321 }
1322
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001323 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
1324 "resolver naddrs:%ui cname:%p ttl:%d",
1325 naddrs, cname, ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001326
1327 if (naddrs) {
1328
1329 if (naddrs == 1) {
1330 rn->u.addr = addr;
1331
1332 } else {
1333
1334 addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
1335 if (addrs == NULL) {
1336 return;
1337 }
1338
1339 n = 0;
1340 i = ans;
1341
1342 for (a = 0; a < nan; a++) {
1343
1344 for ( ;; ) {
1345
1346 if (buf[i] & 0xc0) {
1347 i += 2;
1348 goto ok;
1349 }
1350
1351 if (buf[i] == 0) {
1352 i++;
1353 goto ok;
1354 }
1355
1356 i += 1 + buf[i];
1357 }
1358
1359 ok:
1360
1361 an = (ngx_resolver_an_t *) &buf[i];
1362
1363 qtype = (an->type_hi << 8) + an->type_lo;
1364 len = (an->len_hi << 8) + an->len_lo;
1365
1366 i += sizeof(ngx_resolver_an_t);
1367
1368 if (qtype == NGX_RESOLVE_A) {
1369
1370 addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1371 + (buf[i + 2] << 8) + (buf[i + 3]));
1372
1373 if (n == naddrs) {
1374 break;
1375 }
1376 }
1377
1378 i += len;
1379 }
1380
1381 rn->u.addrs = addrs;
1382
1383 addrs = ngx_resolver_dup(r, rn->u.addrs,
1384 naddrs * sizeof(in_addr_t));
1385 if (addrs == NULL) {
1386 return;
1387 }
1388 }
1389
1390 rn->naddrs = (u_short) naddrs;
1391
1392 ngx_queue_remove(&rn->queue);
1393
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001394 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001395 rn->expire = ngx_time() + r->expire;
1396
1397 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1398
1399 next = rn->waiting;
1400 rn->waiting = NULL;
1401
1402 /* unlock name mutex */
1403
1404 while (next) {
1405 ctx = next;
1406 ctx->state = NGX_OK;
1407 ctx->naddrs = naddrs;
1408 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
1409 ctx->addr = addr;
1410 next = ctx->next;
1411
1412 ctx->handler(ctx);
1413 }
1414
Igor Sysoevfa748fe2009-01-29 14:32:58 +00001415 if (naddrs > 1) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001416 ngx_resolver_free(r, addrs);
1417 }
1418
Maxim Dounin7d863c02012-05-14 09:13:45 +00001419 ngx_resolver_free(r, rn->query);
1420 rn->query = NULL;
1421
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001422 return;
1423
1424 } else if (cname) {
1425
1426 /* CNAME only */
1427
1428 if (ngx_resolver_copy(r, &name, buf, cname, &buf[last]) != NGX_OK) {
1429 return;
1430 }
1431
1432 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
1433 "resolver cname:\"%V\"", &name);
1434
Igor Sysoev28bca1b2007-12-16 20:00:15 +00001435 ngx_queue_remove(&rn->queue);
1436
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001437 rn->cnlen = (u_short) name.len;
1438 rn->u.cname = name.data;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001439
1440 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001441 rn->expire = ngx_time() + r->expire;
1442
1443 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1444
1445 ctx = rn->waiting;
1446 rn->waiting = NULL;
1447
1448 if (ctx) {
1449 ctx->name = name;
1450
1451 (void) ngx_resolve_name_locked(r, ctx);
1452 }
1453
Maxim Dounin7d863c02012-05-14 09:13:45 +00001454 ngx_resolver_free(r, rn->query);
1455 rn->query = NULL;
1456
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001457 return;
1458 }
1459
1460 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001461 "no A or CNAME types in DNS responses, unknown query type: %ui",
1462 qtype);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001463 return;
1464
1465short_response:
1466
1467 err = "short dns response";
1468
1469invalid:
1470
1471 /* unlock name mutex */
1472
1473 ngx_log_error(r->log_level, r->log, 0, err);
1474
1475 return;
1476
1477failed:
1478
1479 /* unlock name mutex */
1480
Igor Sysoev99651522009-09-16 13:48:11 +00001481 ngx_resolver_free(r, name.data);
1482
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001483 return;
1484}
1485
1486
1487static void
1488ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001489 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001490{
1491 char *err;
1492 size_t len;
1493 in_addr_t addr;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001494 int32_t ttl;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001495 ngx_int_t digit;
1496 ngx_str_t name;
Igor Sysoevefde3eb2011-04-23 17:25:06 +00001497 ngx_uint_t i, mask, qident;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001498 ngx_resolver_an_t *an;
1499 ngx_resolver_ctx_t *ctx, *next;
1500 ngx_resolver_node_t *rn;
1501
1502 if (ngx_resolver_copy(r, NULL, buf, &buf[12], &buf[n]) != NGX_OK) {
1503 goto invalid_in_addr_arpa;
1504 }
1505
1506 addr = 0;
1507 i = 12;
1508
1509 for (mask = 0; mask < 32; mask += 8) {
1510 len = buf[i++];
1511
1512 digit = ngx_atoi(&buf[i], len);
1513 if (digit == NGX_ERROR || digit > 255) {
1514 goto invalid_in_addr_arpa;
1515 }
1516
1517 addr += digit << mask;
1518 i += len;
1519 }
1520
1521 if (ngx_strcmp(&buf[i], "\7in-addr\4arpa") != 0) {
1522 goto invalid_in_addr_arpa;
1523 }
1524
1525 /* lock addr mutex */
1526
1527 rn = ngx_resolver_lookup_addr(r, addr);
1528
1529 if (rn == NULL || rn->query == NULL) {
1530 ngx_log_error(r->log_level, r->log, 0,
1531 "unexpected response for %ud.%ud.%ud.%ud",
1532 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1533 (addr >> 8) & 0xff, addr & 0xff);
1534 goto failed;
1535 }
1536
1537 qident = (rn->query[0] << 8) + rn->query[1];
1538
1539 if (ident != qident) {
1540 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001541 "wrong ident %ui response for %ud.%ud.%ud.%ud, expect %ui",
1542 ident, (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1543 (addr >> 8) & 0xff, addr & 0xff, qident);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001544 goto failed;
1545 }
1546
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001547 if (code == 0 && nan == 0) {
1548 code = 3; /* NXDOMAIN */
1549 }
1550
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001551 if (code) {
1552 next = rn->waiting;
1553 rn->waiting = NULL;
1554
1555 ngx_queue_remove(&rn->queue);
1556
1557 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
1558
1559 ngx_resolver_free_node(r, rn);
1560
1561 /* unlock addr mutex */
1562
1563 while (next) {
1564 ctx = next;
1565 ctx->state = code;
1566 next = ctx->next;
1567
1568 ctx->handler(ctx);
1569 }
1570
1571 return;
1572 }
1573
1574 i += sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t);
1575
1576 if (i + 2 + sizeof(ngx_resolver_an_t) > (ngx_uint_t) n) {
1577 goto short_response;
1578 }
1579
1580 /* compression pointer to "XX.XX.XX.XX.in-addr.arpa */
1581
1582 if (buf[i] != 0xc0 || buf[i + 1] != 0x0c) {
1583 err = "invalid in-addr.arpa name in DNS response";
1584 goto invalid;
1585 }
1586
1587 an = (ngx_resolver_an_t *) &buf[i + 2];
1588
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001589 len = (an->len_hi << 8) + an->len_lo;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001590 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
1591 + (an->ttl[2] << 8) + (an->ttl[3]);
1592
1593 if (ttl < 0) {
1594 ttl = 0;
1595 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001596
1597 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
Igor Sysoevefde3eb2011-04-23 17:25:06 +00001598 "resolver qt:%ui cl:%ui len:%uz",
1599 (an->type_hi << 8) + an->type_lo,
1600 (an->class_hi << 8) + an->class_lo, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001601
1602 i += 2 + sizeof(ngx_resolver_an_t);
1603
1604 if (i + len > (ngx_uint_t) n) {
1605 goto short_response;
1606 }
1607
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001608 if (ngx_resolver_copy(r, &name, buf, &buf[i], &buf[n]) != NGX_OK) {
1609 return;
1610 }
1611
1612 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name);
1613
Igor Sysoev2c01fd32009-01-30 11:56:45 +00001614 if (name.len != (size_t) rn->nlen
1615 || ngx_strncmp(name.data, rn->name, name.len) != 0)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001616 {
Igor Sysoev8629b9b2009-01-29 14:29:49 +00001617 if (rn->nlen) {
1618 ngx_resolver_free(r, rn->name);
1619 }
1620
Igor Sysoev9a5f4c72009-02-02 10:17:06 +00001621 rn->nlen = (u_short) name.len;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001622 rn->name = name.data;
1623
Igor Sysoev2c01fd32009-01-30 11:56:45 +00001624 name.data = ngx_resolver_dup(r, rn->name, name.len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001625 if (name.data == NULL) {
1626 goto failed;
1627 }
1628 }
1629
1630 ngx_queue_remove(&rn->queue);
1631
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001632 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001633 rn->expire = ngx_time() + r->expire;
1634
1635 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
1636
1637 next = rn->waiting;
1638 rn->waiting = NULL;
1639
1640 /* unlock addr mutex */
1641
1642 while (next) {
1643 ctx = next;
1644 ctx->state = NGX_OK;
1645 ctx->name = name;
1646 next = ctx->next;
1647
1648 ctx->handler(ctx);
1649 }
1650
1651 ngx_resolver_free(r, name.data);
1652
1653 return;
1654
1655invalid_in_addr_arpa:
1656
1657 ngx_log_error(r->log_level, r->log, 0,
1658 "invalid in-addr.arpa name in DNS response");
1659 return;
1660
1661short_response:
1662
1663 err = "short DNS response";
1664
1665invalid:
1666
1667 /* unlock addr mutex */
1668
1669 ngx_log_error(r->log_level, r->log, 0, err);
1670
1671 return;
1672
1673failed:
1674
1675 /* unlock addr mutex */
1676
1677 return;
1678}
1679
1680
1681static ngx_resolver_node_t *
1682ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
1683{
1684 ngx_int_t rc;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001685 ngx_rbtree_node_t *node, *sentinel;
1686 ngx_resolver_node_t *rn;
1687
1688 node = r->name_rbtree.root;
1689 sentinel = r->name_rbtree.sentinel;
1690
1691 while (node != sentinel) {
1692
1693 if (hash < node->key) {
1694 node = node->left;
1695 continue;
1696 }
1697
1698 if (hash > node->key) {
1699 node = node->right;
1700 continue;
1701 }
1702
1703 /* hash == node->key */
1704
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001705 rn = (ngx_resolver_node_t *) node;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001706
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001707 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001708
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001709 if (rc == 0) {
1710 return rn;
1711 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001712
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001713 node = (rc < 0) ? node->left : node->right;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001714 }
1715
1716 /* not found */
1717
1718 return NULL;
1719}
1720
1721
1722static ngx_resolver_node_t *
1723ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr)
1724{
1725 ngx_rbtree_node_t *node, *sentinel;
1726
1727 node = r->addr_rbtree.root;
1728 sentinel = r->addr_rbtree.sentinel;
1729
1730 while (node != sentinel) {
1731
1732 if (addr < node->key) {
1733 node = node->left;
1734 continue;
1735 }
1736
1737 if (addr > node->key) {
1738 node = node->right;
1739 continue;
1740 }
1741
1742 /* addr == node->key */
1743
1744 return (ngx_resolver_node_t *) node;
1745 }
1746
1747 /* not found */
1748
1749 return NULL;
1750}
1751
1752
1753static void
1754ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
1755 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1756{
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001757 ngx_rbtree_node_t **p;
1758 ngx_resolver_node_t *rn, *rn_temp;
1759
1760 for ( ;; ) {
1761
1762 if (node->key < temp->key) {
1763
1764 p = &temp->left;
1765
1766 } else if (node->key > temp->key) {
1767
1768 p = &temp->right;
1769
1770 } else { /* node->key == temp->key */
1771
1772 rn = (ngx_resolver_node_t *) node;
1773 rn_temp = (ngx_resolver_node_t *) temp;
1774
Igor Sysoeva8d3d222009-09-19 16:15:13 +00001775 p = (ngx_memn2cmp(rn->name, rn_temp->name, rn->nlen, rn_temp->nlen)
1776 < 0) ? &temp->left : &temp->right;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001777 }
1778
1779 if (*p == sentinel) {
1780 break;
1781 }
1782
1783 temp = *p;
1784 }
1785
1786 *p = node;
1787 node->parent = temp;
1788 node->left = sentinel;
1789 node->right = sentinel;
1790 ngx_rbt_red(node);
1791}
1792
1793
1794static ngx_int_t
1795ngx_resolver_create_name_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1796{
1797 u_char *p, *s;
Igor Sysoeve79f4292009-11-11 14:27:24 +00001798 size_t len, nlen;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001799 ngx_uint_t ident;
1800 ngx_resolver_qs_t *qs;
1801 ngx_resolver_query_t *query;
1802
Igor Sysoeve79f4292009-11-11 14:27:24 +00001803 nlen = ctx->name.len ? (1 + ctx->name.len + 1) : 1;
1804
1805 len = sizeof(ngx_resolver_query_t) + nlen + sizeof(ngx_resolver_qs_t);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001806
Igor Sysoev653edd42009-11-11 14:29:20 +00001807 p = ngx_resolver_alloc(ctx->resolver, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001808 if (p == NULL) {
1809 return NGX_ERROR;
1810 }
1811
1812 rn->qlen = (u_short) len;
1813 rn->query = p;
1814
1815 query = (ngx_resolver_query_t *) p;
1816
1817 ident = ngx_random();
1818
1819 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->resolver->log, 0,
1820 "resolve: \"%V\" %i", &ctx->name, ident & 0xffff);
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
Igor Sysoeve79f4292009-11-11 14:27:24 +00001834 p += sizeof(ngx_resolver_query_t) + nlen;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001835
1836 qs = (ngx_resolver_qs_t *) p;
1837
1838 /* query type */
1839 qs->type_hi = 0; qs->type_lo = (u_char) ctx->type;
1840
1841 /* IP query class */
1842 qs->class_hi = 0; qs->class_lo = 1;
1843
1844 /* convert "www.example.com" to "\3www\7example\3com\0" */
1845
1846 len = 0;
1847 p--;
1848 *p-- = '\0';
1849
Ruslan Ermilov487ba702012-04-26 12:58:42 +00001850 if (ctx->name.len == 0) {
1851 return NGX_DECLINED;
1852 }
1853
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001854 for (s = ctx->name.data + ctx->name.len - 1; s >= ctx->name.data; s--) {
1855 if (*s != '.') {
1856 *p = *s;
1857 len++;
1858
1859 } else {
Maxim Dounine34ff162012-03-22 11:57:18 +00001860 if (len == 0 || len > 255) {
Igor Sysoev09b199c2008-04-09 14:45:39 +00001861 return NGX_DECLINED;
1862 }
1863
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001864 *p = (u_char) len;
1865 len = 0;
1866 }
1867
1868 p--;
1869 }
1870
Maxim Dounine34ff162012-03-22 11:57:18 +00001871 if (len == 0 || len > 255) {
1872 return NGX_DECLINED;
1873 }
1874
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001875 *p = (u_char) len;
1876
1877 return NGX_OK;
1878}
1879
1880
1881/* AF_INET only */
1882
1883static ngx_int_t
1884ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1885{
1886 u_char *p, *d;
1887 size_t len;
1888 ngx_int_t n;
1889 ngx_uint_t ident;
1890 ngx_resolver_query_t *query;
1891
1892 len = sizeof(ngx_resolver_query_t)
1893 + sizeof(".255.255.255.255.in-addr.arpa.") - 1
1894 + sizeof(ngx_resolver_qs_t);
1895
Igor Sysoev653edd42009-11-11 14:29:20 +00001896 p = ngx_resolver_alloc(ctx->resolver, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001897 if (p == NULL) {
1898 return NGX_ERROR;
1899 }
1900
1901 rn->query = p;
1902 query = (ngx_resolver_query_t *) p;
1903
1904 ident = ngx_random();
1905
1906 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
1907 query->ident_lo = (u_char) (ident & 0xff);
1908
1909 /* recursion query */
1910 query->flags_hi = 1; query->flags_lo = 0;
1911
1912 /* one question */
1913 query->nqs_hi = 0; query->nqs_lo = 1;
1914 query->nan_hi = 0; query->nan_lo = 0;
1915 query->nns_hi = 0; query->nns_lo = 0;
1916 query->nar_hi = 0; query->nar_lo = 0;
1917
1918 p += sizeof(ngx_resolver_query_t);
1919
Igor Sysoevaa6936e2010-06-23 16:34:54 +00001920 for (n = 0; n < 32; n += 8) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001921 d = ngx_sprintf(&p[1], "%ud", (ctx->addr >> n) & 0xff);
1922 *p = (u_char) (d - &p[1]);
1923 p = d;
1924 }
1925
1926 /* query type "PTR", IP query class */
1927 ngx_memcpy(p, "\7in-addr\4arpa\0\0\14\0\1", 18);
1928
1929 rn->qlen = (u_short)
1930 (p + sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t)
1931 - rn->query);
1932
1933 return NGX_OK;
1934}
1935
1936
1937static ngx_int_t
1938ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src,
1939 u_char *last)
1940{
1941 char *err;
1942 u_char *p, *dst;
1943 ssize_t len;
1944 ngx_uint_t i, n;
1945
1946 p = src;
1947 len = -1;
1948
1949 /*
1950 * compression pointers allow to create endless loop, so we set limit;
1951 * 128 pointers should be enough to store 255-byte name
1952 */
1953
1954 for (i = 0; i < 128; i++) {
1955 n = *p++;
1956
1957 if (n == 0) {
1958 goto done;
1959 }
1960
1961 if (n & 0xc0) {
Igor Sysoevb3ac4fb2008-11-17 08:04:41 +00001962 n = ((n & 0x3f) << 8) + *p;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001963 p = &buf[n];
1964
1965 } else {
1966 len += 1 + n;
1967 p = &p[n];
1968 }
1969
1970 if (p >= last) {
1971 err = "name is out of response";
1972 goto invalid;
1973 }
1974 }
1975
1976 err = "compression pointers loop";
1977
1978invalid:
1979
1980 ngx_log_error(r->log_level, r->log, 0, err);
1981
1982 return NGX_ERROR;
1983
1984done:
1985
1986 if (name == NULL) {
1987 return NGX_OK;
1988 }
1989
Igor Sysoev70966c12009-11-09 18:04:05 +00001990 if (len == -1) {
1991 name->len = 0;
1992 name->data = NULL;
1993 return NGX_OK;
1994 }
1995
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001996 dst = ngx_resolver_alloc(r, len);
1997 if (dst == NULL) {
1998 return NGX_ERROR;
1999 }
2000
2001 name->data = dst;
2002
2003 n = *src++;
2004
2005 for ( ;; ) {
Igor Sysoeveb499ba2011-11-09 13:54:07 +00002006 if (n & 0xc0) {
2007 n = ((n & 0x3f) << 8) + *src;
2008 src = &buf[n];
2009
2010 n = *src++;
2011
2012 } else {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002013 ngx_memcpy(dst, src, n);
2014 dst += n;
2015 src += n;
2016
2017 n = *src++;
2018
2019 if (n != 0) {
2020 *dst++ = '.';
2021 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002022 }
2023
2024 if (n == 0) {
2025 name->len = dst - name->data;
2026 return NGX_OK;
2027 }
2028 }
2029}
2030
2031
2032static void
2033ngx_resolver_timeout_handler(ngx_event_t *ev)
2034{
2035 ngx_resolver_ctx_t *ctx;
2036
2037 ctx = ev->data;
2038
2039 ctx->state = NGX_RESOLVE_TIMEDOUT;
2040
2041 ctx->handler(ctx);
2042}
2043
2044
2045static void
2046ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn)
2047{
2048 /* lock alloc mutex */
2049
2050 if (rn->query) {
2051 ngx_resolver_free_locked(r, rn->query);
2052 }
2053
2054 if (rn->name) {
2055 ngx_resolver_free_locked(r, rn->name);
2056 }
2057
2058 if (rn->cnlen) {
2059 ngx_resolver_free_locked(r, rn->u.cname);
2060 }
2061
2062 if (rn->naddrs > 1) {
2063 ngx_resolver_free_locked(r, rn->u.addrs);
2064 }
2065
2066 ngx_resolver_free_locked(r, rn);
2067
2068 /* unlock alloc mutex */
2069}
2070
2071
2072static void *
2073ngx_resolver_alloc(ngx_resolver_t *r, size_t size)
2074{
2075 u_char *p;
2076
2077 /* lock alloc mutex */
2078
2079 p = ngx_alloc(size, r->log);
2080
2081 /* unlock alloc mutex */
2082
2083 return p;
2084}
2085
2086
Igor Sysoev8ee01f52008-02-28 15:34:53 +00002087static void *
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002088ngx_resolver_calloc(ngx_resolver_t *r, size_t size)
2089{
2090 u_char *p;
2091
2092 p = ngx_resolver_alloc(r, size);
2093
2094 if (p) {
2095 ngx_memzero(p, size);
2096 }
2097
2098 return p;
2099}
2100
2101
2102static void
2103ngx_resolver_free(ngx_resolver_t *r, void *p)
2104{
2105 /* lock alloc mutex */
2106
2107 ngx_free(p);
2108
2109 /* unlock alloc mutex */
2110}
2111
2112
2113static void
2114ngx_resolver_free_locked(ngx_resolver_t *r, void *p)
2115{
2116 ngx_free(p);
2117}
2118
2119
2120static void *
2121ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size)
2122{
2123 void *dst;
2124
2125 dst = ngx_resolver_alloc(r, size);
2126
2127 if (dst == NULL) {
2128 return dst;
2129 }
2130
2131 ngx_memcpy(dst, src, size);
2132
2133 return dst;
2134}
2135
2136
2137char *
2138ngx_resolver_strerror(ngx_int_t err)
2139{
2140 static char *errors[] = {
2141 "Format error", /* FORMERR */
2142 "Server failure", /* SERVFAIL */
2143 "Host not found", /* NXDOMAIN */
2144 "Unimplemented", /* NOTIMP */
2145 "Operation refused" /* REFUSED */
2146 };
2147
2148 if (err > 0 && err < 6) {
2149 return errors[err - 1];
2150 }
2151
2152 if (err == NGX_RESOLVE_TIMEDOUT) {
2153 return "Operation timed out";
2154 }
2155
2156 return "Unknown error";
2157}
2158
2159
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002160static u_char *
2161ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len)
2162{
2163 u_char *p;
2164 ngx_udp_connection_t *uc;
2165
2166 p = buf;
2167
2168 if (log->action) {
2169 p = ngx_snprintf(buf, len, " while %s", log->action);
2170 len -= p - buf;
2171 }
2172
2173 uc = log->data;
2174
2175 if (uc) {
2176 p = ngx_snprintf(p, len, ", resolver: %V", &uc->server);
2177 }
2178
2179 return p;
2180}
2181
2182
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002183ngx_int_t
2184ngx_udp_connect(ngx_udp_connection_t *uc)
2185{
2186 int rc;
2187 ngx_int_t event;
2188 ngx_event_t *rev, *wev;
2189 ngx_socket_t s;
2190 ngx_connection_t *c;
2191
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002192 s = ngx_socket(AF_INET, SOCK_DGRAM, 0);
2193
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002194 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002195
2196 if (s == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002197 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002198 ngx_socket_n " failed");
2199 return NGX_ERROR;
2200 }
2201
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002202 c = ngx_get_connection(s, &uc->log);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002203
2204 if (c == NULL) {
2205 if (ngx_close_socket(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002206 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002207 ngx_close_socket_n "failed");
2208 }
2209
2210 return NGX_ERROR;
2211 }
2212
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002213 if (ngx_nonblocking(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002214 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002215 ngx_nonblocking_n " failed");
2216
2217 ngx_free_connection(c);
2218
2219 if (ngx_close_socket(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002220 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002221 ngx_close_socket_n " failed");
2222 }
2223
2224 return NGX_ERROR;
2225 }
2226
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002227 rev = c->read;
2228 wev = c->write;
2229
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002230 rev->log = &uc->log;
2231 wev->log = &uc->log;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002232
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002233 uc->connection = c;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002234
2235 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
2236
2237#if (NGX_THREADS)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002238
2239 /* TODO: lock event when call completion handler */
2240
2241 rev->lock = &c->lock;
2242 wev->lock = &c->lock;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002243 rev->own_lock = &c->lock;
2244 wev->own_lock = &c->lock;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002245
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002246#endif
2247
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002248 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &uc->log, 0,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002249 "connect to %V, fd:%d #%d", &uc->server, s, c->number);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002250
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002251 rc = connect(s, uc->sockaddr, uc->socklen);
2252
2253 /* TODO: aio, iocp */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002254
2255 if (rc == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002256 ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno,
2257 "connect() failed");
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002258
2259 return NGX_ERROR;
2260 }
2261
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002262 /* UDP sockets are always ready to write */
2263 wev->ready = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002264
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002265 if (ngx_add_event) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002266
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002267 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ?
2268 /* kqueue, epoll */ NGX_CLEAR_EVENT:
2269 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT;
2270 /* eventport event type has no meaning: oneshot only */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002271
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002272 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
2273 return NGX_ERROR;
2274 }
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002275
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002276 } else {
2277 /* rtsig */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002278
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002279 if (ngx_add_conn(c) == NGX_ERROR) {
2280 return NGX_ERROR;
2281 }
2282 }
2283
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002284 return NGX_OK;
2285}