public static List <string> LookUp(string domain, DNSKind kind, string dnsServer = "8.8.8.8") { var entries = new List <DNSEntry>(); string qtype; switch (kind) { case DNSKind.A: qtype = "1"; break; case DNSKind.MX: qtype = "15"; break; default: throw new Exception("Invalid DNS type"); } UdpClient udpc = new UdpClient(dnsServer, 53); // send request to dns server var list = new List <byte>(); list.AddRange(new byte[] { 88, 89, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }); string[] tmp = domain.Split('.'); foreach (string s in tmp) { list.Add(Convert.ToByte(s.Length)); char[] chars = s.ToCharArray(); foreach (char c in chars) { list.Add(Convert.ToByte(Convert.ToInt32(c))); } } list.AddRange(new byte[] { 0, 0, Convert.ToByte(qtype), 0, 1 }); byte[] req = new byte[list.Count]; for (int i = 0; i < list.Count; i++) { req[i] = list[i]; } udpc.Send(req, req.Length); // obtain and process response IPEndPoint ep = null; byte[] recv = udpc.Receive(ref ep); udpc.Close(); int[] resp; resp = new int[recv.Length]; for (int i = 0; i < resp.Length; i++) { resp[i] = Convert.ToInt32(recv[i]); } int status = resp[3]; if (status != 128) { throw new Exception($"dns failure, error code: {status}"); } int answers = resp[7]; if (answers == 0) { throw new Exception("dsn failure, no results"); } int pos = domain.Length + 18; switch (qtype) { // MX record case "15": { while (answers > 0) { int preference = resp[pos + 13]; pos += 14; //offset string host = GetMXRecord(resp, pos, out pos); entries.Add(new DNSEntry(preference, host)); answers--; } break; } // A record case "1": { while (answers > 0) { pos += 11; // offset string str = GetARecord(resp, ref pos); entries.Add(new DNSEntry(0, str)); answers--; } break; } default: { throw new Exception($"dsn failure, unsupported dns type {qtype}"); } } return(entries.OrderBy(x => x.preference).Select(x => x.host).ToList()); }
public static List <string> LookUp(string domain, DNSKind kind, string dnsServer = "8.8.8.8") { var entries = new List <DNSEntry>(); string qtype; switch (kind) { case DNSKind.A: qtype = "1"; break; case DNSKind.MX: qtype = "15"; break; default: throw new Exception("Invalid DNS type"); } UdpClient udpc = new UdpClient(dnsServer, 53); // SEND REQUEST-------------------- List <byte> list = new List <byte>(); list.AddRange(new byte[] { 88, 89, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }); string[] tmp = domain.Split('.'); foreach (string s in tmp) { list.Add(Convert.ToByte(s.Length)); char[] chars = s.ToCharArray(); foreach (char c in chars) { list.Add(Convert.ToByte(Convert.ToInt32(c))); } } list.AddRange(new byte[] { 0, 0, Convert.ToByte(qtype), 0, 1 }); byte[] req = new byte[list.Count]; for (int i = 0; i < list.Count; i++) { req[i] = list[i]; } udpc.Send(req, req.Length); // RECEIVE RESPONSE-------------- IPEndPoint ep = null; byte[] recv = udpc.Receive(ref ep); udpc.Close(); int[] resp; resp = new int[recv.Length]; for (int i = 0; i < resp.Length; i++) { resp[i] = Convert.ToInt32(recv[i]); } int status = resp[3]; if (status != 128) { throw new Exception(string.Format("{0}", status)); } int answers = resp[7]; if (answers == 0) { throw new Exception("No results"); } int pos = domain.Length + 18; if (qtype == "15") // MX record { while (answers > 0) { int preference = resp[pos + 13]; pos += 14; //offset string host = GetMXRecord(resp, pos, out pos); entries.Add(new DNSEntry(preference, host)); answers--; } } else if (qtype == "1") // A record { while (answers > 0) { pos += 11; //offset string str = GetARecord(resp, ref pos); entries.Add(new DNSEntry(0, str)); answers--; } } return(entries.OrderBy(x => x.preference).Select(x => x.host).ToList()); }