在C中建立DNS代理 [英] Build a DNS Proxy in C

查看:49
本文介绍了在C中建立DNS代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Yes, this is a school project and I'm so confused that I don't know where to get started.

感谢您的帮助!

推荐答案

您对要求感到吃惊-因为您要使用的是UDP-> TCP,所以实际上比做UDP-> UDP简单得多.

You've struck lucky with the requirements - because you're going from UDP -> TCP, it's actually a lot simpler than doing UDP -> UDP.

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

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.

如果面向外部的对象是UDP,要弄清楚每个响应所涉及的查询变得更加困难-协议中无法保证响应以与查询相同的顺序到达.

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.

如果不需要多线程,则(使用伪代码)

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
}

这篇关于在C中建立DNS代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