sockets: support Mac OS X way of disabling SIGPIPE on a socket

Mac OS X doesn't have MSG_NOSIGNAL, so we need to use SO_NOSIGPIPE.

Signed-off-by: Steven Noonan <steven@uplinklabs.net>
This commit is contained in:
Steven Noonan 2014-04-13 04:01:38 -07:00
parent 268172ec41
commit ecf0ff3e7f
5 changed files with 34 additions and 2 deletions

View File

@ -253,6 +253,11 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key,
return NULL;
}
if (!set_socket_nosigpipe(sock)) {
kill_sock(sock);
return 0;
}
if (!(set_socket_nonblock(sock) && connect_sock_to(sock, ip_port))) {
kill_sock(sock);
return NULL;
@ -433,4 +438,4 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection)
kill_sock(TCP_connection->sock);
memset(TCP_connection, 0, sizeof(TCP_Client_Connection));
free(TCP_connection);
}
}

View File

@ -702,6 +702,11 @@ static int accept_connection(TCP_Server *TCP_server, sock_t sock)
return 0;
}
if (!set_socket_nosigpipe(sock)) {
kill_sock(sock);
return 0;
}
TCP_Secure_Connection *conn =
&TCP_server->incomming_connection_queue[TCP_server->incomming_connection_queue_index % MAX_INCOMMING_CONNECTIONS];

View File

@ -26,7 +26,7 @@
#include "net_crypto.h"
#include "onion.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__)
#define MSG_NOSIGNAL 0
#endif

View File

@ -150,6 +150,21 @@ int set_socket_nonblock(sock_t sock)
#endif
}
/* Set socket to not emit SIGPIPE
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nosigpipe(sock_t sock)
{
#if defined(__MACH__)
int set = 1;
return (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int)) == 0);
#else
return 1;
#endif
}
/* Set socket to dual (IPv4 + IPv6 socket)
*
* return 1 on success

View File

@ -332,6 +332,13 @@ void kill_sock(sock_t sock);
*/
int set_socket_nonblock(sock_t sock);
/* Set socket to not emit SIGPIPE
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nosigpipe(sock_t sock);
/* Set socket to dual (IPv4 + IPv6 socket)
*
* return 1 on success