Core: extended ngx_sock_ntop() with socklen parameter.

On Linux, sockaddr length is required to process unix socket addresses properly
due to unnamed sockets (which don't have sun_path set at all) and abstract
namespace sockets.
This commit is contained in:
Vladimir Homutov 2013-07-11 16:07:25 +04:00
parent 050f74b751
commit af18946d76
10 changed files with 39 additions and 19 deletions

View File

@ -41,7 +41,7 @@ ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
ls->sockaddr = sa;
ls->socklen = socklen;
len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
len = ngx_sock_ntop(sa, socklen, text, NGX_SOCKADDR_STRLEN, 1);
ls->addr_text.len = len;
switch (ls->sockaddr->sa_family) {
@ -152,7 +152,8 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
return NGX_ERROR;
}
len = ngx_sock_ntop(ls[i].sockaddr, ls[i].addr_text.data, len, 1);
len = ngx_sock_ntop(ls[i].sockaddr, ls[i].socklen,
ls[i].addr_text.data, len, 1);
if (len == 0) {
return NGX_ERROR;
}
@ -1068,7 +1069,7 @@ ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
return NGX_OK;
}
s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, port);
s->len = ngx_sock_ntop(c->local_sockaddr, len, s->data, s->len, port);
return NGX_OK;
}

View File

@ -174,7 +174,8 @@ ngx_inet6_addr(u_char *p, size_t len, u_char *addr)
size_t
ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port)
ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text, size_t len,
ngx_uint_t port)
{
u_char *p;
struct sockaddr_in *sin;
@ -230,9 +231,18 @@ ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port)
case AF_UNIX:
saun = (struct sockaddr_un *) sa;
/* on Linux sockaddr might not include sun_path at all */
if (socklen <= offsetof(struct sockaddr_un, sun_path)) {
p = ngx_snprintf(text, len, "unix:%Z");
} else {
p = ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path);
}
/* we do not include trailing zero in address length */
return ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path) - text - 1;
return (p - text - 1);
#endif
@ -1020,7 +1030,7 @@ ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
goto failed;
}
len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
len = ngx_sock_ntop((struct sockaddr *) sin, rp->ai_addrlen, p, len, 1);
u->addrs[i].name.len = len;
u->addrs[i].name.data = p;
@ -1053,7 +1063,8 @@ ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
goto failed;
}
len = ngx_sock_ntop((struct sockaddr *) sin6, p, len, 1);
len = ngx_sock_ntop((struct sockaddr *) sin6, rp->ai_addrlen, p,
len, 1);
u->addrs[i].name.len = len;
u->addrs[i].name.data = p;
@ -1138,7 +1149,8 @@ ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
return NGX_ERROR;
}
len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
len = ngx_sock_ntop((struct sockaddr *) sin,
sizeof(struct sockaddr_in), p, len, 1);
u->addrs[i].name.len = len;
u->addrs[i].name.data = p;

View File

@ -107,8 +107,8 @@ in_addr_t ngx_inet_addr(u_char *text, size_t len);
ngx_int_t ngx_inet6_addr(u_char *p, size_t len, u_char *addr);
size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len);
#endif
size_t ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len,
ngx_uint_t port);
size_t ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text,
size_t len, ngx_uint_t port);
size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr);
ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,

View File

@ -275,7 +275,8 @@ ngx_event_accept(ngx_event_t *ev)
return;
}
c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
c->addr_text.data,
ls->addr_text_max_len, 0);
if (c->addr_text.len == 0) {
ngx_close_accepted_connection(c);

View File

@ -68,7 +68,8 @@ ngx_event_acceptex(ngx_event_t *rev)
return;
}
c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
c->addr_text.data,
ls->addr_text_max_len, 0);
if (c->addr_text.len == 0) {
/* TODO: close socket */

View File

@ -878,7 +878,8 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
goto failed;
}
len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
len = ngx_sock_ntop((struct sockaddr *) sin, sizeof(struct sockaddr_in),
p, len, 1);
ctx->addrs[i].name.len = len;
ctx->addrs[i].name.data = p;

View File

@ -230,7 +230,8 @@ ngx_http_realip_set_addr(ngx_http_request_t *r, ngx_addr_t *addr)
c = r->connection;
len = ngx_sock_ntop(addr->sockaddr, text, NGX_SOCKADDR_STRLEN, 0);
len = ngx_sock_ntop(addr->sockaddr, addr->socklen, text,
NGX_SOCKADDR_STRLEN, 0);
if (len == 0) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

View File

@ -3033,7 +3033,7 @@ ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
#endif
lsopt.wildcard = 1;
(void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
(void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
NGX_SOCKADDR_STRLEN, 1);
if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
@ -3984,7 +3984,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
lsopt.ipv6only = 1;
#endif
(void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
(void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
NGX_SOCKADDR_STRLEN, 1);
for (n = 2; n < cf->args->nelts; n++) {

View File

@ -465,7 +465,8 @@ ngx_mail_add_addrs(ngx_conf_t *cf, ngx_mail_port_t *mport,
addrs[i].conf.ssl = addr[i].ssl;
#endif
len = ngx_sock_ntop(addr[i].sockaddr, buf, NGX_SOCKADDR_STRLEN, 1);
len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen , buf,
NGX_SOCKADDR_STRLEN, 1);
p = ngx_pnalloc(cf->pool, len);
if (p == NULL) {
@ -513,7 +514,8 @@ ngx_mail_add_addrs6(ngx_conf_t *cf, ngx_mail_port_t *mport,
addrs6[i].conf.ssl = addr[i].ssl;
#endif
len = ngx_sock_ntop(addr[i].sockaddr, buf, NGX_SOCKADDR_STRLEN, 1);
len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf,
NGX_SOCKADDR_STRLEN, 1);
p = ngx_pnalloc(cf->pool, len);
if (p == NULL) {

View File

@ -439,7 +439,8 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ls->bind = 1;
} else {
len = ngx_sock_ntop(sa, buf, NGX_SOCKADDR_STRLEN, 1);
len = ngx_sock_ntop(sa, ls->socklen, buf,
NGX_SOCKADDR_STRLEN, 1);
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"ipv6only is not supported "