diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-04-05 17:00:22 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2018-04-05 19:57:35 +0000 |
commit | 5d50fa50b3f61c2ea6632cd25918440835d4a672 (patch) | |
tree | ca452cf94c4fd6c95f59302bb19f81449ad14e75 /src | |
parent | a8b6cc4cd90ef0649aada4469f91c4e58819f1a2 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/socket.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/socket.c b/src/socket.c index 03789700..2310d75f 100644 --- a/src/socket.c +++ b/src/socket.c @@ -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); |