Mercurial > hg > nginx
comparison src/core/ngx_resolver.c @ 6365:d35b4d590b2d
Resolver: renamed UDP-specific structures, fields and variables.
They will be used for TCP connections as well.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Thu, 28 Jan 2016 15:28:20 +0300 |
parents | ff9b32c0e141 |
children | 2e5c027f2a98 |
comparison
equal
deleted
inserted
replaced
6364:69977457e1a6 | 6365:d35b4d590b2d |
---|---|
51 #define ngx_resolver_node(n) \ | 51 #define ngx_resolver_node(n) \ |
52 (ngx_resolver_node_t *) \ | 52 (ngx_resolver_node_t *) \ |
53 ((u_char *) (n) - offsetof(ngx_resolver_node_t, node)) | 53 ((u_char *) (n) - offsetof(ngx_resolver_node_t, node)) |
54 | 54 |
55 | 55 |
56 ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc); | 56 ngx_int_t ngx_udp_connect(ngx_resolver_connection_t *rec); |
57 | 57 |
58 | 58 |
59 static void ngx_resolver_cleanup(void *data); | 59 static void ngx_resolver_cleanup(void *data); |
60 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree); | 60 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree); |
61 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, | 61 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, |
108 | 108 |
109 | 109 |
110 ngx_resolver_t * | 110 ngx_resolver_t * |
111 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) | 111 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) |
112 { | 112 { |
113 ngx_str_t s; | 113 ngx_str_t s; |
114 ngx_url_t u; | 114 ngx_url_t u; |
115 ngx_uint_t i, j; | 115 ngx_uint_t i, j; |
116 ngx_resolver_t *r; | 116 ngx_resolver_t *r; |
117 ngx_pool_cleanup_t *cln; | 117 ngx_pool_cleanup_t *cln; |
118 ngx_udp_connection_t *uc; | 118 ngx_resolver_connection_t *rec; |
119 | 119 |
120 cln = ngx_pool_cleanup_add(cf->pool, 0); | 120 cln = ngx_pool_cleanup_add(cf->pool, 0); |
121 if (cln == NULL) { | 121 if (cln == NULL) { |
122 return NULL; | 122 return NULL; |
123 } | 123 } |
170 | 170 |
171 r->log = &cf->cycle->new_log; | 171 r->log = &cf->cycle->new_log; |
172 r->log_level = NGX_LOG_ERR; | 172 r->log_level = NGX_LOG_ERR; |
173 | 173 |
174 if (n) { | 174 if (n) { |
175 if (ngx_array_init(&r->udp_connections, cf->pool, n, | 175 if (ngx_array_init(&r->connections, cf->pool, n, |
176 sizeof(ngx_udp_connection_t)) | 176 sizeof(ngx_resolver_connection_t)) |
177 != NGX_OK) | 177 != NGX_OK) |
178 { | 178 { |
179 return NULL; | 179 return NULL; |
180 } | 180 } |
181 } | 181 } |
228 } | 228 } |
229 | 229 |
230 return NULL; | 230 return NULL; |
231 } | 231 } |
232 | 232 |
233 uc = ngx_array_push_n(&r->udp_connections, u.naddrs); | 233 rec = ngx_array_push_n(&r->connections, u.naddrs); |
234 if (uc == NULL) { | 234 if (rec == NULL) { |
235 return NULL; | 235 return NULL; |
236 } | 236 } |
237 | 237 |
238 ngx_memzero(uc, u.naddrs * sizeof(ngx_udp_connection_t)); | 238 ngx_memzero(rec, u.naddrs * sizeof(ngx_resolver_connection_t)); |
239 | 239 |
240 for (j = 0; j < u.naddrs; j++) { | 240 for (j = 0; j < u.naddrs; j++) { |
241 uc[j].sockaddr = u.addrs[j].sockaddr; | 241 rec[j].sockaddr = u.addrs[j].sockaddr; |
242 uc[j].socklen = u.addrs[j].socklen; | 242 rec[j].socklen = u.addrs[j].socklen; |
243 uc[j].server = u.addrs[j].name; | 243 rec[j].server = u.addrs[j].name; |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 return r; | 247 return r; |
248 } | 248 } |
251 static void | 251 static void |
252 ngx_resolver_cleanup(void *data) | 252 ngx_resolver_cleanup(void *data) |
253 { | 253 { |
254 ngx_resolver_t *r = data; | 254 ngx_resolver_t *r = data; |
255 | 255 |
256 ngx_uint_t i; | 256 ngx_uint_t i; |
257 ngx_udp_connection_t *uc; | 257 ngx_resolver_connection_t *rec; |
258 | 258 |
259 if (r) { | 259 if (r) { |
260 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | 260 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, |
261 "cleanup resolver"); | 261 "cleanup resolver"); |
262 | 262 |
271 if (r->event) { | 271 if (r->event) { |
272 ngx_free(r->event); | 272 ngx_free(r->event); |
273 } | 273 } |
274 | 274 |
275 | 275 |
276 uc = r->udp_connections.elts; | 276 rec = r->connections.elts; |
277 | 277 |
278 for (i = 0; i < r->udp_connections.nelts; i++) { | 278 for (i = 0; i < r->connections.nelts; i++) { |
279 if (uc[i].connection) { | 279 if (rec[i].udp) { |
280 ngx_close_connection(uc[i].connection); | 280 ngx_close_connection(rec[i].udp); |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 ngx_free(r); | 284 ngx_free(r); |
285 } | 285 } |
338 | 338 |
339 return temp; | 339 return temp; |
340 } | 340 } |
341 } | 341 } |
342 | 342 |
343 if (r->udp_connections.nelts == 0) { | 343 if (r->connections.nelts == 0) { |
344 return NGX_NO_RESOLVER; | 344 return NGX_NO_RESOLVER; |
345 } | 345 } |
346 | 346 |
347 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); | 347 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); |
348 | 348 |
1092 | 1092 |
1093 | 1093 |
1094 static ngx_int_t | 1094 static ngx_int_t |
1095 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) | 1095 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) |
1096 { | 1096 { |
1097 ssize_t n; | 1097 ssize_t n; |
1098 ngx_udp_connection_t *uc; | 1098 ngx_resolver_connection_t *rec; |
1099 | 1099 |
1100 uc = r->udp_connections.elts; | 1100 rec = r->connections.elts; |
1101 | 1101 |
1102 uc = &uc[r->last_connection++]; | 1102 rec = &rec[r->last_connection++]; |
1103 if (r->last_connection == r->udp_connections.nelts) { | 1103 if (r->last_connection == r->connections.nelts) { |
1104 r->last_connection = 0; | 1104 r->last_connection = 0; |
1105 } | 1105 } |
1106 | 1106 |
1107 if (uc->connection == NULL) { | 1107 if (rec->udp == NULL) { |
1108 | 1108 |
1109 uc->log = *r->log; | 1109 rec->log = *r->log; |
1110 uc->log.handler = ngx_resolver_log_error; | 1110 rec->log.handler = ngx_resolver_log_error; |
1111 uc->log.data = uc; | 1111 rec->log.data = rec; |
1112 uc->log.action = "resolving"; | 1112 rec->log.action = "resolving"; |
1113 | 1113 |
1114 if (ngx_udp_connect(uc) != NGX_OK) { | 1114 if (ngx_udp_connect(rec) != NGX_OK) { |
1115 return NGX_ERROR; | 1115 return NGX_ERROR; |
1116 } | 1116 } |
1117 | 1117 |
1118 uc->connection->data = r; | 1118 rec->udp->data = r; |
1119 uc->connection->read->handler = ngx_resolver_read_response; | 1119 rec->udp->read->handler = ngx_resolver_read_response; |
1120 uc->connection->read->resolver = 1; | 1120 rec->udp->read->resolver = 1; |
1121 } | 1121 } |
1122 | 1122 |
1123 if (rn->naddrs == (u_short) -1) { | 1123 if (rn->naddrs == (u_short) -1) { |
1124 n = ngx_send(uc->connection, rn->query, rn->qlen); | 1124 n = ngx_send(rec->udp, rn->query, rn->qlen); |
1125 | 1125 |
1126 if (n == -1) { | 1126 if (n == -1) { |
1127 return NGX_ERROR; | 1127 return NGX_ERROR; |
1128 } | 1128 } |
1129 | 1129 |
1130 if ((size_t) n != (size_t) rn->qlen) { | 1130 if ((size_t) n != (size_t) rn->qlen) { |
1131 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete"); | 1131 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "send() incomplete"); |
1132 return NGX_ERROR; | 1132 return NGX_ERROR; |
1133 } | 1133 } |
1134 } | 1134 } |
1135 | 1135 |
1136 #if (NGX_HAVE_INET6) | 1136 #if (NGX_HAVE_INET6) |
1137 if (rn->query6 && rn->naddrs6 == (u_short) -1) { | 1137 if (rn->query6 && rn->naddrs6 == (u_short) -1) { |
1138 n = ngx_send(uc->connection, rn->query6, rn->qlen); | 1138 n = ngx_send(rec->udp, rn->query6, rn->qlen); |
1139 | 1139 |
1140 if (n == -1) { | 1140 if (n == -1) { |
1141 return NGX_ERROR; | 1141 return NGX_ERROR; |
1142 } | 1142 } |
1143 | 1143 |
1144 if ((size_t) n != (size_t) rn->qlen) { | 1144 if ((size_t) n != (size_t) rn->qlen) { |
1145 ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete"); | 1145 ngx_log_error(NGX_LOG_CRIT, &rec->log, 0, "send() incomplete"); |
1146 return NGX_ERROR; | 1146 return NGX_ERROR; |
1147 } | 1147 } |
1148 } | 1148 } |
1149 #endif | 1149 #endif |
1150 | 1150 |
3072 | 3072 |
3073 | 3073 |
3074 static u_char * | 3074 static u_char * |
3075 ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len) | 3075 ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len) |
3076 { | 3076 { |
3077 u_char *p; | 3077 u_char *p; |
3078 ngx_udp_connection_t *uc; | 3078 ngx_resolver_connection_t *rec; |
3079 | 3079 |
3080 p = buf; | 3080 p = buf; |
3081 | 3081 |
3082 if (log->action) { | 3082 if (log->action) { |
3083 p = ngx_snprintf(buf, len, " while %s", log->action); | 3083 p = ngx_snprintf(buf, len, " while %s", log->action); |
3084 len -= p - buf; | 3084 len -= p - buf; |
3085 } | 3085 } |
3086 | 3086 |
3087 uc = log->data; | 3087 rec = log->data; |
3088 | 3088 |
3089 if (uc) { | 3089 if (rec) { |
3090 p = ngx_snprintf(p, len, ", resolver: %V", &uc->server); | 3090 p = ngx_snprintf(p, len, ", resolver: %V", &rec->server); |
3091 } | 3091 } |
3092 | 3092 |
3093 return p; | 3093 return p; |
3094 } | 3094 } |
3095 | 3095 |
3096 | 3096 |
3097 ngx_int_t | 3097 ngx_int_t |
3098 ngx_udp_connect(ngx_udp_connection_t *uc) | 3098 ngx_udp_connect(ngx_resolver_connection_t *rec) |
3099 { | 3099 { |
3100 int rc; | 3100 int rc; |
3101 ngx_int_t event; | 3101 ngx_int_t event; |
3102 ngx_event_t *rev, *wev; | 3102 ngx_event_t *rev, *wev; |
3103 ngx_socket_t s; | 3103 ngx_socket_t s; |
3104 ngx_connection_t *c; | 3104 ngx_connection_t *c; |
3105 | 3105 |
3106 s = ngx_socket(uc->sockaddr->sa_family, SOCK_DGRAM, 0); | 3106 s = ngx_socket(rec->sockaddr->sa_family, SOCK_DGRAM, 0); |
3107 | 3107 |
3108 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s); | 3108 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "UDP socket %d", s); |
3109 | 3109 |
3110 if (s == (ngx_socket_t) -1) { | 3110 if (s == (ngx_socket_t) -1) { |
3111 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, | 3111 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
3112 ngx_socket_n " failed"); | 3112 ngx_socket_n " failed"); |
3113 return NGX_ERROR; | 3113 return NGX_ERROR; |
3114 } | 3114 } |
3115 | 3115 |
3116 c = ngx_get_connection(s, &uc->log); | 3116 c = ngx_get_connection(s, &rec->log); |
3117 | 3117 |
3118 if (c == NULL) { | 3118 if (c == NULL) { |
3119 if (ngx_close_socket(s) == -1) { | 3119 if (ngx_close_socket(s) == -1) { |
3120 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, | 3120 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
3121 ngx_close_socket_n "failed"); | 3121 ngx_close_socket_n "failed"); |
3122 } | 3122 } |
3123 | 3123 |
3124 return NGX_ERROR; | 3124 return NGX_ERROR; |
3125 } | 3125 } |
3126 | 3126 |
3127 if (ngx_nonblocking(s) == -1) { | 3127 if (ngx_nonblocking(s) == -1) { |
3128 ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno, | 3128 ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, |
3129 ngx_nonblocking_n " failed"); | 3129 ngx_nonblocking_n " failed"); |
3130 | 3130 |
3131 goto failed; | 3131 goto failed; |
3132 } | 3132 } |
3133 | 3133 |
3134 rev = c->read; | 3134 rev = c->read; |
3135 wev = c->write; | 3135 wev = c->write; |
3136 | 3136 |
3137 rev->log = &uc->log; | 3137 rev->log = &rec->log; |
3138 wev->log = &uc->log; | 3138 wev->log = &rec->log; |
3139 | 3139 |
3140 uc->connection = c; | 3140 rec->udp = c; |
3141 | 3141 |
3142 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); | 3142 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); |
3143 | 3143 |
3144 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &uc->log, 0, | 3144 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0, |
3145 "connect to %V, fd:%d #%uA", &uc->server, s, c->number); | 3145 "connect to %V, fd:%d #%uA", &rec->server, s, c->number); |
3146 | 3146 |
3147 rc = connect(s, uc->sockaddr, uc->socklen); | 3147 rc = connect(s, rec->sockaddr, rec->socklen); |
3148 | 3148 |
3149 /* TODO: iocp */ | 3149 /* TODO: iocp */ |
3150 | 3150 |
3151 if (rc == -1) { | 3151 if (rc == -1) { |
3152 ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno, | 3152 ngx_log_error(NGX_LOG_CRIT, &rec->log, ngx_socket_errno, |
3153 "connect() failed"); | 3153 "connect() failed"); |
3154 | 3154 |
3155 goto failed; | 3155 goto failed; |
3156 } | 3156 } |
3157 | 3157 |
3170 return NGX_OK; | 3170 return NGX_OK; |
3171 | 3171 |
3172 failed: | 3172 failed: |
3173 | 3173 |
3174 ngx_close_connection(c); | 3174 ngx_close_connection(c); |
3175 uc->connection = NULL; | 3175 rec->udp = NULL; |
3176 | 3176 |
3177 return NGX_ERROR; | 3177 return NGX_ERROR; |
3178 } | 3178 } |