From 01776c7a48eab492ecd1b90bcc87d88b9b5a888a Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 21 Sep 2010 22:27:35 +0100 Subject: [PATCH] fixes by David Powers (broadcast and >= 0) Dion't mess with linger. --- C/ypsocks.c | 60 ++++++++++++++++++++++++++++++++++++++------------ pl/sockets.yap | 2 +- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/C/ypsocks.c b/C/ypsocks.c index 6fde03ff9..97f4517bb 100755 --- a/C/ypsocks.c +++ b/C/ypsocks.c @@ -188,7 +188,6 @@ Yap_init_socks(char *host, long interface_port) struct sockaddr_in soadr; struct in_addr adr; struct hostent *he; - struct linger ling; /* For making sockets linger. */ #if USE_SOCKET @@ -224,10 +223,22 @@ Yap_init_socks(char *host, long interface_port) return; } +#if ENABLE_SO_LINGER + struct linger ling; /* disables socket lingering. */ ling.l_onoff = 1; ling.l_linger = 0; - setsockopt(s, SOL_SOCKET, SO_LINGER, (void *) &ling, - sizeof(ling)); + if (setsockopt(s, SOL_SOCKET, SO_LINGER, (void *) &ling, + sizeof(ling)) < 0) { +#if HAVE_STRERROR + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno)); +#else + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_linger)"); +#endif + return; + } +#endif r = connect ( s, (struct sockaddr *) &soadr, sizeof(soadr)); if (r<0) { @@ -760,7 +771,6 @@ p_socket_connect(void) struct hostent *he; struct sockaddr_in saddr; unsigned short int port; - struct linger ling; /* For making sockets linger. */ memset((void *)&saddr,(int) 0, sizeof(saddr)); if (IsVarTerm(thost)) { @@ -794,19 +804,41 @@ p_socket_connect(void) } saddr.sin_port = htons(port); saddr.sin_family = AF_INET; - ling.l_onoff = 1; - ling.l_linger = 0; - if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *) &ling, - sizeof(ling)) < 0) { +#if ENABLE_SO_LINGER + { + struct linger ling; /* For making sockets linger. */ + /* disabled: I see why no reason why we should throw things away by default!! */ + ling.l_onoff = 1; + ling.l_linger = 0; + if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *) &ling, + sizeof(ling)) < 0) { #if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno)); + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno)); #else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger)"); + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_linger)"); #endif - return(FALSE); + return FALSE; + } } +#endif + + { + int one = 1; /* code by David MW Powers */ + + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(one))) { +#if HAVE_STRERROR + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_broadcast: %s)", strerror(socket_errno)); +#else + Yap_Error(SYSTEM_ERROR, TermNil, + "socket_connect/3 (setsockopt_broadcast)"); +#endif + return FALSE; + } + } + flag = connect(fd,(struct sockaddr *)&saddr, sizeof(saddr)); if(flag<0) { #if HAVE_STRERROR @@ -816,7 +848,7 @@ p_socket_connect(void) Yap_Error(SYSTEM_ERROR, TermNil, "socket_connect/3 (connect)"); #endif - return(FALSE); + return FALSE; } Yap_UpdateSocketStream(sno, client_socket, af_inet); } else diff --git a/pl/sockets.yap b/pl/sockets.yap index 37550808a..0a5cce2fd 100644 --- a/pl/sockets.yap +++ b/pl/sockets.yap @@ -51,7 +51,7 @@ socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams) :- '$check_select_time'(Sec0:USec0, Sec, USec, _) :- Sec is Sec0, USec is USec0, - Sec > 0, USec > 0. + Sec >= 0, USec >= 0. '$cp_socket_fds'([], Fds, [], Fds). '$cp_socket_fds'([_|Scks], [[]|Fds], Out, StrFds) :- !,