【问题标题】:Build a DNS Proxy in C在 C 中构建 DNS 代理
【发布时间】:2014-11-15 06:36:03
【问题描述】:

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

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

感谢您的帮助!!

【问题讨论】:

    标签: c dns


    【解决方案1】:

    你很幸运地满足了这些要求 - 因为你是从 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
    }
    

    【讨论】:

      【解决方案2】:

      首先,您需要选择一个用于将消息写入网络的 API。 对于 Windows,您有 Winsock API。 对于类 unix 系统,您可以使用 BSD Sockets API
      虽然大部分课程都使用 BSD API。

      现在你的步骤可能是:

      • 查看rfc 以了解 DNS 实施。您只能关注请求和响应消息的格式,因为您可能需要更改某些字段。
      • 现在编写一个由两个模块组成的客户端服务器代码:
        • 接收 DNS 查询请求的服务器端代码,可能需要更改某些字段。
        • 将其传递给与 Google 的 DNS 服务器交互的模块。
        • 捕获响应并将其转发回请求客户端。 (同样,您可能需要更改某些字段)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 2017-01-13
        相关资源
        最近更新 更多