Commit 62bff456 authored by Karl Heyes's avatar Karl Heyes

make sure sock_connected does not block with timeout 0, use -1

for that, also handle the sock_connected return values properly
in the case of timeout.

svn path=/icecast/trunk/net/; revision=7349
parent bef7e4e0
...@@ -448,20 +448,22 @@ int sock_read_line(sock_t sock, char *buff, const int len) ...@@ -448,20 +448,22 @@ int sock_read_line(sock_t sock, char *buff, const int len)
} }
} }
/* see if a connection has been established /* see if a connection has been established. If timeout is < 0 then wait
* indefinitely, else wait for the stated number of seconds.
* return SOCK_TIMEOUT for timeout * return SOCK_TIMEOUT for timeout
* return SOCK_ERROR for failure * return SOCK_ERROR for failure
* return 0 for try again, interrupted * return 0 for try again, interrupted
* return 1 for ok * return 1 for ok
*/ */
int sock_connected (int sock, unsigned timeout) int sock_connected (int sock, int timeout)
{ {
fd_set wfds; fd_set wfds;
int val = SOCK_ERROR; int val = SOCK_ERROR;
socklen_t size = sizeof val; socklen_t size = sizeof val;
struct timeval tv, *timeval = NULL; struct timeval tv, *timeval = NULL;
if (timeout) /* make a timeout of <0 be indefinite */
if (timeout >= 0)
{ {
tv.tv_sec = timeout; tv.tv_sec = timeout;
tv.tv_usec = 0; tv.tv_usec = 0;
...@@ -531,8 +533,11 @@ int sock_connect_non_blocking (const char *hostname, const unsigned port) ...@@ -531,8 +533,11 @@ int sock_connect_non_blocking (const char *hostname, const unsigned port)
return sock; return sock;
} }
/* issue a connect, but return after the timeout (seconds) is reached. If
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) * timeout is 0 or less then we will wait until the OS gives up on the connect
* The socket is returned
*/
sock_t sock_connect_wto(const char *hostname, int port, int timeout)
{ {
int sock = SOCK_ERROR; int sock = SOCK_ERROR;
struct addrinfo *ai, *head, hints; struct addrinfo *ai, *head, hints;
...@@ -551,7 +556,7 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) ...@@ -551,7 +556,7 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
{ {
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0) if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
{ {
if (timeout) if (timeout > 0)
sock_set_blocking (sock, SOCK_NONBLOCK); sock_set_blocking (sock, SOCK_NONBLOCK);
if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0) if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
...@@ -562,13 +567,15 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) ...@@ -562,13 +567,15 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
{ {
if (sock_recoverable (sock_error())) if (sock_recoverable (sock_error()))
{ {
if (sock_connected (sock, timeout) > 0) int connected = sock_connected (sock, timeout);
if (connected == 0) /* try again, interrupted */
continue;
if (connected == 1) /* connected */
{ {
if (timeout) /* really wants to reset to what it was before */ if (timeout >= 0)
sock_set_blocking(sock, SOCK_BLOCK); sock_set_blocking(sock, SOCK_BLOCK);
break; break;
} }
continue;
} }
sock_close (sock); sock_close (sock);
sock = SOCK_ERROR; sock = SOCK_ERROR;
......
...@@ -114,7 +114,7 @@ int sock_close(sock_t sock); ...@@ -114,7 +114,7 @@ int sock_close(sock_t sock);
/* Connection related socket functions */ /* Connection related socket functions */
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout); sock_t sock_connect_wto(const char *hostname, const int port, const int timeout);
int sock_connect_non_blocking(const char *host, const unsigned port); int sock_connect_non_blocking(const char *host, const unsigned port);
int sock_connected(int sock, unsigned timeout); int sock_connected(int sock, int timeout);
/* Socket write functions */ /* Socket write functions */
int sock_write_bytes(sock_t sock, const void *buff, const size_t len); int sock_write_bytes(sock_t sock, const void *buff, const size_t len);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment