>From b26973ea525de8cba96cef100c217bab5d66e0d8 Mon Sep 17 00:00:00 2001 From: felix Date: Sun, 16 Jun 2013 00:05:45 +0200 Subject: [PATCH] iOS needs socket-option to prevent SIGPIPE. Signed-off-by: Peter Bex --- tcp.scm | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tcp.scm b/tcp.scm index 30369a6..8fd1045 100644 --- a/tcp.scm +++ b/tcp.scm @@ -83,6 +83,27 @@ static WSADATA wsa; #endif static char addr_buffer[ 20 ]; + +static int C_set_socket_options(int socket) +{ + int yes = 1; + int r; + + r = setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes, sizeof(int)); + + if(r != 0) return r; + +#ifdef SO_NOSIGPIPE + /* + * Avoid SIGPIPE (iOS uses *only* SIGPIPE otherwise, not returning EPIPE). + * For consistency we do this everywhere the option is supported. + */ + r = setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&yes, sizeof(int)); +#endif + + return r; +} + EOF ) ) @@ -255,10 +276,7 @@ EOF (##sys#update-errno) (##sys#error "cannot create socket") ) ;; PLT makes this an optional arg to tcp-listen. Should we as well? - (when (eq? -1 ((foreign-lambda* int ((int socket)) - "int yes = 1; - C_return(setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes, sizeof(int)));") - s) ) + (when (eq? -1 ((foreign-lambda int "C_set_socket_options" int) s) ) (network-error/close 'tcp-listen "error while setting up socket" s) ) (when (eq? -1 (##net#bind s addr _sockaddr_in_size)) (network-error/close 'tcp-listen "cannot bind to socket" s host port) ) @@ -540,6 +558,8 @@ EOF (let ((s (##net#socket _af_inet _sock_stream 0)) ) (when (eq? -1 s) (network-error 'tcp-connect "cannot create socket" host port) ) + (when (eq? -1 ((foreign-lambda int "C_set_socket_options" int) s) ) + (network-error/close 'tcp-connect "error while setting up socket" s) ) (unless (##net#make-nonblocking s) (network-error/close 'tcp-connect "fcntl() failed" s) ) (let loop () -- 1.8.2.3