From 99882bc5c797bb199f55491d3609b77e79534da5 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 25 Feb 2016 17:27:07 -0800 Subject: [PATCH] Enable socket keepalives. --- adsbus/Makefile | 2 +- adsbus/incoming.c | 3 +++ adsbus/outgoing.c | 2 ++ adsbus/socket.c | 24 ++++++++++++++++++++++++ adsbus/socket.h | 3 +++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 adsbus/socket.c create mode 100644 adsbus/socket.h diff --git a/adsbus/Makefile b/adsbus/Makefile index 781104b..3c1dba0 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -5,7 +5,7 @@ LIBS ?= -ljansson -lprotobuf-c OBJ_NETWORK = incoming.o outgoing.o receive.o send.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o -OBJ_UTIL = buf.o hex.o opts.o packet.o peer.o rand.o resolve.o server.o uuid.o wakeup.o +OBJ_UTIL = buf.o hex.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o OBJ_PROTO = adsb.pb-c.o all: adsbus diff --git a/adsbus/incoming.c b/adsbus/incoming.c index a6d68c5..84ff62a 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -12,6 +12,7 @@ #include "peer.h" #include "resolve.h" +#include "socket.h" #include "wakeup.h" #include "uuid.h" @@ -65,6 +66,8 @@ static void incoming_handler(struct peer *peer) { local_hbuf, local_sbuf, peer_hbuf, peer_sbuf); + socket_init(fd); + incoming->handler(fd, incoming->passthrough, NULL); } diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 2495ff1..0434fec 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -10,6 +10,7 @@ #include "peer.h" #include "resolve.h" +#include "socket.h" #include "wakeup.h" #include "uuid.h" @@ -89,6 +90,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { case 0: fprintf(stderr, "O %s: Connected to %s/%s\n", outgoing->id, hbuf, sbuf); freeaddrinfo(outgoing->addrs); + socket_init(outgoing->peer.fd); outgoing->attempt = 0; int fd = outgoing->peer.fd; outgoing->peer.fd = -1; diff --git a/adsbus/socket.c b/adsbus/socket.c new file mode 100644 index 0000000..865638c --- /dev/null +++ b/adsbus/socket.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include + +#include "socket.h" + +void socket_init(int fd) { + int optval = 1; + int err = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)); + if (err == -1 && errno == ENOTSOCK) { + return; + } + assert(!err); + + optval = 30; + assert(!setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval))); + optval = 10; + assert(!setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval))); + optval = 3; + assert(!setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval))); +} diff --git a/adsbus/socket.h b/adsbus/socket.h new file mode 100644 index 0000000..9993af3 --- /dev/null +++ b/adsbus/socket.h @@ -0,0 +1,3 @@ +#pragma once + +void socket_init(int);