From 4517db75b571c778f8130fd125e2dc5f328b3750 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Fri, 28 Jun 2019 16:58:43 +0200 Subject: [PATCH 1/2] Properly disable SIGPIPE on OSX network sockets. Disable SO_NOSIGPIPE socket option when avaiable. Use MSG_NOSIGNAL send flag on systems that support it. --- drivers/unix/net_socket_posix.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp index c10443acde..16562e2c8f 100644 --- a/drivers/unix/net_socket_posix.cpp +++ b/drivers/unix/net_socket_posix.cpp @@ -55,10 +55,6 @@ #include -#if defined(__APPLE__) -#define MSG_NOSIGNAL SO_NOSIGPIPE -#endif - // BSD calls this flag IPV6_JOIN_GROUP #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP @@ -87,10 +83,6 @@ #define SOCK_IOCTL ioctlsocket #define SOCK_CLOSE closesocket -// Windows doesn't have this flag -#ifndef MSG_NOSIGNAL -#define MSG_NOSIGNAL 0 -#endif // Workaround missing flag in MinGW #if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET) #define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15) @@ -341,6 +333,13 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) { print_verbose("Unable to turn off UDP WSAENETRESET behaviour on Windows"); } } +#endif +#if defined(SO_NOSIGPIPE) + // Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS) + int par = 1; + if (setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, SOCK_CBUF(&par), sizeof(int)) != 0) { + print_verbose("Unable to turn off SIGPIPE on socket"); + } #endif return OK; } @@ -546,8 +545,10 @@ Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) { ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); int flags = 0; +#ifdef MSG_NOSIGNAL if (_is_stream) flags = MSG_NOSIGNAL; +#endif r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags); if (r_sent < 0) { From c540411a7f8186cea5d3277b0893239f3c57ef80 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Fri, 28 Jun 2019 17:13:38 +0200 Subject: [PATCH 2/2] Fix ip_unix.cpp inclusion order for OpenBSD. --- drivers/unix/ip_unix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp index 39ca9a823e..ce66f07a19 100644 --- a/drivers/unix/ip_unix.cpp +++ b/drivers/unix/ip_unix.cpp @@ -56,7 +56,6 @@ #endif // MINGW hack #endif #else // UNIX -#include #include #ifdef ANDROID_ENABLED // We could drop this file once we up our API level to 24, @@ -73,6 +72,7 @@ #ifdef __FreeBSD__ #include #endif +#include // Order is important on OpenBSD, leave as last #endif static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {