lib/util: allow to set specific TCP keepalive settings per socket.
authorGünther Deschner <gd@samba.org>
Wed, 20 Sep 2017 05:50:08 +0000 (07:50 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 10 Feb 2020 14:14:02 +0000 (15:14 +0100)
Guenther

Signed-off-by: Guenther Deschner <gd@samba.org>
lib/util/util_net.c
lib/util/util_net.h

index 19b5dff37255197a71bf3cff96883835bd7651dd..b8531c2abbf2bcf103a17de08b3cdf3cc910fb37 100644 (file)
@@ -1133,3 +1133,40 @@ void set_socket_options(int fd, const char *options)
        TALLOC_FREE(ctx);
        print_socket_options(fd);
 }
+
+#define SET_SOCK_OPT(_fd, _name, _value) do { \
+       char _val[100]; \
+       snprintf(_val, sizeof(_val) - 1, #_name "=%d", _value); \
+       set_socket_options(sock_fd, _val); \
+} while(0)
+
+void set_socket_options_keepalive(int sock_fd,
+                                 int keepalive_idle_time,
+                                 int keepalive_count,
+                                 int keepalive_interval)
+{
+       DBG_DEBUG("Setting TCP keepalive: idle %d, count: %d, interval: %d\n",
+                 keepalive_idle_time, keepalive_count, keepalive_interval);
+
+       set_socket_options(sock_fd, "SO_KEEPALIVE");
+#ifdef TCP_KEEPIDLE
+       SET_SOCK_OPT(sock_fd, TCP_KEEPIDLE,
+                    keepalive_idle_time);
+#endif
+#ifdef TCP_KEEPCNT
+       SET_SOCK_OPT(sock_fd, TCP_KEEPCNT,
+                    keepalive_count);
+#endif
+#ifdef TCP_KEEPINTVL
+       SET_SOCK_OPT(sock_fd, TCP_KEEPINTVL,
+                    keepalive_interval);
+#endif
+#ifdef TCP_KEEPALIVE_THRESHOLD
+       SET_SOCK_OPT(sock_fd, TCP_KEEPALIVE_THRESHOLD,
+                    keepalive_idle_time * 1000);
+#endif
+#ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
+       SET_SOCK_OPT(sock_fd, TCP_KEEPALIVE_ABORT_THRESHOLD,
+                    keepalive_idle_time * 1000);
+#endif
+}
index b0e4ecf4932a9e2bc9a168e710ef7b62ca42c44f..c50ab68d031832a3be851689223ff9a239636d90 100644 (file)
@@ -125,5 +125,9 @@ const char *client_socket_addr(int fd, char *addr, size_t addr_len);
 
 void set_socket_options(int fd, const char *options);
 void print_socket_options(int fd);
+void set_socket_options_keepalive(int sock_fd,
+                                 int keepalive_idle_time,
+                                 int keepalive_count,
+                                 int keepalive_interval);
 
 #endif /* _SAMBA_UTIL_NET_H_ */