[DllImport("dnsapi.dll",EntryPoint="DnsQuery_A")]
internal static extern UInt32 DnsQuery( [MarshalAs(UnmanagedType.LPStr)]string lpstrName, [MarshalAs(UnmanagedType.U2)]UInt16 wType, [MarshalAs(UnmanagedType.U4)]UInt32 fOptions,
[MarshalAs(UnmanagedType.U4)]UInt32 t1, ref UInt32 prr, [MarshalAs(UnmanagedType.U4)]UInt32 t3 );
private class DnsInfo
{
public DnsInfo()
{
IPAddresses = new List<string>();
}
public string DomainName { get; set; }
public uint TTL { get; set; }
public List<string> IPAddresses { get; set; }
}
private UInt32 GetIP(Int32 ptr)
{
return ((NativeMethods.DNS_A_DATA)Marshal.PtrToStructure(new IntPtr(ptr), typeof(NativeMethods.DNS_A_DATA))).IpAddress;
}
private DnsInfo GetDNSInfo(ref string strDomain)
{
var result = new DnsInfo();
const int DNS_QUERY_CACHE_ONLY = 0x00000010;
UInt32 ppQueryResultsSet = 0;
if (NativeMethods.DnsQuery(strDomain, DNS_TYPE_A, DNS_QUERY_CACHE_ONLY, 0, ref ppQueryResultsSet, 0) == 0)
{
IntPtr ppr = new IntPtr(ppQueryResultsSet);
NativeMethods.DNS_RECORD dnsrec = (NativeMethods.DNS_RECORD)Marshal.PtrToStructure(ppr, typeof(NativeMethods.DNS_RECORD));
NativeMethods.NetApiBufferFree(ppr);
result.DomainName = strDomain;
result.TTL = dnsrec.dwTtl;
var ip = new IPAddress(GetIP(ppr.ToInt32() + Marshal.SizeOf(dnsrec))).ToString();
result.IPAddresses.Add(ip);
var current = dnsrec.pNext;
while (current != IntPtr.Zero)
{
dnsrec = (NativeMethods.DNS_RECORD)Marshal.PtrToStructure(current, typeof(NativeMethods.DNS_RECORD));
// Skip over the header portion of the DNS_RECORD to the data portion.
ip = new IPAddress(GetIP(current.ToInt32() + Marshal.SizeOf(dnsrec))).ToString();
result.IPAddresses.Add(ip);
var old = current;
current = dnsrec.pNext;
NativeMethods.NetApiBufferFree(old);
}
return result;
}
return null;
}
Комментариев нет:
Отправить комментарий