Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] NetworkInterface.GetAllNetworkInterfaces failed with NetworkInformationException (message: Success) #75809

Closed
8212369 opened this issue Sep 18, 2022 · 14 comments · Fixed by #76370 or #76541

Comments

@8212369
Copy link

8212369 commented Sep 18, 2022

Description

On Android 11 release build, I was encountering System.Net.NetworkInformation.NetworkInformationException when called System.Net.NetworkInformation.NetworkInterface。GetAllNetworkInterfaces, and the error string is just success. I have added two permissions: INTERNET and ACCESS_NETWORK_STATE to which no avail. I think it's worth mentioning.

I saw a log category named DOTNET_LINK so I reported the issue here.

Reproduction Steps

Using System.Net namespace, call NetworkInterface.GetAllNetworkInterfaces

Expected behavior

The function completes with no exception

Actual behavior

The function throws System.Net.NetworkInformationException

image

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6.0.9

Other information

I skimmed through the PRs and there was a change to exclusively use the Xamarin's implementation of getifaddrs recently. But on Android 11 Google blocked NETLINK_ROUTE family socket from being bind.

This is also the log that I was getting from adb

image

I think it's correspond to this. I hope I'm right.

One small note, I think this quirk is per vendor distribution, as my Xiaomi phone does not seems have this problem.

Related issues about this blocking .

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 18, 2022
@ghost
Copy link

ghost commented Sep 18, 2022

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

Issue Details

Description

On Android 11 release build, I was encountering System.Net.NetworkInformation.NetworkInformationException when called System.Net.NetworkInterface.NetworkInformation.GetAllNetworkInterfaces, and the error string is just success. I have added two permissions: INTERNET and ACCESS_NETWORK_STATE to which no avail. I think it's worth mentioning.

I saw a log category named DOTNET_LINK so I reported the issue here.

Reproduction Steps

Using System.Net namespace, call NetworkInterface.GetAllNetworkInterfaces

Expected behavior

The function completes with no exception

Actual behavior

The function throws System.Net.NetworkInformationException

image

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6.0.9

Other information

I skimmed through the PRs and there was a change to exclusively use the Xamarin's implementation of getifaddrs recently. But on Android 11 Google blocked NETLINK_ROUTE family socket from being bind.

This is also the log that I was getting from adb

image

I think it's correspond to this. I hope I'm right.

One small note, I think this quirk is per vendor distribution, as my Xiaomi phone does not seems have this problem.

Related issues about this blocking .

Author: 8212369
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

@danmoseley
Copy link
Member

Thanks - could you paste in the stacks/messages as regular text? You can use triple back tick above and below for formatting. This makes it possible for search to find them

@ghost
Copy link

ghost commented Sep 18, 2022

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

On Android 11 release build, I was encountering System.Net.NetworkInformation.NetworkInformationException when called System.Net.NetworkInformation.NetworkInterface。GetAllNetworkInterfaces, and the error string is just success. I have added two permissions: INTERNET and ACCESS_NETWORK_STATE to which no avail. I think it's worth mentioning.

I saw a log category named DOTNET_LINK so I reported the issue here.

Reproduction Steps

Using System.Net namespace, call NetworkInterface.GetAllNetworkInterfaces

Expected behavior

The function completes with no exception

Actual behavior

The function throws System.Net.NetworkInformationException

image

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6.0.9

Other information

I skimmed through the PRs and there was a change to exclusively use the Xamarin's implementation of getifaddrs recently. But on Android 11 Google blocked NETLINK_ROUTE family socket from being bind.

This is also the log that I was getting from adb

image

I think it's correspond to this. I hope I'm right.

One small note, I think this quirk is per vendor distribution, as my Xiaomi phone does not seems have this problem.

Related issues about this blocking .

Author: 8212369
Assignees: -
Labels:

area-System.Net, os-android, untriaged

Milestone: -

@8212369
Copy link
Author

8212369 commented Sep 20, 2022

Here is the exception stack:

