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