コード例 #1
0
        private IPAddress[] GetDnsRecords(string hostName, IPAddress dnsServer)
        {
            IntPtr           ptr  = default(IntPtr);
            List <IPAddress> list = new List <IPAddress>();
            IntPtr           zero = IntPtr.Zero;
            IPv4Array        iPv4ArrayFromIPAddress = GetIPv4ArrayFromIPAddress(dnsServer);

            if (DnsQueryA(hostName, 1, 72, ref iPv4ArrayFromIPAddress, ref ptr, ref zero) == 0)
            {
                DnsRecordA dnsRecordA        = (DnsRecordA)Marshal.PtrToStructure(ptr, typeof(DnsRecordA));
                IPAddress  addressFromRecord = GetAddressFromRecord(dnsRecordA);
                if (addressFromRecord != IPAddress.None)
                {
                    list.Add(addressFromRecord);
                }
                while (!(dnsRecordA.NextRecord == IntPtr.Zero))
                {
                    dnsRecordA        = (DnsRecordA)Marshal.PtrToStructure(dnsRecordA.NextRecord, typeof(DnsRecordA));
                    addressFromRecord = GetAddressFromRecord(dnsRecordA);
                    if (addressFromRecord != IPAddress.None)
                    {
                        list.Add(addressFromRecord);
                    }
                }
            }
            return(list.ToArray());
        }
コード例 #2
0
 private IPAddress GetAddressFromRecord(DnsRecordA record)
 {
     if (record.Type != 1)
     {
         return(IPAddress.None);
     }
     if ((record.Flags & 3) >= 2)
     {
         return(IPAddress.None);
     }
     return(new IPAddress(record.Data));
 }