Mercurial > hg > nginx-quic
annotate src/core/ngx_resolver.c @ 6432:cf3e75cfa951
Added variables support to server_tokens.
It can now be set to "off" conditionally, e.g. using the map
directive.
An empty value will disable the emission of the Server: header
and the signature in error messages generated by nginx.
Any other value is treated as "on", meaning that full nginx
version is emitted in the Server: header and error messages
generated by nginx.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Tue, 15 Mar 2016 13:36:19 +0300 |
parents | a6a5bacf6b74 |
children | c94aba230a5a |
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 | |
6367 | 15 #define NGX_RESOLVER_TCP_RSIZE (2 + 65535) |
16 #define NGX_RESOLVER_TCP_WSIZE 8192 | |
17 | |
1649 | 18 |
583 | 19 typedef struct { |
1649 | 20 u_char ident_hi; |
21 u_char ident_lo; | |
22 u_char flags_hi; | |
23 u_char flags_lo; | |
24 u_char nqs_hi; | |
25 u_char nqs_lo; | |
26 u_char nan_hi; | |
27 u_char nan_lo; | |
28 u_char nns_hi; | |
29 u_char nns_lo; | |
30 u_char nar_hi; | |
31 u_char nar_lo; | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
32 } ngx_resolver_hdr_t; |
1649 | 33 |
34 | |
35 typedef struct { | |
36 u_char type_hi; | |
37 u_char type_lo; | |
38 u_char class_hi; | |
39 u_char class_lo; | |
40 } ngx_resolver_qs_t; | |
41 | |
42 | |
43 typedef struct { | |
44 u_char type_hi; | |
45 u_char type_lo; | |
46 u_char class_hi; | |
47 u_char class_lo; | |
48 u_char ttl[4]; | |
49 u_char len_hi; | |
50 u_char len_lo; | |
51 } ngx_resolver_an_t; | |
52 | |
53 | |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
54 #define ngx_resolver_node(n) \ |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
55 (ngx_resolver_node_t *) \ |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
56 ((u_char *) (n) - offsetof(ngx_resolver_node_t, node)) |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
57 |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
58 |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
59 ngx_int_t ngx_udp_connect(ngx_resolver_connection_t *rec); |
6367 | 60 ngx_int_t ngx_tcp_connect(ngx_resolver_connection_t *rec); |
1649 | 61 |
62 | |
1906 | 63 static void ngx_resolver_cleanup(void *data); |
64 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree); | |
1649 | 65 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
66 ngx_resolver_ctx_t *ctx, ngx_str_t *name); |
1649 | 67 static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, |
68 ngx_queue_t *queue); | |
69 static ngx_int_t ngx_resolver_send_query(ngx_resolver_t *r, | |
70 ngx_resolver_node_t *rn); | |
6367 | 71 static ngx_int_t ngx_resolver_send_udp_query(ngx_resolver_t *r, |
72 ngx_resolver_connection_t *rec, u_char *query, u_short qlen); | |
73 static ngx_int_t ngx_resolver_send_tcp_query(ngx_resolver_t *r, | |
74 ngx_resolver_connection_t *rec, u_char *query, u_short qlen); | |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
75 static ngx_int_t ngx_resolver_create_name_query(ngx_resolver_t *r, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
76 ngx_resolver_node_t *rn, ngx_str_t *name); |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
77 static ngx_int_t ngx_resolver_create_addr_query(ngx_resolver_t *r, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
78 ngx_resolver_node_t *rn, ngx_addr_t *addr); |
1649 | 79 static void ngx_resolver_resend_handler(ngx_event_t *ev); |
80 static time_t ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, | |
81 ngx_queue_t *queue); | |
6196
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
82 static ngx_uint_t ngx_resolver_resend_empty(ngx_resolver_t *r); |
6367 | 83 static void ngx_resolver_udp_read(ngx_event_t *rev); |
84 static void ngx_resolver_tcp_write(ngx_event_t *wev); | |
85 static void ngx_resolver_tcp_read(ngx_event_t *rev); | |
1649 | 86 static void ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, |
6367 | 87 size_t n, ngx_uint_t tcp); |
1649 | 88 static void ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n, |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
89 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t qtype, |
6367 | 90 ngx_uint_t nan, ngx_uint_t trunc, ngx_uint_t ans); |
1649 | 91 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
|
92 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan); |
1649 | 93 static ngx_resolver_node_t *ngx_resolver_lookup_name(ngx_resolver_t *r, |
94 ngx_str_t *name, uint32_t hash); | |
95 static ngx_resolver_node_t *ngx_resolver_lookup_addr(ngx_resolver_t *r, | |
96 in_addr_t addr); | |
97 static void ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
98 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); | |
99 static ngx_int_t ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, | |
100 u_char *buf, u_char *src, u_char *last); | |
101 static void ngx_resolver_timeout_handler(ngx_event_t *ev); | |
102 static void ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn); | |
103 static void *ngx_resolver_alloc(ngx_resolver_t *r, size_t size); | |
1903 | 104 static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size); |
1649 | 105 static void ngx_resolver_free(ngx_resolver_t *r, void *p); |
106 static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p); | |
107 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
108 static ngx_addr_t *ngx_resolver_export(ngx_resolver_t *r, |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
109 ngx_resolver_node_t *rn, ngx_uint_t rotate); |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
110 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len); |
1649 | 111 |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
112 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
113 static void ngx_resolver_rbtree_insert_addr6_value(ngx_rbtree_node_t *temp, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
114 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
115 static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
116 struct in6_addr *addr, uint32_t hash); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
117 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
118 |
1649 | 119 |
120 ngx_resolver_t * | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
121 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) |
1649 | 122 { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
123 ngx_str_t s; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
124 ngx_url_t u; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
125 ngx_uint_t i, j; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
126 ngx_resolver_t *r; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
127 ngx_pool_cleanup_t *cln; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
128 ngx_resolver_connection_t *rec; |
1649 | 129 |
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
|
130 cln = ngx_pool_cleanup_add(cf->pool, 0); |
1906 | 131 if (cln == NULL) { |
132 return NULL; | |
133 } | |
134 | |
135 cln->handler = ngx_resolver_cleanup; | |
136 | |
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
|
137 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); |
1649 | 138 if (r == NULL) { |
139 return NULL; | |
140 } | |
141 | |
1906 | 142 cln->data = r; |
143 | |
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
|
144 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); |
1649 | 145 if (r->event == NULL) { |
146 return NULL; | |
147 } | |
148 | |
1687 | 149 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel, |
150 ngx_resolver_rbtree_insert_value); | |
151 | |
152 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel, | |
153 ngx_rbtree_insert_value); | |
1649 | 154 |
1685 | 155 ngx_queue_init(&r->name_resend_queue); |
156 ngx_queue_init(&r->addr_resend_queue); | |
157 | |
158 ngx_queue_init(&r->name_expire_queue); | |
159 ngx_queue_init(&r->addr_expire_queue); | |
1649 | 160 |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
161 #if (NGX_HAVE_INET6) |
5478
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
162 r->ipv6 = 1; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
163 |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
164 ngx_rbtree_init(&r->addr6_rbtree, &r->addr6_sentinel, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
165 ngx_resolver_rbtree_insert_addr6_value); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
166 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
167 ngx_queue_init(&r->addr6_resend_queue); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
168 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
169 ngx_queue_init(&r->addr6_expire_queue); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
170 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
171 |
1649 | 172 r->event->handler = ngx_resolver_resend_handler; |
173 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
|
174 r->event->log = &cf->cycle->new_log; |
1649 | 175 r->ident = -1; |
176 | |
177 r->resend_timeout = 5; | |
6367 | 178 r->tcp_timeout = 5; |
1649 | 179 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
|
180 r->valid = 0; |
1649 | 181 |
2785
d478379e51ac
*) refactor error_log processing: listen socket log might inherit built-in
Igor Sysoev <igor@sysoev.ru>
parents:
2490
diff
changeset
|
182 r->log = &cf->cycle->new_log; |
3763
beca53d6ab3c
decrease resolver errors level to error
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
183 r->log_level = NGX_LOG_ERR; |
1649 | 184 |
4784
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
185 if (n) { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
186 if (ngx_array_init(&r->connections, cf->pool, n, |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
187 sizeof(ngx_resolver_connection_t)) |
4784
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
188 != NGX_OK) |
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
189 { |
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
190 return NULL; |
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
191 } |
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
192 } |
dd63abf20ba7
Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents:
4684
diff
changeset
|
193 |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
194 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
|
195 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
|
196 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
|
197 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
|
198 |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
199 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
|
200 |
4474 | 201 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
|
202 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
|
203 "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
|
204 return NULL; |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
205 } |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
206 |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
207 continue; |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
208 } |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
209 |
5478
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
210 #if (NGX_HAVE_INET6) |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
211 if (ngx_strncmp(names[i].data, "ipv6=", 5) == 0) { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
212 |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
213 if (ngx_strcmp(&names[i].data[5], "on") == 0) { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
214 r->ipv6 = 1; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
215 |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
216 } else if (ngx_strcmp(&names[i].data[5], "off") == 0) { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
217 r->ipv6 = 0; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
218 |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
219 } else { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
220 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
221 "invalid parameter: %V", &names[i]); |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
222 return NULL; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
223 } |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
224 |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
225 continue; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
226 } |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
227 #endif |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
228 |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
229 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
|
230 |
4671
af9342747669
Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents:
4653
diff
changeset
|
231 u.url = names[i]; |
af9342747669
Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents:
4653
diff
changeset
|
232 u.default_port = 53; |
af9342747669
Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents:
4653
diff
changeset
|
233 |
af9342747669
Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents:
4653
diff
changeset
|
234 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
|
235 if (u.err) { |
bc5f881323b8
Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents:
4619
diff
changeset
|
236 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
|
237 "%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
|
238 u.err, &u.url); |
4643
bc5f881323b8
Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents:
4619
diff
changeset
|
239 } |
bc5f881323b8
Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents:
4619
diff
changeset
|
240 |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
241 return NULL; |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
242 } |
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
243 |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
244 rec = ngx_array_push_n(&r->connections, u.naddrs); |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
245 if (rec == NULL) { |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
246 return NULL; |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
247 } |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
248 |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
249 ngx_memzero(rec, u.naddrs * sizeof(ngx_resolver_connection_t)); |
4684
f5c2c9d656f9
When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents:
4683
diff
changeset
|
250 |
f5c2c9d656f9
When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents:
4683
diff
changeset
|
251 for (j = 0; j < u.naddrs; j++) { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
252 rec[j].sockaddr = u.addrs[j].sockaddr; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
253 rec[j].socklen = u.addrs[j].socklen; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
254 rec[j].server = u.addrs[j].name; |
6367 | 255 rec[j].resolver = r; |
4684
f5c2c9d656f9
When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents:
4683
diff
changeset
|
256 } |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
257 } |
1649 | 258 |
259 return r; | |
260 } | |
261 | |
262 | |
1906 | 263 static void |
264 ngx_resolver_cleanup(void *data) | |
265 { | |
266 ngx_resolver_t *r = data; | |
267 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
268 ngx_uint_t i; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
269 ngx_resolver_connection_t *rec; |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
270 |
1906 | 271 if (r) { |
272 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
273 "cleanup resolver"); | |
274 | |
275 ngx_resolver_cleanup_tree(r, &r->name_rbtree); | |
276 | |
277 ngx_resolver_cleanup_tree(r, &r->addr_rbtree); | |
278 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
279 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
280 ngx_resolver_cleanup_tree(r, &r->addr6_rbtree); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
281 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
282 |
1906 | 283 if (r->event) { |
284 ngx_free(r->event); | |
285 } | |
286 | |
4225
016352c19049
Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents:
3902
diff
changeset
|
287 |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
288 rec = r->connections.elts; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
289 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
290 for (i = 0; i < r->connections.nelts; i++) { |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
291 if (rec[i].udp) { |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
292 ngx_close_connection(rec[i].udp); |
1906 | 293 } |
6367 | 294 |
295 if (rec[i].tcp) { | |
296 ngx_close_connection(rec[i].tcp); | |
297 } | |
6369
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
298 |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
299 if (rec[i].read_buf) { |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
300 ngx_resolver_free(r, rec[i].read_buf->start); |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
301 ngx_resolver_free(r, rec[i].read_buf); |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
302 } |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
303 |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
304 if (rec[i].write_buf) { |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
305 ngx_resolver_free(r, rec[i].write_buf->start); |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
306 ngx_resolver_free(r, rec[i].write_buf); |
06459bc8b92f
Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents:
6368
diff
changeset
|
307 } |
1906 | 308 } |
309 | |
310 ngx_free(r); | |
311 } | |
312 } | |
313 | |
314 | |
315 static void | |
316 ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree) | |
317 { | |
318 ngx_resolver_ctx_t *ctx, *next; | |
319 ngx_resolver_node_t *rn; | |
320 | |
321 while (tree->root != tree->sentinel) { | |
322 | |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
323 rn = ngx_resolver_node(ngx_rbtree_min(tree->root, tree->sentinel)); |
1906 | 324 |
325 ngx_queue_remove(&rn->queue); | |
326 | |
327 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
|
328 next = ctx->next; |
1906 | 329 |
330 if (ctx->event) { | |
331 ngx_resolver_free(r, ctx->event); | |
332 } | |
333 | |
334 ngx_resolver_free(r, ctx); | |
335 } | |
336 | |
337 ngx_rbtree_delete(tree, &rn->node); | |
338 | |
339 ngx_resolver_free_node(r, rn); | |
340 } | |
341 } | |
342 | |
343 | |
1649 | 344 ngx_resolver_ctx_t * |
345 ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp) | |
346 { | |
347 in_addr_t addr; | |
348 ngx_resolver_ctx_t *ctx; | |
349 | |
350 if (temp) { | |
351 addr = ngx_inet_addr(temp->name.data, temp->name.len); | |
352 | |
353 if (addr != INADDR_NONE) { | |
354 temp->resolver = r; | |
355 temp->state = NGX_OK; | |
356 temp->naddrs = 1; | |
357 temp->addrs = &temp->addr; | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
358 temp->addr.sockaddr = (struct sockaddr *) &temp->sin; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
359 temp->addr.socklen = sizeof(struct sockaddr_in); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
360 ngx_memzero(&temp->sin, sizeof(struct sockaddr_in)); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
361 temp->sin.sin_family = AF_INET; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
362 temp->sin.sin_addr.s_addr = addr; |
1649 | 363 temp->quick = 1; |
364 | |
365 return temp; | |
366 } | |
367 } | |
368 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
369 if (r->connections.nelts == 0) { |
1683
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
370 return NGX_NO_RESOLVER; |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
371 } |
1e0b028055ec
allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents:
1679
diff
changeset
|
372 |
1649 | 373 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); |
374 | |
375 if (ctx) { | |
376 ctx->resolver = r; | |
377 } | |
378 | |
379 return ctx; | |
380 } | |
381 | |
382 | |
383 ngx_int_t | |
384 ngx_resolve_name(ngx_resolver_ctx_t *ctx) | |
385 { | |
386 ngx_int_t rc; | |
387 ngx_resolver_t *r; | |
388 | |
389 r = ctx->resolver; | |
390 | |
5505
d091d16ed398
Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents:
5485
diff
changeset
|
391 if (ctx->name.len > 0 && ctx->name.data[ctx->name.len - 1] == '.') { |
d091d16ed398
Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents:
5485
diff
changeset
|
392 ctx->name.len--; |
d091d16ed398
Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents:
5485
diff
changeset
|
393 } |
d091d16ed398
Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents:
5485
diff
changeset
|
394 |
1649 | 395 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, |
396 "resolve: \"%V\"", &ctx->name); | |
397 | |
398 if (ctx->quick) { | |
399 ctx->handler(ctx); | |
400 return NGX_OK; | |
401 } | |
402 | |
403 /* lock name mutex */ | |
404 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
405 rc = ngx_resolve_name_locked(r, ctx, &ctx->name); |
1649 | 406 |
407 if (rc == NGX_OK) { | |
408 return NGX_OK; | |
409 } | |
410 | |
411 /* unlock name mutex */ | |
412 | |
413 if (rc == NGX_AGAIN) { | |
414 return NGX_OK; | |
415 } | |
416 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
417 /* NGX_ERROR */ |
1649 | 418 |
419 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
|
420 ngx_resolver_free(r, ctx->event); |
1649 | 421 } |
422 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
423 ngx_resolver_free(r, ctx); |
1649 | 424 |
425 return NGX_ERROR; | |
426 } | |
427 | |
428 | |
429 void | |
430 ngx_resolve_name_done(ngx_resolver_ctx_t *ctx) | |
431 { | |
432 ngx_resolver_t *r; | |
433 ngx_resolver_ctx_t *w, **p; | |
434 ngx_resolver_node_t *rn; | |
435 | |
436 r = ctx->resolver; | |
437 | |
438 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
439 "resolve name done: %i", ctx->state); | |
440 | |
441 if (ctx->quick) { | |
442 return; | |
443 } | |
444 | |
445 if (ctx->event && ctx->event->timer_set) { | |
446 ngx_del_timer(ctx->event); | |
447 } | |
448 | |
449 /* lock name mutex */ | |
450 | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
451 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { |
1649 | 452 |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
453 rn = ctx->node; |
1649 | 454 |
455 if (rn) { | |
456 p = &rn->waiting; | |
457 w = rn->waiting; | |
458 | |
459 while (w) { | |
460 if (w == ctx) { | |
461 *p = w->next; | |
462 | |
463 goto done; | |
464 } | |
465 | |
466 p = &w->next; | |
467 w = w->next; | |
468 } | |
469 } | |
470 | |
471 ngx_log_error(NGX_LOG_ALERT, r->log, 0, | |
472 "could not cancel %V resolving", &ctx->name); | |
473 } | |
474 | |
475 done: | |
476 | |
477 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue); | |
478 | |
479 /* unlock name mutex */ | |
480 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
481 /* 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
|
482 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
483 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
|
484 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
|
485 } |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
486 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
487 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
|
488 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
489 /* unlock alloc mutex */ |
6196
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
490 |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
491 if (r->event->timer_set && ngx_resolver_resend_empty(r)) { |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
492 ngx_del_timer(r->event); |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
493 } |
1649 | 494 } |
495 | |
496 | |
497 static ngx_int_t | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
498 ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx, |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
499 ngx_str_t *name) |
1649 | 500 { |
501 uint32_t hash; | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
502 ngx_int_t rc; |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
503 ngx_str_t cname; |
1649 | 504 ngx_uint_t naddrs; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
505 ngx_addr_t *addrs; |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
506 ngx_resolver_ctx_t *next, *last; |
1649 | 507 ngx_resolver_node_t *rn; |
508 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
509 ngx_strlow(name->data, name->data, name->len); |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
510 |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
511 hash = ngx_crc32_short(name->data, name->len); |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
512 |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
513 rn = ngx_resolver_lookup_name(r, name, hash); |
1649 | 514 |
515 if (rn) { | |
516 | |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
517 /* ctx can be a list after NGX_RESOLVE_CNAME */ |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
518 for (last = ctx; last->next; last = last->next); |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
519 |
1649 | 520 if (rn->valid >= ngx_time()) { |
521 | |
522 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached"); | |
523 | |
524 ngx_queue_remove(&rn->queue); | |
525 | |
526 rn->expire = ngx_time() + r->expire; | |
527 | |
528 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
529 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
530 naddrs = (rn->naddrs == (u_short) -1) ? 0 : rn->naddrs; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
531 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
532 naddrs += (rn->naddrs6 == (u_short) -1) ? 0 : rn->naddrs6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
533 #endif |
1649 | 534 |
535 if (naddrs) { | |
536 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
537 if (naddrs == 1 && rn->naddrs == 1) { |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
538 addrs = NULL; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
539 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
540 } else { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
541 addrs = ngx_resolver_export(r, rn, 1); |
1649 | 542 if (addrs == NULL) { |
543 return NGX_ERROR; | |
544 } | |
545 } | |
546 | |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
547 last->next = rn->waiting; |
1649 | 548 rn->waiting = NULL; |
549 | |
550 /* unlock name mutex */ | |
551 | |
552 do { | |
553 ctx->state = NGX_OK; | |
554 ctx->naddrs = naddrs; | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
555 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
556 if (addrs == NULL) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
557 ctx->addrs = &ctx->addr; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
558 ctx->addr.sockaddr = (struct sockaddr *) &ctx->sin; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
559 ctx->addr.socklen = sizeof(struct sockaddr_in); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
560 ngx_memzero(&ctx->sin, sizeof(struct sockaddr_in)); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
561 ctx->sin.sin_family = AF_INET; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
562 ctx->sin.sin_addr.s_addr = rn->u.addr; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
563 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
564 } else { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
565 ctx->addrs = addrs; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
566 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
567 |
1649 | 568 next = ctx->next; |
569 | |
570 ctx->handler(ctx); | |
571 | |
572 ctx = next; | |
573 } while (ctx); | |
574 | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
575 if (addrs != NULL) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
576 ngx_resolver_free(r, addrs->sockaddr); |
1649 | 577 ngx_resolver_free(r, addrs); |
578 } | |
579 | |
580 return NGX_OK; | |
581 } | |
582 | |
583 /* NGX_RESOLVE_CNAME */ | |
584 | |
1969 | 585 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) { |
586 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
587 cname.len = rn->cnlen; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
588 cname.data = rn->u.cname; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
589 |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
590 return ngx_resolve_name_locked(r, ctx, &cname); |
1969 | 591 } |
592 | |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
593 last->next = rn->waiting; |
1969 | 594 rn->waiting = NULL; |
595 | |
596 /* unlock name mutex */ | |
597 | |
598 do { | |
599 ctx->state = NGX_RESOLVE_NXDOMAIN; | |
600 next = ctx->next; | |
601 | |
602 ctx->handler(ctx); | |
603 | |
604 ctx = next; | |
605 } while (ctx); | |
606 | |
607 return NGX_OK; | |
1649 | 608 } |
609 | |
610 if (rn->waiting) { | |
611 | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
612 if (ctx->event == NULL) { |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
613 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
614 if (ctx->event == NULL) { |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
615 return NGX_ERROR; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
616 } |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
617 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
618 ctx->event->handler = ngx_resolver_timeout_handler; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
619 ctx->event->data = ctx; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
620 ctx->event->log = r->log; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
621 ctx->ident = -1; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
622 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
623 ngx_add_timer(ctx->event, ctx->timeout); |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
624 } |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
625 |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
626 last->next = rn->waiting; |
1649 | 627 rn->waiting = ctx; |
3297 | 628 ctx->state = NGX_AGAIN; |
1649 | 629 |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
630 do { |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
631 ctx->node = rn; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
632 ctx = ctx->next; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
633 } while (ctx); |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
634 |
1649 | 635 return NGX_AGAIN; |
636 } | |
637 | |
638 ngx_queue_remove(&rn->queue); | |
639 | |
640 /* lock alloc mutex */ | |
641 | |
4619
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
642 if (rn->query) { |
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
643 ngx_resolver_free_locked(r, rn->query); |
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
644 rn->query = NULL; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
645 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
646 rn->query6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
647 #endif |
4619
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
648 } |
1649 | 649 |
650 if (rn->cnlen) { | |
651 ngx_resolver_free_locked(r, rn->u.cname); | |
652 } | |
653 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
654 if (rn->naddrs > 1 && rn->naddrs != (u_short) -1) { |
1649 | 655 ngx_resolver_free_locked(r, rn->u.addrs); |
656 } | |
657 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
658 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
659 if (rn->naddrs6 > 1 && rn->naddrs6 != (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
660 ngx_resolver_free_locked(r, rn->u6.addrs6); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
661 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
662 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
663 |
1649 | 664 /* unlock alloc mutex */ |
665 | |
666 } else { | |
667 | |
668 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t)); | |
669 if (rn == NULL) { | |
670 return NGX_ERROR; | |
671 } | |
672 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
673 rn->name = ngx_resolver_dup(r, name->data, name->len); |
1649 | 674 if (rn->name == NULL) { |
675 ngx_resolver_free(r, rn); | |
676 return NGX_ERROR; | |
677 } | |
678 | |
679 rn->node.key = hash; | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
680 rn->nlen = (u_short) name->len; |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
681 rn->query = NULL; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
682 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
683 rn->query6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
684 #endif |
1649 | 685 |
686 ngx_rbtree_insert(&r->name_rbtree, &rn->node); | |
687 } | |
688 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
689 rc = ngx_resolver_create_name_query(r, rn, name); |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
690 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
691 if (rc == NGX_ERROR) { |
1649 | 692 goto failed; |
693 } | |
694 | |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
695 if (rc == NGX_DECLINED) { |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
696 ngx_rbtree_delete(&r->name_rbtree, &rn->node); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
697 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
698 ngx_resolver_free(r, rn->query); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
699 ngx_resolver_free(r, rn->name); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
700 ngx_resolver_free(r, rn); |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
701 |
6349
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
702 do { |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
703 ctx->state = NGX_RESOLVE_NXDOMAIN; |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
704 next = ctx->next; |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
705 |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
706 ctx->handler(ctx); |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
707 |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
708 ctx = next; |
978e79b95c9f
Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents:
6348
diff
changeset
|
709 } while (ctx); |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
710 |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
711 return NGX_OK; |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
712 } |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
713 |
6366
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
714 rn->last_connection = r->last_connection++; |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
715 if (r->last_connection == r->connections.nelts) { |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
716 r->last_connection = 0; |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
717 } |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
718 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
719 rn->naddrs = (u_short) -1; |
6367 | 720 rn->tcp = 0; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
721 #if (NGX_HAVE_INET6) |
5478
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
722 rn->naddrs6 = r->ipv6 ? (u_short) -1 : 0; |
6367 | 723 rn->tcp6 = 0; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
724 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
725 |
1649 | 726 if (ngx_resolver_send_query(r, rn) != NGX_OK) { |
727 goto failed; | |
728 } | |
729 | |
730 if (ctx->event == NULL) { | |
731 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); | |
732 if (ctx->event == NULL) { | |
733 goto failed; | |
734 } | |
735 | |
736 ctx->event->handler = ngx_resolver_timeout_handler; | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
737 ctx->event->data = ctx; |
1649 | 738 ctx->event->log = r->log; |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
739 ctx->ident = -1; |
1649 | 740 |
741 ngx_add_timer(ctx->event, ctx->timeout); | |
742 } | |
743 | |
744 if (ngx_queue_empty(&r->name_resend_queue)) { | |
745 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000)); | |
746 } | |
747 | |
748 rn->expire = ngx_time() + r->resend_timeout; | |
749 | |
750 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue); | |
751 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
752 rn->code = 0; |
1649 | 753 rn->cnlen = 0; |
754 rn->valid = 0; | |
5485
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
755 rn->ttl = NGX_MAX_UINT32_VALUE; |
1649 | 756 rn->waiting = ctx; |
757 | |
758 ctx->state = NGX_AGAIN; | |
759 | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
760 do { |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
761 ctx->node = rn; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
762 ctx = ctx->next; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
763 } while (ctx); |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
764 |
1649 | 765 return NGX_AGAIN; |
766 | |
767 failed: | |
768 | |
769 ngx_rbtree_delete(&r->name_rbtree, &rn->node); | |
770 | |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
771 if (rn->query) { |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
772 ngx_resolver_free(r, rn->query); |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
773 } |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
774 |
1649 | 775 ngx_resolver_free(r, rn->name); |
776 | |
777 ngx_resolver_free(r, rn); | |
778 | |
779 return NGX_ERROR; | |
780 } | |
583 | 781 |
782 | |
783 ngx_int_t | |
1649 | 784 ngx_resolve_addr(ngx_resolver_ctx_t *ctx) |
785 { | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
786 u_char *name; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
787 in_addr_t addr; |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
788 ngx_queue_t *resend_queue, *expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
789 ngx_rbtree_t *tree; |
1649 | 790 ngx_resolver_t *r; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
791 struct sockaddr_in *sin; |
1649 | 792 ngx_resolver_node_t *rn; |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
793 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
794 uint32_t hash; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
795 struct sockaddr_in6 *sin6; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
796 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
797 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
798 #if (NGX_SUPPRESS_WARN) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
799 addr = 0; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
800 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
801 hash = 0; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
802 sin6 = NULL; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
803 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
804 #endif |
1649 | 805 |
806 r = ctx->resolver; | |
807 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
808 switch (ctx->addr.sockaddr->sa_family) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
809 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
810 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
811 case AF_INET6: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
812 sin6 = (struct sockaddr_in6 *) ctx->addr.sockaddr; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
813 hash = ngx_crc32_short(sin6->sin6_addr.s6_addr, 16); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
814 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
815 /* lock addr mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
816 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
817 rn = ngx_resolver_lookup_addr6(r, &sin6->sin6_addr, hash); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
818 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
819 tree = &r->addr6_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
820 resend_queue = &r->addr6_resend_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
821 expire_queue = &r->addr6_expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
822 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
823 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
824 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
825 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
826 default: /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
827 sin = (struct sockaddr_in *) ctx->addr.sockaddr; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
828 addr = ntohl(sin->sin_addr.s_addr); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
829 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
830 /* lock addr mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
831 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
832 rn = ngx_resolver_lookup_addr(r, addr); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
833 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
834 tree = &r->addr_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
835 resend_queue = &r->addr_resend_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
836 expire_queue = &r->addr_expire_queue; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
837 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
838 |
1649 | 839 if (rn) { |
840 | |
841 if (rn->valid >= ngx_time()) { | |
842 | |
843 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached"); | |
844 | |
845 ngx_queue_remove(&rn->queue); | |
846 | |
847 rn->expire = ngx_time() + r->expire; | |
848 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
849 ngx_queue_insert_head(expire_queue, &rn->queue); |
1649 | 850 |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
851 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
|
852 if (name == NULL) { |
1649 | 853 goto failed; |
854 } | |
855 | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
856 ctx->name.len = rn->nlen; |
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
857 ctx->name.data = name; |
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
858 |
1649 | 859 /* unlock addr mutex */ |
860 | |
861 ctx->state = NGX_OK; | |
862 | |
863 ctx->handler(ctx); | |
864 | |
2484
cf3cd450049c
store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents:
2483
diff
changeset
|
865 ngx_resolver_free(r, name); |
1649 | 866 |
867 return NGX_OK; | |
868 } | |
869 | |
870 if (rn->waiting) { | |
871 | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
872 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
873 if (ctx->event == NULL) { |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
874 return NGX_ERROR; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
875 } |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
876 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
877 ctx->event->handler = ngx_resolver_timeout_handler; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
878 ctx->event->data = ctx; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
879 ctx->event->log = r->log; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
880 ctx->ident = -1; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
881 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
882 ngx_add_timer(ctx->event, ctx->timeout); |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
883 |
1649 | 884 ctx->next = rn->waiting; |
885 rn->waiting = ctx; | |
3297 | 886 ctx->state = NGX_AGAIN; |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
887 ctx->node = rn; |
1649 | 888 |
2487
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
889 /* unlock addr mutex */ |
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
890 |
9b4dce95c744
fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents:
2486
diff
changeset
|
891 return NGX_OK; |
1649 | 892 } |
893 | |
894 ngx_queue_remove(&rn->queue); | |
895 | |
896 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
|
897 rn->query = NULL; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
898 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
899 rn->query6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
900 #endif |
1649 | 901 |
902 } else { | |
903 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t)); | |
904 if (rn == NULL) { | |
905 goto failed; | |
906 } | |
907 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
908 switch (ctx->addr.sockaddr->sa_family) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
909 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
910 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
911 case AF_INET6: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
912 rn->addr6 = sin6->sin6_addr; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
913 rn->node.key = hash; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
914 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
915 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
916 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
917 default: /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
918 rn->node.key = addr; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
919 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
920 |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
921 rn->query = NULL; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
922 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
923 rn->query6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
924 #endif |
1649 | 925 |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
926 ngx_rbtree_insert(tree, &rn->node); |
1649 | 927 } |
928 | |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
929 if (ngx_resolver_create_addr_query(r, rn, &ctx->addr) != NGX_OK) { |
1649 | 930 goto failed; |
931 } | |
932 | |
6366
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
933 rn->last_connection = r->last_connection++; |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
934 if (r->last_connection == r->connections.nelts) { |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
935 r->last_connection = 0; |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
936 } |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
937 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
938 rn->naddrs = (u_short) -1; |
6367 | 939 rn->tcp = 0; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
940 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
941 rn->naddrs6 = (u_short) -1; |
6367 | 942 rn->tcp6 = 0; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
943 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
944 |
1649 | 945 if (ngx_resolver_send_query(r, rn) != NGX_OK) { |
946 goto failed; | |
947 } | |
948 | |
949 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t)); | |
950 if (ctx->event == NULL) { | |
951 goto failed; | |
952 } | |
953 | |
954 ctx->event->handler = ngx_resolver_timeout_handler; | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
955 ctx->event->data = ctx; |
1649 | 956 ctx->event->log = r->log; |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
957 ctx->ident = -1; |
1649 | 958 |
959 ngx_add_timer(ctx->event, ctx->timeout); | |
960 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
961 if (ngx_queue_empty(resend_queue)) { |
1649 | 962 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000)); |
963 } | |
964 | |
965 rn->expire = ngx_time() + r->resend_timeout; | |
966 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
967 ngx_queue_insert_head(resend_queue, &rn->queue); |
1649 | 968 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
969 rn->code = 0; |
1649 | 970 rn->cnlen = 0; |
971 rn->name = NULL; | |
972 rn->nlen = 0; | |
973 rn->valid = 0; | |
5485
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
974 rn->ttl = NGX_MAX_UINT32_VALUE; |
1649 | 975 rn->waiting = ctx; |
976 | |
977 /* unlock addr mutex */ | |
978 | |
979 ctx->state = NGX_AGAIN; | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
980 ctx->node = rn; |
1649 | 981 |
982 return NGX_OK; | |
983 | |
984 failed: | |
985 | |
986 if (rn) { | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
987 ngx_rbtree_delete(tree, &rn->node); |
1649 | 988 |
1960
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
989 if (rn->query) { |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
990 ngx_resolver_free(r, rn->query); |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
991 } |
1609b3c3d604
fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents:
1914
diff
changeset
|
992 |
1649 | 993 ngx_resolver_free(r, rn); |
994 } | |
995 | |
996 /* unlock addr mutex */ | |
997 | |
998 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
|
999 ngx_resolver_free(r, ctx->event); |
1649 | 1000 } |
1001 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1002 ngx_resolver_free(r, ctx); |
1649 | 1003 |
1004 return NGX_ERROR; | |
1005 } | |
1006 | |
1007 | |
1008 void | |
1009 ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx) | |
1010 { | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1011 ngx_queue_t *expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1012 ngx_rbtree_t *tree; |
1649 | 1013 ngx_resolver_t *r; |
1014 ngx_resolver_ctx_t *w, **p; | |
1015 ngx_resolver_node_t *rn; | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
1016 |
1649 | 1017 r = ctx->resolver; |
1018 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1019 switch (ctx->addr.sockaddr->sa_family) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1020 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1021 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1022 case AF_INET6: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1023 tree = &r->addr6_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1024 expire_queue = &r->addr6_expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1025 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1026 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1027 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1028 default: /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1029 tree = &r->addr_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1030 expire_queue = &r->addr_expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1031 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1032 |
1649 | 1033 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, |
1034 "resolve addr done: %i", ctx->state); | |
1035 | |
1036 if (ctx->event && ctx->event->timer_set) { | |
1037 ngx_del_timer(ctx->event); | |
1038 } | |
1039 | |
1040 /* lock addr mutex */ | |
1041 | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
1042 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { |
1649 | 1043 |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
1044 rn = ctx->node; |
1649 | 1045 |
1046 if (rn) { | |
1047 p = &rn->waiting; | |
1048 w = rn->waiting; | |
1049 | |
1050 while (w) { | |
1051 if (w == ctx) { | |
1052 *p = w->next; | |
1053 | |
1054 goto done; | |
1055 } | |
1056 | |
1057 p = &w->next; | |
1058 w = w->next; | |
1059 } | |
1060 } | |
1061 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1062 { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1063 u_char text[NGX_SOCKADDR_STRLEN]; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1064 ngx_str_t addrtext; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1065 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1066 addrtext.data = text; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1067 addrtext.len = ngx_sock_ntop(ctx->addr.sockaddr, ctx->addr.socklen, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1068 text, NGX_SOCKADDR_STRLEN, 0); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1069 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1070 ngx_log_error(NGX_LOG_ALERT, r->log, 0, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1071 "could not cancel %V resolving", &addrtext); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1072 } |
1649 | 1073 } |
1074 | |
1075 done: | |
1076 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1077 ngx_resolver_expire(r, tree, expire_queue); |
1649 | 1078 |
1079 /* unlock addr mutex */ | |
1080 | |
1904
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1081 /* 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
|
1082 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1083 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
|
1084 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
|
1085 } |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1086 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1087 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
|
1088 |
538f06aa8118
fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents:
1903
diff
changeset
|
1089 /* unlock alloc mutex */ |
6196
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1090 |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1091 if (r->event->timer_set && ngx_resolver_resend_empty(r)) { |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1092 ngx_del_timer(r->event); |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1093 } |
1649 | 1094 } |
1095 | |
1096 | |
1097 static void | |
1098 ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue) | |
1099 { | |
1100 time_t now; | |
1101 ngx_uint_t i; | |
1102 ngx_queue_t *q; | |
1103 ngx_resolver_node_t *rn; | |
1104 | |
1105 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire"); | |
1106 | |
1107 now = ngx_time(); | |
1108 | |
1109 for (i = 0; i < 2; i++) { | |
1110 if (ngx_queue_empty(queue)) { | |
1111 return; | |
1112 } | |
1113 | |
1114 q = ngx_queue_last(queue); | |
1115 | |
1116 rn = ngx_queue_data(q, ngx_resolver_node_t, queue); | |
1117 | |
1118 if (now <= rn->expire) { | |
1119 return; | |
1120 } | |
1121 | |
1774 | 1122 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, |
1123 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name); | |
1649 | 1124 |
1125 ngx_queue_remove(q); | |
1126 | |
1127 ngx_rbtree_delete(tree, &rn->node); | |
1128 | |
1129 ngx_resolver_free_node(r, rn); | |
1130 } | |
1131 } | |
1132 | |
1133 | |
1134 static ngx_int_t | |
1135 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) | |
1136 { | |
6367 | 1137 ngx_int_t rc; |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1138 ngx_resolver_connection_t *rec; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1139 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1140 rec = r->connections.elts; |
6366
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
1141 rec = &rec[rn->last_connection]; |
1649 | 1142 |
6367 | 1143 if (rec->log.handler == NULL) { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1144 rec->log = *r->log; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1145 rec->log.handler = ngx_resolver_log_error; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1146 rec->log.data = rec; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
1147 rec->log.action = "resolving"; |
1649 | 1148 } |
1149 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1150 if (rn->naddrs == (u_short) -1) { |
6367 | 1151 rc = rn->tcp ? ngx_resolver_send_tcp_query(r, rec, rn->query, rn->qlen) |
1152 : ngx_resolver_send_udp_query(r, rec, rn->query, rn->qlen); | |
1153 | |
1154 if (rc != NGX_OK) { | |
1155 return rc; | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1156 } |
1649 | 1157 } |
1158 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1159 #if (NGX_HAVE_INET6) |
6367 | 1160 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1161 if (rn->query6 && rn->naddrs6 == (u_short) -1) { |
6367 | 1162 rc = rn->tcp6 |
1163 ? ngx_resolver_send_tcp_query(r, rec, rn->query6, rn->qlen) | |
1164 : ngx_resolver_send_udp_query(r, rec, rn->query6, rn->qlen); | |
1165 | |
1166 if (rc != NGX_OK) { | |
1167 return rc; | |
1168 } | |
1169 } | |
1170 | |
1171 #endif | |
1172 | |
1173 return NGX_OK; | |
1174 } | |
1175 | |
1176 | |
1177 static ngx_int_t | |
1178 ngx_resolver_send_udp_query(ngx_resolver_t *r, ngx_resolver_connection_t *rec, | |
1179 u_char *query, u_short qlen) | |
1180 { | |
1181 ssize_t n; | |
1182 | |
1183 if (rec->udp == NULL) { | |
1184 if (ngx_udp_connect(rec) != NGX_OK) { | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1185 return NGX_ERROR; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1186 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1187 |
6367 | 1188 rec->udp->data = rec; |
1189 rec->udp->read->handler = ngx_resolver_udp_read; | |
1190 rec->udp->read->resolver = 1; | |
1191 } | |
1192 | |
1193 n = ngx_send(rec->udp, query, qlen); | |
1194 | |
1195 if (n == -1) { | |
1196 return NGX_ERROR; | |
1197 } | |
1198 | |
1199 if ((size_t) n != (size_t) qlen) { | |
1200 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "send() incomplete"); | |
1201 return NGX_ERROR; | |
1202 } | |
1203 | |
1204 return NGX_OK; | |
1205 } | |
1206 | |
1207 | |
1208 static ngx_int_t | |
1209 ngx_resolver_send_tcp_query(ngx_resolver_t *r, ngx_resolver_connection_t *rec, | |
1210 u_char *query, u_short qlen) | |
1211 { | |
1212 ngx_buf_t *b; | |
1213 ngx_int_t rc; | |
1214 | |
1215 rc = NGX_OK; | |
1216 | |
1217 if (rec->tcp == NULL) { | |
1218 b = rec->read_buf; | |
1219 | |
1220 if (b == NULL) { | |
1221 b = ngx_resolver_calloc(r, sizeof(ngx_buf_t)); | |
1222 if (b == NULL) { | |
1223 return NGX_ERROR; | |
1224 } | |
1225 | |
1226 b->start = ngx_resolver_alloc(r, NGX_RESOLVER_TCP_RSIZE); | |
1227 if (b->start == NULL) { | |
6368
d73f77bb5caf
Resolver: fixed possible resource leak introduced in 5a16d40c63de.
Ruslan Ermilov <ru@nginx.com>
parents:
6367
diff
changeset
|
1228 ngx_resolver_free(r, b); |
6367 | 1229 return NGX_ERROR; |
1230 } | |
1231 | |
1232 b->end = b->start + NGX_RESOLVER_TCP_RSIZE; | |
1233 | |
1234 rec->read_buf = b; | |
1235 } | |
1236 | |
1237 b->pos = b->start; | |
1238 b->last = b->start; | |
1239 | |
1240 b = rec->write_buf; | |
1241 | |
1242 if (b == NULL) { | |
1243 b = ngx_resolver_calloc(r, sizeof(ngx_buf_t)); | |
1244 if (b == NULL) { | |
1245 return NGX_ERROR; | |
1246 } | |
1247 | |
1248 b->start = ngx_resolver_alloc(r, NGX_RESOLVER_TCP_WSIZE); | |
1249 if (b->start == NULL) { | |
6368
d73f77bb5caf
Resolver: fixed possible resource leak introduced in 5a16d40c63de.
Ruslan Ermilov <ru@nginx.com>
parents:
6367
diff
changeset
|
1250 ngx_resolver_free(r, b); |
6367 | 1251 return NGX_ERROR; |
1252 } | |
1253 | |
1254 b->end = b->start + NGX_RESOLVER_TCP_WSIZE; | |
1255 | |
1256 rec->write_buf = b; | |
1257 } | |
1258 | |
1259 b->pos = b->start; | |
1260 b->last = b->start; | |
1261 | |
1262 rc = ngx_tcp_connect(rec); | |
1263 if (rc == NGX_ERROR) { | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1264 return NGX_ERROR; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1265 } |
6367 | 1266 |
1267 rec->tcp->data = rec; | |
1268 rec->tcp->write->handler = ngx_resolver_tcp_write; | |
1269 rec->tcp->read->handler = ngx_resolver_tcp_read; | |
1270 rec->tcp->read->resolver = 1; | |
1271 | |
1272 ngx_add_timer(rec->tcp->write, (ngx_msec_t) (r->tcp_timeout * 1000)); | |
1649 | 1273 } |
6367 | 1274 |
1275 b = rec->write_buf; | |
1276 | |
1277 if (b->end - b->last < 2 + qlen) { | |
1278 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "buffer overflow"); | |
1279 return NGX_ERROR; | |
1280 } | |
1281 | |
1282 *b->last++ = (u_char) (qlen >> 8); | |
1283 *b->last++ = (u_char) qlen; | |
1284 b->last = ngx_cpymem(b->last, query, qlen); | |
1285 | |
1286 if (rc == NGX_OK) { | |
1287 ngx_resolver_tcp_write(rec->tcp->write); | |
1288 } | |
1649 | 1289 |
1290 return NGX_OK; | |
1291 } | |
1292 | |
1293 | |
1294 static void | |
1295 ngx_resolver_resend_handler(ngx_event_t *ev) | |
1296 { | |
1297 time_t timer, atimer, ntimer; | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1298 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1299 time_t a6timer; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1300 #endif |
1649 | 1301 ngx_resolver_t *r; |
1302 | |
1303 r = ev->data; | |
1304 | |
1305 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, | |
1306 "resolver resend handler"); | |
1307 | |
1308 /* lock name mutex */ | |
1309 | |
1310 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue); | |
1311 | |
1312 /* unlock name mutex */ | |
1313 | |
1314 /* lock addr mutex */ | |
1315 | |
1316 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
|
1317 |
1649 | 1318 /* unlock addr mutex */ |
1319 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1320 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1321 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1322 /* lock addr6 mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1323 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1324 a6timer = ngx_resolver_resend(r, &r->addr6_rbtree, &r->addr6_resend_queue); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1325 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1326 /* unlock addr6 mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1327 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1328 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1329 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1330 timer = ntimer; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1331 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1332 if (timer == 0) { |
1649 | 1333 timer = atimer; |
1334 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1335 } else if (atimer) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1336 timer = ngx_min(timer, atimer); |
1649 | 1337 } |
1338 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1339 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1340 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1341 if (timer == 0) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1342 timer = a6timer; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1343 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1344 } else if (a6timer) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1345 timer = ngx_min(timer, a6timer); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1346 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1347 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1348 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
1349 |
1649 | 1350 if (timer) { |
1351 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000)); | |
1352 } | |
1353 } | |
1354 | |
1355 | |
1356 static time_t | |
1357 ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue) | |
1358 { | |
1359 time_t now; | |
1360 ngx_queue_t *q; | |
1361 ngx_resolver_node_t *rn; | |
1362 | |
1363 now = ngx_time(); | |
1364 | |
1365 for ( ;; ) { | |
1366 if (ngx_queue_empty(queue)) { | |
1367 return 0; | |
1368 } | |
1369 | |
1370 q = ngx_queue_last(queue); | |
1371 | |
1372 rn = ngx_queue_data(q, ngx_resolver_node_t, queue); | |
1373 | |
1374 if (now < rn->expire) { | |
1375 return rn->expire - now; | |
1376 } | |
1377 | |
1774 | 1378 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, |
1379 "resolver resend \"%*s\" %p", | |
1380 (size_t) rn->nlen, rn->name, rn->waiting); | |
1649 | 1381 |
1382 ngx_queue_remove(q); | |
1383 | |
1384 if (rn->waiting) { | |
1385 | |
6366
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
1386 if (++rn->last_connection == r->connections.nelts) { |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
1387 rn->last_connection = 0; |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
1388 } |
2e5c027f2a98
Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents:
6365
diff
changeset
|
1389 |
4683
84d8e60b65f0
Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents:
4671
diff
changeset
|
1390 (void) ngx_resolver_send_query(r, rn); |
84d8e60b65f0
Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents:
4671
diff
changeset
|
1391 |
84d8e60b65f0
Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents:
4671
diff
changeset
|
1392 rn->expire = now + r->resend_timeout; |
84d8e60b65f0
Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents:
4671
diff
changeset
|
1393 |
84d8e60b65f0
Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents:
4671
diff
changeset
|
1394 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
|
1395 |
cf4ee321d195
do not delete failed DNS request if there are waiting clients
Igor Sysoev <igor@sysoev.ru>
parents:
1878
diff
changeset
|
1396 continue; |
1649 | 1397 } |
1398 | |
1399 ngx_rbtree_delete(tree, &rn->node); | |
1400 | |
1401 ngx_resolver_free_node(r, rn); | |
1402 } | |
1403 } | |
1404 | |
1405 | |
6196
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1406 static ngx_uint_t |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1407 ngx_resolver_resend_empty(ngx_resolver_t *r) |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1408 { |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1409 return ngx_queue_empty(&r->name_resend_queue) |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1410 #if (NGX_HAVE_INET6) |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1411 && ngx_queue_empty(&r->addr6_resend_queue) |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1412 #endif |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1413 && ngx_queue_empty(&r->addr_resend_queue); |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1414 } |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1415 |
c3ec43580a48
Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6126
diff
changeset
|
1416 |
1649 | 1417 static void |
6367 | 1418 ngx_resolver_udp_read(ngx_event_t *rev) |
1649 | 1419 { |
6367 | 1420 ssize_t n; |
1421 ngx_connection_t *c; | |
1422 ngx_resolver_connection_t *rec; | |
1423 u_char buf[NGX_RESOLVER_UDP_SIZE]; | |
1649 | 1424 |
1425 c = rev->data; | |
6367 | 1426 rec = c->data; |
1649 | 1427 |
1428 do { | |
1689 | 1429 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE); |
1430 | |
1431 if (n < 0) { | |
1649 | 1432 return; |
1433 } | |
1434 | |
6367 | 1435 ngx_resolver_process_response(rec->resolver, buf, n, 0); |
1649 | 1436 |
1437 } while (rev->ready); | |
1438 } | |
1439 | |
1440 | |
1441 static void | |
6367 | 1442 ngx_resolver_tcp_write(ngx_event_t *wev) |
1443 { | |
1444 off_t sent; | |
1445 ssize_t n; | |
1446 ngx_buf_t *b; | |
1447 ngx_resolver_t *r; | |
1448 ngx_connection_t *c; | |
1449 ngx_resolver_connection_t *rec; | |
1450 | |
1451 c = wev->data; | |
1452 rec = c->data; | |
1453 b = rec->write_buf; | |
1454 r = rec->resolver; | |
1455 | |
1456 if (wev->timedout) { | |
1457 goto failed; | |
1458 } | |
1459 | |
1460 sent = c->sent; | |
1461 | |
1462 while (wev->ready && b->pos < b->last) { | |
1463 n = ngx_send(c, b->pos, b->last - b->pos); | |
1464 | |
1465 if (n == NGX_AGAIN) { | |
1466 break; | |
1467 } | |
1468 | |
1469 if (n == NGX_ERROR) { | |
1470 goto failed; | |
1471 } | |
1472 | |
1473 b->pos += n; | |
1474 } | |
1475 | |
1476 if (b->pos != b->start) { | |
1477 b->last = ngx_movemem(b->start, b->pos, b->last - b->pos); | |
1478 b->pos = b->start; | |
1479 } | |
1480 | |
1481 if (c->sent != sent) { | |
1482 ngx_add_timer(wev, (ngx_msec_t) (r->tcp_timeout * 1000)); | |
1483 } | |
1484 | |
1485 if (ngx_handle_write_event(wev, 0) != NGX_OK) { | |
1486 goto failed; | |
1487 } | |
1488 | |
1489 return; | |
1490 | |
1491 failed: | |
1492 | |
1493 ngx_close_connection(c); | |
1494 rec->tcp = NULL; | |
1495 } | |
1496 | |
1497 | |
1498 static void | |
1499 ngx_resolver_tcp_read(ngx_event_t *rev) | |
1500 { | |
1501 u_char *p; | |
1502 size_t size; | |
1503 ssize_t n; | |
1504 u_short qlen; | |
1505 ngx_buf_t *b; | |
1506 ngx_resolver_t *r; | |
1507 ngx_connection_t *c; | |
1508 ngx_resolver_connection_t *rec; | |
1509 | |
1510 c = rev->data; | |
1511 rec = c->data; | |
1512 b = rec->read_buf; | |
1513 r = rec->resolver; | |
1514 | |
1515 while (rev->ready) { | |
1516 n = ngx_recv(c, b->last, b->end - b->last); | |
1517 | |
1518 if (n == NGX_AGAIN) { | |
1519 break; | |
1520 } | |
1521 | |
1522 if (n == NGX_ERROR || n == 0) { | |
1523 goto failed; | |
1524 } | |
1525 | |
1526 b->last += n; | |
1527 | |
1528 for ( ;; ) { | |
1529 p = b->pos; | |
1530 size = b->last - p; | |
1531 | |
1532 if (size < 2) { | |
1533 break; | |
1534 } | |
1535 | |
1536 qlen = (u_short) *p++ << 8; | |
1537 qlen += *p++; | |
1538 | |
1539 if (size < (size_t) (2 + qlen)) { | |
1540 break; | |
1541 } | |
1542 | |
1543 ngx_resolver_process_response(r, p, qlen, 1); | |
1544 | |
1545 b->pos += 2 + qlen; | |
1546 } | |
1547 | |
1548 if (b->pos != b->start) { | |
1549 b->last = ngx_movemem(b->start, b->pos, b->last - b->pos); | |
1550 b->pos = b->start; | |
1551 } | |
1552 } | |
1553 | |
1554 if (ngx_handle_read_event(rev, 0) != NGX_OK) { | |
1555 goto failed; | |
1556 } | |
1557 | |
1558 return; | |
1559 | |
1560 failed: | |
1561 | |
1562 ngx_close_connection(c); | |
1563 rec->tcp = NULL; | |
1564 } | |
1565 | |
1566 | |
1567 static void | |
1568 ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n, | |
1569 ngx_uint_t tcp) | |
1649 | 1570 { |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1571 char *err; |
6367 | 1572 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan, trunc, |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1573 qtype, qclass; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1574 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1575 ngx_uint_t qident6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1576 #endif |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1577 ngx_queue_t *q; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1578 ngx_resolver_qs_t *qs; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1579 ngx_resolver_hdr_t *response; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1580 ngx_resolver_node_t *rn; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1581 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1582 if (n < sizeof(ngx_resolver_hdr_t)) { |
1649 | 1583 goto short_response; |
1584 } | |
1585 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1586 response = (ngx_resolver_hdr_t *) buf; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1587 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1588 ident = (response->ident_hi << 8) + response->ident_lo; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1589 flags = (response->flags_hi << 8) + response->flags_lo; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1590 nqs = (response->nqs_hi << 8) + response->nqs_lo; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1591 nan = (response->nan_hi << 8) + response->nan_lo; |
6367 | 1592 trunc = flags & 0x0200; |
1649 | 1593 |
1594 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
|
1595 "resolver DNS response %ui fl:%04Xui %ui/%ui/%ud/%ud", |
1649 | 1596 ident, flags, nqs, nan, |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1597 (response->nns_hi << 8) + response->nns_lo, |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1598 (response->nar_hi << 8) + response->nar_lo); |
1649 | 1599 |
5470
aebdca7e8f8f
Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5469
diff
changeset
|
1600 /* response to a standard query */ |
6367 | 1601 if ((flags & 0xf870) != 0x8000 || (trunc && tcp)) { |
1649 | 1602 ngx_log_error(r->log_level, r->log, 0, |
6367 | 1603 "invalid %s DNS response %ui fl:%04Xui", |
1604 tcp ? "TCP" : "UDP", ident, flags); | |
1649 | 1605 return; |
1606 } | |
1607 | |
5470
aebdca7e8f8f
Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5469
diff
changeset
|
1608 code = flags & 0xf; |
1649 | 1609 |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1610 if (code == NGX_RESOLVE_FORMERR) { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1611 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1612 times = 0; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1613 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1614 for (q = ngx_queue_head(&r->name_resend_queue); |
6347
81d44cd4044e
Resolver: fixed possible segmentation fault on DNS format error.
Roman Arutyunyan <arut@nginx.com>
parents:
6196
diff
changeset
|
1615 q != ngx_queue_sentinel(&r->name_resend_queue) && times++ < 100; |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1616 q = ngx_queue_next(q)) |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1617 { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1618 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
|
1619 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
|
1620 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1621 if (qident == ident) { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1622 goto dns_error_name; |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1623 } |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1624 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1625 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1626 if (rn->query6) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1627 qident6 = (rn->query6[0] << 8) + rn->query6[1]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1628 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1629 if (qident6 == ident) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1630 goto dns_error_name; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1631 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1632 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1633 #endif |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1634 } |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1635 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1636 goto dns_error; |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1637 } |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1638 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1639 if (code > NGX_RESOLVE_REFUSED) { |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1640 goto dns_error; |
1649 | 1641 } |
1642 | |
1643 if (nqs != 1) { | |
1644 err = "invalid number of questions in DNS response"; | |
1645 goto done; | |
1646 } | |
1647 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1648 i = sizeof(ngx_resolver_hdr_t); |
1649 | 1649 |
1650 while (i < (ngx_uint_t) n) { | |
1651 if (buf[i] == '\0') { | |
1652 goto found; | |
1653 } | |
1654 | |
5470
aebdca7e8f8f
Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5469
diff
changeset
|
1655 i += 1 + buf[i]; |
1649 | 1656 } |
1657 | |
1658 goto short_response; | |
1659 | |
1660 found: | |
1661 | |
5470
aebdca7e8f8f
Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5469
diff
changeset
|
1662 if (i++ == sizeof(ngx_resolver_hdr_t)) { |
1649 | 1663 err = "zero-length domain name in DNS response"; |
1664 goto done; | |
1665 } | |
1666 | |
1667 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t)) | |
1668 > (ngx_uint_t) n) | |
1669 { | |
1670 goto short_response; | |
1671 } | |
1672 | |
1673 qs = (ngx_resolver_qs_t *) &buf[i]; | |
1674 | |
1675 qtype = (qs->type_hi << 8) + qs->type_lo; | |
1676 qclass = (qs->class_hi << 8) + qs->class_lo; | |
1677 | |
1678 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
|
1679 "resolver DNS response qt:%ui cl:%ui", qtype, qclass); |
1649 | 1680 |
1681 if (qclass != 1) { | |
1682 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
|
1683 "unknown query class %ui in DNS response", qclass); |
1649 | 1684 return; |
1685 } | |
1686 | |
1687 switch (qtype) { | |
1688 | |
1689 case NGX_RESOLVE_A: | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1690 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1691 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1692 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1693 |
6367 | 1694 ngx_resolver_process_a(r, buf, n, ident, code, qtype, nan, trunc, |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1695 i + sizeof(ngx_resolver_qs_t)); |
1649 | 1696 |
1697 break; | |
1698 | |
1699 case NGX_RESOLVE_PTR: | |
1700 | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1701 ngx_resolver_process_ptr(r, buf, n, ident, code, nan); |
1649 | 1702 |
1703 break; | |
1704 | |
1705 default: | |
1706 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
|
1707 "unknown query type %ui in DNS response", qtype); |
1649 | 1708 return; |
1709 } | |
1710 | |
1711 return; | |
1712 | |
1713 short_response: | |
1714 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1715 err = "short DNS response"; |
1649 | 1716 |
1717 done: | |
1718 | |
1719 ngx_log_error(r->log_level, r->log, 0, err); | |
1720 | |
1721 return; | |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1722 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1723 dns_error_name: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1724 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1725 ngx_log_error(r->log_level, r->log, 0, |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1726 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"", |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1727 code, ngx_resolver_strerror(code), ident, |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1728 rn->nlen, rn->name); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1729 return; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1730 |
2282
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1731 dns_error: |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1732 |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1733 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
|
1734 "DNS error (%ui: %s), query id:%ui", |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1735 code, ngx_resolver_strerror(code), ident); |
ec97eb9a8038
show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents:
2281
diff
changeset
|
1736 return; |
1649 | 1737 } |
1738 | |
1739 | |
1740 static void | |
6370 | 1741 ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n, |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1742 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t qtype, |
6367 | 1743 ngx_uint_t nan, ngx_uint_t trunc, ngx_uint_t ans) |
583 | 1744 { |
6367 | 1745 char *err; |
1746 u_char *cname; | |
1747 size_t len; | |
1748 int32_t ttl; | |
1749 uint32_t hash; | |
1750 in_addr_t *addr; | |
1751 ngx_str_t name; | |
1752 ngx_addr_t *addrs; | |
6370 | 1753 ngx_uint_t type, class, qident, naddrs, a, i, j, start; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1754 #if (NGX_HAVE_INET6) |
6367 | 1755 struct in6_addr *addr6; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1756 #endif |
6367 | 1757 ngx_resolver_an_t *an; |
1758 ngx_resolver_ctx_t *ctx, *next; | |
1759 ngx_resolver_node_t *rn; | |
1760 ngx_resolver_connection_t *rec; | |
1649 | 1761 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1762 if (ngx_resolver_copy(r, &name, buf, |
6370 | 1763 buf + sizeof(ngx_resolver_hdr_t), buf + n) |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1764 != NGX_OK) |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1765 { |
1649 | 1766 return; |
1767 } | |
1768 | |
1769 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name); | |
1770 | |
1771 hash = ngx_crc32_short(name.data, name.len); | |
1772 | |
1773 /* lock name mutex */ | |
1774 | |
1775 rn = ngx_resolver_lookup_name(r, &name, hash); | |
1776 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1777 if (rn == NULL) { |
1649 | 1778 ngx_log_error(r->log_level, r->log, 0, |
1779 "unexpected response for %V", &name); | |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
1780 ngx_resolver_free(r, name.data); |
1649 | 1781 goto failed; |
1782 } | |
1783 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1784 switch (qtype) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1785 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1786 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1787 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1788 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1789 if (rn->query6 == NULL || rn->naddrs6 != (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1790 ngx_log_error(r->log_level, r->log, 0, |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1791 "unexpected response for %V", &name); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1792 ngx_resolver_free(r, name.data); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1793 goto failed; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1794 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1795 |
6367 | 1796 if (trunc && rn->tcp6) { |
1797 ngx_resolver_free(r, name.data); | |
1798 goto failed; | |
1799 } | |
1800 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1801 qident = (rn->query6[0] << 8) + rn->query6[1]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1802 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1803 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1804 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1805 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1806 default: /* NGX_RESOLVE_A */ |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1807 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1808 if (rn->query == NULL || rn->naddrs != (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1809 ngx_log_error(r->log_level, r->log, 0, |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1810 "unexpected response for %V", &name); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1811 ngx_resolver_free(r, name.data); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1812 goto failed; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1813 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1814 |
6367 | 1815 if (trunc && rn->tcp) { |
1816 ngx_resolver_free(r, name.data); | |
1817 goto failed; | |
1818 } | |
1819 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1820 qident = (rn->query[0] << 8) + rn->query[1]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1821 } |
1649 | 1822 |
1823 if (ident != qident) { | |
1824 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
|
1825 "wrong ident %ui response for %V, expect %ui", |
1649 | 1826 ident, &name, qident); |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
1827 ngx_resolver_free(r, name.data); |
1649 | 1828 goto failed; |
1829 } | |
1830 | |
3139 | 1831 ngx_resolver_free(r, name.data); |
1832 | |
6367 | 1833 if (trunc) { |
1834 | |
1835 ngx_queue_remove(&rn->queue); | |
1836 | |
1837 if (rn->waiting == NULL) { | |
1838 ngx_rbtree_delete(&r->name_rbtree, &rn->node); | |
1839 ngx_resolver_free_node(r, rn); | |
1840 goto next; | |
1841 } | |
1842 | |
1843 rec = r->connections.elts; | |
1844 rec = &rec[rn->last_connection]; | |
1845 | |
1846 switch (qtype) { | |
1847 | |
1848 #if (NGX_HAVE_INET6) | |
1849 case NGX_RESOLVE_AAAA: | |
1850 | |
1851 rn->tcp6 = 1; | |
1852 | |
1853 (void) ngx_resolver_send_tcp_query(r, rec, rn->query6, rn->qlen); | |
1854 | |
1855 break; | |
1856 #endif | |
1857 | |
1858 default: /* NGX_RESOLVE_A */ | |
1859 | |
1860 rn->tcp = 1; | |
1861 | |
1862 (void) ngx_resolver_send_tcp_query(r, rec, rn->query, rn->qlen); | |
1863 } | |
1864 | |
1865 rn->expire = ngx_time() + r->resend_timeout; | |
1866 | |
1867 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue); | |
1868 | |
1869 goto next; | |
1870 } | |
1871 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1872 if (code == 0 && rn->code) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1873 code = rn->code; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1874 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1875 |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1876 if (code == 0 && nan == 0) { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1877 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1878 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1879 switch (qtype) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1880 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1881 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1882 |
5768
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1883 rn->naddrs6 = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1884 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1885 if (rn->naddrs == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1886 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1887 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1888 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1889 if (rn->naddrs) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1890 goto export; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1891 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1892 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1893 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1894 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1895 default: /* NGX_RESOLVE_A */ |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1896 |
5768
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1897 rn->naddrs = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1898 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1899 if (rn->naddrs6 == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1900 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1901 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1902 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1903 if (rn->naddrs6) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1904 goto export; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1905 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1906 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1907 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1908 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1909 code = NGX_RESOLVE_NXDOMAIN; |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1910 } |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
1911 |
1649 | 1912 if (code) { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1913 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1914 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1915 switch (qtype) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1916 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1917 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1918 |
5768
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1919 rn->naddrs6 = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1920 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1921 if (rn->naddrs == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1922 rn->code = (u_char) code; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1923 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1924 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1925 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1926 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1927 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1928 default: /* NGX_RESOLVE_A */ |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1929 |
5768
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1930 rn->naddrs = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
1931 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1932 if (rn->naddrs6 == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1933 rn->code = (u_char) code; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1934 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1935 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1936 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1937 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
1938 |
1649 | 1939 next = rn->waiting; |
1940 rn->waiting = NULL; | |
1941 | |
1942 ngx_queue_remove(&rn->queue); | |
1943 | |
1944 ngx_rbtree_delete(&r->name_rbtree, &rn->node); | |
1945 | |
1946 /* unlock name mutex */ | |
1947 | |
1948 while (next) { | |
5474
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
1949 ctx = next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
1950 ctx->state = code; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
1951 next = ctx->next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
1952 |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
1953 ctx->handler(ctx); |
1649 | 1954 } |
1955 | |
5920
7420068c4d4b
Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents:
5820
diff
changeset
|
1956 ngx_resolver_free_node(r, rn); |
7420068c4d4b
Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents:
5820
diff
changeset
|
1957 |
1649 | 1958 return; |
1959 } | |
1960 | |
1961 i = ans; | |
1962 naddrs = 0; | |
1963 cname = NULL; | |
1964 | |
1965 for (a = 0; a < nan; a++) { | |
1966 | |
1967 start = i; | |
1968 | |
6370 | 1969 while (i < n) { |
1649 | 1970 |
1971 if (buf[i] & 0xc0) { | |
1972 i += 2; | |
1973 goto found; | |
1974 } | |
1975 | |
1976 if (buf[i] == 0) { | |
1977 i++; | |
1978 goto test_length; | |
1979 } | |
1980 | |
1981 i += 1 + buf[i]; | |
1982 } | |
1983 | |
1984 goto short_response; | |
1985 | |
1986 test_length: | |
1987 | |
1988 if (i - start < 2) { | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
1989 err = "invalid name in DNS response"; |
1649 | 1990 goto invalid; |
1991 } | |
1992 | |
1993 found: | |
1994 | |
6370 | 1995 if (i + sizeof(ngx_resolver_an_t) >= n) { |
1649 | 1996 goto short_response; |
1997 } | |
1998 | |
1999 an = (ngx_resolver_an_t *) &buf[i]; | |
2000 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2001 type = (an->type_hi << 8) + an->type_lo; |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2002 class = (an->class_hi << 8) + an->class_lo; |
1649 | 2003 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
|
2004 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
|
2005 + (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
|
2006 |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2007 if (class != 1) { |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2008 ngx_log_error(r->log_level, r->log, 0, |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2009 "unexpected RR class %ui", class); |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2010 goto failed; |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2011 } |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2012 |
4295
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
2013 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
|
2014 ttl = 0; |
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
2015 } |
1649 | 2016 |
5485
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
2017 rn->ttl = ngx_min(rn->ttl, (uint32_t) ttl); |
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
2018 |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2019 i += sizeof(ngx_resolver_an_t); |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2020 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2021 switch (type) { |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2022 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2023 case NGX_RESOLVE_A: |
1649 | 2024 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2025 if (qtype != NGX_RESOLVE_A) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2026 err = "unexpected A record in DNS response"; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2027 goto invalid; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2028 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2029 |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2030 if (len != 4) { |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2031 err = "invalid A record in DNS response"; |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2032 goto invalid; |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2033 } |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2034 |
6370 | 2035 if (i + 4 > n) { |
1649 | 2036 goto short_response; |
2037 } | |
2038 | |
2039 naddrs++; | |
2040 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2041 break; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2042 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2043 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2044 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2045 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2046 if (qtype != NGX_RESOLVE_AAAA) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2047 err = "unexpected AAAA record in DNS response"; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2048 goto invalid; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2049 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2050 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2051 if (len != 16) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2052 err = "invalid AAAA record in DNS response"; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2053 goto invalid; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2054 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2055 |
6370 | 2056 if (i + 16 > n) { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2057 goto short_response; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2058 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2059 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2060 naddrs++; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2061 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2062 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2063 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2064 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2065 case NGX_RESOLVE_CNAME: |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2066 |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2067 cname = &buf[i]; |
1965 | 2068 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2069 break; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2070 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2071 case NGX_RESOLVE_DNAME: |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2072 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2073 break; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2074 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2075 default: |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2076 |
1966 | 2077 ngx_log_error(r->log_level, r->log, 0, |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2078 "unexpected RR type %ui", type); |
1649 | 2079 } |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2080 |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2081 i += len; |
1649 | 2082 } |
2083 | |
4295
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
2084 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, |
5485
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
2085 "resolver naddrs:%ui cname:%p ttl:%uD", |
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
2086 naddrs, cname, rn->ttl); |
1649 | 2087 |
2088 if (naddrs) { | |
2089 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2090 switch (qtype) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2091 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2092 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2093 case NGX_RESOLVE_AAAA: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2094 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2095 if (naddrs == 1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2096 addr6 = &rn->u6.addr6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2097 rn->naddrs6 = 1; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2098 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2099 } else { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2100 addr6 = ngx_resolver_alloc(r, naddrs * sizeof(struct in6_addr)); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2101 if (addr6 == NULL) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2102 goto failed; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2103 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2104 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2105 rn->u6.addrs6 = addr6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2106 rn->naddrs6 = (u_short) naddrs; |
1649 | 2107 } |
2108 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2109 #if (NGX_SUPPRESS_WARN) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2110 addr = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2111 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2112 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2113 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2114 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2115 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2116 default: /* NGX_RESOLVE_A */ |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2117 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2118 if (naddrs == 1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2119 addr = &rn->u.addr; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2120 rn->naddrs = 1; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2121 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2122 } else { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2123 addr = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t)); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2124 if (addr == NULL) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2125 goto failed; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2126 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2127 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2128 rn->u.addrs = addr; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2129 rn->naddrs = (u_short) naddrs; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2130 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2131 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2132 #if (NGX_HAVE_INET6 && NGX_SUPPRESS_WARN) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2133 addr6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2134 #endif |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2135 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2136 |
6370 | 2137 j = 0; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2138 i = ans; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2139 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2140 for (a = 0; a < nan; a++) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2141 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2142 for ( ;; ) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2143 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2144 if (buf[i] & 0xc0) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2145 i += 2; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2146 break; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2147 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2148 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2149 if (buf[i] == 0) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2150 i++; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2151 break; |
1649 | 2152 } |
2153 | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2154 i += 1 + buf[i]; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2155 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2156 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2157 an = (ngx_resolver_an_t *) &buf[i]; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2158 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2159 type = (an->type_hi << 8) + an->type_lo; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2160 len = (an->len_hi << 8) + an->len_lo; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2161 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2162 i += sizeof(ngx_resolver_an_t); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2163 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2164 if (type == NGX_RESOLVE_A) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2165 |
6370 | 2166 addr[j] = htonl((buf[i] << 24) + (buf[i + 1] << 16) |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2167 + (buf[i + 2] << 8) + (buf[i + 3])); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2168 |
6370 | 2169 if (++j == naddrs) { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2170 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2171 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2172 if (rn->naddrs6 == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2173 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2174 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2175 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2176 |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2177 break; |
1649 | 2178 } |
2179 } | |
2180 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2181 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2182 else if (type == NGX_RESOLVE_AAAA) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2183 |
6370 | 2184 ngx_memcpy(addr6[j].s6_addr, &buf[i], 16); |
2185 | |
2186 if (++j == naddrs) { | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2187 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2188 if (rn->naddrs == (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2189 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2190 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2191 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2192 break; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2193 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2194 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2195 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2196 |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2197 i += len; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2198 } |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2199 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2200 |
5768
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2201 switch (qtype) { |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2202 |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2203 #if (NGX_HAVE_INET6) |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2204 case NGX_RESOLVE_AAAA: |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2205 |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2206 if (rn->naddrs6 == (u_short) -1) { |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2207 rn->naddrs6 = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2208 } |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2209 |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2210 break; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2211 #endif |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2212 |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2213 default: /* NGX_RESOLVE_A */ |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2214 |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2215 if (rn->naddrs == (u_short) -1) { |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2216 rn->naddrs = 0; |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2217 } |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2218 } |
70a0408cca7b
Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents:
5764
diff
changeset
|
2219 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2220 if (rn->naddrs != (u_short) -1 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2221 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2222 && rn->naddrs6 != (u_short) -1 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2223 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2224 && rn->naddrs |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2225 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2226 + rn->naddrs6 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2227 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2228 > 0) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2229 { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2230 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2231 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2232 export: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2233 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2234 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2235 naddrs = rn->naddrs; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2236 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2237 naddrs += rn->naddrs6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2238 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2239 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2240 if (naddrs == 1 && rn->naddrs == 1) { |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2241 addrs = NULL; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2242 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2243 } else { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2244 addrs = ngx_resolver_export(r, rn, 0); |
1649 | 2245 if (addrs == NULL) { |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2246 goto failed; |
1649 | 2247 } |
2248 } | |
2249 | |
2250 ngx_queue_remove(&rn->queue); | |
2251 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2252 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl); |
1649 | 2253 rn->expire = ngx_time() + r->expire; |
2254 | |
2255 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
2256 | |
2257 next = rn->waiting; | |
2258 rn->waiting = NULL; | |
2259 | |
2260 /* unlock name mutex */ | |
2261 | |
2262 while (next) { | |
5474
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2263 ctx = next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2264 ctx->state = NGX_OK; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2265 ctx->naddrs = naddrs; |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2266 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2267 if (addrs == NULL) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2268 ctx->addrs = &ctx->addr; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2269 ctx->addr.sockaddr = (struct sockaddr *) &ctx->sin; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2270 ctx->addr.socklen = sizeof(struct sockaddr_in); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2271 ngx_memzero(&ctx->sin, sizeof(struct sockaddr_in)); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2272 ctx->sin.sin_family = AF_INET; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2273 ctx->sin.sin_addr.s_addr = rn->u.addr; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2274 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2275 } else { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2276 ctx->addrs = addrs; |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2277 } |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2278 |
5474
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2279 next = ctx->next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2280 |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2281 ctx->handler(ctx); |
1649 | 2282 } |
2283 | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2284 if (addrs != NULL) { |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
2285 ngx_resolver_free(r, addrs->sockaddr); |
1649 | 2286 ngx_resolver_free(r, addrs); |
2287 } | |
2288 | |
4619
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
2289 ngx_resolver_free(r, rn->query); |
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
2290 rn->query = NULL; |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2291 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2292 rn->query6 = NULL; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2293 #endif |
4619
3171ec7d0d05
Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4610
diff
changeset
|
2294 |
1649 | 2295 return; |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2296 } |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2297 |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2298 if (cname) { |
1649 | 2299 |
2300 /* CNAME only */ | |
2301 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2302 if (rn->naddrs == (u_short) -1 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2303 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2304 || rn->naddrs6 == (u_short) -1 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2305 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2306 ) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2307 { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2308 goto next; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2309 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2310 |
6370 | 2311 if (ngx_resolver_copy(r, &name, buf, cname, buf + n) != NGX_OK) { |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2312 goto failed; |
1649 | 2313 } |
2314 | |
2315 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, | |
2316 "resolver cname:\"%V\"", &name); | |
2317 | |
1741
0829024c924d
fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents:
1689
diff
changeset
|
2318 ngx_queue_remove(&rn->queue); |
0829024c924d
fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents:
1689
diff
changeset
|
2319 |
1649 | 2320 rn->cnlen = (u_short) name.len; |
2321 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
|
2322 |
5485
8958656a8060
Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents:
5479
diff
changeset
|
2323 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl); |
1649 | 2324 rn->expire = ngx_time() + r->expire; |
2325 | |
2326 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); | |
2327 | |
6352
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2328 ngx_resolver_free(r, rn->query); |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2329 rn->query = NULL; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2330 #if (NGX_HAVE_INET6) |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2331 rn->query6 = NULL; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2332 #endif |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2333 |
1649 | 2334 ctx = rn->waiting; |
2335 rn->waiting = NULL; | |
2336 | |
2337 if (ctx) { | |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2338 |
6352
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2339 if (ctx->recursion++ >= NGX_RESOLVER_MAX_RECURSION) { |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2340 |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2341 /* unlock name mutex */ |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2342 |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2343 do { |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2344 ctx->state = NGX_RESOLVE_NXDOMAIN; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2345 next = ctx->next; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2346 |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2347 ctx->handler(ctx); |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2348 |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2349 ctx = next; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2350 } while (ctx); |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2351 |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2352 return; |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2353 } |
ff9b32c0e141
Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents:
6351
diff
changeset
|
2354 |
6351
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2355 for (next = ctx; next; next = next->next) { |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2356 next->node = NULL; |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2357 } |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2358 |
497d0cff8ace
Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents:
6350
diff
changeset
|
2359 (void) ngx_resolve_name_locked(r, ctx, &name); |
1649 | 2360 } |
2361 | |
5471
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2362 /* unlock name mutex */ |
9c96782d9d05
Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5470
diff
changeset
|
2363 |
1649 | 2364 return; |
2365 } | |
2366 | |
2367 ngx_log_error(r->log_level, r->log, 0, | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2368 "no A or CNAME types in DNS response"); |
1649 | 2369 return; |
2370 | |
2371 short_response: | |
2372 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2373 err = "short DNS response"; |
1649 | 2374 |
2375 invalid: | |
2376 | |
2377 /* unlock name mutex */ | |
2378 | |
2379 ngx_log_error(r->log_level, r->log, 0, err); | |
2380 | |
2381 return; | |
2382 | |
2383 failed: | |
2384 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2385 next: |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2386 |
1649 | 2387 /* unlock name mutex */ |
2388 | |
2389 return; | |
2390 } | |
2391 | |
2392 | |
2393 static void | |
2394 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
|
2395 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan) |
1649 | 2396 { |
2397 char *err; | |
2398 size_t len; | |
2399 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
|
2400 int32_t ttl; |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2401 ngx_int_t octet; |
1649 | 2402 ngx_str_t name; |
6371
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2403 ngx_uint_t mask, type, class, qident, a, i, start; |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2404 ngx_queue_t *expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2405 ngx_rbtree_t *tree; |
1649 | 2406 ngx_resolver_an_t *an; |
2407 ngx_resolver_ctx_t *ctx, *next; | |
2408 ngx_resolver_node_t *rn; | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2409 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2410 uint32_t hash; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2411 ngx_int_t digit; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2412 struct in6_addr addr6; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2413 #endif |
1649 | 2414 |
6372 | 2415 if (ngx_resolver_copy(r, &name, buf, |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2416 buf + sizeof(ngx_resolver_hdr_t), buf + n) |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2417 != NGX_OK) |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2418 { |
5472
ab493c60d9ff
Resolver: fixes in PTR processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5471
diff
changeset
|
2419 return; |
1649 | 2420 } |
2421 | |
6372 | 2422 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name); |
2423 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2424 /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2425 |
1649 | 2426 addr = 0; |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2427 i = sizeof(ngx_resolver_hdr_t); |
1649 | 2428 |
2429 for (mask = 0; mask < 32; mask += 8) { | |
2430 len = buf[i++]; | |
2431 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2432 octet = ngx_atoi(&buf[i], len); |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2433 if (octet == NGX_ERROR || octet > 255) { |
1649 | 2434 goto invalid_in_addr_arpa; |
2435 } | |
2436 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2437 addr += octet << mask; |
1649 | 2438 i += len; |
2439 } | |
2440 | |
5479
c0d6eae5a1c5
Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents:
5478
diff
changeset
|
2441 if (ngx_strcasecmp(&buf[i], (u_char *) "\7in-addr\4arpa") == 0) { |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2442 i += sizeof("\7in-addr\4arpa"); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2443 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2444 /* lock addr mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2445 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2446 rn = ngx_resolver_lookup_addr(r, addr); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2447 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2448 tree = &r->addr_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2449 expire_queue = &r->addr_expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2450 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2451 goto valid; |
1649 | 2452 } |
2453 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2454 invalid_in_addr_arpa: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2455 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2456 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2457 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2458 i = sizeof(ngx_resolver_hdr_t); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2459 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2460 for (octet = 15; octet >= 0; octet--) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2461 if (buf[i++] != '\1') { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2462 goto invalid_ip6_arpa; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2463 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2464 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2465 digit = ngx_hextoi(&buf[i++], 1); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2466 if (digit == NGX_ERROR) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2467 goto invalid_ip6_arpa; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2468 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2469 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2470 addr6.s6_addr[octet] = (u_char) digit; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2471 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2472 if (buf[i++] != '\1') { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2473 goto invalid_ip6_arpa; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2474 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2475 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2476 digit = ngx_hextoi(&buf[i++], 1); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2477 if (digit == NGX_ERROR) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2478 goto invalid_ip6_arpa; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2479 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2480 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2481 addr6.s6_addr[octet] += (u_char) (digit * 16); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2482 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2483 |
5479
c0d6eae5a1c5
Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents:
5478
diff
changeset
|
2484 if (ngx_strcasecmp(&buf[i], (u_char *) "\3ip6\4arpa") == 0) { |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2485 i += sizeof("\3ip6\4arpa"); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2486 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2487 /* lock addr mutex */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2488 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2489 hash = ngx_crc32_short(addr6.s6_addr, 16); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2490 rn = ngx_resolver_lookup_addr6(r, &addr6, hash); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2491 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2492 tree = &r->addr6_rbtree; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2493 expire_queue = &r->addr6_expire_queue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2494 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2495 goto valid; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2496 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2497 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2498 invalid_ip6_arpa: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2499 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2500 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2501 ngx_log_error(r->log_level, r->log, 0, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2502 "invalid in-addr.arpa or ip6.arpa name in DNS response"); |
6372 | 2503 ngx_resolver_free(r, name.data); |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2504 return; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2505 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2506 valid: |
1649 | 2507 |
2508 if (rn == NULL || rn->query == NULL) { | |
2509 ngx_log_error(r->log_level, r->log, 0, | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2510 "unexpected response for %V", &name); |
6372 | 2511 ngx_resolver_free(r, name.data); |
1649 | 2512 goto failed; |
2513 } | |
2514 | |
2515 qident = (rn->query[0] << 8) + rn->query[1]; | |
2516 | |
2517 if (ident != qident) { | |
2518 ngx_log_error(r->log_level, r->log, 0, | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2519 "wrong ident %ui response for %V, expect %ui", |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2520 ident, &name, qident); |
6372 | 2521 ngx_resolver_free(r, name.data); |
1649 | 2522 goto failed; |
2523 } | |
2524 | |
6372 | 2525 ngx_resolver_free(r, name.data); |
2526 | |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
2527 if (code == 0 && nan == 0) { |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2528 code = NGX_RESOLVE_NXDOMAIN; |
1742
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
2529 } |
268b81386fe4
no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents:
1741
diff
changeset
|
2530 |
1649 | 2531 if (code) { |
2532 next = rn->waiting; | |
2533 rn->waiting = NULL; | |
2534 | |
2535 ngx_queue_remove(&rn->queue); | |
2536 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2537 ngx_rbtree_delete(tree, &rn->node); |
1649 | 2538 |
2539 /* unlock addr mutex */ | |
2540 | |
2541 while (next) { | |
5474
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2542 ctx = next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2543 ctx->state = code; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2544 next = ctx->next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2545 |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2546 ctx->handler(ctx); |
1649 | 2547 } |
2548 | |
5920
7420068c4d4b
Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents:
5820
diff
changeset
|
2549 ngx_resolver_free_node(r, rn); |
7420068c4d4b
Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents:
5820
diff
changeset
|
2550 |
1649 | 2551 return; |
2552 } | |
2553 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2554 i += sizeof(ngx_resolver_qs_t); |
1649 | 2555 |
6371
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2556 for (a = 0; a < nan; a++) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2557 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2558 start = i; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2559 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2560 while (i < n) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2561 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2562 if (buf[i] & 0xc0) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2563 i += 2; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2564 goto found; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2565 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2566 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2567 if (buf[i] == 0) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2568 i++; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2569 goto test_length; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2570 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2571 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2572 i += 1 + buf[i]; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2573 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2574 |
1649 | 2575 goto short_response; |
6371
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2576 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2577 test_length: |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2578 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2579 if (i - start < 2) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2580 err = "invalid name in DNS response"; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2581 goto invalid; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2582 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2583 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2584 found: |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2585 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2586 if (i + sizeof(ngx_resolver_an_t) >= n) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2587 goto short_response; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2588 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2589 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2590 an = (ngx_resolver_an_t *) &buf[i]; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2591 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2592 type = (an->type_hi << 8) + an->type_lo; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2593 class = (an->class_hi << 8) + an->class_lo; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2594 len = (an->len_hi << 8) + an->len_lo; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2595 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16) |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2596 + (an->ttl[2] << 8) + (an->ttl[3]); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2597 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2598 if (class != 1) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2599 ngx_log_error(r->log_level, r->log, 0, |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2600 "unexpected RR class %ui", class); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2601 goto failed; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2602 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2603 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2604 if (ttl < 0) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2605 ttl = 0; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2606 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2607 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2608 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2609 "resolver qt:%ui cl:%ui len:%uz", |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2610 type, class, len); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2611 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2612 i += sizeof(ngx_resolver_an_t); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2613 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2614 switch (type) { |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2615 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2616 case NGX_RESOLVE_PTR: |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2617 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2618 goto ptr; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2619 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2620 case NGX_RESOLVE_CNAME: |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2621 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2622 break; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2623 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2624 default: |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2625 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2626 ngx_log_error(r->log_level, r->log, 0, |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2627 "unexpected RR type %ui", type); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2628 } |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2629 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2630 i += len; |
1649 | 2631 } |
2632 | |
6371
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2633 /* unlock addr mutex */ |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2634 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2635 ngx_log_error(r->log_level, r->log, 0, |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2636 "no PTR type in DNS response"); |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2637 return; |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2638 |
33c4d319f08f
Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents:
6370
diff
changeset
|
2639 ptr: |
1649 | 2640 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2641 if (ngx_resolver_copy(r, &name, buf, buf + i, buf + n) != NGX_OK) { |
5472
ab493c60d9ff
Resolver: fixes in PTR processing.
Ruslan Ermilov <ru@nginx.com>
parents:
5471
diff
changeset
|
2642 goto failed; |
1649 | 2643 } |
2644 | |
2645 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name); | |
2646 | |
2486
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
2647 if (name.len != (size_t) rn->nlen |
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
2648 || ngx_strncmp(name.data, rn->name, name.len) != 0) |
1649 | 2649 { |
2482
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
2650 if (rn->nlen) { |
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
2651 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
|
2652 } |
30ec8c5ac75b
fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents:
2314
diff
changeset
|
2653 |
2490
1c87647b7ca5
fix building by msvc, introduced in r2487
Igor Sysoev <igor@sysoev.ru>
parents:
2487
diff
changeset
|
2654 rn->nlen = (u_short) name.len; |
1649 | 2655 rn->name = name.data; |
2656 | |
2486
8de5dc3e7001
use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents:
2484
diff
changeset
|
2657 name.data = ngx_resolver_dup(r, rn->name, name.len); |
1649 | 2658 if (name.data == NULL) { |
2659 goto failed; | |
2660 } | |
2661 } | |
2662 | |
2663 ngx_queue_remove(&rn->queue); | |
2664 | |
4295
05031fce7ce8
Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents:
4267
diff
changeset
|
2665 rn->valid = ngx_time() + (r->valid ? r->valid : ttl); |
1649 | 2666 rn->expire = ngx_time() + r->expire; |
2667 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2668 ngx_queue_insert_head(expire_queue, &rn->queue); |
1649 | 2669 |
2670 next = rn->waiting; | |
2671 rn->waiting = NULL; | |
2672 | |
2673 /* unlock addr mutex */ | |
2674 | |
2675 while (next) { | |
5474
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2676 ctx = next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2677 ctx->state = NGX_OK; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2678 ctx->name = name; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2679 next = ctx->next; |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2680 |
b43b02bb54db
Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents:
5472
diff
changeset
|
2681 ctx->handler(ctx); |
1649 | 2682 } |
2683 | |
2684 ngx_resolver_free(r, name.data); | |
2685 | |
2686 return; | |
2687 | |
2688 short_response: | |
2689 | |
2690 err = "short DNS response"; | |
2691 | |
2692 invalid: | |
2693 | |
2694 /* unlock addr mutex */ | |
2695 | |
2696 ngx_log_error(r->log_level, r->log, 0, err); | |
2697 | |
2698 return; | |
2699 | |
2700 failed: | |
2701 | |
2702 /* unlock addr mutex */ | |
2703 | |
2704 return; | |
2705 } | |
2706 | |
2707 | |
2708 static ngx_resolver_node_t * | |
2709 ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash) | |
2710 { | |
2711 ngx_int_t rc; | |
2712 ngx_rbtree_node_t *node, *sentinel; | |
2713 ngx_resolver_node_t *rn; | |
2714 | |
2715 node = r->name_rbtree.root; | |
2716 sentinel = r->name_rbtree.sentinel; | |
2717 | |
2718 while (node != sentinel) { | |
2719 | |
2720 if (hash < node->key) { | |
2721 node = node->left; | |
2722 continue; | |
2723 } | |
2724 | |
2725 if (hash > node->key) { | |
2726 node = node->right; | |
2727 continue; | |
2728 } | |
2729 | |
2730 /* hash == node->key */ | |
2731 | |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2732 rn = ngx_resolver_node(node); |
4497
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2733 |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2734 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
|
2735 |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2736 if (rc == 0) { |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2737 return rn; |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2738 } |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2739 |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4496
diff
changeset
|
2740 node = (rc < 0) ? node->left : node->right; |
1649 | 2741 } |
2742 | |
2743 /* not found */ | |
2744 | |
2745 return NULL; | |
2746 } | |
2747 | |
2748 | |
2749 static ngx_resolver_node_t * | |
2750 ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr) | |
2751 { | |
2752 ngx_rbtree_node_t *node, *sentinel; | |
2753 | |
2754 node = r->addr_rbtree.root; | |
2755 sentinel = r->addr_rbtree.sentinel; | |
2756 | |
2757 while (node != sentinel) { | |
2758 | |
2759 if (addr < node->key) { | |
2760 node = node->left; | |
2761 continue; | |
2762 } | |
2763 | |
2764 if (addr > node->key) { | |
2765 node = node->right; | |
2766 continue; | |
2767 } | |
2768 | |
2769 /* addr == node->key */ | |
2770 | |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2771 return ngx_resolver_node(node); |
1649 | 2772 } |
2773 | |
2774 /* not found */ | |
2775 | |
2776 return NULL; | |
2777 } | |
2778 | |
2779 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2780 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2781 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2782 static ngx_resolver_node_t * |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2783 ngx_resolver_lookup_addr6(ngx_resolver_t *r, struct in6_addr *addr, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2784 uint32_t hash) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2785 { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2786 ngx_int_t rc; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2787 ngx_rbtree_node_t *node, *sentinel; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2788 ngx_resolver_node_t *rn; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2789 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2790 node = r->addr6_rbtree.root; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2791 sentinel = r->addr6_rbtree.sentinel; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2792 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2793 while (node != sentinel) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2794 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2795 if (hash < node->key) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2796 node = node->left; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2797 continue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2798 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2799 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2800 if (hash > node->key) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2801 node = node->right; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2802 continue; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2803 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2804 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2805 /* hash == node->key */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2806 |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2807 rn = ngx_resolver_node(node); |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2808 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2809 rc = ngx_memcmp(addr, &rn->addr6, 16); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2810 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2811 if (rc == 0) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2812 return rn; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2813 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2814 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2815 node = (rc < 0) ? node->left : node->right; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2816 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2817 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2818 /* not found */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2819 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2820 return NULL; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2821 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2822 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2823 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2824 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2825 |
1649 | 2826 static void |
2827 ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
2828 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
2829 { | |
2830 ngx_rbtree_node_t **p; | |
2831 ngx_resolver_node_t *rn, *rn_temp; | |
2832 | |
2833 for ( ;; ) { | |
2834 | |
2835 if (node->key < temp->key) { | |
2836 | |
2837 p = &temp->left; | |
2838 | |
2839 } else if (node->key > temp->key) { | |
2840 | |
2841 p = &temp->right; | |
2842 | |
2843 } else { /* node->key == temp->key */ | |
2844 | |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2845 rn = ngx_resolver_node(node); |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2846 rn_temp = ngx_resolver_node(temp); |
1649 | 2847 |
3143
ab6258e18099
fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents:
3139
diff
changeset
|
2848 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
|
2849 < 0) ? &temp->left : &temp->right; |
1649 | 2850 } |
2851 | |
2852 if (*p == sentinel) { | |
2853 break; | |
2854 } | |
2855 | |
2856 temp = *p; | |
2857 } | |
2858 | |
2859 *p = node; | |
2860 node->parent = temp; | |
2861 node->left = sentinel; | |
2862 node->right = sentinel; | |
2863 ngx_rbt_red(node); | |
2864 } | |
2865 | |
2866 | |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2867 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2868 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2869 static void |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2870 ngx_resolver_rbtree_insert_addr6_value(ngx_rbtree_node_t *temp, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2871 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2872 { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2873 ngx_rbtree_node_t **p; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2874 ngx_resolver_node_t *rn, *rn_temp; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2875 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2876 for ( ;; ) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2877 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2878 if (node->key < temp->key) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2879 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2880 p = &temp->left; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2881 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2882 } else if (node->key > temp->key) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2883 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2884 p = &temp->right; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2885 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2886 } else { /* node->key == temp->key */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2887 |
5921
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2888 rn = ngx_resolver_node(node); |
5004210e8c78
Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents:
5920
diff
changeset
|
2889 rn_temp = ngx_resolver_node(temp); |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2890 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2891 p = (ngx_memcmp(&rn->addr6, &rn_temp->addr6, 16) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2892 < 0) ? &temp->left : &temp->right; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2893 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2894 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2895 if (*p == sentinel) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2896 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2897 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2898 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2899 temp = *p; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2900 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2901 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2902 *p = node; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2903 node->parent = temp; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2904 node->left = sentinel; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2905 node->right = sentinel; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2906 ngx_rbt_red(node); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2907 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2908 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2909 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2910 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
2911 |
1649 | 2912 static ngx_int_t |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2913 ngx_resolver_create_name_query(ngx_resolver_t *r, ngx_resolver_node_t *rn, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2914 ngx_str_t *name) |
1649 | 2915 { |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2916 u_char *p, *s; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2917 size_t len, nlen; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2918 ngx_uint_t ident; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2919 ngx_resolver_qs_t *qs; |
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2920 ngx_resolver_hdr_t *query; |
1649 | 2921 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2922 nlen = name->len ? (1 + name->len + 1) : 1; |
3306
61bdaac6c668
fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents:
3299
diff
changeset
|
2923 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2924 len = sizeof(ngx_resolver_hdr_t) + nlen + sizeof(ngx_resolver_qs_t); |
1649 | 2925 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2926 #if (NGX_HAVE_INET6) |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2927 p = ngx_resolver_alloc(r, r->ipv6 ? len * 2 : len); |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2928 #else |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2929 p = ngx_resolver_alloc(r, len); |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2930 #endif |
1649 | 2931 if (p == NULL) { |
2932 return NGX_ERROR; | |
2933 } | |
2934 | |
2935 rn->qlen = (u_short) len; | |
2936 rn->query = p; | |
2937 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2938 #if (NGX_HAVE_INET6) |
5478
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
2939 if (r->ipv6) { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
2940 rn->query6 = p + len; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
2941 } |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2942 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2943 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2944 query = (ngx_resolver_hdr_t *) p; |
1649 | 2945 |
2946 ident = ngx_random(); | |
2947 | |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2948 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2949 "resolve: \"%V\" A %i", name, ident & 0xffff); |
1649 | 2950 |
2951 query->ident_hi = (u_char) ((ident >> 8) & 0xff); | |
2952 query->ident_lo = (u_char) (ident & 0xff); | |
2953 | |
2954 /* recursion query */ | |
2955 query->flags_hi = 1; query->flags_lo = 0; | |
2956 | |
2957 /* one question */ | |
2958 query->nqs_hi = 0; query->nqs_lo = 1; | |
2959 query->nan_hi = 0; query->nan_lo = 0; | |
2960 query->nns_hi = 0; query->nns_lo = 0; | |
2961 query->nar_hi = 0; query->nar_lo = 0; | |
2962 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2963 p += sizeof(ngx_resolver_hdr_t) + nlen; |
1649 | 2964 |
2965 qs = (ngx_resolver_qs_t *) p; | |
2966 | |
2967 /* query type */ | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
2968 qs->type_hi = 0; qs->type_lo = NGX_RESOLVE_A; |
1649 | 2969 |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
2970 /* IN query class */ |
1649 | 2971 qs->class_hi = 0; qs->class_lo = 1; |
2972 | |
2973 /* convert "www.example.com" to "\3www\7example\3com\0" */ | |
2974 | |
2975 len = 0; | |
2976 p--; | |
2977 *p-- = '\0'; | |
2978 | |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2979 if (name->len == 0) { |
4610
778d2cc03e22
Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents:
4556
diff
changeset
|
2980 return NGX_DECLINED; |
778d2cc03e22
Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents:
4556
diff
changeset
|
2981 } |
778d2cc03e22
Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents:
4556
diff
changeset
|
2982 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
2983 for (s = name->data + name->len - 1; s >= name->data; s--) { |
1649 | 2984 if (*s != '.') { |
2985 *p = *s; | |
2986 len++; | |
2987 | |
2988 } else { | |
4556
1bddc91e78d6
Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4497
diff
changeset
|
2989 if (len == 0 || len > 255) { |
1961
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
2990 return NGX_DECLINED; |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
2991 } |
99b9feacccb4
return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents:
1960
diff
changeset
|
2992 |
1649 | 2993 *p = (u_char) len; |
2994 len = 0; | |
2995 } | |
2996 | |
2997 p--; | |
2998 } | |
2999 | |
4556
1bddc91e78d6
Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4497
diff
changeset
|
3000 if (len == 0 || len > 255) { |
1bddc91e78d6
Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4497
diff
changeset
|
3001 return NGX_DECLINED; |
1bddc91e78d6
Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4497
diff
changeset
|
3002 } |
1bddc91e78d6
Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4497
diff
changeset
|
3003 |
1649 | 3004 *p = (u_char) len; |
3005 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3006 #if (NGX_HAVE_INET6) |
5478
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
3007 if (!r->ipv6) { |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
3008 return NGX_OK; |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
3009 } |
3cb3175a6fef
The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
5477
diff
changeset
|
3010 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3011 p = rn->query6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3012 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3013 ngx_memcpy(p, rn->query, rn->qlen); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3014 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3015 query = (ngx_resolver_hdr_t *) p; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3016 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3017 ident = ngx_random(); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3018 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3019 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3020 "resolve: \"%V\" AAAA %i", name, ident & 0xffff); |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3021 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3022 query->ident_hi = (u_char) ((ident >> 8) & 0xff); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3023 query->ident_lo = (u_char) (ident & 0xff); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3024 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3025 p += sizeof(ngx_resolver_hdr_t) + nlen; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3026 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3027 qs = (ngx_resolver_qs_t *) p; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3028 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3029 qs->type_lo = NGX_RESOLVE_AAAA; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3030 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3031 |
1649 | 3032 return NGX_OK; |
3033 } | |
3034 | |
3035 | |
3036 static ngx_int_t | |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3037 ngx_resolver_create_addr_query(ngx_resolver_t *r, ngx_resolver_node_t *rn, |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3038 ngx_addr_t *addr) |
1649 | 3039 { |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3040 u_char *p, *d; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3041 size_t len; |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3042 in_addr_t inaddr; |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3043 ngx_int_t n; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3044 ngx_uint_t ident; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3045 ngx_resolver_hdr_t *query; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3046 struct sockaddr_in *sin; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3047 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3048 struct sockaddr_in6 *sin6; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3049 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3050 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3051 switch (addr->sockaddr->sa_family) { |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3052 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3053 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3054 case AF_INET6: |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3055 len = sizeof(ngx_resolver_hdr_t) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3056 + 64 + sizeof(".ip6.arpa.") - 1 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3057 + sizeof(ngx_resolver_qs_t); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3058 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3059 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3060 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3061 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3062 default: /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3063 len = sizeof(ngx_resolver_hdr_t) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3064 + sizeof(".255.255.255.255.in-addr.arpa.") - 1 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3065 + sizeof(ngx_resolver_qs_t); |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3066 } |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
3067 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3068 p = ngx_resolver_alloc(r, len); |
1649 | 3069 if (p == NULL) { |
3070 return NGX_ERROR; | |
3071 } | |
3072 | |
3073 rn->query = p; | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
3074 query = (ngx_resolver_hdr_t *) p; |
1649 | 3075 |
3076 ident = ngx_random(); | |
3077 | |
3078 query->ident_hi = (u_char) ((ident >> 8) & 0xff); | |
3079 query->ident_lo = (u_char) (ident & 0xff); | |
3080 | |
3081 /* recursion query */ | |
3082 query->flags_hi = 1; query->flags_lo = 0; | |
3083 | |
3084 /* one question */ | |
3085 query->nqs_hi = 0; query->nqs_lo = 1; | |
3086 query->nan_hi = 0; query->nan_lo = 0; | |
3087 query->nns_hi = 0; query->nns_lo = 0; | |
3088 query->nar_hi = 0; query->nar_lo = 0; | |
3089 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
3090 p += sizeof(ngx_resolver_hdr_t); |
1649 | 3091 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3092 switch (addr->sockaddr->sa_family) { |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3093 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3094 #if (NGX_HAVE_INET6) |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3095 case AF_INET6: |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3096 sin6 = (struct sockaddr_in6 *) addr->sockaddr; |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3097 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3098 for (n = 15; n >= 0; n--) { |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3099 p = ngx_sprintf(p, "\1%xd\1%xd", |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3100 sin6->sin6_addr.s6_addr[n] & 0xf, |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3101 (sin6->sin6_addr.s6_addr[n] >> 4) & 0xf); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3102 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3103 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3104 p = ngx_cpymem(p, "\3ip6\4arpa\0", 10); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3105 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3106 break; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3107 #endif |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3108 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3109 default: /* AF_INET */ |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3110 |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3111 sin = (struct sockaddr_in *) addr->sockaddr; |
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3112 inaddr = ntohl(sin->sin_addr.s_addr); |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3113 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3114 for (n = 0; n < 32; n += 8) { |
6350
a5767988c022
Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents:
6349
diff
changeset
|
3115 d = ngx_sprintf(&p[1], "%ud", (inaddr >> n) & 0xff); |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3116 *p = (u_char) (d - &p[1]); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3117 p = d; |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3118 } |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3119 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3120 p = ngx_cpymem(p, "\7in-addr\4arpa\0", 14); |
1649 | 3121 } |
3122 | |
5468
5c410d6ca7dd
Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents:
5360
diff
changeset
|
3123 /* query type "PTR", IN query class */ |
5476
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3124 p = ngx_cpymem(p, "\0\14\0\1", 4); |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3125 |
950c9ed3e66f
Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5475
diff
changeset
|
3126 rn->qlen = (u_short) (p - rn->query); |
1649 | 3127 |
3128 return NGX_OK; | |
3129 } | |
3130 | |
3131 | |
3132 static ngx_int_t | |
3133 ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src, | |
3134 u_char *last) | |
3135 { | |
3136 char *err; | |
3137 u_char *p, *dst; | |
3138 ssize_t len; | |
3139 ngx_uint_t i, n; | |
3140 | |
3141 p = src; | |
3142 len = -1; | |
3143 | |
3144 /* | |
3145 * compression pointers allow to create endless loop, so we set limit; | |
3146 * 128 pointers should be enough to store 255-byte name | |
3147 */ | |
3148 | |
3149 for (i = 0; i < 128; i++) { | |
3150 n = *p++; | |
3151 | |
3152 if (n == 0) { | |
3153 goto done; | |
3154 } | |
3155 | |
3156 if (n & 0xc0) { | |
2314
52987a023486
fix compression pointer for big (>255) DNS responses
Igor Sysoev <igor@sysoev.ru>
parents:
2282
diff
changeset
|
3157 n = ((n & 0x3f) << 8) + *p; |
1649 | 3158 p = &buf[n]; |
3159 | |
3160 } else { | |
3161 len += 1 + n; | |
3162 p = &p[n]; | |
3163 } | |
3164 | |
3165 if (p >= last) { | |
3166 err = "name is out of response"; | |
3167 goto invalid; | |
3168 } | |
3169 } | |
3170 | |
3171 err = "compression pointers loop"; | |
3172 | |
3173 invalid: | |
3174 | |
3175 ngx_log_error(r->log_level, r->log, 0, err); | |
3176 | |
3177 return NGX_ERROR; | |
3178 | |
3179 done: | |
3180 | |
3181 if (name == NULL) { | |
583 | 3182 return NGX_OK; |
3183 } | |
3184 | |
3298
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
3185 if (len == -1) { |
5764
f166c521b619
Style: use ngx_str_null().
Tatsuhiko Kubo <cubicdaiya@gmail.com>
parents:
5600
diff
changeset
|
3186 ngx_str_null(name); |
3298
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
3187 return NGX_OK; |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
3188 } |
847ab5a32307
fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents:
3297
diff
changeset
|
3189 |
1649 | 3190 dst = ngx_resolver_alloc(r, len); |
3191 if (dst == NULL) { | |
3192 return NGX_ERROR; | |
3193 } | |
3194 | |
3195 name->data = dst; | |
3196 | |
3197 n = *src++; | |
3198 | |
3199 for ( ;; ) { | |
4267
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3200 if (n & 0xc0) { |
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3201 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
|
3202 src = &buf[n]; |
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3203 |
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3204 n = *src++; |
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3205 |
768212ca0745
Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents:
4225
diff
changeset
|
3206 } else { |
5479
c0d6eae5a1c5
Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents:
5478
diff
changeset
|
3207 ngx_strlow(dst, src, n); |
1649 | 3208 dst += n; |
3209 src += n; | |
3210 | |
3211 n = *src++; | |
3212 | |
3213 if (n != 0) { | |
3214 *dst++ = '.'; | |
3215 } | |
3216 } | |
3217 | |
3218 if (n == 0) { | |
3219 name->len = dst - name->data; | |
3220 return NGX_OK; | |
3221 } | |
3222 } | |
3223 } | |
3224 | |
3225 | |
3226 static void | |
3227 ngx_resolver_timeout_handler(ngx_event_t *ev) | |
3228 { | |
6348
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3229 ngx_resolver_ctx_t *ctx; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3230 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3231 ctx = ev->data; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3232 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3233 ctx->state = NGX_RESOLVE_TIMEDOUT; |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3234 |
7316c57e4fe7
Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents:
6347
diff
changeset
|
3235 ctx->handler(ctx); |
1649 | 3236 } |
3237 | |
3238 | |
3239 static void | |
3240 ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn) | |
3241 { | |
3242 /* lock alloc mutex */ | |
3243 | |
3244 if (rn->query) { | |
3245 ngx_resolver_free_locked(r, rn->query); | |
3246 } | |
3247 | |
3248 if (rn->name) { | |
3249 ngx_resolver_free_locked(r, rn->name); | |
3250 } | |
3251 | |
3252 if (rn->cnlen) { | |
3253 ngx_resolver_free_locked(r, rn->u.cname); | |
3254 } | |
3255 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3256 if (rn->naddrs > 1 && rn->naddrs != (u_short) -1) { |
1649 | 3257 ngx_resolver_free_locked(r, rn->u.addrs); |
3258 } | |
3259 | |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3260 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3261 if (rn->naddrs6 > 1 && rn->naddrs6 != (u_short) -1) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3262 ngx_resolver_free_locked(r, rn->u6.addrs6); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3263 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3264 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3265 |
1649 | 3266 ngx_resolver_free_locked(r, rn); |
3267 | |
3268 /* unlock alloc mutex */ | |
3269 } | |
3270 | |
3271 | |
3272 static void * | |
3273 ngx_resolver_alloc(ngx_resolver_t *r, size_t size) | |
3274 { | |
3275 u_char *p; | |
3276 | |
3277 /* lock alloc mutex */ | |
3278 | |
3279 p = ngx_alloc(size, r->log); | |
3280 | |
3281 /* unlock alloc mutex */ | |
3282 | |
3283 return p; | |
3284 } | |
3285 | |
3286 | |
1903 | 3287 static void * |
1649 | 3288 ngx_resolver_calloc(ngx_resolver_t *r, size_t size) |
3289 { | |
3290 u_char *p; | |
3291 | |
3292 p = ngx_resolver_alloc(r, size); | |
3293 | |
3294 if (p) { | |
3295 ngx_memzero(p, size); | |
3296 } | |
3297 | |
3298 return p; | |
3299 } | |
3300 | |
3301 | |
3302 static void | |
3303 ngx_resolver_free(ngx_resolver_t *r, void *p) | |
3304 { | |
3305 /* lock alloc mutex */ | |
3306 | |
3307 ngx_free(p); | |
3308 | |
3309 /* unlock alloc mutex */ | |
3310 } | |
3311 | |
3312 | |
3313 static void | |
3314 ngx_resolver_free_locked(ngx_resolver_t *r, void *p) | |
3315 { | |
3316 ngx_free(p); | |
3317 } | |
3318 | |
3319 | |
3320 static void * | |
3321 ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size) | |
3322 { | |
3323 void *dst; | |
3324 | |
3325 dst = ngx_resolver_alloc(r, size); | |
3326 | |
3327 if (dst == NULL) { | |
3328 return dst; | |
3329 } | |
3330 | |
3331 ngx_memcpy(dst, src, size); | |
3332 | |
3333 return dst; | |
3334 } | |
3335 | |
3336 | |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3337 static ngx_addr_t * |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3338 ngx_resolver_export(ngx_resolver_t *r, ngx_resolver_node_t *rn, |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3339 ngx_uint_t rotate) |
4871
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3340 { |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3341 ngx_addr_t *dst; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3342 ngx_uint_t d, i, j, n; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3343 u_char (*sockaddr)[NGX_SOCKADDRLEN]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3344 in_addr_t *addr; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3345 struct sockaddr_in *sin; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3346 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3347 struct in6_addr *addr6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3348 struct sockaddr_in6 *sin6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3349 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3350 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3351 n = rn->naddrs; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3352 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3353 n += rn->naddrs6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3354 #endif |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3355 |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3356 dst = ngx_resolver_calloc(r, n * sizeof(ngx_addr_t)); |
4892
063ac68d89dc
Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4871
diff
changeset
|
3357 if (dst == NULL) { |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3358 return NULL; |
4892
063ac68d89dc
Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4871
diff
changeset
|
3359 } |
063ac68d89dc
Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4871
diff
changeset
|
3360 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3361 sockaddr = ngx_resolver_calloc(r, n * NGX_SOCKADDRLEN); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3362 if (sockaddr == NULL) { |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3363 ngx_resolver_free(r, dst); |
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3364 return NULL; |
4871
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3365 } |
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3366 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3367 i = 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3368 d = rotate ? ngx_random() % n : 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3369 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3370 if (rn->naddrs) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3371 j = rotate ? ngx_random() % rn->naddrs : 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3372 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3373 addr = (rn->naddrs == 1) ? &rn->u.addr : rn->u.addrs; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3374 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3375 do { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3376 sin = (struct sockaddr_in *) sockaddr[d]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3377 sin->sin_family = AF_INET; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3378 sin->sin_addr.s_addr = addr[j++]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3379 dst[d].sockaddr = (struct sockaddr *) sin; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3380 dst[d++].socklen = sizeof(struct sockaddr_in); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3381 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3382 if (d == n) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3383 d = 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3384 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3385 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3386 if (j == rn->naddrs) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3387 j = 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3388 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3389 } while (++i < rn->naddrs); |
5475
07dd5bd222ac
Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
5474
diff
changeset
|
3390 } |
4871
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3391 |
5477
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3392 #if (NGX_HAVE_INET6) |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3393 if (rn->naddrs6) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3394 j = rotate ? ngx_random() % rn->naddrs6 : 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3395 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3396 addr6 = (rn->naddrs6 == 1) ? &rn->u6.addr6 : rn->u6.addrs6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3397 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3398 do { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3399 sin6 = (struct sockaddr_in6 *) sockaddr[d]; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3400 sin6->sin6_family = AF_INET6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3401 ngx_memcpy(sin6->sin6_addr.s6_addr, addr6[j++].s6_addr, 16); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3402 dst[d].sockaddr = (struct sockaddr *) sin6; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3403 dst[d++].socklen = sizeof(struct sockaddr_in6); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3404 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3405 if (d == n) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3406 d = 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3407 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3408 |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3409 if (j == rn->naddrs6) { |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3410 j = 0; |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3411 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3412 } while (++i < n); |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3413 } |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3414 #endif |
98876ce2a7fd
Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents:
5476
diff
changeset
|
3415 |
4871
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3416 return dst; |
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3417 } |
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3418 |
c85cefbdaafe
Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4784
diff
changeset
|
3419 |
1649 | 3420 char * |
3421 ngx_resolver_strerror(ngx_int_t err) | |
3422 { | |
3423 static char *errors[] = { | |
3424 "Format error", /* FORMERR */ | |
3425 "Server failure", /* SERVFAIL */ | |
3426 "Host not found", /* NXDOMAIN */ | |
3427 "Unimplemented", /* NOTIMP */ | |
3428 "Operation refused" /* REFUSED */ | |
3429 }; | |
3430 | |
3431 if (err > 0 && err < 6) { | |
3432 return errors[err - 1]; | |
3433 } | |
3434 | |
3435 if (err == NGX_RESOLVE_TIMEDOUT) { | |
3436 return "Operation timed out"; | |
3437 } | |
3438 | |
3439 return "Unknown error"; | |
3440 } | |
3441 | |
3442 | |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3443 static u_char * |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3444 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
|
3445 { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3446 u_char *p; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3447 ngx_resolver_connection_t *rec; |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3448 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3449 p = buf; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3450 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3451 if (log->action) { |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3452 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
|
3453 len -= p - buf; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3454 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3455 |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3456 rec = log->data; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3457 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3458 if (rec) { |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3459 p = ngx_snprintf(p, len, ", resolver: %V", &rec->server); |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3460 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3461 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3462 return p; |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3463 } |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3464 |
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3465 |
1649 | 3466 ngx_int_t |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3467 ngx_udp_connect(ngx_resolver_connection_t *rec) |
1649 | 3468 { |
3469 int rc; | |
3470 ngx_int_t event; | |
3471 ngx_event_t *rev, *wev; | |
3472 ngx_socket_t s; | |
3473 ngx_connection_t *c; | |
3474 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3475 s = ngx_socket(rec->sockaddr->sa_family, SOCK_DGRAM, 0); |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3476 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3477 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "UDP socket %d", s); |
583 | 3478 |
5360
3d2d3e1cf427
Win32: MinGW GCC compatibility.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4892
diff
changeset
|
3479 if (s == (ngx_socket_t) -1) { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3480 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
583 | 3481 ngx_socket_n " failed"); |
3482 return NGX_ERROR; | |
3483 } | |
3484 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3485 c = ngx_get_connection(s, &rec->log); |
583 | 3486 |
3487 if (c == NULL) { | |
3488 if (ngx_close_socket(s) == -1) { | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3489 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
583 | 3490 ngx_close_socket_n "failed"); |
3491 } | |
3492 | |
3493 return NGX_ERROR; | |
3494 } | |
3495 | |
1649 | 3496 if (ngx_nonblocking(s) == -1) { |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3497 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
1649 | 3498 ngx_nonblocking_n " failed"); |
3499 | |
5582
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3500 goto failed; |
1649 | 3501 } |
3502 | |
583 | 3503 rev = c->read; |
3504 wev = c->write; | |
3505 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3506 rev->log = &rec->log; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3507 wev->log = &rec->log; |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3508 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3509 rec->udp = c; |
583 | 3510 |
3511 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); | |
3512 | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3513 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0, |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3514 "connect to %V, fd:%d #%uA", &rec->server, s, c->number); |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3515 |
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3516 rc = connect(s, rec->sockaddr, rec->socklen); |
1649 | 3517 |
6125
4dc8e7b62216
Removed the obsolete aio module.
Ruslan Ermilov <ru@nginx.com>
parents:
5921
diff
changeset
|
3518 /* TODO: iocp */ |
583 | 3519 |
3520 if (rc == -1) { | |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3521 ngx_log_error(NGX_LOG_CRIT, &rec->log, ngx_socket_errno, |
3408
71193a456616
add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents:
3376
diff
changeset
|
3522 "connect() failed"); |
583 | 3523 |
5582
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3524 goto failed; |
583 | 3525 } |
3526 | |
1649 | 3527 /* UDP sockets are always ready to write */ |
3528 wev->ready = 1; | |
3529 | |
6126
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3530 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ? |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3531 /* kqueue, epoll */ NGX_CLEAR_EVENT: |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3532 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT; |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3533 /* eventport event type has no meaning: oneshot only */ |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3534 |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3535 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) { |
adba26ff70b5
Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents:
6125
diff
changeset
|
3536 goto failed; |
583 | 3537 } |
3538 | |
3539 return NGX_OK; | |
5582
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3540 |
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3541 failed: |
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3542 |
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3543 ngx_close_connection(c); |
6365
d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents:
6352
diff
changeset
|
3544 rec->udp = NULL; |
5582
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3545 |
545a4d393e2f
Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents:
5505
diff
changeset
|
3546 return NGX_ERROR; |
583 | 3547 } |
6367 | 3548 |
3549 | |
3550 ngx_int_t | |
3551 ngx_tcp_connect(ngx_resolver_connection_t *rec) | |
3552 { | |
3553 int rc; | |
3554 ngx_int_t event; | |
3555 ngx_err_t err; | |
3556 ngx_uint_t level; | |
3557 ngx_socket_t s; | |
3558 ngx_event_t *rev, *wev; | |
3559 ngx_connection_t *c; | |
3560 | |
3561 s = ngx_socket(rec->sockaddr->sa_family, SOCK_STREAM, 0); | |
3562 | |
3563 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "TCP socket %d", s); | |
3564 | |
3565 if (s == (ngx_socket_t) -1) { | |
3566 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, | |
3567 ngx_socket_n " failed"); | |
3568 return NGX_ERROR; | |
3569 } | |
3570 | |
3571 c = ngx_get_connection(s, &rec->log); | |
3572 | |
3573 if (c == NULL) { | |
3574 if (ngx_close_socket(s) == -1) { | |
3575 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, | |
3576 ngx_close_socket_n "failed"); | |
3577 } | |
3578 | |
3579 return NGX_ERROR; | |
3580 } | |
3581 | |
3582 if (ngx_nonblocking(s) == -1) { | |
3583 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, | |
3584 ngx_nonblocking_n " failed"); | |
3585 | |
3586 goto failed; | |
3587 } | |
3588 | |
3589 rev = c->read; | |
3590 wev = c->write; | |
3591 | |
3592 rev->log = &rec->log; | |
3593 wev->log = &rec->log; | |
3594 | |
3595 rec->tcp = c; | |
3596 | |
3597 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); | |
3598 | |
3599 if (ngx_add_conn) { | |
3600 if (ngx_add_conn(c) == NGX_ERROR) { | |
3601 goto failed; | |
3602 } | |
3603 } | |
3604 | |
3605 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0, | |
3606 "connect to %V, fd:%d #%uA", &rec->server, s, c->number); | |
3607 | |
3608 rc = connect(s, rec->sockaddr, rec->socklen); | |
3609 | |
3610 if (rc == -1) { | |
3611 err = ngx_socket_errno; | |
3612 | |
3613 | |
3614 if (err != NGX_EINPROGRESS | |
3615 #if (NGX_WIN32) | |
3616 /* Winsock returns WSAEWOULDBLOCK (NGX_EAGAIN) */ | |
3617 && err != NGX_EAGAIN | |
3618 #endif | |
3619 ) | |
3620 { | |
3621 if (err == NGX_ECONNREFUSED | |
3622 #if (NGX_LINUX) | |
3623 /* | |
3624 * Linux returns EAGAIN instead of ECONNREFUSED | |
3625 * for unix sockets if listen queue is full | |
3626 */ | |
3627 || err == NGX_EAGAIN | |
3628 #endif | |
3629 || err == NGX_ECONNRESET | |
3630 || err == NGX_ENETDOWN | |
3631 || err == NGX_ENETUNREACH | |
3632 || err == NGX_EHOSTDOWN | |
3633 || err == NGX_EHOSTUNREACH) | |
3634 { | |
3635 level = NGX_LOG_ERR; | |
3636 | |
3637 } else { | |
3638 level = NGX_LOG_CRIT; | |
3639 } | |
3640 | |
3641 ngx_log_error(level, c->log, err, "connect() to %V failed", | |
3642 &rec->server); | |
3643 | |
3644 ngx_close_connection(c); | |
3645 rec->tcp = NULL; | |
3646 | |
3647 return NGX_ERROR; | |
3648 } | |
3649 } | |
3650 | |
3651 if (ngx_add_conn) { | |
3652 if (rc == -1) { | |
3653 | |
3654 /* NGX_EINPROGRESS */ | |
3655 | |
3656 return NGX_AGAIN; | |
3657 } | |
3658 | |
3659 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "connected"); | |
3660 | |
3661 wev->ready = 1; | |
3662 | |
3663 return NGX_OK; | |
3664 } | |
3665 | |
3666 if (ngx_event_flags & NGX_USE_IOCP_EVENT) { | |
3667 | |
3668 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, ngx_socket_errno, | |
3669 "connect(): %d", rc); | |
3670 | |
3671 if (ngx_blocking(s) == -1) { | |
3672 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, | |
3673 ngx_blocking_n " failed"); | |
3674 goto failed; | |
3675 } | |
3676 | |
3677 /* | |
3678 * FreeBSD's aio allows to post an operation on non-connected socket. | |
3679 * NT does not support it. | |
3680 * | |
3681 * TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT | |
3682 */ | |
3683 | |
3684 rev->ready = 1; | |
3685 wev->ready = 1; | |
3686 | |
3687 return NGX_OK; | |
3688 } | |
3689 | |
3690 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) { | |
3691 | |
3692 /* kqueue */ | |
3693 | |
3694 event = NGX_CLEAR_EVENT; | |
3695 | |
3696 } else { | |
3697 | |
3698 /* select, poll, /dev/poll */ | |
3699 | |
3700 event = NGX_LEVEL_EVENT; | |
3701 } | |
3702 | |
3703 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) { | |
3704 goto failed; | |
3705 } | |
3706 | |
3707 if (rc == -1) { | |
3708 | |
3709 /* NGX_EINPROGRESS */ | |
3710 | |
3711 if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) { | |
3712 goto failed; | |
3713 } | |
3714 | |
3715 return NGX_AGAIN; | |
3716 } | |
3717 | |
3718 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "connected"); | |
3719 | |
3720 wev->ready = 1; | |
3721 | |
3722 return NGX_OK; | |
3723 | |
3724 failed: | |
3725 | |
3726 ngx_close_connection(c); | |
3727 rec->tcp = NULL; | |
3728 | |
3729 return NGX_ERROR; | |
3730 } |