[nginx] Changed resolver API to use ngx_addr_t.
Ruslan Ermilov
ru at nginx.com
Fri Dec 13 16:54:46 UTC 2013
details: http://hg.nginx.org/nginx/rev/07dd5bd222ac
branches:
changeset: 5475:07dd5bd222ac
user: Ruslan Ermilov <ru at nginx.com>
date: Fri Dec 06 14:30:27 2013 +0400
description:
Changed resolver API to use ngx_addr_t.
diffstat:
src/core/ngx_resolver.c | 240 +++++++++++++++++++-----------
src/core/ngx_resolver.h | 5 +-
src/event/ngx_event_openssl_stapling.c | 56 ++++--
src/http/ngx_http_upstream.c | 16 +-
src/http/ngx_http_upstream.h | 2 +-
src/http/ngx_http_upstream_round_robin.c | 40 +++--
src/mail/ngx_mail_smtp_handler.c | 43 ++--
7 files changed, 245 insertions(+), 157 deletions(-)
diffs (truncated from 738 to 300 lines):
diff -r b43b02bb54db -r 07dd5bd222ac src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c Fri Dec 13 19:22:44 2013 +0400
+++ b/src/core/ngx_resolver.c Fri Dec 06 14:30:27 2013 +0400
@@ -88,8 +88,8 @@ static void *ngx_resolver_calloc(ngx_res
static void ngx_resolver_free(ngx_resolver_t *r, void *p);
static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p);
static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size);
-static in_addr_t *ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src,
- ngx_uint_t n);
+static ngx_addr_t *ngx_resolver_export(ngx_resolver_t *r, in_addr_t *src,
+ ngx_uint_t n, ngx_uint_t rotate);
static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
@@ -281,7 +281,11 @@ ngx_resolve_start(ngx_resolver_t *r, ngx
temp->state = NGX_OK;
temp->naddrs = 1;
temp->addrs = &temp->addr;
- temp->addr = addr;
+ temp->addr.sockaddr = (struct sockaddr *) &temp->sin;
+ temp->addr.socklen = sizeof(struct sockaddr_in);
+ ngx_memzero(&temp->sin, sizeof(struct sockaddr_in));
+ temp->sin.sin_family = AF_INET;
+ temp->sin.sin_addr.s_addr = addr;
temp->quick = 1;
return temp;
@@ -417,9 +421,9 @@ static ngx_int_t
ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
{
uint32_t hash;
- in_addr_t addr, *addrs;
ngx_int_t rc;
ngx_uint_t naddrs;
+ ngx_addr_t *addrs;
ngx_resolver_ctx_t *next;
ngx_resolver_node_t *rn;
@@ -445,16 +449,14 @@ ngx_resolve_name_locked(ngx_resolver_t *
/* NGX_RESOLVE_A answer */
- if (naddrs != 1) {
- addr = 0;
- addrs = ngx_resolver_rotate(r, rn->u.addrs, naddrs);
+ if (naddrs == 1) {
+ addrs = NULL;
+
+ } else {
+ addrs = ngx_resolver_export(r, rn->u.addrs, naddrs, 1);
if (addrs == NULL) {
return NGX_ERROR;
}
-
- } else {
- addr = rn->u.addr;
- addrs = NULL;
}
ctx->next = rn->waiting;
@@ -465,8 +467,19 @@ ngx_resolve_name_locked(ngx_resolver_t *
do {
ctx->state = NGX_OK;
ctx->naddrs = naddrs;
- ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs;
- ctx->addr = addr;
+
+ if (addrs == NULL) {
+ ctx->addrs = &ctx->addr;
+ ctx->addr.sockaddr = (struct sockaddr *) &ctx->sin;
+ ctx->addr.socklen = sizeof(struct sockaddr_in);
+ ngx_memzero(&ctx->sin, sizeof(struct sockaddr_in));
+ ctx->sin.sin_family = AF_INET;
+ ctx->sin.sin_addr.s_addr = rn->u.addr;
+
+ } else {
+ ctx->addrs = addrs;
+ }
+
next = ctx->next;
ctx->handler(ctx);
@@ -474,7 +487,8 @@ ngx_resolve_name_locked(ngx_resolver_t *
ctx = next;
} while (ctx);
- if (addrs) {
+ if (addrs != NULL) {
+ ngx_resolver_free(r, addrs->sockaddr);
ngx_resolver_free(r, addrs);
}
@@ -626,20 +640,29 @@ failed:
}
+/* AF_INET only */
+
ngx_int_t
ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
{
u_char *name;
+ in_addr_t addr;
ngx_resolver_t *r;
+ struct sockaddr_in *sin;
ngx_resolver_node_t *rn;
r = ctx->resolver;
- ctx->addr = ntohl(ctx->addr);
+ if (ctx->addr.sockaddr->sa_family != AF_INET) {
+ return NGX_ERROR;
+ }
+
+ sin = (struct sockaddr_in *) ctx->addr.sockaddr;
+ addr = ntohl(sin->sin_addr.s_addr);
/* lock addr mutex */
- rn = ngx_resolver_lookup_addr(r, ctx->addr);
+ rn = ngx_resolver_lookup_addr(r, addr);
if (rn) {
@@ -694,7 +717,7 @@ ngx_resolve_addr(ngx_resolver_ctx_t *ctx
goto failed;
}
- rn->node.key = ctx->addr;
+ rn->node.key = addr;
rn->query = NULL;
ngx_rbtree_insert(&r->addr_rbtree, &rn->node);
@@ -765,14 +788,24 @@ failed:
}
+/* AF_INET only */
+
void
ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
{
in_addr_t addr;
ngx_resolver_t *r;
ngx_resolver_ctx_t *w, **p;
+ struct sockaddr_in *sin;
ngx_resolver_node_t *rn;
+ if (ctx->addr.sockaddr->sa_family != AF_INET) {
+ return;
+ }
+
+ sin = (struct sockaddr_in *) ctx->addr.sockaddr;
+ addr = ntohl(sin->sin_addr.s_addr);
+
r = ctx->resolver;
ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
@@ -786,7 +819,7 @@ ngx_resolve_addr_done(ngx_resolver_ctx_t
if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) {
- rn = ngx_resolver_lookup_addr(r, ctx->addr);
+ rn = ngx_resolver_lookup_addr(r, addr);
if (rn) {
p = &rn->waiting;
@@ -804,8 +837,6 @@ ngx_resolve_addr_done(ngx_resolver_ctx_t
}
}
- addr = ntohl(ctx->addr);
-
ngx_log_error(NGX_LOG_ALERT, r->log, 0,
"could not cancel %ud.%ud.%ud.%ud resolving",
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
@@ -1177,8 +1208,9 @@ ngx_resolver_process_a(ngx_resolver_t *r
size_t len;
int32_t ttl;
uint32_t hash;
- in_addr_t addr, *addrs;
+ in_addr_t *addr;
ngx_str_t name;
+ ngx_addr_t *addrs;
ngx_uint_t type, class, qident, naddrs, a, i, n, start;
ngx_resolver_an_t *an;
ngx_resolver_ctx_t *ctx, *next;
@@ -1247,8 +1279,6 @@ ngx_resolver_process_a(ngx_resolver_t *r
i = ans;
naddrs = 0;
- addr = 0;
- addrs = NULL;
cname = NULL;
ttl = 0;
@@ -1319,9 +1349,6 @@ ngx_resolver_process_a(ngx_resolver_t *r
goto short_response;
}
- addr = htonl((buf[i] << 24) + (buf[i + 1] << 16)
- + (buf[i + 2] << 8) + (buf[i + 3]));
-
naddrs++;
break;
@@ -1352,66 +1379,69 @@ ngx_resolver_process_a(ngx_resolver_t *r
if (naddrs) {
if (naddrs == 1) {
- rn->u.addr = addr;
+ addr = &rn->u.addr;
+ rn->naddrs = 1;
} else {
-
- addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
- if (addrs == NULL) {
+ addr = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
+ if (addr == NULL) {
goto failed;
}
- n = 0;
- i = ans;
-
- for (a = 0; a < nan; a++) {
-
- for ( ;; ) {
-
- if (buf[i] & 0xc0) {
- i += 2;
- break;
- }
-
- if (buf[i] == 0) {
- i++;
- break;
- }
-
- i += 1 + buf[i];
+ rn->u.addrs = addr;
+ rn->naddrs = (u_short) naddrs;
+ }
+
+ n = 0;
+ i = ans;
+
+ for (a = 0; a < nan; a++) {
+
+ for ( ;; ) {
+
+ if (buf[i] & 0xc0) {
+ i += 2;
+ break;
}
- an = (ngx_resolver_an_t *) &buf[i];
-
- type = (an->type_hi << 8) + an->type_lo;
- len = (an->len_hi << 8) + an->len_lo;
-
- i += sizeof(ngx_resolver_an_t);
-
- if (type == NGX_RESOLVE_A) {
-
- addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
- + (buf[i + 2] << 8) + (buf[i + 3]));
-
- if (n == naddrs) {
- break;
- }
+ if (buf[i] == 0) {
+ i++;
+ break;
}
- i += len;
+ i += 1 + buf[i];
}
- rn->u.addrs = addrs;
-
- addrs = ngx_resolver_dup(r, rn->u.addrs,
- naddrs * sizeof(in_addr_t));
+ an = (ngx_resolver_an_t *) &buf[i];
+
+ type = (an->type_hi << 8) + an->type_lo;
+ len = (an->len_hi << 8) + an->len_lo;
+
+ i += sizeof(ngx_resolver_an_t);
+
+ if (type == NGX_RESOLVE_A) {
+
+ addr[n] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
+ + (buf[i + 2] << 8) + (buf[i + 3]));
+
+ if (++n == naddrs) {
+ break;
+ }
More information about the nginx-devel
mailing list