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