Merge pull request #30158 from Faless/net/no_sigpipe

Properly disable SIGPIPE on OSX/iOS network sockets.
This commit is contained in:
Rémi Verschelde 2019-06-29 12:03:13 +02:00 committed by GitHub
commit c49ba7de0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View file

@ -56,7 +56,6 @@
#endif // MINGW hack #endif // MINGW hack
#endif #endif
#else // UNIX #else // UNIX
#include <net/if.h>
#include <netdb.h> #include <netdb.h>
#ifdef ANDROID_ENABLED #ifdef ANDROID_ENABLED
// We could drop this file once we up our API level to 24, // We could drop this file once we up our API level to 24,
@ -73,6 +72,7 @@
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#include <net/if.h> // Order is important on OpenBSD, leave as last
#endif #endif
static IP_Address _sockaddr2ip(struct sockaddr *p_addr) { static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {

View file

@ -55,10 +55,6 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#if defined(__APPLE__)
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
// BSD calls this flag IPV6_JOIN_GROUP // BSD calls this flag IPV6_JOIN_GROUP
#if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP)
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
@ -87,10 +83,6 @@
#define SOCK_IOCTL ioctlsocket #define SOCK_IOCTL ioctlsocket
#define SOCK_CLOSE closesocket #define SOCK_CLOSE closesocket
// Windows doesn't have this flag
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
// Workaround missing flag in MinGW // Workaround missing flag in MinGW
#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET) #if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
#define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15) #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"); 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 #endif
return OK; 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); ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
int flags = 0; int flags = 0;
#ifdef MSG_NOSIGNAL
if (_is_stream) if (_is_stream)
flags = MSG_NOSIGNAL; flags = MSG_NOSIGNAL;
#endif
r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags); r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
if (r_sent < 0) { if (r_sent < 0) {