Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GeneratedDllImport in System.Net.NetworkInformation #53966

Merged
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 @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal struct SCDynamicStoreContext
/// Pass null if no callouts are desired.</param>
/// <param name="context">The context associated with the callout.</param>
/// <returns>A reference to the new dynamic store session.</returns>
[DllImport(Libraries.SystemConfigurationLibrary)]
private static extern unsafe SafeCreateHandle SCDynamicStoreCreate(
[GeneratedDllImport(Libraries.SystemConfigurationLibrary)]
private static unsafe partial SafeCreateHandle SCDynamicStoreCreate(
IntPtr allocator,
CFStringRef name,
delegate* unmanaged<SCDynamicStoreRef, CFArrayRef, IntPtr, void> callout,
Expand Down Expand Up @@ -67,8 +67,8 @@ internal static unsafe SafeCreateHandle SCDynamicStoreCreate(
/// <param name="serviceID">The service ID or a regular expression pattern.</param>
/// <param name="entity">The specific global entity, such as IPv4 or DNS.</param>
/// <returns>A string containing the formatted key.</returns>
[DllImport(Libraries.SystemConfigurationLibrary)]
private static extern SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity(
[GeneratedDllImport(Libraries.SystemConfigurationLibrary)]
private static partial SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity(
IntPtr allocator,
CFStringRef domain,
CFStringRef serviceID,
Expand Down Expand Up @@ -99,8 +99,8 @@ internal static SafeCreateHandle SCDynamicStoreKeyCreateNetworkServiceEntity(
/// <param name="order">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.</param>
/// <returns>The new run loop source object.</returns>
[DllImport(Libraries.SystemConfigurationLibrary)]
private static extern SafeCreateHandle SCDynamicStoreCreateRunLoopSource(
[GeneratedDllImport(Libraries.SystemConfigurationLibrary)]
private static partial SafeCreateHandle SCDynamicStoreCreateRunLoopSource(
IntPtr allocator,
SCDynamicStoreRef store,
CFIndex order);
Expand All @@ -125,8 +125,8 @@ internal static SafeCreateHandle SCDynamicStoreCreateRunLoopSource(SCDynamicStor
/// <param name="keys">An array of keys to be monitored or IntPtr.Zero if no specific keys are to be monitored.</param>
/// <param name="patterns">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.</param>
/// <returns>True if the set of notification keys and patterns was successfully updated; false otherwise.</returns>
/// <returns>Non-zero if the set of notification keys and patterns was successfully updated; zero otherwise.</returns>
[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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static extern unsafe int EnumerateInterfaceAddresses(
public static extern unsafe int EnumerateGatewayAddressesForInterface(void* context, uint interfaceIndex, delegate* unmanaged<void*, IpAddressInfo*, void> 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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -509,59 +509,59 @@ 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);

[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<IntPtr, IntPtr, void> callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Interop.IpHlpApi.FIXED_INFO>(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading