nginx 0.8.32
[nginx-catap.git] / src / core / ngx_resolver.c
bloba2b55706299af1365999dc1727db2cca1c2d4165
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
12 #define NGX_RESOLVER_UDP_SIZE 4096
15 typedef struct {
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;
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;
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;
50 ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc);
53 static void ngx_resolver_cleanup(void *data);
54 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree);
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,
72 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans);
73 static void ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
74 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan);
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);
86 static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size);
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 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
93 ngx_resolver_t *
94 ngx_resolver_create(ngx_conf_t *cf, ngx_addr_t *addr)
96 ngx_resolver_t *r;
97 ngx_pool_cleanup_t *cln;
98 ngx_udp_connection_t *uc;
100 cln = ngx_pool_cleanup_add(cf->pool, 0);
101 if (cln == NULL) {
102 return NULL;
105 cln->handler = ngx_resolver_cleanup;
107 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log);
108 if (r == NULL) {
109 return NULL;
112 cln->data = r;
114 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log);
115 if (r->event == NULL) {
116 return NULL;
119 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel,
120 ngx_resolver_rbtree_insert_value);
122 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel,
123 ngx_rbtree_insert_value);
125 ngx_queue_init(&r->name_resend_queue);
126 ngx_queue_init(&r->addr_resend_queue);
128 ngx_queue_init(&r->name_expire_queue);
129 ngx_queue_init(&r->addr_expire_queue);
131 r->event->handler = ngx_resolver_resend_handler;
132 r->event->data = r;
133 r->event->log = &cf->cycle->new_log;
134 r->ident = -1;
136 r->resend_timeout = 5;
137 r->expire = 30;
138 r->valid = 300;
140 r->log = &cf->cycle->new_log;
141 r->log_level = NGX_LOG_ALERT;
143 if (addr) {
144 uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log);
145 if (uc == NULL) {
146 return NULL;
149 r->udp_connection = uc;
151 uc->sockaddr = addr->sockaddr;
152 uc->socklen = addr->socklen;
153 uc->server = addr->name;
155 uc->log = cf->cycle->new_log;
156 uc->log.handler = ngx_resolver_log_error;
157 uc->log.data = uc;
158 uc->log.action = "resolving";
161 return r;
165 static void
166 ngx_resolver_cleanup(void *data)
168 ngx_resolver_t *r = data;
170 if (r) {
171 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
172 "cleanup resolver");
174 ngx_resolver_cleanup_tree(r, &r->name_rbtree);
176 ngx_resolver_cleanup_tree(r, &r->addr_rbtree);
178 if (r->event) {
179 ngx_free(r->event);
182 if (r->udp_connection) {
183 if (r->udp_connection->connection) {
184 ngx_close_connection(r->udp_connection->connection);
187 ngx_free(r->udp_connection);
190 ngx_free(r);
195 static void
196 ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree)
198 ngx_resolver_ctx_t *ctx, *next;
199 ngx_resolver_node_t *rn;
201 while (tree->root != tree->sentinel) {
203 rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel);
205 ngx_queue_remove(&rn->queue);
207 for (ctx = rn->waiting; ctx; ctx = next) {
208 next = ctx->next;
210 if (ctx->event) {
211 ngx_resolver_free(r, ctx->event);
214 ngx_resolver_free(r, ctx);
217 ngx_rbtree_delete(tree, &rn->node);
219 ngx_resolver_free_node(r, rn);
224 ngx_resolver_ctx_t *
225 ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp)
227 in_addr_t addr;
228 ngx_resolver_ctx_t *ctx;
230 if (temp) {
231 addr = ngx_inet_addr(temp->name.data, temp->name.len);
233 if (addr != INADDR_NONE) {
234 temp->resolver = r;
235 temp->state = NGX_OK;
236 temp->naddrs = 1;
237 temp->addrs = &temp->addr;
238 temp->addr = addr;
239 temp->quick = 1;
241 return temp;
245 if (r->udp_connection == NULL) {
246 return NGX_NO_RESOLVER;
249 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t));
251 if (ctx) {
252 ctx->resolver = r;
255 return ctx;
259 ngx_int_t
260 ngx_resolve_name(ngx_resolver_ctx_t *ctx)
262 ngx_int_t rc;
263 ngx_resolver_t *r;
265 r = ctx->resolver;
267 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
268 "resolve: \"%V\"", &ctx->name);
270 if (ctx->quick) {
271 ctx->handler(ctx);
272 return NGX_OK;
275 /* lock name mutex */
277 rc = ngx_resolve_name_locked(r, ctx);
279 if (rc == NGX_OK) {
280 return NGX_OK;
283 /* unlock name mutex */
285 if (rc == NGX_AGAIN) {
286 return NGX_OK;
289 /* NGX_ERROR */
291 if (ctx->event) {
292 ngx_resolver_free(r, ctx->event);
295 ngx_resolver_free(r, ctx);
297 return NGX_ERROR;
301 void
302 ngx_resolve_name_done(ngx_resolver_ctx_t *ctx)
304 uint32_t hash;
305 ngx_resolver_t *r;
306 ngx_resolver_ctx_t *w, **p;
307 ngx_resolver_node_t *rn;
309 r = ctx->resolver;
311 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
312 "resolve name done: %i", ctx->state);
314 if (ctx->quick) {
315 return;
318 if (ctx->event && ctx->event->timer_set) {
319 ngx_del_timer(ctx->event);
322 /* lock name mutex */
324 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
326 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
328 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
330 if (rn) {
331 p = &rn->waiting;
332 w = rn->waiting;
334 while (w) {
335 if (w == ctx) {
336 *p = w->next;
338 goto done;
341 p = &w->next;
342 w = w->next;
346 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
347 "could not cancel %V resolving", &ctx->name);
350 done:
352 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue);
354 /* unlock name mutex */
356 /* lock alloc mutex */
358 if (ctx->event) {
359 ngx_resolver_free_locked(r, ctx->event);
362 ngx_resolver_free_locked(r, ctx);
364 /* unlock alloc mutex */
368 /* NGX_RESOLVE_A only */
370 static ngx_int_t
371 ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
373 uint32_t hash;
374 in_addr_t addr, *addrs;
375 ngx_int_t rc;
376 ngx_uint_t naddrs;
377 ngx_resolver_ctx_t *next;
378 ngx_resolver_node_t *rn;
380 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
382 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
384 if (rn) {
386 if (rn->valid >= ngx_time()) {
388 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
390 ngx_queue_remove(&rn->queue);
392 rn->expire = ngx_time() + r->expire;
394 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
396 naddrs = rn->naddrs;
398 if (naddrs) {
400 /* NGX_RESOLVE_A answer */
402 if (naddrs != 1) {
403 addr = 0;
404 addrs = ngx_resolver_dup(r, rn->u.addrs,
405 naddrs * sizeof(in_addr_t));
406 if (addrs == NULL) {
407 return NGX_ERROR;
410 } else {
411 addr = rn->u.addr;
412 addrs = NULL;
415 ctx->next = rn->waiting;
416 rn->waiting = NULL;
418 /* unlock name mutex */
420 do {
421 ctx->state = NGX_OK;
422 ctx->naddrs = naddrs;
423 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
424 ctx->addr = addr;
425 next = ctx->next;
427 ctx->handler(ctx);
429 ctx = next;
430 } while (ctx);
432 if (addrs) {
433 ngx_resolver_free(r, addrs);
436 return NGX_OK;
439 /* NGX_RESOLVE_CNAME */
441 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) {
443 ctx->name.len = rn->cnlen;
444 ctx->name.data = rn->u.cname;
446 return ngx_resolve_name_locked(r, ctx);
449 ctx->next = rn->waiting;
450 rn->waiting = NULL;
452 /* unlock name mutex */
454 do {
455 ctx->state = NGX_RESOLVE_NXDOMAIN;
456 next = ctx->next;
458 ctx->handler(ctx);
460 ctx = next;
461 } while (ctx);
463 return NGX_OK;
466 if (rn->waiting) {
468 ctx->next = rn->waiting;
469 rn->waiting = ctx;
470 ctx->state = NGX_AGAIN;
472 return NGX_AGAIN;
475 ngx_queue_remove(&rn->queue);
477 /* lock alloc mutex */
479 ngx_resolver_free_locked(r, rn->query);
480 rn->query = NULL;
482 if (rn->cnlen) {
483 ngx_resolver_free_locked(r, rn->u.cname);
486 if (rn->naddrs > 1) {
487 ngx_resolver_free_locked(r, rn->u.addrs);
490 /* unlock alloc mutex */
492 } else {
494 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
495 if (rn == NULL) {
496 return NGX_ERROR;
499 rn->name = ngx_resolver_dup(r, ctx->name.data, ctx->name.len);
500 if (rn->name == NULL) {
501 ngx_resolver_free(r, rn);
502 return NGX_ERROR;
505 rn->node.key = hash;
506 rn->nlen = (u_short) ctx->name.len;
507 rn->query = NULL;
509 ngx_rbtree_insert(&r->name_rbtree, &rn->node);
512 rc = ngx_resolver_create_name_query(rn, ctx);
514 if (rc == NGX_ERROR) {
515 goto failed;
518 if (rc == NGX_DECLINED) {
519 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
521 ngx_resolver_free(r, rn->query);
522 ngx_resolver_free(r, rn->name);
523 ngx_resolver_free(r, rn);
525 ctx->state = NGX_RESOLVE_NXDOMAIN;
526 ctx->handler(ctx);
528 return NGX_OK;
531 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
532 goto failed;
535 if (ctx->event == NULL) {
536 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
537 if (ctx->event == NULL) {
538 goto failed;
541 ctx->event->handler = ngx_resolver_timeout_handler;
542 ctx->event->data = ctx;
543 ctx->event->log = r->log;
544 ctx->ident = -1;
546 ngx_add_timer(ctx->event, ctx->timeout);
549 if (ngx_queue_empty(&r->name_resend_queue)) {
550 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
553 rn->expire = ngx_time() + r->resend_timeout;
555 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue);
557 rn->cnlen = 0;
558 rn->naddrs = 0;
559 rn->valid = 0;
560 rn->waiting = ctx;
562 ctx->state = NGX_AGAIN;
564 return NGX_AGAIN;
566 failed:
568 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
570 if (rn->query) {
571 ngx_resolver_free(r, rn->query);
574 ngx_resolver_free(r, rn->name);
576 ngx_resolver_free(r, rn);
578 return NGX_ERROR;
582 ngx_int_t
583 ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
585 u_char *name;
586 ngx_resolver_t *r;
587 ngx_resolver_node_t *rn;
589 r = ctx->resolver;
591 ctx->addr = ntohl(ctx->addr);
593 /* lock addr mutex */
595 rn = ngx_resolver_lookup_addr(r, ctx->addr);
597 if (rn) {
599 if (rn->valid >= ngx_time()) {
601 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
603 ngx_queue_remove(&rn->queue);
605 rn->expire = ngx_time() + r->expire;
607 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
609 name = ngx_resolver_dup(r, rn->name, rn->nlen);
610 if (name == NULL) {
611 goto failed;
614 ctx->name.len = rn->nlen;
615 ctx->name.data = name;
617 /* unlock addr mutex */
619 ctx->state = NGX_OK;
621 ctx->handler(ctx);
623 ngx_resolver_free(r, name);
625 return NGX_OK;
628 if (rn->waiting) {
630 ctx->next = rn->waiting;
631 rn->waiting = ctx;
632 ctx->state = NGX_AGAIN;
634 /* unlock addr mutex */
636 return NGX_OK;
639 ngx_queue_remove(&rn->queue);
641 ngx_resolver_free(r, rn->query);
642 rn->query = NULL;
644 } else {
645 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
646 if (rn == NULL) {
647 goto failed;
650 rn->node.key = ctx->addr;
651 rn->query = NULL;
653 ngx_rbtree_insert(&r->addr_rbtree, &rn->node);
656 if (ngx_resolver_create_addr_query(rn, ctx) != NGX_OK) {
657 goto failed;
660 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
661 goto failed;
664 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
665 if (ctx->event == NULL) {
666 goto failed;
669 ctx->event->handler = ngx_resolver_timeout_handler;
670 ctx->event->data = ctx;
671 ctx->event->log = r->log;
672 ctx->ident = -1;
674 ngx_add_timer(ctx->event, ctx->timeout);
676 if (ngx_queue_empty(&r->addr_resend_queue)) {
677 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
680 rn->expire = ngx_time() + r->resend_timeout;
682 ngx_queue_insert_head(&r->addr_resend_queue, &rn->queue);
684 rn->cnlen = 0;
685 rn->naddrs = 0;
686 rn->name = NULL;
687 rn->nlen = 0;
688 rn->valid = 0;
689 rn->waiting = ctx;
691 /* unlock addr mutex */
693 ctx->state = NGX_AGAIN;
695 return NGX_OK;
697 failed:
699 if (rn) {
700 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
702 if (rn->query) {
703 ngx_resolver_free(r, rn->query);
706 ngx_resolver_free(r, rn);
709 /* unlock addr mutex */
711 if (ctx->event) {
712 ngx_resolver_free(r, ctx->event);
715 ngx_resolver_free(r, ctx);
717 return NGX_ERROR;
721 void
722 ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
724 in_addr_t addr;
725 ngx_resolver_t *r;
726 ngx_resolver_ctx_t *w, **p;
727 ngx_resolver_node_t *rn;
729 r = ctx->resolver;
731 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
732 "resolve addr done: %i", ctx->state);
734 if (ctx->event && ctx->event->timer_set) {
735 ngx_del_timer(ctx->event);
738 /* lock addr mutex */
740 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
742 rn = ngx_resolver_lookup_addr(r, ctx->addr);
744 if (rn) {
745 p = &rn->waiting;
746 w = rn->waiting;
748 while (w) {
749 if (w == ctx) {
750 *p = w->next;
752 goto done;
755 p = &w->next;
756 w = w->next;
760 addr = ntohl(ctx->addr);
762 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
763 "could not cancel %ud.%ud.%ud.%ud resolving",
764 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
765 (addr >> 8) & 0xff, addr & 0xff);
768 done:
770 ngx_resolver_expire(r, &r->addr_rbtree, &r->addr_expire_queue);
772 /* unlock addr mutex */
774 /* lock alloc mutex */
776 if (ctx->event) {
777 ngx_resolver_free_locked(r, ctx->event);
780 ngx_resolver_free_locked(r, ctx);
782 /* unlock alloc mutex */
786 static void
787 ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
789 time_t now;
790 ngx_uint_t i;
791 ngx_queue_t *q;
792 ngx_resolver_node_t *rn;
794 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire");
796 now = ngx_time();
798 for (i = 0; i < 2; i++) {
799 if (ngx_queue_empty(queue)) {
800 return;
803 q = ngx_queue_last(queue);
805 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
807 if (now <= rn->expire) {
808 return;
811 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
812 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name);
814 ngx_queue_remove(q);
816 ngx_rbtree_delete(tree, &rn->node);
818 ngx_resolver_free_node(r, rn);
823 static ngx_int_t
824 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn)
826 ssize_t n;
827 ngx_udp_connection_t *uc;
829 uc = r->udp_connection;
831 if (uc->connection == NULL) {
832 if (ngx_udp_connect(uc) != NGX_OK) {
833 return NGX_ERROR;
836 uc->connection->data = r;
837 uc->connection->read->handler = ngx_resolver_read_response;
838 uc->connection->read->resolver = 1;
841 n = ngx_send(uc->connection, rn->query, rn->qlen);
843 if (n == -1) {
844 return NGX_ERROR;
847 if ((size_t) n != (size_t) rn->qlen) {
848 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete");
849 return NGX_ERROR;
852 return NGX_OK;
856 static void
857 ngx_resolver_resend_handler(ngx_event_t *ev)
859 time_t timer, atimer, ntimer;
860 ngx_resolver_t *r;
862 r = ev->data;
864 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0,
865 "resolver resend handler");
867 /* lock name mutex */
869 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue);
871 /* unlock name mutex */
873 /* lock addr mutex */
875 atimer = ngx_resolver_resend(r, &r->addr_rbtree, &r->addr_resend_queue);
877 /* unlock addr mutex */
879 if (ntimer == 0) {
880 timer = atimer;
882 } else if (atimer == 0) {
883 timer = ntimer;
885 } else {
886 timer = (atimer < ntimer) ? atimer : ntimer;
889 if (timer) {
890 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000));
895 static time_t
896 ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
898 time_t now;
899 ngx_queue_t *q;
900 ngx_resolver_node_t *rn;
902 now = ngx_time();
904 for ( ;; ) {
905 if (ngx_queue_empty(queue)) {
906 return 0;
909 q = ngx_queue_last(queue);
911 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
913 if (now < rn->expire) {
914 return rn->expire - now;
917 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
918 "resolver resend \"%*s\" %p",
919 (size_t) rn->nlen, rn->name, rn->waiting);
921 ngx_queue_remove(q);
923 if (rn->waiting) {
925 if (ngx_resolver_send_query(r, rn) == NGX_OK) {
927 rn->expire = now + r->resend_timeout;
929 ngx_queue_insert_head(queue, &rn->queue);
932 continue;
935 ngx_rbtree_delete(tree, &rn->node);
937 ngx_resolver_free_node(r, rn);
942 static void
943 ngx_resolver_read_response(ngx_event_t *rev)
945 ssize_t n;
946 ngx_connection_t *c;
947 u_char buf[NGX_RESOLVER_UDP_SIZE];
949 c = rev->data;
951 do {
952 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE);
954 if (n < 0) {
955 return;
958 ngx_resolver_process_response(c->data, buf, n);
960 } while (rev->ready);
964 static void
965 ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n)
967 char *err;
968 size_t len;
969 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan,
970 qtype, qclass;
971 ngx_queue_t *q;
972 ngx_resolver_qs_t *qs;
973 ngx_resolver_node_t *rn;
974 ngx_resolver_query_t *query;
976 if ((size_t) n < sizeof(ngx_resolver_query_t)) {
977 goto short_response;
980 query = (ngx_resolver_query_t *) buf;
982 ident = (query->ident_hi << 8) + query->ident_lo;
983 flags = (query->flags_hi << 8) + query->flags_lo;
984 nqs = (query->nqs_hi << 8) + query->nqs_lo;
985 nan = (query->nan_hi << 8) + query->nan_lo;
987 ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0,
988 "resolver DNS response %ui fl:%04Xui %ui/%ui/%ui/%ui",
989 ident, flags, nqs, nan,
990 (query->nns_hi << 8) + query->nns_lo,
991 (query->nar_hi << 8) + query->nar_lo);
993 if (!(flags & 0x8000)) {
994 ngx_log_error(r->log_level, r->log, 0,
995 "invalid DNS response %ui fl:%04Xui", ident, flags);
996 return;
999 code = flags & 0x7f;
1001 if (code == NGX_RESOLVE_FORMERR) {
1003 times = 0;
1005 for (q = ngx_queue_head(&r->name_resend_queue);
1006 q != ngx_queue_sentinel(&r->name_resend_queue) || times++ < 100;
1007 q = ngx_queue_next(q))
1009 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
1010 qident = (rn->query[0] << 8) + rn->query[1];
1012 if (qident == ident) {
1013 ngx_log_error(r->log_level, r->log, 0,
1014 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"",
1015 code, ngx_resolver_strerror(code), ident,
1016 rn->nlen, rn->name);
1017 return;
1021 goto dns_error;
1024 if (code > NGX_RESOLVE_REFUSED) {
1025 goto dns_error;
1028 if (nqs != 1) {
1029 err = "invalid number of questions in DNS response";
1030 goto done;
1033 i = sizeof(ngx_resolver_query_t);
1035 while (i < (ngx_uint_t) n) {
1036 if (buf[i] == '\0') {
1037 goto found;
1040 len = buf[i];
1041 i += 1 + len;
1044 goto short_response;
1046 found:
1048 if (i++ == 0) {
1049 err = "zero-length domain name in DNS response";
1050 goto done;
1053 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t))
1054 > (ngx_uint_t) n)
1056 goto short_response;
1059 qs = (ngx_resolver_qs_t *) &buf[i];
1061 qtype = (qs->type_hi << 8) + qs->type_lo;
1062 qclass = (qs->class_hi << 8) + qs->class_lo;
1064 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
1065 "resolver DNS response qt:%ui cl:%ui", qtype, qclass);
1067 if (qclass != 1) {
1068 ngx_log_error(r->log_level, r->log, 0,
1069 "unknown query class %ui in DNS response", qclass);
1070 return;
1073 switch (qtype) {
1075 case NGX_RESOLVE_A:
1077 ngx_resolver_process_a(r, buf, n, ident, code, nan,
1078 i + sizeof(ngx_resolver_qs_t));
1080 break;
1082 case NGX_RESOLVE_PTR:
1084 ngx_resolver_process_ptr(r, buf, n, ident, code, nan);
1086 break;
1088 default:
1089 ngx_log_error(r->log_level, r->log, 0,
1090 "unknown query type %ui in DNS response", qtype);
1091 return;
1094 return;
1096 short_response:
1098 err = "short dns response";
1100 done:
1102 ngx_log_error(r->log_level, r->log, 0, err);
1104 return;
1106 dns_error:
1108 ngx_log_error(r->log_level, r->log, 0,
1109 "DNS error (%ui: %s), query id:%ui",
1110 code, ngx_resolver_strerror(code), ident);
1111 return;
1115 static void
1116 ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
1117 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans)
1119 char *err;
1120 u_char *cname;
1121 size_t len;
1122 uint32_t hash;
1123 in_addr_t addr, *addrs;
1124 ngx_str_t name;
1125 ngx_uint_t qtype, qident, naddrs, a, i, n, start;
1126 ngx_resolver_an_t *an;
1127 ngx_resolver_ctx_t *ctx, *next;
1128 ngx_resolver_node_t *rn;
1130 if (ngx_resolver_copy(r, &name, buf, &buf[12], &buf[last]) != NGX_OK) {
1131 return;
1134 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
1136 hash = ngx_crc32_short(name.data, name.len);
1138 /* lock name mutex */
1140 rn = ngx_resolver_lookup_name(r, &name, hash);
1142 if (rn == NULL || rn->query == NULL) {
1143 ngx_log_error(r->log_level, r->log, 0,
1144 "unexpected response for %V", &name);
1145 goto failed;
1148 qident = (rn->query[0] << 8) + rn->query[1];
1150 if (ident != qident) {
1151 ngx_log_error(r->log_level, r->log, 0,
1152 "wrong ident %ui response for %V, expect %ui",
1153 ident, &name, qident);
1154 goto failed;
1157 ngx_resolver_free(r, name.data);
1159 if (code == 0 && nan == 0) {
1160 code = 3; /* NXDOMAIN */
1163 if (code) {
1164 next = rn->waiting;
1165 rn->waiting = NULL;
1167 ngx_queue_remove(&rn->queue);
1169 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
1171 ngx_resolver_free_node(r, rn);
1173 /* unlock name mutex */
1175 while (next) {
1176 ctx = next;
1177 ctx->state = code;
1178 next = ctx->next;
1180 ctx->handler(ctx);
1183 return;
1186 i = ans;
1187 naddrs = 0;
1188 addr = 0;
1189 addrs = NULL;
1190 cname = NULL;
1191 qtype = 0;
1193 for (a = 0; a < nan; a++) {
1195 start = i;
1197 while (i < last) {
1199 if (buf[i] & 0xc0) {
1200 i += 2;
1201 goto found;
1204 if (buf[i] == 0) {
1205 i++;
1206 goto test_length;
1209 i += 1 + buf[i];
1212 goto short_response;
1214 test_length:
1216 if (i - start < 2) {
1217 err = "invalid name in dns response";
1218 goto invalid;
1221 found:
1223 if (i + sizeof(ngx_resolver_an_t) >= last) {
1224 goto short_response;
1227 an = (ngx_resolver_an_t *) &buf[i];
1229 qtype = (an->type_hi << 8) + an->type_lo;
1230 len = (an->len_hi << 8) + an->len_lo;
1232 if (qtype == NGX_RESOLVE_A) {
1234 i += sizeof(ngx_resolver_an_t);
1236 if (i + len > last) {
1237 goto short_response;
1240 addr = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1241 + (buf[i + 2] << 8) + (buf[i + 3]));
1243 naddrs++;
1245 i += len;
1247 } else if (qtype == NGX_RESOLVE_CNAME) {
1248 cname = &buf[i] + sizeof(ngx_resolver_an_t);
1249 i += sizeof(ngx_resolver_an_t) + len;
1251 } else if (qtype == NGX_RESOLVE_DNAME) {
1252 i += sizeof(ngx_resolver_an_t) + len;
1254 } else {
1255 ngx_log_error(r->log_level, r->log, 0,
1256 "unexpected qtype %ui", qtype);
1260 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
1261 "resolver naddrs:%ui cname:%p", naddrs, cname);
1263 if (naddrs) {
1265 if (naddrs == 1) {
1266 rn->u.addr = addr;
1268 } else {
1270 addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
1271 if (addrs == NULL) {
1272 return;
1275 n = 0;
1276 i = ans;
1278 for (a = 0; a < nan; a++) {
1280 for ( ;; ) {
1282 if (buf[i] & 0xc0) {
1283 i += 2;
1284 goto ok;
1287 if (buf[i] == 0) {
1288 i++;
1289 goto ok;
1292 i += 1 + buf[i];
1297 an = (ngx_resolver_an_t *) &buf[i];
1299 qtype = (an->type_hi << 8) + an->type_lo;
1300 len = (an->len_hi << 8) + an->len_lo;
1302 i += sizeof(ngx_resolver_an_t);
1304 if (qtype == NGX_RESOLVE_A) {
1306 addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1307 + (buf[i + 2] << 8) + (buf[i + 3]));
1309 if (n == naddrs) {
1310 break;
1314 i += len;
1317 rn->u.addrs = addrs;
1319 addrs = ngx_resolver_dup(r, rn->u.addrs,
1320 naddrs * sizeof(in_addr_t));
1321 if (addrs == NULL) {
1322 return;
1326 rn->naddrs = (u_short) naddrs;
1328 ngx_queue_remove(&rn->queue);
1330 rn->valid = ngx_time() + r->valid;
1331 rn->expire = ngx_time() + r->expire;
1333 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1335 next = rn->waiting;
1336 rn->waiting = NULL;
1338 /* unlock name mutex */
1340 while (next) {
1341 ctx = next;
1342 ctx->state = NGX_OK;
1343 ctx->naddrs = naddrs;
1344 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
1345 ctx->addr = addr;
1346 next = ctx->next;
1348 ctx->handler(ctx);
1351 if (naddrs > 1) {
1352 ngx_resolver_free(r, addrs);
1355 return;
1357 } else if (cname) {
1359 /* CNAME only */
1361 if (ngx_resolver_copy(r, &name, buf, cname, &buf[last]) != NGX_OK) {
1362 return;
1365 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
1366 "resolver cname:\"%V\"", &name);
1368 ngx_queue_remove(&rn->queue);
1370 rn->cnlen = (u_short) name.len;
1371 rn->u.cname = name.data;
1372 rn->valid = ngx_time() + r->valid;
1373 rn->expire = ngx_time() + r->expire;
1375 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1377 ctx = rn->waiting;
1378 rn->waiting = NULL;
1380 if (ctx) {
1381 ctx->name = name;
1383 (void) ngx_resolve_name_locked(r, ctx);
1386 return;
1389 ngx_log_error(r->log_level, r->log, 0,
1390 "no A or CNAME types in DNS responses, unknown query type: %ui",
1391 qtype);
1392 return;
1394 short_response:
1396 err = "short dns response";
1398 invalid:
1400 /* unlock name mutex */
1402 ngx_log_error(r->log_level, r->log, 0, err);
1404 return;
1406 failed:
1408 /* unlock name mutex */
1410 ngx_resolver_free(r, name.data);
1412 return;
1416 static void
1417 ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
1418 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan)
1420 char *err;
1421 size_t len;
1422 in_addr_t addr;
1423 ngx_int_t digit;
1424 ngx_str_t name;
1425 ngx_uint_t i, mask, qtype, qclass, qident;
1426 ngx_resolver_an_t *an;
1427 ngx_resolver_ctx_t *ctx, *next;
1428 ngx_resolver_node_t *rn;
1430 if (ngx_resolver_copy(r, NULL, buf, &buf[12], &buf[n]) != NGX_OK) {
1431 goto invalid_in_addr_arpa;
1434 addr = 0;
1435 i = 12;
1437 for (mask = 0; mask < 32; mask += 8) {
1438 len = buf[i++];
1440 digit = ngx_atoi(&buf[i], len);
1441 if (digit == NGX_ERROR || digit > 255) {
1442 goto invalid_in_addr_arpa;
1445 addr += digit << mask;
1446 i += len;
1449 if (ngx_strcmp(&buf[i], "\7in-addr\4arpa") != 0) {
1450 goto invalid_in_addr_arpa;
1453 /* lock addr mutex */
1455 rn = ngx_resolver_lookup_addr(r, addr);
1457 if (rn == NULL || rn->query == NULL) {
1458 ngx_log_error(r->log_level, r->log, 0,
1459 "unexpected response for %ud.%ud.%ud.%ud",
1460 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1461 (addr >> 8) & 0xff, addr & 0xff);
1462 goto failed;
1465 qident = (rn->query[0] << 8) + rn->query[1];
1467 if (ident != qident) {
1468 ngx_log_error(r->log_level, r->log, 0,
1469 "wrong ident %ui response for %ud.%ud.%ud.%ud, expect %ui",
1470 ident, (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1471 (addr >> 8) & 0xff, addr & 0xff, qident);
1472 goto failed;
1475 if (code == 0 && nan == 0) {
1476 code = 3; /* NXDOMAIN */
1479 if (code) {
1480 next = rn->waiting;
1481 rn->waiting = NULL;
1483 ngx_queue_remove(&rn->queue);
1485 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
1487 ngx_resolver_free_node(r, rn);
1489 /* unlock addr mutex */
1491 while (next) {
1492 ctx = next;
1493 ctx->state = code;
1494 next = ctx->next;
1496 ctx->handler(ctx);
1499 return;
1502 i += sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t);
1504 if (i + 2 + sizeof(ngx_resolver_an_t) > (ngx_uint_t) n) {
1505 goto short_response;
1508 /* compression pointer to "XX.XX.XX.XX.in-addr.arpa */
1510 if (buf[i] != 0xc0 || buf[i + 1] != 0x0c) {
1511 err = "invalid in-addr.arpa name in DNS response";
1512 goto invalid;
1515 an = (ngx_resolver_an_t *) &buf[i + 2];
1517 qtype = (an->type_hi << 8) + an->type_lo;
1518 qclass = (an->class_hi << 8) + an->class_lo;
1519 len = (an->len_hi << 8) + an->len_lo;
1521 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
1522 "resolver qt:%ui cl:%ui len:%uz", qtype, qclass, len);
1524 i += 2 + sizeof(ngx_resolver_an_t);
1526 if (i + len > (ngx_uint_t) n) {
1527 goto short_response;
1530 if (ngx_resolver_copy(r, &name, buf, &buf[i], &buf[n]) != NGX_OK) {
1531 return;
1534 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name);
1536 if (name.len != (size_t) rn->nlen
1537 || ngx_strncmp(name.data, rn->name, name.len) != 0)
1539 if (rn->nlen) {
1540 ngx_resolver_free(r, rn->name);
1543 rn->nlen = (u_short) name.len;
1544 rn->name = name.data;
1546 name.data = ngx_resolver_dup(r, rn->name, name.len);
1547 if (name.data == NULL) {
1548 goto failed;
1552 ngx_queue_remove(&rn->queue);
1554 rn->valid = ngx_time() + r->valid;
1555 rn->expire = ngx_time() + r->expire;
1557 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
1559 next = rn->waiting;
1560 rn->waiting = NULL;
1562 /* unlock addr mutex */
1564 while (next) {
1565 ctx = next;
1566 ctx->state = NGX_OK;
1567 ctx->name = name;
1568 next = ctx->next;
1570 ctx->handler(ctx);
1573 ngx_resolver_free(r, name.data);
1575 return;
1577 invalid_in_addr_arpa:
1579 ngx_log_error(r->log_level, r->log, 0,
1580 "invalid in-addr.arpa name in DNS response");
1581 return;
1583 short_response:
1585 err = "short DNS response";
1587 invalid:
1589 /* unlock addr mutex */
1591 ngx_log_error(r->log_level, r->log, 0, err);
1593 return;
1595 failed:
1597 /* unlock addr mutex */
1599 return;
1603 static ngx_resolver_node_t *
1604 ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
1606 ngx_int_t rc;
1607 ngx_rbtree_node_t *node, *sentinel;
1608 ngx_resolver_node_t *rn;
1610 node = r->name_rbtree.root;
1611 sentinel = r->name_rbtree.sentinel;
1613 while (node != sentinel) {
1615 if (hash < node->key) {
1616 node = node->left;
1617 continue;
1620 if (hash > node->key) {
1621 node = node->right;
1622 continue;
1625 /* hash == node->key */
1627 do {
1628 rn = (ngx_resolver_node_t *) node;
1630 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
1632 if (rc == 0) {
1633 return rn;
1636 node = (rc < 0) ? node->left : node->right;
1638 } while (node != sentinel && hash == node->key);
1640 break;
1643 /* not found */
1645 return NULL;
1649 static ngx_resolver_node_t *
1650 ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr)
1652 ngx_rbtree_node_t *node, *sentinel;
1654 node = r->addr_rbtree.root;
1655 sentinel = r->addr_rbtree.sentinel;
1657 while (node != sentinel) {
1659 if (addr < node->key) {
1660 node = node->left;
1661 continue;
1664 if (addr > node->key) {
1665 node = node->right;
1666 continue;
1669 /* addr == node->key */
1671 return (ngx_resolver_node_t *) node;
1674 /* not found */
1676 return NULL;
1680 static void
1681 ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
1682 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1684 ngx_rbtree_node_t **p;
1685 ngx_resolver_node_t *rn, *rn_temp;
1687 for ( ;; ) {
1689 if (node->key < temp->key) {
1691 p = &temp->left;
1693 } else if (node->key > temp->key) {
1695 p = &temp->right;
1697 } else { /* node->key == temp->key */
1699 rn = (ngx_resolver_node_t *) node;
1700 rn_temp = (ngx_resolver_node_t *) temp;
1702 p = (ngx_memn2cmp(rn->name, rn_temp->name, rn->nlen, rn_temp->nlen)
1703 < 0) ? &temp->left : &temp->right;
1706 if (*p == sentinel) {
1707 break;
1710 temp = *p;
1713 *p = node;
1714 node->parent = temp;
1715 node->left = sentinel;
1716 node->right = sentinel;
1717 ngx_rbt_red(node);
1721 static ngx_int_t
1722 ngx_resolver_create_name_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1724 u_char *p, *s;
1725 size_t len, nlen;
1726 ngx_uint_t ident;
1727 ngx_resolver_qs_t *qs;
1728 ngx_resolver_query_t *query;
1730 nlen = ctx->name.len ? (1 + ctx->name.len + 1) : 1;
1732 len = sizeof(ngx_resolver_query_t) + nlen + sizeof(ngx_resolver_qs_t);
1734 p = ngx_resolver_alloc(ctx->resolver, len);
1735 if (p == NULL) {
1736 return NGX_ERROR;
1739 rn->qlen = (u_short) len;
1740 rn->query = p;
1742 query = (ngx_resolver_query_t *) p;
1744 ident = ngx_random();
1746 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->resolver->log, 0,
1747 "resolve: \"%V\" %i", &ctx->name, ident & 0xffff);
1749 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
1750 query->ident_lo = (u_char) (ident & 0xff);
1752 /* recursion query */
1753 query->flags_hi = 1; query->flags_lo = 0;
1755 /* one question */
1756 query->nqs_hi = 0; query->nqs_lo = 1;
1757 query->nan_hi = 0; query->nan_lo = 0;
1758 query->nns_hi = 0; query->nns_lo = 0;
1759 query->nar_hi = 0; query->nar_lo = 0;
1761 p += sizeof(ngx_resolver_query_t) + nlen;
1763 qs = (ngx_resolver_qs_t *) p;
1765 /* query type */
1766 qs->type_hi = 0; qs->type_lo = (u_char) ctx->type;
1768 /* IP query class */
1769 qs->class_hi = 0; qs->class_lo = 1;
1771 /* convert "www.example.com" to "\3www\7example\3com\0" */
1773 len = 0;
1774 p--;
1775 *p-- = '\0';
1777 for (s = ctx->name.data + ctx->name.len - 1; s >= ctx->name.data; s--) {
1778 if (*s != '.') {
1779 *p = *s;
1780 len++;
1782 } else {
1783 if (len == 0) {
1784 return NGX_DECLINED;
1787 *p = (u_char) len;
1788 len = 0;
1791 p--;
1794 *p = (u_char) len;
1796 return NGX_OK;
1800 /* AF_INET only */
1802 static ngx_int_t
1803 ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1805 u_char *p, *d;
1806 size_t len;
1807 ngx_int_t n;
1808 ngx_uint_t ident;
1809 ngx_resolver_query_t *query;
1811 len = sizeof(ngx_resolver_query_t)
1812 + sizeof(".255.255.255.255.in-addr.arpa.") - 1
1813 + sizeof(ngx_resolver_qs_t);
1815 p = ngx_resolver_alloc(ctx->resolver, len);
1816 if (p == NULL) {
1817 return NGX_ERROR;
1820 rn->query = p;
1821 query = (ngx_resolver_query_t *) p;
1823 ident = ngx_random();
1825 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
1826 query->ident_lo = (u_char) (ident & 0xff);
1828 /* recursion query */
1829 query->flags_hi = 1; query->flags_lo = 0;
1831 /* one question */
1832 query->nqs_hi = 0; query->nqs_lo = 1;
1833 query->nan_hi = 0; query->nan_lo = 0;
1834 query->nns_hi = 0; query->nns_lo = 0;
1835 query->nar_hi = 0; query->nar_lo = 0;
1837 p += sizeof(ngx_resolver_query_t);
1839 for (n = 0; n < 32; n += 8){
1840 d = ngx_sprintf(&p[1], "%ud", (ctx->addr >> n) & 0xff);
1841 *p = (u_char) (d - &p[1]);
1842 p = d;
1845 /* query type "PTR", IP query class */
1846 ngx_memcpy(p, "\7in-addr\4arpa\0\0\14\0\1", 18);
1848 rn->qlen = (u_short)
1849 (p + sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t)
1850 - rn->query);
1852 return NGX_OK;
1856 static ngx_int_t
1857 ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src,
1858 u_char *last)
1860 char *err;
1861 u_char *p, *dst;
1862 ssize_t len;
1863 ngx_uint_t i, n;
1865 p = src;
1866 len = -1;
1869 * compression pointers allow to create endless loop, so we set limit;
1870 * 128 pointers should be enough to store 255-byte name
1873 for (i = 0; i < 128; i++) {
1874 n = *p++;
1876 if (n == 0) {
1877 goto done;
1880 if (n & 0xc0) {
1881 n = ((n & 0x3f) << 8) + *p;
1882 p = &buf[n];
1884 } else {
1885 len += 1 + n;
1886 p = &p[n];
1889 if (p >= last) {
1890 err = "name is out of response";
1891 goto invalid;
1895 err = "compression pointers loop";
1897 invalid:
1899 ngx_log_error(r->log_level, r->log, 0, err);
1901 return NGX_ERROR;
1903 done:
1905 if (name == NULL) {
1906 return NGX_OK;
1909 if (len == -1) {
1910 name->len = 0;
1911 name->data = NULL;
1912 return NGX_OK;
1915 dst = ngx_resolver_alloc(r, len);
1916 if (dst == NULL) {
1917 return NGX_ERROR;
1920 name->data = dst;
1922 n = *src++;
1924 for ( ;; ) {
1925 if (n != 0xc0) {
1926 ngx_memcpy(dst, src, n);
1927 dst += n;
1928 src += n;
1930 n = *src++;
1932 if (n != 0) {
1933 *dst++ = '.';
1936 } else {
1937 n = ((n & 0x3f) << 8) + *src;
1938 src = &buf[n];
1940 n = *src++;
1943 if (n == 0) {
1944 name->len = dst - name->data;
1945 return NGX_OK;
1951 static void
1952 ngx_resolver_timeout_handler(ngx_event_t *ev)
1954 ngx_resolver_ctx_t *ctx;
1956 ctx = ev->data;
1958 ctx->state = NGX_RESOLVE_TIMEDOUT;
1960 ctx->handler(ctx);
1964 static void
1965 ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn)
1967 /* lock alloc mutex */
1969 if (rn->query) {
1970 ngx_resolver_free_locked(r, rn->query);
1973 if (rn->name) {
1974 ngx_resolver_free_locked(r, rn->name);
1977 if (rn->cnlen) {
1978 ngx_resolver_free_locked(r, rn->u.cname);
1981 if (rn->naddrs > 1) {
1982 ngx_resolver_free_locked(r, rn->u.addrs);
1985 ngx_resolver_free_locked(r, rn);
1987 /* unlock alloc mutex */
1991 static void *
1992 ngx_resolver_alloc(ngx_resolver_t *r, size_t size)
1994 u_char *p;
1996 /* lock alloc mutex */
1998 p = ngx_alloc(size, r->log);
2000 /* unlock alloc mutex */
2002 return p;
2006 static void *
2007 ngx_resolver_calloc(ngx_resolver_t *r, size_t size)
2009 u_char *p;
2011 p = ngx_resolver_alloc(r, size);
2013 if (p) {
2014 ngx_memzero(p, size);
2017 return p;
2021 static void
2022 ngx_resolver_free(ngx_resolver_t *r, void *p)
2024 /* lock alloc mutex */
2026 ngx_free(p);
2028 /* unlock alloc mutex */
2032 static void
2033 ngx_resolver_free_locked(ngx_resolver_t *r, void *p)
2035 ngx_free(p);
2039 static void *
2040 ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size)
2042 void *dst;
2044 dst = ngx_resolver_alloc(r, size);
2046 if (dst == NULL) {
2047 return dst;
2050 ngx_memcpy(dst, src, size);
2052 return dst;
2056 char *
2057 ngx_resolver_strerror(ngx_int_t err)
2059 static char *errors[] = {
2060 "Format error", /* FORMERR */
2061 "Server failure", /* SERVFAIL */
2062 "Host not found", /* NXDOMAIN */
2063 "Unimplemented", /* NOTIMP */
2064 "Operation refused" /* REFUSED */
2067 if (err > 0 && err < 6) {
2068 return errors[err - 1];
2071 if (err == NGX_RESOLVE_TIMEDOUT) {
2072 return "Operation timed out";
2075 return "Unknown error";
2079 static u_char *
2080 ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len)
2082 u_char *p;
2083 ngx_udp_connection_t *uc;
2085 p = buf;
2087 if (log->action) {
2088 p = ngx_snprintf(buf, len, " while %s", log->action);
2089 len -= p - buf;
2092 uc = log->data;
2094 if (uc) {
2095 p = ngx_snprintf(p, len, ", resolver: %V", &uc->server);
2098 return p;
2102 ngx_int_t
2103 ngx_udp_connect(ngx_udp_connection_t *uc)
2105 int rc;
2106 ngx_int_t event;
2107 ngx_event_t *rev, *wev;
2108 ngx_socket_t s;
2109 ngx_connection_t *c;
2111 s = ngx_socket(AF_INET, SOCK_DGRAM, 0);
2113 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s);
2115 if (s == -1) {
2116 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
2117 ngx_socket_n " failed");
2118 return NGX_ERROR;
2121 c = ngx_get_connection(s, &uc->log);
2123 if (c == NULL) {
2124 if (ngx_close_socket(s) == -1) {
2125 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
2126 ngx_close_socket_n "failed");
2129 return NGX_ERROR;
2132 if (ngx_nonblocking(s) == -1) {
2133 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
2134 ngx_nonblocking_n " failed");
2136 ngx_free_connection(c);
2138 if (ngx_close_socket(s) == -1) {
2139 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
2140 ngx_close_socket_n " failed");
2143 return NGX_ERROR;
2146 rev = c->read;
2147 wev = c->write;
2149 rev->log = &uc->log;
2150 wev->log = &uc->log;
2152 uc->connection = c;
2154 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
2156 #if (NGX_THREADS)
2158 /* TODO: lock event when call completion handler */
2160 rev->lock = &c->lock;
2161 wev->lock = &c->lock;
2162 rev->own_lock = &c->lock;
2163 wev->own_lock = &c->lock;
2165 #endif
2167 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &uc->log, 0,
2168 "connect to %V, fd:%d #%d", &uc->server, s, c->number);
2170 rc = connect(s, uc->sockaddr, uc->socklen);
2172 /* TODO: aio, iocp */
2174 if (rc == -1) {
2175 ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno,
2176 "connect() failed");
2178 return NGX_ERROR;
2181 /* UDP sockets are always ready to write */
2182 wev->ready = 1;
2184 if (ngx_add_event) {
2186 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ?
2187 /* kqueue, epoll */ NGX_CLEAR_EVENT:
2188 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT;
2189 /* eventport event type has no meaning: oneshot only */
2191 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
2192 return NGX_ERROR;
2195 } else {
2196 /* rtsig */
2198 if (ngx_add_conn(c) == NGX_ERROR) {
2199 return NGX_ERROR;
2203 return NGX_OK;