Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Add IPHlpApi (#466)
Browse files Browse the repository at this point in the history
* Add IPHlpApi
- GetExtendedTcpTable

* PR feedback

* [chore] Add IPHlpApi

* Make IPHlpApi dependency conditional

Co-authored-by: Andrew Arnott <Andrew.arnott@Microsoft.com>
  • Loading branch information
qmfrederik and AArnott committed Jul 1, 2020
1 parent a62e5d3 commit 840c6ac
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ DwmApi.dll |`PInvoke.DwmApi` | [![NuGet](https://buildstats.info/nuget/PInvok
fusion.dll |`PInvoke.Fusion` | [![NuGet](https://buildstats.info/nuget/PInvoke.Fusion)](https://www.nuget.org/packages/PInvoke.Fusion)|.NET Framework Fusion
gdi32.dll |`PInvoke.Gdi32` | [![NuGet](https://buildstats.info/nuget/PInvoke.Gdi32)](https://www.nuget.org/packages/PInvoke.Gdi32)|[Windows Graphics Device Interface][Gdi]
hid.dll |`PInvoke.Hid` | [![NuGet](https://buildstats.info/nuget/PInvoke.Hid)](https://www.nuget.org/packages/PInvoke.Hid)|[Windows Human Interface Devices][Hid]
iphlpapi.dll |`PInvoke.IPHlpApi`| [![NuGet](https://buildstats.info/nuget/PInvoke.IPHlpApi)](https://www.nuget.org/packages/PInvoke.IPHlpApi)|[IP Helper](IPHlpApi)
kernel32.dll |`PInvoke.Kernel32`| [![NuGet](https://buildstats.info/nuget/PInvoke.Kernel32)](https://www.nuget.org/packages/PInvoke.Kernel32)|Windows Kernel API
magnification.dll |`PInvoke.Magnification`| [![NuGet](https://buildstats.info/nuget/PInvoke.Magnification)](https://www.nuget.org/packages/PInvoke.Magnification)|[Windows Magnification API][Magnification]
mscoree.dll |`PInvoke.MSCorEE` | [![NuGet](https://buildstats.info/nuget/PInvoke.MSCorEE)](https://www.nuget.org/packages/PInvoke.MSCorEE)|.NET Framework CLR host
Expand Down Expand Up @@ -120,6 +121,7 @@ For more information see the [.NET Foundation Code of Conduct](https://dotnetfou
[Crypt32]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa380256
[DwmApi]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969540.aspx
[Hid]: https://msdn.microsoft.com/en-us/library/windows/hardware/ff538865
[IPHlpApi]: https://docs.microsoft.com/en-us/windows/win32/api/_iphlp/
[Magnification]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms692162
[Msi]: https://msdn.microsoft.com/en-us/library/aa372860.aspx
[SetupApi]: https://msdn.microsoft.com/en-us/library/windows/hardware/ff550855
Expand Down
8 changes: 8 additions & 0 deletions src/IPHlpApi.Tests/IPHlpApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\IPHlpApi\IPHlpApi.csproj" />
</ItemGroup>
</Project>
57 changes: 57 additions & 0 deletions src/IPHlpApi.Tests/IPHlpApiFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using PInvoke;
using Xunit;

public class IPHlpApiFacts
{
[Fact]
public void StructSizeTest()
{
Assert.Equal(0x04, Marshal.SizeOf<IPHlpApi.MIB_TCPTABLE_OWNER_PID>());
Assert.Equal(0x18, Marshal.SizeOf<IPHlpApi.MIB_TCPROW_OWNER_PID>());
}

[Fact]
public unsafe void GetExtendedTcpTableTest()
{
IntPtr tcpTablePtr = IntPtr.Zero;
int tcpTableLength = 0;

if (IPHlpApi.GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, bOrder: true, AddressFamily.InterNetwork, IPHlpApi.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0) == Win32ErrorCode.ERROR_INSUFFICIENT_BUFFER)
{
try
{
tcpTablePtr = Marshal.AllocHGlobal(tcpTableLength);

if (IPHlpApi.GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, bOrder: true, AddressFamily.InterNetwork, IPHlpApi.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0) == Win32ErrorCode.ERROR_SUCCESS)
{
IPHlpApi.MIB_TCPTABLE_OWNER_PID* tcpTable = (IPHlpApi.MIB_TCPTABLE_OWNER_PID*)tcpTablePtr;
var tableSize = Marshal.SizeOf<IPHlpApi.MIB_TCPTABLE_OWNER_PID>();

IPHlpApi.MIB_TCPROW_OWNER_PID* tcpRow = (IPHlpApi.MIB_TCPROW_OWNER_PID*)(tcpTablePtr + tableSize);

for (int i = 0; i < tcpTable->dwNumEntries; ++i)
{
Assert.InRange(tcpRow->LocalPort, 0, 65535);
Assert.True(Enum.IsDefined(typeof(TcpState), tcpRow->dwState));

tcpRow++;
}
}
}
finally
{
if (tcpTablePtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(tcpTablePtr);
}
}
}
}
}
292 changes: 292 additions & 0 deletions src/IPHlpApi/IPHLPAPI.exports.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
AddIPAddress
AllocateAndGetInterfaceInfoFromStack
AllocateAndGetIpAddrTableFromStack
CancelIfTimestampConfigChange
CancelIPChangeNotify
CancelMibChangeNotify2
CaptureInterfaceHardwareCrossTimestamp
CloseCompartment
CloseGetIPPhysicalInterfaceForDestination
ConvertCompartmentGuidToId
ConvertCompartmentIdToGuid
ConvertGuidToString
ConvertInterfaceAliasToLuid
ConvertInterfaceGuidToLuid
ConvertInterfaceIndexToLuid
ConvertInterfaceLuidToAlias
ConvertInterfaceLuidToGuid
ConvertInterfaceLuidToIndex
ConvertInterfaceLuidToName
ConvertInterfaceNameToLuid
ConvertInterfacePhysicalAddressToLuid
ConvertIpv4MaskToLength
ConvertLengthToIpv4Mask
ConvertRemoteInterfaceAliasToLuid
ConvertRemoteInterfaceGuidToLuid
ConvertRemoteInterfaceIndexToLuid
ConvertRemoteInterfaceLuidToAlias
ConvertRemoteInterfaceLuidToGuid
ConvertRemoteInterfaceLuidToIndex
ConvertStringToGuid
ConvertStringToInterfacePhysicalAddress
CreateAnycastIpAddressEntry
CreateCompartment
CreateIpForwardEntry
CreateIpForwardEntry2
CreateIpNetEntry
CreateIpNetEntry2
CreatePersistentTcpPortReservation
CreatePersistentUdpPortReservation
CreateProxyArpEntry
CreateSortedAddressPairs
CreateUnicastIpAddressEntry
DeleteAnycastIpAddressEntry
DeleteCompartment
DeleteIPAddress
DeleteIpForwardEntry
DeleteIpForwardEntry2
DeleteIpNetEntry
DeleteIpNetEntry2
DeletePersistentTcpPortReservation
DeletePersistentUdpPortReservation
DeleteProxyArpEntry
DeleteUnicastIpAddressEntry
DisableMediaSense
do_echo_rep
do_echo_req
EnableRouter
FlushIpNetTable
FlushIpNetTable2
FlushIpPathTable
FreeDnsSettings
FreeInterfaceDnsSettings
FreeMibTable
GetAdapterIndex
GetAdapterOrderMap
GetAdaptersAddresses
GetAdaptersInfo
GetAnycastIpAddressEntry
GetAnycastIpAddressTable
GetBestInterface
GetBestInterfaceEx
GetBestRoute
GetBestRoute2
GetCurrentThreadCompartmentId
GetCurrentThreadCompartmentScope
GetDefaultCompartmentId
GetDnsSettings
GetExtendedTcpTable
GetExtendedUdpTable
GetFriendlyIfIndex
GetIcmpStatistics
GetIcmpStatisticsEx
GetIfEntry
GetIfEntry2
GetIfEntry2Ex
GetIfStackTable
GetIfTable
GetIfTable2
GetIfTable2Ex
GetInterfaceCompartmentId
GetInterfaceCurrentTimestampCapabilities
GetInterfaceDnsSettings
GetInterfaceHardwareTimestampCapabilities
GetInterfaceInfo
GetInvertedIfStackTable
GetIpAddrTable
GetIpErrorString
GetIpForwardEntry2
GetIpForwardTable
GetIpForwardTable2
GetIpInterfaceEntry
GetIpInterfaceTable
GetIpNetEntry2
GetIpNetTable
GetIpNetTable2
GetIpNetworkConnectionBandwidthEstimates
GetIpPathEntry
GetIpPathTable
GetIpStatistics
GetIpStatisticsEx
GetJobCompartmentId
GetMulticastIpAddressEntry
GetMulticastIpAddressTable
GetNetworkConnectivityHint
GetNetworkConnectivityHintForInterface
GetNetworkInformation
GetNetworkParams
GetNumberOfInterfaces
GetOwnerModuleFromPidAndInfo
GetOwnerModuleFromTcp6Entry
GetOwnerModuleFromTcpEntry
GetOwnerModuleFromUdp6Entry
GetOwnerModuleFromUdpEntry
GetPerAdapterInfo
GetPerTcp6ConnectionEStats
GetPerTcp6ConnectionStats
GetPerTcpConnectionEStats
GetPerTcpConnectionStats
GetRTTAndHopCount
GetSessionCompartmentId
GetTcp6Table
GetTcp6Table2
GetTcpStatistics
GetTcpStatisticsEx
GetTcpStatisticsEx2
GetTcpTable
GetTcpTable2
GetTeredoPort
GetUdp6Table
GetUdpStatistics
GetUdpStatisticsEx
GetUdpStatisticsEx2
GetUdpTable
GetUnicastIpAddressEntry
GetUnicastIpAddressTable
GetUniDirectionalAdapterInfo
GetWPAOACSupportLevel
Icmp6CreateFile
Icmp6ParseReplies
Icmp6SendEcho2
IcmpCloseHandle
IcmpCreateFile
IcmpParseReplies
IcmpSendEcho
IcmpSendEcho2
IcmpSendEcho2Ex
if_indextoname
if_nametoindex
InitializeCompartmentEntry
InitializeIpForwardEntry
InitializeIpInterfaceEntry
InitializeUnicastIpAddressEntry
InternalCleanupPersistentStore
InternalCreateAnycastIpAddressEntry
InternalCreateIpForwardEntry
InternalCreateIpForwardEntry2
InternalCreateIpNetEntry
InternalCreateIpNetEntry2
InternalCreateUnicastIpAddressEntry
InternalDeleteAnycastIpAddressEntry
InternalDeleteIpForwardEntry
InternalDeleteIpForwardEntry2
InternalDeleteIpNetEntry
InternalDeleteIpNetEntry2
InternalDeleteUnicastIpAddressEntry
InternalFindInterfaceByAddress
InternalGetAnycastIpAddressEntry
InternalGetAnycastIpAddressTable
InternalGetBoundTcp6EndpointTable
InternalGetBoundTcpEndpointTable
InternalGetForwardIpTable2
InternalGetIfEntry2
InternalGetIfTable
InternalGetIfTable2
InternalGetIpAddrTable
InternalGetIpForwardEntry2
InternalGetIpForwardTable
InternalGetIpInterfaceEntry
InternalGetIpInterfaceTable
InternalGetIpNetEntry2
InternalGetIpNetTable
InternalGetIpNetTable2
InternalGetIPPhysicalInterfaceForDestination
InternalGetMulticastIpAddressEntry
InternalGetMulticastIpAddressTable
InternalGetRtcSlotInformation
InternalGetTcp6Table2
InternalGetTcp6TableWithOwnerModule
InternalGetTcp6TableWithOwnerPid
InternalGetTcpTable
InternalGetTcpTable2
InternalGetTcpTableEx
InternalGetTcpTableWithOwnerModule
InternalGetTcpTableWithOwnerPid
InternalGetTunnelPhysicalAdapter
InternalGetUdp6TableWithOwnerModule
InternalGetUdp6TableWithOwnerPid
InternalGetUdpTable
InternalGetUdpTableEx
InternalGetUdpTableWithOwnerModule
InternalGetUdpTableWithOwnerPid
InternalGetUnicastIpAddressEntry
InternalGetUnicastIpAddressTable
InternalIcmpCreateFileEx
InternalSetIfEntry
InternalSetIpForwardEntry
InternalSetIpForwardEntry2
InternalSetIpInterfaceEntry
InternalSetIpNetEntry
InternalSetIpNetEntry2
InternalSetIpStats
InternalSetTcpEntry
InternalSetTeredoPort
InternalSetUnicastIpAddressEntry
IpReleaseAddress
IpRenewAddress
LookupPersistentTcpPortReservation
LookupPersistentUdpPortReservation
NhGetGuidFromInterfaceName
NhGetInterfaceDescriptionFromGuid
NhGetInterfaceNameFromDeviceGuid
NhGetInterfaceNameFromGuid
NhpAllocateAndGetInterfaceInfoFromStack
NotifyAddrChange
NotifyCompartmentChange
NotifyIfTimestampConfigChange
NotifyIpInterfaceChange
NotifyNetworkConnectivityHintChange
NotifyRouteChange
NotifyRouteChange2
NotifyStableUnicastIpAddressTable
NotifyTeredoPortChange
NotifyUnicastIpAddressChange
NTPTimeToNTFileTime
NTTimeToNTPTime
OpenCompartment
ParseNetworkString
PfAddFiltersToInterface
PfAddGlobalFilterToInterface
PfBindInterfaceToIndex
PfBindInterfaceToIPAddress
PfCreateInterface
PfDeleteInterface
PfDeleteLog
PfGetInterfaceStatistics
PfMakeLog
PfRebindFilters
PfRemoveFilterHandles
PfRemoveFiltersFromInterface
PfRemoveGlobalFilterFromInterface
PfSetLogBuffer
PfTestPacket
PfUnBindInterface
register_icmp
ResolveIpNetEntry2
ResolveNeighbor
RestoreMediaSense
SendARP
SetAdapterIpAddress
SetCurrentThreadCompartmentId
SetCurrentThreadCompartmentScope
SetDnsSettings
SetIfEntry
SetInterfaceDnsSettings
SetIpForwardEntry
SetIpForwardEntry2
SetIpInterfaceEntry
SetIpNetEntry
SetIpNetEntry2
SetIpStatistics
SetIpStatisticsEx
SetIpTTL
SetJobCompartmentId
SetNetworkInformation
SetPerTcp6ConnectionEStats
SetPerTcp6ConnectionStats
SetPerTcpConnectionEStats
SetPerTcpConnectionStats
SetSessionCompartmentId
SetTcpEntry
SetUnicastIpAddressEntry
UnenableRouter
Loading

0 comments on commit 840c6ac

Please sign in to comment.