[nginx] Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.

Roman Arutyunyan arut at nginx.com
Mon Jun 20 09:50:03 UTC 2016


details:   http://hg.nginx.org/nginx/rev/b3b7e33083ac
branches:  
changeset: 6593:b3b7e33083ac
user:      Roman Arutyunyan <arut at nginx.com>
date:      Mon Jun 20 11:50:39 2016 +0300
description:
Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.

diffstat:

 src/core/ngx_inet.c                       |  83 +++++++++++++++++++++++-------
 src/core/ngx_inet.h                       |   2 +
 src/core/ngx_proxy_protocol.c             |  11 +--
 src/core/ngx_resolver.c                   |  16 +-----
 src/event/ngx_event_openssl_stapling.c    |  14 +----
 src/http/modules/ngx_http_realip_module.c |  39 +-------------
 src/http/ngx_http.c                       |  26 +---------
 src/http/ngx_http_header_filter_module.c  |  23 +--------
 src/http/ngx_http_upstream_round_robin.c  |  11 +---
 src/http/ngx_http_variables.c             |  54 +------------------
 src/http/v2/ngx_http_v2_filter_module.c   |  23 +--------
 src/mail/ngx_mail.c                       |  26 +---------
 src/mail/ngx_mail_auth_http_module.c      |  29 ++--------
 src/stream/ngx_stream.c                   |  26 +---------
 src/stream/ngx_stream_proxy_module.c      |  24 +-------
 15 files changed, 90 insertions(+), 317 deletions(-)

diffs (682 lines):

diff -r 2c7b488a61fb -r b3b7e33083ac src/core/ngx_inet.c
--- a/src/core/ngx_inet.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/core/ngx_inet.c	Mon Jun 20 11:50:39 2016 +0300
@@ -529,13 +529,9 @@ ngx_int_t
 ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
     size_t len)
 {
-    u_char               *p, *last;
-    size_t                plen;
-    ngx_int_t             rc, port;
-    struct sockaddr_in   *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6  *sin6;
-#endif
+    u_char     *p, *last;
+    size_t      plen;
+    ngx_int_t   rc, port;
 
     rc = ngx_parse_addr(pool, addr, text, len);
 
@@ -585,20 +581,7 @@ ngx_parse_addr_port(ngx_pool_t *pool, ng
         return rc;
     }
 
-    switch (addr->sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = (struct sockaddr_in6 *) addr->sockaddr;
-        sin6->sin6_port = htons((in_port_t) port);
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = (struct sockaddr_in *) addr->sockaddr;
-        sin->sin_port = htons((in_port_t) port);
-        break;
-    }
+    ngx_inet_set_port(addr->sockaddr, port);
 
     return NGX_OK;
 }
@@ -1356,3 +1339,61 @@ ngx_cmp_sockaddr(struct sockaddr *sa1, s
 
     return NGX_OK;
 }
+
+
+in_port_t
+ngx_inet_get_port(struct sockaddr *sa)
+{
+    struct sockaddr_in   *sin;
+#if (NGX_HAVE_INET6)
+    struct sockaddr_in6  *sin6;
+#endif
+
+    switch (sa->sa_family) {
+
+#if (NGX_HAVE_INET6)
+    case AF_INET6:
+        sin6 = (struct sockaddr_in6 *) sa;
+        return ntohs(sin6->sin6_port);
+#endif
+
+#if (NGX_HAVE_UNIX_DOMAIN)
+    case AF_UNIX:
+        return 0;
+#endif
+
+    default: /* AF_INET */
+        sin = (struct sockaddr_in *) sa;
+        return ntohs(sin->sin_port);
+    }
+}
+
+
+void
+ngx_inet_set_port(struct sockaddr *sa, in_port_t port)
+{
+    struct sockaddr_in   *sin;
+#if (NGX_HAVE_INET6)
+    struct sockaddr_in6  *sin6;
+#endif
+
+    switch (sa->sa_family) {
+
+#if (NGX_HAVE_INET6)
+    case AF_INET6:
+        sin6 = (struct sockaddr_in6 *) sa;
+        sin6->sin6_port = htons(port);
+        break;
+#endif
+
+#if (NGX_HAVE_UNIX_DOMAIN)
+    case AF_UNIX:
+        break;
+#endif
+
+    default: /* AF_INET */
+        sin = (struct sockaddr_in *) sa;
+        sin->sin_port = htons(port);
+        break;
+    }
+}
diff -r 2c7b488a61fb -r b3b7e33083ac src/core/ngx_inet.h
--- a/src/core/ngx_inet.h	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/core/ngx_inet.h	Mon Jun 20 11:50:39 2016 +0300
@@ -121,6 +121,8 @@ ngx_int_t ngx_parse_url(ngx_pool_t *pool
 ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
 ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
     struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port);
