private static extern int DnsQuery( [MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName, DnsRecordTypes wType, DnsQueryOptions options, ref IP4_ARRAY dnsServerIpArray, ref IntPtr ppQueryResults, int pReserved);
public static extern int DnsQuery( string name, ushort type, uint options, ref IP4_ARRAY aipServers, //TODO: Ref? ref DNS_QUERY_RESULT queryResults, ref IntPtr reserved );
//--------------------------------------------------------------------------------- // Resolving TXT records only (for now) //--------------------------------------------------------------------------------- public static string GetTXTRecord(string domain, string serverIP = null) { IntPtr recordsArray = IntPtr.Zero; IntPtr dnsRecord = IntPtr.Zero; TXTRecord txtRecord; IP4_ARRAY dnsServerArray = new IP4_ARRAY(); if (serverIP != null) { uint address = BitConverter.ToUInt32(IPAddress.Parse(serverIP).GetAddressBytes(), 0); uint[] ipArray = new uint[1]; ipArray.SetValue(address, 0); dnsServerArray.AddrCount = 1; dnsServerArray.AddrArray = new uint[1]; dnsServerArray.AddrArray[0] = address; } // Interop calls will only work on Windows platform (no mono c#) if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new NotSupportedException(); } ArrayList recordList = new ArrayList(); try { int queryResult = DnsResolver.DnsQuery(ref domain, DnsRecordTypes.DNS_TYPE_TXT, DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref recordsArray, 0); // Check for error if (queryResult != 0) { throw new Win32Exception(queryResult); } // Loop through the result record list for (dnsRecord = recordsArray; !dnsRecord.Equals(IntPtr.Zero); dnsRecord = txtRecord.pNext) { txtRecord = (TXTRecord)Marshal.PtrToStructure(dnsRecord, typeof(TXTRecord)); if (txtRecord.wType == (int)DnsRecordTypes.DNS_TYPE_TXT) { //Console.WriteLine("Size of array: {0}",txtRecord.dwStringCount); string txt = Marshal.PtrToStringAuto(txtRecord.pStringArray); recordList.Add(txt); } } } finally { DnsResolver.DnsRecordListFree(recordsArray, 0); } // Return only the first TXT answer return((string)recordList[0]); }
// Resolving TXT records only for now public static IEnumerable <string> GetTxtRecords(string domain, string serverIp = null) { IntPtr recordsArray = IntPtr.Zero; IP4_ARRAY dnsServerArray = new IP4_ARRAY(); if (serverIp != null) { uint address = BitConverter.ToUInt32(IPAddress.Parse(serverIp).GetAddressBytes(), 0); uint[] ipArray = new uint[1]; ipArray.SetValue(address, 0); dnsServerArray.AddressCount = 1; dnsServerArray.AddressArray = new uint[1]; dnsServerArray.AddressArray[0] = address; } // Interop calls will only work on Windows platform (no mono c#) if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new NotSupportedException(); } ArrayList recordList = new ArrayList(); try { int queryResult = DnsQuery(ref domain, DnsRecordTypes.DNS_TYPE_TXT, DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref recordsArray, 0); // Check for error if (queryResult != 0) { throw new Win32Exception(queryResult); } // Loop through the result record list IntPtr dnsRecord; TxtRecord txtRecord; for (dnsRecord = recordsArray; !dnsRecord.Equals(IntPtr.Zero); dnsRecord = txtRecord.pNext) { txtRecord = (TxtRecord)Marshal.PtrToStructure(dnsRecord, typeof(TxtRecord)); if (txtRecord.wType != (int)DnsRecordTypes.DNS_TYPE_TXT) { continue; } string txt = Marshal.PtrToStringAuto(txtRecord.pStringArray); recordList.Add(txt); } } finally { DnsRecordListFree(recordsArray, 0); } return((string[])recordList.ToArray(typeof(string))); }
public List <string> QueryA(string domain) { IntPtr recordsArray = IntPtr.Zero; try { int result = 0; if (DnsServerIp.Length == 0) { result = DnsQuery(ref domain, DnsRecordTypes.DNS_TYPE_A, DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, IntPtr.Zero, ref recordsArray, IntPtr.Zero); } else { uint address = BitConverter.ToUInt32(IPAddress.Parse(DnsServerIp).GetAddressBytes(), 0); uint[] ipArray = new uint[1]; ipArray.SetValue(address, 0); IP4_ARRAY dnsServerArray = new IP4_ARRAY(); dnsServerArray.AddrCount = 1; dnsServerArray.AddrArray = new uint[1]; dnsServerArray.AddrArray[0] = address; result = DnsQueryWithServerIp(ref domain, DnsRecordTypes.DNS_TYPE_A, DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref recordsArray, IntPtr.Zero); } if (result != 0) { return(null); } DNS_RECORD record; List <string> recordList = new List <string>(); for (IntPtr recordPtr = recordsArray; !recordPtr.Equals(IntPtr.Zero); recordPtr = record.pNext) { record = (DNS_RECORD)Marshal.PtrToStructure(recordPtr, typeof(DNS_RECORD)); if (record.wType == (int)DnsRecordTypes.DNS_TYPE_A) { recordList.Add(IntToIp(record.Data.A.IpAddress)); //Console.WriteLine(IntToIp(record.Data.A.IpAddress)); } } return(recordList); } finally { if (recordsArray != IntPtr.Zero) { DnsRecordListFree(recordsArray, DNS_FREE_TYPE.DnsFreeFlat); } } }
public static string[] GetARecords(string domain, string NSIP) { // DNSIntercept.Main.LogMessage("GetARecords: start. Domain: '" + domain + "', NS: '" + NSIP + "'."); IntPtr ptr1 = IntPtr.Zero; IntPtr ptr2 = IntPtr.Zero; ARecord recA; if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new NotSupportedException(); } ArrayList list1 = new ArrayList(); uint address = BitConverter.ToUInt32(IPAddress.Parse(NSIP).GetAddressBytes(), 0); // This is OK: DNSIntercept.Main.LogMessage("GetARecords: NS: " + NSIP + "=" + address.ToString()); uint[] ipArray = new uint[1]; ipArray.SetValue(address, 0); IP4_ARRAY dnsServerArray = new IP4_ARRAY(); dnsServerArray.AddrCount = 1; dnsServerArray.AddrArray = new uint[1]; dnsServerArray.AddrArray[0] = address; int num1 = DnsA.DnsQuery(ref domain, QueryTypes.DNS_TYPE_A, QueryOptions.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref ptr1, 0); if (num1 != 0) { // OK, this will throw errors like DNS host not found, // for an NXDomain response from DNS server // (which we trap anyhow, using a match in the error string). throw new Win32Exception(num1); } long lipadd = 0; int cnt = 1; for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recA.pNext) { recA = (ARecord)Marshal.PtrToStructure(ptr2, typeof(ARecord)); if (recA.wType == 1) { if (cnt == 1) { lipadd = recA.wIPAddress; if (lipadd == 0) { // DNSIntercept.Main.LogMessage("GetARecords: DNSQuery for .altnet domain returned 0.0.0.0."); } string text1 = long2ip(lipadd); list1.Add(text1); // DNSIntercept.Main.LogMessage("GetARecords: Adding: " + text1); } } cnt = cnt + 1; } DnsA.DnsRecordListFree(ptr2, 0); return((string[])list1.ToArray(typeof(string))); }
public static extern int DnsQueryWithServerIp([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordTypes wType, DnsQueryOptions Options, ref IP4_ARRAY dnsServerIpArray, ref IntPtr ppQueryResultsSet, IntPtr pReserved);