From 3004aa29e3c265142a48764121607e24494ec827 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 10 Jun 2021 12:07:38 -0700 Subject: [PATCH] Use GeneratedDllImport in System.Net.NetworkInformation (#53966) * Use GeneratedDllImport in System.Net.NetworkInformation --- .../Interop.ProtocolStatistics.cs | 14 ++++----- .../OSX/Interop.SystemConfiguration.cs | 16 +++++----- .../Interop.EnumerateInterfaceAddresses.cs | 2 +- .../System.Native/Interop.GetDomainName.cs | 2 +- .../System.Native/Interop.NetworkChange.cs | 2 +- .../IpHlpApi/Interop.GetNetworkParams.cs | 2 +- .../IpHlpApi/Interop.NetworkInformation.cs | 30 +++++++++---------- .../Windows/WinSock/Interop.WSAEventSelect.cs | 10 +++---- .../HostInformationPal.Windows.cs | 6 ++-- .../BsdIPv4GlobalStatistics.cs | 2 +- .../NetworkInformation/BsdIcmpV4Statistics.cs | 4 +-- .../NetworkInformation/BsdIcmpV6Statistics.cs | 4 +-- .../NetworkInformation/BsdTcpStatistics.cs | 4 +-- .../NetworkInformation/BsdUdpStatistics.cs | 4 +-- .../LinuxNetworkInterface.cs | 2 +- .../NetworkAddressChange.OSX.cs | 4 +-- .../NetworkAddressChange.Unix.cs | 4 +-- .../SystemIPGlobalProperties.cs | 16 +++++----- .../SystemIPGlobalStatistics.cs | 14 +++++---- .../SystemIPv4InterfaceProperties.cs | 6 ++-- .../SystemIcmpV4Statistics.cs | 6 ++-- .../SystemNetworkInterface.cs | 17 ++++++----- .../NetworkInformation/SystemTcpStatistics.cs | 12 ++++---- .../NetworkInformation/SystemUdpStatistics.cs | 12 ++++---- 24 files changed, 103 insertions(+), 92 deletions(-) diff --git a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.ProtocolStatistics.cs b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.ProtocolStatistics.cs index ab7ac0fb962c6..476b7a653edb1 100644 --- a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.ProtocolStatistics.cs +++ b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.ProtocolStatistics.cs @@ -34,7 +34,7 @@ public readonly struct TcpGlobalStatistics } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetTcpGlobalStatistics")] - public static extern int GetTcpGlobalStatistics(out TcpGlobalStatistics statistics); + public static unsafe extern int GetTcpGlobalStatistics(TcpGlobalStatistics* statistics); [StructLayoutAttribute(LayoutKind.Sequential)] public readonly struct IPv4GlobalStatistics @@ -56,7 +56,7 @@ public readonly struct IPv4GlobalStatistics } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIPv4GlobalStatistics")] - public static extern int GetIPv4GlobalStatistics(out IPv4GlobalStatistics statistics); + public static unsafe extern int GetIPv4GlobalStatistics(IPv4GlobalStatistics* statistics); [StructLayoutAttribute(LayoutKind.Sequential)] public readonly struct UdpGlobalStatistics @@ -69,7 +69,7 @@ public readonly struct UdpGlobalStatistics } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUdpGlobalStatistics")] - public static extern int GetUdpGlobalStatistics(out UdpGlobalStatistics statistics); + public static unsafe extern int GetUdpGlobalStatistics(UdpGlobalStatistics* statistics); [StructLayoutAttribute(LayoutKind.Sequential)] public readonly struct Icmpv4GlobalStatistics @@ -99,7 +99,7 @@ public readonly struct Icmpv4GlobalStatistics } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv4GlobalStatistics")] - public static extern int GetIcmpv4GlobalStatistics(out Icmpv4GlobalStatistics statistics); + public static unsafe extern int GetIcmpv4GlobalStatistics(Icmpv4GlobalStatistics* statistics); [StructLayoutAttribute(LayoutKind.Sequential)] public readonly struct Icmpv6GlobalStatistics @@ -135,7 +135,7 @@ public readonly struct Icmpv6GlobalStatistics } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv6GlobalStatistics")] - public static extern int GetIcmpv6GlobalStatistics(out Icmpv6GlobalStatistics statistics); + public static unsafe extern int GetIcmpv6GlobalStatistics(Icmpv6GlobalStatistics* statistics); public readonly struct NativeIPInterfaceStatistics { @@ -155,8 +155,8 @@ public readonly struct NativeIPInterfaceStatistics public readonly ulong Flags; } - [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNativeIPInterfaceStatistics")] - public static extern int GetNativeIPInterfaceStatistics(string name, out NativeIPInterfaceStatistics stats); + [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNativeIPInterfaceStatistics", CharSet = CharSet.Ansi)] + public static partial int GetNativeIPInterfaceStatistics(string name, out NativeIPInterfaceStatistics stats); [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNumRoutes")] public static extern int GetNumRoutes(); diff --git a/src/libraries/Common/src/Interop/OSX/Interop.SystemConfiguration.cs b/src/libraries/Common/src/Interop/OSX/Interop.SystemConfiguration.cs index 5d9e896dd4024..ffe34c226aa1f 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.SystemConfiguration.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.SystemConfiguration.cs @@ -34,8 +34,8 @@ internal struct SCDynamicStoreContext /// Pass null if no callouts are desired. /// The context associated with the callout. /// A reference to the new dynamic store session. - [DllImport(Libraries.SystemConfigurationLibrary)] - private static extern unsafe SafeCreateHandle SCDynamicStoreCreate( + [GeneratedDllImport(Libraries.SystemConfigurationLibrary)] + private static unsafe partial SafeCreateHandle SCDynamicStoreCreate( IntPtr allocator, CFStringRef name, delegate* unmanaged callout, @@ -67,8 +67,8 @@ internal static unsafe SafeCreateHandle SCDynamicStoreCreate( /// The service ID or a regular expression pattern. /// The specific global entity, such as IPv4 or DNS. /// A string containing the formatted key. - [DllImport(Libraries.SystemConfigurationLibrary)] - private static extern SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity( + [GeneratedDllImport(Libraries.SystemConfigurationLibrary)] + private static partial SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity( IntPtr allocator, CFStringRef domain, CFStringRef serviceID, @@ -99,8 +99,8 @@ internal static SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity( /// The order in which the sources that are ready to be processed are handled, /// on platforms that support it and for source versions that support it. /// The new run loop source object. - [DllImport(Libraries.SystemConfigurationLibrary)] - private static extern SafeCreateHandle SCDynamicStoreCreateRunLoopSource( + [GeneratedDllImport(Libraries.SystemConfigurationLibrary)] + private static partial SafeCreateHandle SCDynamicStoreCreateRunLoopSource( IntPtr allocator, SCDynamicStoreRef store, CFIndex order); @@ -125,8 +125,8 @@ internal static SafeCreateHandle SCDynamicStoreCreateRunLoopSource(SCDynamicStor /// An array of keys to be monitored or IntPtr.Zero if no specific keys are to be monitored. /// An array of POSIX regex pattern strings used to match keys to be monitored, /// or IntPtr.Zero if no key patterns are to be monitored. - /// True if the set of notification keys and patterns was successfully updated; false otherwise. + /// Non-zero if the set of notification keys and patterns was successfully updated; zero otherwise. [DllImport(Libraries.SystemConfigurationLibrary)] - internal static extern bool SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns); + internal static extern int SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns); } } 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 73e988eb7a52e..2ad68069e48c0 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 @@ -53,7 +53,7 @@ public static extern unsafe int EnumerateInterfaceAddresses( public static extern unsafe int EnumerateGatewayAddressesForInterface(void* context, uint interfaceIndex, delegate* unmanaged onGatewayFound); [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNetworkInterfaces")] - public static unsafe extern int GetNetworkInterfaces(ref int count, ref NetworkInterfaceInfo* addrs, ref int addressCount, ref IpAddressInfo *aa); + public static unsafe extern int GetNetworkInterfaces(int* count, NetworkInterfaceInfo** addrs, int* addressCount, IpAddressInfo** aa); } } diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs index 31f7c07c10c42..466afb7b0101d 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs @@ -10,7 +10,7 @@ internal static partial class Interop { internal static partial class Sys { - [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetDomainName", SetLastError = true)] + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetDomainName")] private static extern unsafe int GetDomainName(byte* name, int len); internal static unsafe string GetDomainName() diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.NetworkChange.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.NetworkChange.cs index 37de2ebb38807..152588f507885 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.NetworkChange.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.NetworkChange.cs @@ -17,7 +17,7 @@ public enum NetworkChangeKind } [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_CreateNetworkChangeListenerSocket")] - public static extern Error CreateNetworkChangeListenerSocket(out int socket); + public static unsafe extern Error CreateNetworkChangeListenerSocket(int* socket); [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_CloseNetworkChangeListenerSocket")] public static extern Error CloseNetworkChangeListenerSocket(int socket); diff --git a/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.GetNetworkParams.cs b/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.GetNetworkParams.cs index d6760bfc17bab..f731ce50e7a38 100644 --- a/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.GetNetworkParams.cs +++ b/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.GetNetworkParams.cs @@ -9,6 +9,6 @@ internal static partial class Interop internal static partial class IpHlpApi { [DllImport(Interop.Libraries.IpHlpApi, ExactSpelling = true)] - internal static extern uint GetNetworkParams(IntPtr pFixedInfo, ref uint pOutBufLen); + internal static unsafe extern uint GetNetworkParams(IntPtr pFixedInfo, uint* pOutBufLen); } } diff --git a/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs b/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs index 81faff1d9a04a..5b4fe4ed49195 100644 --- a/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs +++ b/src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs @@ -324,7 +324,7 @@ internal struct MibTcpStats [StructLayout(LayoutKind.Sequential)] internal struct MibIpStats { - internal bool forwardingEnabled; + internal int forwardingEnabled; internal uint defaultTtl; internal uint packetsReceived; internal uint receivedPacketsWithHeaderErrors; @@ -509,50 +509,50 @@ internal unsafe struct MibUdp6RowOwnerPid } [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetAdaptersAddresses( + internal static unsafe extern uint GetAdaptersAddresses( AddressFamily family, uint flags, IntPtr pReserved, IntPtr adapterAddresses, - ref uint outBufLen); + uint* outBufLen); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetBestInterfaceEx(byte[] ipAddress, out int index); + internal static unsafe extern uint GetBestInterfaceEx(byte* ipAddress, int* index); [DllImport(Interop.Libraries.IpHlpApi)] internal static extern uint GetIfEntry2(ref MibIfRow2 pIfRow); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetIpStatisticsEx(out MibIpStats statistics, AddressFamily family); + internal static unsafe extern uint GetIpStatisticsEx(MibIpStats* statistics, AddressFamily family); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetTcpStatisticsEx(out MibTcpStats statistics, AddressFamily family); + internal static unsafe extern uint GetTcpStatisticsEx(MibTcpStats* statistics, AddressFamily family); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetUdpStatisticsEx(out MibUdpStats statistics, AddressFamily family); + internal static unsafe extern uint GetUdpStatisticsEx(MibUdpStats* statistics, AddressFamily family); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetIcmpStatistics(out MibIcmpInfo statistics); + internal static unsafe extern uint GetIcmpStatistics(MibIcmpInfo* statistics); [DllImport(Interop.Libraries.IpHlpApi)] internal static extern uint GetIcmpStatisticsEx(out MibIcmpInfoEx statistics, AddressFamily family); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order); + internal static unsafe extern uint GetTcpTable(IntPtr pTcpTable, uint* dwOutBufLen, int order); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order, + internal static unsafe extern uint GetExtendedTcpTable(IntPtr pTcpTable, uint* dwOutBufLen, int order, uint IPVersion, TcpTableClass tableClass, uint reserved); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order); + internal static unsafe extern uint GetUdpTable(IntPtr pUdpTable, uint* dwOutBufLen, int order); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order, + internal static unsafe extern uint GetExtendedUdpTable(IntPtr pUdpTable, uint* dwOutBufLen, int order, uint IPVersion, UdpTableClass tableClass, uint reserved); [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern uint GetPerAdapterInfo(uint IfIndex, IntPtr pPerAdapterInfo, ref uint pOutBufLen); + internal static unsafe extern uint GetPerAdapterInfo(uint IfIndex, IntPtr pPerAdapterInfo, uint* pOutBufLen); [DllImport(Interop.Libraries.IpHlpApi)] internal static extern void FreeMibTable(IntPtr handle); @@ -560,8 +560,8 @@ internal static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref uint dwOut [DllImport(Interop.Libraries.IpHlpApi)] internal static extern uint CancelMibChangeNotify2(IntPtr notificationHandle); - [DllImport(Interop.Libraries.IpHlpApi)] - internal static extern unsafe uint NotifyStableUnicastIpAddressTable( + [GeneratedDllImport(Interop.Libraries.IpHlpApi)] + internal static unsafe partial uint NotifyStableUnicastIpAddressTable( AddressFamily addressFamily, out SafeFreeMibTable table, delegate* unmanaged callback, diff --git a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSAEventSelect.cs b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSAEventSelect.cs index 9318345ec9387..a3903f29c3dbb 100644 --- a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSAEventSelect.cs +++ b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSAEventSelect.cs @@ -9,11 +9,11 @@ internal static partial class Interop { internal static partial class Winsock { - [DllImport(Interop.Libraries.Ws2_32, SetLastError = true)] - internal static extern SocketError WSAEventSelect( - [In] SafeSocketHandle socketHandle, - [In] SafeHandle Event, - [In] AsyncEventBits NetworkEvents); + [GeneratedDllImport(Interop.Libraries.Ws2_32, SetLastError = true)] + internal static partial SocketError WSAEventSelect( + SafeSocketHandle socketHandle, + SafeHandle Event, + AsyncEventBits NetworkEvents); [Flags] internal enum AsyncEventBits diff --git a/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Windows.cs b/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Windows.cs index 27f6ba5d1937d..bfd4127724ac7 100644 --- a/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Windows.cs +++ b/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Windows.cs @@ -26,20 +26,20 @@ public static string GetDomainName() return FixedInfo.domainName; } - private static Interop.IpHlpApi.FIXED_INFO GetFixedInfo() + private static unsafe Interop.IpHlpApi.FIXED_INFO GetFixedInfo() { uint size = 0; Interop.IpHlpApi.FIXED_INFO fixedInfo = default; // First we need to get the size of the buffer - uint result = Interop.IpHlpApi.GetNetworkParams(IntPtr.Zero, ref size); + uint result = Interop.IpHlpApi.GetNetworkParams(IntPtr.Zero, &size); while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW) { IntPtr buffer = Marshal.AllocHGlobal((int)size); try { - result = Interop.IpHlpApi.GetNetworkParams(buffer, ref size); + result = Interop.IpHlpApi.GetNetworkParams(buffer, &size); if (result == Interop.IpHlpApi.ERROR_SUCCESS) { fixedInfo = Marshal.PtrToStructure(buffer); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs index 0ac0a8cbd5b1f..b997ac445f6ba 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs @@ -45,7 +45,7 @@ private static unsafe void ProcessIpv4Address(void* pContext, byte* ifaceName, I public unsafe BsdIPv4GlobalStatistics() { Interop.Sys.IPv4GlobalStatistics statistics; - if (Interop.Sys.GetIPv4GlobalStatistics(out statistics) == -1) + if (Interop.Sys.GetIPv4GlobalStatistics(&statistics) == -1) { throw new NetworkInformationException(SR.net_PInvokeError); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV4Statistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV4Statistics.cs index d8322222a851f..37590aa3fd613 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV4Statistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV4Statistics.cs @@ -28,10 +28,10 @@ internal sealed class BsdIcmpV4Statistics : IcmpV4Statistics private readonly long _timestampRequestsReceived; private readonly long _timestampRequestsSent; - public BsdIcmpV4Statistics() + public unsafe BsdIcmpV4Statistics() { Interop.Sys.Icmpv4GlobalStatistics statistics; - if (Interop.Sys.GetIcmpv4GlobalStatistics(out statistics) != 0) + if (Interop.Sys.GetIcmpv4GlobalStatistics(&statistics) != 0) { throw new NetworkInformationException(SR.net_PInvokeError); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV6Statistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV6Statistics.cs index fe23a313fede2..426716242643d 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV6Statistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIcmpV6Statistics.cs @@ -34,10 +34,10 @@ internal sealed class BsdIcmpV6Statistics : IcmpV6Statistics private readonly long _timeExceededMessagesReceived; private readonly long _timeExceededMessagesSent; - public BsdIcmpV6Statistics() + public unsafe BsdIcmpV6Statistics() { Interop.Sys.Icmpv6GlobalStatistics statistics; - if (Interop.Sys.GetIcmpv6GlobalStatistics(out statistics) != 0) + if (Interop.Sys.GetIcmpv6GlobalStatistics(&statistics) != 0) { throw new NetworkInformationException(SR.net_PInvokeError); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdTcpStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdTcpStatistics.cs index ba8b649320539..4281467f07ac0 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdTcpStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdTcpStatistics.cs @@ -15,10 +15,10 @@ internal sealed class BsdTcpStatistics : TcpStatistics private readonly long _segmentsReceived; private readonly int _currentConnections; - public BsdTcpStatistics() + public unsafe BsdTcpStatistics() { Interop.Sys.TcpGlobalStatistics statistics; - if (Interop.Sys.GetTcpGlobalStatistics(out statistics) != 0) + if (Interop.Sys.GetTcpGlobalStatistics(&statistics) != 0) { throw new NetworkInformationException(SR.net_PInvokeError); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdUdpStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdUdpStatistics.cs index c60a62173e6da..248afb771c665 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdUdpStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdUdpStatistics.cs @@ -13,10 +13,10 @@ internal sealed class BsdUdpStatistics : UdpStatistics private readonly long _incomingErrors; private readonly int _numListeners; - public BsdUdpStatistics() + public unsafe BsdUdpStatistics() { Interop.Sys.UdpGlobalStatistics statistics; - if (Interop.Sys.GetUdpGlobalStatistics(out statistics) == -1) + if (Interop.Sys.GetUdpGlobalStatistics(&statistics) == -1) { throw new NetworkInformationException(SR.net_PInvokeError); } 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 a44a3bb315d5e..d411e38bef2a6 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 @@ -79,7 +79,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() Interop.Sys.IpAddressInfo * ai = null; IntPtr globalMemory = (IntPtr)null; - if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref nii, ref addressCount, ref ai) != 0) + if (Interop.Sys.GetNetworkInterfaces(&interfaceCount, &nii, &addressCount, &ai) != 0) { string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage(); throw new NetworkInformationException(message); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs index 353ff91d40c2b..44604d57148d5 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs @@ -161,10 +161,10 @@ private static unsafe void CreateAndStartRunLoop() }, (UIntPtr)2)) { // Try to register our pattern strings with the dynamic store instance. - if (patterns.IsInvalid || !Interop.SystemConfiguration.SCDynamicStoreSetNotificationKeys( + if (patterns.IsInvalid || Interop.SystemConfiguration.SCDynamicStoreSetNotificationKeys( s_dynamicStoreRef.DangerousGetHandle(), IntPtr.Zero, - patterns.DangerousGetHandle())) + patterns.DangerousGetHandle()) == 0) { s_dynamicStoreRef.Dispose(); s_dynamicStoreRef = null; diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs index a0ad6c92bab7e..5d95e8b2b9733 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs @@ -138,11 +138,11 @@ public static event NetworkAvailabilityChangedEventHandler? NetworkAvailabilityC } } - private static void CreateSocket() + private static unsafe void CreateSocket() { Debug.Assert(s_socket == 0, "s_socket != 0, must close existing socket before opening another."); int newSocket; - Interop.Error result = Interop.Sys.CreateNetworkChangeListenerSocket(out newSocket); + Interop.Error result = Interop.Sys.CreateNetworkChangeListenerSocket(&newSocket); if (result != Interop.Error.SUCCESS) { string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage(); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs index dff0aad267f73..a15e1eff8351a 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs @@ -112,7 +112,7 @@ private unsafe List GetAllTcpConnections() if (Socket.OSSupportsIPv4) { // Get the buffer size needed. - result = Interop.IpHlpApi.GetTcpTable(IntPtr.Zero, ref size, true); + result = Interop.IpHlpApi.GetTcpTable(IntPtr.Zero, &size, order: 1); while (result == Interop.IpHlpApi.ERROR_INSUFFICIENT_BUFFER) { @@ -120,7 +120,7 @@ private unsafe List GetAllTcpConnections() IntPtr buffer = Marshal.AllocHGlobal((int)size); try { - result = Interop.IpHlpApi.GetTcpTable(buffer, ref size, true); + result = Interop.IpHlpApi.GetTcpTable(buffer, &size, order: 1); if (result == Interop.IpHlpApi.ERROR_SUCCESS) { @@ -159,7 +159,7 @@ private unsafe List GetAllTcpConnections() { // Get the buffer size needed. size = 0; - result = Interop.IpHlpApi.GetExtendedTcpTable(IntPtr.Zero, ref size, true, + result = Interop.IpHlpApi.GetExtendedTcpTable(IntPtr.Zero, &size, order: 1, (uint)AddressFamily.InterNetworkV6, Interop.IpHlpApi.TcpTableClass.TcpTableOwnerPidAll, 0); @@ -169,7 +169,7 @@ private unsafe List GetAllTcpConnections() IntPtr buffer = Marshal.AllocHGlobal((int)size); try { - result = Interop.IpHlpApi.GetExtendedTcpTable(buffer, ref size, true, + result = Interop.IpHlpApi.GetExtendedTcpTable(buffer, &size, order: 1, (uint)AddressFamily.InterNetworkV6, Interop.IpHlpApi.TcpTableClass.TcpTableOwnerPidAll, 0); if (result == Interop.IpHlpApi.ERROR_SUCCESS) @@ -221,7 +221,7 @@ public unsafe override IPEndPoint[] GetActiveUdpListeners() if (Socket.OSSupportsIPv4) { // Get the buffer size needed. - result = Interop.IpHlpApi.GetUdpTable(IntPtr.Zero, ref size, true); + result = Interop.IpHlpApi.GetUdpTable(IntPtr.Zero, &size, order: 1); while (result == Interop.IpHlpApi.ERROR_INSUFFICIENT_BUFFER) { // Allocate the buffer and get the UDP table. @@ -229,7 +229,7 @@ public unsafe override IPEndPoint[] GetActiveUdpListeners() try { - result = Interop.IpHlpApi.GetUdpTable(buffer, ref size, true); + result = Interop.IpHlpApi.GetUdpTable(buffer, &size, order: 1); if (result == Interop.IpHlpApi.ERROR_SUCCESS) { @@ -273,7 +273,7 @@ public unsafe override IPEndPoint[] GetActiveUdpListeners() { // Get the buffer size needed. size = 0; - result = Interop.IpHlpApi.GetExtendedUdpTable(IntPtr.Zero, ref size, true, + result = Interop.IpHlpApi.GetExtendedUdpTable(IntPtr.Zero, &size, order: 1, (uint)AddressFamily.InterNetworkV6, Interop.IpHlpApi.UdpTableClass.UdpTableOwnerPid, 0); while (result == Interop.IpHlpApi.ERROR_INSUFFICIENT_BUFFER) @@ -282,7 +282,7 @@ public unsafe override IPEndPoint[] GetActiveUdpListeners() IntPtr buffer = Marshal.AllocHGlobal((int)size); try { - result = Interop.IpHlpApi.GetExtendedUdpTable(buffer, ref size, true, + result = Interop.IpHlpApi.GetExtendedUdpTable(buffer, &size, order: 1, (uint)AddressFamily.InterNetworkV6, Interop.IpHlpApi.UdpTableClass.UdpTableOwnerPid, 0); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalStatistics.cs index e8838b31d0c24..353671e9b1897 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalStatistics.cs @@ -12,17 +12,19 @@ internal sealed class SystemIPGlobalStatistics : IPGlobalStatistics private SystemIPGlobalStatistics() { } - internal SystemIPGlobalStatistics(AddressFamily family) + internal unsafe SystemIPGlobalStatistics(AddressFamily family) { - uint result = Interop.IpHlpApi.GetIpStatisticsEx(out _stats, family); - - if (result != Interop.IpHlpApi.ERROR_SUCCESS) + fixed (Interop.IpHlpApi.MibIpStats* pStats = &_stats) { - throw new NetworkInformationException((int)result); + uint result = Interop.IpHlpApi.GetIpStatisticsEx(pStats, family); + if (result != Interop.IpHlpApi.ERROR_SUCCESS) + { + throw new NetworkInformationException((int)result); + } } } - public override bool ForwardingEnabled { get { return _stats.forwardingEnabled; } } + public override bool ForwardingEnabled { get { return _stats.forwardingEnabled != 0; } } public override int DefaultTtl { get { return (int)_stats.defaultTtl; } } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPv4InterfaceProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPv4InterfaceProperties.cs index 72accfe9f4514..7c455b47b455b 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPv4InterfaceProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPv4InterfaceProperties.cs @@ -75,20 +75,20 @@ public override int Index } } - private void GetPerAdapterInfo(uint index) + private unsafe void GetPerAdapterInfo(uint index) { if (index != 0) { uint size = 0; - uint result = Interop.IpHlpApi.GetPerAdapterInfo(index, IntPtr.Zero, ref size); + uint result = Interop.IpHlpApi.GetPerAdapterInfo(index, IntPtr.Zero, &size); while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW) { // Now we allocate the buffer and read the network parameters. IntPtr buffer = Marshal.AllocHGlobal((int)size); try { - result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, ref size); + result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, &size); if (result == Interop.IpHlpApi.ERROR_SUCCESS) { Interop.IpHlpApi.IpPerAdapterInfo ipPerAdapterInfo = diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIcmpV4Statistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIcmpV4Statistics.cs index a2294d8ad5ef6..18f4dd555c267 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIcmpV4Statistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIcmpV4Statistics.cs @@ -8,9 +8,11 @@ internal sealed class SystemIcmpV4Statistics : IcmpV4Statistics { private readonly Interop.IpHlpApi.MibIcmpInfo _stats; - internal SystemIcmpV4Statistics() + internal unsafe SystemIcmpV4Statistics() { - uint result = Interop.IpHlpApi.GetIcmpStatistics(out _stats); + uint result; + fixed (Interop.IpHlpApi.MibIcmpInfo* pStats = &_stats) + result = Interop.IpHlpApi.GetIcmpStatistics(pStats); if (result != Interop.IpHlpApi.ERROR_SUCCESS) { diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs index f2196e3b79a4d..ed806788ae92d 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs @@ -42,14 +42,17 @@ internal static int InternalIPv6LoopbackInterfaceIndex } } - private static int GetBestInterfaceForAddress(IPAddress addr) + private static unsafe int GetBestInterfaceForAddress(IPAddress addr) { int index; Internals.SocketAddress address = new Internals.SocketAddress(addr); - int error = (int)Interop.IpHlpApi.GetBestInterfaceEx(address.Buffer, out index); - if (error != 0) + fixed (byte* buffer = address.Buffer) { - throw new NetworkInformationException(error); + int error = (int)Interop.IpHlpApi.GetBestInterfaceEx(buffer, &index); + if (error != 0) + { + throw new NetworkInformationException(error); + } } return index; @@ -77,7 +80,7 @@ internal static bool InternalGetIsNetworkAvailable() return false; } - internal static NetworkInterface[] GetNetworkInterfaces() + internal static unsafe NetworkInterface[] GetNetworkInterfaces() { AddressFamily family = AddressFamily.Unspecified; uint bufferSize = 0; @@ -91,7 +94,7 @@ internal static NetworkInterface[] GetNetworkInterfaces() // Figure out the right buffer size for the adapter information. uint result = Interop.IpHlpApi.GetAdaptersAddresses( - family, (uint)flags, IntPtr.Zero, IntPtr.Zero, ref bufferSize); + family, (uint)flags, IntPtr.Zero, IntPtr.Zero, &bufferSize); while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW) { @@ -101,7 +104,7 @@ internal static NetworkInterface[] GetNetworkInterfaces() try { result = Interop.IpHlpApi.GetAdaptersAddresses( - family, (uint)flags, IntPtr.Zero, buffer, ref bufferSize); + family, (uint)flags, IntPtr.Zero, buffer, &bufferSize); // If succeeded, we're going to add each new interface. if (result == Interop.IpHlpApi.ERROR_SUCCESS) diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemTcpStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemTcpStatistics.cs index 32453ce74c6a3..9f276ed0ffcc8 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemTcpStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemTcpStatistics.cs @@ -12,13 +12,15 @@ internal sealed class SystemTcpStatistics : TcpStatistics private SystemTcpStatistics() { } - internal SystemTcpStatistics(AddressFamily family) + internal unsafe SystemTcpStatistics(AddressFamily family) { - uint result = Interop.IpHlpApi.GetTcpStatisticsEx(out _stats, family); - - if (result != Interop.IpHlpApi.ERROR_SUCCESS) + fixed (Interop.IpHlpApi.MibTcpStats* pStats = &_stats) { - throw new NetworkInformationException((int)result); + uint result = Interop.IpHlpApi.GetTcpStatisticsEx(pStats, family); + if (result != Interop.IpHlpApi.ERROR_SUCCESS) + { + throw new NetworkInformationException((int)result); + } } } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemUdpStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemUdpStatistics.cs index 20869e1f8c684..5827702b07022 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemUdpStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemUdpStatistics.cs @@ -12,13 +12,15 @@ internal sealed class SystemUdpStatistics : UdpStatistics private SystemUdpStatistics() { } - internal SystemUdpStatistics(AddressFamily family) + internal unsafe SystemUdpStatistics(AddressFamily family) { - uint result = Interop.IpHlpApi.GetUdpStatisticsEx(out _stats, family); - - if (result != Interop.IpHlpApi.ERROR_SUCCESS) + fixed (Interop.IpHlpApi.MibUdpStats* pStats = &_stats) { - throw new NetworkInformationException((int)result); + uint result = Interop.IpHlpApi.GetUdpStatisticsEx(pStats, family); + if (result != Interop.IpHlpApi.ERROR_SUCCESS) + { + throw new NetworkInformationException((int)result); + } } }