+in_port_t ngx_inet_get_port(struct sockaddr *sa);
+void ngx_inet_set_port(struct sockaddr *sa, in_port_t port);
 
 
 #endif /* _NGX_INET_H_INCLUDED_ */
diff -r 2c7b488a61fb -r b3b7e33083ac src/core/ngx_proxy_protocol.c
--- a/src/core/ngx_proxy_protocol.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/core/ngx_proxy_protocol.c	Mon Jun 20 11:50:39 2016 +0300
@@ -141,19 +141,11 @@ ngx_proxy_protocol_write(ngx_connection_
 
     case AF_INET:
         buf = ngx_cpymem(buf, "PROXY TCP4 ", sizeof("PROXY TCP4 ") - 1);
-
-        port = ntohs(((struct sockaddr_in *) c->sockaddr)->sin_port);
-        lport = ntohs(((struct sockaddr_in *) c->local_sockaddr)->sin_port);
-
         break;
 
 #if (NGX_HAVE_INET6)
     case AF_INET6:
         buf = ngx_cpymem(buf, "PROXY TCP6 ", sizeof("PROXY TCP6 ") - 1);
-
-        port = ntohs(((struct sockaddr_in6 *) c->sockaddr)->sin6_port);
-        lport = ntohs(((struct sockaddr_in6 *) c->local_sockaddr)->sin6_port);
-
         break;
 #endif
 
@@ -169,5 +161,8 @@ ngx_proxy_protocol_write(ngx_connection_
     buf += ngx_sock_ntop(c->local_sockaddr, c->local_socklen, buf, last - buf,
                          0);
 
+    port = ngx_inet_get_port(c->sockaddr);
+    lport = ngx_inet_get_port(c->local_sockaddr);
+
     return ngx_slprintf(buf, last, " %ui %ui" CRLF, port, lport);
 }
diff -r 2c7b488a61fb -r b3b7e33083ac src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/core/ngx_resolver.c	Mon Jun 20 11:50:39 2016 +0300
@@ -2996,12 +2996,8 @@ ngx_resolver_srv_names_handler(ngx_resol
     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;
-#endif
 
     r = cctx->resolver;
     ctx = cctx->data;
@@ -3045,17 +3041,7 @@ ngx_resolver_srv_names_handler(ngx_resol
             ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
                        addrs[i].socklen);
 
-            switch (addrs[i].sockaddr->sa_family) {
-#if (NGX_HAVE_INET6)
-            case AF_INET6:
-                sin6 = (struct sockaddr_in6 *) addrs[i].sockaddr;
-                sin6->sin6_port = htons(srv->port);
-                break;
-#endif
-            default: /* AF_INET */
-                sin = (struct sockaddr_in *) addrs[i].sockaddr;
-                sin->sin_port = htons(srv->port);
-            }
+            ngx_inet_set_port(addrs[i].sockaddr, srv->port);
         }
 
         srv->addrs = addrs;
diff -r 2c7b488a61fb -r b3b7e33083ac src/event/ngx_event_openssl_stapling.c
--- a/src/event/ngx_event_openssl_stapling.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/event/ngx_event_openssl_stapling.c	Mon Jun 20 11:50:39 2016 +0300
@@ -920,7 +920,6 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolve
 
     u_char           *p;
     size_t            len;
-    in_port_t         port;
     socklen_t         socklen;
     ngx_uint_t        i;
     struct sockaddr  *sockaddr;
@@ -962,8 +961,6 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolve
         goto failed;
     }
 
-    port = htons(ctx->port);
-
     for (i = 0; i < resolve->naddrs; i++) {
 
         socklen = resolve->addrs[i].socklen;
@@ -974,16 +971,7 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolve
         }
 
         ngx_memcpy(sockaddr, resolve->addrs[i].sockaddr, socklen);
