Closed SteveBush closed 2 years ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.
Author: | SteveBush |
---|---|
Assignees: | - |
Labels: | `area-System.Net`, `os-android`, `untriaged` |
Milestone: | - |
@steveisok I think it'd make sense to change the exception to PNSE
The NetworkInformation class works in Xamarin.Android. By not supporting it in net6.0-android, all of the NuGet packages which use the NetworkInformation would be unavailable on .net60. While I can create an abstraction layer for my code, I use some Nuget packages which no longer work if I compile for net6.0-android.
From what I can see from the code, it looks like the Android implementation of NetworkInformation uses the Linux PAL to call the native method getifaddrs(). In the Xamarin.Android, project there is a native implementation of getifaddrs() for Android.
https://github.com/xamarin/xamarin-android/blob/main/src/monodroid/jni/xamarin_getifaddrs.cc
For my purposes, I have created an abstraction layer for NetworkInformation that uses the ip link command to generate a list of network interfaces. I parse the output of ip link to implement NetworkInformation.GetAllNetworkInterfaces.
Below is the output from the ip link command.
generic_x86_arm:/ $ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc mq state UP mode DORMANT group default qlen 1000
link/ether 02:15:b2:00:00:00 brd ff:ff:ff:ff:ff:ff
6: ip_vti0@NONE: <NOARP> mtu 1364 qdisc noop state DOWN mode DEFAULT group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
7: ip6_vti0@NONE: <NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1
link/tunnel6 :: brd ::
8: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1
link/sit 0.0.0.0 brd 0.0.0.0
9: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN mode DEFAULT group default qlen 1
link/tunnel6 :: brd ::
11: radio0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 42:b4:98:b4:16:71 brd ff:ff:ff:ff:ff:ff link-netnsid 0
generic_x86_arm:/ $
I was thinking about the Netlink even for Linux and some other parts - like getting routes. I'm not sure about Andrioind user but on Linux GetAllActiveNetworkInterfaces
is used quite frequently.
Unfortunately, none of the solutions I proposed above work in API, level 31. The ip commands work fine on a simulator but fail on a device starting in API, level 31. Most of the networking APIs were deprecated in API level 29.
https://developer.android.com/reference/android/net/ConnectivityManager#getAllNetworkInfo()
getAllNetworks was deprecated in Level 31.
public Network[] getAllNetworks ()
This method was deprecated in API level 31.
This method does not provide any notification of network state changes, forcing apps to call it repeatedly. This is inefficient and prone to race conditions. Apps should use methods such as registerNetworkCallback(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback) instead. Apps that desire to obtain information about networks that do not apply to them can use NetworkRequest.Builder#setIncludeOtherUidNetworks.
The best option would be to use the Xamarin native implementation of getifaddrs().
Hello, I have (maybe similar issue) with NetworkInterface.GetAllNetworkInterfaces() under Android. When executing the method I am getting "System.Net.NetworkInformation.NetworkInformationException: 'Success'" exception instead of returing expected values.
the exception is thrown within static method at this line:
var interf = NetworkInterface.GetAllNetworkInterfaces();
Application min. API is 27, target API 31. / NET6 rc1
PJ.
Steps To Reproduce:
1) Dotnet new android 2) Add to csproj so LaunchProfiles start up
3) Modify MainActivity.cs by adding the following code. 4) Build and Run in Visual Studio 17.0.0 Preview 3.1
Result:
An exception is thrown and is null
Expected:
Related to System.Net.NetworkInformation needs Android PAL #51303 The System.Net.NetworkInformation APIs stopped working on Android. I have provided a simple repo. This is related to issue System.Net.NetworkInformation needs Android PAL #51303.
In the code below, a null exception is thrown. I have also seen the following exception in another project:
NetworkInformation.NetworkInformationException : No such file or directory
Suggestions:
I'm writing a mobile and desktop networking application so it would be helpful for the System.NetworkInterface APIs to work cross platform. Android stopped providing direct access to the system files in API level 24.
However, Android includes the ip and ifconfig commands which essentially read the contents of these protected system files and could be used to create a NetworkInterface on Android. See output below.
Alternately, the Android.Net Xamarin code could be used to construct a NetworkInterface.
Code in MainActivity.cs
Configuration Information:
DotNet Versions: 6.0.100-preview.7.21379.14 Android: targeting SDK version 30 Microsoft Visual Studio Enterprise 2022 Preview Version 17.0.0 Preview 3.1 VisualStudio.17.Preview/17.0.0-pre.3.1+31612.314 Microsoft .NET Framework Version 4.8.04084
Android Manifest: