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