【发布时间】:2014-11-15 06:36:03
【问题描述】:
我想在 C 中构建一个简单的 DNS 代理,它接受来自 UDP 端口 53 的 DNS 查询,将查询转发到 Google 的 DNS 服务器 TCP 端口 53 进行查找,然后返回 Google 提供的答案。
是的,这是一个学校项目,我很困惑,不知道从哪里开始。
感谢您的帮助!!
【问题讨论】:
我想在 C 中构建一个简单的 DNS 代理,它接受来自 UDP 端口 53 的 DNS 查询,将查询转发到 Google 的 DNS 服务器 TCP 端口 53 进行查找,然后返回 Google 提供的答案。
是的,这是一个学校项目,我很困惑,不知道从哪里开始。
感谢您的帮助!!
【问题讨论】:
你很幸运地满足了这些要求 - 因为你是从 UDP -> TCP 开始的,它实际上比 UDP -> UDP 简单得多。
具体来说,我的意思是,因为面向外的一面使用的是面向连接的套接字,所以您立即知道您收到的响应必须与您刚刚发送的查询有关,只要您为每个查询使用一个新的 TCP 套接字。
如果面向外的一侧是 UDP,则很难确定每个响应与哪个查询相关 - 协议中无法保证响应与查询的到达顺序相同。
如果不需要多线程,那么(在伪代码中)
"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
}
【讨论】:
首先,您需要选择一个用于将消息写入网络的 API。
对于 Windows,您有 Winsock API。
对于类 unix 系统,您可以使用 BSD Sockets API。
虽然大部分课程都使用 BSD API。
现在你的步骤可能是:
【讨论】: