summaryrefslogtreecommitdiffstats
path: root/usr.sbin/nginx/src/core/ngx_connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/nginx/src/core/ngx_connection.c')
-rw-r--r--usr.sbin/nginx/src/core/ngx_connection.c129
1 files changed, 100 insertions, 29 deletions
diff --git a/usr.sbin/nginx/src/core/ngx_connection.c b/usr.sbin/nginx/src/core/ngx_connection.c
index 986bf0d0208..6b6e3b3a512 100644
--- a/usr.sbin/nginx/src/core/ngx_connection.c
+++ b/usr.sbin/nginx/src/core/ngx_connection.c
@@ -41,7 +41,7 @@ ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
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) {
@@ -82,6 +82,10 @@ ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
ls->setfib = -1;
#endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+ ls->fastopen = -1;
+#endif
+
return ls;
}
@@ -93,8 +97,10 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
ngx_uint_t i;
ngx_listening_t *ls;
socklen_t olen;
-#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
+#if (NGX_HAVE_DEFERRED_ACCEPT || NGX_HAVE_TCP_FASTOPEN)
ngx_err_t err;
+#endif
+#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
struct accept_filter_arg af;
#endif
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
@@ -123,7 +129,7 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
#if (NGX_HAVE_INET6)
case AF_INET6:
ls[i].addr_text_max_len = NGX_INET6_ADDRSTRLEN;
- len = NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1;
+ len = NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1;
break;
#endif
@@ -152,7 +158,8 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
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;
}
@@ -192,7 +199,9 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
#if (NGX_HAVE_SETFIB)
- if (getsockopt(ls[i].setfib, SOL_SOCKET, SO_SETFIB,
+ olen = sizeof(int);
+
+ if (getsockopt(ls[i].fd, SOL_SOCKET, SO_SETFIB,
(void *) &ls[i].setfib, &olen)
== -1)
{
@@ -206,6 +215,27 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
#endif
#endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+
+ olen = sizeof(int);
+
+ if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_FASTOPEN,
+ (void *) &ls[i].fastopen, &olen)
+ == -1)
+ {
+ err = ngx_socket_errno;
+
+ if (err != NGX_EOPNOTSUPP && err != NGX_ENOPROTOOPT) {
+ ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
+ "getsockopt(TCP_FASTOPEN) %V failed, ignored",
+ &ls[i].addr_text);
+ }
+
+ ls[i].fastopen = -1;
+ }
+
+#endif
+
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
ngx_memzero(&af, sizeof(struct accept_filter_arg));
@@ -214,7 +244,7 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, &af, &olen)
== -1)
{
- err = ngx_errno;
+ err = ngx_socket_errno;
if (err == NGX_EINVAL) {
continue;
@@ -247,7 +277,13 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, &olen)
== -1)
{
- ngx_log_error(NGX_LOG_NOTICE, cycle->log, ngx_errno,
+ err = ngx_socket_errno;
+
+ if (err == NGX_EOPNOTSUPP) {
+ continue;
+ }
+
+ ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
"getsockopt(TCP_DEFER_ACCEPT) for %V failed, ignored",
&ls[i].addr_text);
continue;
@@ -296,7 +332,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
continue;
}
- if (ls[i].fd != -1) {
+ if (ls[i].fd != (ngx_socket_t) -1) {
continue;
}
@@ -311,7 +347,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0);
- if (s == -1) {
+ if (s == (ngx_socket_t) -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_socket_n " %V failed", &ls[i].addr_text);
return NGX_ERROR;
@@ -463,16 +499,13 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
void
ngx_configure_listening_sockets(ngx_cycle_t *cycle)
{
- int keepalive;
+ int value;
ngx_uint_t i;
ngx_listening_t *ls;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
struct accept_filter_arg af;
#endif
-#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
- int timeout;
-#endif
ls = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
@@ -502,39 +535,51 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
}
if (ls[i].keepalive) {
- keepalive = (ls[i].keepalive == 1) ? 1 : 0;
+ value = (ls[i].keepalive == 1) ? 1 : 0;
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_KEEPALIVE,
- (const void *) &keepalive, sizeof(int))
+ (const void *) &value, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(SO_KEEPALIVE, %d) %V failed, ignored",
- keepalive, &ls[i].addr_text);
+ value, &ls[i].addr_text);
}
}
#if (NGX_HAVE_KEEPALIVE_TUNABLE)
if (ls[i].keepidle) {
+ value = ls[i].keepidle;
+
+#if (NGX_KEEPALIVE_FACTOR)
+ value *= NGX_KEEPALIVE_FACTOR;
+#endif
+
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPIDLE,
- (const void *) &ls[i].keepidle, sizeof(int))
+ (const void *) &value, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(TCP_KEEPIDLE, %d) %V failed, ignored",
- ls[i].keepidle, &ls[i].addr_text);
+ value, &ls[i].addr_text);
}
}
if (ls[i].keepintvl) {
+ value = ls[i].keepintvl;
+
+#if (NGX_KEEPALIVE_FACTOR)
+ value *= NGX_KEEPALIVE_FACTOR;
+#endif
+
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPINTVL,
- (const void *) &ls[i].keepintvl, sizeof(int))
+ (const void *) &value, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(TCP_KEEPINTVL, %d) %V failed, ignored",
- ls[i].keepintvl, &ls[i].addr_text);
+ value, &ls[i].addr_text);
}
}
@@ -564,6 +609,19 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
}
#endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+ if (ls[i].fastopen != -1) {
+ if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_FASTOPEN,
+ (const void *) &ls[i].fastopen, sizeof(int))
+ == -1)
+ {
+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
+ "setsockopt(TCP_FASTOPEN, %d) %V failed, ignored",
+ ls[i].fastopen, &ls[i].addr_text);
+ }
+ }
+#endif
+
#if 0
if (1) {
int tcp_nodelay = 1;
@@ -603,7 +661,7 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0)
== -1)
{
- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(SO_ACCEPTFILTER, NULL) "
"for %V failed, ignored",
&ls[i].addr_text);
@@ -630,7 +688,7 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
&af, sizeof(struct accept_filter_arg))
== -1)
{
- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(SO_ACCEPTFILTER, \"%s\") "
"for %V failed, ignored",
ls[i].accept_filter, &ls[i].addr_text);
@@ -653,20 +711,20 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
* if syncookies were used), hence we use 1 second timeout
* here.
*/
- timeout = 1;
+ value = 1;
} else {
- timeout = 0;
+ value = 0;
}
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT,
- &timeout, sizeof(int))
+ &value, sizeof(int))
== -1)
{
- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(TCP_DEFER_ACCEPT, %d) for %V failed, "
"ignored",
- timeout, &ls[i].addr_text);
+ value, &ls[i].addr_text);
continue;
}
@@ -859,7 +917,7 @@ ngx_close_connection(ngx_connection_t *c)
ngx_uint_t log_error, level;
ngx_socket_t fd;
- if (c->fd == -1) {
+ if (c->fd == (ngx_socket_t) -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
return;
}
@@ -1034,6 +1092,10 @@ ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
struct sockaddr_in6 *sin6;
#endif
+ if (c->local_socklen == 0) {
+ return NGX_ERROR;
+ }
+
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
@@ -1047,6 +1109,12 @@ ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
break;
#endif
+#if (NGX_HAVE_UNIX_DOMAIN)
+ case AF_UNIX:
+ addr = 1;
+ break;
+#endif
+
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
addr = sin->sin_addr.s_addr;
@@ -1068,13 +1136,16 @@ ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
}
ngx_memcpy(c->local_sockaddr, &sa, len);
+
+ c->local_socklen = len;
}
if (s == NULL) {
return NGX_OK;
}
- s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, port);
+ s->len = ngx_sock_ntop(c->local_sockaddr, c->local_socklen,
+ s->data, s->len, port);
return NGX_OK;
}