Skip to content

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

Description

@8212369

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 .

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Sep 18, 2022
  2. danmoseley commented on Sep 18, 2022

    @danmoseley
    Contributor

    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

  3. 8212369 commented on Sep 20, 2022

    @8212369
    Author

    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));

  4. steveisok commented on Sep 20, 2022

    @steveisok
    Member
  5. forlayo commented on Sep 20, 2022

    @forlayo

    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()
    
  6. removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 21, 2022
  7. added this to the 8.0.0 milestone on Sep 21, 2022
  8. simonrozsival commented on Sep 27, 2022

    @simonrozsival
    Member

    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).

  9. forlayo commented on Sep 28, 2022

    @forlayo

    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

  10. Slm0nB commented on Sep 28, 2022

    @Slm0nB

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

  11. modified the milestones: 8.0.0, 7.0.x on Sep 28, 2022
  12. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Sep 29, 2022
  13. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Oct 3, 2022
  14. forlayo commented on Oct 5, 2022

    @forlayo

    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..

  15. jahmai-ca commented on Oct 10, 2022

    @jahmai-ca

    @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?

  16. MartinRothschink commented on Oct 19, 2022

    @MartinRothschink

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

  17. steveisok commented on Oct 19, 2022

    @steveisok
    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.

  18. ghost locked as resolved and limited conversation to collaborators on Nov 18, 2022
  19. karelz commented on Mar 22, 2023

    @karelz
    Member

    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.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions