Changed resolver API to use ngx_addr_t.

This commit is contained in:
Ruslan Ermilov 2013-12-06 14:30:27 +04:00
parent 552e15ba1c
commit 3aeefbcaea
7 changed files with 238 additions and 150 deletions

View File

@ -88,8 +88,8 @@ static void *ngx_resolver_calloc(ngx_resolver_t *r, size_t size);
static void ngx_resolver_free(ngx_resolver_t *r, void *p); 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_free_locked(ngx_resolver_t *r, void *p);
static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); 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, static ngx_addr_t *ngx_resolver_export(ngx_resolver_t *r, in_addr_t *src,
ngx_uint_t n); 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); 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_resolver_ctx_t *temp)
temp->state = NGX_OK; temp->state = NGX_OK;
temp->naddrs = 1; temp->naddrs = 1;
temp->addrs = &temp->addr; 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; temp->quick = 1;
return temp; return temp;
@ -417,9 +421,9 @@ static ngx_int_t
ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx) ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
{ {
uint32_t hash; uint32_t hash;
in_addr_t addr, *addrs;
ngx_int_t rc; ngx_int_t rc;
ngx_uint_t naddrs; ngx_uint_t naddrs;
ngx_addr_t *addrs;
ngx_resolver_ctx_t *next; ngx_resolver_ctx_t *next;
ngx_resolver_node_t *rn; ngx_resolver_node_t *rn;
@ -445,16 +449,14 @@ ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
/* NGX_RESOLVE_A answer */ /* NGX_RESOLVE_A answer */
if (naddrs != 1) { if (naddrs == 1) {
addr = 0; addrs = NULL;
addrs = ngx_resolver_rotate(r, rn->u.addrs, naddrs);
} else {
addrs = ngx_resolver_export(r, rn->u.addrs, naddrs, 1);
if (addrs == NULL) { if (addrs == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
} else {
addr = rn->u.addr;
addrs = NULL;
} }
ctx->next = rn->waiting; ctx->next = rn->waiting;
@ -465,8 +467,19 @@ ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
do { do {
ctx->state = NGX_OK; ctx->state = NGX_OK;
ctx->naddrs = naddrs; 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; next = ctx->next;
ctx->handler(ctx); ctx->handler(ctx);
@ -474,7 +487,8 @@ ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
ctx = next; ctx = next;
} while (ctx); } while (ctx);
if (addrs) { if (addrs != NULL) {
ngx_resolver_free(r, addrs->sockaddr);
ngx_resolver_free(r, addrs); ngx_resolver_free(r, addrs);
} }
@ -626,20 +640,29 @@ failed:
} }
/* AF_INET only */
ngx_int_t ngx_int_t
ngx_resolve_addr(ngx_resolver_ctx_t *ctx) ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
{ {
u_char *name; u_char *name;
in_addr_t addr;
ngx_resolver_t *r; ngx_resolver_t *r;
struct sockaddr_in *sin;
ngx_resolver_node_t *rn; ngx_resolver_node_t *rn;
r = ctx->resolver; 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 */ /* lock addr mutex */
rn = ngx_resolver_lookup_addr(r, ctx->addr); rn = ngx_resolver_lookup_addr(r, addr);
if (rn) { if (rn) {
@ -694,7 +717,7 @@ ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
goto failed; goto failed;
} }
rn->node.key = ctx->addr; rn->node.key = addr;
rn->query = NULL; rn->query = NULL;
ngx_rbtree_insert(&r->addr_rbtree, &rn->node); ngx_rbtree_insert(&r->addr_rbtree, &rn->node);
@ -765,14 +788,24 @@ failed:
} }
/* AF_INET only */
void void
ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx) ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
{ {
in_addr_t addr; in_addr_t addr;
ngx_resolver_t *r; ngx_resolver_t *r;
ngx_resolver_ctx_t *w, **p; ngx_resolver_ctx_t *w, **p;
struct sockaddr_in *sin;
ngx_resolver_node_t *rn; 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; r = ctx->resolver;
ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_CORE, r->log, 0,
@ -786,7 +819,7 @@ ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { 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) { if (rn) {
p = &rn->waiting; p = &rn->waiting;
@ -804,8 +837,6 @@ ngx_resolve_addr_done(ngx_resolver_ctx_t *ctx)
} }
} }
addr = ntohl(ctx->addr);
ngx_log_error(NGX_LOG_ALERT, r->log, 0, ngx_log_error(NGX_LOG_ALERT, r->log, 0,
"could not cancel %ud.%ud.%ud.%ud resolving", "could not cancel %ud.%ud.%ud.%ud resolving",
(addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff, (addr >> 16) & 0xff,
@ -1177,8 +1208,9 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
size_t len; size_t len;
int32_t ttl; int32_t ttl;
uint32_t hash; uint32_t hash;
in_addr_t addr, *addrs; in_addr_t *addr;
ngx_str_t name; ngx_str_t name;
ngx_addr_t *addrs;
ngx_uint_t type, class, qident, naddrs, a, i, n, start; ngx_uint_t type, class, qident, naddrs, a, i, n, start;
ngx_resolver_an_t *an; ngx_resolver_an_t *an;
ngx_resolver_ctx_t *ctx, *next; ngx_resolver_ctx_t *ctx, *next;
@ -1247,8 +1279,6 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
i = ans; i = ans;
naddrs = 0; naddrs = 0;
addr = 0;
addrs = NULL;
cname = NULL; cname = NULL;
ttl = 0; ttl = 0;
@ -1319,9 +1349,6 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
goto short_response; goto short_response;
} }
addr = htonl((buf[i] << 24) + (buf[i + 1] << 16)
+ (buf[i + 2] << 8) + (buf[i + 3]));
naddrs++; naddrs++;
break; break;
@ -1352,15 +1379,19 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
if (naddrs) { if (naddrs) {
if (naddrs == 1) { if (naddrs == 1) {
rn->u.addr = addr; addr = &rn->u.addr;
rn->naddrs = 1;
} else { } else {
addr = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t));
addrs = ngx_resolver_alloc(r, naddrs * sizeof(in_addr_t)); if (addr == NULL) {
if (addrs == NULL) {
goto failed; goto failed;
} }
rn->u.addrs = addr;
rn->naddrs = (u_short) naddrs;
}
n = 0; n = 0;
i = ans; i = ans;
@ -1390,10 +1421,10 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
if (type == NGX_RESOLVE_A) { if (type == NGX_RESOLVE_A) {
addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16) addr[n] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
+ (buf[i + 2] << 8) + (buf[i + 3])); + (buf[i + 2] << 8) + (buf[i + 3]));
if (n == naddrs) { if (++n == naddrs) {
break; break;
} }
} }
@ -1401,17 +1432,16 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
i += len; i += len;
} }
rn->u.addrs = addrs; if (naddrs == 1) {
addrs = NULL;
addrs = ngx_resolver_dup(r, rn->u.addrs, } else {
naddrs * sizeof(in_addr_t)); addrs = ngx_resolver_export(r, rn->u.addrs, naddrs, 0);
if (addrs == NULL) { if (addrs == NULL) {
goto failed; goto failed;
} }
} }
rn->naddrs = (u_short) naddrs;
ngx_queue_remove(&rn->queue); ngx_queue_remove(&rn->queue);
rn->valid = ngx_time() + (r->valid ? r->valid : ttl); rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
@ -1428,14 +1458,26 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
ctx = next; ctx = next;
ctx->state = NGX_OK; ctx->state = NGX_OK;
ctx->naddrs = naddrs; 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; next = ctx->next;
ctx->handler(ctx); ctx->handler(ctx);
} }
if (naddrs > 1) { if (addrs != NULL) {
ngx_resolver_free(r, addrs->sockaddr);
ngx_resolver_free(r, addrs); ngx_resolver_free(r, addrs);
} }
@ -1918,9 +1960,15 @@ ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
{ {
u_char *p, *d; u_char *p, *d;
size_t len; size_t len;
in_addr_t addr;
ngx_int_t n; ngx_int_t n;
ngx_uint_t ident; ngx_uint_t ident;
ngx_resolver_hdr_t *query; ngx_resolver_hdr_t *query;
struct sockaddr_in *sin;
if (ctx->addr.sockaddr->sa_family != AF_INET) {
return NGX_ERROR;
}
len = sizeof(ngx_resolver_hdr_t) len = sizeof(ngx_resolver_hdr_t)
+ sizeof(".255.255.255.255.in-addr.arpa.") - 1 + sizeof(".255.255.255.255.in-addr.arpa.") - 1
@ -1950,8 +1998,11 @@ ngx_resolver_create_addr_query(ngx_resolver_node_t *rn, ngx_resolver_ctx_t *ctx)
p += sizeof(ngx_resolver_hdr_t); p += sizeof(ngx_resolver_hdr_t);
sin = (struct sockaddr_in *) ctx->addr.sockaddr;
addr = ntohl(sin->sin_addr.s_addr);
for (n = 0; n < 32; n += 8) { for (n = 0; n < 32; n += 8) {
d = ngx_sprintf(&p[1], "%ud", (ctx->addr >> n) & 0xff); d = ngx_sprintf(&p[1], "%ud", (addr >> n) & 0xff);
*p = (u_char) (d - &p[1]); *p = (u_char) (d - &p[1]);
p = d; p = d;
} }
@ -2167,27 +2218,38 @@ ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size)
} }
static in_addr_t * static ngx_addr_t *
ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src, ngx_uint_t n) ngx_resolver_export(ngx_resolver_t *r, in_addr_t *src, ngx_uint_t n,
ngx_uint_t rotate)
{ {
void *dst, *p; ngx_addr_t *dst;
ngx_uint_t j; ngx_uint_t i, j;
struct sockaddr_in *sin;
dst = ngx_resolver_alloc(r, n * sizeof(in_addr_t));
dst = ngx_resolver_calloc(r, n * sizeof(ngx_addr_t));
if (dst == NULL) { if (dst == NULL) {
return dst; return NULL;
} }
j = ngx_random() % n; sin = ngx_resolver_calloc(r, n * sizeof(struct sockaddr_in));
if (j == 0) { if (sin == NULL) {
ngx_memcpy(dst, src, n * sizeof(in_addr_t)); ngx_resolver_free(r, dst);
return dst; return NULL;
} }
p = ngx_cpymem(dst, &src[j], (n - j) * sizeof(in_addr_t)); j = rotate ? ngx_random() % n : 0;
ngx_memcpy(p, src, j * sizeof(in_addr_t));
for (i = 0; i < n; i++) {
dst[i].sockaddr = (struct sockaddr *) &sin[i];
dst[i].socklen = sizeof(struct sockaddr_in);
sin[i].sin_family = AF_INET;
sin[i].sin_addr.s_addr = src[j++];
if (j == n) {
j = 0;
}
}
return dst; return dst;
} }

