Skip to content

Commit

Permalink
socket: Make connecting sockets non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed May 15, 2020
1 parent d235570 commit 43d2697
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions common/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,37 @@ int socket_connect_unix(const char *filename)
strncpy(name.sun_path, filename, sizeof(name.sun_path));
name.sun_path[sizeof(name.sun_path) - 1] = 0;

if (connect(sfd, (struct sockaddr*)&name, sizeof(name)) < 0) {
int flags = fcntl(sfd, F_GETFL, 0);
fcntl(sfd, F_SETFL, flags | O_NONBLOCK);

do {
if (connect(sfd, (struct sockaddr*)&name, sizeof(name)) != -1) {
break;
}
if (errno == EINPROGRESS) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(sfd, &fds);

struct timeval timeout;
timeout.tv_sec = CONNECT_TIMEOUT / 1000;
timeout.tv_usec = (CONNECT_TIMEOUT - (timeout.tv_sec * 1000)) * 1000;
if (select(sfd + 1, NULL, &fds, NULL, &timeout) == 1) {
int so_error;
socklen_t len = sizeof(so_error);
getsockopt(sfd, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len);
if (so_error == 0) {
break;
}
}
}
socket_close(sfd);
sfd = -1;
} while (0);

if (sfd < 0) {
if (verbose >= 2)
fprintf(stderr, "%s: connect: %s\n", __func__,
strerror(errno));
fprintf(stderr, "%s: connect: %s\n", __func__, strerror(errno));
return -1;
}

Expand Down Expand Up @@ -286,7 +312,8 @@ int socket_connect(const char *addr, uint16_t port)
#ifdef WIN32
ioctlsocket(sfd, FIONBIO, &l_yes);
#else
fcntl(sfd, F_SETFL, O_NONBLOCK);
flags = fcntl(sfd, F_GETFL, 0);
fcntl(sfd, F_SETFL, flags | O_NONBLOCK);
#endif

if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
Expand Down Expand Up @@ -325,13 +352,6 @@ int socket_connect(const char *addr, uint16_t port)
return -1;
}

#ifdef WIN32
ioctlsocket(sfd, FIONBIO, &l_no);
#else
flags = fcntl(sfd, F_GETFL, 0);
fcntl(sfd, F_SETFL, flags & (~O_NONBLOCK));
#endif

#ifdef SO_NOSIGPIPE
if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) {
perror("setsockopt()");
Expand Down

0 comments on commit 43d2697

Please sign in to comment.