summaryrefslogtreecommitdiffstats
path: root/src/socket.c
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2018-04-05 17:49:08 +0200
committerHarald Welte <laforge@gnumonks.org>2018-04-05 19:57:35 +0000
commit27cf8df02408bf5e3b7c9140fb242019d9f1d671 (patch)
tree61f4508f64d4b455414929d51b4f4be409b4b4ca /src/socket.c
parent5d50fa50b3f61c2ea6632cd25918440835d4a672 (diff)
socket.c: osmo_sock_init2: connect: Several logic fixes and log improvements
See explanations in previous commits. Change-Id: Ib2f7577b9f498ae9d388ed1f79f6ca0ec6f09664
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/socket.c b/src/socket.c
index 2310d75f..0e0aa240 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -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;
}
}