有没有人知道在使用Resolv :: DNS时如何强制TCP?
似乎当我要求ANY
条记录时,输出会被截断,我得到部分结果。当我执行许多查询(每个记录类型一个)时,我得到更多结果。我也得到不一致的结果(不同的机器,两个连续的查询返回不同的结果,...)
我认为这可能与UDP受限于数据包大小有关。
知道如何强制它使用TCP吗?我可以使用的任何其他DNS pakcage?
答案 0 :(得分: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