Skip to content

Commit e8e491b

Browse files
Filter local interface DNS results by address family (#128705)
> [!NOTE] > This PR description was generated with GitHub Copilot. Fixes #128371 `SystemNative_GetHostEntryForName` already passes the requested address family to `getaddrinfo` through `hint.ai_family`. However, when the queried name matches the value returned by `gethostname()`, the PAL augments those resolver results with local interface addresses from `getifaddrs`. That augmentation path was not applying the same address-family filter, so an IPv4-only lookup could still append IPv6 interface addresses, and vice versa. This became observable on Android after the minimum API level was raised from 21 to 24. Android API 24 exposes Bionic `getifaddrs`, so `HAVE_GETIFADDRS` is now true for Android native builds and this local-interface augmentation can run there. The failing CI pattern points specifically at Windows-hosted physical Android device lanes: those devices appear to enter the local-hostname augmentation path for `localhost`, while Ubuntu emulator lanes and a local Samsung physical device did not. The fix filters `getifaddrs` entries by the requested platform family in both the counting and population passes. Requests with `AF_UNSPEC` are intentionally left unchanged, so default/unspecified lookups can continue returning both IPv4 and IPv6 addresses as before. I considered making this Android-specific or filtering in managed code, but the mismatch is in the native PAL contract: callers pass an address family into `SystemNative_GetHostEntryForName`, `getaddrinfo` honors it, and only the native `getifaddrs` augmentation ignores it. Keeping the fix in native makes the whole returned `HostEntry` internally consistent across platforms. The behavioral impact on non-Android Unix platforms should be limited to local-hostname lookups that explicitly request IPv4 or IPv6; those calls should not receive opposite-family interface addresses. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a72c2a3 commit e8e491b

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/native/libs/System.Native/pal_networking.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,13 @@ int32_t SystemNative_GetHostEntryForName(const uint8_t* address, int32_t address
452452
continue;
453453
}
454454

455-
if (ifa->ifa_addr->sa_family == AF_INET)
455+
sa_family_t interfaceFamily = ifa->ifa_addr->sa_family;
456+
if (platformFamily != AF_UNSPEC && interfaceFamily != platformFamily)
457+
{
458+
continue;
459+
}
460+
461+
if (interfaceFamily == AF_INET)
456462
{
457463
// Remember if there's at least one non-loopback address for IPv4, so that they will be skipped.
458464
if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
@@ -462,7 +468,7 @@ int32_t SystemNative_GetHostEntryForName(const uint8_t* address, int32_t address
462468

463469
entry->IPAddressCount++;
464470
}
465-
else if (ifa->ifa_addr->sa_family == AF_INET6)
471+
else if (interfaceFamily == AF_INET6)
466472
{
467473
// Remember if there's at least one non-loopback address for IPv6, so that they will be skipped.
468474
if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
@@ -512,15 +518,21 @@ int32_t SystemNative_GetHostEntryForName(const uint8_t* address, int32_t address
512518
continue;
513519
}
514520

521+
sa_family_t interfaceFamily = ifa->ifa_addr->sa_family;
522+
if (platformFamily != AF_UNSPEC && interfaceFamily != platformFamily)
523+
{
524+
continue;
525+
}
526+
515527
// Skip loopback addresses if at least one interface has non-loopback one.
516-
if ((!includeIPv4Loopback && ifa->ifa_addr->sa_family == AF_INET && (ifa->ifa_flags & IFF_LOOPBACK) != 0) ||
517-
(!includeIPv6Loopback && ifa->ifa_addr->sa_family == AF_INET6 && (ifa->ifa_flags & IFF_LOOPBACK) != 0))
528+
if ((!includeIPv4Loopback && interfaceFamily == AF_INET && (ifa->ifa_flags & IFF_LOOPBACK) != 0) ||
529+
(!includeIPv6Loopback && interfaceFamily == AF_INET6 && (ifa->ifa_flags & IFF_LOOPBACK) != 0))
518530
{
519531
entry->IPAddressCount--;
520532
continue;
521533
}
522534

523-
if (CopySockAddrToIPAddress(ifa->ifa_addr, ifa->ifa_addr->sa_family, ipAddressList) == 0)
535+
if (CopySockAddrToIPAddress(ifa->ifa_addr, interfaceFamily, ipAddressList) == 0)
524536
{
525537
++ipAddressList;
526538
}

0 commit comments

Comments
 (0)