socket.c: osmo_sock_init2: connect: Several logic fixes and log improvements

See explanations in previous commits.

Change-Id: Ib2f7577b9f498ae9d388ed1f79f6ca0ec6f09664
This commit is contained in:
Pau Espin 2018-04-05 17:49:08 +02:00 committed by Harald Welte
parent 5d50fa50b3
commit 27cf8df024
1 changed files with 17 additions and 8 deletions

View File

@ -242,7 +242,8 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto,
if (flags & OSMO_SOCK_F_CONNECT) {
result = addrinfo_helper(family, type, proto, remote_host, remote_port, false);
if (!result) {
close(sfd);
if (sfd >= 0)
close(sfd);
return -EINVAL;
}
@ -260,16 +261,24 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto,
}
rc = connect(sfd, rp->ai_addr, rp->ai_addrlen);
if (rc != -1 || (rc == -1 && errno == EINPROGRESS))
break;
close(sfd);
sfd = -1;
if (rc != 0 && errno != EINPROGRESS) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n",
remote_host, remote_port, strerror(errno));
/* We want to maintain the bind socket if bind was enabled */
if (!(flags & OSMO_SOCK_F_BIND)) {
close(sfd);
sfd = -1;
}
continue;
}
break;
}
freeaddrinfo(result);
if (rp == NULL) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n",
remote_host, remote_port, strerror(errno));
LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n",
remote_host, remote_port);
if (sfd >= 0)
close(sfd);
return -ENODEV;
}
}