[PATCH rtems-libbsd 2/3] testsuite: Use the first available interface

Kinsey Moore kinsey.moore at oarcorp.com
Fri Jul 9 01:14:17 UTC 2021


An interface with index 1 is not guaranteed to exist since interfaces
that fail to attach do not release their allocated index by design.
Network tests now get the interface name from the first
available/lowest-indexed interface.
---
 .../rtems/bsd/test/default-network-init.h     | 27 ++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/testsuite/include/rtems/bsd/test/default-network-init.h b/testsuite/include/rtems/bsd/test/default-network-init.h
index ce1fc015..68652bbd 100644
--- a/testsuite/include/rtems/bsd/test/default-network-init.h
+++ b/testsuite/include/rtems/bsd/test/default-network-init.h
@@ -220,7 +220,32 @@ Init(rtems_task_argument arg)
 
 #ifndef DEFAULT_NETWORK_NO_INTERFACE_0
 #ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
-	ifname = if_indextoname(1, &ifnamebuf[0]);
+	{
+		struct ifaddrs *ifaddrs, *ifa;
+		sc = getifaddrs(&ifaddrs);
+		assert(sc >= 0);
+
+		/*
+		 * Get the first/lowest valid interface regardless of index
+		 * since interfaces that fail to come up still consume an index
+		 */
+		for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
+			if (ifa->ifa_addr &&
+			    ifa->ifa_addr->sa_family == AF_LINK)
+				break;
+		}
+
+		if (ifa == NULL) {
+			ifname = NULL;
+		}
+		else {
+			ifname = &ifnamebuf[0];
+			strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
+		}
+
+		freeifaddrs(ifaddrs);
+	}
+
 	assert(ifname != NULL);
 #else
 	ifname = NET_CFG_INTERFACE_0;
-- 
2.20.1



More information about the devel mailing list