简体   繁体   English

从DNS获取IP

[英]Get IP from DNS

How can I create a stream of data to find the IP of a certain domain using TCP? 如何使用TCP创建数据流以查找某个域的IP? Searching around the forum and tried something like this: 在论坛上搜索并尝试了以下操作:

client = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);
client.Connect(ep);
String query = "host -T google.com";
byte[] myByte = System.Text.ASCIIEncoding.Default.GetBytes(query);
client.Send(myByte, myByte.Length);
var receivedData = client.Receive(ref ep);

I don't understand how to create the correct message for DNS server. 我不明白如何为DNS服务器创建正确的消息。 If something similar to the HTTP way exist, it would be great. 如果存在类似于HTTP的方式,那就太好了。

UPDATE: This is how you do it! 更新:这是您的操作方式!

    client = new UdpClient();
    IPEndPoint ep = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);
    client.Connect(ep);

    //dynamic dns
    String host1 = "vg.no";
    byte[] hostnameLength = new byte[1];
    byte[] hostdomainLength = new byte[1];


    byte[] tranactionID1 = { 0x46, 0x62 };
    byte[] queryType1 = { 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] hostname = System.Text.ASCIIEncoding.Default.GetBytes(host1.Split('.')[0]);
    hostnameLength[0] = (byte)hostname.Length;
    byte[] hostdomain = System.Text.ASCIIEncoding.Default.GetBytes(host1.Split('.')[1]);
    hostdomainLength[0] = (byte)hostdomain.Length;
    byte[] queryEnd = {0x00, 0x00, 0x01, 0x00, 0x01};
    byte[] dnsQueryString = tranactionID1.Concat(queryType1).Concat(hostnameLength).Concat(hostname).Concat(hostdomainLength).Concat(hostdomain).Concat(queryEnd).ToArray();
    client.Send(dnsQueryString, dnsQueryString.Length);

If the question is simply "Get IP of domain from DNS in C#" the simple answer is 如果问题只是“从C#中的DNS获取域的IP”,则简单的答案是

var res = Dns.GetHostEntry("google.com").AddressList;

This statement return a string array with IP addresses of resolved domain name. 该语句返回一个字符串数组,其中包含解析域名的IP地址。

If the question is more complex "Get IP of domain from DNS in C# using TCP/IP protocol", take a look here 如果问题更复杂“使用TCP / IP协议从C#中的DNS获取域的IP”,请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM