// ===================== static string LookupIPAddressUsingAltNetNS(string nodename, string AltNetNameServerIP) { string ip_string = "0.0.0.0"; try { // LogMessage("LookupIPAddressUsingAltNetNS: start: looking up: " + nodename + ", using NS at " + AltNetNameServerIP + "."); string[] s = DnsA.GetARecords(nodename, AltNetNameServerIP); int listlen = s.Length; // LogMessage("LookupIPAddressUsingAltNetNS. Entries found: " + listlen.ToString()); foreach (string st in s) { ip_string = st; break; //Console.ReadLine(); } if (ip_string.IndexOf(":") >= 0) { return("Error in LookupIPAddressUsingAltNetNS(): We can only handle IPv4 hosts. IPv6 address returned for " + nodename + "."); } LogMessage("LookupIPAddressUsingAltNetNS, success: IP: '" + ip_string + "'."); return(ip_string); } catch (Exception ex) { // LogMessage("LookupIPAddressUsingAltNetNS, error: IP: " + ip_string + "."); return("Error in LookupIPAddressUsingAltNetNS(): " + ex.Message); } }
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))); }