Added helper properties and fixed parameters in DnsApi

pull/328/head
David Hall 2022-09-16 10:47:52 -06:00
parent fa6d6fb4b3
commit 148e52d8d6
3 changed files with 3538 additions and 3462 deletions

View File

@ -28,15 +28,15 @@ namespace Vanara.PInvoke
/// <param name="Status">A value that contains the status associated with this particular set of results.</param>
/// <param name="pQueryContext">A pointer to the user context that was passed to DnsServiceBrowse.</param>
/// <param name="pDnsRecord">
/// A pointer to a DNS_RECORD structure that contains a list of records describing a discovered service on the network. If not ,
/// then you are responsible for freeing the returned RR sets using DnsRecordListFree.
/// A pointer to a DNS_RECORD structure that contains a list of records describing a discovered service on the network. If not
/// <see langword="null"/>, then you are responsible for freeing the returned RR sets using DnsRecordListFree.
/// </param>
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/windns/nc-windns-dns_service_browse_callback DNS_SERVICE_BROWSE_CALLBACK
// DnsServiceBrowseCallback; void DnsServiceBrowseCallback( DWORD Status, PVOID pQueryContext, PDNS_RECORD pDnsRecord ) {...}
[PInvokeData("windns.h")]
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void DNS_SERVICE_BROWSE_CALLBACK(uint Status, [In] IntPtr pQueryContext, IntPtr pDnsRecord);
public delegate void DNS_SERVICE_BROWSE_CALLBACK(DNS_STATUS Status, [In] IntPtr pQueryContext, IntPtr pDnsRecord);
/// <summary>Used to notify your application that service registration has completed.</summary>
/// <param name="Status">A value that contains the status of the registration.</param>
@ -50,21 +50,21 @@ namespace Vanara.PInvoke
// DnsServiceRegisterComplete; void DnsServiceRegisterComplete( DWORD Status, PVOID pQueryContext, PDNS_SERVICE_INSTANCE pInstance ) {...}
[PInvokeData("windns.h")]
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void DNS_SERVICE_REGISTER_COMPLETE(uint Status, [In] IntPtr pQueryContext, [In] IntPtr pInstance);
public delegate void DNS_SERVICE_REGISTER_COMPLETE(DNS_STATUS Status, [In] IntPtr pQueryContext, [In] IntPtr pInstance);
/// <summary>Used to asynchronously return the results of a service resolve operation.</summary>
/// <param name="Status">A value that contains the status associated with this particular set of results.</param>
/// <param name="pQueryContext">A pointer to the user context that was passed to DnsServiceResolve.</param>
/// <param name="pInstance">
/// A pointer to a DNS_SERVICE_INSTANCE structure that contains detailed information about a service on the network. If not , then
/// you are responsible for freeing the data using DnsServiceFreeInstance.
/// A pointer to a DNS_SERVICE_INSTANCE structure that contains detailed information about a service on the network. If not
/// <see langword="null"/>, then you are responsible for freeing the data using DnsServiceFreeInstance.
/// </param>
/// <returns>None</returns>
// https://docs.microsoft.com/en-us/windows/win32/api/windns/nc-windns-dns_service_resolve_complete DNS_SERVICE_RESOLVE_COMPLETE
// DnsServiceResolveComplete; void DnsServiceResolveComplete( DWORD Status, PVOID pQueryContext, PDNS_SERVICE_INSTANCE pInstance ) {...}
[PInvokeData("windns.h")]
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void DNS_SERVICE_RESOLVE_COMPLETE(uint Status, [In] IntPtr pQueryContext, [In] IntPtr pInstance);
public delegate void DNS_SERVICE_RESOLVE_COMPLETE(DNS_STATUS Status, [In] IntPtr pQueryContext, [In] IntPtr pInstance);
/// <summary>Used to asynchronously return the results of an mDNS query.</summary>
/// <param name="pQueryContext">A pointer to the user context that was passed to DnsServiceBrowse.</param>
@ -127,7 +127,8 @@ namespace Vanara.PInvoke
// DnsAcquireContextHandle_W( DWORD CredentialFlags, PVOID Credentials, PHANDLE pContext );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsAcquireContextHandle_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "9a820165-2f78-44f4-b49f-dc7a2b6fb4e5")]
public static extern DNS_STATUS DnsAcquireContextHandle([MarshalAs(UnmanagedType.Bool)] bool CredentialFlags, [In, Optional] IntPtr Credentials, out SafeHDNSCONTEXT pContext);
public static extern DNS_STATUS DnsAcquireContextHandle([MarshalAs(UnmanagedType.Bool)] bool CredentialFlags,
[In, Optional] IntPtr Credentials, out SafeHDNSCONTEXT pContext);
/// <summary>The <c>DnsCancelQuery</c> function can be used to cancel a pending query to the DNS namespace.</summary>
/// <param name="pCancelHandle">
@ -174,7 +175,8 @@ namespace Vanara.PInvoke
// DnsExtractRecordsFromMessage_W( PDNS_MESSAGE_BUFFER pDnsBuffer, WORD wMessageLength, PDNS_RECORD *ppRecord );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsExtractRecordsFromMessage_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "0179bf3e-9243-4dd7-a2ab-e2f6f4bf4b82")]
public static extern DNS_STATUS DnsExtractRecordsFromMessage([In] IntPtr pDnsBuffer, ushort wMessageLength, out SafeDnsRecordList ppRecord);
public static extern DNS_STATUS DnsExtractRecordsFromMessage([In] IntPtr pDnsBuffer, ushort wMessageLength,
out SafeDnsRecordList ppRecord);
/// <summary>The <c>DnsFree</c> function frees memory allocated for DNS records that was obtained using the DnsQuery function.</summary>
/// <param name="pData">A pointer to the DNS data to be freed.</param>
@ -253,7 +255,8 @@ namespace Vanara.PInvoke
// DWORD DnsGetApplicationSettings( DWORD *pcServers, DNS_CUSTOM_SERVER **ppDefaultServers, DNS_APPLICATION_SETTINGS *pSettings );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "NF:windns.DnsGetApplicationSettings", MinClient = PInvokeClient.Windows11)]
public static extern Win32Error DnsGetApplicationSettings(out uint pcServers, out IntPtr ppDefaultServers, out DNS_APPLICATION_SETTINGS pSettings);
public static extern Win32Error DnsGetApplicationSettings(out uint pcServers, out IntPtr ppDefaultServers,
out DNS_APPLICATION_SETTINGS pSettings);
/// <summary>Retrieves the per-application DNS settings.</summary>
/// <param name="ppDefaultServers">
@ -310,7 +313,8 @@ namespace Vanara.PInvoke
// completionRoutine, void *completionContext );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "fdc8eb09-e071-4f03-974a-2b11a657ab18")]
public static extern Win32Error DnsGetProxyInformation([MarshalAs(UnmanagedType.LPWStr)] string hostName, ref DNS_PROXY_INFORMATION proxyInformation, ref DNS_PROXY_INFORMATION defaultProxyInformation,
public static extern Win32Error DnsGetProxyInformation([MarshalAs(UnmanagedType.LPWStr)] string hostName,
ref DNS_PROXY_INFORMATION proxyInformation, ref DNS_PROXY_INFORMATION defaultProxyInformation,
IntPtr completionRoutine = default, IntPtr completionContext = default);
/// <summary>
@ -333,7 +337,8 @@ namespace Vanara.PInvoke
// completionRoutine, void *completionContext );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "fdc8eb09-e071-4f03-974a-2b11a657ab18")]
public static extern Win32Error DnsGetProxyInformation([MarshalAs(UnmanagedType.LPWStr)] string hostName, ref DNS_PROXY_INFORMATION proxyInformation, IntPtr defaultProxyInformation = default,
public static extern Win32Error DnsGetProxyInformation([MarshalAs(UnmanagedType.LPWStr)] string hostName,
ref DNS_PROXY_INFORMATION proxyInformation, IntPtr defaultProxyInformation = default,
IntPtr completionRoutine = default, IntPtr completionContext = default);
/// <summary>
@ -380,7 +385,8 @@ namespace Vanara.PInvoke
// PDNS_RECORD pAddRecords, PDNS_RECORD pDeleteRecords, DWORD Options, HANDLE hCredentials, PVOID pExtraList, PVOID pReserved );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsModifyRecordsInSet_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "4287b4e1-a7a2-4b73-b5bb-21bc639bae73")]
public static extern DNS_STATUS DnsModifyRecordsInSet(in DNS_RECORD pAddRecords, in DNS_RECORD pDeleteRecords, DNS_UPDATE Options, [Optional] IntPtr hCredentials, [In, Out, Optional] IntPtr pExtraList, IntPtr pReserved = default);
public static extern DNS_STATUS DnsModifyRecordsInSet(in DNS_RECORD pAddRecords, in DNS_RECORD pDeleteRecords,
DNS_UPDATE Options, [Optional] IntPtr hCredentials, [In, Out, Optional] IntPtr pExtraList, IntPtr pReserved = default);
/// <summary>
/// The <c>DnsModifyRecordsInSet</c> function adds, modifies or removes a Resource Record (RR) set that may have been previously
@ -426,7 +432,8 @@ namespace Vanara.PInvoke
// PDNS_RECORD pAddRecords, PDNS_RECORD pDeleteRecords, DWORD Options, HANDLE hCredentials, PVOID pExtraList, PVOID pReserved );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsModifyRecordsInSet_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "4287b4e1-a7a2-4b73-b5bb-21bc639bae73")]
public static extern DNS_STATUS DnsModifyRecordsInSet([In] IntPtr pAddRecords, [In] IntPtr pDeleteRecords, DNS_UPDATE Options, [Optional] IntPtr hCredentials, [In, Out, Optional] IntPtr pExtraList, IntPtr pReserved = default);
public static extern DNS_STATUS DnsModifyRecordsInSet([In] IntPtr pAddRecords, [In] IntPtr pDeleteRecords,
DNS_UPDATE Options, [Optional] IntPtr hCredentials, [In, Out, Optional] IntPtr pExtraList, IntPtr pReserved = default);
/// <summary>The <c>DnsNameCompare</c> function compares two DNS names.</summary>
/// <param name="pName1"/>
@ -497,8 +504,8 @@ namespace Vanara.PInvoke
// DWORD Options, PVOID pExtra, PDNS_RECORD *ppQueryResults, PVOID *pReserved );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "3d810b76-cea1-4904-9b5a-c2566b332c2c")]
public static extern DNS_STATUS DnsQuery(string pszName, DNS_TYPE wType, DNS_QUERY_OPTIONS Options, [In, Out, Optional] IntPtr pExtra,
out SafeDnsRecordList ppQueryResults, IntPtr pReserved = default);
public static extern DNS_STATUS DnsQuery(string pszName, DNS_TYPE wType, DNS_QUERY_OPTIONS Options,
[In, Out, Optional] IntPtr pExtra, out SafeDnsRecordList ppQueryResults, IntPtr pReserved = default);
/// <summary>
/// <para>
@ -664,8 +671,9 @@ namespace Vanara.PInvoke
// Config, DWORD Flag, PCWSTR pwsAdapterName, PVOID pReserved, PVOID pBuffer, PDWORD pBufLen );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "83de7df8-7e89-42fe-b609-1dc173afc9df")]
public static extern DNS_STATUS DnsQueryConfig(DNS_CONFIG_TYPE Config, DNS_CONFIG_FLAG Flag, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pwsAdapterName,
[In, Optional] IntPtr pReserved, IntPtr pBuffer, ref uint pBufLen);
public static extern DNS_STATUS DnsQueryConfig(DNS_CONFIG_TYPE Config, DNS_CONFIG_FLAG Flag,
[Optional, MarshalAs(UnmanagedType.LPWStr)] string pwsAdapterName, [In, Optional] IntPtr pReserved, IntPtr pBuffer,
ref uint pBufLen);
/// <summary>
/// <para>
@ -767,7 +775,8 @@ namespace Vanara.PInvoke
// pQueryRequest, PDNS_QUERY_RESULT pQueryResults, PDNS_QUERY_CANCEL pCancelHandle );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "22664B9A-5010-42E7-880B-8D5B16A9F2DC")]
public static extern DNS_STATUS DnsQueryEx(in DNS_QUERY_REQUEST pQueryRequest, ref DNS_QUERY_RESULT pQueryResults, ref DNS_QUERY_CANCEL pCancelHandle);
public static extern DNS_STATUS DnsQueryEx(in DNS_QUERY_REQUEST pQueryRequest, ref DNS_QUERY_RESULT pQueryResults,
ref DNS_QUERY_CANCEL pCancelHandle);
/// <summary>
/// <para>
@ -869,7 +878,8 @@ namespace Vanara.PInvoke
// pQueryRequest, PDNS_QUERY_RESULT pQueryResults, PDNS_QUERY_CANCEL pCancelHandle );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "22664B9A-5010-42E7-880B-8D5B16A9F2DC")]
public static extern DNS_STATUS DnsQueryEx(in DNS_QUERY_REQUEST3 pQueryRequest, ref DNS_QUERY_RESULT pQueryResults, ref DNS_QUERY_CANCEL pCancelHandle);
public static extern DNS_STATUS DnsQueryEx(in DNS_QUERY_REQUEST3 pQueryRequest, ref DNS_QUERY_RESULT pQueryResults,
ref DNS_QUERY_CANCEL pCancelHandle);
/// <summary>The <c>DnsRecordCompare</c> function compares two DNS resource records (RR).</summary>
/// <param name="pRecord1">A pointer to a DNS_RECORD structure that contains the first DNS RR of the comparison pair.</param>
@ -1084,7 +1094,8 @@ namespace Vanara.PInvoke
// PDNS_RECORD pReplaceSet, DWORD Options, HANDLE hContext, PVOID pExtraInfo, PVOID pReserved );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsReplaceRecordSetW", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "7b99f440-72fa-4cf4-9267-98f436e99a50")]
public static extern DNS_STATUS DnsReplaceRecordSet(in DNS_RECORD pReplaceSet, DNS_UPDATE Options, [Optional] HDNSCONTEXT hContext, IntPtr pExtraInfo = default, IntPtr pReserved = default);
public static extern DNS_STATUS DnsReplaceRecordSet(in DNS_RECORD pReplaceSet, DNS_UPDATE Options, [Optional] HDNSCONTEXT hContext,
IntPtr pExtraInfo = default, IntPtr pReserved = default);
/// <summary>The <c>DnsReplaceRecordSet</c> function type replaces an existing resource record (RR) set.</summary>
/// <param name="pReplaceSet">
@ -1107,7 +1118,8 @@ namespace Vanara.PInvoke
// PDNS_RECORD pReplaceSet, DWORD Options, HANDLE hContext, PVOID pExtraInfo, PVOID pReserved );
[DllImport(Lib.Dnsapi, SetLastError = false, EntryPoint = "DnsReplaceRecordSetW", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "7b99f440-72fa-4cf4-9267-98f436e99a50")]
public static extern DNS_STATUS DnsReplaceRecordSet([In] IntPtr pReplaceSet, DNS_UPDATE Options, [Optional] HDNSCONTEXT hContext, IntPtr pExtraInfo = default, IntPtr pReserved = default);
public static extern DNS_STATUS DnsReplaceRecordSet([In] IntPtr pReplaceSet, DNS_UPDATE Options, [Optional] HDNSCONTEXT hContext,
IntPtr pExtraInfo = default, IntPtr pReserved = default);
/// <summary>Used to initiate a DNS-SD discovery for services running on the local network.</summary>
/// <param name="pRequest">A pointer to a DNS_SERVICE_BROWSE_REQUEST structure that contains the browse request information.</param>
@ -1222,6 +1234,23 @@ namespace Vanara.PInvoke
[PInvokeData("windns.h")]
public static extern DNS_STATUS DnsServiceDeRegister(in DNS_SERVICE_REGISTER_REQUEST pRequest, in DNS_SERVICE_CANCEL pCancel);
/// <summary>Used to remove a registered service.</summary>
/// <param name="pRequest">A pointer to the DNS_SERVICE_REGISTER_REQUEST structure that was used to register the service.</param>
/// <param name="pCancel">Must be <see langword="null"/>.</param>
/// <returns>
/// If successful, returns <c>DNS_REQUEST_PENDING</c>; otherwise, returns the appropriate DNS-specific error code as defined in
/// <c>Winerror.h</c>. For extended error information, call GetLastError.
/// </returns>
/// <remarks>
/// This function is asynchronous. The callback will be invoked when the deregistration is completed, with a copy of the
/// DNS_SERVICE_INSTANCE structure that was passed to DnsServiceRegister when the service was registered.
/// </remarks>
// https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsservicederegister DWORD DnsServiceDeRegister(
// PDNS_SERVICE_REGISTER_REQUEST pRequest, PDNS_SERVICE_CANCEL pCancel );
[DllImport(Lib.Dnsapi, SetLastError = true, ExactSpelling = true)]
[PInvokeData("windns.h")]
public static extern DNS_STATUS DnsServiceDeRegister(in DNS_SERVICE_REGISTER_REQUEST pRequest, [In, Optional] IntPtr pCancel);
/// <summary>Used to free the resources associated with a DNS_SERVICE_INSTANCE structure.</summary>
/// <param name="pInstance">A pointer to the DNS_SERVICE_INSTANCE structure that is to be freed.</param>
/// <returns>None</returns>
@ -1504,7 +1533,8 @@ namespace Vanara.PInvoke
// PSOCKADDR server, PCWSTR queryName, PDWORD serverStatus );
[DllImport(Lib.Dnsapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("windns.h", MSDNShortId = "5b362d05-87b2-44dd-8198-bcb5ab5a64f6")]
public static extern DNS_STATUS DnsValidateServerStatus([In] SOCKADDR server, [Optional, MarshalAs(UnmanagedType.LPWStr)] string queryName, out DnsServerStatus serverStatus);
public static extern DNS_STATUS DnsValidateServerStatus([In] SOCKADDR server, [Optional, MarshalAs(UnmanagedType.LPWStr)] string queryName,
out DnsServerStatus serverStatus);
/// <summary>
/// The <c>DnsWriteQuestionToBuffer</c> function type creates a DNS query message and stores it in a DNS_MESSAGE_BUFFER structure.
@ -1530,8 +1560,8 @@ namespace Vanara.PInvoke
[DllImport(Lib.Dnsapi, SetLastError = true, EntryPoint = "DnsWriteQuestionToBuffer_W", CharSet = CharSet.Unicode)]
[PInvokeData("windns.h", MSDNShortId = "9aa853aa-d9b5-41e3-a82a-c25de199924d")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DnsWriteQuestionToBuffer([In, Out] IntPtr pDnsBuffer, ref uint pdwBufferSize, [MarshalAs(UnmanagedType.LPWStr)] string pszName,
DNS_TYPE wType, ushort Xid, [MarshalAs(UnmanagedType.Bool)] bool fRecursionDesired);
public static extern bool DnsWriteQuestionToBuffer([In, Out] IntPtr pDnsBuffer, ref uint pdwBufferSize,
[MarshalAs(UnmanagedType.LPWStr)] string pszName, DNS_TYPE wType, ushort Xid, [MarshalAs(UnmanagedType.Bool)] bool fRecursionDesired);
/// <summary>Provides a handle to a DNS Context.</summary>
[StructLayout(LayoutKind.Sequential)]

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using Vanara.Extensions;
using Vanara.InteropServices;
@ -9,11 +11,11 @@ using IP4_ADDRESS = Vanara.PInvoke.Ws2_32.IN_ADDR;
using IP6_ADDRESS = Vanara.PInvoke.Ws2_32.IN6_ADDR;
#pragma warning disable IDE1006 // Naming Styles
namespace Vanara.PInvoke
namespace Vanara.PInvoke;
/// <summary>Functions, structures and constants from windns.h.</summary>
public static partial class DnsApi
{
/// <summary>Functions, structures and constants from windns.h.</summary>
public static partial class DnsApi
{
/// <summary>Defines the maximum sockaddr length for DNS addresses</summary>
public const int DNS_ADDR_MAX_SOCKADDR_LENGTH = 32;
@ -781,7 +783,7 @@ namespace Vanara.PInvoke
/// <summary/>
[CorrespondingType(typeof(DNS_WINSR_DATA))]
DNS_TYPE_NBSTAT = (DNS_TYPE_WINSR),
DNS_TYPE_NBSTAT = DNS_TYPE_WINSR,
}
/// <summary>A value that contains a bitmap of DNS Update Options.</summary>
@ -1672,18 +1674,22 @@ namespace Vanara.PInvoke
/// A pointer to a string that represents a set of NAPTR RR flags which determine the interpretation and processing of NAPTR
/// record fields as defined in section 2 of RFC 2915.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pFlags;
[MarshalAs(UnmanagedType.LPTStr)]
public string pFlags;
/// <summary>
/// A pointer to a string that represents the available services in this rewrite path as defined in section 2 of RFC 2915.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pService;
[MarshalAs(UnmanagedType.LPTStr)]
public string pService;
/// <summary>A pointer to a string that represents a substitution expression as defined in sections 2 and 3 of RFC 2915.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pRegularExpression;
[MarshalAs(UnmanagedType.LPTStr)]
public string pRegularExpression;
/// <summary>A pointer to a string that represents the next NAPTR query name as defined in section 2 of RFC 2915.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pReplacement;
[MarshalAs(UnmanagedType.LPTStr)]
public string pReplacement;
}
/// <summary>The <c>DNS_NSEC_DATA</c> structure represents an NSEC resource record (RR) as specified in section 4 of RFC 4034.</summary>
@ -1701,7 +1707,8 @@ namespace Vanara.PInvoke
/// A pointer to a string that represents the authoritative owner name of the next domain in the canonical ordering of the zone
/// as specified in section 4.1.1 of RFC 4034.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNextDomainName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNextDomainName;
/// <summary>The length, in bytes, of <c>TypeBitMaps</c>.</summary>
public ushort wTypeBitMapsLength;
@ -1808,7 +1815,8 @@ namespace Vanara.PInvoke
public struct DNS_NXT_DATA
{
/// <summary>A pointer to a string that represents the name of the next domain.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameNext;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameNext;
/// <summary>The number of elements in the <c>wTypes</c> array. <c>wNumTypes</c> must be 2 or greater but cannot exceed 8.</summary>
public ushort wNumTypes;
@ -1884,7 +1892,8 @@ namespace Vanara.PInvoke
public struct DNS_PTR_DATA
{
/// <summary>A pointer to a string that represents the pointer (PTR) record data.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameHost;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameHost;
}
/// <summary>A <c>DNS_QUERY_CANCEL</c> structure can be used to cancel an asynchronous DNS query.</summary>
@ -2191,7 +2200,8 @@ namespace Vanara.PInvoke
/// A pointer to a string that represents the domain name of the record set. This must be in the string format that corresponds
/// to the function called, such as ANSI, Unicode, or UTF8.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
/// <summary>
/// A value that represents the RR DNS Record Type. <c>wType</c> determines the format of <c>Data</c>. For example, if the value
@ -2233,14 +2243,14 @@ namespace Vanara.PInvoke
get
{
if (wType == 0 || wDataLength == 0) return null;
var type = CorrespondingTypeAttribute.GetCorrespondingTypes(wType, CorrespondingAction.GetSet).FirstOrDefault();
var ptr = DataPtr;
Type type = CorrespondingTypeAttribute.GetCorrespondingTypes(wType, CorrespondingAction.GetSet).FirstOrDefault();
IntPtr ptr = DataPtr;
return type is null ? ptr : ptr.Convert(wDataLength, type, CharSet.Unicode);
}
set => wDataLength = (ushort)DataPtr.Write(value, 0, DataSize);
}
private static readonly int DataSize = Marshal.SizeOf(typeof(DNS_RECORD)) - 16 - (IntPtr.Size * 2);
private static readonly int DataSize = Marshal.SizeOf(typeof(DNS_RECORD)) - 16 - IntPtr.Size * 2;
/// <summary>Gets the pointer to the 'Data' union.</summary>
/// <value>The 'Data' union pointer.</value>
@ -2265,9 +2275,10 @@ namespace Vanara.PInvoke
public T GetDataAsType<T>() where T : struct
{
if (wType == 0 || wDataLength == 0) return default;
if (!CorrespondingTypeAttribute.CanGet<DNS_TYPE>(typeof(T), out var tType) || tType != wType)
if (!CorrespondingTypeAttribute.CanGet<DNS_TYPE>(typeof(T), out DNS_TYPE tType) || tType != wType)
throw new ArgumentException("The current record does not support retrieving the supplied type param.");
var ptr = DataPtr;
IntPtr ptr = DataPtr;
return ptr.Convert<T>(wDataLength, CharSet.Unicode);
}
@ -2592,7 +2603,8 @@ namespace Vanara.PInvoke
/// generalized form "_&lt;ServiceType&gt;._&lt;TransportProtocol&gt;.local". For example, "_http._tcp.local", which defines a
/// query to browse for http services on the local link.
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)] public string QueryName;
[MarshalAs(UnmanagedType.LPWStr)]
public string QueryName;
/// <summary>The callback function based on <see cref="Version"/>.</summary>
public DNS_SERVICE_BROWSE_REQUEST_CALLBACK Callback;
@ -2605,13 +2617,15 @@ namespace Vanara.PInvoke
/// A pointer to a function (of type DNS_SERVICE_BROWSE_CALLBACK) that represents the callback to be invoked asynchronously.
/// This field is used if <see cref="Version"/> is <c>DNS_QUERY_REQUEST_VERSION1</c>.
/// </summary>
[FieldOffset(0), MarshalAs(UnmanagedType.FunctionPtr)] public DNS_SERVICE_BROWSE_CALLBACK pBrowseCallback;
[FieldOffset(0), MarshalAs(UnmanagedType.FunctionPtr)]
public DNS_SERVICE_BROWSE_CALLBACK pBrowseCallback;
/// <summary>
/// A pointer to a function (of type DNS_QUERY_COMPLETION_ROUTINE) that represents the callback to be invoked
/// asynchronously. This field is used if <see cref="Version"/> is <c>DNS_QUERY_REQUEST_VERSION2</c>.
/// </summary>
[FieldOffset(0), MarshalAs(UnmanagedType.FunctionPtr)] public DNS_QUERY_COMPLETION_ROUTINE pBrowseCallbackV2;
[FieldOffset(0), MarshalAs(UnmanagedType.FunctionPtr)]
public DNS_QUERY_COMPLETION_ROUTINE pBrowseCallbackV2;
}
/// <summary>A pointer to a user context.</summary>
@ -2647,17 +2661,25 @@ namespace Vanara.PInvoke
/// with ".local". It takes the generalized form "&lt;ServiceName&gt;._&lt;ServiceType&gt;._&lt;TransportProtocol&gt;.local".
/// For example, "MyMusicServer._http._tcp.local".
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)] public string pszInstanceName;
[MarshalAs(UnmanagedType.LPWStr)]
public string pszInstanceName;
/// <summary>A string that represents the name of the host of the service.</summary>
[MarshalAs(UnmanagedType.LPWStr)] public string pszHostName;
[MarshalAs(UnmanagedType.LPWStr)]
public string pszHostName;
/// <summary>A pointer to an <c>IP4_ADDRESS</c> structure that represents the service-associated IPv4 address.</summary>
public IntPtr ip4Address;
/// <summary>The service-associated IPv4 address, if defined.</summary>
public IPEndPoint IPv4EndPoint => ip4Address == IntPtr.Zero ? null : new(new IPAddress(ip4Address.ToArray<byte>(4)), wPort);
/// <summary>A pointer to an IP6_ADDRESS structure that represents the service-associated IPv6 address.</summary>
public IntPtr ip6Address;
/// <summary>The service-associated IPv6 address, if defined.</summary>
public IPEndPoint IPv6EndPoint => ip6Address == IntPtr.Zero ? null : new(new IPAddress(ip6Address.ToArray<byte>(16)), wPort);
/// <summary>A value that represents the port on which the service is running.</summary>
public ushort wPort;
@ -2678,6 +2700,14 @@ namespace Vanara.PInvoke
/// <summary>A value that contains the interface index on which the service was discovered.</summary>
public uint dwInterfaceIndex;
/// <summary>Gets the properties exposed by this structure.</summary>
/// <value>The DNS service properties.</value>
public IReadOnlyDictionary<string, string> Properties => dwPropertyCount == 0 ?
new Dictionary<string, string>(0) :
keys.ToStringEnum((int)dwPropertyCount, CharSet.Unicode).
Zip(values.ToStringEnum((int)dwPropertyCount, CharSet.Unicode), (k, v) => new { k, v }).
ToDictionary(x => x.k, x => x.v);
}
/// <summary>
@ -2716,7 +2746,8 @@ namespace Vanara.PInvoke
/// <see langword="true"/> if the DNS protocol should be used to advertise the service; <see langword="false"/> if the mDNS
/// protocol should be used.
/// </summary>
[MarshalAs(UnmanagedType.Bool)] public bool unicastEnabled;
[MarshalAs(UnmanagedType.Bool)]
public bool unicastEnabled;
}
/// <summary>
@ -2743,7 +2774,8 @@ namespace Vanara.PInvoke
/// name, and ends with ".local". It takes the generalized form
/// "&lt;ServiceName&gt;._&lt;ServiceType&gt;._&lt;TransportProtocol&gt;.local". For example, "MyMusicServer._http._tcp.local".
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)] public string QueryName;
[MarshalAs(UnmanagedType.LPWStr)]
public string QueryName;
/// <summary>A pointer to a function (of type DNS_SERVICE_RESOLVE_COMPLETE) that represents the callback to be invoked asynchronously.</summary>
[MarshalAs(UnmanagedType.FunctionPtr)]
@ -2832,7 +2864,8 @@ namespace Vanara.PInvoke
public ushort wSignatureLength;
/// <summary>A pointer to a string that represents the name of the <c>Signature</c> generator.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameSigner;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameSigner;
/// <summary>A <c>BYTE</c> array that contains the RR set signature as specified in section 3.1.8 of RFC 4034.</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
@ -2854,10 +2887,12 @@ namespace Vanara.PInvoke
/// <summary>
/// A pointer to a string that represents the name of the authoritative DNS server for the zone to which the record belongs.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNamePrimaryServer;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNamePrimaryServer;
/// <summary>A pointer to a string that represents the name of the responsible party for the zone to which the record belongs.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameAdministrator;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameAdministrator;
/// <summary>The serial number of the SOA record.</summary>
public uint dwSerialNo;
@ -2889,7 +2924,8 @@ namespace Vanara.PInvoke
public struct DNS_SRV_DATA
{
/// <summary>A pointer to a string that represents the target host.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameTarget;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameTarget;
/// <summary>
/// The priority of the target host specified in <c>pNameTarget</c>. Lower numbers imply higher priority to clients attempting
@ -2925,7 +2961,8 @@ namespace Vanara.PInvoke
public struct DNS_TKEY_DATA
{
/// <summary>A pointer to a string that represents the name of the key as defined in section 2.1 of RFC 2930.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameAlgorithm;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameAlgorithm;
/// <summary>
/// A pointer to a string representing the name of the algorithm as defined in section 2.3 of RFC 2930. <c>pKey</c> is used to
@ -3013,7 +3050,8 @@ namespace Vanara.PInvoke
public byte cAlgNameLength;
/// <summary>Reserved. Do not use.</summary>
[MarshalAs(UnmanagedType.Bool)] public bool bPacketPointers;
[MarshalAs(UnmanagedType.Bool)]
public bool bPacketPointers;
}
/// <summary>Undocumented.</summary>
@ -3061,7 +3099,8 @@ namespace Vanara.PInvoke
/// A pointer to a string that represents the name of the key used to generate <c>pSignature</c> as defined in section 2.3 of
/// RFC 2845.
/// </summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameAlgorithm;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameAlgorithm;
/// <summary>
/// <para>
@ -3146,7 +3185,8 @@ namespace Vanara.PInvoke
public byte cAlgNameLength;
/// <summary>Reserved for future use. Do not use.</summary>
[MarshalAs(UnmanagedType.Bool)] public bool bPacketPointers;
[MarshalAs(UnmanagedType.Bool)]
public bool bPacketPointers;
}
/// <summary>The <c>DNS_TXT_DATA</c> structure represents a DNS text (TXT) record as specified in section 3.3.14 of RFC 1035.</summary>
@ -3268,7 +3308,8 @@ namespace Vanara.PInvoke
public uint dwCacheTimeout;
/// <summary>A pointer to a string that represents the domain name to append to the name returned by a WINS reverse-lookup.</summary>
[MarshalAs(UnmanagedType.LPTStr)] public string pNameResultDomain;
[MarshalAs(UnmanagedType.LPTStr)]
public string pNameResultDomain;
}
/// <summary>
@ -3415,7 +3456,8 @@ namespace Vanara.PInvoke
public uint ulRefCount;
/// <summary>A string representing the name to be queried over mDNS.</summary>
[MarshalAs(UnmanagedType.LPWStr)] public string Query;
[MarshalAs(UnmanagedType.LPWStr)]
public string Query;
/// <summary>A value representing the type of the records to be queried. See DNS_RECORD_TYPE for possible values.</summary>
public DNS_TYPE QueryType;
@ -3442,7 +3484,8 @@ namespace Vanara.PInvoke
public IntPtr pQueryContext;
/// <summary>Reserved. Do not use.</summary>
[MarshalAs(UnmanagedType.Bool)] public bool fAnswerReceived;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnswerReceived;
/// <summary>Reserved. Do not use.</summary>
public uint ulResendCount;
@ -3453,28 +3496,33 @@ namespace Vanara.PInvoke
public class SafePDNS_SERVICE_INSTANCE : SafeHANDLE
{
private uint _dwInterfaceIndex;
private IP4_ADDRESS? _ip4Address;
private IP6_ADDRESS? _ip6Address;
private string[] _keys;
private IPEndPoint _ip4Address;
private IPEndPoint _ip6Address;
private string _pszHostName;
private string _pszInstanceName;
private string[] _values;
private IReadOnlyDictionary<string, string> _props;
private ushort _wPort;
private ushort _wPriority;
private ushort _wWeight;
private bool populated = false;
/// <summary>Initializes a new instance of the <see cref="SafePDNS_SERVICE_INSTANCE"/> class.</summary>
/// <param name="pDnsServiceInst">A valid pointer to a DNS_SERVICE_INSTANCE instance.</param>
public SafePDNS_SERVICE_INSTANCE(IntPtr pDnsServiceInst) : base(pDnsServiceInst, true) { }
private SafePDNS_SERVICE_INSTANCE() { }
/// <summary>A value that contains the interface index on which the service was discovered.</summary>
public uint dwInterfaceIndex => PopulateFetch(ref _dwInterfaceIndex);
/// <summary>A pointer to an <c>IP4_ADDRESS</c> structure that represents the service-associated IPv4 address.</summary>
public IP4_ADDRESS? ip4Address => PopulateFetch(ref _ip4Address);
public IPEndPoint ip4Address => PopulateFetch(ref _ip4Address);
/// <summary>A pointer to an IP6_ADDRESS structure that represents the service-associated IPv6 address.</summary>
public IP6_ADDRESS? ip6Address => PopulateFetch(ref _ip6Address);
public IPEndPoint ip6Address => PopulateFetch(ref _ip6Address);
/// <summary/>
public string[] keys => PopulateFetch(ref _keys);
public IReadOnlyDictionary<string, string> properties => PopulateFetch(ref _props);
/// <summary>A string that represents the name of the host of the service.</summary>
public string pszHostName => PopulateFetch(ref _pszHostName);
@ -3486,9 +3534,6 @@ namespace Vanara.PInvoke
/// </summary>
public string pszInstanceName => PopulateFetch(ref _pszInstanceName);
/// <summary/>
public string[] values => PopulateFetch(ref _values);
/// <summary>A value that represents the port on which the service is running.</summary>
public ushort wPort => PopulateFetch(ref _wPort);
@ -3510,16 +3555,15 @@ namespace Vanara.PInvoke
{
if (!populated && !IsInvalid && !IsClosed)
{
var val = handle.ToStructure<DNS_SERVICE_INSTANCE>();
DNS_SERVICE_INSTANCE val = handle.ToStructure<DNS_SERVICE_INSTANCE>();
_pszInstanceName = val.pszInstanceName;
_pszHostName = val.pszHostName;
_ip4Address = val.ip4Address.ToNullableStructure<IP4_ADDRESS>();
_ip6Address = val.ip6Address.ToNullableStructure<IP6_ADDRESS>();
_ip4Address = val.IPv4EndPoint;
_ip6Address = val.IPv6EndPoint;
_wPort = val.wPort;
_props = val.Properties;
_wPriority = val.wPriority;
_wWeight = val.wWeight;
_keys = val.keys.ToStringEnum((int)val.dwPropertyCount, CharSet.Unicode).ToArray();
_values = val.values.ToStringEnum((int)val.dwPropertyCount, CharSet.Unicode).ToArray();
_dwInterfaceIndex = val.dwInterfaceIndex;
populated = true;
}
@ -3540,7 +3584,7 @@ namespace Vanara.PInvoke
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr pNativeData, SizeT allocatedBytes)
{
if (pNativeData == IntPtr.Zero) return null;
using var s = new PDNS_MESSAGE_BUFFER(pNativeData, allocatedBytes);
using PDNS_MESSAGE_BUFFER s = new(pNativeData, allocatedBytes);
return s.Value;
}
}
@ -3576,14 +3620,13 @@ namespace Vanara.PInvoke
SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(typeof(DNS_NSEC3_DATA));
SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object managedObject) =>
managedObject is null ? SafeCoTaskMemHandle.Null : (SafeAllocatedMemoryHandle)new SafeDNS_NSEC3_DATA((DNS_NSEC3_DATA)managedObject);
managedObject is null ? SafeCoTaskMemHandle.Null : new SafeDNS_NSEC3_DATA((DNS_NSEC3_DATA)managedObject);
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr pNativeData, SizeT allocatedBytes)
{
if (pNativeData == IntPtr.Zero) return null;
using var s = new SafeDNS_NSEC3_DATA(pNativeData, allocatedBytes);
using SafeDNS_NSEC3_DATA s = new(pNativeData, allocatedBytes);
return s.Value;
}
}
}
}

