简体   繁体   English

在C中建立DNS代理

[英]Build a DNS Proxy in C

I want to build a simple DNS Proxy in C, which accepts DNS Queries from UDP Port 53, forwards the query to Google's DNS server TCP port 53 to do the lookup, and then returns the answer offered by Google. 我想用C构建一个简单的DNS代理,该代理从UDP端口53接受DNS查询,将查询转发到Google的DNS服务器TCP端口53进行查找,然后返回Google提供的答案。

Yes, this is a school project and I'm so confused that I don't know where to get started. 是的,这是一个学校项目,我很困惑,我不知道从哪里开始。

Thanks for helping!! 感谢您的帮助!

You've struck lucky with the requirements - because you're going from UDP -> TCP, it's actually a lot simpler than doing UDP -> UDP. 您对这些要求感到很幸运-因为您要使用UDP-> TCP,所以实际上比做UDP-> UDP简单得多。

Specifically, what I mean is that because the outward facing side is using a connection orientated socket, you know straight away that the response you receive must pertain to the query you just sent, so long as you use a new TCP socket for each query. 具体来说,我的意思是因为面向外的一侧正在使用面向连接的套接字,因此您可以立即知道,只要为每个查询使用新的TCP套接字,您收到的响应就必须与您刚刚发送的查询有关。

If the outward facing side had been UDP it becomes a lot harder to figure out which query each response relates to - there's no guarantee in the protocol that responses arrive in the same order as the queries. 如果面向外部的是UDP,则很难弄清每个响应与哪个查询相关-协议中无法保证响应以与查询相同的顺序到达。

If multithreading isn't a requirement, then (in pseudo-code) 如果不需要多线程,那么(使用伪代码)

"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
}

first of all you need to chose an API for writing messages on to a network. 首先,您需要选择用于将消息写入网络的API。 For windows, you have Winsock API . 对于Windows,您具有Winsock API For unix-like systems you have the BSD Sockets API . 对于类Unix系统,您可以使用BSD套接字API
Although most of the courses use the BSD API. 尽管大多数课程都使用BSD API。

Now your steps may be: 现在您的步骤可能是:

  • have a look at rfc for DNS implementation. 看看用于DNS实施的rfc You can only focus on format of request and response messages, as you may need to change some fields. 由于您可能需要更改某些字段,因此您只能关注请求和响应消息的格式。
  • Now write a client server code which consists of two modules: 现在编写一个包含两个模块的客户端服务器代码:
    • Server side code to receive a DNS query request, may need to change some fields. 服务器端代码要接收DNS查询请求,可能需要更改某些字段。
    • Pass it on to the Module that interacts with Google's DNS server. 将其传递给与Google的DNS服务器交互的模块。
    • Capture the response and forward it back to requesting client. 捕获响应并将其转发回请求客户端。 (again you may need to change some fields) (同样,您可能需要更改一些字段)

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

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