Try to resolve interface names before removing nameless interfaces.
For interfaces that didn't get their name set, try to resolve their name
using if_indextoname first, before removing them from the list.
This allows getifaddrs() to keep returning interfaces that only have an
IPv6 address set (as opposed to the previous behavior where only
interfaces with an IPv4 address would be returned).
Change-Id: I0e4e6611948b12794cd3e354538f2964fbf31078
Fix: 148886805
Bug: 141455849
Test: atest NetworkInterfaceTest
Test: atest bionic-unit-tests-static
Test: atest IpSecManagerTunnelTest
diff --git a/libc/bionic/ifaddrs.cpp b/libc/bionic/ifaddrs.cpp
index e21ec40..0e9b544 100644
--- a/libc/bionic/ifaddrs.cpp
+++ b/libc/bionic/ifaddrs.cpp
@@ -248,11 +248,20 @@
}
}
-static void remove_nameless_interfaces(ifaddrs** list) {
+static void resolve_or_remove_nameless_interfaces(ifaddrs** list) {
ifaddrs_storage* addr = reinterpret_cast<ifaddrs_storage*>(*list);
ifaddrs_storage* prev_addr = nullptr;
while (addr != nullptr) {
ifaddrs* next_addr = addr->ifa.ifa_next;
+
+ // Try resolving interfaces without a name first.
+ if (strlen(addr->name) == 0) {
+ if (if_indextoname(addr->interface_index, addr->name) != nullptr) {
+ addr->ifa.ifa_name = addr->name;
+ }
+ }
+
+ // If the interface could not be resolved, remove it.
if (strlen(addr->name) == 0) {
if (prev_addr == nullptr) {
*list = next_addr;
@@ -296,8 +305,8 @@
if (!getlink_success) {
async_safe_format_log(ANDROID_LOG_INFO, "ifaddrs", "Failed to send RTM_GETLINK request");
// If we weren't able to depend on GETLINK messages, it's possible some
- // interfaces never got their name set. Remove those.
- remove_nameless_interfaces(out);
+ // interfaces never got their name set. Resolve them using if_indextoname or remove them.
+ resolve_or_remove_nameless_interfaces(out);
}
return 0;