From 27cf8df02408bf5e3b7c9140fb242019d9f1d671 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Thu, 5 Apr 2018 17:49:08 +0200 Subject: socket.c: osmo_sock_init2: connect: Several logic fixes and log improvements See explanations in previous commits. Change-Id: Ib2f7577b9f498ae9d388ed1f79f6ca0ec6f09664 --- src/socket.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/socket.c') 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; } } -- cgit v1.2.3