From: Michael Tremer Date: Thu, 28 Jan 2021 14:11:09 +0000 (+0000) Subject: client: Enable sending keepalive packets every 10 seconds X-Git-Tag: 0.1.0~44 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=17482392928a9c1f32c83b4a77260919f7247691;p=fireperf.git client: Enable sending keepalive packets every 10 seconds Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index 87026c9..45ea2ad 100644 --- a/src/client.c +++ b/src/client.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -51,8 +52,53 @@ static int connect_socket(struct fireperf_config* conf, int fd) { .sin6_port = htons(conf->port), }; + // Enable keepalive + int flags = 1; + int r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&flags, sizeof(flags)); + if (r) { + ERROR(conf, "Could not set SO_KEEPALIVE on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + + // Set keepalive interval + if (conf->keepalive_interval) { + DEBUG(conf, "Setting keepalive interval to %d\n", conf->keepalive_interval); + + r = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, + (void*)&conf->keepalive_interval, sizeof(conf->keepalive_interval)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPINTVL on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + + DEBUG(conf, "Setting keepalive idle interval to %d\n", conf->keepalive_interval); + + r = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, + (void*)&flags, sizeof(flags)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPIDLE on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + } + + // Set keepalive count + if (conf->keepalive_count) { + DEBUG(conf, "Setting keepalive count to %d\n", conf->keepalive_count); + + r = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, + (void*)&conf->keepalive_count, sizeof(conf->keepalive_count)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPCNT on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + } + // Connect to the server - int r = connect(fd, &peer, sizeof(peer)); + r = connect(fd, &peer, sizeof(peer)); if (r && (errno != EINPROGRESS)) { ERROR(conf, "Could not connect to server: %s\n", strerror(errno)); return 1; diff --git a/src/main.c b/src/main.c index 77cad48..db9fe54 100644 --- a/src/main.c +++ b/src/main.c @@ -156,6 +156,8 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { int main(int argc, char* argv[]) { struct fireperf_config conf = { + .keepalive_count = DEFAULT_KEEPALIVE_COUNT, + .keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL, .loglevel = DEFAULT_LOG_LEVEL, .mode = FIREPERF_MODE_NONE, .port = DEFAULT_PORT, diff --git a/src/main.h b/src/main.h index 9c5a396..fe71936 100644 --- a/src/main.h +++ b/src/main.h @@ -23,6 +23,8 @@ #include +#define DEFAULT_KEEPALIVE_COUNT 3 +#define DEFAULT_KEEPALIVE_INTERVAL 10 #define DEFAULT_LOG_LEVEL LOG_INFO #define DEFAULT_PARALLEL 1 #define DEFAULT_PORT 5001 @@ -45,6 +47,8 @@ struct fireperf_config { } mode; struct in6_addr address; int keepalive_only; + int keepalive_count; + int keepalive_interval; int port; unsigned long parallel; unsigned int timeout;