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