Commit 187d1e80 authored by JoyJ's avatar JoyJ

add some comments

parent 96fc75ef
......@@ -16,10 +16,14 @@ class Program
}
public static (string,int) GetSrvInfoForYgopro(string host)
{
//添加ygopro前缀
host = $"_ygopro._tcp.{host}";
//DNS服务器
string dnsServer = "114.114.114.114";
//DNS服务器端口
int dnsPort = 53;
//发送的数据
byte[] req = CreateRequestData(host);
byte[]? res = null;
......@@ -28,7 +32,9 @@ class Program
Console.WriteLine("Request string:");
Console.WriteLine(BitConverter.ToString(req));
#endif
//发送数据
udp.Send(req, req.Length);
//接收数据
IPEndPoint? ep = null;
res = udp.Receive(ref ep);
#if DEBUG
......@@ -39,8 +45,12 @@ class Program
Console.WriteLine($"{index} {(char)(res[index])} {(int)res[index]}");
}
#endif
//返回的数据头很长,与发送的数据长度相同。
//数据头之后,第16字节~17字节为2字节的端口号。
int resPort = res[req.Length + 16] * 256 + res[req.Length + 17];
//第18字节开始,是
//1字节的short,表示下一段host字符串的长度 -> 一段host的string -> 1字节的short,表示下一段host字符串的长度 -> 一段host的string……
//直到长度==0为止
int start = req.Length + 18;
int nextLen = res[start];
string resHost = "";
......@@ -56,14 +66,15 @@ class Program
nextLen = res[start];
}
resHost = resHost.Substring(0, resHost.Length - 1);
//拼接host
return (resHost,resPort);
}
private static byte[] CreateRequestData(string qName)
{
//参考资料: https://datatracker.ietf.org/doc/html/rfc2782 RFC2782
MemoryStream ms = new MemoryStream();
//参考资料: https://datatracker.ietf.org/doc/html/rfc2782 RFC2782
WriteBytes(0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0);
string[] buffs = qName.Split('.');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment