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

After investigating osmo-msc showing this log message and looking at the
code, it's a bit difficult to find out what's going on in the code:
socket.c:224 unable to bind socket: (null):0: Protocol not supported

The root cause was not yet found, but probably SCTP is not enabled in
the kernel of the host running it.

The cod eis most probably failing during socket() and not due to bind
error as the log says, so let's print an error if socket() fails.

Then, if setsockopt fails, we want to still keep trying in case an extra
addr was offered by addrinfo_helper. It is definetly wrong to continue
if setsockopt fails, because then we are skipping the bind(), which is a
fundamental part of what osmo_sock_init2 does.

Then, let's print the bind error when it really happens, and re-write
the extra log at the end if we reach the point at which no suitable addr
is found.

Change-Id: I1854422ad92dadf33ed4d849e15c0380c3bf1626
This commit is contained in:
Pau Espin 2018-04-05 17:00:22 +02:00 committed by Harald Welte
parent a8b6cc4cd9
commit 5d50fa50b3
1 changed files with 19 additions and 7 deletions

View File

@ -91,8 +91,11 @@ static int socket_helper(const struct addrinfo *rp, unsigned int flags)
int sfd, on = 1;
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
if (sfd == -1) {
LOGP(DLGLOBAL, LOGL_ERROR,
"unable to create socket: %s\n", strerror(errno));
return sfd;
}
if (flags & OSMO_SOCK_F_NONBLOCK) {
if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
LOGP(DLGLOBAL, LOGL_ERROR,
@ -212,20 +215,29 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto,
"cannot setsockopt socket:"
" %s:%u: %s\n",
local_host, local_port, strerror(errno));
break;
close(sfd);
continue;
}
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close(sfd);
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n",
local_host, local_port, strerror(errno));
close(sfd);
continue;
}
break;
}
freeaddrinfo(result);
if (rp == NULL) {
LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n",
local_host, local_port, strerror(errno));
LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n",
local_host, local_port);
return -ENODEV;
}
}
/* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it
was already closed and func returned. If OSMO_SOCK_F_BIND is not
set, then sfd = -1 */
/* figure out remote side of socket */
if (flags & OSMO_SOCK_F_CONNECT) {
result = addrinfo_helper(family, type, proto, remote_host, remote_port, false);