Skip to content

Commit

Permalink
[Android] Throw PNSE for unavailable network information (dotnet#63633)
Browse files Browse the repository at this point in the history
* Update tests

* Add android specific implementation

* Add UnsupportedOSPlatform attributes

* Fix typo

* Remove unnecessary file reference

* Clean-up code

* Minor code clean-up

* Remove dictionary

* Refactoring

* Revert comment change
  • Loading branch information
simonrozsival committed Jan 13, 2022
1 parent 6f49429 commit 7c28896
Show file tree
Hide file tree
Showing 20 changed files with 774 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ public virtual void CopyTo(System.Net.NetworkInformation.IPAddressInformation[]
public abstract partial class IPGlobalProperties
{
protected IPGlobalProperties() { }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract string DhcpScopeName { get; }
public abstract string DomainName { get; }
public abstract string HostName { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsWinsProxy { get; }
public abstract System.Net.NetworkInformation.NetBiosNodeType NodeType { get; }
public virtual System.IAsyncResult BeginGetUnicastAddresses(System.AsyncCallback? callback, object? state) { throw null; }
Expand All @@ -136,58 +138,92 @@ protected IPGlobalProperties() { }
public abstract System.Net.IPEndPoint[] GetActiveTcpListeners();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.IPEndPoint[] GetActiveUdpListeners();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IcmpV4Statistics GetIcmpV4Statistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IcmpV6Statistics GetIcmpV6Statistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("illumos")]
[System.Runtime.Versioning.UnsupportedOSPlatform("solaris")]
public static System.Net.NetworkInformation.IPGlobalProperties GetIPGlobalProperties() { throw null; }
public abstract System.Net.NetworkInformation.IPGlobalStatistics GetIPv4GlobalStatistics();
public abstract System.Net.NetworkInformation.IPGlobalStatistics GetIPv6GlobalStatistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.TcpStatistics GetTcpIPv4Statistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.TcpStatistics GetTcpIPv6Statistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.UdpStatistics GetUdpIPv4Statistics();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.UdpStatistics GetUdpIPv6Statistics();
public virtual System.Net.NetworkInformation.UnicastIPAddressInformationCollection GetUnicastAddresses() { throw null; }
public virtual System.Threading.Tasks.Task<System.Net.NetworkInformation.UnicastIPAddressInformationCollection> GetUnicastAddressesAsync() { throw null; }
}
public abstract partial class IPGlobalStatistics
{
protected IPGlobalStatistics() { }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract int DefaultTtl { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool ForwardingEnabled { get; }
public abstract int NumberOfInterfaces { get; }
public abstract int NumberOfIPAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract int NumberOfRoutes { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long OutputPacketRequests { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long OutputPacketRoutingDiscards { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long OutputPacketsDiscarded { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long OutputPacketsWithNoRoute { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketFragmentFailures { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketReassembliesRequired { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketReassemblyFailures { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketReassemblyTimeout { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketsFragmented { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long PacketsReassembled { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPackets { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsDelivered { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsDiscarded { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsForwarded { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsWithAddressErrors { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsWithHeadersErrors { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract long ReceivedPacketsWithUnknownProtocol { get; }
}
public abstract partial class IPInterfaceProperties
{
protected IPInterfaceProperties() { }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IPAddressInformationCollection AnycastAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IPAddressCollection DhcpServerAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IPAddressCollection DnsAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract string DnsSuffix { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.GatewayIPAddressInformationCollection GatewayAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsDnsEnabled { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsDynamicDnsEnabled { get; }
public abstract System.Net.NetworkInformation.MulticastIPAddressInformationCollection MulticastAddresses { get; }
public abstract System.Net.NetworkInformation.UnicastIPAddressInformationCollection UnicastAddresses { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract System.Net.NetworkInformation.IPAddressCollection WinsServersAddresses { get; }
public abstract System.Net.NetworkInformation.IPv4InterfaceProperties GetIPv4Properties();
public abstract System.Net.NetworkInformation.IPv6InterfaceProperties GetIPv6Properties();
Expand All @@ -212,11 +248,16 @@ public abstract partial class IPv4InterfaceProperties
{
protected IPv4InterfaceProperties() { }
public abstract int Index { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsAutomaticPrivateAddressingActive { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsAutomaticPrivateAddressingEnabled { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsDhcpEnabled { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool IsForwardingEnabled { get; }
public abstract int Mtu { get; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public abstract bool UsesWins { get; }
}
public abstract partial class IPv4InterfaceStatistics
Expand Down Expand Up @@ -316,7 +357,9 @@ protected NetworkInterface() { }
[System.Runtime.Versioning.UnsupportedOSPlatform("solaris")]
public static System.Net.NetworkInformation.NetworkInterface[] GetAllNetworkInterfaces() { throw null; }
public virtual System.Net.NetworkInformation.IPInterfaceProperties GetIPProperties() { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public virtual System.Net.NetworkInformation.IPInterfaceStatistics GetIPStatistics() { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public virtual System.Net.NetworkInformation.IPv4InterfaceStatistics GetIPv4Statistics() { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatform("illumos")]
[System.Runtime.Versioning.UnsupportedOSPlatform("solaris")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris;$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris;$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
Expand Down Expand Up @@ -118,8 +118,8 @@
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs" Link="Common\Interop\CoreLib\Unix\Interop.Errors.cs" />
<Compile Include="$(CommonPath)System\IO\RowConfigReader.cs" Link="Common\System\IO\RowConfigReader.cs" />
</ItemGroup>
<!-- Linux -->
<ItemGroup Condition="'$(TargetsLinux)' == 'true'">
<!-- Linux (other than Android) -->
<ItemGroup Condition="'$(TargetsLinux)' == 'true' and '$(TargetsAndroid)' != 'true'">
<Compile Include="System\Net\NetworkInformation\ExceptionHelper.Linux.cs" />
<Compile Include="System\Net\NetworkInformation\LinuxIcmpV4Statistics.cs" />
<Compile Include="System\Net\NetworkInformation\LinuxIcmpV6Statistics.cs" />
Expand All @@ -144,6 +144,17 @@
<Compile Include="$(CommonPath)System\IO\StringParser.cs" Link="Common\System\IO\StringParser.cs" />
<Compile Include="$(CommonPath)Interop\Linux\Interop.LinuxNetDeviceFlags.cs" Link="Common\Interop\Linux\Interop.LinuxNetDeviceFlags.cs" />
</ItemGroup>
<!-- Android -->
<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
<Compile Include="System\Net\NetworkInformation\IPGlobalPropertiesPal.Android.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidIPGlobalProperties.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidIPGlobalStatistics.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidIPInterfaceProperties.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidIPv4InterfaceProperties.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidIPv6InterfaceProperties.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkInterfacePal.Android.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidNetworkInterface.cs" />
</ItemGroup>
<!-- OSX -->
<ItemGroup Condition="'$(TargetsOSX)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsFreeBSD)' == 'true'">
<Compile Include="System\Net\NetworkInformation\IPGlobalPropertiesPal.Bsd.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Net.NetworkInformation
{
internal sealed class AndroidIPGlobalProperties : UnixIPGlobalProperties
{
public override TcpConnectionInformation[] GetActiveTcpConnections() => throw new PlatformNotSupportedException();

public override IPEndPoint[] GetActiveTcpListeners() => throw new PlatformNotSupportedException();

public override IPEndPoint[] GetActiveUdpListeners() => throw new PlatformNotSupportedException();

public override IcmpV4Statistics GetIcmpV4Statistics() => throw new PlatformNotSupportedException();

public override IcmpV6Statistics GetIcmpV6Statistics() => throw new PlatformNotSupportedException();

public override IPGlobalStatistics GetIPv4GlobalStatistics()
=> new AndroidIPGlobalStatistics(ipv4: true);

public override IPGlobalStatistics GetIPv6GlobalStatistics()
=> new AndroidIPGlobalStatistics(ipv4: false);

public override TcpStatistics GetTcpIPv4Statistics() => throw new PlatformNotSupportedException();

public override TcpStatistics GetTcpIPv6Statistics() => throw new PlatformNotSupportedException();

public override UdpStatistics GetUdpIPv4Statistics() => throw new PlatformNotSupportedException();

public override UdpStatistics GetUdpIPv6Statistics() => throw new PlatformNotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Net.Sockets;

namespace System.Net.NetworkInformation
{
internal sealed class AndroidIPGlobalStatistics : IPGlobalStatistics
{
public AndroidIPGlobalStatistics(bool ipv4)
{
AndroidNetworkInterface[] networkInterfaces = NetworkInterfacePal.GetAndroidNetworkInterfaces();

foreach (var networkInterface in networkInterfaces)
{
var component = ipv4 ? NetworkInterfaceComponent.IPv4 : NetworkInterfaceComponent.IPv6;
if (networkInterface.Supports(component))
{
NumberOfInterfaces++;
}

foreach (UnixUnicastIPAddressInformation addressInformation in networkInterface.UnicastAddress)
{
bool isIPv4 = addressInformation.Address.AddressFamily == AddressFamily.InterNetwork;
if (isIPv4 == ipv4)
{
NumberOfIPAddresses++;
}
}

if (networkInterface.MulticastAddresess != null)
{
foreach (IPAddress address in networkInterface.MulticastAddresess)
{
bool isIPv4 = address.AddressFamily == AddressFamily.InterNetwork;
if (isIPv4 == ipv4)
{
NumberOfIPAddresses++;
}
}
}
}
}

public override int NumberOfInterfaces { get; }
public override int NumberOfIPAddresses { get; }

public override int DefaultTtl => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override bool ForwardingEnabled => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override int NumberOfRoutes => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long OutputPacketRequests => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long OutputPacketRoutingDiscards => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long OutputPacketsDiscarded => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long OutputPacketsWithNoRoute => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketFragmentFailures => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketReassembliesRequired => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketReassemblyFailures => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketReassemblyTimeout => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketsFragmented => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long PacketsReassembled => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPackets => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsDelivered => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsDiscarded => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsForwarded => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsWithAddressErrors => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsWithHeadersErrors => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override long ReceivedPacketsWithUnknownProtocol => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;

namespace System.Net.NetworkInformation
{
internal sealed class AndroidIPInterfaceProperties : UnixIPInterfaceProperties
{
private readonly AndroidIPv4InterfaceProperties _ipv4Properties;
private readonly AndroidIPv6InterfaceProperties _ipv6Properties;

public AndroidIPInterfaceProperties(AndroidNetworkInterface ani)
: base(ani, globalConfig: true)
{
_ipv4Properties = new AndroidIPv4InterfaceProperties(ani);
_ipv6Properties = new AndroidIPv6InterfaceProperties(ani);
}

public override IPv4InterfaceProperties GetIPv4Properties() => _ipv4Properties;
public override IPv6InterfaceProperties GetIPv6Properties() => _ipv6Properties;

public override bool IsDynamicDnsEnabled => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override IPAddressInformationCollection AnycastAddresses => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override GatewayIPAddressInformationCollection GatewayAddresses => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override IPAddressCollection DhcpServerAddresses => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
public override IPAddressCollection WinsServersAddresses => throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}
}
Loading

0 comments on commit 7c28896

Please sign in to comment.