From e365ceb02448ad19362c1d8bfd3e48536afa9097 Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Sat, 18 Sep 2021 21:25:30 +0200 Subject: enable TCP keepalived --- src/main.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') diff --git a/src/main.c b/src/main.c index d08336b..793dfb6 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ #include #include #include +#include #define TIP_TORRENT_PORT 9999 @@ -325,8 +326,14 @@ static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int ev ev_io_start(tip_main_loop, &cli->io); } +#define TIP_TCP_KEEPALIVE_IDLE 60 +#define TIP_TCP_KEEPALIVE_INTL 30 +#define TIP_TCP_KEEPALIVE_CNT 4 + static int tip_client_connect(const char *addr) { + int intl = TIP_TCP_KEEPALIVE_INTL, cnt = TIP_TCP_KEEPALIVE_CNT; + int on = 1, idle = TIP_TCP_KEEPALIVE_IDLE; struct tip_client *cli = &_cli; int remote_fd; int flags; @@ -337,6 +344,11 @@ static int tip_client_connect(const char *addr) if (remote_fd < 0) return -1; + setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(int)); + setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(int)); + setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPINTVL, &intl, sizeof(int)); + setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(int)); + flags = fcntl(remote_fd, F_GETFL); flags |= O_NONBLOCK; ret = fcntl(remote_fd, F_SETFL, flags); -- cgit v1.2.3-18-g5258