本文发表在 rolia.net 枫下论坛using System;
using System.Runtime.InteropServices;
using System.Collections;
namespace MyCommon
{
public class DNS
{
/// <summary>
/// Dll Entrypoint for DNS Services
/// </summary>
[DllImport("dnsapi.dll",EntryPoint="DnsQuery_A")]
private static extern UInt32 DnsQuery( [MarshalAs(UnmanagedType.LPStr)]string lpstrName, [MarshalAs(UnmanagedType.U2)]UInt16 wType, [MarshalAs(UnmanagedType.U4)]UInt32 fOptions, [MarshalAs(UnmanagedType.LPStr)]string DNSServer, ref UInt32 prr, [MarshalAs(UnmanagedType.U4)]UInt32 t3 );
private string _DNSServer = null;
public string DNSServer
{
get
{
return _DNSServer;
}
set
{
_DNSServer = value;
}
}
public DNS(){}
public DNS(string DNSServer)
{
_DNSServer = DNSServer;
}
public IEnumerator LookupAll( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_ALL, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator LookupMX( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_MX, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator LookupA( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_A, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator Lookup( String strDomain, UInt16 dnstype, UInt32 querytype )
{
return( Lookup( ref strDomain, dnstype, querytype ) );
}
/// <summary>
/// Used to verify a domain for any type of record
/// </summary>
/// <param name="strDomain"></param>
/// <returns></returns>
private IEnumerator Lookup( ref string strDomain, UInt16 dnstype, UInt32 querytype )
{
DNS_RECORD dnsrec;
DNS_WRAPPER wrapper;
UInt32 ppQueryResultsSet = 0;
object Data = new object();
ArrayList list = new ArrayList();
try
{
uint temp = DnsQuery( strDomain, dnstype, querytype, _DNSServer, ref ppQueryResultsSet, (UInt32)0 );
if( DnsQuery( strDomain, dnstype, querytype, _DNSServer, ref ppQueryResultsSet, (UInt32)0 ) == 0 )
{
IntPtr ppr = new IntPtr( ppQueryResultsSet );
// Parse the records.
// Call function to loop through linked list and fill an array of records
do
{
// Get the DNS_RECORD
dnsrec = (DNS_RECORD)Marshal.PtrToStructure( ppr, typeof(DNS_RECORD) );
// Get the Data part
GetData( ppr.ToInt32(), ref dnsrec, ref Data );
// Wrap data in a struct with the type and data
wrapper = new DNS_WRAPPER();
wrapper.dnsType = dnsrec.wType;
wrapper.dnsData = Data;
// Add wrapper to array
list.Add( wrapper );
ppr = dnsrec.pNext;
} while ( ppr.ToInt32() != 0 );
}
}
catch
{
}
return( list.GetEnumerator() );
} // end of VerifyDomain
private void GetData( Int32 ptr, ref DNS_RECORD dnsrec, ref object Data )
{
ptr += Marshal.SizeOf( dnsrec );// Skip over the header portion of the DNS_RECORD to the data portion.
//ptr += 24;
switch ( dnsrec.wType )
{
case DNSTypes.DNS_TYPE_A:
Data = (DNS_A_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_A_DATA) );
break;
case DNSTypes.DNS_TYPE_CNAME:
case DNSTypes.DNS_TYPE_MB:
case DNSTypes.DNS_TYPE_MD:
case DNSTypes.DNS_TYPE_MF:
case DNSTypes.DNS_TYPE_MG:
case DNSTypes.DNS_TYPE_MR:
case DNSTypes.DNS_TYPE_NS:
case DNSTypes.DNS_TYPE_PTR:
Data = (DNS_PTR_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_PTR_DATA) );
break;
case DNSTypes.DNS_TYPE_HINFO:
case DNSTypes.DNS_TYPE_ISDN:
case DNSTypes.DNS_TYPE_X25:
Data = (DNS_TXT_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TXT_DATA) );
break;
case DNSTypes.DNS_TYPE_MINFO:
case DNSTypes.DNS_TYPE_RP:
Data = (DNS_MINFO_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_MINFO_DATA) );
break;
case DNSTypes.DNS_TYPE_MX:
case DNSTypes.DNS_TYPE_AFSDB:
case DNSTypes.DNS_TYPE_RT:
Data = (DNS_MX_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_MX_DATA) );
break;
case DNSTypes.DNS_TYPE_NULL:
Data = (DNS_NULL_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_NULL_DATA) );
break;
case DNSTypes.DNS_TYPE_SOA:
Data = (DNS_SOA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_SOA_DATA) );
break;
case DNSTypes.DNS_TYPE_WKS:
Data = (DNS_WKS_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WKS_DATA) );
break;
case DNSTypes.DNS_TYPE_AAAA:
Data = (DNS_AAAA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_AAAA_DATA) );
break;
case DNSTypes.DNS_TYPE_ATMA:
Data = (DNS_ATMA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_ATMA_DATA) );
break;
case DNSTypes.DNS_TYPE_NBSTAT:
//case DNS_TYPE_WINSR:
Data = (DNS_WINSR_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WINSR_DATA) );
break;
case DNSTypes.DNS_TYPE_SRV:
Data = (DNS_SRV_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_SRV_DATA) );
break;
case DNSTypes.DNS_TYPE_TKEY:
Data = (DNS_TKEY_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TKEY_DATA) );
break;
case DNSTypes.DNS_TYPE_TSIG:
Data = (DNS_TSIG_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TSIG_DATA) );
break;
case DNSTypes.DNS_TYPE_WINS:
Data = (DNS_WINS_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WINS_DATA) );
break;
case DNSTypes.DNS_TYPE_LOC:
Data = (DNS_LOC_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_LOC_DATA) );
break;
case DNSTypes.DNS_TYPE_AXFR:
case DNSTypes.DNS_TYPE_GPOS:
case DNSTypes.DNS_TYPE_IXFR:
case DNSTypes.DNS_TYPE_KEY:
case DNSTypes.DNS_TYPE_MAILA:
case DNSTypes.DNS_TYPE_MAILB:
case DNSTypes.DNS_TYPE_NSAP:
case DNSTypes.DNS_TYPE_NSAPPTR:
case DNSTypes.DNS_TYPE_NXT:
case DNSTypes.DNS_TYPE_PX:
case DNSTypes.DNS_TYPE_SIG:
case DNSTypes.DNS_TYPE_TEXT:
default:
Data = null;
break;
}// end of switch for each dns record type
}// end of GetDataType
/// <summary>
/// Structure for the actual DNS Record
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct DNS_RECORD
{
// public IntPtr pNext;
public IntPtr pNext;// 4 bytes
//[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string pName;// 4 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U2)] public UInt16 wType;// 2 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U2)] public UInt16 wDataLength;// 2 bytes
[ StructLayout( LayoutKind.Explicit )]// 4 bytes
public struct DNS_RECORD_FLAGS
{
[ FieldOffset( 0 )]
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 DW;
[ FieldOffset( 0 )]
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 S;
}// end of union for flags
public DNS_RECORD_FLAGS flags;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 dwTtl;// 4 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 dwReserved;// 4 bytes
// Can't fill in rest of the structure because if it doesn't line up, c# will complain.
} // end of struct _DnsRecord
}// end of class CDNS
/// <summary>
/// Structure for the DNS_WRAPPER
/// </summary>
public struct DNS_WRAPPER
{
public UInt16 dnsType;
public object dnsData;
}// end of DNS_IP6_ADDRESS
/// <summary>
/// Structure for the DNS_IP6_ADDRESS
/// </summary>
public struct DNS_IP6_ADDRESS
{
public UInt16 IP6Word1;
public UInt16 IP6Word2;
public UInt16 IP6Word3;
public UInt16 IP6Word4;
public UInt16 IP6Word5;
public UInt16 IP6Word6;
public UInt16 IP6Word7;
public UInt16 IP6Word8;
}// end of DNS_IP6_ADDRESS
[StructLayout(LayoutKind.Sequential)]
public struct DNS_A_DATA
{
public UInt32 IpAddress;
}// end of DNS_A_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SOA_DATA
{
public string pNamePrimaryServer;
public string pNameAdministrator;
public UInt32 dwSerialNo;
public UInt32 dwRefresh;
public UInt32 dwRetry;
public UInt32 dwExpire;
public UInt32 dwDefaultTtl;
}// end of DNS_SOA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_PTR_DATA
{
public string pNameHost;
}// end of DNS_PTR_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MINFO_DATA
{
public string pNameMailbox;
public string pNameErrorsMailbox;
}// end of DNS_MINFO_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MX_DATA
{
public string pNameExchange;
public UInt16 wPreference;
public UInt16 wPad; // to keep dword aligned
}// end of DNS_MX_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TXT_DATA
{
public UInt32 dwStringCount;
public string pStringArray;
}// end of DNS_TXT_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NULL_DATA
{
public UInt32 dwByteCount;
public string pData;
}// end of DNS_NULL_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WKS_DATA
{
public UInt32 IpAddress;
public char chProtocol;
public byte BitMask;
}// end of DNS_WKS_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_AAAA_DATA
{
public DNS_IP6_ADDRESS Ip6Address;
}// end of DNS_AAAA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_KEY_DATA
{
public UInt16 wFlags;
public byte chProtocol;
public byte chAlgorithm;
public byte Key;
}// end of DNS_KEY_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_LOC_DATA
{
public UInt16 wVersion;
public UInt16 wSize;
public UInt16 wHorPrec;
public UInt32 dwLatitude;
public UInt32 dwLongitude;
public UInt32 dwAltitude;
}// end of DNS_LOC_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SIG_DATA
{
public string pNameSigner;
public UInt16 wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public UInt32 dwOriginalTtl;
public UInt32 dwExpiration;
public UInt32 dwTimeSigned;
public UInt16 wKeyTag;
public UInt16 Pad; // keep byte field aligned
public byte Signature;
}// end of DNS_SIG_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_ATMA_DATA
{
public byte AddressType;
public string Address;
}// end of DNS_ATMA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NXT_DATA
{
public string pNameNext;
public byte bTypeBitMap;
}// end of DNS_NXT_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SRV_DATA
{
public string pNameNext;
public UInt16 wPriority;
public UInt16 wWeight;
public UInt16 wPort;
public UInt16 Pad;
}// end of DNS_SRV_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TKEY_DATA
{
public string pNameAlgorithm;
public IntPtr pAlgorithmPacket;
public IntPtr pKey;
public IntPtr pOtherData;
public UInt32 dwCreateTime;
public UInt32 ExpireTime;
public UInt16 wMode;
public UInt16 wError;
public UInt16 wKeyLength;
public UInt16 wOtherLength;
public char cAlgNameLength;
public bool bPacketPointers;
}// end of DNS_TKEY_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TSIG_DATA
{
public string pNameAlgorithm;
public IntPtr pAlgorithmPacket;
public IntPtr pKey;
public IntPtr pOtherData;
public UInt64 i64CreateTime;
public UInt16 wFudgeTime;
public UInt16 wOriginalXid;
public UInt16 wError;
public UInt16 wKeyLength;
public UInt16 wOtherLength;
public char cAlgNameLength;
public bool bPacketPointers;
}// end of DNS_TSIG_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINS_DATA
{
public UInt32 dwMappingFlag;
public UInt32 dwLookupTimeout;
public UInt32 dwCacheTimeout;
public UInt32 cWinsServerCount;
public UInt32 WinsServers;
}// end of DNS_WINS_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINSR_DATA
{
public UInt32 dwMappingFlag;
public UInt32 dwLookupTimeout;
public UInt32 dwCacheTimeout;
public string pNameResultDomain;
}// end of DNS_WINSR_DATA
public struct DNSTypes
{
/// <summary>
/// Various DNS record types
/// </summary>
public const UInt16 DNS_TYPE_A = 0x0001; // 1
public const UInt16 DNS_TYPE_NS = 0x0002; // 2
public const UInt16 DNS_TYPE_MD = 0x0003; // 3
public const UInt16 DNS_TYPE_MF = 0x0004; // 4
public const UInt16 DNS_TYPE_CNAME = 0x0005; // 5
public const UInt16 DNS_TYPE_SOA = 0x0006; // 6
public const UInt16 DNS_TYPE_MB = 0x0007; // 7
public const UInt16 DNS_TYPE_MG = 0x0008; // 8
public const UInt16 DNS_TYPE_MR = 0x0009; // 9
public const UInt16 DNS_TYPE_NULL = 0x000a; // 10
public const UInt16 DNS_TYPE_WKS = 0x000b; // 11
public const UInt16 DNS_TYPE_PTR = 0x000c; // 12
public const UInt16 DNS_TYPE_HINFO = 0x000d; // 13
public const UInt16 DNS_TYPE_MINFO = 0x000e; // 14
public const UInt16 DNS_TYPE_MX = 0x000f; // 15
public const UInt16 DNS_TYPE_TEXT = 0x0010; // 16
// RFC 1183
public const UInt16 DNS_TYPE_RP = 0x0011; // 17
public const UInt16 DNS_TYPE_AFSDB = 0x0012; // 18
public const UInt16 DNS_TYPE_X25 = 0x0013; // 19
public const UInt16 DNS_TYPE_ISDN = 0x0014; // 20
public const UInt16 DNS_TYPE_RT = 0x0015; // 21
// RFC 1348
public const UInt16 DNS_TYPE_NSAP = 0x0016; // 22
public const UInt16 DNS_TYPE_NSAPPTR = 0x0017; // 23
// RFC 2065 (DNS security)
public const UInt16 DNS_TYPE_SIG = 0x0018; // 24
public const UInt16 DNS_TYPE_KEY = 0x0019; // 25
// RFC 1664 (X.400 mail)
public const UInt16 DNS_TYPE_PX = 0x001a; // 26
// RFC 1712 (Geographic position)
public const UInt16 DNS_TYPE_GPOS = 0x001b; // 27
// RFC 1886 (IPv6 Address)
public const UInt16 DNS_TYPE_AAAA = 0x001c; // 28
// RFC 1876 (Geographic location)
public const UInt16 DNS_TYPE_LOC = 0x001d; // 29
// RFC 2065 (Secure negative response)
public const UInt16 DNS_TYPE_NXT = 0x001e; // 30
// RFC 2052 (Service location)
public const UInt16 DNS_TYPE_SRV = 0x0021; // 33
// ATM Standard something-or-another
public const UInt16 DNS_TYPE_ATMA = 0x0022; // 34
//
// Query only types (1035, 1995)
//
public const UInt16 DNS_TYPE_TKEY = 0x00f9; // 249
public const UInt16 DNS_TYPE_TSIG = 0x00fa; // 250
public const UInt16 DNS_TYPE_IXFR = 0x00fb; // 251
public const UInt16 DNS_TYPE_AXFR = 0x00fc; // 252
public const UInt16 DNS_TYPE_MAILB = 0x00fd; // 253
public const UInt16 DNS_TYPE_MAILA = 0x00fe; // 254
public const UInt16 DNS_TYPE_ALL = 0x00ff; // 255
public const UInt16 DNS_TYPE_ANY = 0x00ff; // 255
//
// Temp Microsoft types -- use until get IANA approval for real type
//
public const UInt16 DNS_TYPE_WINS = 0xff01; // 64K - 255
public const UInt16 DNS_TYPE_WINSR = 0xff02; // 64K - 254
public const UInt16 DNS_TYPE_NBSTAT = DNS_TYPE_WINSR;
/// <summary>
/// Various DNS query types
/// </summary>
public const UInt32 DNS_QUERY_STANDARD = 0x00000000;
public const UInt32 DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 0x00000001;
public const UInt32 DNS_QUERY_USE_TCP_ONLY = 0x00000002;
public const UInt32 DNS_QUERY_NO_RECURSION = 0x00000004;
public const UInt32 DNS_QUERY_BYPASS_CACHE = 0x00000008;
public const UInt32 DNS_QUERY_CACHE_ONLY = 0x00000010;
public const UInt32 DNS_QUERY_SOCKET_KEEPALIVE = 0x00000100;
public const UInt32 DNS_QUERY_TREAT_AS_FQDN = 0x00001000;
public const UInt32 DNS_QUERY_ALLOW_EMPTY_AUTH_RESP = 0x00010000;
public const UInt32 DNS_QUERY_DONT_RESET_TTL_VALUES = 0x00100000;
public const UInt32 DNS_QUERY_RESERVED = 0xff000000;
}
} // end namespace更多精彩文章及讨论,请光临枫下论坛 rolia.net
using System.Runtime.InteropServices;
using System.Collections;
namespace MyCommon
{
public class DNS
{
/// <summary>
/// Dll Entrypoint for DNS Services
/// </summary>
[DllImport("dnsapi.dll",EntryPoint="DnsQuery_A")]
private static extern UInt32 DnsQuery( [MarshalAs(UnmanagedType.LPStr)]string lpstrName, [MarshalAs(UnmanagedType.U2)]UInt16 wType, [MarshalAs(UnmanagedType.U4)]UInt32 fOptions, [MarshalAs(UnmanagedType.LPStr)]string DNSServer, ref UInt32 prr, [MarshalAs(UnmanagedType.U4)]UInt32 t3 );
private string _DNSServer = null;
public string DNSServer
{
get
{
return _DNSServer;
}
set
{
_DNSServer = value;
}
}
public DNS(){}
public DNS(string DNSServer)
{
_DNSServer = DNSServer;
}
public IEnumerator LookupAll( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_ALL, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator LookupMX( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_MX, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator LookupA( String strDomain )
{
return( Lookup( ref strDomain, DNSTypes.DNS_TYPE_A, DNSTypes.DNS_QUERY_STANDARD | DNSTypes.DNS_QUERY_TREAT_AS_FQDN | DNSTypes.DNS_QUERY_BYPASS_CACHE ) );
}
public IEnumerator Lookup( String strDomain, UInt16 dnstype, UInt32 querytype )
{
return( Lookup( ref strDomain, dnstype, querytype ) );
}
/// <summary>
/// Used to verify a domain for any type of record
/// </summary>
/// <param name="strDomain"></param>
/// <returns></returns>
private IEnumerator Lookup( ref string strDomain, UInt16 dnstype, UInt32 querytype )
{
DNS_RECORD dnsrec;
DNS_WRAPPER wrapper;
UInt32 ppQueryResultsSet = 0;
object Data = new object();
ArrayList list = new ArrayList();
try
{
uint temp = DnsQuery( strDomain, dnstype, querytype, _DNSServer, ref ppQueryResultsSet, (UInt32)0 );
if( DnsQuery( strDomain, dnstype, querytype, _DNSServer, ref ppQueryResultsSet, (UInt32)0 ) == 0 )
{
IntPtr ppr = new IntPtr( ppQueryResultsSet );
// Parse the records.
// Call function to loop through linked list and fill an array of records
do
{
// Get the DNS_RECORD
dnsrec = (DNS_RECORD)Marshal.PtrToStructure( ppr, typeof(DNS_RECORD) );
// Get the Data part
GetData( ppr.ToInt32(), ref dnsrec, ref Data );
// Wrap data in a struct with the type and data
wrapper = new DNS_WRAPPER();
wrapper.dnsType = dnsrec.wType;
wrapper.dnsData = Data;
// Add wrapper to array
list.Add( wrapper );
ppr = dnsrec.pNext;
} while ( ppr.ToInt32() != 0 );
}
}
catch
{
}
return( list.GetEnumerator() );
} // end of VerifyDomain
private void GetData( Int32 ptr, ref DNS_RECORD dnsrec, ref object Data )
{
ptr += Marshal.SizeOf( dnsrec );// Skip over the header portion of the DNS_RECORD to the data portion.
//ptr += 24;
switch ( dnsrec.wType )
{
case DNSTypes.DNS_TYPE_A:
Data = (DNS_A_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_A_DATA) );
break;
case DNSTypes.DNS_TYPE_CNAME:
case DNSTypes.DNS_TYPE_MB:
case DNSTypes.DNS_TYPE_MD:
case DNSTypes.DNS_TYPE_MF:
case DNSTypes.DNS_TYPE_MG:
case DNSTypes.DNS_TYPE_MR:
case DNSTypes.DNS_TYPE_NS:
case DNSTypes.DNS_TYPE_PTR:
Data = (DNS_PTR_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_PTR_DATA) );
break;
case DNSTypes.DNS_TYPE_HINFO:
case DNSTypes.DNS_TYPE_ISDN:
case DNSTypes.DNS_TYPE_X25:
Data = (DNS_TXT_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TXT_DATA) );
break;
case DNSTypes.DNS_TYPE_MINFO:
case DNSTypes.DNS_TYPE_RP:
Data = (DNS_MINFO_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_MINFO_DATA) );
break;
case DNSTypes.DNS_TYPE_MX:
case DNSTypes.DNS_TYPE_AFSDB:
case DNSTypes.DNS_TYPE_RT:
Data = (DNS_MX_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_MX_DATA) );
break;
case DNSTypes.DNS_TYPE_NULL:
Data = (DNS_NULL_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_NULL_DATA) );
break;
case DNSTypes.DNS_TYPE_SOA:
Data = (DNS_SOA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_SOA_DATA) );
break;
case DNSTypes.DNS_TYPE_WKS:
Data = (DNS_WKS_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WKS_DATA) );
break;
case DNSTypes.DNS_TYPE_AAAA:
Data = (DNS_AAAA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_AAAA_DATA) );
break;
case DNSTypes.DNS_TYPE_ATMA:
Data = (DNS_ATMA_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_ATMA_DATA) );
break;
case DNSTypes.DNS_TYPE_NBSTAT:
//case DNS_TYPE_WINSR:
Data = (DNS_WINSR_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WINSR_DATA) );
break;
case DNSTypes.DNS_TYPE_SRV:
Data = (DNS_SRV_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_SRV_DATA) );
break;
case DNSTypes.DNS_TYPE_TKEY:
Data = (DNS_TKEY_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TKEY_DATA) );
break;
case DNSTypes.DNS_TYPE_TSIG:
Data = (DNS_TSIG_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_TSIG_DATA) );
break;
case DNSTypes.DNS_TYPE_WINS:
Data = (DNS_WINS_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_WINS_DATA) );
break;
case DNSTypes.DNS_TYPE_LOC:
Data = (DNS_LOC_DATA)Marshal.PtrToStructure( new IntPtr(ptr), typeof(DNS_LOC_DATA) );
break;
case DNSTypes.DNS_TYPE_AXFR:
case DNSTypes.DNS_TYPE_GPOS:
case DNSTypes.DNS_TYPE_IXFR:
case DNSTypes.DNS_TYPE_KEY:
case DNSTypes.DNS_TYPE_MAILA:
case DNSTypes.DNS_TYPE_MAILB:
case DNSTypes.DNS_TYPE_NSAP:
case DNSTypes.DNS_TYPE_NSAPPTR:
case DNSTypes.DNS_TYPE_NXT:
case DNSTypes.DNS_TYPE_PX:
case DNSTypes.DNS_TYPE_SIG:
case DNSTypes.DNS_TYPE_TEXT:
default:
Data = null;
break;
}// end of switch for each dns record type
}// end of GetDataType
/// <summary>
/// Structure for the actual DNS Record
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct DNS_RECORD
{
// public IntPtr pNext;
public IntPtr pNext;// 4 bytes
//[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string pName;// 4 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U2)] public UInt16 wType;// 2 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U2)] public UInt16 wDataLength;// 2 bytes
[ StructLayout( LayoutKind.Explicit )]// 4 bytes
public struct DNS_RECORD_FLAGS
{
[ FieldOffset( 0 )]
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 DW;
[ FieldOffset( 0 )]
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 S;
}// end of union for flags
public DNS_RECORD_FLAGS flags;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 dwTtl;// 4 bytes
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 dwReserved;// 4 bytes
// Can't fill in rest of the structure because if it doesn't line up, c# will complain.
} // end of struct _DnsRecord
}// end of class CDNS
/// <summary>
/// Structure for the DNS_WRAPPER
/// </summary>
public struct DNS_WRAPPER
{
public UInt16 dnsType;
public object dnsData;
}// end of DNS_IP6_ADDRESS
/// <summary>
/// Structure for the DNS_IP6_ADDRESS
/// </summary>
public struct DNS_IP6_ADDRESS
{
public UInt16 IP6Word1;
public UInt16 IP6Word2;
public UInt16 IP6Word3;
public UInt16 IP6Word4;
public UInt16 IP6Word5;
public UInt16 IP6Word6;
public UInt16 IP6Word7;
public UInt16 IP6Word8;
}// end of DNS_IP6_ADDRESS
[StructLayout(LayoutKind.Sequential)]
public struct DNS_A_DATA
{
public UInt32 IpAddress;
}// end of DNS_A_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SOA_DATA
{
public string pNamePrimaryServer;
public string pNameAdministrator;
public UInt32 dwSerialNo;
public UInt32 dwRefresh;
public UInt32 dwRetry;
public UInt32 dwExpire;
public UInt32 dwDefaultTtl;
}// end of DNS_SOA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_PTR_DATA
{
public string pNameHost;
}// end of DNS_PTR_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MINFO_DATA
{
public string pNameMailbox;
public string pNameErrorsMailbox;
}// end of DNS_MINFO_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MX_DATA
{
public string pNameExchange;
public UInt16 wPreference;
public UInt16 wPad; // to keep dword aligned
}// end of DNS_MX_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TXT_DATA
{
public UInt32 dwStringCount;
public string pStringArray;
}// end of DNS_TXT_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NULL_DATA
{
public UInt32 dwByteCount;
public string pData;
}// end of DNS_NULL_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WKS_DATA
{
public UInt32 IpAddress;
public char chProtocol;
public byte BitMask;
}// end of DNS_WKS_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_AAAA_DATA
{
public DNS_IP6_ADDRESS Ip6Address;
}// end of DNS_AAAA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_KEY_DATA
{
public UInt16 wFlags;
public byte chProtocol;
public byte chAlgorithm;
public byte Key;
}// end of DNS_KEY_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_LOC_DATA
{
public UInt16 wVersion;
public UInt16 wSize;
public UInt16 wHorPrec;
public UInt32 dwLatitude;
public UInt32 dwLongitude;
public UInt32 dwAltitude;
}// end of DNS_LOC_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SIG_DATA
{
public string pNameSigner;
public UInt16 wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public UInt32 dwOriginalTtl;
public UInt32 dwExpiration;
public UInt32 dwTimeSigned;
public UInt16 wKeyTag;
public UInt16 Pad; // keep byte field aligned
public byte Signature;
}// end of DNS_SIG_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_ATMA_DATA
{
public byte AddressType;
public string Address;
}// end of DNS_ATMA_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NXT_DATA
{
public string pNameNext;
public byte bTypeBitMap;
}// end of DNS_NXT_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SRV_DATA
{
public string pNameNext;
public UInt16 wPriority;
public UInt16 wWeight;
public UInt16 wPort;
public UInt16 Pad;
}// end of DNS_SRV_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TKEY_DATA
{
public string pNameAlgorithm;
public IntPtr pAlgorithmPacket;
public IntPtr pKey;
public IntPtr pOtherData;
public UInt32 dwCreateTime;
public UInt32 ExpireTime;
public UInt16 wMode;
public UInt16 wError;
public UInt16 wKeyLength;
public UInt16 wOtherLength;
public char cAlgNameLength;
public bool bPacketPointers;
}// end of DNS_TKEY_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TSIG_DATA
{
public string pNameAlgorithm;
public IntPtr pAlgorithmPacket;
public IntPtr pKey;
public IntPtr pOtherData;
public UInt64 i64CreateTime;
public UInt16 wFudgeTime;
public UInt16 wOriginalXid;
public UInt16 wError;
public UInt16 wKeyLength;
public UInt16 wOtherLength;
public char cAlgNameLength;
public bool bPacketPointers;
}// end of DNS_TSIG_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINS_DATA
{
public UInt32 dwMappingFlag;
public UInt32 dwLookupTimeout;
public UInt32 dwCacheTimeout;
public UInt32 cWinsServerCount;
public UInt32 WinsServers;
}// end of DNS_WINS_DATA
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINSR_DATA
{
public UInt32 dwMappingFlag;
public UInt32 dwLookupTimeout;
public UInt32 dwCacheTimeout;
public string pNameResultDomain;
}// end of DNS_WINSR_DATA
public struct DNSTypes
{
/// <summary>
/// Various DNS record types
/// </summary>
public const UInt16 DNS_TYPE_A = 0x0001; // 1
public const UInt16 DNS_TYPE_NS = 0x0002; // 2
public const UInt16 DNS_TYPE_MD = 0x0003; // 3
public const UInt16 DNS_TYPE_MF = 0x0004; // 4
public const UInt16 DNS_TYPE_CNAME = 0x0005; // 5
public const UInt16 DNS_TYPE_SOA = 0x0006; // 6
public const UInt16 DNS_TYPE_MB = 0x0007; // 7
public const UInt16 DNS_TYPE_MG = 0x0008; // 8
public const UInt16 DNS_TYPE_MR = 0x0009; // 9
public const UInt16 DNS_TYPE_NULL = 0x000a; // 10
public const UInt16 DNS_TYPE_WKS = 0x000b; // 11
public const UInt16 DNS_TYPE_PTR = 0x000c; // 12
public const UInt16 DNS_TYPE_HINFO = 0x000d; // 13
public const UInt16 DNS_TYPE_MINFO = 0x000e; // 14
public const UInt16 DNS_TYPE_MX = 0x000f; // 15
public const UInt16 DNS_TYPE_TEXT = 0x0010; // 16
// RFC 1183
public const UInt16 DNS_TYPE_RP = 0x0011; // 17
public const UInt16 DNS_TYPE_AFSDB = 0x0012; // 18
public const UInt16 DNS_TYPE_X25 = 0x0013; // 19
public const UInt16 DNS_TYPE_ISDN = 0x0014; // 20
public const UInt16 DNS_TYPE_RT = 0x0015; // 21
// RFC 1348
public const UInt16 DNS_TYPE_NSAP = 0x0016; // 22
public const UInt16 DNS_TYPE_NSAPPTR = 0x0017; // 23
// RFC 2065 (DNS security)
public const UInt16 DNS_TYPE_SIG = 0x0018; // 24
public const UInt16 DNS_TYPE_KEY = 0x0019; // 25
// RFC 1664 (X.400 mail)
public const UInt16 DNS_TYPE_PX = 0x001a; // 26
// RFC 1712 (Geographic position)
public const UInt16 DNS_TYPE_GPOS = 0x001b; // 27
// RFC 1886 (IPv6 Address)
public const UInt16 DNS_TYPE_AAAA = 0x001c; // 28
// RFC 1876 (Geographic location)
public const UInt16 DNS_TYPE_LOC = 0x001d; // 29
// RFC 2065 (Secure negative response)
public const UInt16 DNS_TYPE_NXT = 0x001e; // 30
// RFC 2052 (Service location)
public const UInt16 DNS_TYPE_SRV = 0x0021; // 33
// ATM Standard something-or-another
public const UInt16 DNS_TYPE_ATMA = 0x0022; // 34
//
// Query only types (1035, 1995)
//
public const UInt16 DNS_TYPE_TKEY = 0x00f9; // 249
public const UInt16 DNS_TYPE_TSIG = 0x00fa; // 250
public const UInt16 DNS_TYPE_IXFR = 0x00fb; // 251
public const UInt16 DNS_TYPE_AXFR = 0x00fc; // 252
public const UInt16 DNS_TYPE_MAILB = 0x00fd; // 253
public const UInt16 DNS_TYPE_MAILA = 0x00fe; // 254
public const UInt16 DNS_TYPE_ALL = 0x00ff; // 255
public const UInt16 DNS_TYPE_ANY = 0x00ff; // 255
//
// Temp Microsoft types -- use until get IANA approval for real type
//
public const UInt16 DNS_TYPE_WINS = 0xff01; // 64K - 255
public const UInt16 DNS_TYPE_WINSR = 0xff02; // 64K - 254
public const UInt16 DNS_TYPE_NBSTAT = DNS_TYPE_WINSR;
/// <summary>
/// Various DNS query types
/// </summary>
public const UInt32 DNS_QUERY_STANDARD = 0x00000000;
public const UInt32 DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 0x00000001;
public const UInt32 DNS_QUERY_USE_TCP_ONLY = 0x00000002;
public const UInt32 DNS_QUERY_NO_RECURSION = 0x00000004;
public const UInt32 DNS_QUERY_BYPASS_CACHE = 0x00000008;
public const UInt32 DNS_QUERY_CACHE_ONLY = 0x00000010;
public const UInt32 DNS_QUERY_SOCKET_KEEPALIVE = 0x00000100;
public const UInt32 DNS_QUERY_TREAT_AS_FQDN = 0x00001000;
public const UInt32 DNS_QUERY_ALLOW_EMPTY_AUTH_RESP = 0x00010000;
public const UInt32 DNS_QUERY_DONT_RESET_TTL_VALUES = 0x00100000;
public const UInt32 DNS_QUERY_RESERVED = 0xff000000;
}
} // end namespace更多精彩文章及讨论,请光临枫下论坛 rolia.net