Fix r_socket_connect not using the given timeout in unix ##network

Connect isn't effected by r_socket_block_time since it can only change the
timeout of read(and not implemented, write) actions with setsockopt.
Also, connect may wait for an unspecified amount of time so we have to use
select.
This commit is contained in:
yossizap 2019-11-16 15:17:56 +00:00 committed by radare
parent b5902beb36
commit ebfbf1a039

View File

@ -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;