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