gpt4 book ai didi

c - 在 C 中构建 DNS 代理

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:48 24 4
gpt4 key购买 nike

我想在 C 中构建一个简单的 DNS 代理,它接受来自 UDP 端口 53 的 DNS 查询,将查询转发到 Google 的 DNS 服务器 TCP 端口 53 进行查找,然后返回 Google 提供的答案。

是的,这是一个学校项目,我很困惑,不知道从哪里开始。

感谢您的帮助!!

最佳答案

您很幸运满足了这些要求 - 因为您是从 UDP -> TCP 进行的,它实际上比执行 UDP -> UDP 简单得多。

具体来说,我的意思是因为面向外的一面使用的是面向连接的套接字,所以您立即知道您收到的响应必须与您刚刚发送的查询有关,只要您为每个查询使用一个新的 TCP 套接字。

如果面向外的一侧是 UDP,则很难确定每个响应与哪个查询相关 - 协议(protocol)中无法保证响应与查询的到达顺序相同。

如果多线程不是必需的,那么(在伪代码中)

"open" a UDP socket
"bind" that socket to port 53
while (true) {
"recvfrom" a packet from the UDP socket
... and remember the address it was received from
"open" a TCP socket
"connect" it to Google's DNS
"write" the length of the original query (two bytes, network order - RFC 1035)
"write" the contents of the original query
"read" a two byte length header
"read" that many bytes from the TCP socket
"close" the TCP socket
"sendto" those bytes back over the UDP socket to the original client address
}

关于c - 在 C 中构建 DNS 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4212618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com