Added DRT unit test and more DRT work

pull/328/head
David Hall 2022-10-04 14:18:34 -06:00
parent 48b09fd666
commit bdf07f10f5
5 changed files with 83 additions and 17 deletions

View File

@ -233,7 +233,7 @@ public class DistributedRoutingTable
evt = CreateEvent(null, false, false);
DrtOpen(pSettings, evt, default, out HDRT h).ThrowIfFailed();
hDrt = new((IntPtr)h);
selfPin = GCHandle.Alloc(this).AddrOfPinnedObject();
selfPin = (IntPtr)GCHandle.Alloc(this, GCHandleType.Normal);
Win32Error.ThrowLastErrorIfFalse(RegisterWaitForSingleObject(out drtWaitEvent, evt, DrtEventCallback, selfPin, INFINITE, WT.WT_EXECUTEDEFAULT));
}

View File

@ -13,6 +13,8 @@ namespace Vanara.PInvoke
public static partial class Drt
{
private const string Lib_Drt = "drt.dll";
private const string Lib_DrtProv = "drtprov.dll";
private const string Lib_DrtTrans = "drttransport.dll";
/// <summary/>
public const uint DRT_PAYLOAD_REVOKED = (1 << 0);
@ -585,7 +587,7 @@ namespace Vanara.PInvoke
/// </returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatederivedkey HRESULT DrtCreateDerivedKey( PCCERT_CONTEXT
// pLocalCert, DRT_DATA *pKey );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDerivedKey")]
public static extern HRESULT DrtCreateDerivedKey(PCCERT_CONTEXT pLocalCert, out DRT_DATA pKey);
@ -629,7 +631,7 @@ namespace Vanara.PInvoke
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatederivedkeysecurityprovider HRESULT
// DrtCreateDerivedKeySecurityProvider( PCCERT_CONTEXT pRootCert, PCCERT_CONTEXT pLocalCert, DRT_SECURITY_PROVIDER
// **ppSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDerivedKeySecurityProvider")]
public static extern HRESULT DrtCreateDerivedKeySecurityProvider(PCCERT_CONTEXT pRootCert, PCCERT_CONTEXT pLocalCert, out IntPtr ppSecurityProvider);
@ -660,7 +662,7 @@ namespace Vanara.PInvoke
/// </returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatednsbootstrapresolver HRESULT
// DrtCreateDnsBootstrapResolver( USHORT port, PCWSTR pwszAddress, DRT_BOOTSTRAP_PROVIDER **ppModule );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDnsBootstrapResolver")]
public static extern HRESULT DrtCreateDnsBootstrapResolver(ushort port, [MarshalAs(UnmanagedType.LPWStr)] string pwszAddress, out IntPtr ppModule);
@ -724,7 +726,7 @@ namespace Vanara.PInvoke
/// </remarks>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreateipv6udptransport HRESULT DrtCreateIpv6UdpTransport(
// DRT_SCOPE scope, ULONG dwScopeId, ULONG dwLocalityThreshold, USHORT *pwPort, HDRT_TRANSPORT *phTransport );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtTrans, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateIpv6UdpTransport")]
public static extern HRESULT DrtCreateIpv6UdpTransport(DRT_SCOPE scope, uint dwScopeId, uint dwLocalityThreshold, ref ushort pwPort, out HDRT_TRANSPORT phTransport);
@ -752,7 +754,7 @@ namespace Vanara.PInvoke
/// </returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatenullsecurityprovider HRESULT
// DrtCreateNullSecurityProvider( DRT_SECURITY_PROVIDER **ppSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateNullSecurityProvider")]
public static extern HRESULT DrtCreateNullSecurityProvider(out IntPtr ppSecurityProvider);
@ -808,7 +810,7 @@ namespace Vanara.PInvoke
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatepnrpbootstrapresolver HRESULT
// DrtCreatePnrpBootstrapResolver( BOOL fPublish, PCWSTR pwzPeerName, PCWSTR pwzCloudName, PCWSTR pwzPublishingIdentity,
// DRT_BOOTSTRAP_PROVIDER **ppResolver );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreatePnrpBootstrapResolver")]
public static extern HRESULT DrtCreatePnrpBootstrapResolver([MarshalAs(UnmanagedType.Bool)] bool fPublish, [MarshalAs(UnmanagedType.LPWStr)] string pwzPeerName,
[Optional, MarshalAs(UnmanagedType.LPWStr)] string pwzCloudName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pwzPublishingIdentity,
@ -821,7 +823,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletederivedkeysecurityprovider void
// DrtDeleteDerivedKeySecurityProvider( DRT_SECURITY_PROVIDER *pSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDerivedKeySecurityProvider")]
public static extern void DrtDeleteDerivedKeySecurityProvider(in DRT_SECURITY_PROVIDER pSecurityProvider);
@ -832,7 +834,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletederivedkeysecurityprovider void
// DrtDeleteDerivedKeySecurityProvider( DRT_SECURITY_PROVIDER *pSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDerivedKeySecurityProvider")]
public static extern void DrtDeleteDerivedKeySecurityProvider([In] IntPtr pSecurityProvider);
@ -841,7 +843,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletednsbootstrapresolver void DrtDeleteDnsBootstrapResolver(
// DRT_BOOTSTRAP_PROVIDER *pResolver );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDnsBootstrapResolver")]
public static extern void DrtDeleteDnsBootstrapResolver(in DRT_BOOTSTRAP_PROVIDER pResolver);
@ -850,7 +852,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletednsbootstrapresolver void DrtDeleteDnsBootstrapResolver(
// DRT_BOOTSTRAP_PROVIDER *pResolver );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDnsBootstrapResolver")]
public static extern void DrtDeleteDnsBootstrapResolver([In] IntPtr pResolver);
@ -884,7 +886,7 @@ namespace Vanara.PInvoke
/// </returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeleteipv6udptransport HRESULT DrtDeleteIpv6UdpTransport(
// HDRT_TRANSPORT hTransport );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtTrans, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteIpv6UdpTransport")]
public static extern HRESULT DrtDeleteIpv6UdpTransport(HDRT_TRANSPORT hTransport);
@ -893,7 +895,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletenullsecurityprovider void DrtDeleteNullSecurityProvider(
// DRT_SECURITY_PROVIDER *pSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteNullSecurityProvider")]
public static extern void DrtDeleteNullSecurityProvider(in DRT_SECURITY_PROVIDER pSecurityProvider);
@ -902,7 +904,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletenullsecurityprovider void DrtDeleteNullSecurityProvider(
// DRT_SECURITY_PROVIDER *pSecurityProvider );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteNullSecurityProvider")]
public static extern void DrtDeleteNullSecurityProvider([In] IntPtr pSecurityProvider);
@ -913,7 +915,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletepnrpbootstrapresolver void DrtDeletePnrpBootstrapResolver(
// DRT_BOOTSTRAP_PROVIDER *pResolver );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeletePnrpBootstrapResolver")]
public static extern void DrtDeletePnrpBootstrapResolver(in DRT_BOOTSTRAP_PROVIDER pResolver);
@ -924,7 +926,7 @@ namespace Vanara.PInvoke
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletepnrpbootstrapresolver void DrtDeletePnrpBootstrapResolver(
// DRT_BOOTSTRAP_PROVIDER *pResolver );
[DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)]
[DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)]
[PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeletePnrpBootstrapResolver")]
public static extern void DrtDeletePnrpBootstrapResolver([In] IntPtr pResolver);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<ProjectExtensions>
<SupportedDlls>Drt.dll</SupportedDlls>
<SupportedDlls>drt.dll;drtprov.dll;drttransport.dll</SupportedDlls>
</ProjectExtensions>
<PropertyGroup>
<Description>PInvoke API (methods, structures and constants) imported from Windows Drt.dll for the Distributed Routing Table (DRT) API.</Description>

View File

@ -4,5 +4,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Net\Vanara.Net.csproj" />
<ProjectReference Include="..\..\PInvoke\FirewallApi\Vanara.PInvoke.FirewallApi.vbproj" />
</ItemGroup>
</Project>

View File

@ -2,12 +2,16 @@
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Vanara.InteropServices;
using Vanara.Net;
using static Vanara.PInvoke.Dhcp;
using static Vanara.PInvoke.FirewallApi;
namespace Vanara.PInvoke.Tests;
@ -71,4 +75,63 @@ public class NetTests
client.Dispose();
}
}
[Test]
public void TestDrt()
{
using var fw = new Firewall();
fw.AddApp(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
try
{
DistributedRoutingTable drt = new(null, DrtBootstrapProvider.CreateDnsBootstrapResolver(null, 0));
}
finally
{
try { fw.RemoveApp(Process.GetCurrentProcess().MainModule.FileName); } catch { }
}
}
private class Firewall : IDisposable
{
Dictionary<string, ComReleaser<INetFwAuthorizedApplication>> newApps = new();
ComReleaser<INetFwMgr> pMgr;
ComReleaser<INetFwPolicy> pPol;
ComReleaser<INetFwProfile> pProf;
ComReleaser<INetFwAuthorizedApplications> pSet;
public Firewall()
{
pMgr = ComReleaserFactory.Create(new INetFwMgr());
Assert.IsNotNull(pMgr.Item);
pPol = ComReleaserFactory.Create(pMgr.Item.LocalPolicy);
Assert.IsNotNull(pPol.Item);
pProf = ComReleaserFactory.Create(pPol.Item.CurrentProfile);
Assert.IsNotNull(pProf.Item);
pSet = ComReleaserFactory.Create(pProf.Item.AuthorizedApplications);
Assert.IsNotNull(pSet.Item);
}
public void AddApp(string name, string exePath)
{
var pNewApp = ComReleaserFactory.Create(new INetFwAuthorizedApplication());
pNewApp.Item.Name = name;
pNewApp.Item.ProcessImageFileName = exePath;
pNewApp.Item.Enabled = true;
pSet.Item.Add(pNewApp.Item);
newApps.Add(exePath, pNewApp);
}
public void Dispose()
{
pSet.Dispose();
pProf.Dispose();
pPol.Dispose();
pMgr.Dispose();
foreach (var app in newApps.Values)
app.Dispose();
}
public void RemoveApp(string exePath) => pSet.Item.Remove(exePath);
}
}