Enable socket keepalives.

This commit is contained in:
Ian Gulliver
2016-02-25 17:27:07 -08:00
parent 3235211bc2
commit 99882bc5c7
5 changed files with 33 additions and 1 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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;

24
adsbus/socket.c Normal file
View File

@@ -0,0 +1,24 @@
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#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)));
}

3
adsbus/socket.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void socket_init(int);