[MERGED] libosmocore[master]: socket.c: osmo_sock_init2: bind: Several logic fixes and log...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Apr 5 19:57:36 UTC 2018


Harald Welte has submitted this change and it was merged.

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


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
---
M src/socket.c
1 file changed, 19 insertions(+), 7 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/socket.c b/src/socket.c
index 0378970..2310d75 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -91,8 +91,11 @@
 	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 @@
 					"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);

-- 
To view, visit https://gerrit.osmocom.org/7645
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1854422ad92dadf33ed4d849e15c0380c3bf1626
Gerrit-PatchSet: 3
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list