-
-        switch (sockaddr->sa_family) {
-#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;
-        }
+        ngx_inet_set_port(sockaddr, ctx->port);
 
         ctx->addrs[i].sockaddr = sockaddr;
         ctx->addrs[i].socklen = socklen;
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/modules/ngx_http_realip_module.c	Mon Jun 20 11:50:39 2016 +0300
@@ -138,10 +138,6 @@ ngx_http_realip_handler(ngx_http_request
     ngx_list_part_t             *part;
     ngx_table_elt_t             *header;
     ngx_connection_t            *c;
-    struct sockaddr_in          *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6         *sin6;
-#endif
     ngx_http_realip_ctx_t       *ctx;
     ngx_http_realip_loc_conf_t  *rlcf;
 
@@ -242,21 +238,7 @@ found:
         != NGX_DECLINED)
     {
         if (rlcf->type == NGX_HTTP_REALIP_PROXY) {
-
-            switch (addr.sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-            case AF_INET6:
-                sin6 = (struct sockaddr_in6 *) addr.sockaddr;
-                sin6->sin6_port = htons(c->proxy_protocol_port);
-                break;
-#endif
-
-            default: /* AF_INET */
-                sin = (struct sockaddr_in *) addr.sockaddr;
-                sin->sin_port = htons(c->proxy_protocol_port);
-                break;
-            }
+            ngx_inet_set_port(addr.sockaddr, c->proxy_protocol_port);
         }
 
         return ngx_http_realip_set_addr(r, &addr);
@@ -578,24 +560,7 @@ ngx_http_realip_remote_port_variable(ngx
         return NGX_ERROR;
     }
 
-    switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        port = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        port = ntohs(((struct sockaddr_in *) sa)->sin_port);
-        break;
-    }
+    port = ngx_inet_get_port(sa);
 
     if (port > 0 && port < 65536) {
         v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/ngx_http.c
--- a/src/http/ngx_http.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/ngx_http.c	Mon Jun 20 11:50:39 2016 +0300
@@ -1144,12 +1144,8 @@ ngx_http_add_listen(ngx_conf_t *cf, ngx_
     in_port_t                   p;
     ngx_uint_t                  i;
     struct sockaddr            *sa;
-    struct sockaddr_in         *sin;
     ngx_http_conf_port_t       *port;
     ngx_http_core_main_conf_t  *cmcf;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6        *sin6;
-#endif
 
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
@@ -1162,27 +1158,7 @@ ngx_http_add_listen(ngx_conf_t *cf, ngx_
     }
 
     sa = &lsopt->sockaddr.sockaddr;
-
-    switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = &lsopt->sockaddr.sockaddr_in6;
-        p = sin6->sin6_port;
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        p = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = &lsopt->sockaddr.sockaddr_in;
-        p = sin->sin_port;
-        break;
-    }
+    p = ngx_inet_get_port(sa);
 
     port = cmcf->ports->elts;
     for (i = 0; i < cmcf->ports->nelts; i++) {
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/ngx_http_header_filter_module.c	Mon Jun 20 11:50:39 2016 +0300
@@ -161,10 +161,6 @@ ngx_http_header_filter(ngx_http_request_
     ngx_connection_t          *c;
     ngx_http_core_loc_conf_t  *clcf;
     ngx_http_core_srv_conf_t  *cscf;
-    struct sockaddr_in        *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6       *sin6;
-#endif
     u_char                     addr[NGX_SOCKADDR_STRLEN];
 
     if (r->header_sent) {
@@ -333,24 +329,7 @@ ngx_http_header_filter(ngx_http_request_
             }
         }
 
-        switch (c->local_sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-        case AF_INET6:
-            sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
-            port = ntohs(sin6->sin6_port);
-            break;
-#endif
-#if (NGX_HAVE_UNIX_DOMAIN)
-        case AF_UNIX:
-            port = 0;
-            break;
-#endif
-        default: /* AF_INET */
-            sin = (struct sockaddr_in *) c->local_sockaddr;
-            port = ntohs(sin->sin_port);
-            break;
-        }
+        port = ngx_inet_get_port(c->local_sockaddr);
 
         len += sizeof("Location: https://") - 1
                + host.len
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/ngx_http_upstream_round_robin.c
--- a/src/http/ngx_http_upstream_round_robin.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/ngx_http_upstream_round_robin.c	Mon Jun 20 11:50:39 2016 +0300
@@ -354,16 +354,7 @@ ngx_http_upstream_create_round_robin_pee
             }
 
             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);
-            }
+            ngx_inet_set_port(sockaddr, ur->port);
 
             p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
             if (p == NULL) {
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/ngx_http_variables.c	Mon Jun 20 11:50:39 2016 +0300
@@ -1201,11 +1201,7 @@ static ngx_int_t
 ngx_http_variable_remote_port(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {
-    ngx_uint_t            port;
-    struct sockaddr_in   *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6  *sin6;
-#endif
+    ngx_uint_t  port;
 
     v->len = 0;
     v->valid = 1;
@@ -1217,26 +1213,7 @@ ngx_http_variable_remote_port(ngx_http_r
         return NGX_ERROR;
     }
 
-    switch (r->connection->sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
-        port = ntohs(sin6->sin6_port);
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        port = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = (struct sockaddr_in *) r->connection->sockaddr;
-        port = ntohs(sin->sin_port);
-        break;
-    }
+    port = ngx_inet_get_port(r->connection->sockaddr);
 
     if (port > 0 && port < 65536) {
         v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
@@ -1321,11 +1298,7 @@ static ngx_int_t
 ngx_http_variable_server_port(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {
-    ngx_uint_t            port;
-    struct sockaddr_in   *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6  *sin6;
-#endif
+    ngx_uint_t  port;
 
     v->len = 0;
     v->valid = 1;
@@ -1341,26 +1314,7 @@ ngx_http_variable_server_port(ngx_http_r
         return NGX_ERROR;
     }
 
-    switch (r->connection->local_sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
-        port = ntohs(sin6->sin6_port);
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        port = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = (struct sockaddr_in *) r->connection->local_sockaddr;
-        port = ntohs(sin->sin_port);
-        break;
-    }
+    port = ngx_inet_get_port(r->connection->local_sockaddr);
 
     if (port > 0 && port < 65536) {
         v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
diff -r 2c7b488a61fb -r b3b7e33083ac src/http/v2/ngx_http_v2_filter_module.c
--- a/src/http/v2/ngx_http_v2_filter_module.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/http/v2/ngx_http_v2_filter_module.c	Mon Jun 20 11:50:39 2016 +0300
@@ -137,10 +137,6 @@ ngx_http_v2_header_filter(ngx_http_reque
     ngx_http_v2_out_frame_t   *frame;
     ngx_http_core_loc_conf_t  *clcf;
     ngx_http_core_srv_conf_t  *cscf;
-    struct sockaddr_in        *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6       *sin6;
-#endif
     u_char                     addr[NGX_SOCKADDR_STRLEN];
 
     static const u_char nginx[5] = "\x84\xaa\x63\x55\xe7";
@@ -280,24 +276,7 @@ ngx_http_v2_header_filter(ngx_http_reque
                 }
             }
 
-            switch (fc->local_sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-            case AF_INET6:
-                sin6 = (struct sockaddr_in6 *) fc->local_sockaddr;
-                port = ntohs(sin6->sin6_port);
-                break;
-#endif
-#if (NGX_HAVE_UNIX_DOMAIN)
-            case AF_UNIX:
-                port = 0;
-                break;
-#endif
-            default: /* AF_INET */
-                sin = (struct sockaddr_in *) fc->local_sockaddr;
-                port = ntohs(sin->sin_port);
-                break;
-            }
+            port = ngx_inet_get_port(fc->local_sockaddr);
 
             location.len = sizeof("https://") - 1 + host.len
                            + r->headers_out.location->value.len;
diff -r 2c7b488a61fb -r b3b7e33083ac src/mail/ngx_mail.c
--- a/src/mail/ngx_mail.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/mail/ngx_mail.c	Mon Jun 20 11:50:39 2016 +0300
@@ -228,35 +228,11 @@ ngx_mail_add_ports(ngx_conf_t *cf, ngx_a
     in_port_t              p;
     ngx_uint_t             i;
     struct sockaddr       *sa;
-    struct sockaddr_in    *sin;
     ngx_mail_conf_port_t  *port;
     ngx_mail_conf_addr_t  *addr;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6   *sin6;
-#endif
 
     sa = &listen->sockaddr.sockaddr;
-
-    switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = &listen->sockaddr.sockaddr_in6;
-        p = sin6->sin6_port;
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        p = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = &listen->sockaddr.sockaddr_in;
-        p = sin->sin_port;
-        break;
-    }
+    p = ngx_inet_get_port(sa);
 
     port = ports->elts;
     for (i = 0; i < ports->nelts; i++) {
diff -r 2c7b488a61fb -r b3b7e33083ac src/mail/ngx_mail_auth_http_module.c
--- a/src/mail/ngx_mail_auth_http_module.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/mail/ngx_mail_auth_http_module.c	Mon Jun 20 11:50:39 2016 +0300
@@ -462,15 +462,11 @@ static void
 ngx_mail_auth_http_process_headers(ngx_mail_session_t *s,
     ngx_mail_auth_http_ctx_t *ctx)
 {
-    u_char               *p;
-    time_t                timer;
-    size_t                len, size;
-    ngx_int_t             rc, port, n;
-    ngx_addr_t           *peer;
-    struct sockaddr_in   *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6  *sin6;
-#endif
+    u_char      *p;
+    time_t       timer;
+    size_t       len, size;
+    ngx_int_t    rc, port, n;
+    ngx_addr_t  *peer;
 
     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
                    "mail auth http process headers");
@@ -813,20 +809,7 @@ ngx_mail_auth_http_process_headers(ngx_m
                 return;
             }
 
-            switch (peer->sockaddr->sa_family) {
-
-#if (NGX_HAVE_INET6)
-            case AF_INET6:
-                sin6 = (struct sockaddr_in6 *) peer->sockaddr;
-                sin6->sin6_port = htons((in_port_t) port);
-                break;
-#endif
-
-            default: /* AF_INET */
-                sin = (struct sockaddr_in *) peer->sockaddr;
-                sin->sin_port = htons((in_port_t) port);
-                break;
-            }
+            ngx_inet_set_port(peer->sockaddr, port);
 
             len = ctx->addr.len + 1 + ctx->port.len;
 
diff -r 2c7b488a61fb -r b3b7e33083ac src/stream/ngx_stream.c
--- a/src/stream/ngx_stream.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/stream/ngx_stream.c	Mon Jun 20 11:50:39 2016 +0300
@@ -243,35 +243,11 @@ ngx_stream_add_ports(ngx_conf_t *cf, ngx
     in_port_t                p;
     ngx_uint_t               i;
     struct sockaddr         *sa;
-    struct sockaddr_in      *sin;
     ngx_stream_conf_port_t  *port;
     ngx_stream_conf_addr_t  *addr;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6     *sin6;
-#endif
 
     sa = &listen->sockaddr.sockaddr;
-
-    switch (sa->sa_family) {
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = &listen->sockaddr.sockaddr_in6;
-        p = sin6->sin6_port;
-        break;
-#endif
-
-#if (NGX_HAVE_UNIX_DOMAIN)
-    case AF_UNIX:
-        p = 0;
-        break;
-#endif
-
-    default: /* AF_INET */
-        sin = &listen->sockaddr.sockaddr_in;
-        p = sin->sin_port;
-        break;
-    }
+    p = ngx_inet_get_port(sa);
 
     port = ports->elts;
     for (i = 0; i < ports->nelts; i++) {
diff -r 2c7b488a61fb -r b3b7e33083ac src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Mon Jun 20 10:41:17 2016 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Mon Jun 20 11:50:39 2016 +0300
@@ -446,12 +446,8 @@ static ngx_int_t
 ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u,
     ngx_stream_upstream_local_t *local)
 {
-    ngx_addr_t           *addr;
-    ngx_connection_t     *c;
-    struct sockaddr_in   *sin;
-#if (NGX_HAVE_INET6)
-    struct sockaddr_in6  *sin6;
-#endif
+    ngx_addr_t        *addr;
+    ngx_connection_t  *c;
 
     if (local == NULL) {
         u->peer.local = NULL;
@@ -484,21 +480,7 @@ ngx_stream_proxy_set_local(ngx_stream_se
     }
 
     ngx_memcpy(addr->sockaddr, c->sockaddr, c->socklen);
-
-    switch (addr->sockaddr->sa_family) {
-
-    case AF_INET:
-        sin = (struct sockaddr_in *) addr->sockaddr;
-        sin->sin_port = 0;
-        break;
-
-#if (NGX_HAVE_INET6)
-    case AF_INET6:
-        sin6 = (struct sockaddr_in6 *) addr->sockaddr;
-        sin6->sin6_port = 0;
-        break;
-#endif
-    }
+    ngx_inet_set_port(addr->sockaddr, 0);
 
     addr->name = c->addr_text;
     u->peer.local = addr;



More information about the nginx-devel mailing list