diff --git a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs index 946059dc870934..58f6501a825c9e 100644 --- a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs +++ b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs @@ -2,7 +2,6 @@ // 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 @@ -10,9 +9,9 @@ internal static partial class Interop internal static partial class Sys { [StructLayout(LayoutKind.Sequential)] - public struct IPEndPointInfo + public unsafe struct IPEndPointInfo { - public InlineArray16 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. diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs index 99bfb7cba730b5..cb237ff1f1b2cc 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs @@ -1,7 +1,6 @@ // 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 @@ -9,38 +8,38 @@ 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 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 AddressBytes; + public fixed byte AddressBytes[16]; public byte NumAddressBytes; public byte PrefixLength; - private InlineArray2 __padding; + private fixed byte __padding[2]; } [StructLayout(LayoutKind.Sequential)] - public struct NetworkInterfaceInfo + public unsafe struct NetworkInterfaceInfo { - public InlineArray16 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 AddressBytes; + public fixed byte AddressBytes[8]; public byte SupportsMulticast; - private InlineArray3 __padding; + private fixed byte __padding[3]; } [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_EnumerateInterfaceAddresses")] diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs index 7a331560c69932..dcf66895aea7b1 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs @@ -19,7 +19,7 @@ internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfac _index = networkInterfaceInfo->InterfaceIndex; if (networkInterfaceInfo->NumAddressBytes > 0) { - _physicalAddress = new PhysicalAddress(((ReadOnlySpan)networkInterfaceInfo->AddressBytes)[..networkInterfaceInfo->NumAddressBytes].ToArray()); + _physicalAddress = new PhysicalAddress(new ReadOnlySpan(networkInterfaceInfo->AddressBytes, networkInterfaceInfo->NumAddressBytes).ToArray()); } _mtu = networkInterfaceInfo->Mtu; @@ -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)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]); + var address = new IPAddress(new ReadOnlySpan(addressInfo->AddressBytes, addressInfo->NumAddressBytes)); if (address.IsIPv6LinkLocal) { address.ScopeId = addressInfo->InterfaceIndex; diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs index c01b320866b282..448d2181e9b074 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs @@ -32,12 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners) continue; } - IPAddress localIPAddress = new IPAddress(((ReadOnlySpan)nativeInfo.LocalEndPoint.AddressBytes)[..checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)]); + IPAddress localIPAddress = new IPAddress(new ReadOnlySpan(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)nativeInfo.RemoteEndPoint.AddressBytes)[..checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)]); + new IPAddress(new ReadOnlySpan(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes))); IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port); connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state); @@ -87,7 +87,7 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners() int port = (int)endPointInfo.Port; IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ? IPAddress.Any : - new IPAddress(((ReadOnlySpan)endPointInfo.AddressBytes)[..checked((int)endPointInfo.NumAddressBytes)]); + new IPAddress(new ReadOnlySpan(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes))); endPoints[i] = new IPEndPoint(ipAddress, port); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs index 0b25c83c83465e..8ce6f541bf0de9 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs @@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI { Context* context = (Context*)pContext; - IPAddress ipAddress = new IPAddress(((ReadOnlySpan)gatewayAddressInfo->AddressBytes)[..gatewayAddressInfo->NumAddressBytes]); + IPAddress ipAddress = new IPAddress(new ReadOnlySpan(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes)); if (ipAddress.IsIPv6LinkLocal) { // For Link-Local addresses add ScopeId as that is not part of the route entry. diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs index 9a2ef06a50d40e..c2e776a07bee0c 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs @@ -34,7 +34,7 @@ public static bool IsMulticast(IPAddress address) /// A new IPAddress created with the information in the native structure. public static unsafe IPAddress GetIPAddressFromNativeInfo(Interop.Sys.IpAddressInfo* addressInfo) { - IPAddress ipAddress = new IPAddress(((ReadOnlySpan)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]); + IPAddress ipAddress = new IPAddress(new ReadOnlySpan(addressInfo->AddressBytes, addressInfo->NumAddressBytes)); return ipAddress; } } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs index 0310e630b7cda9..71330d0eb9ddc7 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs @@ -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; @@ -104,7 +104,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() if (nii->NumAddressBytes > 0) { - lni._physicalAddress = new PhysicalAddress(((ReadOnlySpan)nii->AddressBytes)[..nii->NumAddressBytes].ToArray()); + lni._physicalAddress = new PhysicalAddress(new ReadOnlySpan(nii->AddressBytes, nii->NumAddressBytes).ToArray()); } interfaces[i] = lni; @@ -114,7 +114,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() while (addressCount != 0) { - var address = new IPAddress(((ReadOnlySpan)ai->AddressBytes)[..ai->NumAddressBytes]); + var address = new IPAddress(new ReadOnlySpan(ai->AddressBytes, ai->NumAddressBytes)); if (address.IsIPv6LinkLocal) { address.ScopeId = ai->InterfaceIndex; diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs index 8bc48a35dd3318..565d8df9ab4b6e 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs @@ -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); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs index 78b655131860f7..444d19bf01a42b 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs @@ -98,7 +98,11 @@ protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo* addressInfo, protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo* llAddr) { - byte[] macAddress = ((ReadOnlySpan)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;