diff --git a/libr/socket/socket.c b/libr/socket/socket.c index 545a8950f0..e1f41a58de 100644 --- a/libr/socket/socket.c +++ b/libr/socket/socket.c @@ -348,44 +348,29 @@ R_API bool r_socket_connect(RSocket *s, const char *host, const char *port, int s->fd = -1; continue; } - if (timeout > 0) { - r_socket_block_time (s, 1, timeout, 0); - //fcntl (s->fd, F_SETFL, O_NONBLOCK, 1); - } + + r_socket_block_time (s, 0, 0, 0); ret = connect (s->fd, rp->ai_addr, rp->ai_addrlen); - if (timeout == 0 && ret == 0) { + if (ret == 0) { freeaddrinfo (res); return true; } - if (ret == 0 /* || nonblocking */) { + if (errno == EINPROGRESS) { struct timeval tv; - fd_set fdset, errset; - FD_ZERO (&fdset); - FD_SET (s->fd, &fdset); - tv.tv_sec = 1; //timeout; + tv.tv_sec = timeout; tv.tv_usec = 0; - if (r_socket_is_connected (s)) { - freeaddrinfo (res); - return true; - } - if (select (s->fd + 1, NULL, NULL, &errset, &tv) == 1) { - int so_error; - socklen_t len = sizeof so_error; - ret = getsockopt (s->fd, SOL_SOCKET, - SO_ERROR, &so_error, &len); - - if (ret == 0 && so_error == 0) { - //fcntl (s->fd, F_SETFL, O_NONBLOCK, 0); - //r_socket_block_time (s, 0, 0); + if ((ret = select (s->fd + 1, NULL, NULL, NULL, &tv)) != -1) { + if (r_socket_is_connected (s)) { freeaddrinfo (res); return true; } + } else { + perror ("connect"); } } - close (s->fd); - s->fd = -1; + r_socket_close (s); } freeaddrinfo (res); if (!rp) { @@ -638,6 +623,7 @@ R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout) { return NULL; } +// Only applies to read in UNIX R_API int r_socket_block_time(RSocket *s, int block, int sec, int usec) { #if __UNIX__ int ret, flags;