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