Skip to content

Commit

Permalink
report detailed error when tcp connect fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Mauchle committed Jun 28, 2018
1 parent ef6e5a0 commit f4ac0ad
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse) {
}

int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, int timeout) {
int origflags, r = -1;
int origflags, r = -1, sockerr = 0;
socklen_t errlen = sizeof(sockerr);
struct pollfd fds[1];

origflags = fcntl(s, F_GETFL, 0);
Expand All @@ -202,8 +203,15 @@ int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, in
if (poll(fds, 1, timeout * 1000) < 1)
goto exit;

if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL) ) {
debug(DBG_WARN, "Connection failed or refused");
if (fds[0].revents & POLLERR) {
if(!getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &errlen))
debug(DBG_WARN, "Connection failed: %s", strerror(sockerr));
else
debug(DBG_WARN, "Connection failed: unknown error");
} else if (fds[0].revents & POLLHUP) {
debug(DBG_WARN, "Connect error: hang up");
} else if (fds[0].revents & POLLNVAL) {
debug(DBG_WARN, "Connect error: fd not open");
} else if(fds[0].revents & POLLOUT) {
debug(DBG_DBG, "Connection up");
r = 0;
Expand Down

0 comments on commit f4ac0ad

Please sign in to comment.