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