blob: 222b3d718ff77fa5b9a094307e7e5c97099679c6 [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;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +040029} ngx_resolver_hdr_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);
Maxim Dounin6a0f47e2012-09-28 18:28:38 +000091static in_addr_t *ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src,
92 ngx_uint_t n);
Igor Sysoev089b2fd2010-01-11 11:01:02 +000093static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +000094
95
Igor Sysoevcb4d5382007-11-23 17:13:26 +000096ngx_resolver_t *
Igor Sysoev62071812011-10-24 16:09:05 +000097ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
Igor Sysoevcb4d5382007-11-23 17:13:26 +000098{
Ruslan Ermilovbec516b2011-11-16 13:11:39 +000099 ngx_str_t s;
Igor Sysoev62071812011-10-24 16:09:05 +0000100 ngx_url_t u;
Ruslan Ermilov2c49af82012-06-18 12:46:05 +0000101 ngx_uint_t i, j;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000102 ngx_resolver_t *r;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000103 ngx_pool_cleanup_t *cln;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000104 ngx_udp_connection_t *uc;
105
Igor Sysoevdfd55112008-03-04 10:42:05 +0000106 cln = ngx_pool_cleanup_add(cf->pool, 0);
Igor Sysoev4491dae2008-02-28 20:38:34 +0000107 if (cln == NULL) {
108 return NULL;
109 }
110
111 cln->handler = ngx_resolver_cleanup;
112
Igor Sysoevdfd55112008-03-04 10:42:05 +0000113 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000114 if (r == NULL) {
115 return NULL;
116 }
117
Igor Sysoev4491dae2008-02-28 20:38:34 +0000118 cln->data = r;
119
Igor Sysoevdfd55112008-03-04 10:42:05 +0000120 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000121 if (r->event == NULL) {
122 return NULL;
123 }
124
Igor Sysoev826c02e2007-12-03 12:17:26 +0000125 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel,
126 ngx_resolver_rbtree_insert_value);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000127
Igor Sysoev826c02e2007-12-03 12:17:26 +0000128 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel,
129 ngx_rbtree_insert_value);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000130
Igor Sysoeva9d3a0d2007-12-03 11:21:19 +0000131 ngx_queue_init(&r->name_resend_queue);
132 ngx_queue_init(&r->addr_resend_queue);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000133
Igor Sysoeva9d3a0d2007-12-03 11:21:19 +0000134 ngx_queue_init(&r->name_expire_queue);
135 ngx_queue_init(&r->addr_expire_queue);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000136
137 r->event->handler = ngx_resolver_resend_handler;
138 r->event->data = r;
Igor Sysoev0cd76ea2009-04-30 13:53:42 +0000139 r->event->log = &cf->cycle->new_log;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000140 r->ident = -1;
141
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000142 r->resend_timeout = 5;
143 r->expire = 30;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000144 r->valid = 0;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000145
Igor Sysoev0cd76ea2009-04-30 13:53:42 +0000146 r->log = &cf->cycle->new_log;
Igor Sysoev49ac2b22010-09-27 11:23:45 +0000147 r->log_level = NGX_LOG_ERR;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000148
Maxim Dounin42a75bb2012-08-06 10:48:09 +0000149 if (n) {
150 if (ngx_array_init(&r->udp_connections, cf->pool, n,
151 sizeof(ngx_udp_connection_t))
152 != NGX_OK)
153 {
154 return NULL;
155 }
156 }
157
Igor Sysoev62071812011-10-24 16:09:05 +0000158 for (i = 0; i < n; i++) {
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000159 if (ngx_strncmp(names[i].data, "valid=", 6) == 0) {
160 s.len = names[i].len - 6;
161 s.data = names[i].data + 6;
162
163 r->valid = ngx_parse_time(&s, 1);
164
Maxim Dounin9f38b202012-02-13 15:41:11 +0000165 if (r->valid == (time_t) NGX_ERROR) {
Ruslan Ermilovbec516b2011-11-16 13:11:39 +0000166 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
167 "invalid parameter: %V", &names[i]);
168 return NULL;
169 }
170
171 continue;
172 }
173
Igor Sysoev62071812011-10-24 16:09:05 +0000174 ngx_memzero(&u, sizeof(ngx_url_t));
175
Ruslan Ermilovac7f7042012-06-04 14:23:27 +0000176 u.url = names[i];
177 u.default_port = 53;
Igor Sysoev62071812011-10-24 16:09:05 +0000178
Ruslan Ermilovac7f7042012-06-04 14:23:27 +0000179 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
Ruslan Ermilovd2e005c2012-05-22 13:12:14 +0000180 if (u.err) {
181 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
182 "%s in resolver \"%V\"",
Ruslan Ermilovac7f7042012-06-04 14:23:27 +0000183 u.err, &u.url);
Ruslan Ermilovd2e005c2012-05-22 13:12:14 +0000184 }
185
Igor Sysoev62071812011-10-24 16:09:05 +0000186 return NULL;
187 }
188
Ruslan Ermilov2c49af82012-06-18 12:46:05 +0000189 uc = ngx_array_push_n(&r->udp_connections, u.naddrs);
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000190 if (uc == NULL) {
191 return NULL;
192 }
193
Ruslan Ermilov2c49af82012-06-18 12:46:05 +0000194 ngx_memzero(uc, u.naddrs * sizeof(ngx_udp_connection_t));
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000195
Ruslan Ermilov2c49af82012-06-18 12:46:05 +0000196 for (j = 0; j < u.naddrs; j++) {
197 uc[j].sockaddr = u.addrs[j].sockaddr;
198 uc[j].socklen = u.addrs[j].socklen;
199 uc[j].server = u.addrs[j].name;
200 }
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000201 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000202
203 return r;
204}
205
206
Igor Sysoev4491dae2008-02-28 20:38:34 +0000207static void
208ngx_resolver_cleanup(void *data)
209{
210 ngx_resolver_t *r = data;
211
Igor Sysoev62071812011-10-24 16:09:05 +0000212 ngx_uint_t i;
213 ngx_udp_connection_t *uc;
214
Igor Sysoev4491dae2008-02-28 20:38:34 +0000215 if (r) {
216 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
217 "cleanup resolver");
218
219 ngx_resolver_cleanup_tree(r, &r->name_rbtree);
220
221 ngx_resolver_cleanup_tree(r, &r->addr_rbtree);
222
223 if (r->event) {
224 ngx_free(r->event);
225 }
226
Igor Sysoev4491dae2008-02-28 20:38:34 +0000227
Igor Sysoev62071812011-10-24 16:09:05 +0000228 uc = r->udp_connections.elts;
229
230 for (i = 0; i < r->udp_connections.nelts; i++) {
231 if (uc[i].connection) {
232 ngx_close_connection(uc[i].connection);
233 }
Igor Sysoev4491dae2008-02-28 20:38:34 +0000234 }
235
236 ngx_free(r);
237 }
238}
239
240
241static void
242ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree)
243{
244 ngx_resolver_ctx_t *ctx, *next;
245 ngx_resolver_node_t *rn;
246
247 while (tree->root != tree->sentinel) {
248
249 rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel);
250
251 ngx_queue_remove(&rn->queue);
252
253 for (ctx = rn->waiting; ctx; ctx = next) {
Igor Sysoev433608c2008-05-14 07:54:52 +0000254 next = ctx->next;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000255
256 if (ctx->event) {
257 ngx_resolver_free(r, ctx->event);
258 }
259
260 ngx_resolver_free(r, ctx);
261 }
262
263 ngx_rbtree_delete(tree, &rn->node);
264
265 ngx_resolver_free_node(r, rn);
266 }
267}
268
269
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000270ngx_resolver_ctx_t *
271ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp)
272{
273 in_addr_t addr;
274 ngx_resolver_ctx_t *ctx;
275
276 if (temp) {
277 addr = ngx_inet_addr(temp->name.data, temp->name.len);
278
279 if (addr != INADDR_NONE) {
280 temp->resolver = r;
281 temp->state = NGX_OK;
282 temp->naddrs = 1;
283 temp->addrs = &temp->addr;
284 temp->addr = addr;
285 temp->quick = 1;
286
287 return temp;
288 }
289 }
290
Igor Sysoev62071812011-10-24 16:09:05 +0000291 if (r->udp_connections.nelts == 0) {
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000292 return NGX_NO_RESOLVER;
293 }
294
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000295 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t));
296
297 if (ctx) {
298 ctx->resolver = r;
299 }
300
301 return ctx;
302}
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000303
304
305ngx_int_t
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000306ngx_resolve_name(ngx_resolver_ctx_t *ctx)
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000307{
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000308 ngx_int_t rc;
309 ngx_resolver_t *r;
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000310
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000311 r = ctx->resolver;
312
313 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
314 "resolve: \"%V\"", &ctx->name);
315
316 if (ctx->quick) {
317 ctx->handler(ctx);
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000318 return NGX_OK;
319 }
320
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000321 /* lock name mutex */
322
323 rc = ngx_resolve_name_locked(r, ctx);
324
325 if (rc == NGX_OK) {
326 return NGX_OK;
327 }
328
329 /* unlock name mutex */
330
331 if (rc == NGX_AGAIN) {
332 return NGX_OK;
333 }
334
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000335 /* NGX_ERROR */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000336
337 if (ctx->event) {
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000338 ngx_resolver_free(r, ctx->event);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000339 }
340
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000341 ngx_resolver_free(r, ctx);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000342
343 return NGX_ERROR;
344}
345
346
347void
348ngx_resolve_name_done(ngx_resolver_ctx_t *ctx)
349{
350 uint32_t hash;
351 ngx_resolver_t *r;
352 ngx_resolver_ctx_t *w, **p;
353 ngx_resolver_node_t *rn;
354
355 r = ctx->resolver;
356
357 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
358 "resolve name done: %i", ctx->state);
359
360 if (ctx->quick) {
361 return;
362 }
363
364 if (ctx->event && ctx->event->timer_set) {
365 ngx_del_timer(ctx->event);
366 }
367
368 /* lock name mutex */
369
370 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
371
372 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
373
374 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
375
376 if (rn) {
377 p = &rn->waiting;
378 w = rn->waiting;
379
380 while (w) {
381 if (w == ctx) {
382 *p = w->next;
383
384 goto done;
385 }
386
387 p = &w->next;
388 w = w->next;
389 }
390 }
391
392 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
393 "could not cancel %V resolving", &ctx->name);
394 }
395
396done:
397
398 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue);
399
400 /* unlock name mutex */
401
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000402 /* lock alloc mutex */
403
404 if (ctx->event) {
405 ngx_resolver_free_locked(r, ctx->event);
406 }
407
408 ngx_resolver_free_locked(r, ctx);
409
410 /* unlock alloc mutex */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000411}
412
413
414/* NGX_RESOLVE_A only */
415
416static ngx_int_t
417ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
418{
419 uint32_t hash;
420 in_addr_t addr, *addrs;
Igor Sysoev09b199c2008-04-09 14:45:39 +0000421 ngx_int_t rc;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000422 ngx_uint_t naddrs;
423 ngx_resolver_ctx_t *next;
424 ngx_resolver_node_t *rn;
425
426 hash = ngx_crc32_short(ctx->name.data, ctx->name.len);
427
428 rn = ngx_resolver_lookup_name(r, &ctx->name, hash);
429
430 if (rn) {
431
432 if (rn->valid >= ngx_time()) {
433
434 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
435
436 ngx_queue_remove(&rn->queue);
437
438 rn->expire = ngx_time() + r->expire;
439
440 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
441
442 naddrs = rn->naddrs;
443
444 if (naddrs) {
445
446 /* NGX_RESOLVE_A answer */
447
448 if (naddrs != 1) {
449 addr = 0;
Maxim Dounin6a0f47e2012-09-28 18:28:38 +0000450 addrs = ngx_resolver_rotate(r, rn->u.addrs, naddrs);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000451 if (addrs == NULL) {
452 return NGX_ERROR;
453 }
454
455 } else {
456 addr = rn->u.addr;
457 addrs = NULL;
458 }
459
460 ctx->next = rn->waiting;
461 rn->waiting = NULL;
462
463 /* unlock name mutex */
464
465 do {
466 ctx->state = NGX_OK;
467 ctx->naddrs = naddrs;
468 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
469 ctx->addr = addr;
470 next = ctx->next;
471
472 ctx->handler(ctx);
473
474 ctx = next;
475 } while (ctx);
476
477 if (addrs) {
478 ngx_resolver_free(r, addrs);
479 }
480
481 return NGX_OK;
482 }
483
484 /* NGX_RESOLVE_CNAME */
485
Igor Sysoeva8372d82008-04-12 07:29:20 +0000486 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000487
Igor Sysoeva8372d82008-04-12 07:29:20 +0000488 ctx->name.len = rn->cnlen;
489 ctx->name.data = rn->u.cname;
490
491 return ngx_resolve_name_locked(r, ctx);
492 }
493
494 ctx->next = rn->waiting;
495 rn->waiting = NULL;
496
497 /* unlock name mutex */
498
499 do {
500 ctx->state = NGX_RESOLVE_NXDOMAIN;
501 next = ctx->next;
502
503 ctx->handler(ctx);
504
505 ctx = next;
506 } while (ctx);
507
508 return NGX_OK;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000509 }
510
511 if (rn->waiting) {
512
513 ctx->next = rn->waiting;
514 rn->waiting = ctx;
Igor Sysoev64bfa872009-11-09 17:45:56 +0000515 ctx->state = NGX_AGAIN;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000516
517 return NGX_AGAIN;
518 }
519
520 ngx_queue_remove(&rn->queue);
521
522 /* lock alloc mutex */
523
Maxim Dounin7d863c02012-05-14 09:13:45 +0000524 if (rn->query) {
525 ngx_resolver_free_locked(r, rn->query);
526 rn->query = NULL;
527 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000528
529 if (rn->cnlen) {
530 ngx_resolver_free_locked(r, rn->u.cname);
531 }
532
533 if (rn->naddrs > 1) {
534 ngx_resolver_free_locked(r, rn->u.addrs);
535 }
536
537 /* unlock alloc mutex */
538
539 } else {
540
541 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
542 if (rn == NULL) {
543 return NGX_ERROR;
544 }
545
546 rn->name = ngx_resolver_dup(r, ctx->name.data, ctx->name.len);
547 if (rn->name == NULL) {
548 ngx_resolver_free(r, rn);
549 return NGX_ERROR;
550 }
551
552 rn->node.key = hash;
553 rn->nlen = (u_short) ctx->name.len;
Igor Sysoev949aea42008-04-09 14:26:08 +0000554 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000555
556 ngx_rbtree_insert(&r->name_rbtree, &rn->node);
557 }
558
Igor Sysoev09b199c2008-04-09 14:45:39 +0000559 rc = ngx_resolver_create_name_query(rn, ctx);
560
561 if (rc == NGX_ERROR) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000562 goto failed;
563 }
564
Igor Sysoev09b199c2008-04-09 14:45:39 +0000565 if (rc == NGX_DECLINED) {
566 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
567
568 ngx_resolver_free(r, rn->query);
569 ngx_resolver_free(r, rn->name);
570 ngx_resolver_free(r, rn);
571
572 ctx->state = NGX_RESOLVE_NXDOMAIN;
573 ctx->handler(ctx);
574
575 return NGX_OK;
576 }
577
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000578 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
579 goto failed;
580 }
581
582 if (ctx->event == NULL) {
583 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
584 if (ctx->event == NULL) {
585 goto failed;
586 }
587
588 ctx->event->handler = ngx_resolver_timeout_handler;
589 ctx->event->data = ctx;
590 ctx->event->log = r->log;
591 ctx->ident = -1;
592
593 ngx_add_timer(ctx->event, ctx->timeout);
594 }
595
596 if (ngx_queue_empty(&r->name_resend_queue)) {
597 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
598 }
599
600 rn->expire = ngx_time() + r->resend_timeout;
601
602 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue);
603
604 rn->cnlen = 0;
605 rn->naddrs = 0;
606 rn->valid = 0;
607 rn->waiting = ctx;
608
609 ctx->state = NGX_AGAIN;
610
611 return NGX_AGAIN;
612
613failed:
614
615 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
616
Igor Sysoev949aea42008-04-09 14:26:08 +0000617 if (rn->query) {
618 ngx_resolver_free(r, rn->query);
619 }
620
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000621 ngx_resolver_free(r, rn->name);
622
623 ngx_resolver_free(r, rn);
624
625 return NGX_ERROR;
626}
627
628
629ngx_int_t
630ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
631{
Igor Sysoev92588722009-01-29 14:35:23 +0000632 u_char *name;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000633 ngx_resolver_t *r;
634 ngx_resolver_node_t *rn;
635
636 r = ctx->resolver;
637
638 ctx->addr = ntohl(ctx->addr);
639
640 /* lock addr mutex */
641
642 rn = ngx_resolver_lookup_addr(r, ctx->addr);
643
644 if (rn) {
645
646 if (rn->valid >= ngx_time()) {
647
648 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
649
650 ngx_queue_remove(&rn->queue);
651
652 rn->expire = ngx_time() + r->expire;
653
654 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
655
Igor Sysoev92588722009-01-29 14:35:23 +0000656 name = ngx_resolver_dup(r, rn->name, rn->nlen);
657 if (name == NULL) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000658 goto failed;
659 }
660
Igor Sysoev92588722009-01-29 14:35:23 +0000661 ctx->name.len = rn->nlen;
662 ctx->name.data = name;
663
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000664 /* unlock addr mutex */
665
666 ctx->state = NGX_OK;
667
668 ctx->handler(ctx);
669
Igor Sysoev92588722009-01-29 14:35:23 +0000670 ngx_resolver_free(r, name);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000671
672 return NGX_OK;
673 }
674
675 if (rn->waiting) {
676
677 ctx->next = rn->waiting;
678 rn->waiting = ctx;
Igor Sysoev64bfa872009-11-09 17:45:56 +0000679 ctx->state = NGX_AGAIN;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000680
Igor Sysoev8ef386e2009-01-31 20:33:01 +0000681 /* unlock addr mutex */
682
683 return NGX_OK;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000684 }
685
686 ngx_queue_remove(&rn->queue);
687
688 ngx_resolver_free(r, rn->query);
Igor Sysoev949aea42008-04-09 14:26:08 +0000689 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000690
691 } else {
692 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
693 if (rn == NULL) {
694 goto failed;
695 }
696
697 rn->node.key = ctx->addr;
Igor Sysoev949aea42008-04-09 14:26:08 +0000698 rn->query = NULL;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000699
700 ngx_rbtree_insert(&r->addr_rbtree, &rn->node);
701 }
702
703 if (ngx_resolver_create_addr_query(rn, ctx) != NGX_OK) {
704 goto failed;
705 }
706
707 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
708 goto failed;
709 }
710
711 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
712 if (ctx->event == NULL) {
713 goto failed;
714 }
715
716 ctx->event->handler = ngx_resolver_timeout_handler;
717 ctx->event->data = ctx;
718 ctx->event->log = r->log;
719 ctx->ident = -1;
720
721 ngx_add_timer(ctx->event, ctx->timeout);
722
723 if (ngx_queue_empty(&r->addr_resend_queue)) {
724 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
725 }
726
727 rn->expire = ngx_time() + r->resend_timeout;
728
729 ngx_queue_insert_head(&r->addr_resend_queue, &rn->queue);
730
731 rn->cnlen = 0;
732 rn->naddrs = 0;
733 rn->name = NULL;
734 rn->nlen = 0;
735 rn->valid = 0;
736 rn->waiting = ctx;
737
738 /* unlock addr mutex */
739
740 ctx->state = NGX_AGAIN;
741
742 return NGX_OK;
743
744failed:
745
746 if (rn) {
747 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
748
Igor Sysoev949aea42008-04-09 14:26:08 +0000749 if (rn->query) {
750 ngx_resolver_free(r, rn->query);
751 }
752
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000753 ngx_resolver_free(r, rn);
754 }
755
756 /* unlock addr mutex */
757
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000758 if (ctx->event) {
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000759 ngx_resolver_free(r, ctx->event);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000760 }
761
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000762 ngx_resolver_free(r, ctx);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000763
764 return NGX_ERROR;
765}
766
767
768void
769ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
770{
771 in_addr_t addr;
772 ngx_resolver_t *r;
773 ngx_resolver_ctx_t *w, **p;
774 ngx_resolver_node_t *rn;
775
776 r = ctx->resolver;
777
778 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
779 "resolve addr done: %i", ctx->state);
780
781 if (ctx->event && ctx->event->timer_set) {
782 ngx_del_timer(ctx->event);
783 }
784
785 /* lock addr mutex */
786
787 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
788
789 rn = ngx_resolver_lookup_addr(r, ctx->addr);
790
791 if (rn) {
792 p = &rn->waiting;
793 w = rn->waiting;
794
795 while (w) {
796 if (w == ctx) {
797 *p = w->next;
798
799 goto done;
800 }
801
802 p = &w->next;
803 w = w->next;
804 }
805 }
806
807 addr = ntohl(ctx->addr);
808
809 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
810 "could not cancel %ud.%ud.%ud.%ud resolving",
811 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
812 (addr >> 8) & 0xff, addr & 0xff);
813 }
814
815done:
816
817 ngx_resolver_expire(r, &r->addr_rbtree, &r->addr_expire_queue);
818
819 /* unlock addr mutex */
820
Igor Sysoev7c4cf272008-02-28 20:09:39 +0000821 /* lock alloc mutex */
822
823 if (ctx->event) {
824 ngx_resolver_free_locked(r, ctx->event);
825 }
826
827 ngx_resolver_free_locked(r, ctx);
828
829 /* unlock alloc mutex */
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000830}
831
832
833static void
834ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
835{
836 time_t now;
837 ngx_uint_t i;
838 ngx_queue_t *q;
839 ngx_resolver_node_t *rn;
840
841 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire");
842
843 now = ngx_time();
844
845 for (i = 0; i < 2; i++) {
846 if (ngx_queue_empty(queue)) {
847 return;
848 }
849
850 q = ngx_queue_last(queue);
851
852 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
853
854 if (now <= rn->expire) {
855 return;
856 }
857
Igor Sysoevd02661a2007-12-24 17:05:31 +0000858 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
859 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000860
861 ngx_queue_remove(q);
862
863 ngx_rbtree_delete(tree, &rn->node);
864
865 ngx_resolver_free_node(r, rn);
866 }
867}
868
869
870static ngx_int_t
871ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn)
872{
873 ssize_t n;
874 ngx_udp_connection_t *uc;
875
Igor Sysoev62071812011-10-24 16:09:05 +0000876 uc = r->udp_connections.elts;
877
878 uc = &uc[r->last_connection++];
879 if (r->last_connection == r->udp_connections.nelts) {
880 r->last_connection = 0;
881 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000882
883 if (uc->connection == NULL) {
Maxim Dounin53d96772012-02-27 16:57:02 +0000884
885 uc->log = *r->log;
886 uc->log.handler = ngx_resolver_log_error;
887 uc->log.data = uc;
888 uc->log.action = "resolving";
889
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000890 if (ngx_udp_connect(uc) != NGX_OK) {
891 return NGX_ERROR;
892 }
893
894 uc->connection->data = r;
895 uc->connection->read->handler = ngx_resolver_read_response;
Igor Sysoev4491dae2008-02-28 20:38:34 +0000896 uc->connection->read->resolver = 1;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000897 }
898
899 n = ngx_send(uc->connection, rn->query, rn->qlen);
900
901 if (n == -1) {
902 return NGX_ERROR;
903 }
904
905 if ((size_t) n != (size_t) rn->qlen) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +0000906 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete");
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000907 return NGX_ERROR;
908 }
909
910 return NGX_OK;
911}
912
913
914static void
915ngx_resolver_resend_handler(ngx_event_t *ev)
916{
917 time_t timer, atimer, ntimer;
918 ngx_resolver_t *r;
919
920 r = ev->data;
921
922 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0,
923 "resolver resend handler");
924
925 /* lock name mutex */
926
927 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue);
928
929 /* unlock name mutex */
930
931 /* lock addr mutex */
932
933 atimer = ngx_resolver_resend(r, &r->addr_rbtree, &r->addr_resend_queue);
Igor Sysoevb4e5b4c2007-12-01 19:57:37 +0000934
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000935 /* unlock addr mutex */
936
937 if (ntimer == 0) {
938 timer = atimer;
939
940 } else if (atimer == 0) {
941 timer = ntimer;
942
943 } else {
944 timer = (atimer < ntimer) ? atimer : ntimer;
945 }
946
947 if (timer) {
948 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000));
949 }
950}
951
952
953static time_t
954ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
955{
956 time_t now;
957 ngx_queue_t *q;
958 ngx_resolver_node_t *rn;
959
960 now = ngx_time();
961
962 for ( ;; ) {
963 if (ngx_queue_empty(queue)) {
964 return 0;
965 }
966
967 q = ngx_queue_last(queue);
968
969 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
970
971 if (now < rn->expire) {
972 return rn->expire - now;
973 }
974
Igor Sysoevd02661a2007-12-24 17:05:31 +0000975 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
976 "resolver resend \"%*s\" %p",
977 (size_t) rn->nlen, rn->name, rn->waiting);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000978
979 ngx_queue_remove(q);
980
981 if (rn->waiting) {
982
Ruslan Ermilovda8bb222012-06-18 12:30:45 +0000983 (void) ngx_resolver_send_query(r, rn);
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000984
Ruslan Ermilovda8bb222012-06-18 12:30:45 +0000985 rn->expire = now + r->resend_timeout;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000986
Ruslan Ermilovda8bb222012-06-18 12:30:45 +0000987 ngx_queue_insert_head(queue, q);
Igor Sysoev61adfb22008-02-06 16:08:52 +0000988
989 continue;
Igor Sysoevcb4d5382007-11-23 17:13:26 +0000990 }
991
992 ngx_rbtree_delete(tree, &rn->node);
993
994 ngx_resolver_free_node(r, rn);
995 }
996}
997
998
999static void
1000ngx_resolver_read_response(ngx_event_t *rev)
1001{
1002 ssize_t n;
1003 ngx_connection_t *c;
1004 u_char buf[NGX_RESOLVER_UDP_SIZE];
1005
1006 c = rev->data;
1007
1008 do {
Igor Sysoeve67d4612007-12-03 16:46:46 +00001009 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001010
Igor Sysoeve67d4612007-12-03 16:46:46 +00001011 if (n < 0) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001012 return;
1013 }
1014
1015 ngx_resolver_process_response(c->data, buf, n);
1016
1017 } while (rev->ready);
1018}
1019
1020
1021static void
1022ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n)
1023{
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001024 char *err;
1025 size_t len;
1026 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan,
1027 qtype, qclass;
1028 ngx_queue_t *q;
1029 ngx_resolver_qs_t *qs;
1030 ngx_resolver_hdr_t *response;
1031 ngx_resolver_node_t *rn;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001032
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001033 if (n < sizeof(ngx_resolver_hdr_t)) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001034 goto short_response;
1035 }
1036
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001037 response = (ngx_resolver_hdr_t *) buf;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001038
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001039 ident = (response->ident_hi << 8) + response->ident_lo;
1040 flags = (response->flags_hi << 8) + response->flags_lo;
1041 nqs = (response->nqs_hi << 8) + response->nqs_lo;
1042 nan = (response->nan_hi << 8) + response->nan_lo;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001043
1044 ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0,
Maxim Dounin01f1e4d2012-06-03 23:18:24 +00001045 "resolver DNS response %ui fl:%04Xui %ui/%ui/%ud/%ud",
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001046 ident, flags, nqs, nan,
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001047 (response->nns_hi << 8) + response->nns_lo,
1048 (response->nar_hi << 8) + response->nar_lo);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001049
1050 if (!(flags & 0x8000)) {
1051 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001052 "invalid DNS response %ui fl:%04Xui", ident, flags);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001053 return;
1054 }
1055
1056 code = flags & 0x7f;
1057
Igor Sysoev571a5e12008-10-24 14:38:09 +00001058 if (code == NGX_RESOLVE_FORMERR) {
1059
1060 times = 0;
1061
1062 for (q = ngx_queue_head(&r->name_resend_queue);
1063 q != ngx_queue_sentinel(&r->name_resend_queue) || times++ < 100;
1064 q = ngx_queue_next(q))
1065 {
1066 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
1067 qident = (rn->query[0] << 8) + rn->query[1];
1068
1069 if (qident == ident) {
1070 ngx_log_error(r->log_level, r->log, 0,
1071 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"",
1072 code, ngx_resolver_strerror(code), ident,
1073 rn->nlen, rn->name);
1074 return;
1075 }
1076 }
1077
1078 goto dns_error;
1079 }
1080
1081 if (code > NGX_RESOLVE_REFUSED) {
1082 goto dns_error;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001083 }
1084
1085 if (nqs != 1) {
1086 err = "invalid number of questions in DNS response";
1087 goto done;
1088 }
1089
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001090 i = sizeof(ngx_resolver_hdr_t);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001091
1092 while (i < (ngx_uint_t) n) {
1093 if (buf[i] == '\0') {
1094 goto found;
1095 }
1096
1097 len = buf[i];
1098 i += 1 + len;
1099 }
1100
1101 goto short_response;
1102
1103found:
1104
1105 if (i++ == 0) {
1106 err = "zero-length domain name in DNS response";
1107 goto done;
1108 }
1109
1110 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t))
1111 > (ngx_uint_t) n)
1112 {
1113 goto short_response;
1114 }
1115
1116 qs = (ngx_resolver_qs_t *) &buf[i];
1117
1118 qtype = (qs->type_hi << 8) + qs->type_lo;
1119 qclass = (qs->class_hi << 8) + qs->class_lo;
1120
1121 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001122 "resolver DNS response qt:%ui cl:%ui", qtype, qclass);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001123
1124 if (qclass != 1) {
1125 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001126 "unknown query class %ui in DNS response", qclass);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001127 return;
1128 }
1129
1130 switch (qtype) {
1131
1132 case NGX_RESOLVE_A:
1133
1134 ngx_resolver_process_a(r, buf, n, ident, code, nan,
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001135 i + sizeof(ngx_resolver_qs_t));
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001136
1137 break;
1138
1139 case NGX_RESOLVE_PTR:
1140
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001141 ngx_resolver_process_ptr(r, buf, n, ident, code, nan);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001142
1143 break;
1144
1145 default:
1146 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001147 "unknown query type %ui in DNS response", qtype);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001148 return;
1149 }
1150
1151 return;
1152
1153short_response:
1154
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001155 err = "short DNS response";
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001156
1157done:
1158
1159 ngx_log_error(r->log_level, r->log, 0, err);
1160
1161 return;
Igor Sysoev571a5e12008-10-24 14:38:09 +00001162
1163dns_error:
1164
1165 ngx_log_error(r->log_level, r->log, 0,
1166 "DNS error (%ui: %s), query id:%ui",
1167 code, ngx_resolver_strerror(code), ident);
1168 return;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001169}
1170
1171
1172static void
1173ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
1174 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans)
1175{
1176 char *err;
1177 u_char *cname;
1178 size_t len;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001179 int32_t ttl;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001180 uint32_t hash;
1181 in_addr_t addr, *addrs;
1182 ngx_str_t name;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001183 ngx_uint_t type, qident, naddrs, a, i, n, start;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001184 ngx_resolver_an_t *an;
1185 ngx_resolver_ctx_t *ctx, *next;
1186 ngx_resolver_node_t *rn;
1187
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001188 if (ngx_resolver_copy(r, &name, buf,
1189 buf + sizeof(ngx_resolver_hdr_t), buf + last)
1190 != NGX_OK)
1191 {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001192 return;
1193 }
1194
1195 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
1196
1197 hash = ngx_crc32_short(name.data, name.len);
1198
1199 /* lock name mutex */
1200
1201 rn = ngx_resolver_lookup_name(r, &name, hash);
1202
1203 if (rn == NULL || rn->query == NULL) {
1204 ngx_log_error(r->log_level, r->log, 0,
1205 "unexpected response for %V", &name);
1206 goto failed;
1207 }
1208
1209 qident = (rn->query[0] << 8) + rn->query[1];
1210
1211 if (ident != qident) {
1212 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001213 "wrong ident %ui response for %V, expect %ui",
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001214 ident, &name, qident);
1215 goto failed;
1216 }
1217
Igor Sysoev99651522009-09-16 13:48:11 +00001218 ngx_resolver_free(r, name.data);
1219
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001220 if (code == 0 && nan == 0) {
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001221 code = NGX_RESOLVE_NXDOMAIN;
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001222 }
1223
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001224 if (code) {
1225 next = rn->waiting;
1226 rn->waiting = NULL;
1227
1228 ngx_queue_remove(&rn->queue);
1229
1230 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
1231
1232 ngx_resolver_free_node(r, rn);
1233
1234 /* unlock name mutex */
1235
1236 while (next) {
1237 ctx = next;
1238 ctx->state = code;
1239 next = ctx->next;
1240
1241 ctx->handler(ctx);
1242 }
1243
1244 return;
1245 }
1246
1247 i = ans;
1248 naddrs = 0;
1249 addr = 0;
1250 addrs = NULL;
1251 cname = NULL;
Ruslan Ermilov777879c2011-11-16 13:27:33 +00001252 ttl = 0;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001253
1254 for (a = 0; a < nan; a++) {
1255
1256 start = i;
1257
1258 while (i < last) {
1259
1260 if (buf[i] & 0xc0) {
1261 i += 2;
1262 goto found;
1263 }
1264
1265 if (buf[i] == 0) {
1266 i++;
1267 goto test_length;
1268 }
1269
1270 i += 1 + buf[i];
1271 }
1272
1273 goto short_response;
1274
1275 test_length:
1276
1277 if (i - start < 2) {
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001278 err = "invalid name in DNS response";
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001279 goto invalid;
1280 }
1281
1282 found:
1283
1284 if (i + sizeof(ngx_resolver_an_t) >= last) {
1285 goto short_response;
1286 }
1287
1288 an = (ngx_resolver_an_t *) &buf[i];
1289
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001290 type = (an->type_hi << 8) + an->type_lo;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001291 len = (an->len_hi << 8) + an->len_lo;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001292 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
1293 + (an->ttl[2] << 8) + (an->ttl[3]);
1294
1295 if (ttl < 0) {
1296 ttl = 0;
1297 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001298
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001299 switch (type) {
1300
1301 case NGX_RESOLVE_A:
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001302
1303 i += sizeof(ngx_resolver_an_t);
1304
1305 if (i + len > last) {
1306 goto short_response;
1307 }
1308
1309 addr = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1310 + (buf[i + 2] << 8) + (buf[i + 3]));
1311
1312 naddrs++;
1313
1314 i += len;
1315
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001316 break;
1317
1318 case NGX_RESOLVE_CNAME:
1319
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001320 cname = &buf[i] + sizeof(ngx_resolver_an_t);
1321 i += sizeof(ngx_resolver_an_t) + len;
Igor Sysoev19540cd2008-04-10 17:12:21 +00001322
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001323 break;
1324
1325 case NGX_RESOLVE_DNAME:
1326
Igor Sysoev19540cd2008-04-10 17:12:21 +00001327 i += sizeof(ngx_resolver_an_t) + len;
Igor Sysoev4ad5be62008-04-10 17:26:14 +00001328
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001329 break;
1330
1331 default:
1332
Igor Sysoev4ad5be62008-04-10 17:26:14 +00001333 ngx_log_error(r->log_level, r->log, 0,
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001334 "unexpected RR type %ui", type);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001335 }
1336 }
1337
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001338 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
1339 "resolver naddrs:%ui cname:%p ttl:%d",
1340 naddrs, cname, ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001341
1342 if (naddrs) {
1343
1344 if (naddrs == 1) {
1345 rn->u.addr = addr;
1346
1347 } else {
1348
1349 addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
1350 if (addrs == NULL) {
1351 return;
1352 }
1353
1354 n = 0;
1355 i = ans;
1356
1357 for (a = 0; a < nan; a++) {
1358
1359 for ( ;; ) {
1360
1361 if (buf[i] & 0xc0) {
1362 i += 2;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001363 break;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001364 }
1365
1366 if (buf[i] == 0) {
1367 i++;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001368 break;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001369 }
1370
1371 i += 1 + buf[i];
1372 }
1373
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001374 an = (ngx_resolver_an_t *) &buf[i];
1375
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001376 type = (an->type_hi << 8) + an->type_lo;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001377 len = (an->len_hi << 8) + an->len_lo;
1378
1379 i += sizeof(ngx_resolver_an_t);
1380
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001381 if (type == NGX_RESOLVE_A) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001382
1383 addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
1384 + (buf[i + 2] << 8) + (buf[i + 3]));
1385
1386 if (n == naddrs) {
1387 break;
1388 }
1389 }
1390
1391 i += len;
1392 }
1393
1394 rn->u.addrs = addrs;
1395
1396 addrs = ngx_resolver_dup(r, rn->u.addrs,
1397 naddrs * sizeof(in_addr_t));
1398 if (addrs == NULL) {
1399 return;
1400 }
1401 }
1402
1403 rn->naddrs = (u_short) naddrs;
1404
1405 ngx_queue_remove(&rn->queue);
1406
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001407 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001408 rn->expire = ngx_time() + r->expire;
1409
1410 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1411
1412 next = rn->waiting;
1413 rn->waiting = NULL;
1414
1415 /* unlock name mutex */
1416
1417 while (next) {
1418 ctx = next;
1419 ctx->state = NGX_OK;
1420 ctx->naddrs = naddrs;
1421 ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
1422 ctx->addr = addr;
1423 next = ctx->next;
1424
1425 ctx->handler(ctx);
1426 }
1427
Igor Sysoevfa748fe2009-01-29 14:32:58 +00001428 if (naddrs > 1) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001429 ngx_resolver_free(r, addrs);
1430 }
1431
Maxim Dounin7d863c02012-05-14 09:13:45 +00001432 ngx_resolver_free(r, rn->query);
1433 rn->query = NULL;
1434
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001435 return;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001436 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001437
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001438 if (cname) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001439
1440 /* CNAME only */
1441
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001442 if (ngx_resolver_copy(r, &name, buf, cname, buf + last) != NGX_OK) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001443 return;
1444 }
1445
1446 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
1447 "resolver cname:\"%V\"", &name);
1448
Igor Sysoev28bca1b2007-12-16 20:00:15 +00001449 ngx_queue_remove(&rn->queue);
1450
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001451 rn->cnlen = (u_short) name.len;
1452 rn->u.cname = name.data;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001453
1454 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001455 rn->expire = ngx_time() + r->expire;
1456
1457 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
1458
1459 ctx = rn->waiting;
1460 rn->waiting = NULL;
1461
1462 if (ctx) {
1463 ctx->name = name;
1464
1465 (void) ngx_resolve_name_locked(r, ctx);
1466 }
1467
Maxim Dounin7d863c02012-05-14 09:13:45 +00001468 ngx_resolver_free(r, rn->query);
1469 rn->query = NULL;
1470
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001471 return;
1472 }
1473
1474 ngx_log_error(r->log_level, r->log, 0,
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001475 "no A or CNAME types in DNS response");
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001476 return;
1477
1478short_response:
1479
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001480 err = "short DNS response";
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001481
1482invalid:
1483
1484 /* unlock name mutex */
1485
1486 ngx_log_error(r->log_level, r->log, 0, err);
1487
1488 return;
1489
1490failed:
1491
1492 /* unlock name mutex */
1493
Igor Sysoev99651522009-09-16 13:48:11 +00001494 ngx_resolver_free(r, name.data);
1495
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001496 return;
1497}
1498
1499
1500static void
1501ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001502 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001503{
1504 char *err;
1505 size_t len;
1506 in_addr_t addr;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001507 int32_t ttl;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001508 ngx_int_t octet;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001509 ngx_str_t name;
Igor Sysoevefde3eb2011-04-23 17:25:06 +00001510 ngx_uint_t i, mask, qident;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001511 ngx_resolver_an_t *an;
1512 ngx_resolver_ctx_t *ctx, *next;
1513 ngx_resolver_node_t *rn;
1514
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001515 if (ngx_resolver_copy(r, NULL, buf,
1516 buf + sizeof(ngx_resolver_hdr_t), buf + n)
1517 != NGX_OK)
1518 {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001519 goto invalid_in_addr_arpa;
1520 }
1521
1522 addr = 0;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001523 i = sizeof(ngx_resolver_hdr_t);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001524
1525 for (mask = 0; mask < 32; mask += 8) {
1526 len = buf[i++];
1527
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001528 octet = ngx_atoi(&buf[i], len);
1529 if (octet == NGX_ERROR || octet > 255) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001530 goto invalid_in_addr_arpa;
1531 }
1532
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001533 addr += octet << mask;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001534 i += len;
1535 }
1536
1537 if (ngx_strcmp(&buf[i], "\7in-addr\4arpa") != 0) {
1538 goto invalid_in_addr_arpa;
1539 }
1540
1541 /* lock addr mutex */
1542
1543 rn = ngx_resolver_lookup_addr(r, addr);
1544
1545 if (rn == NULL || rn->query == NULL) {
1546 ngx_log_error(r->log_level, r->log, 0,
1547 "unexpected response for %ud.%ud.%ud.%ud",
1548 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1549 (addr >> 8) & 0xff, addr & 0xff);
1550 goto failed;
1551 }
1552
1553 qident = (rn->query[0] << 8) + rn->query[1];
1554
1555 if (ident != qident) {
1556 ngx_log_error(r->log_level, r->log, 0,
Igor Sysoev7c5c1dc2008-04-10 17:27:07 +00001557 "wrong ident %ui response for %ud.%ud.%ud.%ud, expect %ui",
1558 ident, (addr >> 24) & 0xff, (addr >> 16) & 0xff,
1559 (addr >> 8) & 0xff, addr & 0xff, qident);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001560 goto failed;
1561 }
1562
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001563 if (code == 0 && nan == 0) {
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001564 code = NGX_RESOLVE_NXDOMAIN;
Igor Sysoevc0cadf12007-12-16 20:47:55 +00001565 }
1566
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001567 if (code) {
1568 next = rn->waiting;
1569 rn->waiting = NULL;
1570
1571 ngx_queue_remove(&rn->queue);
1572
1573 ngx_rbtree_delete(&r->addr_rbtree, &rn->node);
1574
1575 ngx_resolver_free_node(r, rn);
1576
1577 /* unlock addr mutex */
1578
1579 while (next) {
1580 ctx = next;
1581 ctx->state = code;
1582 next = ctx->next;
1583
1584 ctx->handler(ctx);
1585 }
1586
1587 return;
1588 }
1589
1590 i += sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t);
1591
1592 if (i + 2 + sizeof(ngx_resolver_an_t) > (ngx_uint_t) n) {
1593 goto short_response;
1594 }
1595
1596 /* compression pointer to "XX.XX.XX.XX.in-addr.arpa */
1597
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001598 if (buf[i] != 0xc0 || buf[i + 1] != sizeof(ngx_resolver_hdr_t)) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001599 err = "invalid in-addr.arpa name in DNS response";
1600 goto invalid;
1601 }
1602
1603 an = (ngx_resolver_an_t *) &buf[i + 2];
1604
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001605 len = (an->len_hi << 8) + an->len_lo;
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001606 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
1607 + (an->ttl[2] << 8) + (an->ttl[3]);
1608
1609 if (ttl < 0) {
1610 ttl = 0;
1611 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001612
1613 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
Igor Sysoevefde3eb2011-04-23 17:25:06 +00001614 "resolver qt:%ui cl:%ui len:%uz",
1615 (an->type_hi << 8) + an->type_lo,
1616 (an->class_hi << 8) + an->class_lo, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001617
1618 i += 2 + sizeof(ngx_resolver_an_t);
1619
1620 if (i + len > (ngx_uint_t) n) {
1621 goto short_response;
1622 }
1623
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001624 if (ngx_resolver_copy(r, &name, buf, buf + i, buf + n) != NGX_OK) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001625 return;
1626 }
1627
1628 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name);
1629
Igor Sysoev2c01fd32009-01-30 11:56:45 +00001630 if (name.len != (size_t) rn->nlen
1631 || ngx_strncmp(name.data, rn->name, name.len) != 0)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001632 {
Igor Sysoev8629b9b2009-01-29 14:29:49 +00001633 if (rn->nlen) {
1634 ngx_resolver_free(r, rn->name);
1635 }
1636
Igor Sysoev9a5f4c72009-02-02 10:17:06 +00001637 rn->nlen = (u_short) name.len;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001638 rn->name = name.data;
1639
Igor Sysoev2c01fd32009-01-30 11:56:45 +00001640 name.data = ngx_resolver_dup(r, rn->name, name.len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001641 if (name.data == NULL) {
1642 goto failed;
1643 }
1644 }
1645
1646 ngx_queue_remove(&rn->queue);
1647
Ruslan Ermilovbec516b2011-11-16 13:11:39 +00001648 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001649 rn->expire = ngx_time() + r->expire;
1650
1651 ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
1652
1653 next = rn->waiting;
1654 rn->waiting = NULL;
1655
1656 /* unlock addr mutex */
1657
1658 while (next) {
1659 ctx = next;
1660 ctx->state = NGX_OK;
1661 ctx->name = name;
1662 next = ctx->next;
1663
1664 ctx->handler(ctx);
1665 }
1666
1667 ngx_resolver_free(r, name.data);
1668
1669 return;
1670
1671invalid_in_addr_arpa:
1672
1673 ngx_log_error(r->log_level, r->log, 0,
1674 "invalid in-addr.arpa name in DNS response");
1675 return;
1676
1677short_response:
1678
1679 err = "short DNS response";
1680
1681invalid:
1682
1683 /* unlock addr mutex */
1684
1685 ngx_log_error(r->log_level, r->log, 0, err);
1686
1687 return;
1688
1689failed:
1690
1691 /* unlock addr mutex */
1692
1693 return;
1694}
1695
1696
1697static ngx_resolver_node_t *
1698ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
1699{
1700 ngx_int_t rc;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001701 ngx_rbtree_node_t *node, *sentinel;
1702 ngx_resolver_node_t *rn;
1703
1704 node = r->name_rbtree.root;
1705 sentinel = r->name_rbtree.sentinel;
1706
1707 while (node != sentinel) {
1708
1709 if (hash < node->key) {
1710 node = node->left;
1711 continue;
1712 }
1713
1714 if (hash > node->key) {
1715 node = node->right;
1716 continue;
1717 }
1718
1719 /* hash == node->key */
1720
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001721 rn = (ngx_resolver_node_t *) node;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001722
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001723 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001724
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001725 if (rc == 0) {
1726 return rn;
1727 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001728
Maxim Dounin7ca6c1f2012-02-27 22:15:39 +00001729 node = (rc < 0) ? node->left : node->right;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001730 }
1731
1732 /* not found */
1733
1734 return NULL;
1735}
1736
1737
1738static ngx_resolver_node_t *
1739ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr)
1740{
1741 ngx_rbtree_node_t *node, *sentinel;
1742
1743 node = r->addr_rbtree.root;
1744 sentinel = r->addr_rbtree.sentinel;
1745
1746 while (node != sentinel) {
1747
1748 if (addr < node->key) {
1749 node = node->left;
1750 continue;
1751 }
1752
1753 if (addr > node->key) {
1754 node = node->right;
1755 continue;
1756 }
1757
1758 /* addr == node->key */
1759
1760 return (ngx_resolver_node_t *) node;
1761 }
1762
1763 /* not found */
1764
1765 return NULL;
1766}
1767
1768
1769static void
1770ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
1771 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1772{
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001773 ngx_rbtree_node_t **p;
1774 ngx_resolver_node_t *rn, *rn_temp;
1775
1776 for ( ;; ) {
1777
1778 if (node->key < temp->key) {
1779
1780 p = &temp->left;
1781
1782 } else if (node->key > temp->key) {
1783
1784 p = &temp->right;
1785
1786 } else { /* node->key == temp->key */
1787
1788 rn = (ngx_resolver_node_t *) node;
1789 rn_temp = (ngx_resolver_node_t *) temp;
1790
Igor Sysoeva8d3d222009-09-19 16:15:13 +00001791 p = (ngx_memn2cmp(rn->name, rn_temp->name, rn->nlen, rn_temp->nlen)
1792 < 0) ? &temp->left : &temp->right;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001793 }
1794
1795 if (*p == sentinel) {
1796 break;
1797 }
1798
1799 temp = *p;
1800 }
1801
1802 *p = node;
1803 node->parent = temp;
1804 node->left = sentinel;
1805 node->right = sentinel;
1806 ngx_rbt_red(node);
1807}
1808
1809
1810static ngx_int_t
1811ngx_resolver_create_name_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1812{
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001813 u_char *p, *s;
1814 size_t len, nlen;
1815 ngx_uint_t ident;
1816 ngx_resolver_qs_t *qs;
1817 ngx_resolver_hdr_t *query;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001818
Igor Sysoeve79f4292009-11-11 14:27:24 +00001819 nlen = ctx->name.len ? (1 + ctx->name.len + 1) : 1;
1820
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001821 len = sizeof(ngx_resolver_hdr_t) + nlen + sizeof(ngx_resolver_qs_t);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001822
Igor Sysoev653edd42009-11-11 14:29:20 +00001823 p = ngx_resolver_alloc(ctx->resolver, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001824 if (p == NULL) {
1825 return NGX_ERROR;
1826 }
1827
1828 rn->qlen = (u_short) len;
1829 rn->query = p;
1830
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001831 query = (ngx_resolver_hdr_t *) p;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001832
1833 ident = ngx_random();
1834
1835 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->resolver->log, 0,
1836 "resolve: \"%V\" %i", &ctx->name, ident & 0xffff);
1837
1838 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
1839 query->ident_lo = (u_char) (ident & 0xff);
1840
1841 /* recursion query */
1842 query->flags_hi = 1; query->flags_lo = 0;
1843
1844 /* one question */
1845 query->nqs_hi = 0; query->nqs_lo = 1;
1846 query->nan_hi = 0; query->nan_lo = 0;
1847 query->nns_hi = 0; query->nns_lo = 0;
1848 query->nar_hi = 0; query->nar_lo = 0;
1849
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001850 p += sizeof(ngx_resolver_hdr_t) + nlen;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001851
1852 qs = (ngx_resolver_qs_t *) p;
1853
1854 /* query type */
1855 qs->type_hi = 0; qs->type_lo = (u_char) ctx->type;
1856
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001857 /* IN query class */
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001858 qs->class_hi = 0; qs->class_lo = 1;
1859
1860 /* convert "www.example.com" to "\3www\7example\3com\0" */
1861
1862 len = 0;
1863 p--;
1864 *p-- = '\0';
1865
Ruslan Ermilov487ba702012-04-26 12:58:42 +00001866 if (ctx->name.len == 0) {
1867 return NGX_DECLINED;
1868 }
1869
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001870 for (s = ctx->name.data + ctx->name.len - 1; s >= ctx->name.data; s--) {
1871 if (*s != '.') {
1872 *p = *s;
1873 len++;
1874
1875 } else {
Maxim Dounine34ff162012-03-22 11:57:18 +00001876 if (len == 0 || len > 255) {
Igor Sysoev09b199c2008-04-09 14:45:39 +00001877 return NGX_DECLINED;
1878 }
1879
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001880 *p = (u_char) len;
1881 len = 0;
1882 }
1883
1884 p--;
1885 }
1886
Maxim Dounine34ff162012-03-22 11:57:18 +00001887 if (len == 0 || len > 255) {
1888 return NGX_DECLINED;
1889 }
1890
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001891 *p = (u_char) len;
1892
1893 return NGX_OK;
1894}
1895
1896
1897/* AF_INET only */
1898
1899static ngx_int_t
1900ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
1901{
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001902 u_char *p, *d;
1903 size_t len;
1904 ngx_int_t n;
1905 ngx_uint_t ident;
1906 ngx_resolver_hdr_t *query;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001907
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001908 len = sizeof(ngx_resolver_hdr_t)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001909 + sizeof(".255.255.255.255.in-addr.arpa.") - 1
1910 + sizeof(ngx_resolver_qs_t);
1911
Igor Sysoev653edd42009-11-11 14:29:20 +00001912 p = ngx_resolver_alloc(ctx->resolver, len);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001913 if (p == NULL) {
1914 return NGX_ERROR;
1915 }
1916
1917 rn->query = p;
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001918 query = (ngx_resolver_hdr_t *) p;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001919
1920 ident = ngx_random();
1921
1922 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
1923 query->ident_lo = (u_char) (ident & 0xff);
1924
1925 /* recursion query */
1926 query->flags_hi = 1; query->flags_lo = 0;
1927
1928 /* one question */
1929 query->nqs_hi = 0; query->nqs_lo = 1;
1930 query->nan_hi = 0; query->nan_lo = 0;
1931 query->nns_hi = 0; query->nns_lo = 0;
1932 query->nar_hi = 0; query->nar_lo = 0;
1933
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001934 p += sizeof(ngx_resolver_hdr_t);
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001935
Igor Sysoevaa6936e2010-06-23 16:34:54 +00001936 for (n = 0; n < 32; n += 8) {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001937 d = ngx_sprintf(&p[1], "%ud", (ctx->addr >> n) & 0xff);
1938 *p = (u_char) (d - &p[1]);
1939 p = d;
1940 }
1941
Ruslan Ermilov3d245d42013-12-06 14:30:27 +04001942 /* query type "PTR", IN query class */
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001943 ngx_memcpy(p, "\7in-addr\4arpa\0\0\14\0\1", 18);
1944
1945 rn->qlen = (u_short)
1946 (p + sizeof("\7in-addr\4arpa") + sizeof(ngx_resolver_qs_t)
1947 - rn->query);
1948
1949 return NGX_OK;
1950}
1951
1952
1953static ngx_int_t
1954ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src,
1955 u_char *last)
1956{
1957 char *err;
1958 u_char *p, *dst;
1959 ssize_t len;
1960 ngx_uint_t i, n;
1961
1962 p = src;
1963 len = -1;
1964
1965 /*
1966 * compression pointers allow to create endless loop, so we set limit;
1967 * 128 pointers should be enough to store 255-byte name
1968 */
1969
1970 for (i = 0; i < 128; i++) {
1971 n = *p++;
1972
1973 if (n == 0) {
1974 goto done;
1975 }
1976
1977 if (n & 0xc0) {
Igor Sysoevb3ac4fb2008-11-17 08:04:41 +00001978 n = ((n & 0x3f) << 8) + *p;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00001979 p = &buf[n];
1980
1981 } else {
1982 len += 1 + n;
1983 p = &p[n];
1984 }
1985
1986 if (p >= last) {
1987 err = "name is out of response";
1988 goto invalid;
1989 }
1990 }
1991
1992 err = "compression pointers loop";
1993
1994invalid:
1995
1996 ngx_log_error(r->log_level, r->log, 0, err);
1997
1998 return NGX_ERROR;
1999
2000done:
2001
2002 if (name == NULL) {
2003 return NGX_OK;
2004 }
2005
Igor Sysoev70966c12009-11-09 18:04:05 +00002006 if (len == -1) {
2007 name->len = 0;
2008 name->data = NULL;
2009 return NGX_OK;
2010 }
2011
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002012 dst = ngx_resolver_alloc(r, len);
2013 if (dst == NULL) {
2014 return NGX_ERROR;
2015 }
2016
2017 name->data = dst;
2018
2019 n = *src++;
2020
2021 for ( ;; ) {
Igor Sysoeveb499ba2011-11-09 13:54:07 +00002022 if (n & 0xc0) {
2023 n = ((n & 0x3f) << 8) + *src;
2024 src = &buf[n];
2025
2026 n = *src++;
2027
2028 } else {
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002029 ngx_memcpy(dst, src, n);
2030 dst += n;
2031 src += n;
2032
2033 n = *src++;
2034
2035 if (n != 0) {
2036 *dst++ = '.';
2037 }
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002038 }
2039
2040 if (n == 0) {
2041 name->len = dst - name->data;
2042 return NGX_OK;
2043 }
2044 }
2045}
2046
2047
2048static void
2049ngx_resolver_timeout_handler(ngx_event_t *ev)
2050{
2051 ngx_resolver_ctx_t *ctx;
2052
2053 ctx = ev->data;
2054
2055 ctx->state = NGX_RESOLVE_TIMEDOUT;
2056
2057 ctx->handler(ctx);
2058}
2059
2060
2061static void
2062ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn)
2063{
2064 /* lock alloc mutex */
2065
2066 if (rn->query) {
2067 ngx_resolver_free_locked(r, rn->query);
2068 }
2069
2070 if (rn->name) {
2071 ngx_resolver_free_locked(r, rn->name);
2072 }
2073
2074 if (rn->cnlen) {
2075 ngx_resolver_free_locked(r, rn->u.cname);
2076 }
2077
2078 if (rn->naddrs > 1) {
2079 ngx_resolver_free_locked(r, rn->u.addrs);
2080 }
2081
2082 ngx_resolver_free_locked(r, rn);
2083
2084 /* unlock alloc mutex */
2085}
2086
2087
2088static void *
2089ngx_resolver_alloc(ngx_resolver_t *r, size_t size)
2090{
2091 u_char *p;
2092
2093 /* lock alloc mutex */
2094
2095 p = ngx_alloc(size, r->log);
2096
2097 /* unlock alloc mutex */
2098
2099 return p;
2100}
2101
2102
Igor Sysoev8ee01f52008-02-28 15:34:53 +00002103static void *
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002104ngx_resolver_calloc(ngx_resolver_t *r, size_t size)
2105{
2106 u_char *p;
2107
2108 p = ngx_resolver_alloc(r, size);
2109
2110 if (p) {
2111 ngx_memzero(p, size);
2112 }
2113
2114 return p;
2115}
2116
2117
2118static void
2119ngx_resolver_free(ngx_resolver_t *r, void *p)
2120{
2121 /* lock alloc mutex */
2122
2123 ngx_free(p);
2124
2125 /* unlock alloc mutex */
2126}
2127
2128
2129static void
2130ngx_resolver_free_locked(ngx_resolver_t *r, void *p)
2131{
2132 ngx_free(p);
2133}
2134
2135
2136static void *
2137ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size)
2138{
2139 void *dst;
2140
2141 dst = ngx_resolver_alloc(r, size);
2142
2143 if (dst == NULL) {
2144 return dst;
2145 }
2146
2147 ngx_memcpy(dst, src, size);
2148
2149 return dst;
2150}
2151
2152
Maxim Dounin6a0f47e2012-09-28 18:28:38 +00002153static in_addr_t *
2154ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src, ngx_uint_t n)
2155{
2156 void *dst, *p;
2157 ngx_uint_t j;
2158
2159 dst = ngx_resolver_alloc(r, n * sizeof(in_addr_t));
2160
Maxim Dounin7b373182012-10-24 14:07:08 +00002161 if (dst == NULL) {
2162 return dst;
2163 }
2164
Maxim Dounin6a0f47e2012-09-28 18:28:38 +00002165 j = ngx_random() % n;
2166
2167 if (j == 0) {
2168 ngx_memcpy(dst, src, n * sizeof(in_addr_t));
2169 return dst;
2170 }
2171
2172 p = ngx_cpymem(dst, &src[j], (n - j) * sizeof(in_addr_t));
2173 ngx_memcpy(p, src, j * sizeof(in_addr_t));
2174
2175 return dst;
2176}
2177
2178
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002179char *
2180ngx_resolver_strerror(ngx_int_t err)
2181{
2182 static char *errors[] = {
2183 "Format error", /* FORMERR */
2184 "Server failure", /* SERVFAIL */
2185 "Host not found", /* NXDOMAIN */
2186 "Unimplemented", /* NOTIMP */
2187 "Operation refused" /* REFUSED */
2188 };
2189
2190 if (err > 0 && err < 6) {
2191 return errors[err - 1];
2192 }
2193
2194 if (err == NGX_RESOLVE_TIMEDOUT) {
2195 return "Operation timed out";
2196 }
2197
2198 return "Unknown error";
2199}
2200
2201
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002202static u_char *
2203ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len)
2204{
2205 u_char *p;
2206 ngx_udp_connection_t *uc;
2207
2208 p = buf;
2209
2210 if (log->action) {
2211 p = ngx_snprintf(buf, len, " while %s", log->action);
2212 len -= p - buf;
2213 }
2214
2215 uc = log->data;
2216
2217 if (uc) {
2218 p = ngx_snprintf(p, len, ", resolver: %V", &uc->server);
2219 }
2220
2221 return p;
2222}
2223
2224
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002225ngx_int_t
2226ngx_udp_connect(ngx_udp_connection_t *uc)
2227{
2228 int rc;
2229 ngx_int_t event;
2230 ngx_event_t *rev, *wev;
2231 ngx_socket_t s;
2232 ngx_connection_t *c;
2233
Ruslan Ermilovac7f7042012-06-04 14:23:27 +00002234 s = ngx_socket(uc->sockaddr->sa_family, SOCK_DGRAM, 0);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002235
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002236 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002237
Maxim Dounin48d96ce2013-09-04 20:48:28 +04002238 if (s == (ngx_socket_t) -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002239 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002240 ngx_socket_n " failed");
2241 return NGX_ERROR;
2242 }
2243
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002244 c = ngx_get_connection(s, &uc->log);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002245
2246 if (c == NULL) {
2247 if (ngx_close_socket(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002248 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002249 ngx_close_socket_n "failed");
2250 }
2251
2252 return NGX_ERROR;
2253 }
2254
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002255 if (ngx_nonblocking(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002256 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002257 ngx_nonblocking_n " failed");
2258
2259 ngx_free_connection(c);
2260
2261 if (ngx_close_socket(s) == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002262 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002263 ngx_close_socket_n " failed");
2264 }
2265
2266 return NGX_ERROR;
2267 }
2268
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002269 rev = c->read;
2270 wev = c->write;
2271
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002272 rev->log = &uc->log;
2273 wev->log = &uc->log;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002274
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002275 uc->connection = c;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002276
2277 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
2278
2279#if (NGX_THREADS)
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002280
2281 /* TODO: lock event when call completion handler */
2282
2283 rev->lock = &c->lock;
2284 wev->lock = &c->lock;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002285 rev->own_lock = &c->lock;
2286 wev->own_lock = &c->lock;
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002287
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002288#endif
2289
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002290 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &uc->log, 0,
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002291 "connect to %V, fd:%d #%d", &uc->server, s, c->number);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002292
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002293 rc = connect(s, uc->sockaddr, uc->socklen);
2294
2295 /* TODO: aio, iocp */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002296
2297 if (rc == -1) {
Igor Sysoev089b2fd2010-01-11 11:01:02 +00002298 ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno,
2299 "connect() failed");
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002300
2301 return NGX_ERROR;
2302 }
2303
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002304 /* UDP sockets are always ready to write */
2305 wev->ready = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002306
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002307 if (ngx_add_event) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002308
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002309 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ?
2310 /* kqueue, epoll */ NGX_CLEAR_EVENT:
2311 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT;
2312 /* eventport event type has no meaning: oneshot only */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002313
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002314 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
2315 return NGX_ERROR;
2316 }
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002317
Igor Sysoevcb4d5382007-11-23 17:13:26 +00002318 } else {
2319 /* rtsig */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002320
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002321 if (ngx_add_conn(c) == NGX_ERROR) {
2322 return NGX_ERROR;
2323 }
2324 }
2325
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002326 return NGX_OK;
2327}