summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2017-09-23 10:30:08 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2017-09-23 10:30:08 -0400
commit93e91954a7058e122ec55421fd4c46acc8a5837d (patch)
treed742b1f2378e8815a71138ead7cf2001dac49368
parent17373221b8b52e3d087f2655517f5340050e4c71 (diff)
downloadlighttpd1.4-93e91954a7058e122ec55421fd4c46acc8a5837d.tar.gz
lighttpd1.4-93e91954a7058e122ec55421fd4c46acc8a5837d.zip
[core] fdevent setsockopt() helper functions
fdevent_set_tcp_nodelay() fdevent_set_so_reuseaddr()
-rw-r--r--src/fdevent.c12
-rw-r--r--src/fdevent.h3
-rw-r--r--src/gw_backend.c7
-rw-r--r--src/network.c15
4 files changed, 23 insertions, 14 deletions
diff --git a/src/fdevent.c b/src/fdevent.c
index 280181c1..232cfead 100644
--- a/src/fdevent.c
+++ b/src/fdevent.c
@@ -795,3 +795,15 @@ int fdevent_is_tcp_half_closed(int fd) {
return 0; /* false (not half-closed) or TCP state unknown */
#endif
}
+
+
+int fdevent_set_tcp_nodelay (const int fd, const int opt)
+{
+ return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+}
+
+
+int fdevent_set_so_reuseaddr (const int fd, const int opt)
+{
+ return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+}
diff --git a/src/fdevent.h b/src/fdevent.h
index a00bfe04..4453636d 100644
--- a/src/fdevent.h
+++ b/src/fdevent.h
@@ -243,5 +243,8 @@ int fdevent_connect_status(int fd);
/* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
int fdevent_is_tcp_half_closed(int fd);
+int fdevent_set_tcp_nodelay (const int fd, const int opt);
+
+int fdevent_set_so_reuseaddr (const int fd, const int opt);
#endif
diff --git a/src/gw_backend.c b/src/gw_backend.c
index 5dfa38cb..0bc4939e 100644
--- a/src/gw_backend.c
+++ b/src/gw_backend.c
@@ -487,7 +487,6 @@ static int gw_spawn_connection(server *srv, gw_host *host, gw_proc *proc, int de
/* server is not up, spawn it */
char_array env;
size_t i;
- int val;
int dfd = -1;
/* reopen socket */
@@ -498,8 +497,7 @@ static int gw_spawn_connection(server *srv, gw_host *host, gw_proc *proc, int de
return -1;
}
- val = 1;
- if (setsockopt(gw_fd,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val)) < 0) {
+ if (fdevent_set_so_reuseaddr(gw_fd, 1) < 0) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"socketsockopt failed:", strerror(errno));
close(gw_fd);
@@ -1646,8 +1644,7 @@ static void gw_set_state(server *srv, gw_handler_ctx *hctx, gw_connection_state_
void gw_set_transparent(server *srv, gw_handler_ctx *hctx) {
if (AF_UNIX != hctx->host->family) {
- int v = 1;
- if (-1 == setsockopt(hctx->fd,IPPROTO_TCP,TCP_NODELAY,&v,sizeof(v))) {
+ if (-1 == fdevent_set_tcp_nodelay(hctx->fd, 1)) {
/*(error, but not critical)*/
}
}
diff --git a/src/network.c b/src/network.c
index 382f570f..5c168376 100644
--- a/src/network.c
+++ b/src/network.c
@@ -70,7 +70,6 @@ static handler_t network_server_handle_fdevent(server *srv, void *context, int r
}
static int network_server_init(server *srv, buffer *host_token, size_t sidx) {
- int val;
socklen_t addr_len;
server_socket *srv_socket;
unsigned int port = 0;
@@ -226,9 +225,9 @@ static int network_server_init(server *srv, buffer *host_token, size_t sidx) {
if (AF_INET6 == srv_socket->addr.plain.sa_family
&& host != NULL) {
if (s->set_v6only) {
- val = 1;
+ int val = 1;
if (-1 == setsockopt(srv_socket->fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val))) {
- log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(IPV6_V6ONLY) failed:", strerror(errno));
+ log_error_write(srv, __FILE__, __LINE__, "ss", "setsockopt(IPV6_V6ONLY) failed:", strerror(errno));
goto error_free_socket;
}
} else {
@@ -241,16 +240,14 @@ static int network_server_init(server *srv, buffer *host_token, size_t sidx) {
/* */
srv->cur_fds = srv_socket->fd;
- val = 1;
- if (setsockopt(srv_socket->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(SO_REUSEADDR) failed:", strerror(errno));
+ if (fdevent_set_so_reuseaddr(srv_socket->fd, 1) < 0) {
+ log_error_write(srv, __FILE__, __LINE__, "ss", "setsockopt(SO_REUSEADDR) failed:", strerror(errno));
goto error_free_socket;
}
if (srv_socket->addr.plain.sa_family != AF_UNIX) {
- val = 1;
- if (setsockopt(srv_socket->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) {
- log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(TCP_NODELAY) failed:", strerror(errno));
+ if (fdevent_set_tcp_nodelay(srv_socket->fd, 1) < 0) {
+ log_error_write(srv, __FILE__, __LINE__, "ss", "setsockopt(TCP_NODELAY) failed:", strerror(errno));
goto error_free_socket;
}
}