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() returns strange values on Android 11 + IPv6 #6649

Closed
MartinRothschink opened this issue Apr 29, 2022 · 4 comments
Labels
external partner/android Issues for the Android SDK partner Issue or Request from a partner team platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@MartinRothschink
Copy link

MartinRothschink commented Apr 29, 2022

Description

All tests have been done on a Xaomi Pad 5 (physical device, Android 11).

If target sdk is set to < 30, the values returned by NetworkInterface.GetAllNetworkInterfaces() seem valid:
ipv6-28

If target sdk is set to 31, the values returned by NetworkInterface.GetAllNetworkInterfaces() are outside of the enum ranges and name/description are empty. This screen shot shows the situation if IPv6 is not enabled in the router:
ipv4-31

If IPv6 is enabled in the router, NetworkInterfaceType and OperationalStatus values are even worse.
ipv6-31

Steps to Reproduce

  1. Create a new Maui app (VS 2022 17.2 Preview 5)
  2. Replace MainPage with the files below

MainPage.xaml.cs

namespace MauiApp1;

using System.Net.NetworkInformation;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        LoadNics();
        BindingContext = this;
    }

    public Nic[] Nics { get; set; }

    private void LoadNics()
    {
        var nics = new List<Nic>();
        var ifs = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var i in ifs)
        {
            var n = new Nic
            {
                Name = i.Name,
                Description = i.Description,
                Status = i.OperationalStatus,
                Type = i.NetworkInterfaceType
            };

            if (i.Supports(NetworkInterfaceComponent.IPv4))
                n.Supports += "IPv4 ";
            if (i.Supports(NetworkInterfaceComponent.IPv6))
                n.Supports += "IPv6 ";

            foreach (var a in i.GetIPProperties().UnicastAddresses)
            {
                if (a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    n.IPv6Addresses += a.Address.ToString() + " ";

                if (a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    n.IPv4Addresses += a.Address.ToString() + " ";
            }

            nics.Add(n);
        }

        Nics = nics.ToArray();
    }
}


public class Nic
{
    public string Name { get; set; }
    public string Description { get; set; }
    public OperationalStatus Status { get; set; }
    public NetworkInterfaceType Type { get; set; }
    public string Supports { get; set;}
    public string IPv4Addresses { get; set; }
    public string IPv6Addresses { get; set; }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage">

    <ListView ItemsSource="{Binding Nics, Mode=OneWay}" SelectionMode="None" RowHeight="{OnPlatform Android=140}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid ColumnSpacing="8">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Label Grid.Column="0" Grid.Row="0" Text="Name" FontAttributes="Bold" FontSize="Subtitle"/>
                        <Label Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Name}" FontSize="Subtitle"/>
                        <Label Grid.Column="1" Grid.Row="1" Text="Description" />
                        <Label Grid.Column="2" Grid.Row="1" Text="{Binding Description}" />
                        <Label Grid.Column="1" Grid.Row="2" Text="Status" />
                        <Label Grid.Column="2" Grid.Row="2" Text="{Binding Status}" />
                        <Label Grid.Column="1" Grid.Row="3" Text="Type" />
                        <Label Grid.Column="2" Grid.Row="3" Text="{Binding Type}" />
                        <Label Grid.Column="1" Grid.Row="4" Text="Supports" />
                        <Label Grid.Column="2" Grid.Row="4" Text="{Binding Supports}" />
                        <Label Grid.Column="1" Grid.Row="5" Text="IPv4Addresses" />
                        <Label Grid.Column="2" Grid.Row="5" Text="{Binding IPv4Addresses}" />
                        <Label Grid.Column="1" Grid.Row="6" Text="IPv6Addresses" />
                        <Label Grid.Column="2" Grid.Row="6" Text="{Binding IPv6Addresses}" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 10/11 (target sdk >= 30)

Did you find any workaround?

Currently I have to ignore the values for OperationalStatus and use the interface with valid UnicastAddresses.

Relevant log output

No response

@MartinRothschink MartinRothschink added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Apr 29, 2022
@v-longmin v-longmin added s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage and removed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels Apr 29, 2022
@v-longmin
Copy link

Verified repro on Android 12.0 with VS 17.3.0 Preview 1.0 [32427.455.main]. Repro with above project.

@Eilon
Copy link
Member

Eilon commented Apr 29, 2022

@jonathanpeppers - should this go to Xamarin Android? Or dotnet/runtime?

@jonathanpeppers
Copy link
Member

I would send this to xamarin/xamarin-android, thanks.

We have some code there that passes these values to the BCL.

@Eilon
Copy link
Member

Eilon commented Apr 29, 2022

This issue was moved to dotnet/android#6973

@dotnet dotnet locked and limited conversation to collaborators Apr 29, 2022
@Eilon Eilon closed this as completed Apr 29, 2022
@samhouts samhouts added partner Issue or Request from a partner team partner/android Issues for the Android SDK external labels May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
external partner/android Issues for the Android SDK partner Issue or Request from a partner team platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants