[nginx] Core: extended ngx_sock_ntop() with socklen parameter.

Homutov Vladimir vl at nginx.com
Thu Jul 11 12:44:01 UTC 2013


details:   http://hg.nginx.org/nginx/rev/05ba5bce31e0
branches:  
changeset: 5263:05ba5bce31e0
user:      Vladimir Homutov <vl at nginx.com>
date:      Thu Jul 11 16:07:25 2013 +0400
description:
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.

diffstat:

 src/core/ngx_connection.c                 |   7 ++++---
 src/core/ngx_inet.c                       |  22 +++++++++++++++++-----
 src/core/ngx_inet.h                       |   4 ++--
 src/event/ngx_event_accept.c              |   3 ++-
 src/event/ngx_event_acceptex.c            |   3 ++-
 src/event/ngx_event_openssl_stapling.c    |   3 ++-
 src/http/modules/ngx_http_realip_module.c |   3 ++-
 src/http/ngx_http_core_module.c           |   4 ++--
 src/mail/ngx_mail.c                       |   6 ++++--
 src/mail/ngx_mail_core_module.c           |   3 ++-
 10 files changed, 39 insertions(+), 19 deletions(-)

diffs (216 lines):

diff -r 626f288fa5ed -r 05ba5bce31e0 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/core/ngx_connection.c	Thu Jul 11 16:07:25 2013 +0400
@@ -41,7 +41,7 @@ ngx_create_listening(ngx_conf_t *cf, voi
     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 *c
             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_connec
         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;
 }
diff -r 626f288fa5ed -r 05ba5bce31e0 src/core/ngx_inet.c
--- a/src/core/ngx_inet.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/core/ngx_inet.c	Thu Jul 11 16:07:25 2013 +0400
@@ -174,7 +174,8 @@ ngx_inet6_addr(u_char *p, size_t len, u_
 
 
 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_cha
     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, 
             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, 
             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, 
                 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;
diff -r 626f288fa5ed -r 05ba5bce31e0 src/core/ngx_inet.h
--- a/src/core/ngx_inet.h	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/core/ngx_inet.h	Thu Jul 11 16:07:25 2013 +0400
@@ -107,8 +107,8 @@ in_addr_t ngx_inet_addr(u_char *text, si
 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,
diff -r 626f288fa5ed -r 05ba5bce31e0 src/event/ngx_event_accept.c
--- a/src/event/ngx_event_accept.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/event/ngx_event_accept.c	Thu Jul 11 16:07:25 2013 +0400
@@ -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);
diff -r 626f288fa5ed -r 05ba5bce31e0 src/event/ngx_event_acceptex.c
--- a/src/event/ngx_event_acceptex.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/event/ngx_event_acceptex.c	Thu Jul 11 16:07:25 2013 +0400
@@ -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 */
diff -r 626f288fa5ed -r 05ba5bce31e0 src/event/ngx_event_openssl_stapling.c
--- a/src/event/ngx_event_openssl_stapling.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/event/ngx_event_openssl_stapling.c	Thu Jul 11 16:07:25 2013 +0400
@@ -878,7 +878,8 @@ ngx_ssl_ocsp_resolve_handler(ngx_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;
diff -r 626f288fa5ed -r 05ba5bce31e0 src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/http/modules/ngx_http_realip_module.c	Thu Jul 11 16:07:25 2013 +0400
@@ -230,7 +230,8 @@ ngx_http_realip_set_addr(ngx_http_reques
 
     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;
     }
diff -r 626f288fa5ed -r 05ba5bce31e0 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/http/ngx_http_core_module.c	Thu Jul 11 16:07:25 2013 +0400
@@ -3033,7 +3033,7 @@ ngx_http_core_server(ngx_conf_t *cf, ngx
 #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
     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++) {
diff -r 626f288fa5ed -r 05ba5bce31e0 src/mail/ngx_mail.c
--- a/src/mail/ngx_mail.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/mail/ngx_mail.c	Thu Jul 11 16:07:25 2013 +0400
@@ -465,7 +465,8 @@ ngx_mail_add_addrs(ngx_conf_t *cf, ngx_m
         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_
         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) {
diff -r 626f288fa5ed -r 05ba5bce31e0 src/mail/ngx_mail_core_module.c
--- a/src/mail/ngx_mail_core_module.c	Fri Jul 05 11:42:25 2013 +0400
+++ b/src/mail/ngx_mail_core_module.c	Thu Jul 11 16:07:25 2013 +0400
@@ -439,7 +439,8 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
                 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 "



More information about the nginx-devel mailing list