View File

@ -121,8 +121,9 @@ struct ngx_resolver_ctx_s {
ngx_str_t name; ngx_str_t name;
ngx_uint_t naddrs; ngx_uint_t naddrs;
in_addr_t *addrs; ngx_addr_t *addrs;
in_addr_t addr; ngx_addr_t addr;
struct sockaddr_in sin;
ngx_resolver_handler_pt handler; ngx_resolver_handler_pt handler;
void *data; void *data;

View File

@ -819,8 +819,9 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
u_char *p; u_char *p;
size_t len; size_t len;
in_port_t port; in_port_t port;
socklen_t socklen;
ngx_uint_t i; ngx_uint_t i;
struct sockaddr_in *sin; struct sockaddr *sockaddr;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ctx->log, 0, ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ctx->log, 0,
"ssl ocsp resolve handler"); "ssl ocsp resolve handler");
@ -835,15 +836,19 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
#if (NGX_DEBUG) #if (NGX_DEBUG)
{ {
in_addr_t addr; u_char text[NGX_SOCKADDR_STRLEN];
ngx_str_t addr;
addr.data = text;
for (i = 0; i < resolve->naddrs; i++) { for (i = 0; i < resolve->naddrs; i++) {
addr = ntohl(resolve->addrs[i]); addr.len = ngx_sock_ntop(resolve->addrs[i].sockaddr,
resolve->addrs[i].socklen,
text, NGX_SOCKADDR_STRLEN, 0);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ctx->log, 0,
"name was resolved to %V", &addr);
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, ctx->log, 0,
"name was resolved to %ud.%ud.%ud.%ud",
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
} }
} }
#endif #endif
@ -859,27 +864,34 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
for (i = 0; i < resolve->naddrs; i++) { for (i = 0; i < resolve->naddrs; i++) {
sin = ngx_pcalloc(ctx->pool, sizeof(struct sockaddr_in)); socklen = resolve->addrs[i].socklen;
if (sin == NULL) {
sockaddr = ngx_palloc(ctx->pool, socklen);
if (sockaddr == NULL) {
goto failed; goto failed;
} }
sin->sin_family = AF_INET; ngx_memcpy(sockaddr, resolve->addrs[i].sockaddr, socklen);
sin->sin_port = port;
sin->sin_addr.s_addr = resolve->addrs[i];
ctx->addrs[i].sockaddr = (struct sockaddr *) sin; switch (sockaddr->sa_family) {
ctx->addrs[i].socklen = sizeof(struct sockaddr_in); #if (NGX_HAVE_INET6)
case AF_INET6:
((struct sockaddr_in6 *) sockaddr)->sin6_port = port;
break;
#endif
default: /* AF_INET */
((struct sockaddr_in *) sockaddr)->sin_port = port;
}
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1; ctx->addrs[i].sockaddr = sockaddr;
ctx->addrs[i].socklen = socklen;
p = ngx_pnalloc(ctx->pool, len); p = ngx_pnalloc(ctx->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) { if (p == NULL) {
goto failed; goto failed;
} }
len = ngx_sock_ntop((struct sockaddr *) sin, sizeof(struct sockaddr_in), len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
p, len, 1);
ctx->addrs[i].name.len = len; ctx->addrs[i].name.len = len;
ctx->addrs[i].name.data = p; ctx->addrs[i].name.data = p;

View File

@ -918,16 +918,18 @@ ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
#if (NGX_DEBUG) #if (NGX_DEBUG)
{ {
in_addr_t addr; u_char text[NGX_SOCKADDR_STRLEN];
ngx_str_t addr;
ngx_uint_t i; ngx_uint_t i;
for (i = 0; i < ctx->naddrs; i++) { addr.data = text;
addr = ntohl(ur->addrs[i]);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, for (i = 0; i < ctx->naddrs; i++) {
"name was resolved to %ud.%ud.%ud.%ud", addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,
(addr >> 24) & 0xff, (addr >> 16) & 0xff, text, NGX_SOCKADDR_STRLEN, 0);
(addr >> 8) & 0xff, addr & 0xff);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"name was resolved to %V", &addr);
} }
} }
#endif #endif

