Open weppos opened 14 years ago
Fixed a problem with rescue ArgumentError (closed by df6be6cfc30e26cd73fe38a1dfb637da0ad3b804) and with IPAddr handling (closed by df6be6cfc30e26cd73fe38a1dfb637da0ad3b804)
This bug is still present on 0.7.1. The problem is that if, for example, I lookup for "match_1.dev", the call make_query_packet(argument, type, cls) will take around 10 seconds because of this check.
I think I found a smarter solution. Should we replace that implementation with this one?
def is_ip_address?(addr)
catch(:valid_ip) do
if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
throw(:valid_ip, true) if $~.captures.all? {|i| i.to_i < 256}
else
# IPv6 (normal)
throw(:valid_ip, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
throw(:valid_ip, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
throw(:valid_ip, true) if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
# IPv6 (IPv4 compat)
throw(:valid_ip, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_v4?($')
throw(:valid_ip, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
throw(:valid_ip, true) if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
end
false
end
end
def make_query_packet(string, type, cls)
if string.is_a?(IPAddr) then
name = string.reverse
type = Net::DNS::PTR
@logger.warn "PTR query required for address #{string}, changing type to PTR"
elsif is_ip_address?(string) # See if it's an IP or IPv6 address
begin
name = IPAddr.new(string.chomp(".")).reverse
type = Net::DNS::PTR
rescue ArgumentError
name = string if valid? string
end
else
name = string if valid? string
end
# Create the packet
packet = Net::DNS::Packet.new(name, type, cls)
if packet.query?
packet.header.recursive = @config[:recursive] ? 1 : 0
end
# DNSSEC and TSIG stuff to be inserted here
packet
end
Posted on behalf of Joshua Born and restored from http://rubyforge.org/forum/message.php?msg_id=91646.
By: Marco Ceresa