[nginx] Introduced the ngx_sockaddr_t type.

Ruslan Ermilov ru at nginx.com
Mon May 23 13:37:57 UTC 2016


details:   http://hg.nginx.org/nginx/rev/adf25b8d0431
branches:  
changeset: 6559:adf25b8d0431
user:      Ruslan Ermilov <ru at nginx.com>
date:      Mon May 23 16:37:20 2016 +0300
description:
Introduced the ngx_sockaddr_t type.

It's properly aligned and can hold any supported sockaddr.

diffstat:

 src/core/ngx_connection.c                             |  10 ++--
 src/core/ngx_inet.c                                   |   6 +-
 src/core/ngx_inet.h                                   |  29 +++++++-----
 src/core/ngx_resolver.c                               |  42 +++++++++---------
 src/event/ngx_event_accept.c                          |  17 +++----
 src/http/modules/ngx_http_upstream_keepalive_module.c |   2 +-
 src/http/ngx_http_core_module.c                       |   2 +-
 src/http/ngx_http_core_module.h                       |  12 +----
 src/mail/ngx_mail.h                                   |  12 +----
 src/mail/ngx_mail_core_module.c                       |   2 +-
 src/stream/ngx_stream.h                               |  12 +----
 src/stream/ngx_stream_core_module.c                   |   2 +-
 12 files changed, 60 insertions(+), 88 deletions(-)

diffs (406 lines):

