From 8635449a060aae62af80dad139dc06fbb9a346d9 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 13 Jun 2020 21:39:39 +0200 Subject: [PATCH] Set NOSIGPIPE on all sockets. MacOS can raise a SIGPIPE when a local socket gets disconnected. --- src/meshlink.c | 10 ++++++++++ src/net_socket.c | 5 +++++ src/utcp-test.c | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/src/meshlink.c b/src/meshlink.c index d3cf36ad..a5d8d431 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -268,6 +268,11 @@ char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int fami int s = socket_in_netns(aip->ai_family, aip->ai_socktype, aip->ai_protocol, mesh->netns); +#ifdef SO_NOSIGPIPE + int nosigpipe = 1; + setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe)); +#endif + if(s >= 0) { set_timeout(s, 5000); @@ -2905,6 +2910,11 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { continue; } +#ifdef SO_NOSIGPIPE + int nosigpipe = 1; + setsockopt(state.sock, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe)); +#endif + set_timeout(state.sock, 5000); if(connect(state.sock, aip->ai_addr, aip->ai_addrlen)) { diff --git a/src/net_socket.c b/src/net_socket.c index 51a79c1d..5464b95f 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -71,6 +71,11 @@ static void configure_tcp(connection_t *c) { int lowdelay = IPTOS_LOWDELAY; setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay)); #endif + +#if defined(SO_NOSIGPIPE) + int nosigpipe = 1; + setsockopt(c->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&nosigpipe, sizeof(nosigpipe)); +#endif } static void retry_outgoing_handler(event_loop_t *loop, void *data) { diff --git a/src/utcp-test.c b/src/utcp-test.c index d78c4b86..564bfba8 100644 --- a/src/utcp-test.c +++ b/src/utcp-test.c @@ -270,6 +270,11 @@ int main(int argc, char *argv[]) { return 1; } } else { +#ifdef SO_NOSIGPIPE + int nosigpipe = 1; + setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe)); +#endif + if(connect(s, ai->ai_addr, ai->ai_addrlen)) { debug("Could not connect: %s\n", strerror(errno)); return 1; -- 2.39.5