#############################################################################*/
#include <errno.h>
+#include <netinet/tcp.h>
#include <signal.h>
#include <string.h>
#include <sys/epoll.h>
.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;
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,