Skip to content

[Android] NetworkInterface.GetAllNetworkInterfaces() returns strange values on Android 11 + IPv6 #6973

Closed
@Eilon

Description

@Eilon

Issue moved from dotnet/maui#6649


From @MartinRothschink on Friday, April 29, 2022 7:05:05 AM

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

Metadata

Metadata

Assignees

Labels

Area: Mono RuntimeMono-related issues: BCL bugs, AOT issues, etc.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions