aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/server_socket.c8
-rw-r--r--src/socket_util.c15
-rw-r--r--src/socket_util.h3
3 files changed, 24 insertions, 2 deletions
diff --git a/src/server_socket.c b/src/server_socket.c
index 82ad81f12..fc8dbd17a 100644
--- a/src/server_socket.c
+++ b/src/server_socket.c
@@ -160,12 +160,16 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
size_t address_length = sizeof(address);
int fd = accept_cloexec_nonblock(s->fd, (struct sockaddr*)&address,
&address_length);
- if (fd >= 0)
+ if (fd >= 0) {
+ if (socket_keepalive(fd))
+ g_warning("Could not set TCP keepalive option: %s",
+ g_strerror(errno));
s->parent->callback(fd, (const struct sockaddr*)&address,
address_length, get_remote_uid(fd),
s->parent->callback_ctx);
- else
+ } else {
g_warning("accept() failed: %s", g_strerror(errno));
+ }
return true;
}
diff --git a/src/socket_util.c b/src/socket_util.c
index a89a67ed6..aa0a44e44 100644
--- a/src/socket_util.c
+++ b/src/socket_util.c
@@ -148,3 +148,18 @@ socket_bind_listen(int domain, int type, int protocol,
return fd;
}
+
+int
+socket_keepalive(int fd)
+{
+ const int reuse = 1;
+
+#ifdef WIN32
+ const char *optval = (const char *)&reuse;
+#else
+ const void *optval = &reuse;
+#endif
+
+ return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
+ optval, sizeof(reuse));
+}
diff --git a/src/socket_util.h b/src/socket_util.h
index 3ebf4084c..f27751aa3 100644
--- a/src/socket_util.h
+++ b/src/socket_util.h
@@ -63,4 +63,7 @@ socket_bind_listen(int domain, int type, int protocol,
int backlog,
GError **error);
+int
+socket_keepalive(int fd);
+
#endif