socket.c: osmo_sock_init: Several logic fixes and log improvements

See explanations in previous commit.

Change-Id: I4889e777d8627fdfb52c97ab3ab353b6ed34aab2
This commit is contained in:
Pau Espin 2018-04-05 17:49:40 +02:00 committed by Harald Welte
parent 27cf8df024
commit 3a32147366
1 changed files with 16 additions and 8 deletions

View File

@ -339,8 +339,10 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
if (flags & OSMO_SOCK_F_CONNECT) {
rc = connect(sfd, rp->ai_addr, rp->ai_addrlen);
if (rc != -1 || (rc == -1 && errno == EINPROGRESS))
break;
if (rc != 0 && errno != EINPROGRESS) {
close(sfd);
continue;
}
} else {
rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
&on, sizeof(on));
@ -349,18 +351,24 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
"cannot setsockopt socket:"
" %s:%u: %s\n",
host, port, strerror(errno));
break;
close(sfd);
continue;
}
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:"
"%s:%u: %s\n",
host, port, strerror(errno));
close(sfd);
continue;
}
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
}
close(sfd);
break;
}
freeaddrinfo(result);
if (rp == NULL) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect/bind socket: %s:%u: %s\n",
host, port, strerror(errno));
LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n",
host, port);
return -ENODEV;
}