diff -r 68854ce64ec7 -r adf25b8d0431 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/core/ngx_connection.c	Mon May 23 16:37:20 2016 +0300
@@ -151,12 +151,12 @@ ngx_set_inherited_sockets(ngx_cycle_t *c
     ls = cycle->listening.elts;
     for (i = 0; i < cycle->listening.nelts; i++) {
 
-        ls[i].sockaddr = ngx_palloc(cycle->pool, NGX_SOCKADDRLEN);
+        ls[i].sockaddr = ngx_palloc(cycle->pool, sizeof(ngx_sockaddr_t));
         if (ls[i].sockaddr == NULL) {
             return NGX_ERROR;
         }
 
-        ls[i].socklen = NGX_SOCKADDRLEN;
+        ls[i].socklen = sizeof(ngx_sockaddr_t);
         if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) {
             ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
                           "getsockname() of the inherited "
@@ -1278,7 +1278,7 @@ ngx_connection_local_sockaddr(ngx_connec
 {
     socklen_t             len;
     ngx_uint_t            addr;
-    u_char                sa[NGX_SOCKADDRLEN];
+    ngx_sockaddr_t        sa;
     struct sockaddr_in   *sin;
 #if (NGX_HAVE_INET6)
     ngx_uint_t            i;
@@ -1316,9 +1316,9 @@ ngx_connection_local_sockaddr(ngx_connec
 
     if (addr == 0) {
 
-        len = NGX_SOCKADDRLEN;
+        len = sizeof(ngx_sockaddr_t);
 
-        if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) {
+        if (getsockname(c->fd, &sa.sockaddr, &len) == -1) {
             ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");
             return NGX_ERROR;
         }
diff -r 68854ce64ec7 -r adf25b8d0431 src/core/ngx_inet.c
--- a/src/core/ngx_inet.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/core/ngx_inet.c	Mon May 23 16:37:20 2016 +0300
@@ -763,7 +763,7 @@ ngx_parse_inet_url(ngx_pool_t *pool, ngx
             return NGX_ERROR;
         }
 
-        ngx_memcpy(sin, u->sockaddr, sizeof(struct sockaddr_in));
+        ngx_memcpy(sin, &u->sockaddr, sizeof(struct sockaddr_in));
 
         u->addrs[0].sockaddr = (struct sockaddr *) sin;
         u->addrs[0].socklen = sizeof(struct sockaddr_in);
@@ -790,7 +790,7 @@ ngx_parse_inet_url(ngx_pool_t *pool, ngx
 
     u->family = u->addrs[0].sockaddr->sa_family;
     u->socklen = u->addrs[0].socklen;
-    ngx_memcpy(u->sockaddr, u->addrs[0].sockaddr, u->addrs[0].socklen);
+    ngx_memcpy(&u->sockaddr, u->addrs[0].sockaddr, u->addrs[0].socklen);
 
     switch (u->family) {
 
@@ -920,7 +920,7 @@ ngx_parse_inet6_url(ngx_pool_t *pool, ng
         return NGX_ERROR;
     }
 
-    ngx_memcpy(sin6, u->sockaddr, sizeof(struct sockaddr_in6));
+    ngx_memcpy(sin6, &u->sockaddr, sizeof(struct sockaddr_in6));
 
     u->addrs[0].sockaddr = (struct sockaddr *) sin6;
     u->addrs[0].socklen = sizeof(struct sockaddr_in6);
diff -r 68854ce64ec7 -r adf25b8d0431 src/core/ngx_inet.h
--- a/src/core/ngx_inet.h	Mon May 23 12:50:59 2016 +0300
+++ b/src/core/ngx_inet.h	Mon May 23 16:37:20 2016 +0300
@@ -13,14 +13,6 @@
 #include <ngx_core.h>
 
 
-/*
- * TODO: autoconfigure NGX_SOCKADDRLEN and NGX_SOCKADDR_STRLEN as
- *       sizeof(struct sockaddr_storage)
- *       sizeof(struct sockaddr_un)
- *       sizeof(struct sockaddr_in6)
- *       sizeof(struct sockaddr_in)
- */
-
 #define NGX_INET_ADDRSTRLEN   (sizeof("255.255.255.255") - 1)
 #define NGX_INET6_ADDRSTRLEN                                                 \
     (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
@@ -29,15 +21,26 @@
 
 #if (NGX_HAVE_UNIX_DOMAIN)
 #define NGX_SOCKADDR_STRLEN   (sizeof("unix:") - 1 + NGX_UNIX_ADDRSTRLEN)
+#elif (NGX_HAVE_INET6)
+#define NGX_SOCKADDR_STRLEN   (NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1)
 #else
-#define NGX_SOCKADDR_STRLEN   (NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1)
+#define NGX_SOCKADDR_STRLEN   (NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1)
 #endif
 
+/* compatibility */
+#define NGX_SOCKADDRLEN       sizeof(ngx_sockaddr_t)
+
+
+typedef union {
+    struct sockaddr           sockaddr;
+    struct sockaddr_in        sockaddr_in;
+#if (NGX_HAVE_INET6)
+    struct sockaddr_in6       sockaddr_in6;
+#endif
 #if (NGX_HAVE_UNIX_DOMAIN)
-#define NGX_SOCKADDRLEN       sizeof(struct sockaddr_un)
-#else
-#define NGX_SOCKADDRLEN       512
+    struct sockaddr_un        sockaddr_un;
 #endif
+} ngx_sockaddr_t;
 
 
 typedef struct {
@@ -92,7 +95,7 @@ typedef struct {
     unsigned                  wildcard:1;
 
     socklen_t                 socklen;
-    u_char                    sockaddr[NGX_SOCKADDRLEN];
+    ngx_sockaddr_t            sockaddr;
 
     ngx_addr_t               *addrs;
     ngx_uint_t                naddrs;
diff -r 68854ce64ec7 -r adf25b8d0431 src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/core/ngx_resolver.c	Mon May 23 16:37:20 2016 +0300
@@ -2992,15 +2992,15 @@ failed:
 static void
 ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *cctx)
 {
-    ngx_uint_t                 i;
-    u_char                   (*sockaddr)[NGX_SOCKADDRLEN];
-    ngx_addr_t                *addrs;
-    ngx_resolver_t            *r;
-    struct sockaddr_in        *sin;
-    ngx_resolver_ctx_t        *ctx;
-    ngx_resolver_srv_name_t   *srv;
+    ngx_uint_t                i;
+    ngx_addr_t               *addrs;
+    ngx_resolver_t           *r;
+    ngx_sockaddr_t           *sockaddr;
+    struct sockaddr_in       *sin;
+    ngx_resolver_ctx_t       *ctx;
+    ngx_resolver_srv_name_t  *srv;
 #if (NGX_HAVE_INET6)
-    struct sockaddr_in6       *sin6;
+    struct sockaddr_in6      *sin6;
 #endif
 
     r = cctx->resolver;
@@ -3026,7 +3026,7 @@ ngx_resolver_srv_names_handler(ngx_resol
             return;
         }
 
-        sockaddr = ngx_resolver_alloc(r, cctx->naddrs * NGX_SOCKADDRLEN);
+        sockaddr = ngx_resolver_alloc(r, cctx->naddrs * sizeof(ngx_sockaddr_t));
         if (sockaddr == NULL) {
             ngx_resolver_free(r, addrs);
             ngx_resolve_name_done(cctx);
@@ -3039,10 +3039,10 @@ ngx_resolver_srv_names_handler(ngx_resol
         }
 
         for (i = 0; i < cctx->naddrs; i++) {
-            addrs[i].sockaddr = (struct sockaddr *) sockaddr[i];
+            addrs[i].sockaddr = &sockaddr[i].sockaddr;
             addrs[i].socklen = cctx->addrs[i].socklen;
 
-            ngx_memcpy(sockaddr[i], cctx->addrs[i].sockaddr,
+            ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
                        addrs[i].socklen);
 
             switch (addrs[i].sockaddr->sa_family) {
@@ -4161,14 +4161,14 @@ static ngx_resolver_addr_t *
 ngx_resolver_export(ngx_resolver_t *r, ngx_resolver_node_t *rn,
     ngx_uint_t rotate)
 {
-    ngx_uint_t             d, i, j, n;
-    u_char               (*sockaddr)[NGX_SOCKADDRLEN];
-    in_addr_t             *addr;
-    struct sockaddr_in    *sin;
-    ngx_resolver_addr_t   *dst;
+    ngx_uint_t            d, i, j, n;
+    in_addr_t            *addr;
+    ngx_sockaddr_t       *sockaddr;
+    struct sockaddr_in   *sin;
+    ngx_resolver_addr_t  *dst;
 #if (NGX_HAVE_INET6)
-    struct in6_addr       *addr6;
-    struct sockaddr_in6   *sin6;
+    struct in6_addr      *addr6;
+    struct sockaddr_in6  *sin6;
 #endif
 
     n = rn->naddrs;
@@ -4181,7 +4181,7 @@ ngx_resolver_export(ngx_resolver_t *r, n
         return NULL;
     }
 
-    sockaddr = ngx_resolver_calloc(r, n * NGX_SOCKADDRLEN);
+    sockaddr = ngx_resolver_calloc(r, n * sizeof(ngx_sockaddr_t));
     if (sockaddr == NULL) {
         ngx_resolver_free(r, dst);
         return NULL;
@@ -4196,7 +4196,7 @@ ngx_resolver_export(ngx_resolver_t *r, n
         addr = (rn->naddrs == 1) ? &rn->u.addr : rn->u.addrs;
 
         do {
-            sin = (struct sockaddr_in *) sockaddr[d];
+            sin = &sockaddr[d].sockaddr_in;
             sin->sin_family = AF_INET;
             sin->sin_addr.s_addr = addr[j++];
             dst[d].sockaddr = (struct sockaddr *) sin;
@@ -4219,7 +4219,7 @@ ngx_resolver_export(ngx_resolver_t *r, n
         addr6 = (rn->naddrs6 == 1) ? &rn->u6.addr6 : rn->u6.addrs6;
 
         do {
-            sin6 = (struct sockaddr_in6 *) sockaddr[d];
+            sin6 = &sockaddr[d].sockaddr_in6;
             sin6->sin6_family = AF_INET6;
             ngx_memcpy(sin6->sin6_addr.s6_addr, addr6[j++].s6_addr, 16);
             dst[d].sockaddr = (struct sockaddr *) sin6;
diff -r 68854ce64ec7 -r adf25b8d0431 src/event/ngx_event_accept.c
--- a/src/event/ngx_event_accept.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/event/ngx_event_accept.c	Mon May 23 16:37:20 2016 +0300
@@ -28,10 +28,10 @@ ngx_event_accept(ngx_event_t *ev)
     ngx_uint_t         level;
     ngx_socket_t       s;
     ngx_event_t       *rev, *wev;
+    ngx_sockaddr_t     sa;
     ngx_listening_t   *ls;
     ngx_connection_t  *c, *lc;
     ngx_event_conf_t  *ecf;
-    u_char             sa[NGX_SOCKADDRLEN];
 #if (NGX_HAVE_ACCEPT4)
     static ngx_uint_t  use_accept4 = 1;
 #endif
@@ -58,17 +58,16 @@ ngx_event_accept(ngx_event_t *ev)
                    "accept on %V, ready: %d", &ls->addr_text, ev->available);
 
     do {
-        socklen = NGX_SOCKADDRLEN;
+        socklen = sizeof(ngx_sockaddr_t);
 
 #if (NGX_HAVE_ACCEPT4)
         if (use_accept4) {
-            s = accept4(lc->fd, (struct sockaddr *) sa, &socklen,
-                        SOCK_NONBLOCK);
+            s = accept4(lc->fd, &sa.sockaddr, &socklen, SOCK_NONBLOCK);
         } else {
-            s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
+            s = accept(lc->fd, &sa.sockaddr, &socklen);
         }
 #else
-        s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
+        s = accept(lc->fd, &sa.sockaddr, &socklen);
 #endif
 
         if (s == (ngx_socket_t) -1) {
@@ -171,7 +170,7 @@ ngx_event_accept(ngx_event_t *ev)
             return;
         }
 
-        ngx_memcpy(c->sockaddr, sa, socklen);
+        ngx_memcpy(c->sockaddr, &sa, socklen);
 
         log = ngx_palloc(c->pool, sizeof(ngx_log_t));
         if (log == NULL) {
@@ -328,10 +327,10 @@ ngx_event_recvmsg(ngx_event_t *ev)
     ngx_event_t       *rev, *wev;
     struct iovec       iov[1];
     struct msghdr      msg;
+    ngx_sockaddr_t     sa;
     ngx_listening_t   *ls;
     ngx_event_conf_t  *ecf;
     ngx_connection_t  *c, *lc;
-    u_char             sa[NGX_SOCKADDRLEN];
     static u_char      buffer[65535];
 
 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
@@ -376,7 +375,7 @@ ngx_event_recvmsg(ngx_event_t *ev)
         iov[0].iov_len = sizeof(buffer);
 
         msg.msg_name = &sa;
-        msg.msg_namelen = sizeof(sa);
+        msg.msg_namelen = sizeof(ngx_sockaddr_t);
         msg.msg_iov = iov;
         msg.msg_iovlen = 1;
 
diff -r 68854ce64ec7 -r adf25b8d0431 src/http/modules/ngx_http_upstream_keepalive_module.c
--- a/src/http/modules/ngx_http_upstream_keepalive_module.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/http/modules/ngx_http_upstream_keepalive_module.c	Mon May 23 16:37:20 2016 +0300
@@ -29,7 +29,7 @@ typedef struct {
     ngx_connection_t                  *connection;
 
     socklen_t                          socklen;
-    u_char                             sockaddr[NGX_SOCKADDRLEN];
+    ngx_sockaddr_t                     sockaddr;
 
 } ngx_http_upstream_keepalive_cache_t;
 
diff -r 68854ce64ec7 -r adf25b8d0431 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/http/ngx_http_core_module.c	Mon May 23 16:37:20 2016 +0300
@@ -3998,7 +3998,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx
 
     ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
 
-    ngx_memcpy(&lsopt.u.sockaddr, u.sockaddr, u.socklen);
+    ngx_memcpy(&lsopt.u.sockaddr, &u.sockaddr, u.socklen);
 
     lsopt.socklen = u.socklen;
     lsopt.backlog = NGX_LISTEN_BACKLOG;
diff -r 68854ce64ec7 -r adf25b8d0431 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h	Mon May 23 12:50:59 2016 +0300
+++ b/src/http/ngx_http_core_module.h	Mon May 23 16:37:20 2016 +0300
@@ -58,17 +58,7 @@ typedef struct ngx_http_core_loc_conf_s 
 
 
 typedef struct {
-    union {
-        struct sockaddr        sockaddr;
-        struct sockaddr_in     sockaddr_in;
-#if (NGX_HAVE_INET6)
-        struct sockaddr_in6    sockaddr_in6;
-#endif
-#if (NGX_HAVE_UNIX_DOMAIN)
-        struct sockaddr_un     sockaddr_un;
-#endif
-    } u;
-
+    ngx_sockaddr_t             u;
     socklen_t                  socklen;
 
     unsigned                   set:1;
diff -r 68854ce64ec7 -r adf25b8d0431 src/mail/ngx_mail.h
--- a/src/mail/ngx_mail.h	Mon May 23 12:50:59 2016 +0300
+++ b/src/mail/ngx_mail.h	Mon May 23 16:37:20 2016 +0300
@@ -27,17 +27,7 @@ typedef struct {
 
 
 typedef struct {
-    union {
-        struct sockaddr     sockaddr;
-        struct sockaddr_in  sockaddr_in;
-#if (NGX_HAVE_INET6)
-        struct sockaddr_in6 sockaddr_in6;
-#endif
-#if (NGX_HAVE_UNIX_DOMAIN)
-        struct sockaddr_un  sockaddr_un;
-#endif
-    } u;
-
+    ngx_sockaddr_t          u;
     socklen_t               socklen;
 
     /* server ctx */
diff -r 68854ce64ec7 -r adf25b8d0431 src/mail/ngx_mail_core_module.c
--- a/src/mail/ngx_mail_core_module.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/mail/ngx_mail_core_module.c	Mon May 23 16:37:20 2016 +0300
@@ -337,7 +337,7 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
 
     ngx_memzero(ls, sizeof(ngx_mail_listen_t));
 
-    ngx_memcpy(&ls->u.sockaddr, u.sockaddr, u.socklen);
+    ngx_memcpy(&ls->u.sockaddr, &u.sockaddr, u.socklen);
 
     ls->socklen = u.socklen;
     ls->backlog = NGX_LISTEN_BACKLOG;
diff -r 68854ce64ec7 -r adf25b8d0431 src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h	Mon May 23 12:50:59 2016 +0300
+++ b/src/stream/ngx_stream.h	Mon May 23 16:37:20 2016 +0300
@@ -31,17 +31,7 @@ typedef struct {
 
 
 typedef struct {
-    union {
-        struct sockaddr     sockaddr;
-        struct sockaddr_in  sockaddr_in;
-#if (NGX_HAVE_INET6)
-        struct sockaddr_in6 sockaddr_in6;
-#endif
-#if (NGX_HAVE_UNIX_DOMAIN)
-        struct sockaddr_un  sockaddr_un;
-#endif
-    } u;
-
+    ngx_sockaddr_t          u;
     socklen_t               socklen;
 
     /* server ctx */
diff -r 68854ce64ec7 -r adf25b8d0431 src/stream/ngx_stream_core_module.c
--- a/src/stream/ngx_stream_core_module.c	Mon May 23 12:50:59 2016 +0300
+++ b/src/stream/ngx_stream_core_module.c	Mon May 23 16:37:20 2016 +0300
@@ -280,7 +280,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
 
     ngx_memzero(ls, sizeof(ngx_stream_listen_t));
 
-    ngx_memcpy(&ls->u.sockaddr, u.sockaddr, u.socklen);
+    ngx_memcpy(&ls->u.sockaddr, &u.sockaddr, u.socklen);
 
     ls->socklen = u.socklen;
     ls->backlog = NGX_LISTEN_BACKLOG;



More information about the nginx-devel mailing list