View File

@ -256,7 +256,7 @@ typedef struct {
ngx_uint_t no_port; /* unsigned no_port:1 */ ngx_uint_t no_port; /* unsigned no_port:1 */
ngx_uint_t naddrs; ngx_uint_t naddrs;
in_addr_t *addrs; ngx_addr_t *addrs;
struct sockaddr *sockaddr; struct sockaddr *sockaddr;
socklen_t socklen; socklen_t socklen;

View File

@ -266,8 +266,9 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
{ {
u_char *p; u_char *p;
size_t len; size_t len;
socklen_t socklen;
ngx_uint_t i, n; ngx_uint_t i, n;
struct sockaddr_in *sin; struct sockaddr *sockaddr;
ngx_http_upstream_rr_peers_t *peers; ngx_http_upstream_rr_peers_t *peers;
ngx_http_upstream_rr_peer_data_t *rrp; ngx_http_upstream_rr_peer_data_t *rrp;
@ -306,27 +307,34 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
for (i = 0; i < ur->naddrs; i++) { for (i = 0; i < ur->naddrs; i++) {
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1; socklen = ur->addrs[i].socklen;
p = ngx_pnalloc(r->pool, len); sockaddr = ngx_palloc(r->pool, socklen);
if (sockaddr == NULL) {
return NGX_ERROR;
}
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
switch (sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
break;
#endif
default: /* AF_INET */
((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
}
p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) { if (p == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN); len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in)); peers->peer[i].sockaddr = sockaddr;
if (sin == NULL) { peers->peer[i].socklen = socklen;
return NGX_ERROR;
}
sin->sin_family = AF_INET;
sin->sin_port = htons(ur->port);
sin->sin_addr.s_addr = ur->addrs[i];
peers->peer[i].sockaddr = (struct sockaddr *) sin;
peers->peer[i].socklen = sizeof(struct sockaddr_in);
peers->peer[i].name.len = len; peers->peer[i].name.len = len;
peers->peer[i].name.data = p; peers->peer[i].name.data = p;
peers->peer[i].weight = 1; peers->peer[i].weight = 1;

View File

@ -55,7 +55,6 @@ static ngx_str_t smtp_tempunavail = ngx_string("[TEMPUNAVAIL]");
void void
ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c) ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
{ {
struct sockaddr_in *sin;
ngx_resolver_ctx_t *ctx; ngx_resolver_ctx_t *ctx;
ngx_mail_core_srv_conf_t *cscf; ngx_mail_core_srv_conf_t *cscf;
@ -67,11 +66,13 @@ ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
return; return;
} }
if (c->sockaddr->sa_family != AF_INET) { #if (NGX_HAVE_UNIX_DOMAIN)
if (c->sockaddr->sa_family == AF_UNIX) {
s->host = smtp_tempunavail; s->host = smtp_tempunavail;
ngx_mail_smtp_greeting(s, c); ngx_mail_smtp_greeting(s, c);
return; return;
} }
#endif
c->log->action = "in resolving client address"; c->log->action = "in resolving client address";
@ -81,11 +82,8 @@ ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
return; return;
} }
/* AF_INET only */ ctx->addr.sockaddr = c->sockaddr;
ctx->addr.socklen = c->socklen;
sin = (struct sockaddr_in *) c->sockaddr;
ctx->addr = sin->sin_addr.s_addr;
ctx->handler = ngx_mail_smtp_resolve_addr_handler; ctx->handler = ngx_mail_smtp_resolve_addr_handler;
ctx->data = s; ctx->data = s;
ctx->timeout = cscf->resolver_timeout; ctx->timeout = cscf->resolver_timeout;
@ -181,10 +179,8 @@ ngx_mail_smtp_resolve_name(ngx_event_t *rev)
static void static void
ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx) ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx)
{ {
in_addr_t addr;
ngx_uint_t i; ngx_uint_t i;
ngx_connection_t *c; ngx_connection_t *c;
struct sockaddr_in *sin;
ngx_mail_session_t *s; ngx_mail_session_t *s;
s = ctx->data; s = ctx->data;
@ -205,22 +201,29 @@ ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx)
} else { } else {
/* AF_INET only */ #if (NGX_DEBUG)
{
u_char text[NGX_SOCKADDR_STRLEN];
ngx_str_t addr;
sin = (struct sockaddr_in *) c->sockaddr; addr.data = text;
for (i = 0; i < ctx->naddrs; i++) { for (i = 0; i < ctx->naddrs; i++) {
addr.len = ngx_sock_ntop(ctx->addrs[i].sockaddr,
ctx->addrs[i].socklen,
text, NGX_SOCKADDR_STRLEN, 0);
addr = ctx->addrs[i]; ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
"name was resolved to %V", &addr);
}
}
#endif
ngx_log_debug4(NGX_LOG_DEBUG_MAIL, c->log, 0, for (i = 0; i < ctx->naddrs; i++) {
"name was resolved to %ud.%ud.%ud.%ud", if (ngx_cmp_sockaddr(ctx->addrs[i].sockaddr, ctx->addrs[i].socklen,
(ntohl(addr) >> 24) & 0xff, c->sockaddr, c->socklen, 0)
(ntohl(addr) >> 16) & 0xff, == NGX_OK)
(ntohl(addr) >> 8) & 0xff, {
ntohl(addr) & 0xff);
if (addr == sin->sin_addr.s_addr) {
goto found; goto found;
} }
} }