2

有谁知道在使用 Resolv::DNS 时如何强制 TCP?
似乎当我要求ANY记录时,输出被截断并且我得到部分结果。当我执行许多查询(每种记录类型一个)时,我会得到更多结果。我也得到不一致的结果(机器之间不同,两个顺序查询返回不同的结果,......)

我认为这可能与 UDP 受限于数据包大小有关。

知道如何强制它使用 TCP 吗?我可以使用任何其他 DNS 软件包吗?

4

1 回答 1

2

我遇到了同样的问题,想Resolv用于仅 TCP 查询,因为我期望结果集非常大。我最终挖掘了 Resolv 的源代码并了解到,默认情况下,只有在 UDP 查询失败时才会执行 TCP 查询。我发现我可以继承Resolv::DNS并覆盖该each_resource方法。这是我的来源:

require 'resolv'

# A TCP-only resolver built from `Resolv::DNS`. See the docs for what it's about.
# http://ruby-doc.org/stdlib-1.9.3/libdoc/resolv/rdoc/Resolv/DNS.html
class TcpDNS < Resolv::DNS
  # Override fetch_resource to use a TCP requester instead of a UDP requester. This
  # is mostly borrowed from `lib/resolv.rb` with the UDP->TCP fallback logic removed.
  def each_resource(name, typeclass, &proc)
    lazy_initialize
    senders = {}
    requester = nil
    begin
      @config.resolv(name) { |candidate, tout, nameserver, port|
        requester = make_tcp_requester(nameserver, port)
        msg = Message.new
        msg.rd = 1
        msg.add_question(candidate, typeclass)
        unless sender = senders[[candidate, nameserver, port]]
          sender = senders[[candidate, nameserver, port]] =
            requester.sender(msg, candidate, nameserver, port)
        end

        begin # HACK
          reply, reply_name = requester.request(sender, tout)
        rescue
          return
        end

        case reply.rcode
        when RCode::NoError
          extract_resources(reply, reply_name, typeclass, &proc)
          return
        when RCode::NXDomain
          raise Config::NXDomain.new(reply_name.to_s)
        else
          raise Config::OtherResolvError.new(reply_name.to_s)
        end
      }
    ensure
      requester.close
    end
  end
end

然后使用它很简单,如下所示:

TcpDNS.open :nameserver => ns_addrs, :search => '', :ndots => 1 do |dns|
  resp = dns.getresources target, Resolv::DNS::Resource::IN::ANY
end
于 2013-08-16T06:17:55.820 回答