annotate src/core/ngx_resolver.c @ 6699:9cf2dce316e5

Fixed log levels of configuration parsing errors. All the errors that prevent loading configuration must be printed on the "emerg" log level. Previously, nginx might silently fail to load configuration in some cases as the default log level is "error".
author Valentin Bartenev <vbart@nginx.com>
date Tue, 20 Sep 2016 15:07:16 +0300
parents b3b7e33083ac
children c3e3de6d2672
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 /*
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
4412
d620f497c50f Copyright updated.
Maxim Konovalov <maxim@nginx.com>
parents: 4296
diff changeset
4 * Copyright (C) Nginx, Inc.
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5 */
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 #include <ngx_config.h>
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 #include <ngx_core.h>
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10 #include <ngx_event.h>
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
13 #define NGX_RESOLVER_UDP_SIZE 4096
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
14
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
15 #define NGX_RESOLVER_TCP_RSIZE (2 + 65535)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
16 #define NGX_RESOLVER_TCP_WSIZE 8192
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
17
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
18
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
19 typedef struct {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
20 u_char ident_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
21 u_char ident_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
22 u_char flags_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
23 u_char flags_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
24 u_char nqs_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
25 u_char nqs_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
26 u_char nan_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
27 u_char nan_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
28 u_char nns_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
29 u_char nns_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
30 u_char nar_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
31 u_char nar_lo;
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
32 } ngx_resolver_hdr_t;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
33
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
34
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
35 typedef struct {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
36 u_char type_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
37 u_char type_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
38 u_char class_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
39 u_char class_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
40 } ngx_resolver_qs_t;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
41
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
42
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
43 typedef struct {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
44 u_char type_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
45 u_char type_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
46 u_char class_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
47 u_char class_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
48 u_char ttl[4];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
49 u_char len_hi;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
50 u_char len_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
51 } ngx_resolver_an_t;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
52
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
53
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
54 #define ngx_resolver_node(n) \
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
55 (ngx_resolver_node_t *) \
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
56 ((u_char *) (n) - offsetof(ngx_resolver_node_t, node))
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
57
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
58
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
59 ngx_int_t ngx_udp_connect(ngx_resolver_connection_t *rec);
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
60 ngx_int_t ngx_tcp_connect(ngx_resolver_connection_t *rec);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
61
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
62
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
63 static void ngx_resolver_cleanup(void *data);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
64 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
65 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r,
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
66 ngx_resolver_ctx_t *ctx, ngx_str_t *name);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
67 static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
68 ngx_queue_t *queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
69 static ngx_int_t ngx_resolver_send_query(ngx_resolver_t *r,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
70 ngx_resolver_node_t *rn);
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
71 static ngx_int_t ngx_resolver_send_udp_query(ngx_resolver_t *r,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
72 ngx_resolver_connection_t *rec, u_char *query, u_short qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
73 static ngx_int_t ngx_resolver_send_tcp_query(ngx_resolver_t *r,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
74 ngx_resolver_connection_t *rec, u_char *query, u_short qlen);
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
75 static ngx_int_t ngx_resolver_create_name_query(ngx_resolver_t *r,
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
76 ngx_resolver_node_t *rn, ngx_str_t *name);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
77 static ngx_int_t ngx_resolver_create_srv_query(ngx_resolver_t *r,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
78 ngx_resolver_node_t *rn, ngx_str_t *name);
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
79 static ngx_int_t ngx_resolver_create_addr_query(ngx_resolver_t *r,
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
80 ngx_resolver_node_t *rn, ngx_resolver_addr_t *addr);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
81 static void ngx_resolver_resend_handler(ngx_event_t *ev);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
82 static time_t ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
83 ngx_queue_t *queue);
6196
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
84 static ngx_uint_t ngx_resolver_resend_empty(ngx_resolver_t *r);
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
85 static void ngx_resolver_udp_read(ngx_event_t *rev);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
86 static void ngx_resolver_tcp_write(ngx_event_t *wev);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
87 static void ngx_resolver_tcp_read(ngx_event_t *rev);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
88 static void ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf,
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
89 size_t n, ngx_uint_t tcp);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
90 static void ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n,
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
91 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t qtype,
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
92 ngx_uint_t nan, ngx_uint_t trunc, ngx_uint_t ans);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
93 static void ngx_resolver_process_srv(ngx_resolver_t *r, u_char *buf, size_t n,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
94 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
95 ngx_uint_t trunc, ngx_uint_t ans);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
96 static void ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
97 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
98 static ngx_resolver_node_t *ngx_resolver_lookup_name(ngx_resolver_t *r,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
99 ngx_str_t *name, uint32_t hash);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
100 static ngx_resolver_node_t *ngx_resolver_lookup_srv(ngx_resolver_t *r,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
101 ngx_str_t *name, uint32_t hash);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
102 static ngx_resolver_node_t *ngx_resolver_lookup_addr(ngx_resolver_t *r,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
103 in_addr_t addr);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
104 static void ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
105 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
106 static ngx_int_t ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
107 u_char *buf, u_char *src, u_char *last);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
108 static void ngx_resolver_timeout_handler(ngx_event_t *ev);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
109 static void ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
110 static void *ngx_resolver_alloc(ngx_resolver_t *r, size_t size);
1903
1ea922585517 static ngx_resolver_calloc()
Igor Sysoev <igor@sysoev.ru>
parents: 1880
diff changeset
111 static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
112 static void ngx_resolver_free(ngx_resolver_t *r, void *p);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
113 static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
114 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
115 static ngx_resolver_addr_t *ngx_resolver_export(ngx_resolver_t *r,
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
116 ngx_resolver_node_t *rn, ngx_uint_t rotate);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
117 static void ngx_resolver_report_srv(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx);
3408
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
118 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
119 static void ngx_resolver_resolve_srv_names(ngx_resolver_ctx_t *ctx,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
120 ngx_resolver_node_t *rn);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
121 static void ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
122 static ngx_int_t ngx_resolver_cmp_srvs(const void *one, const void *two);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
123
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
124 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
125 static void ngx_resolver_rbtree_insert_addr6_value(ngx_rbtree_node_t *temp,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
126 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
127 static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
128 struct in6_addr *addr, uint32_t hash);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
129 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
130
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
131
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
132 ngx_resolver_t *
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
133 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
134 {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
135 ngx_str_t s;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
136 ngx_url_t u;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
137 ngx_uint_t i, j;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
138 ngx_resolver_t *r;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
139 ngx_pool_cleanup_t *cln;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
140 ngx_resolver_connection_t *rec;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
141
1913
c0f873458e2b use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents: 1906
diff changeset
142 cln = ngx_pool_cleanup_add(cf->pool, 0);
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
143 if (cln == NULL) {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
144 return NULL;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
145 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
146
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
147 cln->handler = ngx_resolver_cleanup;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
148
1913
c0f873458e2b use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents: 1906
diff changeset
149 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
150 if (r == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
151 return NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
152 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
153
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
154 cln->data = r;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
155
1913
c0f873458e2b use cf->cycle->new_log because at merge stage cf->pool->log is old log
Igor Sysoev <igor@sysoev.ru>
parents: 1906
diff changeset
156 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
157 if (r->event == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
158 return NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
159 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
160
1687
568d432effb9 use ngx_rbtree_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1685
diff changeset
161 ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel,
568d432effb9 use ngx_rbtree_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1685
diff changeset
162 ngx_resolver_rbtree_insert_value);
568d432effb9 use ngx_rbtree_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1685
diff changeset
163
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
164 ngx_rbtree_init(&r->srv_rbtree, &r->srv_sentinel,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
165 ngx_resolver_rbtree_insert_value);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
166
1687
568d432effb9 use ngx_rbtree_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1685
diff changeset
167 ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel,
568d432effb9 use ngx_rbtree_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1685
diff changeset
168 ngx_rbtree_insert_value);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
169
1685
a24c909ad879 use ngx_queue_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1683
diff changeset
170 ngx_queue_init(&r->name_resend_queue);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
171 ngx_queue_init(&r->srv_resend_queue);
1685
a24c909ad879 use ngx_queue_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1683
diff changeset
172 ngx_queue_init(&r->addr_resend_queue);
a24c909ad879 use ngx_queue_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1683
diff changeset
173
a24c909ad879 use ngx_queue_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1683
diff changeset
174 ngx_queue_init(&r->name_expire_queue);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
175 ngx_queue_init(&r->srv_expire_queue);
1685
a24c909ad879 use ngx_queue_init()
Igor Sysoev <igor@sysoev.ru>
parents: 1683
diff changeset
176 ngx_queue_init(&r->addr_expire_queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
177
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
178 #if (NGX_HAVE_INET6)
5478
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
179 r->ipv6 = 1;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
180
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
181 ngx_rbtree_init(&r->addr6_rbtree, &r->addr6_sentinel,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
182 ngx_resolver_rbtree_insert_addr6_value);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
183
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
184 ngx_queue_init(&r->addr6_resend_queue);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
185
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
186 ngx_queue_init(&r->addr6_expire_queue);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
187 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
188
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
189 r->event->handler = ngx_resolver_resend_handler;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
190 r->event->data = r;
2785
d478379e51ac *) refactor error_log processing: listen socket log might inherit built-in
Igor Sysoev <igor@sysoev.ru>
parents: 2490
diff changeset
191 r->event->log = &cf->cycle->new_log;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
192 r->ident = -1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
193
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
194 r->resend_timeout = 5;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
195 r->tcp_timeout = 5;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
196 r->expire = 30;
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
197 r->valid = 0;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
198
2785
d478379e51ac *) refactor error_log processing: listen socket log might inherit built-in
Igor Sysoev <igor@sysoev.ru>
parents: 2490
diff changeset
199 r->log = &cf->cycle->new_log;
3763
beca53d6ab3c decrease resolver errors level to error
Igor Sysoev <igor@sysoev.ru>
parents: 3642
diff changeset
200 r->log_level = NGX_LOG_ERR;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
201
4784
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
202 if (n) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
203 if (ngx_array_init(&r->connections, cf->pool, n,
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
204 sizeof(ngx_resolver_connection_t))
4784
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
205 != NGX_OK)
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
206 {
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
207 return NULL;
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
208 }
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
209 }
dd63abf20ba7 Resolver: fixed possible memory leak in ngx_resolver_create().
Maxim Dounin <mdounin@mdounin.ru>
parents: 4684
diff changeset
210
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
211 for (i = 0; i < n; i++) {
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
212 if (ngx_strncmp(names[i].data, "valid=", 6) == 0) {
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
213 s.len = names[i].len - 6;
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
214 s.data = names[i].data + 6;
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
215
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
216 r->valid = ngx_parse_time(&s, 1);
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
217
4474
41f640a693de Time parsing cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4412
diff changeset
218 if (r->valid == (time_t) NGX_ERROR) {
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
219 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
220 "invalid parameter: %V", &names[i]);
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
221 return NULL;
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
222 }
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
223
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
224 continue;
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
225 }
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
226
5478
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
227 #if (NGX_HAVE_INET6)
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
228 if (ngx_strncmp(names[i].data, "ipv6=", 5) == 0) {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
229
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
230 if (ngx_strcmp(&names[i].data[5], "on") == 0) {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
231 r->ipv6 = 1;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
232
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
233 } else if (ngx_strcmp(&names[i].data[5], "off") == 0) {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
234 r->ipv6 = 0;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
235
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
236 } else {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
237 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
238 "invalid parameter: %V", &names[i]);
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
239 return NULL;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
240 }
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
241
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
242 continue;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
243 }
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
244 #endif
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
245
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
246 ngx_memzero(&u, sizeof(ngx_url_t));
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
247
4671
af9342747669 Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents: 4653
diff changeset
248 u.url = names[i];
af9342747669 Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents: 4653
diff changeset
249 u.default_port = 53;
af9342747669 Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents: 4653
diff changeset
250
af9342747669 Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents: 4653
diff changeset
251 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
4643
bc5f881323b8 Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents: 4619
diff changeset
252 if (u.err) {
bc5f881323b8 Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents: 4619
diff changeset
253 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
bc5f881323b8 Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents: 4619
diff changeset
254 "%s in resolver \"%V\"",
4671
af9342747669 Support for IPv6 literals and an optional port in resolver.
Ruslan Ermilov <ru@nginx.com>
parents: 4653
diff changeset
255 u.err, &u.url);
4643
bc5f881323b8 Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents: 4619
diff changeset
256 }
bc5f881323b8 Fixed potential null pointer dereference in ngx_resolver_create().
Ruslan Ermilov <ru@nginx.com>
parents: 4619
diff changeset
257
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
258 return NULL;
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
259 }
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
260
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
261 rec = ngx_array_push_n(&r->connections, u.naddrs);
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
262 if (rec == NULL) {
1683
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
263 return NULL;
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
264 }
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
265
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
266 ngx_memzero(rec, u.naddrs * sizeof(ngx_resolver_connection_t));
4684
f5c2c9d656f9 When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents: 4683
diff changeset
267
f5c2c9d656f9 When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents: 4683
diff changeset
268 for (j = 0; j < u.naddrs; j++) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
269 rec[j].sockaddr = u.addrs[j].sockaddr;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
270 rec[j].socklen = u.addrs[j].socklen;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
271 rec[j].server = u.addrs[j].name;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
272 rec[j].resolver = r;
4684
f5c2c9d656f9 When "resolver" is configured with a domain name, only the first
Ruslan Ermilov <ru@nginx.com>
parents: 4683
diff changeset
273 }
1683
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
274 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
275
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
276 return r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
277 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
278
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
279
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
280 static void
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
281 ngx_resolver_cleanup(void *data)
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
282 {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
283 ngx_resolver_t *r = data;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
284
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
285 ngx_uint_t i;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
286 ngx_resolver_connection_t *rec;
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
287
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
288 if (r) {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
289 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
290 "cleanup resolver");
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
291
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
292 ngx_resolver_cleanup_tree(r, &r->name_rbtree);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
293
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
294 ngx_resolver_cleanup_tree(r, &r->srv_rbtree);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
295
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
296 ngx_resolver_cleanup_tree(r, &r->addr_rbtree);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
297
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
298 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
299 ngx_resolver_cleanup_tree(r, &r->addr6_rbtree);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
300 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
301
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
302 if (r->event) {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
303 ngx_free(r->event);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
304 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
305
4225
016352c19049 Support of several servers in the "resolver" directive.
Igor Sysoev <igor@sysoev.ru>
parents: 3902
diff changeset
306
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
307 rec = r->connections.elts;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
308
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
309 for (i = 0; i < r->connections.nelts; i++) {
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
310 if (rec[i].udp) {
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
311 ngx_close_connection(rec[i].udp);
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
312 }
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
313
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
314 if (rec[i].tcp) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
315 ngx_close_connection(rec[i].tcp);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
316 }
6369
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
317
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
318 if (rec[i].read_buf) {
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
319 ngx_resolver_free(r, rec[i].read_buf->start);
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
320 ngx_resolver_free(r, rec[i].read_buf);
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
321 }
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
322
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
323 if (rec[i].write_buf) {
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
324 ngx_resolver_free(r, rec[i].write_buf->start);
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
325 ngx_resolver_free(r, rec[i].write_buf);
06459bc8b92f Resolver: free TCP buffers on resolver cleanup.
Roman Arutyunyan <arut@nginx.com>
parents: 6368
diff changeset
326 }
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
327 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
328
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
329 ngx_free(r);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
330 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
331 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
332
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
333
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
334 static void
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
335 ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree)
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
336 {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
337 ngx_resolver_ctx_t *ctx, *next;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
338 ngx_resolver_node_t *rn;
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
339
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
340 while (tree->root != tree->sentinel) {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
341
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
342 rn = ngx_resolver_node(ngx_rbtree_min(tree->root, tree->sentinel));
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
343
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
344 ngx_queue_remove(&rn->queue);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
345
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
346 for (ctx = rn->waiting; ctx; ctx = next) {
2006
b52cb9bf2064 style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents: 1969
diff changeset
347 next = ctx->next;
1906
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
348
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
349 if (ctx->event) {
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
350 ngx_resolver_free(r, ctx->event);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
351 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
352
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
353 ngx_resolver_free(r, ctx);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
354 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
355
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
356 ngx_rbtree_delete(tree, &rn->node);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
357
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
358 ngx_resolver_free_node(r, rn);
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
359 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
360 }
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
361
4a8c9139e579 cleanup resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1904
diff changeset
362
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
363 ngx_resolver_ctx_t *
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
364 ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
365 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
366 in_addr_t addr;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
367 ngx_resolver_ctx_t *ctx;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
368
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
369 if (temp) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
370 addr = ngx_inet_addr(temp->name.data, temp->name.len);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
371
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
372 if (addr != INADDR_NONE) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
373 temp->resolver = r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
374 temp->state = NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
375 temp->naddrs = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
376 temp->addrs = &temp->addr;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
377 temp->addr.sockaddr = (struct sockaddr *) &temp->sin;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
378 temp->addr.socklen = sizeof(struct sockaddr_in);
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
379 ngx_memzero(&temp->sin, sizeof(struct sockaddr_in));
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
380 temp->sin.sin_family = AF_INET;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
381 temp->sin.sin_addr.s_addr = addr;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
382 temp->quick = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
383
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
384 return temp;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
385 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
386 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
387
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
388 if (r->connections.nelts == 0) {
1683
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
389 return NGX_NO_RESOLVER;
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
390 }
1e0b028055ec allow to use IP addresses without defined resolver
Igor Sysoev <igor@sysoev.ru>
parents: 1679
diff changeset
391
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
392 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
393
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
394 if (ctx) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
395 ctx->resolver = r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
396 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
397
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
398 return ctx;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
399 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
400
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
401
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
402 ngx_int_t
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
403 ngx_resolve_name(ngx_resolver_ctx_t *ctx)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
404 {
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
405 size_t slen;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
406 ngx_int_t rc;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
407 ngx_str_t name;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
408 ngx_resolver_t *r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
409
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
410 r = ctx->resolver;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
411
5505
d091d16ed398 Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents: 5485
diff changeset
412 if (ctx->name.len > 0 && ctx->name.data[ctx->name.len - 1] == '.') {
d091d16ed398 Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents: 5485
diff changeset
413 ctx->name.len--;
d091d16ed398 Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents: 5485
diff changeset
414 }
d091d16ed398 Resolver: added support for domain names with a trailing dot.
Yichun Zhang <agentzh@gmail.com>
parents: 5485
diff changeset
415
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
416 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
417 "resolve: \"%V\"", &ctx->name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
418
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
419 if (ctx->quick) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
420 ctx->handler(ctx);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
421 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
422 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
423
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
424 if (ctx->service.len) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
425 slen = ctx->service.len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
426
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
427 if (ngx_strlchr(ctx->service.data,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
428 ctx->service.data + ctx->service.len, '.')
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
429 == NULL)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
430 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
431 slen += sizeof("_._tcp") - 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
432 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
433
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
434 name.len = slen + 1 + ctx->name.len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
435
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
436 name.data = ngx_resolver_alloc(r, name.len);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
437 if (name.data == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
438 return NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
439 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
440
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
441 if (slen == ctx->service.len) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
442 ngx_sprintf(name.data, "%V.%V", &ctx->service, &ctx->name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
443
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
444 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
445 ngx_sprintf(name.data, "_%V._tcp.%V", &ctx->service, &ctx->name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
446 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
447
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
448 /* lock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
449
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
450 rc = ngx_resolve_name_locked(r, ctx, &name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
451
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
452 ngx_resolver_free(r, name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
453
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
454 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
455 /* lock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
456
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
457 rc = ngx_resolve_name_locked(r, ctx, &ctx->name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
458 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
459
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
460 if (rc == NGX_OK) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
461 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
462 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
463
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
464 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
465
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
466 if (rc == NGX_AGAIN) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
467 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
468 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
469
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
470 /* NGX_ERROR */
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
471
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
472 if (ctx->event) {
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
473 ngx_resolver_free(r, ctx->event);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
474 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
475
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
476 ngx_resolver_free(r, ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
477
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
478 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
479 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
480
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
481
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
482 void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
483 ngx_resolve_name_done(ngx_resolver_ctx_t *ctx)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
484 {
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
485 ngx_uint_t i;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
486 ngx_resolver_t *r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
487 ngx_resolver_ctx_t *w, **p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
488 ngx_resolver_node_t *rn;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
489
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
490 r = ctx->resolver;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
491
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
492 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
493 "resolve name done: %i", ctx->state);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
494
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
495 if (ctx->quick) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
496 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
497 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
498
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
499 if (ctx->event && ctx->event->timer_set) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
500 ngx_del_timer(ctx->event);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
501 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
502
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
503 /* lock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
504
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
505 if (ctx->nsrvs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
506 for (i = 0; i < ctx->nsrvs; i++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
507 if (ctx->srvs[i].ctx) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
508 ngx_resolve_name_done(ctx->srvs[i].ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
509 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
510
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
511 if (ctx->srvs[i].addrs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
512 ngx_resolver_free(r, ctx->srvs[i].addrs->sockaddr);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
513 ngx_resolver_free(r, ctx->srvs[i].addrs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
514 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
515
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
516 ngx_resolver_free(r, ctx->srvs[i].name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
517 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
518
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
519 ngx_resolver_free(r, ctx->srvs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
520 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
521
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
522 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
523
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
524 rn = ctx->node;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
525
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
526 if (rn) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
527 p = &rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
528 w = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
529
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
530 while (w) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
531 if (w == ctx) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
532 *p = w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
533
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
534 goto done;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
535 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
536
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
537 p = &w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
538 w = w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
539 }
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
540
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
541 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
542 "could not cancel %V resolving", &ctx->name);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
543 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
544 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
545
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
546 done:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
547
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
548 if (ctx->service.len) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
549 ngx_resolver_expire(r, &r->srv_rbtree, &r->srv_expire_queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
550
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
551 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
552 ngx_resolver_expire(r, &r->name_rbtree, &r->name_expire_queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
553 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
554
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
555 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
556
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
557 /* lock alloc mutex */
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
558
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
559 if (ctx->event) {
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
560 ngx_resolver_free_locked(r, ctx->event);
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
561 }
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
562
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
563 ngx_resolver_free_locked(r, ctx);
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
564
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
565 /* unlock alloc mutex */
6196
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
566
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
567 if (r->event->timer_set && ngx_resolver_resend_empty(r)) {
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
568 ngx_del_timer(r->event);
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
569 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
570 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
571
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
572
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
573 static ngx_int_t
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
574 ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx,
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
575 ngx_str_t *name)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
576 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
577 uint32_t hash;
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
578 ngx_int_t rc;
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
579 ngx_str_t cname;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
580 ngx_uint_t i, naddrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
581 ngx_queue_t *resend_queue, *expire_queue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
582 ngx_rbtree_t *tree;
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
583 ngx_resolver_ctx_t *next, *last;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
584 ngx_resolver_addr_t *addrs;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
585 ngx_resolver_node_t *rn;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
586
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
587 ngx_strlow(name->data, name->data, name->len);
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
588
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
589 hash = ngx_crc32_short(name->data, name->len);
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
590
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
591 if (ctx->service.len) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
592 rn = ngx_resolver_lookup_srv(r, name, hash);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
593
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
594 tree = &r->srv_rbtree;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
595 resend_queue = &r->srv_resend_queue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
596 expire_queue = &r->srv_expire_queue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
597
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
598 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
599 rn = ngx_resolver_lookup_name(r, name, hash);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
600
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
601 tree = &r->name_rbtree;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
602 resend_queue = &r->name_resend_queue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
603 expire_queue = &r->name_expire_queue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
604 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
605
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
606 if (rn) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
607
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
608 /* ctx can be a list after NGX_RESOLVE_CNAME */
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
609 for (last = ctx; last->next; last = last->next);
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
610
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
611 if (rn->valid >= ngx_time()) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
612
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
613 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
614
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
615 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
616
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
617 rn->expire = ngx_time() + r->expire;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
618
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
619 ngx_queue_insert_head(expire_queue, &rn->queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
620
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
621 naddrs = (rn->naddrs == (u_short) -1) ? 0 : rn->naddrs;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
622 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
623 naddrs += (rn->naddrs6 == (u_short) -1) ? 0 : rn->naddrs6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
624 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
625
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
626 if (naddrs) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
627
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
628 if (naddrs == 1 && rn->naddrs == 1) {
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
629 addrs = NULL;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
630
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
631 } else {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
632 addrs = ngx_resolver_export(r, rn, 1);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
633 if (addrs == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
634 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
635 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
636 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
637
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
638 last->next = rn->waiting;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
639 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
640
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
641 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
642
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
643 do {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
644 ctx->state = NGX_OK;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
645 ctx->valid = rn->valid;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
646 ctx->naddrs = naddrs;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
647
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
648 if (addrs == NULL) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
649 ctx->addrs = &ctx->addr;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
650 ctx->addr.sockaddr = (struct sockaddr *) &ctx->sin;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
651 ctx->addr.socklen = sizeof(struct sockaddr_in);
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
652 ngx_memzero(&ctx->sin, sizeof(struct sockaddr_in));
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
653 ctx->sin.sin_family = AF_INET;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
654 ctx->sin.sin_addr.s_addr = rn->u.addr;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
655
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
656 } else {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
657 ctx->addrs = addrs;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
658 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
659
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
660 next = ctx->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
661
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
662 ctx->handler(ctx);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
663
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
664 ctx = next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
665 } while (ctx);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
666
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
667 if (addrs != NULL) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
668 ngx_resolver_free(r, addrs->sockaddr);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
669 ngx_resolver_free(r, addrs);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
670 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
671
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
672 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
673 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
674
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
675 if (rn->nsrvs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
676 last->next = rn->waiting;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
677 rn->waiting = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
678
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
679 /* unlock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
680
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
681 do {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
682 next = ctx->next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
683
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
684 ngx_resolver_resolve_srv_names(ctx, rn);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
685
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
686 ctx = next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
687 } while (ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
688
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
689 return NGX_OK;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
690 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
691
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
692 /* NGX_RESOLVE_CNAME */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
693
1969
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
694 if (ctx->recursion++ < NGX_RESOLVER_MAX_RECURSION) {
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
695
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
696 cname.len = rn->cnlen;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
697 cname.data = rn->u.cname;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
698
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
699 return ngx_resolve_name_locked(r, ctx, &cname);
1969
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
700 }
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
701
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
702 last->next = rn->waiting;
1969
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
703 rn->waiting = NULL;
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
704
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
705 /* unlock name mutex */
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
706
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
707 do {
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
708 ctx->state = NGX_RESOLVE_NXDOMAIN;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
709 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
1969
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
710 next = ctx->next;
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
711
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
712 ctx->handler(ctx);
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
713
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
714 ctx = next;
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
715 } while (ctx);
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
716
41fd9a2e0755 limit CNAME recursion
Igor Sysoev <igor@sysoev.ru>
parents: 1967
diff changeset
717 return NGX_OK;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
718 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
719
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
720 if (rn->waiting) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
721
6457
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
722 if (ctx->event == NULL && ctx->timeout) {
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
723 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
724 if (ctx->event == NULL) {
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
725 return NGX_ERROR;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
726 }
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
727
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
728 ctx->event->handler = ngx_resolver_timeout_handler;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
729 ctx->event->data = ctx;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
730 ctx->event->log = r->log;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
731 ctx->ident = -1;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
732
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
733 ngx_add_timer(ctx->event, ctx->timeout);
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
734 }
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
735
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
736 last->next = rn->waiting;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
737 rn->waiting = ctx;
3297
ebc5384479b4 fix segfault in resolver:
Igor Sysoev <igor@sysoev.ru>
parents: 3269
diff changeset
738 ctx->state = NGX_AGAIN;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
739
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
740 do {
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
741 ctx->node = rn;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
742 ctx = ctx->next;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
743 } while (ctx);
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
744
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
745 return NGX_AGAIN;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
746 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
747
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
748 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
749
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
750 /* lock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
751
4619
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
752 if (rn->query) {
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
753 ngx_resolver_free_locked(r, rn->query);
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
754 rn->query = NULL;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
755 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
756 rn->query6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
757 #endif
4619
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
758 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
759
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
760 if (rn->cnlen) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
761 ngx_resolver_free_locked(r, rn->u.cname);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
762 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
763
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
764 if (rn->naddrs > 1 && rn->naddrs != (u_short) -1) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
765 ngx_resolver_free_locked(r, rn->u.addrs);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
766 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
767
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
768 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
769 if (rn->naddrs6 > 1 && rn->naddrs6 != (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
770 ngx_resolver_free_locked(r, rn->u6.addrs6);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
771 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
772 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
773
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
774 if (rn->nsrvs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
775 for (i = 0; i < rn->nsrvs; i++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
776 if (rn->u.srvs[i].name.data) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
777 ngx_resolver_free_locked(r, rn->u.srvs[i].name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
778 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
779 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
780
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
781 ngx_resolver_free_locked(r, rn->u.srvs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
782 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
783
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
784 /* unlock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
785
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
786 } else {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
787
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
788 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
789 if (rn == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
790 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
791 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
792
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
793 rn->name = ngx_resolver_dup(r, name->data, name->len);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
794 if (rn->name == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
795 ngx_resolver_free(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
796 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
797 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
798
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
799 rn->node.key = hash;
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
800 rn->nlen = (u_short) name->len;
1960
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
801 rn->query = NULL;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
802 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
803 rn->query6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
804 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
805
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
806 ngx_rbtree_insert(tree, &rn->node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
807 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
808
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
809 if (ctx->service.len) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
810 rc = ngx_resolver_create_srv_query(r, rn, name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
811
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
812 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
813 rc = ngx_resolver_create_name_query(r, rn, name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
814 }
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
815
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
816 if (rc == NGX_ERROR) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
817 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
818 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
819
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
820 if (rc == NGX_DECLINED) {
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
821 ngx_rbtree_delete(tree, &rn->node);
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
822
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
823 ngx_resolver_free(r, rn->query);
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
824 ngx_resolver_free(r, rn->name);
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
825 ngx_resolver_free(r, rn);
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
826
6349
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
827 do {
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
828 ctx->state = NGX_RESOLVE_NXDOMAIN;
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
829 next = ctx->next;
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
830
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
831 ctx->handler(ctx);
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
832
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
833 ctx = next;
978e79b95c9f Resolver: fixed CNAME processing for several requests.
Ruslan Ermilov <ru@nginx.com>
parents: 6348
diff changeset
834 } while (ctx);
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
835
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
836 return NGX_OK;
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
837 }
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
838
6366
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
839 rn->last_connection = r->last_connection++;
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
840 if (r->last_connection == r->connections.nelts) {
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
841 r->last_connection = 0;
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
842 }
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
843
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
844 rn->naddrs = (u_short) -1;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
845 rn->tcp = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
846 #if (NGX_HAVE_INET6)
5478
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
847 rn->naddrs6 = r->ipv6 ? (u_short) -1 : 0;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
848 rn->tcp6 = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
849 #endif
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
850 rn->nsrvs = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
851
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
852 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
853 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
854 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
855
6457
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
856 if (ctx->event == NULL && ctx->timeout) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
857 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
858 if (ctx->event == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
859 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
860 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
861
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
862 ctx->event->handler = ngx_resolver_timeout_handler;
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
863 ctx->event->data = ctx;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
864 ctx->event->log = r->log;
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
865 ctx->ident = -1;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
866
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
867 ngx_add_timer(ctx->event, ctx->timeout);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
868 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
869
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
870 if (ngx_queue_empty(resend_queue)) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
871 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
872 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
873
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
874 rn->expire = ngx_time() + r->resend_timeout;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
875
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
876 ngx_queue_insert_head(resend_queue, &rn->queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
877
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
878 rn->code = 0;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
879 rn->cnlen = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
880 rn->valid = 0;
5485
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
881 rn->ttl = NGX_MAX_UINT32_VALUE;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
882 rn->waiting = ctx;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
883
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
884 ctx->state = NGX_AGAIN;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
885
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
886 do {
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
887 ctx->node = rn;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
888 ctx = ctx->next;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
889 } while (ctx);
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
890
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
891 return NGX_AGAIN;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
892
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
893 failed:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
894
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
895 ngx_rbtree_delete(tree, &rn->node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
896
1960
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
897 if (rn->query) {
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
898 ngx_resolver_free(r, rn->query);
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
899 }
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
900
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
901 ngx_resolver_free(r, rn->name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
902
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
903 ngx_resolver_free(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
904
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
905 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
906 }
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
907
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
908
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
909 ngx_int_t
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
910 ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
911 {
2484
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
912 u_char *name;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
913 in_addr_t addr;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
914 ngx_queue_t *resend_queue, *expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
915 ngx_rbtree_t *tree;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
916 ngx_resolver_t *r;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
917 struct sockaddr_in *sin;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
918 ngx_resolver_node_t *rn;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
919 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
920 uint32_t hash;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
921 struct sockaddr_in6 *sin6;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
922 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
923
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
924 #if (NGX_SUPPRESS_WARN)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
925 addr = 0;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
926 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
927 hash = 0;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
928 sin6 = NULL;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
929 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
930 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
931
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
932 r = ctx->resolver;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
933
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
934 switch (ctx->addr.sockaddr->sa_family) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
935
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
936 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
937 case AF_INET6:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
938 sin6 = (struct sockaddr_in6 *) ctx->addr.sockaddr;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
939 hash = ngx_crc32_short(sin6->sin6_addr.s6_addr, 16);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
940
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
941 /* lock addr mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
942
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
943 rn = ngx_resolver_lookup_addr6(r, &sin6->sin6_addr, hash);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
944
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
945 tree = &r->addr6_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
946 resend_queue = &r->addr6_resend_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
947 expire_queue = &r->addr6_expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
948
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
949 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
950 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
951
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
952 default: /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
953 sin = (struct sockaddr_in *) ctx->addr.sockaddr;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
954 addr = ntohl(sin->sin_addr.s_addr);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
955
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
956 /* lock addr mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
957
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
958 rn = ngx_resolver_lookup_addr(r, addr);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
959
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
960 tree = &r->addr_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
961 resend_queue = &r->addr_resend_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
962 expire_queue = &r->addr_expire_queue;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
963 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
964
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
965 if (rn) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
966
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
967 if (rn->valid >= ngx_time()) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
968
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
969 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolve cached");
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
970
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
971 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
972
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
973 rn->expire = ngx_time() + r->expire;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
974
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
975 ngx_queue_insert_head(expire_queue, &rn->queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
976
2484
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
977 name = ngx_resolver_dup(r, rn->name, rn->nlen);
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
978 if (name == NULL) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
979 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
980 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
981
2484
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
982 ctx->name.len = rn->nlen;
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
983 ctx->name.data = name;
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
984
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
985 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
986
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
987 ctx->state = NGX_OK;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
988 ctx->valid = rn->valid;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
989
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
990 ctx->handler(ctx);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
991
2484
cf3cd450049c store name pointer in variable allocated on stack
Igor Sysoev <igor@sysoev.ru>
parents: 2483
diff changeset
992 ngx_resolver_free(r, name);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
993
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
994 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
995 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
996
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
997 if (rn->waiting) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
998
6457
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
999 if (ctx->event == NULL && ctx->timeout) {
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1000 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1001 if (ctx->event == NULL) {
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1002 return NGX_ERROR;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1003 }
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1004
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1005 ctx->event->handler = ngx_resolver_timeout_handler;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1006 ctx->event->data = ctx;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1007 ctx->event->log = r->log;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1008 ctx->ident = -1;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1009
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1010 ngx_add_timer(ctx->event, ctx->timeout);
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
1011 }
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
1012
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1013 ctx->next = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1014 rn->waiting = ctx;
3297
ebc5384479b4 fix segfault in resolver:
Igor Sysoev <igor@sysoev.ru>
parents: 3269
diff changeset
1015 ctx->state = NGX_AGAIN;
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
1016 ctx->node = rn;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1017
2487
9b4dce95c744 fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents: 2486
diff changeset
1018 /* unlock addr mutex */
9b4dce95c744 fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents: 2486
diff changeset
1019
9b4dce95c744 fix return code, this fixes segfault when two or more
Igor Sysoev <igor@sysoev.ru>
parents: 2486
diff changeset
1020 return NGX_OK;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1021 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1022
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1023 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1024
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1025 ngx_resolver_free(r, rn->query);
1960
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1026 rn->query = NULL;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1027 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1028 rn->query6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1029 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1030
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1031 } else {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1032 rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1033 if (rn == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1034 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1035 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1036
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1037 switch (ctx->addr.sockaddr->sa_family) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1038
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1039 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1040 case AF_INET6:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1041 rn->addr6 = sin6->sin6_addr;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1042 rn->node.key = hash;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1043 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1044 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1045
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1046 default: /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1047 rn->node.key = addr;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1048 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1049
1960
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1050 rn->query = NULL;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1051 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1052 rn->query6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1053 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1054
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1055 ngx_rbtree_insert(tree, &rn->node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1056 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1057
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
1058 if (ngx_resolver_create_addr_query(r, rn, &ctx->addr) != NGX_OK) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1059 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1060 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1061
6366
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1062 rn->last_connection = r->last_connection++;
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1063 if (r->last_connection == r->connections.nelts) {
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1064 r->last_connection = 0;
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1065 }
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1066
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1067 rn->naddrs = (u_short) -1;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1068 rn->tcp = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1069 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1070 rn->naddrs6 = (u_short) -1;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1071 rn->tcp6 = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1072 #endif
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1073 rn->nsrvs = 0;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1074
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1075 if (ngx_resolver_send_query(r, rn) != NGX_OK) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1076 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1077 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1078
6457
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1079 if (ctx->event == NULL && ctx->timeout) {
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1080 ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1081 if (ctx->event == NULL) {
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1082 goto failed;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1083 }
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1084
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1085 ctx->event->handler = ngx_resolver_timeout_handler;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1086 ctx->event->data = ctx;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1087 ctx->event->log = r->log;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1088 ctx->ident = -1;
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1089
a6711b85da83 Resolver: do not enable resolve timer if provided timeout is zero.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6456
diff changeset
1090 ngx_add_timer(ctx->event, ctx->timeout);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1091 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1092
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1093 if (ngx_queue_empty(resend_queue)) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1094 ngx_add_timer(r->event, (ngx_msec_t) (r->resend_timeout * 1000));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1095 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1096
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1097 rn->expire = ngx_time() + r->resend_timeout;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1098
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1099 ngx_queue_insert_head(resend_queue, &rn->queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1100
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1101 rn->code = 0;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1102 rn->cnlen = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1103 rn->name = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1104 rn->nlen = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1105 rn->valid = 0;
5485
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
1106 rn->ttl = NGX_MAX_UINT32_VALUE;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1107 rn->waiting = ctx;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1108
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1109 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1110
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1111 ctx->state = NGX_AGAIN;
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
1112 ctx->node = rn;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1113
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1114 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1115
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1116 failed:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1117
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1118 if (rn) {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1119 ngx_rbtree_delete(tree, &rn->node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1120
1960
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1121 if (rn->query) {
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1122 ngx_resolver_free(r, rn->query);
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1123 }
1609b3c3d604 fix memory leak on resolver query send failure
Igor Sysoev <igor@sysoev.ru>
parents: 1914
diff changeset
1124
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1125 ngx_resolver_free(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1126 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1127
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1128 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1129
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1130 if (ctx->event) {
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1131 ngx_resolver_free(r, ctx->event);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1132 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1133
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1134 ngx_resolver_free(r, ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1135
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1136 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1137 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1138
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1139
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1140 void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1141 ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1142 {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1143 ngx_queue_t *expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1144 ngx_rbtree_t *tree;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1145 ngx_resolver_t *r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1146 ngx_resolver_ctx_t *w, **p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1147 ngx_resolver_node_t *rn;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
1148
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1149 r = ctx->resolver;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1150
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1151 switch (ctx->addr.sockaddr->sa_family) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1152
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1153 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1154 case AF_INET6:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1155 tree = &r->addr6_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1156 expire_queue = &r->addr6_expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1157 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1158 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1159
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1160 default: /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1161 tree = &r->addr_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1162 expire_queue = &r->addr_expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1163 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1164
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1165 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1166 "resolve addr done: %i", ctx->state);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1167
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1168 if (ctx->event && ctx->event->timer_set) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1169 ngx_del_timer(ctx->event);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1170 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1171
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1172 /* lock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1173
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
1174 if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1175
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
1176 rn = ctx->node;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1177
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1178 if (rn) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1179 p = &rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1180 w = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1181
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1182 while (w) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1183 if (w == ctx) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1184 *p = w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1185
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1186 goto done;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1187 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1188
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1189 p = &w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1190 w = w->next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1191 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1192 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1193
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1194 {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1195 u_char text[NGX_SOCKADDR_STRLEN];
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1196 ngx_str_t addrtext;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1197
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1198 addrtext.data = text;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1199 addrtext.len = ngx_sock_ntop(ctx->addr.sockaddr, ctx->addr.socklen,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1200 text, NGX_SOCKADDR_STRLEN, 0);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1201
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1202 ngx_log_error(NGX_LOG_ALERT, r->log, 0,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1203 "could not cancel %V resolving", &addrtext);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1204 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1205 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1206
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1207 done:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1208
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1209 ngx_resolver_expire(r, tree, expire_queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1210
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1211 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1212
1904
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1213 /* lock alloc mutex */
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1214
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1215 if (ctx->event) {
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1216 ngx_resolver_free_locked(r, ctx->event);
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1217 }
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1218
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1219 ngx_resolver_free_locked(r, ctx);
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1220
538f06aa8118 fix memory leaks, use unlocked ngx_resolver_free() for seldom failed cases
Igor Sysoev <igor@sysoev.ru>
parents: 1903
diff changeset
1221 /* unlock alloc mutex */
6196
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1222
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1223 if (r->event->timer_set && ngx_resolver_resend_empty(r)) {
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1224 ngx_del_timer(r->event);
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1225 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1226 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1227
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1228
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1229 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1230 ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1231 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1232 time_t now;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1233 ngx_uint_t i;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1234 ngx_queue_t *q;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1235 ngx_resolver_node_t *rn;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1236
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1237 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire");
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1238
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1239 now = ngx_time();
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1240
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1241 for (i = 0; i < 2; i++) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1242 if (ngx_queue_empty(queue)) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1243 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1244 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1245
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1246 q = ngx_queue_last(queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1247
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1248 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1249
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1250 if (now <= rn->expire) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1251 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1252 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1253
1774
68d21fd1dc64 use %*s format
Igor Sysoev <igor@sysoev.ru>
parents: 1742
diff changeset
1254 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
68d21fd1dc64 use %*s format
Igor Sysoev <igor@sysoev.ru>
parents: 1742
diff changeset
1255 "resolver expire \"%*s\"", (size_t) rn->nlen, rn->name);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1256
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1257 ngx_queue_remove(q);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1258
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1259 ngx_rbtree_delete(tree, &rn->node);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1260
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1261 ngx_resolver_free_node(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1262 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1263 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1264
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1265
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1266 static ngx_int_t
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1267 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1268 {
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1269 ngx_int_t rc;
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1270 ngx_resolver_connection_t *rec;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1271
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1272 rec = r->connections.elts;
6366
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1273 rec = &rec[rn->last_connection];
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1274
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1275 if (rec->log.handler == NULL) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1276 rec->log = *r->log;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1277 rec->log.handler = ngx_resolver_log_error;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1278 rec->log.data = rec;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
1279 rec->log.action = "resolving";
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1280 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1281
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1282 if (rn->naddrs == (u_short) -1) {
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1283 rc = rn->tcp ? ngx_resolver_send_tcp_query(r, rec, rn->query, rn->qlen)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1284 : ngx_resolver_send_udp_query(r, rec, rn->query, rn->qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1285
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1286 if (rc != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1287 return rc;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1288 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1289 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1290
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1291 #if (NGX_HAVE_INET6)
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1292
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1293 if (rn->query6 && rn->naddrs6 == (u_short) -1) {
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1294 rc = rn->tcp6
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1295 ? ngx_resolver_send_tcp_query(r, rec, rn->query6, rn->qlen)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1296 : ngx_resolver_send_udp_query(r, rec, rn->query6, rn->qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1297
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1298 if (rc != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1299 return rc;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1300 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1301 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1302
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1303 #endif
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1304
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1305 return NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1306 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1307
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1308
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1309 static ngx_int_t
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1310 ngx_resolver_send_udp_query(ngx_resolver_t *r, ngx_resolver_connection_t *rec,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1311 u_char *query, u_short qlen)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1312 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1313 ssize_t n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1314
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1315 if (rec->udp == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1316 if (ngx_udp_connect(rec) != NGX_OK) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1317 return NGX_ERROR;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1318 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1319
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1320 rec->udp->data = rec;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1321 rec->udp->read->handler = ngx_resolver_udp_read;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1322 rec->udp->read->resolver = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1323 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1324
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1325 n = ngx_send(rec->udp, query, qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1326
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1327 if (n == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1328 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1329 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1330
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1331 if ((size_t) n != (size_t) qlen) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1332 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "send() incomplete");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1333 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1334 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1335
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1336 return NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1337 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1338
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1339
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1340 static ngx_int_t
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1341 ngx_resolver_send_tcp_query(ngx_resolver_t *r, ngx_resolver_connection_t *rec,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1342 u_char *query, u_short qlen)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1343 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1344 ngx_buf_t *b;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1345 ngx_int_t rc;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1346
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1347 rc = NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1348
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1349 if (rec->tcp == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1350 b = rec->read_buf;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1351
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1352 if (b == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1353 b = ngx_resolver_calloc(r, sizeof(ngx_buf_t));
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1354 if (b == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1355 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1356 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1357
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1358 b->start = ngx_resolver_alloc(r, NGX_RESOLVER_TCP_RSIZE);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1359 if (b->start == NULL) {
6368
d73f77bb5caf Resolver: fixed possible resource leak introduced in 5a16d40c63de.
Ruslan Ermilov <ru@nginx.com>
parents: 6367
diff changeset
1360 ngx_resolver_free(r, b);
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1361 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1362 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1363
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1364 b->end = b->start + NGX_RESOLVER_TCP_RSIZE;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1365
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1366 rec->read_buf = b;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1367 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1368
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1369 b->pos = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1370 b->last = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1371
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1372 b = rec->write_buf;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1373
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1374 if (b == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1375 b = ngx_resolver_calloc(r, sizeof(ngx_buf_t));
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1376 if (b == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1377 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1378 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1379
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1380 b->start = ngx_resolver_alloc(r, NGX_RESOLVER_TCP_WSIZE);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1381 if (b->start == NULL) {
6368
d73f77bb5caf Resolver: fixed possible resource leak introduced in 5a16d40c63de.
Ruslan Ermilov <ru@nginx.com>
parents: 6367
diff changeset
1382 ngx_resolver_free(r, b);
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1383 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1384 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1385
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1386 b->end = b->start + NGX_RESOLVER_TCP_WSIZE;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1387
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1388 rec->write_buf = b;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1389 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1390
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1391 b->pos = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1392 b->last = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1393
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1394 rc = ngx_tcp_connect(rec);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1395 if (rc == NGX_ERROR) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1396 return NGX_ERROR;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1397 }
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1398
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1399 rec->tcp->data = rec;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1400 rec->tcp->write->handler = ngx_resolver_tcp_write;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1401 rec->tcp->read->handler = ngx_resolver_tcp_read;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1402 rec->tcp->read->resolver = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1403
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1404 ngx_add_timer(rec->tcp->write, (ngx_msec_t) (r->tcp_timeout * 1000));
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1405 }
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1406
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1407 b = rec->write_buf;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1408
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1409 if (b->end - b->last < 2 + qlen) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1410 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "buffer overflow");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1411 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1412 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1413
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1414 *b->last++ = (u_char) (qlen >> 8);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1415 *b->last++ = (u_char) qlen;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1416 b->last = ngx_cpymem(b->last, query, qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1417
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1418 if (rc == NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1419 ngx_resolver_tcp_write(rec->tcp->write);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1420 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1421
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1422 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1423 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1424
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1425
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1426 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1427 ngx_resolver_resend_handler(ngx_event_t *ev)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1428 {
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1429 time_t timer, atimer, stimer, ntimer;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1430 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1431 time_t a6timer;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1432 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1433 ngx_resolver_t *r;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1434
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1435 r = ev->data;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1436
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1437 ngx_log_debug0(NGX_LOG_DEBUG_CORE, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1438 "resolver resend handler");
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1439
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1440 /* lock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1441
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1442 ntimer = ngx_resolver_resend(r, &r->name_rbtree, &r->name_resend_queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1443
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1444 stimer = ngx_resolver_resend(r, &r->srv_rbtree, &r->srv_resend_queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1445
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1446 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1447
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1448 /* lock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1449
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1450 atimer = ngx_resolver_resend(r, &r->addr_rbtree, &r->addr_resend_queue);
1679
ca317d9b5c09 style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents: 1649
diff changeset
1451
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1452 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1453
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1454 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1455
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1456 /* lock addr6 mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1457
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1458 a6timer = ngx_resolver_resend(r, &r->addr6_rbtree, &r->addr6_resend_queue);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1459
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1460 /* unlock addr6 mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1461
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1462 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1463
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1464 timer = ntimer;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1465
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1466 if (timer == 0) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1467 timer = atimer;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1468
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1469 } else if (atimer) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1470 timer = ngx_min(timer, atimer);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1471 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1472
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1473 if (timer == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1474 timer = stimer;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1475
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1476 } else if (stimer) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1477 timer = ngx_min(timer, stimer);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1478 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1479
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1480 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1481
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1482 if (timer == 0) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1483 timer = a6timer;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1484
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1485 } else if (a6timer) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1486 timer = ngx_min(timer, a6timer);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1487 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1488
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1489 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
1490
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1491 if (timer) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1492 ngx_add_timer(r->event, (ngx_msec_t) (timer * 1000));
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1493 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1494 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1495
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1496
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1497 static time_t
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1498 ngx_resolver_resend(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1499 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1500 time_t now;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1501 ngx_queue_t *q;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1502 ngx_resolver_node_t *rn;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1503
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1504 now = ngx_time();
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1505
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1506 for ( ;; ) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1507 if (ngx_queue_empty(queue)) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1508 return 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1509 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1510
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1511 q = ngx_queue_last(queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1512
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1513 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1514
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1515 if (now < rn->expire) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1516 return rn->expire - now;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1517 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1518
1774
68d21fd1dc64 use %*s format
Igor Sysoev <igor@sysoev.ru>
parents: 1742
diff changeset
1519 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
68d21fd1dc64 use %*s format
Igor Sysoev <igor@sysoev.ru>
parents: 1742
diff changeset
1520 "resolver resend \"%*s\" %p",
68d21fd1dc64 use %*s format
Igor Sysoev <igor@sysoev.ru>
parents: 1742
diff changeset
1521 (size_t) rn->nlen, rn->name, rn->waiting);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1522
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1523 ngx_queue_remove(q);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1524
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1525 if (rn->waiting) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1526
6366
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1527 if (++rn->last_connection == r->connections.nelts) {
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1528 rn->last_connection = 0;
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1529 }
2e5c027f2a98 Resolver: per-request DNS server balancer.
Roman Arutyunyan <arut@nginx.com>
parents: 6365
diff changeset
1530
4683
84d8e60b65f0 Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents: 4671
diff changeset
1531 (void) ngx_resolver_send_query(r, rn);
84d8e60b65f0 Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents: 4671
diff changeset
1532
84d8e60b65f0 Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents: 4671
diff changeset
1533 rn->expire = now + r->resend_timeout;
84d8e60b65f0 Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents: 4671
diff changeset
1534
84d8e60b65f0 Fixed crash in ngx_resolver_cleanup_tree().
Ruslan Ermilov <ru@nginx.com>
parents: 4671
diff changeset
1535 ngx_queue_insert_head(queue, q);
1879
cf4ee321d195 do not delete failed DNS request if there are waiting clients
Igor Sysoev <igor@sysoev.ru>
parents: 1878
diff changeset
1536
cf4ee321d195 do not delete failed DNS request if there are waiting clients
Igor Sysoev <igor@sysoev.ru>
parents: 1878
diff changeset
1537 continue;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1538 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1539
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1540 ngx_rbtree_delete(tree, &rn->node);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1541
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1542 ngx_resolver_free_node(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1543 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1544 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1545
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1546
6196
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1547 static ngx_uint_t
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1548 ngx_resolver_resend_empty(ngx_resolver_t *r)
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1549 {
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1550 return ngx_queue_empty(&r->name_resend_queue)
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1551 #if (NGX_HAVE_INET6)
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1552 && ngx_queue_empty(&r->addr6_resend_queue)
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1553 #endif
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1554 && ngx_queue_empty(&r->addr_resend_queue);
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1555 }
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1556
c3ec43580a48 Resolver: canceled resend timer on empty resend queues.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6126
diff changeset
1557
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1558 static void
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1559 ngx_resolver_udp_read(ngx_event_t *rev)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1560 {
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1561 ssize_t n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1562 ngx_connection_t *c;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1563 ngx_resolver_connection_t *rec;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1564 u_char buf[NGX_RESOLVER_UDP_SIZE];
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1565
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1566 c = rev->data;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1567 rec = c->data;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1568
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1569 do {
1689
0b592a68aade ngx_udp_recv()
Igor Sysoev <igor@sysoev.ru>
parents: 1687
diff changeset
1570 n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE);
0b592a68aade ngx_udp_recv()
Igor Sysoev <igor@sysoev.ru>
parents: 1687
diff changeset
1571
0b592a68aade ngx_udp_recv()
Igor Sysoev <igor@sysoev.ru>
parents: 1687
diff changeset
1572 if (n < 0) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1573 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1574 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1575
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1576 ngx_resolver_process_response(rec->resolver, buf, n, 0);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1577
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1578 } while (rev->ready);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1579 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1580
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1581
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1582 static void
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1583 ngx_resolver_tcp_write(ngx_event_t *wev)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1584 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1585 off_t sent;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1586 ssize_t n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1587 ngx_buf_t *b;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1588 ngx_resolver_t *r;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1589 ngx_connection_t *c;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1590 ngx_resolver_connection_t *rec;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1591
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1592 c = wev->data;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1593 rec = c->data;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1594 b = rec->write_buf;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1595 r = rec->resolver;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1596
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1597 if (wev->timedout) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1598 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1599 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1600
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1601 sent = c->sent;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1602
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1603 while (wev->ready && b->pos < b->last) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1604 n = ngx_send(c, b->pos, b->last - b->pos);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1605
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1606 if (n == NGX_AGAIN) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1607 break;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1608 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1609
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1610 if (n == NGX_ERROR) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1611 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1612 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1613
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1614 b->pos += n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1615 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1616
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1617 if (b->pos != b->start) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1618 b->last = ngx_movemem(b->start, b->pos, b->last - b->pos);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1619 b->pos = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1620 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1621
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1622 if (c->sent != sent) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1623 ngx_add_timer(wev, (ngx_msec_t) (r->tcp_timeout * 1000));
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1624 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1625
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1626 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1627 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1628 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1629
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1630 return;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1631
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1632 failed:
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1633
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1634 ngx_close_connection(c);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1635 rec->tcp = NULL;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1636 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1637
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1638
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1639 static void
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1640 ngx_resolver_tcp_read(ngx_event_t *rev)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1641 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1642 u_char *p;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1643 size_t size;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1644 ssize_t n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1645 u_short qlen;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1646 ngx_buf_t *b;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1647 ngx_resolver_t *r;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1648 ngx_connection_t *c;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1649 ngx_resolver_connection_t *rec;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1650
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1651 c = rev->data;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1652 rec = c->data;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1653 b = rec->read_buf;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1654 r = rec->resolver;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1655
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1656 while (rev->ready) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1657 n = ngx_recv(c, b->last, b->end - b->last);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1658
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1659 if (n == NGX_AGAIN) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1660 break;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1661 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1662
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1663 if (n == NGX_ERROR || n == 0) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1664 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1665 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1666
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1667 b->last += n;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1668
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1669 for ( ;; ) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1670 p = b->pos;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1671 size = b->last - p;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1672
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1673 if (size < 2) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1674 break;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1675 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1676
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1677 qlen = (u_short) *p++ << 8;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1678 qlen += *p++;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1679
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1680 if (size < (size_t) (2 + qlen)) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1681 break;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1682 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1683
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1684 ngx_resolver_process_response(r, p, qlen, 1);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1685
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1686 b->pos += 2 + qlen;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1687 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1688
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1689 if (b->pos != b->start) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1690 b->last = ngx_movemem(b->start, b->pos, b->last - b->pos);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1691 b->pos = b->start;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1692 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1693 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1694
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1695 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1696 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1697 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1698
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1699 return;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1700
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1701 failed:
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1702
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1703 ngx_close_connection(c);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1704 rec->tcp = NULL;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1705 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1706
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1707
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1708 static void
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1709 ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1710 ngx_uint_t tcp)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1711 {
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1712 char *err;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1713 ngx_uint_t i, times, ident, qident, flags, code, nqs, nan, trunc,
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1714 qtype, qclass;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1715 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1716 ngx_uint_t qident6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1717 #endif
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1718 ngx_queue_t *q;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1719 ngx_resolver_qs_t *qs;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1720 ngx_resolver_hdr_t *response;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1721 ngx_resolver_node_t *rn;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1722
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1723 if (n < sizeof(ngx_resolver_hdr_t)) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1724 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1725 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1726
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1727 response = (ngx_resolver_hdr_t *) buf;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1728
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1729 ident = (response->ident_hi << 8) + response->ident_lo;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1730 flags = (response->flags_hi << 8) + response->flags_lo;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1731 nqs = (response->nqs_hi << 8) + response->nqs_lo;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1732 nan = (response->nan_hi << 8) + response->nan_lo;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1733 trunc = flags & 0x0200;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1734
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1735 ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0,
6509
7640d6c213e1 Removed redundant "u" format specifier.
Ruslan Ermilov <ru@nginx.com>
parents: 6478
diff changeset
1736 "resolver DNS response %ui fl:%04Xi %ui/%ui/%ud/%ud",
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1737 ident, flags, nqs, nan,
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1738 (response->nns_hi << 8) + response->nns_lo,
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1739 (response->nar_hi << 8) + response->nar_lo);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1740
5470
aebdca7e8f8f Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5469
diff changeset
1741 /* response to a standard query */
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1742 if ((flags & 0xf870) != 0x8000 || (trunc && tcp)) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1743 ngx_log_error(r->log_level, r->log, 0,
6509
7640d6c213e1 Removed redundant "u" format specifier.
Ruslan Ermilov <ru@nginx.com>
parents: 6478
diff changeset
1744 "invalid %s DNS response %ui fl:%04Xi",
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1745 tcp ? "TCP" : "UDP", ident, flags);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1746 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1747 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1748
5470
aebdca7e8f8f Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5469
diff changeset
1749 code = flags & 0xf;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1750
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1751 if (code == NGX_RESOLVE_FORMERR) {
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1752
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1753 times = 0;
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1754
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1755 for (q = ngx_queue_head(&r->name_resend_queue);
6347
81d44cd4044e Resolver: fixed possible segmentation fault on DNS format error.
Roman Arutyunyan <arut@nginx.com>
parents: 6196
diff changeset
1756 q != ngx_queue_sentinel(&r->name_resend_queue) && times++ < 100;
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1757 q = ngx_queue_next(q))
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1758 {
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1759 rn = ngx_queue_data(q, ngx_resolver_node_t, queue);
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1760 qident = (rn->query[0] << 8) + rn->query[1];
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1761
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1762 if (qident == ident) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1763 goto dns_error_name;
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1764 }
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1765
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1766 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1767 if (rn->query6) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1768 qident6 = (rn->query6[0] << 8) + rn->query6[1];
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1769
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1770 if (qident6 == ident) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1771 goto dns_error_name;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1772 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1773 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1774 #endif
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1775 }
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1776
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1777 goto dns_error;
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1778 }
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1779
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1780 if (code > NGX_RESOLVE_REFUSED) {
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1781 goto dns_error;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1782 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1783
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1784 if (nqs != 1) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1785 err = "invalid number of questions in DNS response";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1786 goto done;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1787 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1788
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1789 i = sizeof(ngx_resolver_hdr_t);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1790
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1791 while (i < (ngx_uint_t) n) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1792 if (buf[i] == '\0') {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1793 goto found;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1794 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1795
5470
aebdca7e8f8f Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5469
diff changeset
1796 i += 1 + buf[i];
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1797 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1798
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1799 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1800
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1801 found:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1802
5470
aebdca7e8f8f Resolver: fixed response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5469
diff changeset
1803 if (i++ == sizeof(ngx_resolver_hdr_t)) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1804 err = "zero-length domain name in DNS response";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1805 goto done;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1806 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1807
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1808 if (i + sizeof(ngx_resolver_qs_t) + nan * (2 + sizeof(ngx_resolver_an_t))
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1809 > (ngx_uint_t) n)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1810 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1811 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1812 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1813
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1814 qs = (ngx_resolver_qs_t *) &buf[i];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1815
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1816 qtype = (qs->type_hi << 8) + qs->type_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1817 qclass = (qs->class_hi << 8) + qs->class_lo;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1818
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1819 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
1967
4a4b15c9c474 use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents: 1966
diff changeset
1820 "resolver DNS response qt:%ui cl:%ui", qtype, qclass);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1821
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1822 if (qclass != 1) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1823 ngx_log_error(r->log_level, r->log, 0,
1967
4a4b15c9c474 use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents: 1966
diff changeset
1824 "unknown query class %ui in DNS response", qclass);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1825 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1826 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1827
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1828 switch (qtype) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1829
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1830 case NGX_RESOLVE_A:
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1831 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1832 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1833 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1834
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1835 ngx_resolver_process_a(r, buf, n, ident, code, qtype, nan, trunc,
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
1836 i + sizeof(ngx_resolver_qs_t));
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1837
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1838 break;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1839
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1840 case NGX_RESOLVE_SRV:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1841
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1842 ngx_resolver_process_srv(r, buf, n, ident, code, nan, trunc,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1843 i + sizeof(ngx_resolver_qs_t));
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1844
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1845 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1846
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1847 case NGX_RESOLVE_PTR:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1848
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
1849 ngx_resolver_process_ptr(r, buf, n, ident, code, nan);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1850
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1851 break;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1852
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1853 default:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1854 ngx_log_error(r->log_level, r->log, 0,
1967
4a4b15c9c474 use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents: 1966
diff changeset
1855 "unknown query type %ui in DNS response", qtype);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1856 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1857 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1858
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1859 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1860
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1861 short_response:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1862
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1863 err = "short DNS response";
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1864
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1865 done:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1866
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1867 ngx_log_error(r->log_level, r->log, 0, err);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1868
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1869 return;
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1870
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1871 dns_error_name:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1872
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1873 ngx_log_error(r->log_level, r->log, 0,
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1874 "DNS error (%ui: %s), query id:%ui, name:\"%*s\"",
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1875 code, ngx_resolver_strerror(code), ident,
6478
3ef7bb882ad4 Fixed logging with variable field width.
Sergey Kandaurov <pluknet@nginx.com>
parents: 6460
diff changeset
1876 (size_t) rn->nlen, rn->name);
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1877 return;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1878
2282
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1879 dns_error:
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1880
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1881 ngx_log_error(r->log_level, r->log, 0,
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1882 "DNS error (%ui: %s), query id:%ui",
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1883 code, ngx_resolver_strerror(code), ident);
ec97eb9a8038 show name for a FORMERR DNS response
Igor Sysoev <igor@sysoev.ru>
parents: 2281
diff changeset
1884 return;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1885 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1886
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1887
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1888 static void
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
1889 ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t n,
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1890 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t qtype,
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1891 ngx_uint_t nan, ngx_uint_t trunc, ngx_uint_t ans)
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1892 {
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1893 char *err;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1894 u_char *cname;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1895 size_t len;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1896 int32_t ttl;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1897 uint32_t hash;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1898 in_addr_t *addr;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1899 ngx_str_t name;
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
1900 ngx_uint_t type, class, qident, naddrs, a, i, j, start;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1901 #if (NGX_HAVE_INET6)
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1902 struct in6_addr *addr6;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1903 #endif
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1904 ngx_resolver_an_t *an;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1905 ngx_resolver_ctx_t *ctx, *next;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1906 ngx_resolver_node_t *rn;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
1907 ngx_resolver_addr_t *addrs;
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1908 ngx_resolver_connection_t *rec;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1909
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1910 if (ngx_resolver_copy(r, &name, buf,
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
1911 buf + sizeof(ngx_resolver_hdr_t), buf + n)
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1912 != NGX_OK)
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
1913 {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1914 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1915 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1916
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1917 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1918
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1919 hash = ngx_crc32_short(name.data, name.len);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1920
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1921 /* lock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1922
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1923 rn = ngx_resolver_lookup_name(r, &name, hash);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1924
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1925 if (rn == NULL) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1926 ngx_log_error(r->log_level, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1927 "unexpected response for %V", &name);
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
1928 ngx_resolver_free(r, name.data);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1929 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1930 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1931
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1932 switch (qtype) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1933
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1934 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1935 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1936
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1937 if (rn->query6 == NULL || rn->naddrs6 != (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1938 ngx_log_error(r->log_level, r->log, 0,
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1939 "unexpected response for %V", &name);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1940 ngx_resolver_free(r, name.data);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1941 goto failed;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1942 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1943
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1944 if (trunc && rn->tcp6) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1945 ngx_resolver_free(r, name.data);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1946 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1947 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1948
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1949 qident = (rn->query6[0] << 8) + rn->query6[1];
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1950
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1951 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1952 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1953
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1954 default: /* NGX_RESOLVE_A */
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1955
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1956 if (rn->query == NULL || rn->naddrs != (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1957 ngx_log_error(r->log_level, r->log, 0,
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1958 "unexpected response for %V", &name);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1959 ngx_resolver_free(r, name.data);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1960 goto failed;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1961 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1962
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1963 if (trunc && rn->tcp) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1964 ngx_resolver_free(r, name.data);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1965 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1966 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1967
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1968 qident = (rn->query[0] << 8) + rn->query[1];
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
1969 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1970
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1971 if (ident != qident) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1972 ngx_log_error(r->log_level, r->log, 0,
1967
4a4b15c9c474 use correct ngx_sprintf() formats
Igor Sysoev <igor@sysoev.ru>
parents: 1966
diff changeset
1973 "wrong ident %ui response for %V, expect %ui",
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1974 ident, &name, qident);
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
1975 ngx_resolver_free(r, name.data);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1976 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1977 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
1978
3139
5f6144fc674d fix memory leak in resolver
Igor Sysoev <igor@sysoev.ru>
parents: 2785
diff changeset
1979 ngx_resolver_free(r, name.data);
5f6144fc674d fix memory leak in resolver
Igor Sysoev <igor@sysoev.ru>
parents: 2785
diff changeset
1980
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1981 if (trunc) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1982
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1983 ngx_queue_remove(&rn->queue);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1984
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1985 if (rn->waiting == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1986 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1987 ngx_resolver_free_node(r, rn);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1988 goto next;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1989 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1990
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1991 rec = r->connections.elts;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1992 rec = &rec[rn->last_connection];
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1993
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1994 switch (qtype) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1995
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1996 #if (NGX_HAVE_INET6)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1997 case NGX_RESOLVE_AAAA:
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1998
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
1999 rn->tcp6 = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2000
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2001 (void) ngx_resolver_send_tcp_query(r, rec, rn->query6, rn->qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2002
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2003 break;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2004 #endif
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2005
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2006 default: /* NGX_RESOLVE_A */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2007
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2008 rn->tcp = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2009
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2010 (void) ngx_resolver_send_tcp_query(r, rec, rn->query, rn->qlen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2011 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2012
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2013 rn->expire = ngx_time() + r->resend_timeout;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2014
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2015 ngx_queue_insert_head(&r->name_resend_queue, &rn->queue);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2016
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2017 goto next;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2018 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
2019
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2020 if (code == 0 && rn->code) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2021 code = rn->code;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2022 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2023
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
2024 if (code == 0 && nan == 0) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2025
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2026 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2027 switch (qtype) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2028
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2029 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2030
5768
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2031 rn->naddrs6 = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2032
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2033 if (rn->naddrs == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2034 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2035 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2036
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2037 if (rn->naddrs) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2038 goto export;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2039 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2040
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2041 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2042
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2043 default: /* NGX_RESOLVE_A */
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2044
5768
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2045 rn->naddrs = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2046
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2047 if (rn->naddrs6 == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2048 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2049 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2050
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2051 if (rn->naddrs6) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2052 goto export;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2053 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2054 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2055 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2056
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2057 code = NGX_RESOLVE_NXDOMAIN;
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
2058 }
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
2059
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2060 if (code) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2061
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2062 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2063 switch (qtype) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2064
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2065 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2066
5768
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2067 rn->naddrs6 = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2068
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2069 if (rn->naddrs == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2070 rn->code = (u_char) code;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2071 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2072 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2073
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2074 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2075
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2076 default: /* NGX_RESOLVE_A */
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2077
5768
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2078 rn->naddrs = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2079
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2080 if (rn->naddrs6 == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2081 rn->code = (u_char) code;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2082 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2083 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2084 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2085 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2086
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2087 next = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2088 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2089
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2090 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2091
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2092 ngx_rbtree_delete(&r->name_rbtree, &rn->node);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2093
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2094 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2095
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2096 while (next) {
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2097 ctx = next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2098 ctx->state = code;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
2099 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2100 next = ctx->next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2101
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2102 ctx->handler(ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2103 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2104
5920
7420068c4d4b Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents: 5820
diff changeset
2105 ngx_resolver_free_node(r, rn);
7420068c4d4b Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents: 5820
diff changeset
2106
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2107 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2108 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2109
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2110 i = ans;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2111 naddrs = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2112 cname = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2113
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2114 for (a = 0; a < nan; a++) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2115
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2116 start = i;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2117
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2118 while (i < n) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2119
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2120 if (buf[i] & 0xc0) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2121 i += 2;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2122 goto found;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2123 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2124
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2125 if (buf[i] == 0) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2126 i++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2127 goto test_length;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2128 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2129
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2130 i += 1 + buf[i];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2131 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2132
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2133 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2134
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2135 test_length:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2136
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2137 if (i - start < 2) {
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2138 err = "invalid name in DNS response";
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2139 goto invalid;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2140 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2141
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2142 found:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2143
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2144 if (i + sizeof(ngx_resolver_an_t) >= n) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2145 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2146 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2147
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2148 an = (ngx_resolver_an_t *) &buf[i];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2149
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2150 type = (an->type_hi << 8) + an->type_lo;
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2151 class = (an->class_hi << 8) + an->class_lo;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2152 len = (an->len_hi << 8) + an->len_lo;
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2153 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2154 + (an->ttl[2] << 8) + (an->ttl[3]);
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2155
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2156 if (class != 1) {
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2157 ngx_log_error(r->log_level, r->log, 0,
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2158 "unexpected RR class %ui", class);
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2159 goto failed;
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2160 }
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2161
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2162 if (ttl < 0) {
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2163 ttl = 0;
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2164 }
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2165
5485
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
2166 rn->ttl = ngx_min(rn->ttl, (uint32_t) ttl);
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
2167
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2168 i += sizeof(ngx_resolver_an_t);
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2169
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2170 switch (type) {
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2171
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2172 case NGX_RESOLVE_A:
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2173
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2174 if (qtype != NGX_RESOLVE_A) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2175 err = "unexpected A record in DNS response";
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2176 goto invalid;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2177 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2178
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2179 if (len != 4) {
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2180 err = "invalid A record in DNS response";
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2181 goto invalid;
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2182 }
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2183
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2184 if (i + 4 > n) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2185 goto short_response;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2186 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2187
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2188 naddrs++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2189
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2190 break;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2191
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2192 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2193 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2194
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2195 if (qtype != NGX_RESOLVE_AAAA) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2196 err = "unexpected AAAA record in DNS response";
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2197 goto invalid;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2198 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2199
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2200 if (len != 16) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2201 err = "invalid AAAA record in DNS response";
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2202 goto invalid;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2203 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2204
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2205 if (i + 16 > n) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2206 goto short_response;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2207 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2208
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2209 naddrs++;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2210
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2211 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2212 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2213
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2214 case NGX_RESOLVE_CNAME:
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2215
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2216 cname = &buf[i];
1965
14463ae77723 take into account DNAME
Igor Sysoev <igor@sysoev.ru>
parents: 1961
diff changeset
2217
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2218 break;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2219
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2220 case NGX_RESOLVE_DNAME:
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2221
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2222 break;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2223
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2224 default:
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2225
1966
fb02f63eafd6 log unexpected qtype
Igor Sysoev <igor@sysoev.ru>
parents: 1965
diff changeset
2226 ngx_log_error(r->log_level, r->log, 0,
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2227 "unexpected RR type %ui", type);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2228 }
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2229
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2230 i += len;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2231 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2232
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2233 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
5485
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
2234 "resolver naddrs:%ui cname:%p ttl:%uD",
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
2235 naddrs, cname, rn->ttl);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2236
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2237 if (naddrs) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2238
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2239 switch (qtype) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2240
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2241 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2242 case NGX_RESOLVE_AAAA:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2243
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2244 if (naddrs == 1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2245 addr6 = &rn->u6.addr6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2246 rn->naddrs6 = 1;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2247
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2248 } else {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2249 addr6 = ngx_resolver_alloc(r, naddrs * sizeof(struct in6_addr));
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2250 if (addr6 == NULL) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2251 goto failed;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2252 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2253
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2254 rn->u6.addrs6 = addr6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2255 rn->naddrs6 = (u_short) naddrs;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2256 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2257
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2258 #if (NGX_SUPPRESS_WARN)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2259 addr = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2260 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2261
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2262 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2263 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2264
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2265 default: /* NGX_RESOLVE_A */
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2266
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2267 if (naddrs == 1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2268 addr = &rn->u.addr;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2269 rn->naddrs = 1;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2270
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2271 } else {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2272 addr = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2273 if (addr == NULL) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2274 goto failed;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2275 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2276
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2277 rn->u.addrs = addr;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2278 rn->naddrs = (u_short) naddrs;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2279 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2280
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2281 #if (NGX_HAVE_INET6 && NGX_SUPPRESS_WARN)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2282 addr6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2283 #endif
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2284 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2285
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2286 j = 0;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2287 i = ans;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2288
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2289 for (a = 0; a < nan; a++) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2290
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2291 for ( ;; ) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2292
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2293 if (buf[i] & 0xc0) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2294 i += 2;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2295 break;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2296 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2297
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2298 if (buf[i] == 0) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2299 i++;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2300 break;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2301 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2302
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2303 i += 1 + buf[i];
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2304 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2305
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2306 an = (ngx_resolver_an_t *) &buf[i];
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2307
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2308 type = (an->type_hi << 8) + an->type_lo;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2309 len = (an->len_hi << 8) + an->len_lo;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2310
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2311 i += sizeof(ngx_resolver_an_t);
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2312
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2313 if (type == NGX_RESOLVE_A) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2314
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2315 addr[j] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2316 + (buf[i + 2] << 8) + (buf[i + 3]));
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2317
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2318 if (++j == naddrs) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2319
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2320 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2321 if (rn->naddrs6 == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2322 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2323 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2324 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2325
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2326 break;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2327 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2328 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2329
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2330 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2331 else if (type == NGX_RESOLVE_AAAA) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2332
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2333 ngx_memcpy(addr6[j].s6_addr, &buf[i], 16);
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2334
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2335 if (++j == naddrs) {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2336
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2337 if (rn->naddrs == (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2338 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2339 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2340
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2341 break;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2342 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2343 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2344 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2345
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2346 i += len;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2347 }
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2348 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2349
5768
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2350 switch (qtype) {
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2351
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2352 #if (NGX_HAVE_INET6)
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2353 case NGX_RESOLVE_AAAA:
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2354
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2355 if (rn->naddrs6 == (u_short) -1) {
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2356 rn->naddrs6 = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2357 }
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2358
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2359 break;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2360 #endif
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2361
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2362 default: /* NGX_RESOLVE_A */
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2363
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2364 if (rn->naddrs == (u_short) -1) {
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2365 rn->naddrs = 0;
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2366 }
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2367 }
70a0408cca7b Resolver: fixed resend on malformed responses.
Ruslan Ermilov <ru@nginx.com>
parents: 5764
diff changeset
2368
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2369 if (rn->naddrs != (u_short) -1
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2370 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2371 && rn->naddrs6 != (u_short) -1
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2372 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2373 && rn->naddrs
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2374 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2375 + rn->naddrs6
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2376 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2377 > 0)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2378 {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2379
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2380 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2381 export:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2382 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2383
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2384 naddrs = rn->naddrs;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2385 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2386 naddrs += rn->naddrs6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2387 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2388
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2389 if (naddrs == 1 && rn->naddrs == 1) {
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2390 addrs = NULL;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2391
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2392 } else {
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2393 addrs = ngx_resolver_export(r, rn, 0);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2394 if (addrs == NULL) {
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2395 goto failed;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2396 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2397 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2398
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2399 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2400
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2401 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2402 rn->expire = ngx_time() + r->expire;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2403
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2404 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2405
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2406 next = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2407 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2408
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2409 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2410
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2411 while (next) {
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2412 ctx = next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2413 ctx->state = NGX_OK;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
2414 ctx->valid = rn->valid;
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2415 ctx->naddrs = naddrs;
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2416
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2417 if (addrs == NULL) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2418 ctx->addrs = &ctx->addr;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2419 ctx->addr.sockaddr = (struct sockaddr *) &ctx->sin;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2420 ctx->addr.socklen = sizeof(struct sockaddr_in);
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2421 ngx_memzero(&ctx->sin, sizeof(struct sockaddr_in));
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2422 ctx->sin.sin_family = AF_INET;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2423 ctx->sin.sin_addr.s_addr = rn->u.addr;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2424
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2425 } else {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2426 ctx->addrs = addrs;
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2427 }
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2428
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2429 next = ctx->next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2430
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
2431 ctx->handler(ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2432 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2433
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2434 if (addrs != NULL) {
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
2435 ngx_resolver_free(r, addrs->sockaddr);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2436 ngx_resolver_free(r, addrs);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2437 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2438
4619
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
2439 ngx_resolver_free(r, rn->query);
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
2440 rn->query = NULL;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2441 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2442 rn->query6 = NULL;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2443 #endif
4619
3171ec7d0d05 Resolver: protection from duplicate responses.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4610
diff changeset
2444
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2445 return;
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2446 }
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2447
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2448 if (cname) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2449
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2450 /* CNAME only */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2451
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2452 if (rn->naddrs == (u_short) -1
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2453 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2454 || rn->naddrs6 == (u_short) -1
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2455 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2456 )
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2457 {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2458 goto next;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2459 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2460
6370
8c8739bdd3f1 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6369
diff changeset
2461 if (ngx_resolver_copy(r, &name, buf, cname, buf + n) != NGX_OK) {
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2462 goto failed;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2463 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2464
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2465 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2466 "resolver cname:\"%V\"", &name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2467
1741
0829024c924d fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents: 1689
diff changeset
2468 ngx_queue_remove(&rn->queue);
0829024c924d fix segfault if response will have CNAME only
Igor Sysoev <igor@sysoev.ru>
parents: 1689
diff changeset
2469
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2470 rn->cnlen = (u_short) name.len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2471 rn->u.cname = name.data;
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
2472
5485
8958656a8060 Resolver: use minimum TTL for caching (ticket #329).
Ruslan Ermilov <ru@nginx.com>
parents: 5479
diff changeset
2473 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2474 rn->expire = ngx_time() + r->expire;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2475
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2476 ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2477
6352
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2478 ngx_resolver_free(r, rn->query);
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2479 rn->query = NULL;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2480 #if (NGX_HAVE_INET6)
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2481 rn->query6 = NULL;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2482 #endif
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2483
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2484 ctx = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2485 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2486
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2487 if (ctx) {
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2488
6352
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2489 if (ctx->recursion++ >= NGX_RESOLVER_MAX_RECURSION) {
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2490
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2491 /* unlock name mutex */
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2492
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2493 do {
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2494 ctx->state = NGX_RESOLVE_NXDOMAIN;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2495 next = ctx->next;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2496
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2497 ctx->handler(ctx);
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2498
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2499 ctx = next;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2500 } while (ctx);
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2501
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2502 return;
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2503 }
ff9b32c0e141 Resolver: limited CNAME recursion.
Ruslan Ermilov <ru@nginx.com>
parents: 6351
diff changeset
2504
6351
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2505 for (next = ctx; next; next = next->next) {
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2506 next->node = NULL;
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2507 }
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2508
497d0cff8ace Resolver: fixed use-after-free memory accesses with CNAME.
Roman Arutyunyan <arut@nginx.com>
parents: 6350
diff changeset
2509 (void) ngx_resolve_name_locked(r, ctx, &name);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2510 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2511
5471
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2512 /* unlock name mutex */
9c96782d9d05 Resolver: fixes in A processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5470
diff changeset
2513
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2514 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2515 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2516
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2517 ngx_log_error(r->log_level, r->log, 0,
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2518 "no A or CNAME types in DNS response");
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2519 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2520
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2521 short_response:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2522
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
2523 err = "short DNS response";
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2524
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2525 invalid:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2526
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2527 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2528
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2529 ngx_log_error(r->log_level, r->log, 0, err);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2530
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2531 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2532
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2533 failed:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2534
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2535 next:
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
2536
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2537 /* unlock name mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2538
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2539 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2540 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2541
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2542
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
2543 static void
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2544 ngx_resolver_process_srv(ngx_resolver_t *r, u_char *buf, size_t n,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2545 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2546 ngx_uint_t trunc, ngx_uint_t ans)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2547 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2548 char *err;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2549 u_char *cname;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2550 size_t len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2551 int32_t ttl;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2552 uint32_t hash;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2553 ngx_str_t name;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2554 ngx_uint_t type, qident, class, start, nsrvs, a, i, j;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2555 ngx_resolver_an_t *an;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2556 ngx_resolver_ctx_t *ctx, *next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2557 ngx_resolver_srv_t *srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2558 ngx_resolver_node_t *rn;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2559 ngx_resolver_connection_t *rec;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2560
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2561 if (ngx_resolver_copy(r, &name, buf,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2562 buf + sizeof(ngx_resolver_hdr_t), buf + n)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2563 != NGX_OK)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2564 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2565 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2566 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2567
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2568 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2569
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2570 hash = ngx_crc32_short(name.data, name.len);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2571
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2572 rn = ngx_resolver_lookup_srv(r, &name, hash);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2573
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2574 if (rn == NULL || rn->query == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2575 ngx_log_error(r->log_level, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2576 "unexpected response for %V", &name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2577 ngx_resolver_free(r, name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2578 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2579 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2580
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2581 if (trunc && rn->tcp) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2582 ngx_resolver_free(r, name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2583 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2584 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2585
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2586 qident = (rn->query[0] << 8) + rn->query[1];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2587
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2588 if (ident != qident) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2589 ngx_log_error(r->log_level, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2590 "wrong ident %ui response for %V, expect %ui",
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2591 ident, &name, qident);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2592 ngx_resolver_free(r, name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2593 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2594 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2595
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2596 ngx_resolver_free(r, name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2597
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2598 if (trunc) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2599
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2600 ngx_queue_remove(&rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2601
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2602 if (rn->waiting == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2603 ngx_rbtree_delete(&r->srv_rbtree, &rn->node);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2604 ngx_resolver_free_node(r, rn);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2605 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2606 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2607
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2608 rec = r->connections.elts;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2609 rec = &rec[rn->last_connection];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2610
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2611 rn->tcp = 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2612
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2613 (void) ngx_resolver_send_tcp_query(r, rec, rn->query, rn->qlen);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2614
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2615 rn->expire = ngx_time() + r->resend_timeout;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2616
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2617 ngx_queue_insert_head(&r->srv_resend_queue, &rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2618
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2619 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2620 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2621
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2622 if (code == 0 && rn->code) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2623 code = rn->code;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2624 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2625
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2626 if (code == 0 && nan == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2627 code = NGX_RESOLVE_NXDOMAIN;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2628 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2629
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2630 if (code) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2631 next = rn->waiting;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2632 rn->waiting = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2633
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2634 ngx_queue_remove(&rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2635
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2636 ngx_rbtree_delete(&r->srv_rbtree, &rn->node);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2637
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2638 while (next) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2639 ctx = next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2640 ctx->state = code;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2641 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2642 next = ctx->next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2643
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2644 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2645 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2646
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2647 ngx_resolver_free_node(r, rn);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2648
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2649 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2650 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2651
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2652 i = ans;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2653 nsrvs = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2654 cname = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2655
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2656 for (a = 0; a < nan; a++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2657
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2658 start = i;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2659
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2660 while (i < n) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2661
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2662 if (buf[i] & 0xc0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2663 i += 2;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2664 goto found;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2665 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2666
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2667 if (buf[i] == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2668 i++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2669 goto test_length;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2670 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2671
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2672 i += 1 + buf[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2673 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2674
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2675 goto short_response;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2676
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2677 test_length:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2678
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2679 if (i - start < 2) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2680 err = "invalid name DNS response";
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2681 goto invalid;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2682 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2683
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2684 found:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2685
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2686 if (i + sizeof(ngx_resolver_an_t) >= n) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2687 goto short_response;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2688 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2689
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2690 an = (ngx_resolver_an_t *) &buf[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2691
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2692 type = (an->type_hi << 8) + an->type_lo;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2693 class = (an->class_hi << 8) + an->class_lo;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2694 len = (an->len_hi << 8) + an->len_lo;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2695 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2696 + (an->ttl[2] << 8) + (an->ttl[3]);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2697
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2698 if (class != 1) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2699 ngx_log_error(r->log_level, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2700 "unexpected RR class %ui", class);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2701 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2702 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2703
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2704 if (ttl < 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2705 ttl = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2706 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2707
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2708 rn->ttl = ngx_min(rn->ttl, (uint32_t) ttl);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2709
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2710 i += sizeof(ngx_resolver_an_t);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2711
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2712 switch (type) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2713
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2714 case NGX_RESOLVE_SRV:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2715
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2716 if (i + 6 > n) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2717 goto short_response;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2718 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2719
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2720 if (ngx_resolver_copy(r, NULL, buf, &buf[i + 6], buf + n)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2721 != NGX_OK)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2722 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2723 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2724 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2725
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2726 nsrvs++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2727
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2728 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2729
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2730 case NGX_RESOLVE_CNAME:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2731
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2732 cname = &buf[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2733
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2734 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2735
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2736 case NGX_RESOLVE_DNAME:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2737
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2738 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2739
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2740 default:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2741
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2742 ngx_log_error(r->log_level, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2743 "unexpected RR type %ui", type);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2744 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2745
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2746 i += len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2747 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2748
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2749 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2750 "resolver nsrvs:%ui cname:%p ttl:%uD",
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2751 nsrvs, cname, rn->ttl);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2752
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2753 if (nsrvs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2754
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2755 srvs = ngx_resolver_calloc(r, nsrvs * sizeof(ngx_resolver_srv_t));
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2756 if (srvs == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2757 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2758 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2759
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2760 rn->u.srvs = srvs;
6460
034329824dd3 Win32: fixed build after 384154fc634f.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6458
diff changeset
2761 rn->nsrvs = (u_short) nsrvs;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2762
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2763 j = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2764 i = ans;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2765
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2766 for (a = 0; a < nan; a++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2767
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2768 for ( ;; ) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2769
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2770 if (buf[i] & 0xc0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2771 i += 2;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2772 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2773 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2774
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2775 if (buf[i] == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2776 i++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2777 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2778 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2779
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2780 i += 1 + buf[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2781 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2782
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2783 an = (ngx_resolver_an_t *) &buf[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2784
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2785 type = (an->type_hi << 8) + an->type_lo;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2786 len = (an->len_hi << 8) + an->len_lo;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2787
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2788 i += sizeof(ngx_resolver_an_t);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2789
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2790 if (type == NGX_RESOLVE_SRV) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2791
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2792 srvs[j].priority = (buf[i] << 8) + buf[i + 1];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2793 srvs[j].weight = (buf[i + 2] << 8) + buf[i + 3];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2794
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2795 if (srvs[j].weight == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2796 srvs[j].weight = 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2797 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2798
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2799 srvs[j].port = (buf[i + 4] << 8) + buf[i + 5];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2800
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2801 if (ngx_resolver_copy(r, &srvs[j].name, buf, &buf[i + 6],
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2802 buf + n)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2803 != NGX_OK)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2804 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2805 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2806 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2807
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2808 j++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2809 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2810
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2811 i += len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2812 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2813
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2814 ngx_sort(srvs, nsrvs, sizeof(ngx_resolver_srv_t),
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2815 ngx_resolver_cmp_srvs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2816
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2817 ngx_resolver_free(r, rn->query);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2818 rn->query = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2819
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2820 ngx_queue_remove(&rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2821
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2822 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2823 rn->expire = ngx_time() + r->expire;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2824
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2825 ngx_queue_insert_head(&r->srv_expire_queue, &rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2826
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2827 next = rn->waiting;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2828 rn->waiting = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2829
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2830 while (next) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2831 ctx = next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2832 next = ctx->next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2833
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2834 ngx_resolver_resolve_srv_names(ctx, rn);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2835 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2836
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2837 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2838 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2839
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2840 rn->nsrvs = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2841
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2842 if (cname) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2843
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2844 /* CNAME only */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2845
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2846 if (ngx_resolver_copy(r, &name, buf, cname, buf + n) != NGX_OK) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2847 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2848 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2849
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2850 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2851 "resolver cname:\"%V\"", &name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2852
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2853 ngx_queue_remove(&rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2854
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2855 rn->cnlen = (u_short) name.len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2856 rn->u.cname = name.data;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2857
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2858 rn->valid = ngx_time() + (r->valid ? r->valid : (time_t) rn->ttl);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2859 rn->expire = ngx_time() + r->expire;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2860
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2861 ngx_queue_insert_head(&r->srv_expire_queue, &rn->queue);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2862
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2863 ngx_resolver_free(r, rn->query);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2864 rn->query = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2865 #if (NGX_HAVE_INET6)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2866 rn->query6 = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2867 #endif
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2868
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2869 ctx = rn->waiting;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2870 rn->waiting = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2871
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2872 if (ctx) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2873
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2874 if (ctx->recursion++ >= NGX_RESOLVER_MAX_RECURSION) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2875
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2876 /* unlock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2877
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2878 do {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2879 ctx->state = NGX_RESOLVE_NXDOMAIN;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2880 next = ctx->next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2881
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2882 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2883
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2884 ctx = next;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2885 } while (ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2886
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2887 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2888 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2889
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2890 for (next = ctx; next; next = next->next) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2891 next->node = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2892 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2893
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2894 (void) ngx_resolve_name_locked(r, ctx, &name);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2895 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2896
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2897 /* unlock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2898
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2899 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2900 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2901
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2902 ngx_log_error(r->log_level, r->log, 0, "no SRV type in DNS response");
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2903
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2904 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2905
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2906 short_response:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2907
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2908 err = "short DNS response";
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2909
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2910 invalid:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2911
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2912 /* unlock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2913
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2914 ngx_log_error(r->log_level, r->log, 0, err);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2915
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2916 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2917
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2918 failed:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2919
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2920 /* unlock name mutex */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2921
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2922 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2923 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2924
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2925
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2926 static void
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2927 ngx_resolver_resolve_srv_names(ngx_resolver_ctx_t *ctx, ngx_resolver_node_t *rn)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2928 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2929 ngx_uint_t i;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2930 ngx_resolver_t *r;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2931 ngx_resolver_ctx_t *cctx;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2932 ngx_resolver_srv_name_t *srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2933
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2934 r = ctx->resolver;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2935
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2936 ctx->node = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2937 ctx->state = NGX_OK;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2938 ctx->valid = rn->valid;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2939 ctx->count = rn->nsrvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2940
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2941 srvs = ngx_resolver_calloc(r, rn->nsrvs * sizeof(ngx_resolver_srv_name_t));
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2942 if (srvs == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2943 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2944 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2945
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2946 ctx->srvs = srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2947 ctx->nsrvs = rn->nsrvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2948
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2949 for (i = 0; i < rn->nsrvs; i++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2950 srvs[i].name.data = ngx_resolver_alloc(r, rn->u.srvs[i].name.len);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2951 if (srvs[i].name.data == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2952 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2953 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2954
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2955 srvs[i].name.len = rn->u.srvs[i].name.len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2956 ngx_memcpy(srvs[i].name.data, rn->u.srvs[i].name.data,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2957 srvs[i].name.len);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2958
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2959 cctx = ngx_resolve_start(r, NULL);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2960 if (cctx == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2961 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2962 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2963
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2964 cctx->name = srvs[i].name;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2965 cctx->handler = ngx_resolver_srv_names_handler;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2966 cctx->data = ctx;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2967 cctx->srvs = &srvs[i];
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2968 cctx->timeout = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2969
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2970 srvs[i].priority = rn->u.srvs[i].priority;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2971 srvs[i].weight = rn->u.srvs[i].weight;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2972 srvs[i].port = rn->u.srvs[i].port;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2973 srvs[i].ctx = cctx;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2974
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2975 if (ngx_resolve_name(cctx) == NGX_ERROR) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2976 srvs[i].ctx = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2977 goto failed;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2978 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2979 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2980
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2981 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2982
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2983 failed:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2984
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2985 ctx->state = NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2986 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2987
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2988 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2989 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2990
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2991
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2992 static void
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2993 ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *cctx)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
2994 {
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
2995 ngx_uint_t i;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
2996 ngx_addr_t *addrs;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
2997 ngx_resolver_t *r;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
2998 ngx_sockaddr_t *sockaddr;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
2999 ngx_resolver_ctx_t *ctx;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
3000 ngx_resolver_srv_name_t *srv;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3001
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3002 r = cctx->resolver;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3003 ctx = cctx->data;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3004 srv = cctx->srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3005
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3006 ctx->count--;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3007
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3008 srv->ctx = NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3009
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3010 if (cctx->naddrs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3011
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3012 ctx->valid = ngx_min(ctx->valid, cctx->valid);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3013
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3014 addrs = ngx_resolver_calloc(r, cctx->naddrs * sizeof(ngx_addr_t));
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3015 if (addrs == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3016 ngx_resolve_name_done(cctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3017
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3018 ctx->state = NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3019 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3020
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3021 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3022 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3023 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3024
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
3025 sockaddr = ngx_resolver_alloc(r, cctx->naddrs * sizeof(ngx_sockaddr_t));
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3026 if (sockaddr == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3027 ngx_resolver_free(r, addrs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3028 ngx_resolve_name_done(cctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3029
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3030 ctx->state = NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3031 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3032
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3033 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3034 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3035 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3036
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3037 for (i = 0; i < cctx->naddrs; i++) {
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
3038 addrs[i].sockaddr = &sockaddr[i].sockaddr;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3039 addrs[i].socklen = cctx->addrs[i].socklen;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3040
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
3041 ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3042 addrs[i].socklen);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3043
6593
b3b7e33083ac Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.
Roman Arutyunyan <arut@nginx.com>
parents: 6559
diff changeset
3044 ngx_inet_set_port(addrs[i].sockaddr, srv->port);
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3045 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3046
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3047 srv->addrs = addrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3048 srv->naddrs = cctx->naddrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3049 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3050
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3051 ngx_resolve_name_done(cctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3052
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3053 if (ctx->count == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3054 ngx_resolver_report_srv(r, ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3055 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3056 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3057
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3058
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3059 static void
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3060 ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
3061 ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3062 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3063 char *err;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3064 size_t len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3065 in_addr_t addr;
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
3066 int32_t ttl;
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3067 ngx_int_t octet;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3068 ngx_str_t name;
6371
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3069 ngx_uint_t mask, type, class, qident, a, i, start;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3070 ngx_queue_t *expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3071 ngx_rbtree_t *tree;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3072 ngx_resolver_an_t *an;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3073 ngx_resolver_ctx_t *ctx, *next;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3074 ngx_resolver_node_t *rn;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3075 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3076 uint32_t hash;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3077 ngx_int_t digit;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3078 struct in6_addr addr6;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3079 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3080
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3081 if (ngx_resolver_copy(r, &name, buf,
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3082 buf + sizeof(ngx_resolver_hdr_t), buf + n)
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3083 != NGX_OK)
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3084 {
5472
ab493c60d9ff Resolver: fixes in PTR processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5471
diff changeset
3085 return;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3086 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3087
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3088 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver qs:%V", &name);
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3089
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3090 /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3091
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3092 addr = 0;
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3093 i = sizeof(ngx_resolver_hdr_t);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3094
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3095 for (mask = 0; mask < 32; mask += 8) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3096 len = buf[i++];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3097
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3098 octet = ngx_atoi(&buf[i], len);
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3099 if (octet == NGX_ERROR || octet > 255) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3100 goto invalid_in_addr_arpa;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3101 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3102
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3103 addr += octet << mask;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3104 i += len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3105 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3106
5479
c0d6eae5a1c5 Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents: 5478
diff changeset
3107 if (ngx_strcasecmp(&buf[i], (u_char *) "\7in-addr\4arpa") == 0) {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3108 i += sizeof("\7in-addr\4arpa");
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3109
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3110 /* lock addr mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3111
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3112 rn = ngx_resolver_lookup_addr(r, addr);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3113
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3114 tree = &r->addr_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3115 expire_queue = &r->addr_expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3116
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3117 goto valid;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3118 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3119
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3120 invalid_in_addr_arpa:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3121
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3122 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3123
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3124 i = sizeof(ngx_resolver_hdr_t);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3125
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3126 for (octet = 15; octet >= 0; octet--) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3127 if (buf[i++] != '\1') {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3128 goto invalid_ip6_arpa;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3129 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3130
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3131 digit = ngx_hextoi(&buf[i++], 1);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3132 if (digit == NGX_ERROR) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3133 goto invalid_ip6_arpa;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3134 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3135
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3136 addr6.s6_addr[octet] = (u_char) digit;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3137
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3138 if (buf[i++] != '\1') {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3139 goto invalid_ip6_arpa;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3140 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3141
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3142 digit = ngx_hextoi(&buf[i++], 1);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3143 if (digit == NGX_ERROR) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3144 goto invalid_ip6_arpa;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3145 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3146
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3147 addr6.s6_addr[octet] += (u_char) (digit * 16);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3148 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3149
5479
c0d6eae5a1c5 Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents: 5478
diff changeset
3150 if (ngx_strcasecmp(&buf[i], (u_char *) "\3ip6\4arpa") == 0) {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3151 i += sizeof("\3ip6\4arpa");
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3152
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3153 /* lock addr mutex */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3154
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3155 hash = ngx_crc32_short(addr6.s6_addr, 16);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3156 rn = ngx_resolver_lookup_addr6(r, &addr6, hash);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3157
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3158 tree = &r->addr6_rbtree;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3159 expire_queue = &r->addr6_expire_queue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3160
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3161 goto valid;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3162 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3163
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3164 invalid_ip6_arpa:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3165 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3166
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3167 ngx_log_error(r->log_level, r->log, 0,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3168 "invalid in-addr.arpa or ip6.arpa name in DNS response");
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3169 ngx_resolver_free(r, name.data);
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3170 return;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3171
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3172 valid:
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3173
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3174 if (rn == NULL || rn->query == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3175 ngx_log_error(r->log_level, r->log, 0,
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3176 "unexpected response for %V", &name);
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3177 ngx_resolver_free(r, name.data);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3178 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3179 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3180
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3181 qident = (rn->query[0] << 8) + rn->query[1];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3182
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3183 if (ident != qident) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3184 ngx_log_error(r->log_level, r->log, 0,
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3185 "wrong ident %ui response for %V, expect %ui",
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3186 ident, &name, qident);
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3187 ngx_resolver_free(r, name.data);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3188 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3189 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3190
6372
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3191 ngx_resolver_free(r, name.data);
a6a5bacf6b74 Resolver: style.
Ruslan Ermilov <ru@nginx.com>
parents: 6371
diff changeset
3192
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
3193 if (code == 0 && nan == 0) {
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3194 code = NGX_RESOLVE_NXDOMAIN;
1742
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
3195 }
268b81386fe4 no answers in DNS response should be NXDOMAIN
Igor Sysoev <igor@sysoev.ru>
parents: 1741
diff changeset
3196
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3197 if (code) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3198 next = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3199 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3200
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3201 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3202
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3203 ngx_rbtree_delete(tree, &rn->node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3204
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3205 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3206
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3207 while (next) {
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3208 ctx = next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3209 ctx->state = code;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
3210 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3211 next = ctx->next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3212
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3213 ctx->handler(ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3214 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3215
5920
7420068c4d4b Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents: 5820
diff changeset
3216 ngx_resolver_free_node(r, rn);
7420068c4d4b Resolver: fixed use-after-free memory access.
Ruslan Ermilov <ru@nginx.com>
parents: 5820
diff changeset
3217
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3218 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3219 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3220
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3221 i += sizeof(ngx_resolver_qs_t);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3222
6371
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3223 for (a = 0; a < nan; a++) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3224
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3225 start = i;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3226
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3227 while (i < n) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3228
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3229 if (buf[i] & 0xc0) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3230 i += 2;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3231 goto found;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3232 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3233
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3234 if (buf[i] == 0) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3235 i++;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3236 goto test_length;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3237 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3238
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3239 i += 1 + buf[i];
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3240 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3241
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3242 goto short_response;
6371
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3243
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3244 test_length:
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3245
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3246 if (i - start < 2) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3247 err = "invalid name in DNS response";
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3248 goto invalid;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3249 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3250
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3251 found:
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3252
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3253 if (i + sizeof(ngx_resolver_an_t) >= n) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3254 goto short_response;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3255 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3256
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3257 an = (ngx_resolver_an_t *) &buf[i];
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3258
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3259 type = (an->type_hi << 8) + an->type_lo;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3260 class = (an->class_hi << 8) + an->class_lo;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3261 len = (an->len_hi << 8) + an->len_lo;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3262 ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3263 + (an->ttl[2] << 8) + (an->ttl[3]);
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3264
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3265 if (class != 1) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3266 ngx_log_error(r->log_level, r->log, 0,
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3267 "unexpected RR class %ui", class);
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3268 goto failed;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3269 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3270
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3271 if (ttl < 0) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3272 ttl = 0;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3273 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3274
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3275 ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3276 "resolver qt:%ui cl:%ui len:%uz",
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3277 type, class, len);
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3278
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3279 i += sizeof(ngx_resolver_an_t);
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3280
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3281 switch (type) {
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3282
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3283 case NGX_RESOLVE_PTR:
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3284
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3285 goto ptr;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3286
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3287 case NGX_RESOLVE_CNAME:
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3288
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3289 break;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3290
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3291 default:
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3292
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3293 ngx_log_error(r->log_level, r->log, 0,
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3294 "unexpected RR type %ui", type);
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3295 }
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3296
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3297 i += len;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3298 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3299
6371
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3300 /* unlock addr mutex */
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3301
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3302 ngx_log_error(r->log_level, r->log, 0,
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3303 "no PTR type in DNS response");
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3304 return;
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3305
33c4d319f08f Resolver: improved PTR response processing.
Ruslan Ermilov <ru@nginx.com>
parents: 6370
diff changeset
3306 ptr:
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3307
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3308 if (ngx_resolver_copy(r, &name, buf, buf + i, buf + n) != NGX_OK) {
5472
ab493c60d9ff Resolver: fixes in PTR processing.
Ruslan Ermilov <ru@nginx.com>
parents: 5471
diff changeset
3309 goto failed;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3310 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3311
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3312 ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver an:%V", &name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3313
2486
8de5dc3e7001 use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents: 2484
diff changeset
3314 if (name.len != (size_t) rn->nlen
8de5dc3e7001 use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents: 2484
diff changeset
3315 || ngx_strncmp(name.data, rn->name, name.len) != 0)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3316 {
2482
30ec8c5ac75b fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents: 2314
diff changeset
3317 if (rn->nlen) {
30ec8c5ac75b fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents: 2314
diff changeset
3318 ngx_resolver_free(r, rn->name);
30ec8c5ac75b fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents: 2314
diff changeset
3319 }
30ec8c5ac75b fix reverse resolving cache: it stored zero length names
Igor Sysoev <igor@sysoev.ru>
parents: 2314
diff changeset
3320
2490
1c87647b7ca5 fix building by msvc, introduced in r2487
Igor Sysoev <igor@sysoev.ru>
parents: 2487
diff changeset
3321 rn->nlen = (u_short) name.len;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3322 rn->name = name.data;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3323
2486
8de5dc3e7001 use length of uncompressed name
Igor Sysoev <igor@sysoev.ru>
parents: 2484
diff changeset
3324 name.data = ngx_resolver_dup(r, rn->name, name.len);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3325 if (name.data == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3326 goto failed;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3327 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3328 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3329
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3330 ngx_queue_remove(&rn->queue);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3331
4295
05031fce7ce8 Now nginx uses TTL of a DNS response when calculating cache validity.
Ruslan Ermilov <ru@nginx.com>
parents: 4267
diff changeset
3332 rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3333 rn->expire = ngx_time() + r->expire;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3334
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3335 ngx_queue_insert_head(expire_queue, &rn->queue);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3336
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3337 next = rn->waiting;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3338 rn->waiting = NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3339
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3340 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3341
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3342 while (next) {
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3343 ctx = next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3344 ctx->state = NGX_OK;
6456
c94aba230a5a Resolver: introduced valid field in resolver responses.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6372
diff changeset
3345 ctx->valid = rn->valid;
5474
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3346 ctx->name = name;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3347 next = ctx->next;
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3348
b43b02bb54db Resolver: fixed indentation.
Ruslan Ermilov <ru@nginx.com>
parents: 5472
diff changeset
3349 ctx->handler(ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3350 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3351
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3352 ngx_resolver_free(r, name.data);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3353
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3354 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3355
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3356 short_response:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3357
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3358 err = "short DNS response";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3359
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3360 invalid:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3361
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3362 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3363
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3364 ngx_log_error(r->log_level, r->log, 0, err);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3365
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3366 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3367
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3368 failed:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3369
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3370 /* unlock addr mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3371
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3372 return;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3373 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3374
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3375
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3376 static ngx_resolver_node_t *
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3377 ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3378 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3379 ngx_int_t rc;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3380 ngx_rbtree_node_t *node, *sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3381 ngx_resolver_node_t *rn;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3382
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3383 node = r->name_rbtree.root;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3384 sentinel = r->name_rbtree.sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3385
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3386 while (node != sentinel) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3387
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3388 if (hash < node->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3389 node = node->left;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3390 continue;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3391 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3392
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3393 if (hash > node->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3394 node = node->right;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3395 continue;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3396 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3397
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3398 /* hash == node->key */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3399
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3400 rn = ngx_resolver_node(node);
4497
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3401
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3402 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3403
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3404 if (rc == 0) {
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3405 return rn;
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3406 }
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3407
95ab6658654a Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4496
diff changeset
3408 node = (rc < 0) ? node->left : node->right;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3409 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3410
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3411 /* not found */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3412
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3413 return NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3414 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3415
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3416
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3417 static ngx_resolver_node_t *
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3418 ngx_resolver_lookup_srv(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3419 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3420 ngx_int_t rc;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3421 ngx_rbtree_node_t *node, *sentinel;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3422 ngx_resolver_node_t *rn;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3423
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3424 node = r->srv_rbtree.root;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3425 sentinel = r->srv_rbtree.sentinel;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3426
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3427 while (node != sentinel) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3428
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3429 if (hash < node->key) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3430 node = node->left;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3431 continue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3432 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3433
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3434 if (hash > node->key) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3435 node = node->right;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3436 continue;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3437 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3438
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3439 /* hash == node->key */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3440
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3441 rn = ngx_resolver_node(node);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3442
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3443 rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3444
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3445 if (rc == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3446 return rn;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3447 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3448
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3449 node = (rc < 0) ? node->left : node->right;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3450 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3451
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3452 /* not found */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3453
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3454 return NULL;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3455 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3456
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3457
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3458 static ngx_resolver_node_t *
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3459 ngx_resolver_lookup_addr(ngx_resolver_t *r, in_addr_t addr)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3460 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3461 ngx_rbtree_node_t *node, *sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3462
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3463 node = r->addr_rbtree.root;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3464 sentinel = r->addr_rbtree.sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3465
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3466 while (node != sentinel) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3467
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3468 if (addr < node->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3469 node = node->left;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3470 continue;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3471 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3472
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3473 if (addr > node->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3474 node = node->right;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3475 continue;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3476 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3477
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3478 /* addr == node->key */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3479
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3480 return ngx_resolver_node(node);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3481 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3482
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3483 /* not found */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3484
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3485 return NULL;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3486 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3487
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3488
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3489 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3490
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3491 static ngx_resolver_node_t *
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3492 ngx_resolver_lookup_addr6(ngx_resolver_t *r, struct in6_addr *addr,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3493 uint32_t hash)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3494 {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3495 ngx_int_t rc;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3496 ngx_rbtree_node_t *node, *sentinel;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3497 ngx_resolver_node_t *rn;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3498
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3499 node = r->addr6_rbtree.root;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3500 sentinel = r->addr6_rbtree.sentinel;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3501
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3502 while (node != sentinel) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3503
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3504 if (hash < node->key) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3505 node = node->left;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3506 continue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3507 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3508
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3509 if (hash > node->key) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3510 node = node->right;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3511 continue;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3512 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3513
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3514 /* hash == node->key */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3515
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3516 rn = ngx_resolver_node(node);
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3517
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3518 rc = ngx_memcmp(addr, &rn->addr6, 16);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3519
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3520 if (rc == 0) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3521 return rn;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3522 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3523
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3524 node = (rc < 0) ? node->left : node->right;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3525 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3526
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3527 /* not found */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3528
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3529 return NULL;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3530 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3531
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3532 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3533
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3534
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3535 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3536 ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3537 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3538 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3539 ngx_rbtree_node_t **p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3540 ngx_resolver_node_t *rn, *rn_temp;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3541
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3542 for ( ;; ) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3543
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3544 if (node->key < temp->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3545
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3546 p = &temp->left;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3547
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3548 } else if (node->key > temp->key) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3549
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3550 p = &temp->right;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3551
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3552 } else { /* node->key == temp->key */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3553
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3554 rn = ngx_resolver_node(node);
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3555 rn_temp = ngx_resolver_node(temp);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3556
3143
ab6258e18099 fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents: 3139
diff changeset
3557 p = (ngx_memn2cmp(rn->name, rn_temp->name, rn->nlen, rn_temp->nlen)
ab6258e18099 fix resolver cache rbtree comparison
Igor Sysoev <igor@sysoev.ru>
parents: 3139
diff changeset
3558 < 0) ? &temp->left : &temp->right;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3559 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3560
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3561 if (*p == sentinel) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3562 break;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3563 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3564
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3565 temp = *p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3566 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3567
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3568 *p = node;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3569 node->parent = temp;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3570 node->left = sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3571 node->right = sentinel;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3572 ngx_rbt_red(node);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3573 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3574
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3575
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3576 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3577
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3578 static void
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3579 ngx_resolver_rbtree_insert_addr6_value(ngx_rbtree_node_t *temp,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3580 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3581 {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3582 ngx_rbtree_node_t **p;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3583 ngx_resolver_node_t *rn, *rn_temp;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3584
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3585 for ( ;; ) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3586
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3587 if (node->key < temp->key) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3588
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3589 p = &temp->left;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3590
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3591 } else if (node->key > temp->key) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3592
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3593 p = &temp->right;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3594
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3595 } else { /* node->key == temp->key */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3596
5921
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3597 rn = ngx_resolver_node(node);
5004210e8c78 Resolver: fixed debug event logging.
Ruslan Ermilov <ru@nginx.com>
parents: 5920
diff changeset
3598 rn_temp = ngx_resolver_node(temp);
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3599
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3600 p = (ngx_memcmp(&rn->addr6, &rn_temp->addr6, 16)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3601 < 0) ? &temp->left : &temp->right;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3602 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3603
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3604 if (*p == sentinel) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3605 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3606 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3607
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3608 temp = *p;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3609 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3610
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3611 *p = node;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3612 node->parent = temp;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3613 node->left = sentinel;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3614 node->right = sentinel;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3615 ngx_rbt_red(node);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3616 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3617
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3618 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3619
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3620
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3621 static ngx_int_t
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3622 ngx_resolver_create_name_query(ngx_resolver_t *r, ngx_resolver_node_t *rn,
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3623 ngx_str_t *name)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3624 {
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3625 u_char *p, *s;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3626 size_t len, nlen;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3627 ngx_uint_t ident;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3628 ngx_resolver_qs_t *qs;
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3629 ngx_resolver_hdr_t *query;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3630
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3631 nlen = name->len ? (1 + name->len + 1) : 1;
3306
61bdaac6c668 fix resolving an empty name (".")
Igor Sysoev <igor@sysoev.ru>
parents: 3299
diff changeset
3632
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3633 len = sizeof(ngx_resolver_hdr_t) + nlen + sizeof(ngx_resolver_qs_t);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3634
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3635 #if (NGX_HAVE_INET6)
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3636 p = ngx_resolver_alloc(r, r->ipv6 ? len * 2 : len);
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3637 #else
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3638 p = ngx_resolver_alloc(r, len);
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3639 #endif
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3640 if (p == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3641 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3642 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3643
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3644 rn->qlen = (u_short) len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3645 rn->query = p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3646
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3647 #if (NGX_HAVE_INET6)
5478
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3648 if (r->ipv6) {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3649 rn->query6 = p + len;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3650 }
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3651 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3652
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3653 query = (ngx_resolver_hdr_t *) p;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3654
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3655 ident = ngx_random();
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3656
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3657 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3658 "resolve: \"%V\" A %i", name, ident & 0xffff);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3659
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3660 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3661 query->ident_lo = (u_char) (ident & 0xff);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3662
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3663 /* recursion query */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3664 query->flags_hi = 1; query->flags_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3665
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3666 /* one question */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3667 query->nqs_hi = 0; query->nqs_lo = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3668 query->nan_hi = 0; query->nan_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3669 query->nns_hi = 0; query->nns_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3670 query->nar_hi = 0; query->nar_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3671
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3672 p += sizeof(ngx_resolver_hdr_t) + nlen;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3673
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3674 qs = (ngx_resolver_qs_t *) p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3675
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3676 /* query type */
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3677 qs->type_hi = 0; qs->type_lo = NGX_RESOLVE_A;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3678
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3679 /* IN query class */
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3680 qs->class_hi = 0; qs->class_lo = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3681
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3682 /* convert "www.example.com" to "\3www\7example\3com\0" */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3683
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3684 len = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3685 p--;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3686 *p-- = '\0';
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3687
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3688 if (name->len == 0) {
4610
778d2cc03e22 Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents: 4556
diff changeset
3689 return NGX_DECLINED;
778d2cc03e22 Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents: 4556
diff changeset
3690 }
778d2cc03e22 Fixed segmentation fault in ngx_resolver_create_name_query().
Ruslan Ermilov <ru@nginx.com>
parents: 4556
diff changeset
3691
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3692 for (s = name->data + name->len - 1; s >= name->data; s--) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3693 if (*s != '.') {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3694 *p = *s;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3695 len++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3696
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3697 } else {
4556
1bddc91e78d6 Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4497
diff changeset
3698 if (len == 0 || len > 255) {
1961
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
3699 return NGX_DECLINED;
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
3700 }
99b9feacccb4 return NXDOMAIN for ".." in host name
Igor Sysoev <igor@sysoev.ru>
parents: 1960
diff changeset
3701
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3702 *p = (u_char) len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3703 len = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3704 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3705
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3706 p--;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3707 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3708
4556
1bddc91e78d6 Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4497
diff changeset
3709 if (len == 0 || len > 255) {
1bddc91e78d6 Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4497
diff changeset
3710 return NGX_DECLINED;
1bddc91e78d6 Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4497
diff changeset
3711 }
1bddc91e78d6 Resolver: added missing sanity checking when creating name queries.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4497
diff changeset
3712
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3713 *p = (u_char) len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3714
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3715 #if (NGX_HAVE_INET6)
5478
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3716 if (!r->ipv6) {
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3717 return NGX_OK;
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3718 }
3cb3175a6fef The "ipv6=" boolean parameter of the "resolver" directive.
Ruslan Ermilov <ru@nginx.com>
parents: 5477
diff changeset
3719
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3720 p = rn->query6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3721
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3722 ngx_memcpy(p, rn->query, rn->qlen);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3723
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3724 query = (ngx_resolver_hdr_t *) p;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3725
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3726 ident = ngx_random();
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3727
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3728 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3729 "resolve: \"%V\" AAAA %i", name, ident & 0xffff);
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3730
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3731 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3732 query->ident_lo = (u_char) (ident & 0xff);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3733
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3734 p += sizeof(ngx_resolver_hdr_t) + nlen;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3735
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3736 qs = (ngx_resolver_qs_t *) p;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3737
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3738 qs->type_lo = NGX_RESOLVE_AAAA;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3739 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
3740
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3741 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3742 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3743
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3744
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3745 static ngx_int_t
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3746 ngx_resolver_create_srv_query(ngx_resolver_t *r, ngx_resolver_node_t *rn,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3747 ngx_str_t *name)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3748 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3749 u_char *p, *s;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3750 size_t len, nlen;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3751 ngx_uint_t ident;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3752 ngx_resolver_qs_t *qs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3753 ngx_resolver_hdr_t *query;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3754
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3755 nlen = name->len ? (1 + name->len + 1) : 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3756
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3757 len = sizeof(ngx_resolver_hdr_t) + nlen + sizeof(ngx_resolver_qs_t);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3758
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3759 p = ngx_resolver_alloc(r, len);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3760 if (p == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3761 return NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3762 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3763
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3764 rn->qlen = (u_short) len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3765 rn->query = p;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3766
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3767 query = (ngx_resolver_hdr_t *) p;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3768
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3769 ident = ngx_random();
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3770
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3771 ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0,
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3772 "resolve: \"%V\" SRV %i", name, ident & 0xffff);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3773
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3774 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3775 query->ident_lo = (u_char) (ident & 0xff);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3776
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3777 /* recursion query */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3778 query->flags_hi = 1; query->flags_lo = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3779
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3780 /* one question */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3781 query->nqs_hi = 0; query->nqs_lo = 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3782 query->nan_hi = 0; query->nan_lo = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3783 query->nns_hi = 0; query->nns_lo = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3784 query->nar_hi = 0; query->nar_lo = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3785
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3786 p += sizeof(ngx_resolver_hdr_t) + nlen;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3787
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3788 qs = (ngx_resolver_qs_t *) p;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3789
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3790 /* query type */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3791 qs->type_hi = 0; qs->type_lo = NGX_RESOLVE_SRV;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3792
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3793 /* IN query class */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3794 qs->class_hi = 0; qs->class_lo = 1;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3795
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3796 /* converts "www.example.com" to "\3www\7example\3com\0" */
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3797
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3798 len = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3799 p--;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3800 *p-- = '\0';
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3801
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3802 if (name->len == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3803 return NGX_DECLINED;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3804 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3805
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3806 for (s = name->data + name->len - 1; s >= name->data; s--) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3807 if (*s != '.') {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3808 *p = *s;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3809 len++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3810
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3811 } else {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3812 if (len == 0 || len > 255) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3813 return NGX_DECLINED;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3814 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3815
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3816 *p = (u_char) len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3817 len = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3818 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3819
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3820 p--;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3821 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3822
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3823 if (len == 0 || len > 255) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3824 return NGX_DECLINED;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3825 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3826
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3827 *p = (u_char) len;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3828
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3829 return NGX_OK;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3830 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3831
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3832
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3833 static ngx_int_t
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3834 ngx_resolver_create_addr_query(ngx_resolver_t *r, ngx_resolver_node_t *rn,
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
3835 ngx_resolver_addr_t *addr)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3836 {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3837 u_char *p, *d;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3838 size_t len;
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3839 in_addr_t inaddr;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3840 ngx_int_t n;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3841 ngx_uint_t ident;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3842 ngx_resolver_hdr_t *query;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3843 struct sockaddr_in *sin;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3844 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3845 struct sockaddr_in6 *sin6;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3846 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3847
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3848 switch (addr->sockaddr->sa_family) {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3849
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3850 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3851 case AF_INET6:
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3852 len = sizeof(ngx_resolver_hdr_t)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3853 + 64 + sizeof(".ip6.arpa.") - 1
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3854 + sizeof(ngx_resolver_qs_t);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3855
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3856 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3857 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3858
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3859 default: /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3860 len = sizeof(ngx_resolver_hdr_t)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3861 + sizeof(".255.255.255.255.in-addr.arpa.") - 1
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3862 + sizeof(ngx_resolver_qs_t);
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
3863 }
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3864
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3865 p = ngx_resolver_alloc(r, len);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3866 if (p == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3867 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3868 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3869
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3870 rn->query = p;
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3871 query = (ngx_resolver_hdr_t *) p;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3872
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3873 ident = ngx_random();
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3874
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3875 query->ident_hi = (u_char) ((ident >> 8) & 0xff);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3876 query->ident_lo = (u_char) (ident & 0xff);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3877
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3878 /* recursion query */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3879 query->flags_hi = 1; query->flags_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3880
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3881 /* one question */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3882 query->nqs_hi = 0; query->nqs_lo = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3883 query->nan_hi = 0; query->nan_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3884 query->nns_hi = 0; query->nns_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3885 query->nar_hi = 0; query->nar_lo = 0;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3886
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3887 p += sizeof(ngx_resolver_hdr_t);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3888
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3889 switch (addr->sockaddr->sa_family) {
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3890
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3891 #if (NGX_HAVE_INET6)
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3892 case AF_INET6:
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3893 sin6 = (struct sockaddr_in6 *) addr->sockaddr;
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3894
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3895 for (n = 15; n >= 0; n--) {
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3896 p = ngx_sprintf(p, "\1%xd\1%xd",
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3897 sin6->sin6_addr.s6_addr[n] & 0xf,
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3898 (sin6->sin6_addr.s6_addr[n] >> 4) & 0xf);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3899 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3900
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3901 p = ngx_cpymem(p, "\3ip6\4arpa\0", 10);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3902
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3903 break;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3904 #endif
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3905
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3906 default: /* AF_INET */
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3907
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3908 sin = (struct sockaddr_in *) addr->sockaddr;
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3909 inaddr = ntohl(sin->sin_addr.s_addr);
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3910
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3911 for (n = 0; n < 32; n += 8) {
6350
a5767988c022 Resolver: changed the ngx_resolver_create_*_query() arguments.
Roman Arutyunyan <arut@nginx.com>
parents: 6349
diff changeset
3912 d = ngx_sprintf(&p[1], "%ud", (inaddr >> n) & 0xff);
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3913 *p = (u_char) (d - &p[1]);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3914 p = d;
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3915 }
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3916
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3917 p = ngx_cpymem(p, "\7in-addr\4arpa\0", 14);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3918 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3919
5468
5c410d6ca7dd Resolver: improved code readability.
Ruslan Ermilov <ru@nginx.com>
parents: 5360
diff changeset
3920 /* query type "PTR", IN query class */
5476
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3921 p = ngx_cpymem(p, "\0\14\0\1", 4);
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3922
950c9ed3e66f Resolver: implemented IPv6 address to name resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5475
diff changeset
3923 rn->qlen = (u_short) (p - rn->query);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3924
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3925 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3926 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3927
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3928
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3929 static ngx_int_t
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3930 ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src,
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3931 u_char *last)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3932 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3933 char *err;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3934 u_char *p, *dst;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3935 ssize_t len;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3936 ngx_uint_t i, n;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3937
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3938 p = src;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3939 len = -1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3940
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3941 /*
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3942 * compression pointers allow to create endless loop, so we set limit;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3943 * 128 pointers should be enough to store 255-byte name
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3944 */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3945
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3946 for (i = 0; i < 128; i++) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3947 n = *p++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3948
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3949 if (n == 0) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3950 goto done;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3951 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3952
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3953 if (n & 0xc0) {
2314
52987a023486 fix compression pointer for big (>255) DNS responses
Igor Sysoev <igor@sysoev.ru>
parents: 2282
diff changeset
3954 n = ((n & 0x3f) << 8) + *p;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3955 p = &buf[n];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3956
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3957 } else {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3958 len += 1 + n;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3959 p = &p[n];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3960 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3961
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3962 if (p >= last) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3963 err = "name is out of response";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3964 goto invalid;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3965 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3966 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3967
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3968 err = "compression pointers loop";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3969
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3970 invalid:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3971
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3972 ngx_log_error(r->log_level, r->log, 0, err);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3973
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3974 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3975
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3976 done:
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3977
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3978 if (name == NULL) {
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3979 return NGX_OK;
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3980 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3981
3298
847ab5a32307 fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents: 3297
diff changeset
3982 if (len == -1) {
5764
f166c521b619 Style: use ngx_str_null().
Tatsuhiko Kubo <cubicdaiya@gmail.com>
parents: 5600
diff changeset
3983 ngx_str_null(name);
3298
847ab5a32307 fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents: 3297
diff changeset
3984 return NGX_OK;
847ab5a32307 fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents: 3297
diff changeset
3985 }
847ab5a32307 fix "PTR ." case in address resolver
Igor Sysoev <igor@sysoev.ru>
parents: 3297
diff changeset
3986
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3987 dst = ngx_resolver_alloc(r, len);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3988 if (dst == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3989 return NGX_ERROR;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3990 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3991
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3992 name->data = dst;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3993
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3994 n = *src++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3995
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
3996 for ( ;; ) {
4267
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
3997 if (n & 0xc0) {
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
3998 n = ((n & 0x3f) << 8) + *src;
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
3999 src = &buf[n];
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
4000
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
4001 n = *src++;
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
4002
768212ca0745 Fixed compression pointer processing in DNS response greater than 255 bytes.
Igor Sysoev <igor@sysoev.ru>
parents: 4225
diff changeset
4003 } else {
5479
c0d6eae5a1c5 Resolver: lookups are case-insensitive.
Ruslan Ermilov <ru@nginx.com>
parents: 5478
diff changeset
4004 ngx_strlow(dst, src, n);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4005 dst += n;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4006 src += n;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4007
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4008 n = *src++;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4009
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4010 if (n != 0) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4011 *dst++ = '.';
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4012 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4013 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4014
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4015 if (n == 0) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4016 name->len = dst - name->data;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4017 return NGX_OK;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4018 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4019 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4020 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4021
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4022
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4023 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4024 ngx_resolver_timeout_handler(ngx_event_t *ev)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4025 {
6348
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4026 ngx_resolver_ctx_t *ctx;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4027
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4028 ctx = ev->data;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4029
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4030 ctx->state = NGX_RESOLVE_TIMEDOUT;
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4031
7316c57e4fe7 Resolver: fixed crashes in timeout handler.
Ruslan Ermilov <ru@nginx.com>
parents: 6347
diff changeset
4032 ctx->handler(ctx);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4033 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4034
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4035
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4036 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4037 ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4038 {
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4039 ngx_uint_t i;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4040
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4041 /* lock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4042
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4043 if (rn->query) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4044 ngx_resolver_free_locked(r, rn->query);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4045 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4046
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4047 if (rn->name) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4048 ngx_resolver_free_locked(r, rn->name);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4049 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4050
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4051 if (rn->cnlen) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4052 ngx_resolver_free_locked(r, rn->u.cname);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4053 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4054
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4055 if (rn->naddrs > 1 && rn->naddrs != (u_short) -1) {
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4056 ngx_resolver_free_locked(r, rn->u.addrs);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4057 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4058
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4059 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4060 if (rn->naddrs6 > 1 && rn->naddrs6 != (u_short) -1) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4061 ngx_resolver_free_locked(r, rn->u6.addrs6);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4062 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4063 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4064
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4065 if (rn->nsrvs) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4066 for (i = 0; i < rn->nsrvs; i++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4067 if (rn->u.srvs[i].name.data) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4068 ngx_resolver_free_locked(r, rn->u.srvs[i].name.data);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4069 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4070 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4071
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4072 ngx_resolver_free_locked(r, rn->u.srvs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4073 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4074
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4075 ngx_resolver_free_locked(r, rn);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4076
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4077 /* unlock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4078 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4079
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4080
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4081 static void *
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4082 ngx_resolver_alloc(ngx_resolver_t *r, size_t size)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4083 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4084 u_char *p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4085
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4086 /* lock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4087
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4088 p = ngx_alloc(size, r->log);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4089
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4090 /* unlock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4091
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4092 return p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4093 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4094
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4095
1903
1ea922585517 static ngx_resolver_calloc()
Igor Sysoev <igor@sysoev.ru>
parents: 1880
diff changeset
4096 static void *
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4097 ngx_resolver_calloc(ngx_resolver_t *r, size_t size)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4098 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4099 u_char *p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4100
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4101 p = ngx_resolver_alloc(r, size);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4102
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4103 if (p) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4104 ngx_memzero(p, size);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4105 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4106
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4107 return p;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4108 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4109
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4110
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4111 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4112 ngx_resolver_free(ngx_resolver_t *r, void *p)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4113 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4114 /* lock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4115
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4116 ngx_free(p);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4117
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4118 /* unlock alloc mutex */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4119 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4120
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4121
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4122 static void
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4123 ngx_resolver_free_locked(ngx_resolver_t *r, void *p)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4124 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4125 ngx_free(p);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4126 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4127
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4128
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4129 static void *
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4130 ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4131 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4132 void *dst;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4133
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4134 dst = ngx_resolver_alloc(r, size);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4135
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4136 if (dst == NULL) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4137 return dst;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4138 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4139
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4140 ngx_memcpy(dst, src, size);
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4141
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4142 return dst;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4143 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4144
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4145
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4146 static ngx_resolver_addr_t *
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4147 ngx_resolver_export(ngx_resolver_t *r, ngx_resolver_node_t *rn,
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4148 ngx_uint_t rotate)
4871
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4149 {
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4150 ngx_uint_t d, i, j, n;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4151 in_addr_t *addr;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4152 ngx_sockaddr_t *sockaddr;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4153 struct sockaddr_in *sin;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4154 ngx_resolver_addr_t *dst;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4155 #if (NGX_HAVE_INET6)
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4156 struct in6_addr *addr6;
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4157 struct sockaddr_in6 *sin6;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4158 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4159
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4160 n = rn->naddrs;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4161 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4162 n += rn->naddrs6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4163 #endif
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4164
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4165 dst = ngx_resolver_calloc(r, n * sizeof(ngx_resolver_addr_t));
4892
063ac68d89dc Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4871
diff changeset
4166 if (dst == NULL) {
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4167 return NULL;
4892
063ac68d89dc Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4871
diff changeset
4168 }
063ac68d89dc Resolver: added missing memory allocation error handling.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4871
diff changeset
4169
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4170 sockaddr = ngx_resolver_calloc(r, n * sizeof(ngx_sockaddr_t));
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4171 if (sockaddr == NULL) {
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4172 ngx_resolver_free(r, dst);
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4173 return NULL;
4871
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4174 }
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4175
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4176 i = 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4177 d = rotate ? ngx_random() % n : 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4178
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4179 if (rn->naddrs) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4180 j = rotate ? ngx_random() % rn->naddrs : 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4181
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4182 addr = (rn->naddrs == 1) ? &rn->u.addr : rn->u.addrs;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4183
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4184 do {
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4185 sin = &sockaddr[d].sockaddr_in;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4186 sin->sin_family = AF_INET;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4187 sin->sin_addr.s_addr = addr[j++];
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4188 dst[d].sockaddr = (struct sockaddr *) sin;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4189 dst[d++].socklen = sizeof(struct sockaddr_in);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4190
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4191 if (d == n) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4192 d = 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4193 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4194
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4195 if (j == rn->naddrs) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4196 j = 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4197 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4198 } while (++i < rn->naddrs);
5475
07dd5bd222ac Changed resolver API to use ngx_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents: 5474
diff changeset
4199 }
4871
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4200
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4201 #if (NGX_HAVE_INET6)
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4202 if (rn->naddrs6) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4203 j = rotate ? ngx_random() % rn->naddrs6 : 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4204
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4205 addr6 = (rn->naddrs6 == 1) ? &rn->u6.addr6 : rn->u6.addrs6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4206
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4207 do {
6559
adf25b8d0431 Introduced the ngx_sockaddr_t type.
Ruslan Ermilov <ru@nginx.com>
parents: 6509
diff changeset
4208 sin6 = &sockaddr[d].sockaddr_in6;
5477
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4209 sin6->sin6_family = AF_INET6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4210 ngx_memcpy(sin6->sin6_addr.s6_addr, addr6[j++].s6_addr, 16);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4211 dst[d].sockaddr = (struct sockaddr *) sin6;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4212 dst[d++].socklen = sizeof(struct sockaddr_in6);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4213
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4214 if (d == n) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4215 d = 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4216 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4217
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4218 if (j == rn->naddrs6) {
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4219 j = 0;
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4220 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4221 } while (++i < n);
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4222 }
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4223 #endif
98876ce2a7fd Resolver: implemented IPv6 name to address resolving.
Ruslan Ermilov <ru@nginx.com>
parents: 5476
diff changeset
4224
4871
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4225 return dst;
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4226 }
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4227
c85cefbdaafe Resolver: cached addresses are returned with random rotation now.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4784
diff changeset
4228
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4229 static void
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4230 ngx_resolver_report_srv(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4231 {
6460
034329824dd3 Win32: fixed build after 384154fc634f.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6458
diff changeset
4232 ngx_uint_t naddrs, nsrvs, nw, i, j, k, l, m, n, w;
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4233 ngx_resolver_addr_t *addrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4234 ngx_resolver_srv_name_t *srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4235
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4236 naddrs = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4237
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4238 for (i = 0; i < ctx->nsrvs; i++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4239 naddrs += ctx->srvs[i].naddrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4240 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4241
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4242 if (naddrs == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4243 ctx->state = NGX_RESOLVE_NXDOMAIN;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4244 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4245
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4246 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4247 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4248 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4249
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4250 addrs = ngx_resolver_calloc(r, naddrs * sizeof(ngx_resolver_addr_t));
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4251 if (addrs == NULL) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4252 ctx->state = NGX_ERROR;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4253 ctx->valid = ngx_time() + (r->valid ? r->valid : 10);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4254
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4255 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4256 return;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4257 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4258
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4259 srvs = ctx->srvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4260 nsrvs = ctx->nsrvs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4261
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4262 i = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4263 n = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4264
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4265 do {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4266 nw = 0;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4267
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4268 for (j = i; j < nsrvs; j++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4269 if (srvs[j].priority != srvs[i].priority) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4270 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4271 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4272
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4273 nw += srvs[j].naddrs * srvs[j].weight;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4274 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4275
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4276 if (nw == 0) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4277 goto next_srv;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4278 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4279
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4280 w = ngx_random() % nw;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4281
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4282 for (k = i; k < j; k++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4283 if (w < srvs[k].naddrs * srvs[k].weight) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4284 break;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4285 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4286
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4287 w -= srvs[k].naddrs * srvs[k].weight;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4288 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4289
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4290 for (l = i; l < j; l++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4291
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4292 for (m = 0; m < srvs[k].naddrs; m++) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4293 addrs[n].socklen = srvs[k].addrs[m].socklen;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4294 addrs[n].sockaddr = srvs[k].addrs[m].sockaddr;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4295 addrs[n].name = srvs[k].name;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4296 addrs[n].priority = srvs[k].priority;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4297 addrs[n].weight = srvs[k].weight;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4298 n++;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4299 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4300
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4301 if (++k == j) {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4302 k = i;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4303 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4304 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4305
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4306 next_srv:
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4307
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4308 i = j;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4309
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4310 } while (i < ctx->nsrvs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4311
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4312 ctx->state = NGX_OK;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4313 ctx->addrs = addrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4314 ctx->naddrs = naddrs;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4315
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4316 ctx->handler(ctx);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4317
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4318 ngx_resolver_free(r, addrs);
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4319 }
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4320
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4321
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4322 char *
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4323 ngx_resolver_strerror(ngx_int_t err)
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4324 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4325 static char *errors[] = {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4326 "Format error", /* FORMERR */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4327 "Server failure", /* SERVFAIL */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4328 "Host not found", /* NXDOMAIN */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4329 "Unimplemented", /* NOTIMP */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4330 "Operation refused" /* REFUSED */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4331 };
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4332
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4333 if (err > 0 && err < 6) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4334 return errors[err - 1];
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4335 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4336
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4337 if (err == NGX_RESOLVE_TIMEDOUT) {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4338 return "Operation timed out";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4339 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4340
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4341 return "Unknown error";
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4342 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4343
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4344
3408
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4345 static u_char *
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4346 ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len)
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4347 {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4348 u_char *p;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4349 ngx_resolver_connection_t *rec;
3408
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4350
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4351 p = buf;
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4352
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4353 if (log->action) {
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4354 p = ngx_snprintf(buf, len, " while %s", log->action);
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4355 len -= p - buf;
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4356 }
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4357
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4358 rec = log->data;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4359
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4360 if (rec) {
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4361 p = ngx_snprintf(p, len, ", resolver: %V", &rec->server);
3408
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4362 }
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4363
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4364 return p;
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4365 }
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4366
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4367
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4368 ngx_int_t
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4369 ngx_udp_connect(ngx_resolver_connection_t *rec)
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4370 {
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4371 int rc;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4372 ngx_int_t event;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4373 ngx_event_t *rev, *wev;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4374 ngx_socket_t s;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4375 ngx_connection_t *c;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4376
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4377 s = ngx_socket(rec->sockaddr->sa_family, SOCK_DGRAM, 0);
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4378
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4379 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "UDP socket %d", s);
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4380
5360
3d2d3e1cf427 Win32: MinGW GCC compatibility.
Maxim Dounin <mdounin@mdounin.ru>
parents: 4892
diff changeset
4381 if (s == (ngx_socket_t) -1) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4382 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4383 ngx_socket_n " failed");
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4384 return NGX_ERROR;
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4385 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4386
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4387 c = ngx_get_connection(s, &rec->log);
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4388
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4389 if (c == NULL) {
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4390 if (ngx_close_socket(s) == -1) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4391 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4392 ngx_close_socket_n "failed");
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4393 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4394
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4395 return NGX_ERROR;
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4396 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4397
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4398 if (ngx_nonblocking(s) == -1) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4399 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4400 ngx_nonblocking_n " failed");
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4401
5582
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4402 goto failed;
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4403 }
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4404
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4405 rev = c->read;
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4406 wev = c->write;
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4407
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4408 rev->log = &rec->log;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4409 wev->log = &rec->log;
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4410
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4411 rec->udp = c;
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4412
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4413 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4414
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4415 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0,
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4416 "connect to %V, fd:%d #%uA", &rec->server, s, c->number);
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4417
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4418 rc = connect(s, rec->sockaddr, rec->socklen);
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4419
6125
4dc8e7b62216 Removed the obsolete aio module.
Ruslan Ermilov <ru@nginx.com>
parents: 5921
diff changeset
4420 /* TODO: iocp */
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4421
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4422 if (rc == -1) {
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4423 ngx_log_error(NGX_LOG_CRIT, &rec->log, ngx_socket_errno,
3408
71193a456616 add context to a resolver log
Igor Sysoev <igor@sysoev.ru>
parents: 3376
diff changeset
4424 "connect() failed");
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4425
5582
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4426 goto failed;
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4427 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4428
1649
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4429 /* UDP sockets are always ready to write */
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4430 wev->ready = 1;
80901499140d resolver
Igor Sysoev <igor@sysoev.ru>
parents: 583
diff changeset
4431
6126
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4432 event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ?
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4433 /* kqueue, epoll */ NGX_CLEAR_EVENT:
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4434 /* select, poll, /dev/poll */ NGX_LEVEL_EVENT;
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4435 /* eventport event type has no meaning: oneshot only */
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4436
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4437 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
adba26ff70b5 Removed the obsolete rtsig module.
Ruslan Ermilov <ru@nginx.com>
parents: 6125
diff changeset
4438 goto failed;
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4439 }
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4440
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4441 return NGX_OK;
5582
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4442
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4443 failed:
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4444
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4445 ngx_close_connection(c);
6365
d35b4d590b2d Resolver: renamed UDP-specific structures, fields and variables.
Roman Arutyunyan <arut@nginx.com>
parents: 6352
diff changeset
4446 rec->udp = NULL;
5582
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4447
545a4d393e2f Resolver: properly handle connect() failures.
Ruslan Ermilov <ru@nginx.com>
parents: 5505
diff changeset
4448 return NGX_ERROR;
583
4e296b7d25bf nginx-0.3.13-RELEASE import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4449 }
6367
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4450
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4451
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4452 ngx_int_t
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4453 ngx_tcp_connect(ngx_resolver_connection_t *rec)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4454 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4455 int rc;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4456 ngx_int_t event;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4457 ngx_err_t err;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4458 ngx_uint_t level;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4459 ngx_socket_t s;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4460 ngx_event_t *rev, *wev;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4461 ngx_connection_t *c;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4462
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4463 s = ngx_socket(rec->sockaddr->sa_family, SOCK_STREAM, 0);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4464
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4465 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "TCP socket %d", s);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4466
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4467 if (s == (ngx_socket_t) -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4468 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4469 ngx_socket_n " failed");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4470 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4471 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4472
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4473 c = ngx_get_connection(s, &rec->log);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4474
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4475 if (c == NULL) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4476 if (ngx_close_socket(s) == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4477 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4478 ngx_close_socket_n "failed");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4479 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4480
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4481 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4482 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4483
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4484 if (ngx_nonblocking(s) == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4485 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4486 ngx_nonblocking_n " failed");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4487
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4488 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4489 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4490
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4491 rev = c->read;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4492 wev = c->write;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4493
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4494 rev->log = &rec->log;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4495 wev->log = &rec->log;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4496
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4497 rec->tcp = c;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4498
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4499 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4500
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4501 if (ngx_add_conn) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4502 if (ngx_add_conn(c) == NGX_ERROR) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4503 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4504 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4505 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4506
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4507 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4508 "connect to %V, fd:%d #%uA", &rec->server, s, c->number);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4509
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4510 rc = connect(s, rec->sockaddr, rec->socklen);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4511
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4512 if (rc == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4513 err = ngx_socket_errno;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4514
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4515
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4516 if (err != NGX_EINPROGRESS
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4517 #if (NGX_WIN32)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4518 /* Winsock returns WSAEWOULDBLOCK (NGX_EAGAIN) */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4519 && err != NGX_EAGAIN
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4520 #endif
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4521 )
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4522 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4523 if (err == NGX_ECONNREFUSED
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4524 #if (NGX_LINUX)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4525 /*
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4526 * Linux returns EAGAIN instead of ECONNREFUSED
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4527 * for unix sockets if listen queue is full
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4528 */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4529 || err == NGX_EAGAIN
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4530 #endif
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4531 || err == NGX_ECONNRESET
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4532 || err == NGX_ENETDOWN
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4533 || err == NGX_ENETUNREACH
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4534 || err == NGX_EHOSTDOWN
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4535 || err == NGX_EHOSTUNREACH)
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4536 {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4537 level = NGX_LOG_ERR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4538
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4539 } else {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4540 level = NGX_LOG_CRIT;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4541 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4542
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4543 ngx_log_error(level, c->log, err, "connect() to %V failed",
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4544 &rec->server);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4545
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4546 ngx_close_connection(c);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4547 rec->tcp = NULL;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4548
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4549 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4550 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4551 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4552
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4553 if (ngx_add_conn) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4554 if (rc == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4555
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4556 /* NGX_EINPROGRESS */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4557
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4558 return NGX_AGAIN;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4559 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4560
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4561 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "connected");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4562
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4563 wev->ready = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4564
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4565 return NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4566 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4567
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4568 if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4569
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4570 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, ngx_socket_errno,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4571 "connect(): %d", rc);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4572
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4573 if (ngx_blocking(s) == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4574 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4575 ngx_blocking_n " failed");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4576 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4577 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4578
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4579 /*
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4580 * FreeBSD's aio allows to post an operation on non-connected socket.
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4581 * NT does not support it.
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4582 *
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4583 * TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4584 */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4585
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4586 rev->ready = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4587 wev->ready = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4588
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4589 return NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4590 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4591
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4592 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4593
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4594 /* kqueue */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4595
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4596 event = NGX_CLEAR_EVENT;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4597
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4598 } else {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4599
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4600 /* select, poll, /dev/poll */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4601
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4602 event = NGX_LEVEL_EVENT;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4603 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4604
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4605 if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4606 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4607 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4608
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4609 if (rc == -1) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4610
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4611 /* NGX_EINPROGRESS */
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4612
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4613 if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) {
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4614 goto failed;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4615 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4616
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4617 return NGX_AGAIN;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4618 }
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4619
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4620 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "connected");
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4621
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4622 wev->ready = 1;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4623
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4624 return NGX_OK;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4625
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4626 failed:
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4627
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4628 ngx_close_connection(c);
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4629 rec->tcp = NULL;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4630
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4631 return NGX_ERROR;
5a16d40c63de Resolver: TCP support.
Roman Arutyunyan <arut@nginx.com>
parents: 6366
diff changeset
4632 }
6458
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4633
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4634
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4635 static ngx_int_t
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4636 ngx_resolver_cmp_srvs(const void *one, const void *two)
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4637 {
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4638 ngx_int_t p1, p2;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4639 ngx_resolver_srv_t *first, *second;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4640
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4641 first = (ngx_resolver_srv_t *) one;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4642 second = (ngx_resolver_srv_t *) two;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4643
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4644 p1 = first->priority;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4645 p2 = second->priority;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4646
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4647 return p1 - p2;
384154fc634f Resolver: added support for SRV records.
Dmitry Volyntsev <xeioex@nginx.com>
parents: 6457
diff changeset
4648 }