Browse Source

[core] fdevent setsockopt() helper functions

fdevent_set_tcp_nodelay()
fdevent_set_so_reuseaddr()
personal/stbuehler/mod-csrf
Glenn Strauss 4 years ago
parent
commit
93e91954a7
  1. 12
      src/fdevent.c
  2. 3
      src/fdevent.h
  3. 7
      src/gw_backend.c
  4. 15
      src/network.c

12
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));
}

3
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

7
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)*/
}
}

15
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;
}
}

Loading…
Cancel
Save