View File

@ -142,17 +142,20 @@ namespace Vanara.PInvoke.Tests
[Test]
public void DnsServiceBrowseTest()
{
using var evt = Kernel32.CreateEvent(null, false, true);
const int timeout = 20000;
const string query = "_http._tcp.local";
using var evt = Kernel32.CreateEvent(null, false, false);
var br = new DNS_SERVICE_BROWSE_REQUEST
{
Version = DNS_QUERY_REQUEST_VERSION1,
QueryName = "_windns-example._udp",
pQueryContext = (IntPtr)evt
QueryName = query,
pQueryContext = (IntPtr)evt,
Callback = new() { pBrowseCallback = (DNS_SERVICE_BROWSE_CALLBACK)Callback }
};
br.Callback.pBrowseCallback = Callback;
Assert.That(DnsServiceBrowse(br, out var cancel), ResultIs.Value(Win32Error.DNS_REQUEST_PENDING));
if (Kernel32.WaitForSingleObject(evt, 20000) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
if (Kernel32.WaitForSingleObject(evt, timeout) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
{
Assert.That(DnsServiceBrowseCancel(cancel), ResultIs.Successful);
Assert.Fail("Browse callback not called.");
@ -161,33 +164,31 @@ namespace Vanara.PInvoke.Tests
var queryRequest = new MDNS_QUERY_REQUEST
{
Version = DNS_QUERY_REQUEST_VERSION1,
Query = "_windns-example._udp.local",
Query = query,
QueryType = DNS_TYPE.DNS_TYPE_PTR,
QueryOptions = (ulong)DNS_QUERY_OPTIONS.DNS_QUERY_STANDARD,
pQueryCallback = QueryCallback,
pQueryContext = (IntPtr)evt
};
Assert.That(DnsStartMulticastQuery(queryRequest, out var queryHandle), ResultIs.Successful);
if (Kernel32.WaitForSingleObject(evt, 20000) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
if (Kernel32.WaitForSingleObject(evt, timeout) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
{
Assert.That(DnsStopMulticastQuery(queryHandle), ResultIs.Successful);
Assert.Fail("Multicast callback not called.");
}
void Callback(uint Status, IntPtr pQueryContext, IntPtr pDnsRecord)
void Callback(Win32Error Status, IntPtr pQueryContext, IntPtr pDnsRecord)
{
using var recs = new SafeDnsRecordList(pDnsRecord);
System.Diagnostics.Debug.WriteLine($"Stat:{(Win32Error)Status}; Name:{recs.FirstOrDefault().pName}");
Kernel32.SetEvent(pQueryContext);
}
void QueryCallback(IntPtr pQueryContext, IntPtr pQueryHandle, IntPtr pQueryResults)
{
var pQueryRecs = IntPtr.Zero;
unsafe
{
var pQR = (DNS_QUERY_RESULT*)(void*)pQueryResults;
pQueryResults = pQR->pQueryRecords;
}
using var recs = new SafeDnsRecordList(pQueryRecs);
ref DNS_QUERY_RESULT pQR = ref pQueryResults.AsRef<DNS_QUERY_RESULT>();
System.Diagnostics.Debug.WriteLine($"Stat:{pQR.QueryStatus}; Opt:{pQR.QueryOptions}");
using var recs = new SafeDnsRecordList(pQR.pQueryRecords);
using var qevt = Kernel32.CreateEvent(null, false, true);
var rec = recs.FirstOrDefault();
if (rec.wDataLength == 0)
@ -196,11 +197,11 @@ namespace Vanara.PInvoke.Tests
{
Version = DNS_QUERY_REQUEST_VERSION1,
QueryName = rec.Data is DNS_PTR_DATA d ? d.pNameHost : null,
pResolveCompletionCallback = ResolveCallback,
pResolveCompletionCallback = (DNS_SERVICE_RESOLVE_COMPLETE)ResolveCallback,
pQueryContext = (IntPtr)qevt
};
Assert.That(DnsServiceResolve(resolveRequest, out var cancel), ResultIs.Value(Win32Error.DNS_REQUEST_PENDING));
if (Kernel32.WaitForSingleObject(qevt, 20000) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
if (Kernel32.WaitForSingleObject(qevt, timeout) != Kernel32.WAIT_STATUS.WAIT_OBJECT_0)
{
Assert.That(DnsServiceResolveCancel(cancel), ResultIs.Successful);
Assert.Fail("Resolve callback not called.");
@ -208,8 +209,10 @@ namespace Vanara.PInvoke.Tests
Kernel32.SetEvent(pQueryContext);
}
void ResolveCallback(uint Status, IntPtr pQueryContext, IntPtr pInstance)
void ResolveCallback(Win32Error Status, IntPtr pQueryContext, IntPtr pInstance)
{
using var inst = new SafePDNS_SERVICE_INSTANCE(pInstance);
System.Diagnostics.Debug.WriteLine($"Stat:{(Win32Error)Status}; Name:{inst.pszInstanceName}");
Kernel32.SetEvent(pQueryContext);
}
}
@ -233,7 +236,7 @@ namespace Vanara.PInvoke.Tests
Assert.That(DnsServiceRegisterCancel(cancel), ResultIs.Successful);
Assert.That(DnsServiceDeRegister(sr, cancel), ResultIs.Value(Win32Error.DNS_REQUEST_PENDING));
void Callback(uint Status, IntPtr pQueryContext, IntPtr pInstance) => callbackCalled = true;
void Callback(Win32Error Status, IntPtr pQueryContext, IntPtr pInstance) => callbackCalled = true;
}
[Test]