Change in libosmocore[master]: socket: Fix bug in osmo_sock_init2(AF_UNSPEC) matching IP versions

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/.

pespin gerrit-no-reply at lists.osmocom.org
Mon Aug 31 17:01:39 UTC 2020


pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/19919 )


Change subject: socket: Fix bug in osmo_sock_init2(AF_UNSPEC) matching IP versions
......................................................................

socket: Fix bug in osmo_sock_init2(AF_UNSPEC) matching IP versions

See previous commit for a bug description.

Fixes: 2c962f5de1eeea119cfac7d9d92db31c570353b9

Change-Id: I59bf4b4b3ed14766a5a5285923d1ffa9fc8b2294
---
M src/socket.c
M tests/socket/socket_test.c
M tests/socket/socket_test.err
3 files changed, 22 insertions(+), 15 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/19/19919/1

diff --git a/src/socket.c b/src/socket.c
index e8a2ce2..56f01dc 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -316,19 +316,27 @@
 			}
 		}
 
-		/* priotize ipv6 as per RFC */
-		if (local_ipv6 && remote_ipv6)
-			family = AF_INET6;
-		else if (local_ipv4 && remote_ipv4)
-			family = AF_INET;
-		else {
-			if (local)
-				freeaddrinfo(local);
-			if (remote)
-				freeaddrinfo(remote);
-			LOGP(DLGLOBAL, LOGL_ERROR, "Unable to find a common protocol (IPv4 or IPv6) for local host: %s and remote host: %s.\n",
-			     local_host, remote_host);
-			return -ENODEV;
+		if ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) {
+			/* prioritize ipv6 as per RFC */
+			if (local_ipv6 && remote_ipv6)
+				family = AF_INET6;
+			else if (local_ipv4 && remote_ipv4)
+				family = AF_INET;
+			else {
+				if (local)
+					freeaddrinfo(local);
+				if (remote)
+					freeaddrinfo(remote);
+				LOGP(DLGLOBAL, LOGL_ERROR,
+				     "Unable to find a common protocol (IPv4 or IPv6) "
+				     "for local host: %s and remote host: %s.\n",
+				     local_host, remote_host);
+				return -ENODEV;
+			}
+		} else if ((flags & OSMO_SOCK_F_BIND)) {
+			family = local_ipv6 ? AF_INET6 : AF_INET;
+		} else if ((flags & OSMO_SOCK_F_CONNECT)) {
+			family = remote_ipv6 ? AF_INET6 : AF_INET;
 		}
 	}
 
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c
index 5cf20b7..ae77184 100644
--- a/tests/socket/socket_test.c
+++ b/tests/socket/socket_test.c
@@ -147,7 +147,7 @@
 	printf("Checking osmo_sock_init2(AF_UNSPEC) BIND on IPv4\n");
 	fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, NULL, 0,
 			     OSMO_SOCK_F_BIND);
-	OSMO_ASSERT(fd == -ENODEV); /* BUG! */
+	OSMO_ASSERT(fd >= 0);
 
 	talloc_free(name);
 
diff --git a/tests/socket/socket_test.err b/tests/socket/socket_test.err
index 37504e7..0f0f8da 100644
--- a/tests/socket/socket_test.err
+++ b/tests/socket/socket_test.err
@@ -2,4 +2,3 @@
 invalid: you have to specify either BIND or CONNECT flags
 Unable to find a common protocol (IPv4 or IPv6) for local host: 127.0.0.1 and remote host: ::1.
 Unable to find a common protocol (IPv4 or IPv6) for local host: ::1 and remote host: 127.0.0.1.
-Unable to find a common protocol (IPv4 or IPv6) for local host: 127.0.0.1 and remote host: (null).

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19919
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I59bf4b4b3ed14766a5a5285923d1ffa9fc8b2294
Gerrit-Change-Number: 19919
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200831/50051a77/attachment.htm>


More information about the gerrit-log mailing list