[nginx] Stream: embed ngx_stream_listen_t into ngx_stream_conf_a...
Ruslan Ermilov
ru at nginx.com
Mon Jun 8 20:30:02 UTC 2015
details: http://hg.nginx.org/nginx/rev/c13091e6292c
branches:
changeset: 6170:c13091e6292c
user: Ruslan Ermilov <ru at nginx.com>
date: Mon Jun 08 23:11:42 2015 +0300
description:
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
diffstat:
src/stream/ngx_stream.c | 74 ++++++++++++++-----------------------
src/stream/ngx_stream.h | 36 ++++++-----------
src/stream/ngx_stream_core_module.c | 14 ++++---
3 files changed, 49 insertions(+), 75 deletions(-)
diffs (292 lines):
diff -r f654addf0eea -r c13091e6292c src/stream/ngx_stream.c
--- a/src/stream/ngx_stream.c Fri Jun 05 23:03:13 2015 +0300
+++ b/src/stream/ngx_stream.c Mon Jun 08 23:11:42 2015 +0300
@@ -239,13 +239,13 @@ ngx_stream_add_ports(ngx_conf_t *cf, ngx
struct sockaddr_in6 *sin6;
#endif
- sa = (struct sockaddr *) &listen->sockaddr;
+ sa = &listen->u.sockaddr;
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
- sin6 = (struct sockaddr_in6 *) sa;
+ sin6 = &listen->u.sockaddr_in6;
p = sin6->sin6_port;
break;
#endif
@@ -257,7 +257,7 @@ ngx_stream_add_ports(ngx_conf_t *cf, ngx
#endif
default: /* AF_INET */
- sin = (struct sockaddr_in *) sa;
+ sin = &listen->u.sockaddr_in;
p = sin->sin_port;
break;
}
@@ -297,26 +297,7 @@ found:
return NGX_ERROR;
}
- addr->sockaddr = (struct sockaddr *) &listen->sockaddr;
- addr->socklen = listen->socklen;
- addr->ctx = listen->ctx;
- addr->bind = listen->bind;
- addr->wildcard = listen->wildcard;
- addr->so_keepalive = listen->so_keepalive;
-#if (NGX_HAVE_KEEPALIVE_TUNABLE)
- addr->tcp_keepidle = listen->tcp_keepidle;
- addr->tcp_keepintvl = listen->tcp_keepintvl;
- addr->tcp_keepcnt = listen->tcp_keepcnt;
-#endif
-#if (NGX_STREAM_SSL)
- addr->ssl = listen->ssl;
-#endif
-#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
- addr->ipv6only = listen->ipv6only;
-#endif
-#if (NGX_HAVE_REUSEPORT)
- addr->reuseport = listen->reuseport;
-#endif
+ addr->opt = *listen;
return NGX_OK;
}
@@ -346,8 +327,8 @@ ngx_stream_optimize_servers(ngx_conf_t *
* to the "*:port" only and ignore the other bindings
*/
- if (addr[last - 1].wildcard) {
- addr[last - 1].bind = 1;
+ if (addr[last - 1].opt.wildcard) {
+ addr[last - 1].opt.bind = 1;
bind_wildcard = 1;
} else {
@@ -358,12 +339,13 @@ ngx_stream_optimize_servers(ngx_conf_t *
while (i < last) {
- if (bind_wildcard && !addr[i].bind) {
+ if (bind_wildcard && !addr[i].opt.bind) {
i++;
continue;
}
- ls = ngx_create_listening(cf, addr[i].sockaddr, addr[i].socklen);
+ ls = ngx_create_listening(cf, &addr[i].opt.u.sockaddr,
+ addr[i].opt.socklen);
if (ls == NULL) {
return NGX_CONF_ERROR;
}
@@ -372,25 +354,25 @@ ngx_stream_optimize_servers(ngx_conf_t *
ls->handler = ngx_stream_init_connection;
ls->pool_size = 256;
- cscf = addr->ctx->srv_conf[ngx_stream_core_module.ctx_index];
+ cscf = addr->opt.ctx->srv_conf[ngx_stream_core_module.ctx_index];
ls->logp = cscf->error_log;
ls->log.data = &ls->addr_text;
ls->log.handler = ngx_accept_log_error;
- ls->keepalive = addr[i].so_keepalive;
+ ls->keepalive = addr[i].opt.so_keepalive;
#if (NGX_HAVE_KEEPALIVE_TUNABLE)
- ls->keepidle = addr[i].tcp_keepidle;
- ls->keepintvl = addr[i].tcp_keepintvl;
- ls->keepcnt = addr[i].tcp_keepcnt;
+ ls->keepidle = addr[i].opt.tcp_keepidle;
+ ls->keepintvl = addr[i].opt.tcp_keepintvl;
+ ls->keepcnt = addr[i].opt.tcp_keepcnt;
#endif
#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
- ls->ipv6only = addr[i].ipv6only;
+ ls->ipv6only = addr[i].opt.ipv6only;
#endif
#if (NGX_HAVE_REUSEPORT)
- ls->reuseport = addr[i].reuseport;
+ ls->reuseport = addr[i].opt.reuseport;
#endif
stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t));
@@ -451,15 +433,15 @@ ngx_stream_add_addrs(ngx_conf_t *cf, ngx
for (i = 0; i < stport->naddrs; i++) {
- sin = (struct sockaddr_in *) addr[i].sockaddr;
+ sin = &addr[i].opt.u.sockaddr_in;
addrs[i].addr = sin->sin_addr.s_addr;
- addrs[i].conf.ctx = addr[i].ctx;
+ addrs[i].conf.ctx = addr[i].opt.ctx;
#if (NGX_STREAM_SSL)
- addrs[i].conf.ssl = addr[i].ssl;
+ addrs[i].conf.ssl = addr[i].opt.ssl;
#endif
- len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf,
+ len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf,
NGX_SOCKADDR_STRLEN, 1);
p = ngx_pnalloc(cf->pool, len);
@@ -500,15 +482,15 @@ ngx_stream_add_addrs6(ngx_conf_t *cf, ng
for (i = 0; i < stport->naddrs; i++) {
- sin6 = (struct sockaddr_in6 *) addr[i].sockaddr;
+ sin6 = &addr[i].opt.u.sockaddr_in6;
addrs6[i].addr6 = sin6->sin6_addr;
- addrs6[i].conf.ctx = addr[i].ctx;
+ addrs6[i].conf.ctx = addr[i].opt.ctx;
#if (NGX_STREAM_SSL)
- addrs6[i].conf.ssl = addr[i].ssl;
+ addrs6[i].conf.ssl = addr[i].opt.ssl;
#endif
- len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf,
+ len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf,
NGX_SOCKADDR_STRLEN, 1);
p = ngx_pnalloc(cf->pool, len);
@@ -536,22 +518,22 @@ ngx_stream_cmp_conf_addrs(const void *on
first = (ngx_stream_conf_addr_t *) one;
second = (ngx_stream_conf_addr_t *) two;
- if (first->wildcard) {
+ if (first->opt.wildcard) {
/* a wildcard must be the last resort, shift it to the end */
return 1;
}
- if (second->wildcard) {
+ if (second->opt.wildcard) {
/* a wildcard must be the last resort, shift it to the end */
return -1;
}
- if (first->bind && !second->bind) {
+ if (first->opt.bind && !second->opt.bind) {
/* shift explicit bind()ed addresses to the start */
return -1;
}
- if (!first->bind && second->bind) {
+ if (!first->opt.bind && second->opt.bind) {
/* shift explicit bind()ed addresses to the start */
return 1;
}
diff -r f654addf0eea -r c13091e6292c src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h Fri Jun 05 23:03:13 2015 +0300
+++ b/src/stream/ngx_stream.h Mon Jun 08 23:11:42 2015 +0300
@@ -31,7 +31,18 @@ typedef struct {
typedef struct {
- u_char sockaddr[NGX_SOCKADDRLEN];
+ 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_char sockaddr_data[NGX_SOCKADDRLEN];
+ } u;
+
socklen_t socklen;
/* server ctx */
@@ -96,28 +107,7 @@ typedef struct {
typedef struct {
- struct sockaddr *sockaddr;
- socklen_t socklen;
-
- ngx_stream_conf_ctx_t *ctx;
-
- unsigned bind:1;
- unsigned wildcard:1;
-#if (NGX_STREAM_SSL)
- unsigned ssl:1;
-#endif
-#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
- unsigned ipv6only:1;
-#endif
-#if (NGX_HAVE_REUSEPORT)
- unsigned reuseport:1;
-#endif
- unsigned so_keepalive:2;
-#if (NGX_HAVE_KEEPALIVE_TUNABLE)
- int tcp_keepidle;
- int tcp_keepintvl;
- int tcp_keepcnt;
-#endif
+ ngx_stream_listen_t opt;
} ngx_stream_conf_addr_t;
diff -r f654addf0eea -r c13091e6292c src/stream/ngx_stream_core_module.c
--- a/src/stream/ngx_stream_core_module.c Fri Jun 05 23:03:13 2015 +0300
+++ b/src/stream/ngx_stream_core_module.c Mon Jun 08 23:11:42 2015 +0300
@@ -272,7 +272,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
for (i = 0; i < cmcf->listen.nelts; i++) {
- sa = (struct sockaddr *) ls[i].sockaddr;
+ sa = &ls[i].u.sockaddr;
if (sa->sa_family != u.family) {
continue;
@@ -284,7 +284,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
case AF_INET6:
off = offsetof(struct sockaddr_in6, sin6_addr);
len = 16;
- sin6 = (struct sockaddr_in6 *) sa;
+ sin6 = &ls[i].u.sockaddr_in6;
port = sin6->sin6_port;
break;
#endif
@@ -300,12 +300,14 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
default: /* AF_INET */
off = offsetof(struct sockaddr_in, sin_addr);
len = 4;
- sin = (struct sockaddr_in *) sa;
+ sin = &ls[i].u.sockaddr_in;
port = sin->sin_port;
break;
}
- if (ngx_memcmp(ls[i].sockaddr + off, u.sockaddr + off, len) != 0) {
+ if (ngx_memcmp(ls[i].u.sockaddr_data + off, u.sockaddr + off, len)
+ != 0)
+ {
continue;
}
@@ -325,7 +327,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
ngx_memzero(ls, sizeof(ngx_stream_listen_t));
- ngx_memcpy(ls->sockaddr, u.sockaddr, u.socklen);
+ ngx_memcpy(&ls->u.sockaddr, u.sockaddr, u.socklen);
ls->socklen = u.socklen;
ls->wildcard = u.wildcard;
@@ -347,7 +349,7 @@ ngx_stream_core_listen(ngx_conf_t *cf, n
struct sockaddr *sa;
u_char buf[NGX_SOCKADDR_STRLEN];
- sa = (struct sockaddr *) ls->sockaddr;
+ sa = &ls->u.sockaddr;
if (sa->sa_family == AF_INET6) {
More information about the nginx-devel
mailing list