Ejemplo n.º 1
0
        internal IEnumerable <ManagedDnsCacheEntry> GetDnsCacheEntries()
        {
            var ret   = new List <ManagedDnsCacheEntry>();
            var ptr   = IntPtr.Zero;
            var table = IntPtr.Zero;

            if (_dnsGetCacheDataTable.Invoke(out table))
            {
                ptr = table;
                do
                {
                    var entry      = (DnsCacheEntry)Marshal.PtrToStructure(ptr, typeof(DnsCacheEntry));
                    var domainName = Marshal.PtrToStringAuto(entry.pszName);

                    ret.AddRange(ExtractDnsARecords(domainName));
                    ret.AddRange(ExtractDnsAAAARecords(domainName));

                    var temp = ptr;
                    ptr = entry.pNext;

                    Win32Interop.DnsFree(entry.pszName, DnsFreeType.FreeFlat);
                    Win32Interop.DnsFree(temp, DnsFreeType.FreeFlat);
                }while (ptr != IntPtr.Zero);
            }
            return(ret);
        }
Ejemplo n.º 2
0
 public void Dispose()
 {
     if (_dnsapiLibHandle != IntPtr.Zero)
     {
         if (!Win32Interop.FreeLibrary(_dnsapiLibHandle))
         {
             throw new Win32Exception(Marshal.GetLastWin32Error());
         }
     }
 }
Ejemplo n.º 3
0
        internal DnsCacheHelper()
        {
            _dnsapiLibHandle = Win32Interop.LoadLibrary(@"dnsapi.dll");
            if (_dnsapiLibHandle == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var procAddress = Win32Interop.GetProcAddress(_dnsapiLibHandle, nameof(Win32Interop.DnsGetCacheDataTable));

            _dnsGetCacheDataTable = (Win32Interop.DnsGetCacheDataTable)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(Win32Interop.DnsGetCacheDataTable));
        }
Ejemplo n.º 4
0
        private IEnumerable <ManagedDnsCacheEntry> ExtractDnsARecords(string domain)
        {
            var resultPtr = IntPtr.Zero;
            var ret       = new List <ManagedDnsCacheEntry>();

            if (Win32Interop.DnsQuery(ref domain, DnsRecordType.A, (DnsQueryType)0x8010, IntPtr.Zero, ref resultPtr, IntPtr.Zero) == 0)
            {
                var recordIndexPtr = resultPtr;

                do
                {
                    var record = (DnsRecord)Marshal.PtrToStructure(recordIndexPtr, typeof(DnsRecord));
                    if (record.RecordType == DnsRecordType.A)
                    {
                        int size = Marshal.SizeOf(record);
                        recordIndexPtr += size;
                        var ipv4 = (DnsARecord)Marshal.PtrToStructure(recordIndexPtr, typeof(DnsARecord));

                        ret.Add(new ManagedDnsCacheEntry {
                            DomainName = domain, Address = new IPAddress(ipv4.IpAddress)
                        });
                        recordIndexPtr = record.Next;
                    }
                    else
                    {
                        recordIndexPtr = IntPtr.Zero;
                    }
                } while (recordIndexPtr != IntPtr.Zero);

                if (resultPtr != IntPtr.Zero)
                {
                    Win32Interop.DnsFree(resultPtr, DnsFreeType.FreeRecordList);
                }
            }

            return(ret);
        }