]> git.ipfire.org Git - fireperf.git/commitdiff
client: Enable sending keepalive packets every 10 seconds
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 14:11:09 +0000 (14:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 14:11:09 +0000 (14:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/main.c
src/main.h

index 87026c9ba992c4a2eb13c305ae4fc6c6a2dadbfd..45ea2ad3e7f7c111292a080cadaa34d306fcab82 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <netinet/tcp.h>
 #include <signal.h>
 #include <string.h>
 #include <sys/epoll.h>
@@ -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;
index 77cad48112e04a117e980484506723310c82b10c..db9fe54269d97a1a72207e74010740707b152c21 100644 (file)
@@ -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,
index 9c5a396fb5c36825d2d6003c2623727c4ebc2672..fe7193697941958fad2daa17c7d4bc6e11fc8b58 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <netinet/in.h>
 
+#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;