Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Sys
{
[StructLayout(LayoutKind.Sequential)]
public struct IPEndPointInfo
public unsafe struct IPEndPointInfo
{
public InlineArray16<byte> AddressBytes;
public fixed byte AddressBytes[16];
public uint NumAddressBytes;
public uint Port;
private uint __padding; // For native struct-size padding. Does not contain useful data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Sys
{
[StructLayout(LayoutKind.Sequential)]
public struct LinkLayerAddressInfo
public unsafe struct LinkLayerAddressInfo
{
public int InterfaceIndex;
public InlineArray8<byte> AddressBytes;
public fixed byte AddressBytes[8];
public byte NumAddressBytes;
private byte __padding; // For native struct-size padding. Does not contain useful data.
public ushort HardwareType;
}

[StructLayout(LayoutKind.Sequential)]
public struct IpAddressInfo
public unsafe struct IpAddressInfo
{
public int InterfaceIndex;
public InlineArray16<byte> AddressBytes;
public fixed byte AddressBytes[16];
public byte NumAddressBytes;
public byte PrefixLength;
private InlineArray2<byte> __padding;
private fixed byte __padding[2];
}

[StructLayout(LayoutKind.Sequential)]
public struct NetworkInterfaceInfo
public unsafe struct NetworkInterfaceInfo
{
public InlineArray16<byte> Name;
public fixed byte Name[16];
public long Speed;
public int InterfaceIndex;
public int Mtu;
public ushort HardwareType;
public byte OperationalState;
public byte NumAddressBytes;
public InlineArray8<byte> AddressBytes;
public fixed byte AddressBytes[8];
public byte SupportsMulticast;
private InlineArray3<byte> __padding;
private fixed byte __padding[3];
}

[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_EnumerateInterfaceAddresses")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfac
_index = networkInterfaceInfo->InterfaceIndex;
if (networkInterfaceInfo->NumAddressBytes > 0)
{
_physicalAddress = new PhysicalAddress(((ReadOnlySpan<byte>)networkInterfaceInfo->AddressBytes)[..networkInterfaceInfo->NumAddressBytes].ToArray());
_physicalAddress = new PhysicalAddress(new ReadOnlySpan<byte>(networkInterfaceInfo->AddressBytes, networkInterfaceInfo->NumAddressBytes).ToArray());
}

_mtu = networkInterfaceInfo->Mtu;
Expand All @@ -33,7 +33,7 @@ internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfac

internal unsafe void AddAddress(Interop.Sys.IpAddressInfo *addressInfo)
{
var address = new IPAddress(((ReadOnlySpan<byte>)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]);
var address = new IPAddress(new ReadOnlySpan<byte>(addressInfo->AddressBytes, addressInfo->NumAddressBytes));
if (address.IsIPv6LinkLocal)
{
address.ScopeId = addressInfo->InterfaceIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
continue;
}

IPAddress localIPAddress = new IPAddress(((ReadOnlySpan<byte>)nativeInfo.LocalEndPoint.AddressBytes)[..checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)]);
IPAddress localIPAddress = new IPAddress(new ReadOnlySpan<byte>(nativeInfo.LocalEndPoint.AddressBytes, checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)));
IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ?
IPAddress.Any :
new IPAddress(((ReadOnlySpan<byte>)nativeInfo.RemoteEndPoint.AddressBytes)[..checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)]);
new IPAddress(new ReadOnlySpan<byte>(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)));

IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
Expand Down Expand Up @@ -87,7 +87,7 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners()
int port = (int)endPointInfo.Port;
IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ?
IPAddress.Any :
new IPAddress(((ReadOnlySpan<byte>)endPointInfo.AddressBytes)[..checked((int)endPointInfo.NumAddressBytes)]);
new IPAddress(new ReadOnlySpan<byte>(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes)));

endPoints[i] = new IPEndPoint(ipAddress, port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI
{
Context* context = (Context*)pContext;

IPAddress ipAddress = new IPAddress(((ReadOnlySpan<byte>)gatewayAddressInfo->AddressBytes)[..gatewayAddressInfo->NumAddressBytes]);
IPAddress ipAddress = new IPAddress(new ReadOnlySpan<byte>(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes));
if (ipAddress.IsIPv6LinkLocal)
{
// For Link-Local addresses add ScopeId as that is not part of the route entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static bool IsMulticast(IPAddress address)
/// <returns>A new IPAddress created with the information in the native structure.</returns>
public static unsafe IPAddress GetIPAddressFromNativeInfo(Interop.Sys.IpAddressInfo* addressInfo)
{
IPAddress ipAddress = new IPAddress(((ReadOnlySpan<byte>)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]);
IPAddress ipAddress = new IPAddress(new ReadOnlySpan<byte>(addressInfo->AddressBytes, addressInfo->NumAddressBytes));
return ipAddress;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()

for (int i = 0; i < interfaceCount; i++)
{
var lni = new LinuxNetworkInterface(Utf8StringMarshaller.ConvertToManaged((byte*)&nii->Name)!, nii->InterfaceIndex, systemProperties);
var lni = new LinuxNetworkInterface(Utf8StringMarshaller.ConvertToManaged(nii->Name)!, nii->InterfaceIndex, systemProperties);
lni._interfaceType = (NetworkInterfaceType)nii->HardwareType;
lni._speed = nii->Speed;
lni._operationalStatus = (OperationalStatus)nii->OperationalState;
Expand All @@ -104,7 +104,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()

if (nii->NumAddressBytes > 0)
{
lni._physicalAddress = new PhysicalAddress(((ReadOnlySpan<byte>)nii->AddressBytes)[..nii->NumAddressBytes].ToArray());
lni._physicalAddress = new PhysicalAddress(new ReadOnlySpan<byte>(nii->AddressBytes, nii->NumAddressBytes).ToArray());
}

interfaces[i] = lni;
Expand All @@ -114,7 +114,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()

while (addressCount != 0)
{
var address = new IPAddress(((ReadOnlySpan<byte>)ai->AddressBytes)[..ai->NumAddressBytes]);
var address = new IPAddress(new ReadOnlySpan<byte>(ai->AddressBytes, ai->NumAddressBytes));
if (address.IsIPv6LinkLocal)
{
address.ScopeId = ai->InterfaceIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static unsafe AndroidNetworkInterface[] ToAndroidNetworkInterfaceArray(i
var networkInterfaceInfo = (Interop.Sys.NetworkInterfaceInfo*)networkInterfacesPtr;
for (int i = 0; i < interfaceCount; i++, networkInterfaceInfo++)
{
var name = Utf8StringMarshaller.ConvertToManaged((byte*)&networkInterfaceInfo->Name);
var name = Utf8StringMarshaller.ConvertToManaged(networkInterfaceInfo->Name);
networkInterfaces[i] = new AndroidNetworkInterface(name!, networkInterfaceInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo* addressInfo,

protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo* llAddr)
{
byte[] macAddress = ((ReadOnlySpan<byte>)llAddr->AddressBytes)[..llAddr->NumAddressBytes].ToArray();
byte[] macAddress = new byte[llAddr->NumAddressBytes];
fixed (byte* macAddressPtr = macAddress)
{
Buffer.MemoryCopy(llAddr->AddressBytes, macAddressPtr, llAddr->NumAddressBytes, llAddr->NumAddressBytes);
}
PhysicalAddress physicalAddress = new PhysicalAddress(macAddress);

_index = llAddr->InterfaceIndex;
Expand Down
Loading