From aa11eea121614ecf87f73818b6a97189b21cd7b0 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 28 Feb 2016 22:21:07 -0800 Subject: [PATCH] Move more socket code into socket.c --- adsbus/receive.c | 4 ++-- adsbus/send.c | 4 ++-- adsbus/socket.c | 15 +++++++++++++++ adsbus/socket.h | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/adsbus/receive.c b/adsbus/receive.c index 8076d3b..bb463f5 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -14,6 +14,7 @@ #include "peer.h" #include "proto.h" #include "raw.h" +#include "socket.h" #include "send.h" #include "uuid.h" @@ -132,8 +133,7 @@ void receive_cleanup() { void receive_new(int fd, void __attribute__((unused)) *passthrough, struct peer *on_close) { peer_count_in++; - int res = shutdown(fd, SHUT_WR); - assert(res == 0 || (res == -1 && errno == ENOTSOCK)); + socket_receive_init(fd); struct receive *receive = malloc(sizeof(*receive)); assert(receive); diff --git a/adsbus/send.c b/adsbus/send.c index 43221d4..b93dd2a 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -18,6 +18,7 @@ #include "peer.h" #include "proto.h" #include "raw.h" +#include "socket.h" #include "stats.h" #include "uuid.h" @@ -114,8 +115,7 @@ struct serializer *send_get_serializer(char *name) { void send_new(int fd, struct serializer *serializer, struct peer *on_close) { peer_count_out++; - int res = shutdown(fd, SHUT_RD); - assert(res == 0 || (res == -1 && errno == ENOTSOCK)); + socket_send_init(fd); struct send *send = malloc(sizeof(*send)); assert(send); diff --git a/adsbus/socket.c b/adsbus/socket.c index 89bd56d..0dfaa57 100644 --- a/adsbus/socket.c +++ b/adsbus/socket.c @@ -8,16 +8,19 @@ #include "socket.h" void socket_pre_bind_init(int fd) { + // Called by transport code; safe to assume that fd is a socket int optval = 1; assert(!setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval))); } void socket_bound_init(int fd) { + // Called by transport code; safe to assume that fd is a socket int qlen = 5; assert(!setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen))); } void socket_connected_init(int fd) { + // Called by transport code; safe to assume that fd is a socket int optval = 1; assert(!setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval))); optval = 30; @@ -27,3 +30,15 @@ void socket_connected_init(int fd) { optval = 3; assert(!setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval))); } + +void socket_send_init(int fd) { + // Called by data flow code; NOT safe to assume that fd is a socket + int res = shutdown(fd, SHUT_RD); + assert(res == 0 || (res == -1 && errno == ENOTSOCK)); +} + +void socket_receive_init(int fd) { + // Called by data flow code; NOT safe to assume that fd is a socket + int res = shutdown(fd, SHUT_WR); + assert(res == 0 || (res == -1 && errno == ENOTSOCK)); +} diff --git a/adsbus/socket.h b/adsbus/socket.h index 12de4af..d5ae8dd 100644 --- a/adsbus/socket.h +++ b/adsbus/socket.h @@ -3,3 +3,5 @@ void socket_pre_bind_init(int); void socket_bound_init(int); void socket_connected_init(int); +void socket_send_init(int); +void socket_receive_init(int);