dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

NetworkInterface.GetAllNetworkInterfaces does not work correctly on some iOS devices #106577

Open amp64 opened 3 weeks ago

amp64 commented 3 weeks ago

Description

This API fails to return all of the interfaces, and when it does this it destabilizes the network stack in the host such that the debugger connection is trashed a few seconds afterwards.

Reproduction Steps

iPhone 13 Pro with two SIMs and on WiFi, running 17.5.1 Call this API, while debugging (debug build) from PC.

Expected behavior

This API should return all of the network interfaces, including "Wireless80211" which is the wifi interface.

Actual behavior

It returns 33 interfaces but the wifi one is not there. A few seconds later the debugger connection dies due to problems with 'UsbStream'. (Might not be releated, but only happens after this call is made).

Regression?

Unknown, this phone is new to me.

Known Workarounds

None found.

Configuration

.NET 8 Microsoft.iOS.Sdk/17.2.8053 iPhone 13 Pro with two SIMs and on WiFi, running 17.5.1 I have two other, older phones that are fine but don't return nearly the same number of network interfaces.

Other information

I see other, older bugs on Android and Linux that were the result of unexpected data from the underlying OS APIs, I'm guessing this coulde be related.

dotnet-policy-service[bot] commented 3 weeks ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 3 weeks ago

Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger See info in area-owners.md if you want to be subscribed.

ivanpovazan commented 2 weeks ago

Hello @amp64, could you share your testing/repro project?

Are you setting a breakpoint in FinishedLaunching method by any chance? If so, it happens because the app hasn't fully launched yet and the launch iOS watchdog terminates the app.

One suggestion would be to override OnActivated method in AppDelegate.cs e.g.,:

public override void OnActivated(UIApplication application)
{
    foreach (NetworkInterface netiface in NetworkInterface.GetAllNetworkInterfaces())
    {
        Console.WriteLine($"interface name : {netiface.Description}");
    }
    Console.WriteLine();
}

and debug that method instead, so that termination does not happen.


The above snippet prints out the following for me (tested on iPhone Xr iOS 17.5.1 with WiFi connected):

2024-08-29 15:34:11.169 repro106577[2055:744577] interface name : lo0
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip0
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip3
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip4
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip1
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip2
2024-08-29 15:34:11.170 repro106577[2055:744577] interface name : pdp_ip6
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : pdp_ip5
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : pdp_ip7
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : ap1
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : en0
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : utun0
2024-08-29 15:34:11.171 repro106577[2055:744577] interface name : utun1
2024-08-29 15:34:11.172 repro106577[2055:744577] interface name : utun2
2024-08-29 15:34:11.172 repro106577[2055:744577] interface name : utun3
2024-08-29 15:34:11.172 repro106577[2055:744577] interface name : anpi0
2024-08-29 15:34:11.172 repro106577[2055:744577] interface name : en1
2024-08-29 15:34:11.172 repro106577[2055:744577] interface name : en2
2024-08-29 15:34:11.173 repro106577[2055:744577] interface name : awdl0
2024-08-29 15:34:11.173 repro106577[2055:744577] interface name : llw0
2024-08-29 15:34:11.173 repro106577[2055:744577] interface name : utun4
2024-08-29 15:34:11.173 repro106577[2055:744577] interface name : utun5
2024-08-29 15:34:11.173 repro106577[2055:744577] interface name : utun6

ap1, en0, awdl0, llw0 are all Wireless80211.

Doing the "similar" in native code (ran from Xcode):

#include <ifaddrs.h>
...
struct ifaddrs* interfaces = NULL;
NSInteger success = getifaddrs(&interfaces);
if (success == 0)
{
    struct ifaddrs *interface = interfaces;
    while (interface != NULL)
    {
        NSString* name = [NSString stringWithUTF8String:interface->ifa_name];
        NSLog(@"interface name: %@", name);
        interface = interface->ifa_next;
    }
}
freeifaddrs(interfaces);

Prints out:

interface name: lo0
interface name: lo0
interface name: lo0
interface name: lo0
interface name: pdp_ip0
interface name: pdp_ip3
interface name: pdp_ip4
interface name: pdp_ip1
interface name: pdp_ip2
interface name: pdp_ip6
interface name: pdp_ip5
interface name: pdp_ip7
interface name: ap1
interface name: en0
interface name: en0
interface name: en0
interface name: en0
interface name: en0
interface name: utun0
interface name: utun0
interface name: utun1
interface name: utun1
interface name: utun2
interface name: utun2
interface name: utun3
interface name: utun3
interface name: anpi0
interface name: anpi0
interface name: en1
interface name: en2
interface name: en2
interface name: en2
interface name: awdl0
interface name: awdl0
interface name: llw0
interface name: llw0
interface name: utun4
interface name: utun4
interface name: utun5
interface name: utun5
interface name: utun6
interface name: utun6
interface name: utun6

Afaik, the only difference is that we return unique interfaces (@wfurt can confirm).

Let me know how we can help.

dotnet-policy-service[bot] commented 2 weeks ago

This issue has been marked needs-author-action and may be missing some important information.

wfurt commented 2 weeks ago

I was also wondering if there is mismatch between some friendly name and the actual OS interface. On all the other platforms we do not get duplicates e.g. if that what the API does on iOS filtering out seems very reasonable @ivanpovazan