[nginx] Use ngx_cmp_sockaddr() where appropriate.

Ruslan Ermilov ru at nginx.com
Fri May 20 16:19:09 UTC 2016


details:   http://hg.nginx.org/nginx/rev/6f8254ae61b8
branches:  
changeset: 6557:6f8254ae61b8
user:      Ruslan Ermilov <ru at nginx.com>
date:      Fri May 20 19:10:42 2016 +0300
description:
Use ngx_cmp_sockaddr() where appropriate.

diffstat:

 src/http/ngx_http.c                 |  37 ++---------------------
 src/http/ngx_http_core_module.h     |   1 -
 src/mail/ngx_mail.h                 |   1 -
 src/mail/ngx_mail_core_module.c     |  56 +++---------------------------------
 src/stream/ngx_stream.h             |   1 -
 src/stream/ngx_stream_core_module.c |  56 +++---------------------------------
 6 files changed, 16 insertions(+), 136 deletions(-)

diffs (290 lines):

diff -r 654d2dae97d3 -r 6f8254ae61b8 src/http/ngx_http.c
--- a/src/http/ngx_http.c	Fri May 20 18:41:17 2016 +0300
+++ b/src/http/ngx_http.c	Fri May 20 19:10:42 2016 +0300
@@ -1215,14 +1215,8 @@ static ngx_int_t
 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
 {
-    u_char                *p;
-    size_t                 len, off;
     ngx_uint_t             i, default_server, proxy_protocol;
-    struct sockaddr       *sa;
     ngx_http_conf_addr_t  *addr;
-#if (NGX_HAVE_UNIX_DOMAIN)
-    struct sockaddr_un    *saun;
-#endif
 #if (NGX_HTTP_SSL)
     ngx_uint_t             ssl;
 #endif
@@ -1235,37 +1229,14 @@ ngx_http_add_addresses(ngx_conf_t *cf, n
      * may fill some fields in inherited sockaddr struct's
      */
 
-    sa = &lsopt->u.sockaddr;
-
-    switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        off = offsetof(struct sockaddr_in6, sin6_addr);
-        len = 16;
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        off = offsetof(struct sockaddr_un, sun_path);
-        len = sizeof(saun->sun_path);
-        break;
-#endif
-
-    default: /* AF_INET */
-        off = offsetof(struct sockaddr_in, sin_addr);
-        len = 4;
-        break;
-    }
-
-    p = lsopt->u.sockaddr_data + off;
-
     addr = port->addrs.elts;
 
     for (i = 0; i < port->addrs.nelts; i++) {
 
-        if (ngx_memcmp(p, addr[i].opt.u.sockaddr_data + off, len) != 0) {
+        if (ngx_cmp_sockaddr(&lsopt->u.sockaddr, lsopt->socklen,
+                             &addr[i].opt.u.sockaddr, addr[i].opt.socklen, 0)
+            != NGX_OK)
+        {
             continue;
         }
 
diff -r 654d2dae97d3 -r 6f8254ae61b8 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h	Fri May 20 18:41:17 2016 +0300
+++ b/src/http/ngx_http_core_module.h	Fri May 20 19:10:42 2016 +0300
@@ -67,7 +67,6 @@ typedef struct {
 #if (NGX_HAVE_UNIX_DOMAIN)
         struct sockaddr_un     sockaddr_un;
 #endif
-        u_char                 sockaddr_data[NGX_SOCKADDRLEN];
     } u;
 
     socklen_t                  socklen;
diff -r 654d2dae97d3 -r 6f8254ae61b8 src/mail/ngx_mail.h
--- a/src/mail/ngx_mail.h	Fri May 20 18:41:17 2016 +0300
+++ b/src/mail/ngx_mail.h	Fri May 20 19:10:42 2016 +0300
@@ -36,7 +36,6 @@ typedef struct {
 #if (NGX_HAVE_UNIX_DOMAIN)
         struct sockaddr_un  sockaddr_un;
 #endif
-        u_char              sockaddr_data[NGX_SOCKADDRLEN];
     } u;
 
     socklen_t               socklen;
diff -r 654d2dae97d3 -r 6f8254ae61b8 src/mail/ngx_mail_core_module.c
--- a/src/mail/ngx_mail_core_module.c	Fri May 20 18:41:17 2016 +0300
+++ b/src/mail/ngx_mail_core_module.c	Fri May 20 19:10:42 2016 +0300
@@ -288,19 +288,12 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
 {
     ngx_mail_core_srv_conf_t  *cscf = conf;
 
-    size_t                      len, off;
-    in_port_t                   port;
     ngx_str_t                  *value;
     ngx_url_t                   u;
     ngx_uint_t                  i, m;
-    struct sockaddr            *sa;
     ngx_mail_listen_t          *ls;
     ngx_mail_module_t          *module;
-    struct sockaddr_in         *sin;
     ngx_mail_core_main_conf_t  *cmcf;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6        *sin6;
-#endif
 
     value = cf->args->elts;
 
@@ -325,49 +318,13 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
 
     for (i = 0; i < cmcf->listen.nelts; i++) {
 
-        sa = &ls[i].u.sockaddr;
-
-        if (sa->sa_family != u.family) {
-            continue;
-        }
-
-        switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-        case AF_INET6:
-            off = offsetof(struct sockaddr_in6, sin6_addr);
-            len = 16;
-            sin6 = &ls[i].u.sockaddr_in6;
-            port = ntohs(sin6->sin6_port);
-            break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-        case AF_UNIX:
-            off = offsetof(struct sockaddr_un, sun_path);
-            len = sizeof(((struct sockaddr_un *) sa)->sun_path);
-            port = 0;
-            break;
-#endif
-
-        default: /* AF_INET */
-            off = offsetof(struct sockaddr_in, sin_addr);
-            len = 4;
-            sin = &ls[i].u.sockaddr_in;
-            port = ntohs(sin->sin_port);
-            break;
-        }
-
-        if (ngx_memcmp(ls[i].u.sockaddr_data + off, u.sockaddr + off, len)
-            != 0)
+        if (ngx_cmp_sockaddr(&ls[i].u.sockaddr, ls[i].socklen,
+                             (struct sockaddr *) &u.sockaddr, u.socklen, 1)
+            != NGX_OK)
         {
             continue;
         }
 
-        if (port != u.port) {
-            continue;
-        }
-
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "duplicate \"%V\" address and port pair", &u.url);
         return NGX_CONF_ERROR;
@@ -434,11 +391,10 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
 
         if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) {
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+            size_t  len;
             u_char  buf[NGX_SOCKADDR_STRLEN];
 
-            sa = &ls->u.sockaddr;
-
-            if (sa->sa_family == AF_INET6) {
+            if (ls->u.sockaddr.sa_family == AF_INET6) {
 
                 if (ngx_strcmp(&value[i].data[10], "n") == 0) {
                     ls->ipv6only = 1;
@@ -456,7 +412,7 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx
                 ls->bind = 1;
 
             } else {
-                len = ngx_sock_ntop(sa, ls->socklen, buf,
+                len = ngx_sock_ntop(&ls->u.sockaddr, ls->socklen, buf,
                                     NGX_SOCKADDR_STRLEN, 1);
 
                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
diff -r 654d2dae97d3 -r 6f8254ae61b8 src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h	Fri May 20 18:41:17 2016 +0300
+++ b/src/stream/ngx_stream.h	Fri May 20 19:10:42 2016 +0300
@@ -40,7 +40,6 @@ typedef struct {
 #if (NGX_HAVE_UNIX_DOMAIN)
         struct sockaddr_un  sockaddr_un;
 #endif
-        u_char              sockaddr_data[NGX_SOCKADDRLEN];
     } u;
 
     socklen_t               socklen;
diff -r 654d2dae97d3 -r 6f8254ae61b8 src/stream/ngx_stream_core_module.c
--- a/src/stream/ngx_stream_core_module.c	Fri May 20 18:41:17 2016 +0300
+++ b/src/stream/ngx_stream_core_module.c	Fri May 20 19:10:42 2016 +0300
@@ -248,18 +248,11 @@ ngx_stream_core_server(ngx_conf_t *cf, n
 static char *
 ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    size_t                        len, off;
-    in_port_t                     port;
     ngx_str_t                    *value;
     ngx_url_t                     u;
     ngx_uint_t                    i, backlog;
-    struct sockaddr              *sa;
-    struct sockaddr_in           *sin;
     ngx_stream_listen_t          *ls;
     ngx_stream_core_main_conf_t  *cmcf;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6          *sin6;
-#endif
 
     value = cf->args->elts;
 
@@ -284,49 +277,13 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
 
     for (i = 0; i < cmcf->listen.nelts; i++) {
 
-        sa = &ls[i].u.sockaddr;
-
-        if (sa->sa_family != u.family) {
-            continue;
-        }
-
-        switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-        case AF_INET6:
-            off = offsetof(struct sockaddr_in6, sin6_addr);
-            len = 16;
-            sin6 = &ls[i].u.sockaddr_in6;
-            port = sin6->sin6_port;
-            break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-        case AF_UNIX:
-            off = offsetof(struct sockaddr_un, sun_path);
-            len = sizeof(((struct sockaddr_un *) sa)->sun_path);
-            port = 0;
-            break;
-#endif
-
-        default: /* AF_INET */
-            off = offsetof(struct sockaddr_in, sin_addr);
-            len = 4;
-            sin = &ls[i].u.sockaddr_in;
-            port = sin->sin_port;
-            break;
-        }
-
-        if (ngx_memcmp(ls[i].u.sockaddr_data + off, u.sockaddr + off, len)
-            != 0)
+        if (ngx_cmp_sockaddr(&ls[i].u.sockaddr, ls[i].socklen,
+                             (struct sockaddr *) &u.sockaddr, u.socklen, 1)
+            != NGX_OK)
         {
             continue;
         }
 
-        if (port != u.port) {
-            continue;
-        }
-
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "duplicate \"%V\" address and port pair", &u.url);
         return NGX_CONF_ERROR;
@@ -384,11 +341,10 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
 
         if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) {
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+            size_t  len;
             u_char  buf[NGX_SOCKADDR_STRLEN];
 
-            sa = &ls->u.sockaddr;
-
-            if (sa->sa_family == AF_INET6) {
+            if (ls->u.sockaddr.sa_family == AF_INET6) {
 
                 if (ngx_strcmp(&value[i].data[10], "n") == 0) {
                     ls->ipv6only = 1;
@@ -406,7 +362,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
                 ls->bind = 1;
 
             } else {
-                len = ngx_sock_ntop(sa, ls->socklen, buf,
+                len = ngx_sock_ntop(&ls->u.sockaddr, ls->socklen, buf,
                                     NGX_SOCKADDR_STRLEN, 1);
 
                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,



More information about the nginx-devel mailing list