[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.NetworkInformation.NetworkInformationException (0x80004005): Success
   at System.Net.NetworkInformation.NetworkInterfacePal.TransformNetworkInterfacess[AndroidNetworkInterface[]](Func`5 transform)
   at System.Net.NetworkInformation.NetworkInterfacePal.GetAndroidNetworkInterfaces()
   at System.Net.NetworkInformation.NetworkInterfacePal.GetAllNetworkInterfaces()
   at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
   at Microsoft.Phone.Net.NetworkInformation.NetworkInterface.get_NetworkInterfaceType()
   at TestApp.MetricsSender.CheckConnectionType()
   at System.Threading.Thread.StartCallback()

The log message I think to be related is this line. This is the only error line I found near this exception:

LOG_WARN("Failed to bind to the netlink socket. %s\n", strerror(errno));

@steveisok
Copy link
Member

/cc @simonrozsival

@forlayo
Copy link

forlayo commented Sep 20, 2022

Another one suffering the issue here , this is what I see on logcat..

...
09-20 20:38:26.869  6067  6110 W DOTNET_NETLINK: Failed to bind to the netlink socket. Permission denied
...
09-20 20:38:26.865  6067  6067 W com.displaynote.amadeus: type=1400 audit(0.0:23150): avc: denied { bind } for comm=2E4E455420546872656164506F6F6C scontext=u:r:untrusted_app:s0:c168,c256,c512,c768 tcontext=u:r:untrusted_app:s0:c168,c256,c512,c768 tclass=netlink_route_socket permissive=0 b/155595000 app=com.displaynote.amadeus
09-20 20:38:26.889  6067  6110 E System.Net.NetworkInformation.NetworkInformationException (0x80004005): Success
09-20 20:38:26.889  6067  6110 E Amadeus.Services.Discovery.Method.CloudDiscovery:    at System.Net.NetworkInformation.NetworkInterfacePal.TransformNetworkInterfacess[AndroidNetworkInterface[]](Func`5 transform)
09-20 20:38:26.889  6067  6110 E Amadeus.Services.Discovery.Method.CloudDiscovery:    at System.Net.NetworkInformation.NetworkInterfacePal.GetAndroidNetworkInterfaces()
09-20 20:38:26.889  6067  6110 E Amadeus.Services.Discovery.Method.CloudDiscovery:    at System.Net.NetworkInformation.NetworkInterfacePal.GetAllNetworkInterfaces()
09-20 20:38:26.889  6067  6110 E Amadeus.Services.Discovery.Method.CloudDiscovery:    at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()

@steveisok steveisok removed the untriaged New issue has not been triaged by the area owner label Sep 21, 2022
@steveisok steveisok added this to the 8.0.0 milestone Sep 21, 2022
@simonrozsival
Copy link
Member

simonrozsival commented Sep 27, 2022

I started looking into the problem and as a quick workaround it is possible to modify AndroidManifest.xml with:

<uses-sdk android:minSdkVersion="29" />

When the minSdkVersion is 29 or lower, the exception isn't thrown and a correct array of interfaces is returned (tested on a Samsung phone with Android 12). In the case when it is necessary to use 30 or newer, it is necessary to use Java.Net.NetworkInterface.NetworkInterfaces.

I'll keep working on a proper fix for this issue that will work with new SDKs.

EDIT: The targetSdkVersion needs to be 29 or lower which unfortunately makes this workaround unusable for apps uploaded to the Google Play Store (https://developer.android.com/google/play/requirements/target-sdk).

@simonrozsival simonrozsival self-assigned this Sep 27, 2022
@forlayo
Copy link

forlayo commented Sep 28, 2022

If anyone is looking for a workaround, without changing minSdkVersion and as @simonrozsival is suggesting with java bindings you can do something like this:

    public IList<IPAddress> GetAllLocalValidIp4Addresses()
    {
#if ANDROID

        // This is a workaround for this issue of MAUI on Android -> https://github.com/dotnet/runtime/issues/75809 
        // Instead of using regular .net we use Java Interop bindings directly.

        var inetEnum = Java.Net.NetworkInterface.NetworkInterfaces;
        if (inetEnum is null)
        {
            return new List<IPAddress>();
        }

        var ipAddresses = new List<IPAddress>();
        foreach (var interfaces in Java.Util.Collections.List(inetEnum))
        {
            var addresses = (interfaces as Java.Net.NetworkInterface)?.InetAddresses;
            if (addresses == null)
            {
                continue;
            }

            foreach (Java.Net.InetAddress address in Java.Util.Collections.List(addresses))
            {
                if (address.HostAddress == null || address.IsLoopbackAddress || address is not Java.Net.Inet4Address)
                {
                    continue;
                }
                ipAddresses.Add(IPAddress.Parse(address.HostAddress));
            }
        }

        return ipAddresses;
#else
#pragma warning disable CS8619 // This is not going to have any null, but the compiler is not able to determine it.
        return NetworkInterface
            .GetAllNetworkInterfaces()
            .Where(n => n.OperationalStatus == OperationalStatus.Up)
            .Where(n => n.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || n.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            .Where(n => !n.Description.ToLower().Contains("virtual"))
            .Where(n => !n.Description.ToLower().Contains("pseudo"))
            .SelectMany(n => n.GetIPProperties().UnicastAddresses)
            .Where(n => n.Address.AddressFamily == AddressFamily.InterNetwork)
            .Select(g => g?.Address)
            .Where(a => a != null) // Here I am checking to not adding anything null.
            .ToList();
#pragma warning restore CS8619
#endif
    }

Sorry @simonrozsival I didn't noticed you're a developer of the project :D

@Slm0nB
Copy link

Slm0nB commented Sep 28, 2022

We are struggling with this issue as well, and we looking forward to a fix. Thank you for the work around - appreciated!

@marek-safar marek-safar modified the milestones: 8.0.0, 7.0.x Sep 28, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Sep 29, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Oct 3, 2022
@forlayo
Copy link

forlayo commented Oct 5, 2022

I started looking into the problem and as a quick workaround it is possible to modify AndroidManifest.xml with:

<uses-sdk android:minSdkVersion="29" />

When the minSdkVersion is 29 or lower, the exception isn't thrown and a correct array of interfaces is returned (tested on a Samsung phone with Android 12). In the case when it is necessary to use 30 or newer, it is necessary to use Java.Net.NetworkInterface.NetworkInterfaces.

I'll keep working on a proper fix for this issue that will work with new SDKs.

EDIT: The targetSdkVersion needs to be 29 or lower which unfortunately makes this workaround unusable for apps uploaded to the Google Play Store (https://developer.android.com/google/play/requirements/target-sdk).

I am on an issue with a project that uses a third party library ( sipsorcery ) which is .net 6, even setting minSdkVersion on my project my app still crashes as now the crash is into that library.

I am checking how to patch it as it's opensource, but it's pure .net6 so not sure if I am going to be able to access Java.Net.NetworkInterface. It doesn't looks like that at first glance..

forlayo added a commit to forlayo/sipsorcery that referenced this issue Oct 5, 2022
This produces a crash when using SipSorcery on Android devices, as the
retrieval of all network interfaces creates an internal crash.
@jahmai-ca
Copy link

@simonrozsival I can see this has been back-ported to net7, but it also affects net6, and net7 isn't released yet. Can we get a back port to net6 please?

@MartinRothschink
Copy link

I second that, we need net6 support.
That function was always not working correctly on Android: dotnet/maui#6649

@steveisok
Copy link
Member

This was an oversight on my part as I only backported our change to .NET 7. I'll work to get this back to .NET 6.

sipsorcery pushed a commit to sipsorcery-org/sipsorcery that referenced this issue Oct 25, 2022
This produces a crash when using SipSorcery on Android devices, as the
retrieval of all network interfaces creates an internal crash.
@ghost ghost locked as resolved and limited conversation to collaborators Nov 18, 2022
@karelz
Copy link
Member

karelz commented Mar 22, 2023

Fixed in main (8.0) in PRs #76370 and #76541, and in 7.0 (RC2?) in PR #76565 and in 6.0.12 in PR #77260.

@karelz karelz modified the milestones: 7.0.x, 6.0.x Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.