2001-05-30 05:10:30 -04:00
|
|
|
require 'socket'
|
|
|
|
require 'fcntl'
|
|
|
|
require 'timeout'
|
|
|
|
require 'thread'
|
|
|
|
|
2008-08-07 21:58:40 -04:00
|
|
|
begin
|
|
|
|
require 'securerandom'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
# Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can
|
2010-01-02 13:10:47 -05:00
|
|
|
# handle multiple DNS requests concurrently without blocking the entire ruby
|
2005-06-09 18:47:35 -04:00
|
|
|
# interpreter.
|
|
|
|
#
|
2010-01-02 13:10:47 -05:00
|
|
|
# See also resolv-replace.rb to replace the libc resolver with Resolv.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# Resolv can look up various DNS resources using the DNS module directly.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# Examples:
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# p Resolv.getaddress "www.ruby-lang.org"
|
|
|
|
# p Resolv.getname "210.251.121.214"
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# Resolv::DNS.open do |dns|
|
|
|
|
# ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
|
|
|
|
# p ress.map { |r| r.address }
|
|
|
|
# ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
|
|
|
|
# p ress.map { |r| [r.exchange.to_s, r.preference] }
|
|
|
|
# end
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# == Bugs
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# * NIS is not supported.
|
|
|
|
# * /etc/nsswitch.conf is not supported.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Resolv
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Looks up the first IP address for +name+.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def self.getaddress(name)
|
|
|
|
DefaultResolver.getaddress(name)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up all IP address for +name+.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def self.getaddresses(name)
|
|
|
|
DefaultResolver.getaddresses(name)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all IP addresses for +name+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def self.each_address(name, &block)
|
|
|
|
DefaultResolver.each_address(name, &block)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up the hostname of +address+.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def self.getname(address)
|
|
|
|
DefaultResolver.getname(address)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up all hostnames for +address+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def self.getnames(address)
|
|
|
|
DefaultResolver.getnames(address)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all hostnames for +address+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def self.each_name(address, &proc)
|
|
|
|
DefaultResolver.each_name(address, &proc)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Creates a new Resolv using +resolvers+.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(resolvers=[Hosts.new, DNS.new])
|
|
|
|
@resolvers = resolvers
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up the first IP address for +name+.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getaddress(name)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_address(name) {|address| return address}
|
|
|
|
raise ResolvError.new("no address for #{name}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up all IP address for +name+.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getaddresses(name)
|
|
|
|
ret = []
|
|
|
|
each_address(name) {|address| ret << address}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all IP addresses for +name+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_address(name)
|
|
|
|
if AddressRegex =~ name
|
|
|
|
yield name
|
|
|
|
return
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
2001-11-01 00:11:24 -05:00
|
|
|
yielded = false
|
|
|
|
@resolvers.each {|r|
|
|
|
|
r.each_address(name) {|address|
|
|
|
|
yield address.to_s
|
2003-07-23 21:03:28 -04:00
|
|
|
yielded = true
|
2001-11-01 00:11:24 -05:00
|
|
|
}
|
|
|
|
return if yielded
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up the hostname of +address+.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getname(address)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_name(address) {|name| return name}
|
|
|
|
raise ResolvError.new("no name for #{address}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up all hostnames for +address+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getnames(address)
|
|
|
|
ret = []
|
|
|
|
each_name(address) {|name| ret << name}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all hostnames for +address+.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_name(address)
|
|
|
|
yielded = false
|
|
|
|
@resolvers.each {|r|
|
|
|
|
r.each_name(address) {|name|
|
|
|
|
yield name.to_s
|
2003-07-23 21:03:28 -04:00
|
|
|
yielded = true
|
2001-11-01 00:11:24 -05:00
|
|
|
}
|
|
|
|
return if yielded
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates a failure to resolve a name or address.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class ResolvError < StandardError; end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Indicates a timeout resolving a name or address.
|
|
|
|
|
|
|
|
class ResolvTimeout < TimeoutError; end
|
|
|
|
|
|
|
|
##
|
2009-01-31 08:42:18 -05:00
|
|
|
# Resolv::Hosts is a hostname resolver that uses the system hosts file.
|
2002-01-15 22:37:23 -05:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Hosts
|
2009-02-01 18:12:52 -05:00
|
|
|
if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
|
2003-07-23 21:03:28 -04:00
|
|
|
require 'win32/resolv'
|
|
|
|
DefaultFileName = Win32::Resolv.get_hosts_path
|
|
|
|
else
|
|
|
|
DefaultFileName = '/etc/hosts'
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
2009-01-31 08:42:18 -05:00
|
|
|
# Creates a new Resolv::Hosts, using +filename+ for its data source.
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(filename = DefaultFileName)
|
|
|
|
@filename = filename
|
|
|
|
@mutex = Mutex.new
|
2001-09-08 10:17:53 -04:00
|
|
|
@initialized = nil
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def lazy_initialize # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
@mutex.synchronize {
|
|
|
|
unless @initialized
|
|
|
|
@name2addr = {}
|
|
|
|
@addr2name = {}
|
|
|
|
open(@filename) {|f|
|
|
|
|
f.each {|line|
|
|
|
|
line.sub!(/#.*/, '')
|
|
|
|
addr, hostname, *aliases = line.split(/\s+/)
|
|
|
|
next unless addr
|
2003-07-23 21:03:28 -04:00
|
|
|
addr.untaint
|
|
|
|
hostname.untaint
|
2001-05-30 05:10:30 -04:00
|
|
|
@addr2name[addr] = [] unless @addr2name.include? addr
|
|
|
|
@addr2name[addr] << hostname
|
|
|
|
@addr2name[addr] += aliases
|
|
|
|
@name2addr[hostname] = [] unless @name2addr.include? hostname
|
|
|
|
@name2addr[hostname] << addr
|
|
|
|
aliases.each {|n|
|
2003-07-23 21:03:28 -04:00
|
|
|
n.untaint
|
2001-05-30 05:10:30 -04:00
|
|
|
@name2addr[n] = [] unless @name2addr.include? n
|
|
|
|
@name2addr[n] << addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@name2addr.each {|name, arr| arr.reverse!}
|
|
|
|
@initialized = true
|
|
|
|
end
|
|
|
|
}
|
2005-01-28 03:18:59 -05:00
|
|
|
self
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets the IP address of +name+ from the hosts file.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getaddress(name)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_address(name) {|address| return address}
|
|
|
|
raise ResolvError.new("#{@filename} has no name: #{name}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets all IP addresses for +name+ from the hosts file.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getaddresses(name)
|
|
|
|
ret = []
|
|
|
|
each_address(name) {|address| ret << address}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all IP addresses for +name+ retrieved from the hosts file.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_address(name, &proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
lazy_initialize
|
|
|
|
if @name2addr.include?(name)
|
2003-07-23 21:03:28 -04:00
|
|
|
@name2addr[name].each(&proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets the hostname of +address+ from the hosts file.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getname(address)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_name(address) {|name| return name}
|
|
|
|
raise ResolvError.new("#{@filename} has no address: #{address}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets all hostnames for +address+ from the hosts file.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getnames(address)
|
|
|
|
ret = []
|
|
|
|
each_name(address) {|name| ret << name}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
2008-06-04 05:37:38 -04:00
|
|
|
# Iterates over all hostnames for +address+ retrieved from the hosts file.
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_name(address, &proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
lazy_initialize
|
|
|
|
if @addr2name.include?(address)
|
2001-11-01 00:11:24 -05:00
|
|
|
@addr2name[address].each(&proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Resolv::DNS is a DNS stub resolver.
|
|
|
|
#
|
|
|
|
# Information taken from the following places:
|
|
|
|
#
|
|
|
|
# * STD0013
|
|
|
|
# * RFC 1035
|
|
|
|
# * ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
|
|
|
|
# * etc.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class DNS
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Default DNS Port
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
Port = 53
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Default DNS UDP packet size
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
UDPSize = 512
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Creates a new DNS resolver. See Resolv::DNS.new for argument details.
|
|
|
|
#
|
|
|
|
# Yields the created DNS resolver to the block, if given, otherwise
|
|
|
|
# returns it.
|
|
|
|
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.open(*args)
|
|
|
|
dns = new(*args)
|
|
|
|
return dns unless block_given?
|
|
|
|
begin
|
2003-07-23 21:03:28 -04:00
|
|
|
yield dns
|
2002-07-12 12:34:02 -04:00
|
|
|
ensure
|
2003-07-23 21:03:28 -04:00
|
|
|
dns.close
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Creates a new DNS resolver.
|
|
|
|
#
|
|
|
|
# +config_info+ can be:
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# nil:: Uses /etc/resolv.conf.
|
|
|
|
# String:: Path to a file using /etc/resolv.conf's format.
|
|
|
|
# Hash:: Must contain :nameserver, :search and :ndots keys.
|
2010-01-02 02:03:24 -05:00
|
|
|
# :nameserver_port can be used to specify port number of nameserver address.
|
|
|
|
#
|
|
|
|
# The value of :nameserver should be an address string or
|
|
|
|
# an array of address strings.
|
|
|
|
# - :nameserver => '8.8.8.8'
|
|
|
|
# - :nameserver => ['8.8.8.8', '8.8.4.4']
|
|
|
|
#
|
|
|
|
# The value of :nameserver_port should be an array of
|
|
|
|
# pair of nameserver address and port number.
|
|
|
|
# - :nameserver_port => [['8.8.8.8', 53], ['8.8.4.4', 53]]
|
2005-06-09 18:47:35 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Resolv::DNS.new(:nameserver => ['210.251.121.21'],
|
|
|
|
# :search => ['ruby-lang.org'],
|
|
|
|
# :ndots => 1)
|
|
|
|
|
2004-05-12 04:44:11 -04:00
|
|
|
def initialize(config_info=nil)
|
2001-05-30 05:10:30 -04:00
|
|
|
@mutex = Mutex.new
|
2004-05-12 04:44:11 -04:00
|
|
|
@config = Config.new(config_info)
|
2001-09-08 10:17:53 -04:00
|
|
|
@initialized = nil
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def lazy_initialize # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
@mutex.synchronize {
|
|
|
|
unless @initialized
|
|
|
|
@config.lazy_initialize
|
|
|
|
@initialized = true
|
|
|
|
end
|
|
|
|
}
|
2005-01-28 03:18:59 -05:00
|
|
|
self
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Closes the DNS resolver.
|
|
|
|
|
2002-07-12 12:34:02 -04:00
|
|
|
def close
|
|
|
|
@mutex.synchronize {
|
|
|
|
if @initialized
|
|
|
|
@initialized = false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets the IP address of +name+ from the DNS resolver.
|
|
|
|
#
|
|
|
|
# +name+ can be a Resolv::DNS::Name or a String. Retrieved address will
|
|
|
|
# be a Resolv::IPv4 or Resolv::IPv6
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getaddress(name)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_address(name) {|address| return address}
|
|
|
|
raise ResolvError.new("DNS result has no information for #{name}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets all IP addresses for +name+ from the DNS resolver.
|
|
|
|
#
|
|
|
|
# +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
|
|
|
|
# be a Resolv::IPv4 or Resolv::IPv6
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getaddresses(name)
|
|
|
|
ret = []
|
|
|
|
each_address(name) {|address| ret << address}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all IP addresses for +name+ retrieved from the DNS
|
|
|
|
# resolver.
|
|
|
|
#
|
|
|
|
# +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
|
|
|
|
# be a Resolv::IPv4 or Resolv::IPv6
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_address(name)
|
2008-01-05 10:53:35 -05:00
|
|
|
each_resource(name, Resource::IN::A) {|resource| yield resource.address}
|
2009-01-31 08:40:05 -05:00
|
|
|
if use_ipv6?
|
|
|
|
each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_ipv6?
|
|
|
|
begin
|
|
|
|
list = Socket.ip_address_list
|
|
|
|
rescue NotImplementedError
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
list.any? {|a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? }
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
2009-01-31 08:40:05 -05:00
|
|
|
private :use_ipv6?
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets the hostname for +address+ from the DNS resolver.
|
|
|
|
#
|
|
|
|
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
|
|
|
|
# name will be a Resolv::DNS::Name.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getname(address)
|
2001-11-01 00:11:24 -05:00
|
|
|
each_name(address) {|name| return name}
|
|
|
|
raise ResolvError.new("DNS result has no information for #{address}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Gets all hostnames for +address+ from the DNS resolver.
|
|
|
|
#
|
|
|
|
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
|
|
|
|
# names will be Resolv::DNS::Name instances.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getnames(address)
|
|
|
|
ret = []
|
|
|
|
each_name(address) {|name| ret << name}
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all hostnames for +address+ retrieved from the DNS
|
|
|
|
# resolver.
|
|
|
|
#
|
|
|
|
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
|
|
|
|
# names will be Resolv::DNS::Name instances.
|
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def each_name(address)
|
2001-05-30 05:10:30 -04:00
|
|
|
case address
|
|
|
|
when Name
|
|
|
|
ptr = address
|
|
|
|
when IPv4::Regex
|
|
|
|
ptr = IPv4.create(address).to_name
|
|
|
|
when IPv6::Regex
|
|
|
|
ptr = IPv6.create(address).to_name
|
|
|
|
else
|
|
|
|
raise ResolvError.new("cannot interpret as address: #{address}")
|
|
|
|
end
|
2001-11-08 01:43:14 -05:00
|
|
|
each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Look up the +typeclass+ DNS resource of +name+.
|
|
|
|
#
|
|
|
|
# +name+ must be a Resolv::DNS::Name or a String.
|
|
|
|
#
|
|
|
|
# +typeclass+ should be one of the following:
|
|
|
|
#
|
|
|
|
# * Resolv::DNS::Resource::IN::A
|
|
|
|
# * Resolv::DNS::Resource::IN::AAAA
|
|
|
|
# * Resolv::DNS::Resource::IN::ANY
|
|
|
|
# * Resolv::DNS::Resource::IN::CNAME
|
|
|
|
# * Resolv::DNS::Resource::IN::HINFO
|
|
|
|
# * Resolv::DNS::Resource::IN::MINFO
|
|
|
|
# * Resolv::DNS::Resource::IN::MX
|
|
|
|
# * Resolv::DNS::Resource::IN::NS
|
|
|
|
# * Resolv::DNS::Resource::IN::PTR
|
|
|
|
# * Resolv::DNS::Resource::IN::SOA
|
|
|
|
# * Resolv::DNS::Resource::IN::TXT
|
|
|
|
# * Resolv::DNS::Resource::IN::WKS
|
|
|
|
#
|
|
|
|
# Returned resource is represented as a Resolv::DNS::Resource instance,
|
|
|
|
# i.e. Resolv::DNS::Resource::IN::A.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def getresource(name, typeclass)
|
2001-11-08 01:43:14 -05:00
|
|
|
each_resource(name, typeclass) {|resource| return resource}
|
2001-11-01 00:11:24 -05:00
|
|
|
raise ResolvError.new("DNS result has no information for #{name}")
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Looks up all +typeclass+ DNS resources for +name+. See #getresource for
|
|
|
|
# argument details.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-11-01 00:11:24 -05:00
|
|
|
def getresources(name, typeclass)
|
|
|
|
ret = []
|
2001-11-08 01:43:14 -05:00
|
|
|
each_resource(name, typeclass) {|resource| ret << resource}
|
2001-11-01 00:11:24 -05:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Iterates over all +typeclass+ DNS resources for +name+. See
|
|
|
|
# #getresource for argument details.
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2001-11-08 01:43:14 -05:00
|
|
|
def each_resource(name, typeclass, &proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
lazy_initialize
|
2010-10-29 17:01:53 -04:00
|
|
|
requester = make_udp_requester
|
2001-05-30 05:10:30 -04:00
|
|
|
senders = {}
|
|
|
|
begin
|
2010-01-02 02:03:24 -05:00
|
|
|
@config.resolv(name) {|candidate, tout, nameserver, port|
|
2001-05-30 05:10:30 -04:00
|
|
|
msg = Message.new
|
|
|
|
msg.rd = 1
|
|
|
|
msg.add_question(candidate, typeclass)
|
2010-01-02 02:03:24 -05:00
|
|
|
unless sender = senders[[candidate, nameserver, port]]
|
|
|
|
sender = senders[[candidate, nameserver, port]] =
|
|
|
|
requester.sender(msg, candidate, nameserver, port)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
reply, reply_name = requester.request(sender, tout)
|
2001-05-30 05:10:30 -04:00
|
|
|
case reply.rcode
|
|
|
|
when RCode::NoError
|
2010-10-29 17:01:53 -04:00
|
|
|
if reply.tc == 1 and not Requester::TCP === requester
|
|
|
|
requester.close
|
|
|
|
# Retry via TCP:
|
2010-11-27 04:03:26 -05:00
|
|
|
requester = make_tcp_requester(nameserver, port)
|
2010-10-29 17:01:53 -04:00
|
|
|
senders = {}
|
|
|
|
# This will use TCP for all remaining candidates (assuming the
|
|
|
|
# current candidate does not already respond successfully via
|
|
|
|
# TCP). This makes sense because we already know the full
|
|
|
|
# response will not fit in an untruncated UDP packet.
|
|
|
|
redo
|
|
|
|
else
|
|
|
|
extract_resources(reply, reply_name, typeclass, &proc)
|
|
|
|
end
|
2003-07-23 21:03:28 -04:00
|
|
|
return
|
2001-05-30 05:10:30 -04:00
|
|
|
when RCode::NXDomain
|
2002-03-22 00:15:44 -05:00
|
|
|
raise Config::NXDomain.new(reply_name.to_s)
|
2001-05-30 05:10:30 -04:00
|
|
|
else
|
2002-03-22 00:15:44 -05:00
|
|
|
raise Config::OtherResolvError.new(reply_name.to_s)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
ensure
|
2008-08-07 21:58:40 -04:00
|
|
|
requester.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-29 17:01:53 -04:00
|
|
|
def make_udp_requester # :nodoc:
|
2010-02-10 20:29:38 -05:00
|
|
|
nameserver_port = @config.nameserver_port
|
|
|
|
if nameserver_port.length == 1
|
|
|
|
Requester::ConnectedUDP.new(*nameserver_port[0])
|
2008-08-07 21:58:40 -04:00
|
|
|
else
|
2010-02-10 20:29:38 -05:00
|
|
|
Requester::UnconnectedUDP.new(*nameserver_port)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-27 04:03:26 -05:00
|
|
|
def make_tcp_requester(host, port) # :nodoc:
|
|
|
|
return Requester::TCP.new(host, port)
|
2010-10-29 17:01:53 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def extract_resources(msg, name, typeclass) # :nodoc:
|
2001-11-01 00:11:24 -05:00
|
|
|
if typeclass < Resource::ANY
|
2003-07-23 21:03:28 -04:00
|
|
|
n0 = Name.create(name)
|
2001-11-01 00:11:24 -05:00
|
|
|
msg.each_answer {|n, ttl, data|
|
2003-07-23 21:03:28 -04:00
|
|
|
yield data if n0 == n
|
|
|
|
}
|
2001-11-01 00:11:24 -05:00
|
|
|
end
|
|
|
|
yielded = false
|
2001-05-30 05:10:30 -04:00
|
|
|
n0 = Name.create(name)
|
|
|
|
msg.each_answer {|n, ttl, data|
|
|
|
|
if n0 == n
|
|
|
|
case data
|
|
|
|
when typeclass
|
2003-07-23 21:03:28 -04:00
|
|
|
yield data
|
|
|
|
yielded = true
|
2001-05-30 05:10:30 -04:00
|
|
|
when Resource::CNAME
|
|
|
|
n0 = data.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2001-11-01 00:11:24 -05:00
|
|
|
return if yielded
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.each_answer {|n, ttl, data|
|
|
|
|
if n0 == n
|
|
|
|
case data
|
|
|
|
when typeclass
|
2003-07-23 21:03:28 -04:00
|
|
|
yield data
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-08-07 21:58:40 -04:00
|
|
|
if defined? SecureRandom
|
|
|
|
def self.random(arg) # :nodoc:
|
|
|
|
begin
|
|
|
|
SecureRandom.random_number(arg)
|
|
|
|
rescue NotImplementedError
|
|
|
|
rand(arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def self.random(arg) # :nodoc:
|
|
|
|
rand(arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.rangerand(range) # :nodoc:
|
|
|
|
base = range.begin
|
|
|
|
len = range.end - range.begin
|
|
|
|
if !range.exclude_end?
|
|
|
|
len += 1
|
|
|
|
end
|
|
|
|
base + random(len)
|
|
|
|
end
|
|
|
|
|
|
|
|
RequestID = {}
|
|
|
|
RequestIDMutex = Mutex.new
|
|
|
|
|
|
|
|
def self.allocate_request_id(host, port) # :nodoc:
|
|
|
|
id = nil
|
|
|
|
RequestIDMutex.synchronize {
|
|
|
|
h = (RequestID[[host, port]] ||= {})
|
|
|
|
begin
|
|
|
|
id = rangerand(0x0000..0xffff)
|
2008-09-14 11:18:53 -04:00
|
|
|
end while h[id]
|
2008-08-07 21:58:40 -04:00
|
|
|
h[id] = true
|
|
|
|
}
|
|
|
|
id
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.free_request_id(host, port, id) # :nodoc:
|
|
|
|
RequestIDMutex.synchronize {
|
|
|
|
key = [host, port]
|
|
|
|
if h = RequestID[key]
|
|
|
|
h.delete id
|
|
|
|
if h.empty?
|
|
|
|
RequestID.delete key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc:
|
2008-08-07 21:58:40 -04:00
|
|
|
begin
|
|
|
|
port = rangerand(1024..65535)
|
2010-02-10 20:29:38 -05:00
|
|
|
udpsock.bind(bind_host, port)
|
2008-08-07 21:58:40 -04:00
|
|
|
rescue Errno::EADDRINUSE
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Requester # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize
|
|
|
|
@senders = {}
|
2010-02-10 20:29:38 -05:00
|
|
|
@socks = nil
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2008-08-07 21:58:40 -04:00
|
|
|
def request(sender, tout)
|
|
|
|
timelimit = Time.now + tout
|
|
|
|
sender.send
|
2010-02-24 08:44:08 -05:00
|
|
|
while true
|
|
|
|
now = Time.now
|
2008-08-07 21:58:40 -04:00
|
|
|
timeout = timelimit - now
|
2010-02-24 08:44:08 -05:00
|
|
|
if timeout <= 0
|
|
|
|
raise ResolvTimeout
|
|
|
|
end
|
2010-02-10 20:29:38 -05:00
|
|
|
select_result = IO.select(@socks, nil, nil, timeout)
|
|
|
|
if !select_result
|
2008-08-07 21:58:40 -04:00
|
|
|
raise ResolvTimeout
|
|
|
|
end
|
2010-05-28 20:06:17 -04:00
|
|
|
begin
|
|
|
|
reply, from = recv_reply(select_result[0])
|
2010-05-31 08:51:13 -04:00
|
|
|
rescue Errno::ECONNREFUSED, # GNU/Linux, FreeBSD
|
|
|
|
Errno::ECONNRESET # Windows
|
2010-05-28 20:06:17 -04:00
|
|
|
# No name server running on the server?
|
|
|
|
# Don't wait anymore.
|
|
|
|
raise ResolvTimeout
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
begin
|
|
|
|
msg = Message.decode(reply)
|
|
|
|
rescue DecodeError
|
|
|
|
next # broken DNS message ignored
|
|
|
|
end
|
|
|
|
if s = @senders[[from,msg.id]]
|
|
|
|
break
|
|
|
|
else
|
|
|
|
# unexpected DNS message ignored
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
return msg, s.data
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2008-08-07 21:58:40 -04:00
|
|
|
def close
|
2010-02-10 20:29:38 -05:00
|
|
|
socks = @socks
|
|
|
|
@socks = nil
|
|
|
|
if socks
|
|
|
|
socks.each {|sock| sock.close }
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Sender # :nodoc:
|
2008-08-07 21:58:40 -04:00
|
|
|
def initialize(msg, data, sock)
|
2002-07-12 12:34:02 -04:00
|
|
|
@msg = msg
|
2001-05-30 05:10:30 -04:00
|
|
|
@data = data
|
2002-07-12 12:34:02 -04:00
|
|
|
@sock = sock
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class UnconnectedUDP < Requester # :nodoc:
|
2010-02-10 20:29:38 -05:00
|
|
|
def initialize(*nameserver_port)
|
2001-05-30 05:10:30 -04:00
|
|
|
super()
|
2010-02-10 20:29:38 -05:00
|
|
|
@nameserver_port = nameserver_port
|
|
|
|
@socks_hash = {}
|
|
|
|
@socks = []
|
|
|
|
nameserver_port.each {|host, port|
|
|
|
|
if host.index(':')
|
|
|
|
bind_host = "::"
|
|
|
|
af = Socket::AF_INET6
|
|
|
|
else
|
|
|
|
bind_host = "0.0.0.0"
|
|
|
|
af = Socket::AF_INET
|
|
|
|
end
|
|
|
|
next if @socks_hash[bind_host]
|
|
|
|
sock = UDPSocket.new(af)
|
|
|
|
sock.do_not_reverse_lookup = true
|
|
|
|
sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
|
|
|
|
DNS.bind_random_port(sock, bind_host)
|
|
|
|
@socks << sock
|
|
|
|
@socks_hash[bind_host] = sock
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def recv_reply(readable_socks)
|
|
|
|
reply, from = readable_socks[0].recvfrom(UDPSize)
|
2008-08-07 21:58:40 -04:00
|
|
|
return reply, [from[3],from[1]]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender(msg, data, host, port=Port)
|
2001-05-30 05:10:30 -04:00
|
|
|
service = [host, port]
|
2008-08-07 21:58:40 -04:00
|
|
|
id = DNS.allocate_request_id(host, port)
|
2001-05-30 05:10:30 -04:00
|
|
|
request = msg.encode
|
|
|
|
request[0,2] = [id].pack('n')
|
2010-02-10 20:29:38 -05:00
|
|
|
sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
|
2001-05-30 05:10:30 -04:00
|
|
|
return @senders[[service, id]] =
|
2010-02-10 20:29:38 -05:00
|
|
|
Sender.new(request, data, sock, host, port)
|
2008-08-07 21:58:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
super
|
|
|
|
@senders.each_key {|service, id|
|
|
|
|
DNS.free_request_id(service[0], service[1], id)
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Sender < Requester::Sender # :nodoc:
|
2008-08-07 21:58:40 -04:00
|
|
|
def initialize(msg, data, sock, host, port)
|
|
|
|
super(msg, data, sock)
|
2001-05-30 05:10:30 -04:00
|
|
|
@host = host
|
|
|
|
@port = port
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
attr_reader :data
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def send
|
|
|
|
@sock.send(@msg, 0, @host, @port)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class ConnectedUDP < Requester # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(host, port=Port)
|
|
|
|
super()
|
|
|
|
@host = host
|
|
|
|
@port = port
|
2009-10-06 11:31:22 -04:00
|
|
|
is_ipv6 = host.index(':')
|
2010-02-10 20:29:38 -05:00
|
|
|
sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET)
|
|
|
|
@socks = [sock]
|
|
|
|
sock.do_not_reverse_lookup = true
|
|
|
|
sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
|
|
|
|
DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0")
|
|
|
|
sock.connect(host, port)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def recv_reply(readable_socks)
|
|
|
|
reply = readable_socks[0].recv(UDPSize)
|
2008-08-07 21:58:40 -04:00
|
|
|
return reply, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender(msg, data, host=@host, port=@port)
|
2001-05-30 05:10:30 -04:00
|
|
|
unless host == @host && port == @port
|
|
|
|
raise RequestError.new("host/port don't match: #{host}:#{port}")
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
id = DNS.allocate_request_id(@host, @port)
|
2001-05-30 05:10:30 -04:00
|
|
|
request = msg.encode
|
|
|
|
request[0,2] = [id].pack('n')
|
2010-02-10 20:29:38 -05:00
|
|
|
return @senders[[nil,id]] = Sender.new(request, data, @socks[0])
|
2008-08-07 21:58:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
super
|
|
|
|
@senders.each_key {|from, id|
|
|
|
|
DNS.free_request_id(@host, @port, id)
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Sender < Requester::Sender # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def send
|
|
|
|
@sock.send(@msg, 0)
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
attr_reader :data
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class TCP < Requester # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(host, port=Port)
|
|
|
|
super()
|
|
|
|
@host = host
|
|
|
|
@port = port
|
2010-02-10 20:29:38 -05:00
|
|
|
sock = TCPSocket.new(@host, @port)
|
|
|
|
@socks = [sock]
|
|
|
|
sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
|
2001-05-30 05:10:30 -04:00
|
|
|
@senders = {}
|
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def recv_reply(readable_socks)
|
|
|
|
len = readable_socks[0].read(2).unpack('n')[0]
|
|
|
|
reply = @socks[0].read(len)
|
2008-08-07 21:58:40 -04:00
|
|
|
return reply, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender(msg, data, host=@host, port=@port)
|
2001-05-30 05:10:30 -04:00
|
|
|
unless host == @host && port == @port
|
|
|
|
raise RequestError.new("host/port don't match: #{host}:#{port}")
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
id = DNS.allocate_request_id(@host, @port)
|
2001-05-30 05:10:30 -04:00
|
|
|
request = msg.encode
|
|
|
|
request[0,2] = [request.length, id].pack('nn')
|
2010-02-10 20:29:38 -05:00
|
|
|
return @senders[[nil,id]] = Sender.new(request, data, @socks[0])
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Sender < Requester::Sender # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def send
|
|
|
|
@sock.print(@msg)
|
|
|
|
@sock.flush
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
attr_reader :data
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
super
|
|
|
|
@senders.each_key {|from,id|
|
|
|
|
DNS.free_request_id(@host, @port, id)
|
|
|
|
}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates a problem with the DNS request.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class RequestError < StandardError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Config # :nodoc:
|
2004-05-12 04:44:11 -04:00
|
|
|
def initialize(config_info=nil)
|
2001-05-30 05:10:30 -04:00
|
|
|
@mutex = Mutex.new
|
2004-05-12 04:44:11 -04:00
|
|
|
@config_info = config_info
|
2003-07-23 21:03:28 -04:00
|
|
|
@initialized = nil
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2004-05-12 04:44:11 -04:00
|
|
|
def Config.parse_resolv_conf(filename)
|
|
|
|
nameserver = []
|
|
|
|
search = nil
|
|
|
|
ndots = 1
|
|
|
|
open(filename) {|f|
|
|
|
|
f.each {|line|
|
|
|
|
line.sub!(/[#;].*/, '')
|
|
|
|
keyword, *args = line.split(/\s+/)
|
|
|
|
args.each { |arg|
|
|
|
|
arg.untaint
|
|
|
|
}
|
|
|
|
next unless keyword
|
|
|
|
case keyword
|
|
|
|
when 'nameserver'
|
|
|
|
nameserver += args
|
|
|
|
when 'domain'
|
2005-01-18 11:29:23 -05:00
|
|
|
next if args.empty?
|
2004-05-12 04:44:11 -04:00
|
|
|
search = [args[0]]
|
|
|
|
when 'search'
|
2005-01-18 11:29:23 -05:00
|
|
|
next if args.empty?
|
2004-05-12 04:44:11 -04:00
|
|
|
search = args
|
2005-01-28 03:18:59 -05:00
|
|
|
when 'options'
|
|
|
|
args.each {|arg|
|
|
|
|
case arg
|
|
|
|
when /\Andots:(\d+)\z/
|
|
|
|
ndots = $1.to_i
|
|
|
|
end
|
|
|
|
}
|
2004-05-12 04:44:11 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { :nameserver => nameserver, :search => search, :ndots => ndots }
|
|
|
|
end
|
|
|
|
|
|
|
|
def Config.default_config_hash(filename="/etc/resolv.conf")
|
|
|
|
if File.exist? filename
|
|
|
|
config_hash = Config.parse_resolv_conf(filename)
|
|
|
|
else
|
2009-02-01 18:12:52 -05:00
|
|
|
if /mswin|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
|
2008-03-31 12:55:54 -04:00
|
|
|
require 'win32/resolv'
|
2004-05-12 04:44:11 -04:00
|
|
|
search, nameserver = Win32::Resolv.get_resolv_info
|
|
|
|
config_hash = {}
|
|
|
|
config_hash[:nameserver] = nameserver if nameserver
|
2004-08-22 23:44:32 -04:00
|
|
|
config_hash[:search] = [search].flatten if search
|
2004-05-12 04:44:11 -04:00
|
|
|
end
|
|
|
|
end
|
2010-01-19 06:00:33 -05:00
|
|
|
config_hash || {}
|
2004-05-12 04:44:11 -04:00
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def lazy_initialize
|
|
|
|
@mutex.synchronize {
|
|
|
|
unless @initialized
|
2010-01-02 02:03:24 -05:00
|
|
|
@nameserver_port = []
|
2001-05-30 05:10:30 -04:00
|
|
|
@search = nil
|
|
|
|
@ndots = 1
|
2004-05-12 04:44:11 -04:00
|
|
|
case @config_info
|
|
|
|
when nil
|
|
|
|
config_hash = Config.default_config_hash
|
|
|
|
when String
|
|
|
|
config_hash = Config.parse_resolv_conf(@config_info)
|
|
|
|
when Hash
|
|
|
|
config_hash = @config_info.dup
|
|
|
|
if String === config_hash[:nameserver]
|
|
|
|
config_hash[:nameserver] = [config_hash[:nameserver]]
|
2003-07-23 21:03:28 -04:00
|
|
|
end
|
2004-05-12 04:44:11 -04:00
|
|
|
if String === config_hash[:search]
|
|
|
|
config_hash[:search] = [config_hash[:search]]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
2010-01-02 02:03:24 -05:00
|
|
|
if config_hash.include? :nameserver
|
|
|
|
@nameserver_port = config_hash[:nameserver].map {|ns| [ns, Port] }
|
|
|
|
end
|
|
|
|
if config_hash.include? :nameserver_port
|
|
|
|
@nameserver_port = config_hash[:nameserver_port].map {|ns, port| [ns, (port || Port)] }
|
|
|
|
end
|
2004-05-12 04:44:11 -04:00
|
|
|
@search = config_hash[:search] if config_hash.include? :search
|
|
|
|
@ndots = config_hash[:ndots] if config_hash.include? :ndots
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2010-01-02 02:03:24 -05:00
|
|
|
if @nameserver_port.empty?
|
2010-01-19 05:58:28 -05:00
|
|
|
@nameserver_port << ['0.0.0.0', Port]
|
2010-01-02 02:03:24 -05:00
|
|
|
end
|
2004-05-12 04:44:11 -04:00
|
|
|
if @search
|
|
|
|
@search = @search.map {|arg| Label.split(arg) }
|
|
|
|
else
|
2001-05-30 05:10:30 -04:00
|
|
|
hostname = Socket.gethostname
|
|
|
|
if /\./ =~ hostname
|
2002-03-12 03:12:32 -05:00
|
|
|
@search = [Label.split($')]
|
2001-05-30 05:10:30 -04:00
|
|
|
else
|
2002-03-12 03:12:32 -05:00
|
|
|
@search = [[]]
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
2004-05-12 04:44:11 -04:00
|
|
|
|
2010-01-02 02:03:24 -05:00
|
|
|
if !@nameserver_port.kind_of?(Array) ||
|
|
|
|
@nameserver_port.any? {|ns_port|
|
|
|
|
!(Array === ns_port) ||
|
|
|
|
ns_port.length != 2
|
|
|
|
!(String === ns_port[0]) ||
|
|
|
|
!(Integer === ns_port[1])
|
|
|
|
}
|
|
|
|
raise ArgumentError.new("invalid nameserver config: #{@nameserver_port.inspect}")
|
2004-05-12 04:44:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if !@search.kind_of?(Array) ||
|
|
|
|
!@search.all? {|ls| ls.all? {|l| Label::Str === l } }
|
|
|
|
raise ArgumentError.new("invalid search config: #{@search.inspect}")
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@ndots.kind_of?(Integer)
|
|
|
|
raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}")
|
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
@initialized = true
|
|
|
|
end
|
|
|
|
}
|
2005-01-28 03:18:59 -05:00
|
|
|
self
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def single?
|
|
|
|
lazy_initialize
|
2010-01-02 02:03:24 -05:00
|
|
|
if @nameserver_port.length == 1
|
|
|
|
return @nameserver_port[0]
|
2001-05-30 05:10:30 -04:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def nameserver_port
|
|
|
|
@nameserver_port
|
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def generate_candidates(name)
|
|
|
|
candidates = nil
|
2003-07-23 21:03:28 -04:00
|
|
|
name = Name.create(name)
|
|
|
|
if name.absolute?
|
2001-05-30 05:10:30 -04:00
|
|
|
candidates = [name]
|
2003-07-23 21:03:28 -04:00
|
|
|
else
|
|
|
|
if @ndots <= name.length - 1
|
|
|
|
candidates = [Name.new(name.to_a)]
|
|
|
|
else
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
candidates.concat(@search.map {|domain| Name.new(name.to_a + domain)})
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
return candidates
|
|
|
|
end
|
|
|
|
|
|
|
|
InitialTimeout = 5
|
|
|
|
|
|
|
|
def generate_timeouts
|
|
|
|
ts = [InitialTimeout]
|
2010-01-02 02:03:24 -05:00
|
|
|
ts << ts[-1] * 2 / @nameserver_port.length
|
2001-05-30 05:10:30 -04:00
|
|
|
ts << ts[-1] * 2
|
|
|
|
ts << ts[-1] * 2
|
|
|
|
return ts
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolv(name)
|
|
|
|
candidates = generate_candidates(name)
|
|
|
|
timeouts = generate_timeouts
|
|
|
|
begin
|
|
|
|
candidates.each {|candidate|
|
|
|
|
begin
|
|
|
|
timeouts.each {|tout|
|
2010-01-02 02:03:24 -05:00
|
|
|
@nameserver_port.each {|nameserver, port|
|
2001-05-30 05:10:30 -04:00
|
|
|
begin
|
2010-01-02 02:03:24 -05:00
|
|
|
yield candidate, tout, nameserver, port
|
2002-01-15 22:37:23 -05:00
|
|
|
rescue ResolvTimeout
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
raise ResolvError.new("DNS resolv timeout: #{name}")
|
|
|
|
rescue NXDomain
|
|
|
|
end
|
|
|
|
}
|
2005-01-21 06:11:44 -05:00
|
|
|
rescue ResolvError
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates no such domain was found.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class NXDomain < ResolvError
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates some other unhandled resolver error was encountered.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class OtherResolvError < ResolvError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
module OpCode # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
Query = 0
|
|
|
|
IQuery = 1
|
|
|
|
Status = 2
|
|
|
|
Notify = 4
|
|
|
|
Update = 5
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
module RCode # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
NoError = 0
|
|
|
|
FormErr = 1
|
|
|
|
ServFail = 2
|
|
|
|
NXDomain = 3
|
|
|
|
NotImp = 4
|
|
|
|
Refused = 5
|
|
|
|
YXDomain = 6
|
|
|
|
YXRRSet = 7
|
|
|
|
NXRRSet = 8
|
|
|
|
NotAuth = 9
|
|
|
|
NotZone = 10
|
|
|
|
BADVERS = 16
|
|
|
|
BADSIG = 16
|
|
|
|
BADKEY = 17
|
|
|
|
BADTIME = 18
|
|
|
|
BADMODE = 19
|
|
|
|
BADNAME = 20
|
|
|
|
BADALG = 21
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates that the DNS response was unable to be decoded.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class DecodeError < StandardError
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Indicates that the DNS request was unable to be encoded.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class EncodeError < StandardError
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
module Label # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def self.split(arg)
|
|
|
|
labels = []
|
|
|
|
arg.scan(/[^\.]+/) {labels << Str.new($&)}
|
|
|
|
return labels
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Str # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(string)
|
|
|
|
@string = string
|
|
|
|
@downcase = string.downcase
|
|
|
|
end
|
|
|
|
attr_reader :string, :downcase
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return @string
|
|
|
|
end
|
|
|
|
|
2003-07-23 21:03:28 -04:00
|
|
|
def inspect
|
|
|
|
return "#<#{self.class} #{self.to_s}>"
|
|
|
|
end
|
2001-11-01 00:11:24 -05:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def ==(other)
|
|
|
|
return @downcase == other.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
return @downcase.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A representation of a DNS name.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Name
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Creates a new DNS name from +arg+. +arg+ can be:
|
|
|
|
#
|
|
|
|
# Name:: returns +arg+.
|
|
|
|
# String:: Creates a new Name.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when Name
|
|
|
|
return arg
|
|
|
|
when String
|
2002-03-12 03:12:32 -05:00
|
|
|
return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
|
2001-05-30 05:10:30 -04:00
|
|
|
else
|
2002-03-12 03:12:32 -05:00
|
|
|
raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def initialize(labels, absolute=true) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
@labels = labels
|
2003-07-23 21:03:28 -04:00
|
|
|
@absolute = absolute
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def inspect # :nodoc:
|
2005-02-07 10:24:09 -05:00
|
|
|
"#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>"
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# True if this name is absolute.
|
|
|
|
|
2002-03-12 03:12:32 -05:00
|
|
|
def absolute?
|
|
|
|
return @absolute
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def ==(other) # :nodoc:
|
2005-02-07 10:24:09 -05:00
|
|
|
return false unless Name === other
|
2007-12-26 08:50:31 -05:00
|
|
|
return @labels.join == other.to_a.join && @absolute == other.absolute?
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
alias eql? == # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns true if +other+ is a subdomain.
|
|
|
|
#
|
|
|
|
# Example:
|
2005-02-07 10:24:09 -05:00
|
|
|
#
|
|
|
|
# domain = Resolv::DNS::Name.create("y.z")
|
|
|
|
# p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true
|
|
|
|
# p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true
|
|
|
|
# p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false
|
|
|
|
# p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false
|
|
|
|
# p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false
|
|
|
|
# p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false
|
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2005-02-07 10:24:09 -05:00
|
|
|
def subdomain_of?(other)
|
|
|
|
raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other
|
|
|
|
return false if @absolute != other.absolute?
|
|
|
|
other_len = other.length
|
|
|
|
return false if @labels.length <= other_len
|
|
|
|
return @labels[-other_len, other_len] == other.to_a
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def hash # :nodoc:
|
2002-03-12 03:12:32 -05:00
|
|
|
return @labels.hash ^ @absolute.hash
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def to_a # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @labels
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def length # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @labels.length
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def [](i) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @labels[i]
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
2005-02-07 10:24:09 -05:00
|
|
|
# returns the domain name as a string.
|
|
|
|
#
|
|
|
|
# The domain name doesn't have a trailing dot even if the name object is
|
|
|
|
# absolute.
|
|
|
|
#
|
2005-06-09 18:47:35 -04:00
|
|
|
# Example:
|
|
|
|
#
|
2005-02-07 10:24:09 -05:00
|
|
|
# p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
|
|
|
|
# p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def to_s
|
|
|
|
return @labels.join('.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class Message # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
@@identifier = -1
|
|
|
|
|
|
|
|
def initialize(id = (@@identifier += 1) & 0xffff)
|
|
|
|
@id = id
|
|
|
|
@qr = 0
|
|
|
|
@opcode = 0
|
|
|
|
@aa = 0
|
|
|
|
@tc = 0
|
|
|
|
@rd = 0 # recursion desired
|
|
|
|
@ra = 0 # recursion available
|
|
|
|
@rcode = 0
|
|
|
|
@question = []
|
|
|
|
@answer = []
|
|
|
|
@authority = []
|
|
|
|
@additional = []
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :id, :qr, :opcode, :aa, :tc, :rd, :ra, :rcode
|
|
|
|
attr_reader :question, :answer, :authority, :additional
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
return @id == other.id &&
|
|
|
|
@qr == other.qr &&
|
|
|
|
@opcode == other.opcode &&
|
|
|
|
@aa == other.aa &&
|
|
|
|
@tc == other.tc &&
|
|
|
|
@rd == other.rd &&
|
|
|
|
@ra == other.ra &&
|
|
|
|
@rcode == other.rcode &&
|
|
|
|
@question == other.question &&
|
|
|
|
@answer == other.answer &&
|
|
|
|
@authority == other.authority &&
|
|
|
|
@additional == other.additional
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_question(name, typeclass)
|
|
|
|
@question << [Name.create(name), typeclass]
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_question
|
|
|
|
@question.each {|name, typeclass|
|
|
|
|
yield name, typeclass
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_answer(name, ttl, data)
|
|
|
|
@answer << [Name.create(name), ttl, data]
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_answer
|
|
|
|
@answer.each {|name, ttl, data|
|
|
|
|
yield name, ttl, data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_authority(name, ttl, data)
|
|
|
|
@authority << [Name.create(name), ttl, data]
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_authority
|
|
|
|
@authority.each {|name, ttl, data|
|
|
|
|
yield name, ttl, data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_additional(name, ttl, data)
|
|
|
|
@additional << [Name.create(name), ttl, data]
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_additional
|
|
|
|
@additional.each {|name, ttl, data|
|
|
|
|
yield name, ttl, data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_resource
|
|
|
|
each_answer {|name, ttl, data| yield name, ttl, data}
|
|
|
|
each_authority {|name, ttl, data| yield name, ttl, data}
|
|
|
|
each_additional {|name, ttl, data| yield name, ttl, data}
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode
|
|
|
|
return MessageEncoder.new {|msg|
|
|
|
|
msg.put_pack('nnnnnn',
|
|
|
|
@id,
|
|
|
|
(@qr & 1) << 15 |
|
|
|
|
(@opcode & 15) << 11 |
|
|
|
|
(@aa & 1) << 10 |
|
|
|
|
(@tc & 1) << 9 |
|
|
|
|
(@rd & 1) << 8 |
|
|
|
|
(@ra & 1) << 7 |
|
|
|
|
(@rcode & 15),
|
|
|
|
@question.length,
|
|
|
|
@answer.length,
|
|
|
|
@authority.length,
|
|
|
|
@additional.length)
|
|
|
|
@question.each {|q|
|
|
|
|
name, typeclass = q
|
|
|
|
msg.put_name(name)
|
|
|
|
msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
|
|
|
|
}
|
|
|
|
[@answer, @authority, @additional].each {|rr|
|
|
|
|
rr.each {|r|
|
|
|
|
name, ttl, data = r
|
|
|
|
msg.put_name(name)
|
|
|
|
msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue, ttl)
|
|
|
|
msg.put_length16 {data.encode_rdata(msg)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.to_s
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class MessageEncoder # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize
|
|
|
|
@data = ''
|
|
|
|
@names = {}
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return @data
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_bytes(d)
|
|
|
|
@data << d
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_pack(template, *d)
|
|
|
|
@data << d.pack(template)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_length16
|
|
|
|
length_index = @data.length
|
|
|
|
@data << "\0\0"
|
|
|
|
data_start = @data.length
|
|
|
|
yield
|
|
|
|
data_end = @data.length
|
|
|
|
@data[length_index, 2] = [data_end - data_start].pack("n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_string(d)
|
|
|
|
self.put_pack("C", d.length)
|
|
|
|
@data << d
|
|
|
|
end
|
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
def put_string_list(ds)
|
|
|
|
ds.each {|d|
|
|
|
|
self.put_string(d)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def put_name(d)
|
|
|
|
put_labels(d.to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_labels(d)
|
|
|
|
d.each_index {|i|
|
|
|
|
domain = d[i..-1]
|
|
|
|
if idx = @names[domain]
|
|
|
|
self.put_pack("n", 0xc000 | idx)
|
|
|
|
return
|
|
|
|
else
|
|
|
|
@names[domain] = @data.length
|
|
|
|
self.put_label(d[i])
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@data << "\0"
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_label(d)
|
2007-12-26 08:50:31 -05:00
|
|
|
self.put_string(d.to_s)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def Message.decode(m)
|
|
|
|
o = Message.new(0)
|
|
|
|
MessageDecoder.new(m) {|msg|
|
|
|
|
id, flag, qdcount, ancount, nscount, arcount =
|
|
|
|
msg.get_unpack('nnnnnn')
|
|
|
|
o.id = id
|
|
|
|
o.qr = (flag >> 15) & 1
|
|
|
|
o.opcode = (flag >> 11) & 15
|
|
|
|
o.aa = (flag >> 10) & 1
|
|
|
|
o.tc = (flag >> 9) & 1
|
|
|
|
o.rd = (flag >> 8) & 1
|
|
|
|
o.ra = (flag >> 7) & 1
|
|
|
|
o.rcode = flag & 15
|
|
|
|
(1..qdcount).each {
|
|
|
|
name, typeclass = msg.get_question
|
|
|
|
o.add_question(name, typeclass)
|
|
|
|
}
|
|
|
|
(1..ancount).each {
|
|
|
|
name, ttl, data = msg.get_rr
|
|
|
|
o.add_answer(name, ttl, data)
|
|
|
|
}
|
|
|
|
(1..nscount).each {
|
|
|
|
name, ttl, data = msg.get_rr
|
|
|
|
o.add_authority(name, ttl, data)
|
|
|
|
}
|
|
|
|
(1..arcount).each {
|
|
|
|
name, ttl, data = msg.get_rr
|
|
|
|
o.add_additional(name, ttl, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
class MessageDecoder # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
|
|
|
@index = 0
|
|
|
|
@limit = data.length
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
2010-01-02 11:31:00 -05:00
|
|
|
def inspect
|
|
|
|
"\#<#{self.class}: #{@data[0, @index].inspect} #{@data[@index..-1].inspect}>"
|
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def get_length16
|
|
|
|
len, = self.get_unpack('n')
|
|
|
|
save_limit = @limit
|
|
|
|
@limit = @index + len
|
2003-10-16 13:47:19 -04:00
|
|
|
d = yield(len)
|
2001-05-30 05:10:30 -04:00
|
|
|
if @index < @limit
|
2004-03-29 02:54:38 -05:00
|
|
|
raise DecodeError.new("junk exists")
|
2001-05-30 05:10:30 -04:00
|
|
|
elsif @limit < @index
|
2004-03-29 02:54:38 -05:00
|
|
|
raise DecodeError.new("limit exceeded")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
@limit = save_limit
|
|
|
|
return d
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_bytes(len = @limit - @index)
|
|
|
|
d = @data[@index, len]
|
|
|
|
@index += len
|
|
|
|
return d
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_unpack(template)
|
|
|
|
len = 0
|
|
|
|
template.each_byte {|byte|
|
2006-08-08 06:29:02 -04:00
|
|
|
byte = "%c" % byte
|
2001-05-30 05:10:30 -04:00
|
|
|
case byte
|
|
|
|
when ?c, ?C
|
|
|
|
len += 1
|
|
|
|
when ?n
|
|
|
|
len += 2
|
|
|
|
when ?N
|
|
|
|
len += 4
|
|
|
|
else
|
|
|
|
raise StandardError.new("unsupported template: '#{byte.chr}' in '#{template}'")
|
|
|
|
end
|
|
|
|
}
|
2004-03-29 02:54:38 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit < @index + len
|
2001-05-30 05:10:30 -04:00
|
|
|
arr = @data.unpack("@#{@index}#{template}")
|
|
|
|
@index += len
|
2001-06-19 00:35:17 -04:00
|
|
|
return arr
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_string
|
2006-08-08 06:29:02 -04:00
|
|
|
len = @data[@index].ord
|
2004-03-29 02:54:38 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
|
2001-05-30 05:10:30 -04:00
|
|
|
d = @data[@index + 1, len]
|
|
|
|
@index += 1 + len
|
|
|
|
return d
|
|
|
|
end
|
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
def get_string_list
|
|
|
|
strings = []
|
|
|
|
while @index < @limit
|
|
|
|
strings << self.get_string
|
|
|
|
end
|
|
|
|
strings
|
|
|
|
end
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def get_name
|
|
|
|
return Name.new(self.get_labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_labels(limit=nil)
|
|
|
|
limit = @index if !limit || @index < limit
|
|
|
|
d = []
|
|
|
|
while true
|
2006-08-08 06:29:02 -04:00
|
|
|
case @data[@index].ord
|
2001-05-30 05:10:30 -04:00
|
|
|
when 0
|
|
|
|
@index += 1
|
|
|
|
return d
|
|
|
|
when 192..255
|
|
|
|
idx = self.get_unpack('n')[0] & 0x3fff
|
|
|
|
if limit <= idx
|
|
|
|
raise DecodeError.new("non-backward name pointer")
|
|
|
|
end
|
|
|
|
save_index = @index
|
|
|
|
@index = idx
|
|
|
|
d += self.get_labels(limit)
|
|
|
|
@index = save_index
|
|
|
|
return d
|
|
|
|
else
|
|
|
|
d << self.get_label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return d
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_label
|
|
|
|
return Label::Str.new(self.get_string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_question
|
|
|
|
name = self.get_name
|
|
|
|
type, klass = self.get_unpack("nn")
|
|
|
|
return name, Resource.get_class(type, klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_rr
|
|
|
|
name = self.get_name
|
|
|
|
type, klass, ttl = self.get_unpack('nnN')
|
|
|
|
typeclass = Resource.get_class(type, klass)
|
2005-06-15 11:32:46 -04:00
|
|
|
res = self.get_length16 { typeclass.decode_rdata self }
|
|
|
|
res.instance_variable_set :@ttl, ttl
|
|
|
|
return name, ttl, res
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A DNS query abstract class.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Query
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2008-09-14 11:18:53 -04:00
|
|
|
raise EncodeError.new("#{self.class} is query.")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2008-09-14 11:18:53 -04:00
|
|
|
raise DecodeError.new("#{self.class} is query.")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A DNS resource abstract class.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Resource < Query
|
|
|
|
|
2005-06-15 11:32:46 -04:00
|
|
|
##
|
|
|
|
# Remaining Time To Live for this Resource.
|
|
|
|
|
|
|
|
attr_reader :ttl
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
ClassHash = {} # :nodoc:
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def ==(other) # :nodoc:
|
2005-06-15 11:32:46 -04:00
|
|
|
return false unless self.class == other.class
|
|
|
|
s_ivars = self.instance_variables
|
|
|
|
s_ivars.sort!
|
|
|
|
s_ivars.delete "@ttl"
|
|
|
|
o_ivars = other.instance_variables
|
|
|
|
o_ivars.sort!
|
|
|
|
o_ivars.delete "@ttl"
|
|
|
|
return s_ivars == o_ivars &&
|
|
|
|
s_ivars.collect {|name| self.instance_variable_get name} ==
|
|
|
|
o_ivars.collect {|name| other.instance_variable_get name}
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def eql?(other) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def hash # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
h = 0
|
2005-06-15 11:32:46 -04:00
|
|
|
vars = self.instance_variables
|
|
|
|
vars.delete "@ttl"
|
|
|
|
vars.each {|name|
|
|
|
|
h ^= self.instance_variable_get(name).hash
|
2001-05-30 05:10:30 -04:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.get_class(type_value, class_value) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return ClassHash[[type_value, class_value]] ||
|
|
|
|
Generic.create(type_value, class_value)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A generic resource abstract class.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class Generic < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new generic resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Data for this generic resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :data
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_bytes(data)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self.new(msg.get_bytes)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.create(type_value, class_value) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
c = Class.new(Generic)
|
|
|
|
c.const_set(:TypeValue, type_value)
|
|
|
|
c.const_set(:ClassValue, class_value)
|
|
|
|
Generic.const_set("Type#{type_value}_Class#{class_value}", c)
|
|
|
|
ClassHash[[type_value, class_value]] = c
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Domain Name resource abstract class.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class DomainName < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new DomainName from +name+.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The name of this DomainName.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :name
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_name(@name)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self.new(msg.get_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Standard (class generic) RRs
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
ClassValue = nil # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# An authoritative name server.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
class NS < DomainName
|
2005-06-09 18:47:35 -04:00
|
|
|
TypeValue = 2 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# The canonical name for an alias.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class CNAME < DomainName
|
2005-06-09 18:47:35 -04:00
|
|
|
TypeValue = 5 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Start Of Authority resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class SOA < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
TypeValue = 6 # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new SOA record. See the attr documentation for the
|
|
|
|
# details of each argument.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(mname, rname, serial, refresh, retry_, expire, minimum)
|
|
|
|
@mname = mname
|
|
|
|
@rname = rname
|
|
|
|
@serial = serial
|
|
|
|
@refresh = refresh
|
|
|
|
@retry = retry_
|
|
|
|
@expire = expire
|
|
|
|
@minimum = minimum
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Name of the host where the master zone file for this zone resides.
|
|
|
|
|
|
|
|
attr_reader :mname
|
|
|
|
|
|
|
|
##
|
|
|
|
# The person responsible for this domain name.
|
|
|
|
|
|
|
|
attr_reader :rname
|
|
|
|
|
|
|
|
##
|
|
|
|
# The version number of the zone file.
|
|
|
|
|
|
|
|
attr_reader :serial
|
|
|
|
|
|
|
|
##
|
|
|
|
# How often, in seconds, a secondary name server is to check for
|
|
|
|
# updates from the primary name server.
|
|
|
|
|
|
|
|
attr_reader :refresh
|
|
|
|
|
|
|
|
##
|
|
|
|
# How often, in seconds, a secondary name server is to retry after a
|
|
|
|
# failure to check for a refresh.
|
|
|
|
|
|
|
|
attr_reader :retry
|
|
|
|
|
|
|
|
##
|
|
|
|
# Time in seconds that a secondary name server is to use the data
|
|
|
|
# before refreshing from the primary name server.
|
|
|
|
|
|
|
|
attr_reader :expire
|
|
|
|
|
|
|
|
##
|
|
|
|
# The minimum number of seconds to be used for TTL values in RRs.
|
|
|
|
|
|
|
|
attr_reader :minimum
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_name(@mname)
|
|
|
|
msg.put_name(@rname)
|
|
|
|
msg.put_pack('NNNNN', @serial, @refresh, @retry, @expire, @minimum)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
mname = msg.get_name
|
|
|
|
rname = msg.get_name
|
|
|
|
serial, refresh, retry_, expire, minimum = msg.get_unpack('NNNNN')
|
|
|
|
return self.new(
|
|
|
|
mname, rname, serial, refresh, retry_, expire, minimum)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A Pointer to another DNS name.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class PTR < DomainName
|
2005-06-09 18:47:35 -04:00
|
|
|
TypeValue = 12 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Host Information resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class HINFO < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
TypeValue = 13 # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new HINFO running +os+ on +cpu+.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(cpu, os)
|
|
|
|
@cpu = cpu
|
|
|
|
@os = os
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# CPU architecture for this resource.
|
|
|
|
|
|
|
|
attr_reader :cpu
|
|
|
|
|
|
|
|
##
|
|
|
|
# Operating system for this resource.
|
|
|
|
|
|
|
|
attr_reader :os
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_string(@cpu)
|
|
|
|
msg.put_string(@os)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
cpu = msg.get_string
|
|
|
|
os = msg.get_string
|
|
|
|
return self.new(cpu, os)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Mailing list or mailbox information.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class MINFO < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
TypeValue = 14 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(rmailbx, emailbx)
|
|
|
|
@rmailbx = rmailbx
|
|
|
|
@emailbx = emailbx
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Domain name responsible for this mail list or mailbox.
|
|
|
|
|
|
|
|
attr_reader :rmailbx
|
|
|
|
|
|
|
|
##
|
|
|
|
# Mailbox to use for error messages related to the mail list or mailbox.
|
|
|
|
|
|
|
|
attr_reader :emailbx
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_name(@rmailbx)
|
|
|
|
msg.put_name(@emailbx)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
rmailbx = msg.get_string
|
|
|
|
emailbx = msg.get_string
|
|
|
|
return self.new(rmailbx, emailbx)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Mail Exchanger resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class MX < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
TypeValue= 15 # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new MX record with +preference+, accepting mail at
|
|
|
|
# +exchange+.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(preference, exchange)
|
|
|
|
@preference = preference
|
|
|
|
@exchange = exchange
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# The preference for this MX.
|
|
|
|
|
|
|
|
attr_reader :preference
|
|
|
|
|
|
|
|
##
|
|
|
|
# The host of this MX.
|
|
|
|
|
|
|
|
attr_reader :exchange
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_pack('n', @preference)
|
|
|
|
msg.put_name(@exchange)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-11-01 00:11:24 -05:00
|
|
|
preference, = msg.get_unpack('n')
|
2001-05-30 05:10:30 -04:00
|
|
|
exchange = msg.get_name
|
|
|
|
return self.new(preference, exchange)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Unstructured text resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class TXT < Resource
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
TypeValue = 16 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
def initialize(first_string, *rest_strings)
|
|
|
|
@strings = [first_string, *rest_strings]
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Returns an Array of Strings for this TXT record.
|
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
attr_reader :strings
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Returns the first string from +strings+.
|
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
def data
|
|
|
|
@strings[0]
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2005-02-05 13:31:20 -05:00
|
|
|
msg.put_string_list(@strings)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2005-02-05 13:31:20 -05:00
|
|
|
strings = msg.get_string_list
|
|
|
|
return self.new(*strings)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A Query type requesting any RR.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class ANY < Query
|
2005-06-09 18:47:35 -04:00
|
|
|
TypeValue = 255 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
ClassInsensitiveTypes = [ # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
NS, CNAME, SOA, PTR, HINFO, MINFO, MX, TXT, ANY
|
|
|
|
]
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# module IN contains ARPA Internet specific RRs.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
module IN
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
ClassValue = 1 # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
ClassInsensitiveTypes.each {|s|
|
|
|
|
c = Class.new(s)
|
|
|
|
c.const_set(:TypeValue, s::TypeValue)
|
|
|
|
c.const_set(:ClassValue, ClassValue)
|
|
|
|
ClassHash[[s::TypeValue, ClassValue]] = c
|
|
|
|
self.const_set(s.name.sub(/.*::/, ''), c)
|
|
|
|
}
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# IPv4 Address resource
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class A < Resource
|
2006-02-19 19:15:36 -05:00
|
|
|
TypeValue = 1
|
|
|
|
ClassValue = IN::ClassValue
|
|
|
|
ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new A for +address+.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(address)
|
|
|
|
@address = IPv4.create(address)
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The Resolv::IPv4 address for this A.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :address
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_bytes(@address.address)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self.new(IPv4.new(msg.get_bytes(4)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Well Known Service resource.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class WKS < Resource
|
2006-02-19 19:15:36 -05:00
|
|
|
TypeValue = 11
|
|
|
|
ClassValue = IN::ClassValue
|
|
|
|
ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(address, protocol, bitmap)
|
|
|
|
@address = IPv4.create(address)
|
|
|
|
@protocol = protocol
|
|
|
|
@bitmap = bitmap
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# The host these services run on.
|
|
|
|
|
|
|
|
attr_reader :address
|
|
|
|
|
|
|
|
##
|
|
|
|
# IP protocol number for these services.
|
|
|
|
|
|
|
|
attr_reader :protocol
|
|
|
|
|
|
|
|
##
|
|
|
|
# A bit map of enabled services on this host.
|
|
|
|
#
|
|
|
|
# If protocol is 6 (TCP) then the 26th bit corresponds to the SMTP
|
|
|
|
# service (port 25). If this bit is set, then an SMTP server should
|
|
|
|
# be listening on TCP port 25; if zero, SMTP service is not
|
|
|
|
# supported.
|
|
|
|
|
|
|
|
attr_reader :bitmap
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_bytes(@address.address)
|
|
|
|
msg.put_pack("n", @protocol)
|
|
|
|
msg.put_bytes(@bitmap)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
address = IPv4.new(msg.get_bytes(4))
|
2001-11-01 00:11:24 -05:00
|
|
|
protocol, = msg.get_unpack("n")
|
2001-05-30 05:10:30 -04:00
|
|
|
bitmap = msg.get_bytes
|
|
|
|
return self.new(address, protocol, bitmap)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# An IPv6 address record.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class AAAA < Resource
|
2006-02-19 19:15:36 -05:00
|
|
|
TypeValue = 28
|
|
|
|
ClassValue = IN::ClassValue
|
|
|
|
ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new AAAA for +address+.
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def initialize(address)
|
|
|
|
@address = IPv6.create(address)
|
|
|
|
end
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# The Resolv::IPv6 address for this AAAA.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :address
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def encode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
msg.put_bytes(@address.address)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self.new(IPv6.new(msg.get_bytes(16)))
|
|
|
|
end
|
|
|
|
end
|
2005-01-29 00:14:11 -05:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
2005-01-29 00:14:11 -05:00
|
|
|
# SRV resource record defined in RFC 2782
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-01-29 00:14:11 -05:00
|
|
|
# These records identify the hostname and port that a service is
|
|
|
|
# available at.
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2005-01-29 00:14:11 -05:00
|
|
|
class SRV < Resource
|
2006-02-19 19:15:36 -05:00
|
|
|
TypeValue = 33
|
|
|
|
ClassValue = IN::ClassValue
|
|
|
|
ClassHash[[TypeValue, ClassValue]] = self # :nodoc:
|
2005-01-29 00:14:11 -05:00
|
|
|
|
|
|
|
# Create a SRV resource record.
|
2005-06-09 18:47:35 -04:00
|
|
|
#
|
|
|
|
# See the documentation for #priority, #weight, #port and #target
|
|
|
|
# for +priority+, +weight+, +port and +target+ respectively.
|
|
|
|
|
2005-01-29 00:14:11 -05:00
|
|
|
def initialize(priority, weight, port, target)
|
|
|
|
@priority = priority.to_int
|
|
|
|
@weight = weight.to_int
|
|
|
|
@port = port.to_int
|
|
|
|
@target = Name.create(target)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
# The priority of this target host.
|
|
|
|
#
|
|
|
|
# A client MUST attempt to contact the target host with the
|
|
|
|
# lowest-numbered priority it can reach; target hosts with the same
|
|
|
|
# priority SHOULD be tried in an order defined by the weight field.
|
|
|
|
# The range is 0-65535. Note that it is not widely implemented and
|
|
|
|
# should be set to zero.
|
|
|
|
|
|
|
|
attr_reader :priority
|
|
|
|
|
|
|
|
# A server selection mechanism.
|
|
|
|
#
|
|
|
|
# The weight field specifies a relative weight for entries with the
|
|
|
|
# same priority. Larger weights SHOULD be given a proportionately
|
|
|
|
# higher probability of being selected. The range of this number is
|
|
|
|
# 0-65535. Domain administrators SHOULD use Weight 0 when there
|
|
|
|
# isn't any server selection to do, to make the RR easier to read
|
|
|
|
# for humans (less noisy). Note that it is not widely implemented
|
|
|
|
# and should be set to zero.
|
|
|
|
|
|
|
|
attr_reader :weight
|
|
|
|
|
|
|
|
# The port on this target host of this service.
|
|
|
|
#
|
|
|
|
# The range is 0-65535.
|
2005-01-29 00:14:11 -05:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
attr_reader :port
|
|
|
|
|
|
|
|
# The domain name of the target host.
|
|
|
|
#
|
|
|
|
# A target of "." means that the service is decidedly not available
|
|
|
|
# at this domain.
|
|
|
|
|
|
|
|
attr_reader :target
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
2005-01-29 00:14:11 -05:00
|
|
|
msg.put_pack("n", @priority)
|
|
|
|
msg.put_pack("n", @weight)
|
|
|
|
msg.put_pack("n", @port)
|
|
|
|
msg.put_name(@target)
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
2005-01-29 00:14:11 -05:00
|
|
|
priority, = msg.get_unpack("n")
|
|
|
|
weight, = msg.get_unpack("n")
|
|
|
|
port, = msg.get_unpack("n")
|
|
|
|
target = msg.get_name
|
|
|
|
return self.new(priority, weight, port, target)
|
|
|
|
end
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A Resolv::DNS IPv4 address.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class IPv4
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Regular expression IPv4 addresses must match.
|
|
|
|
|
2010-04-18 05:09:46 -04:00
|
|
|
Regex256 = /0
|
|
|
|
|1(?:[0-9][0-9]?)?
|
|
|
|
|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
|
|
|
|
|[3-9][0-9]?/x
|
|
|
|
Regex = /\A(#{Regex256})\.(#{Regex256})\.(#{Regex256})\.(#{Regex256})\z/
|
2001-05-30 05:10:30 -04:00
|
|
|
|
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when IPv4
|
|
|
|
return arg
|
|
|
|
when Regex
|
|
|
|
if (0..255) === (a = $1.to_i) &&
|
|
|
|
(0..255) === (b = $2.to_i) &&
|
|
|
|
(0..255) === (c = $3.to_i) &&
|
|
|
|
(0..255) === (d = $4.to_i)
|
|
|
|
return self.new([a, b, c, d].pack("CCCC"))
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("IPv4 address with invalid value: " + arg)
|
|
|
|
end
|
|
|
|
else
|
2004-03-29 02:54:38 -05:00
|
|
|
raise ArgumentError.new("cannot interpret as IPv4 address: #{arg.inspect}")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def initialize(address) # :nodoc:
|
2010-01-02 11:31:00 -05:00
|
|
|
unless address.kind_of?(String)
|
|
|
|
raise ArgumentError, 'IPv4 address must be a string'
|
|
|
|
end
|
|
|
|
unless address.length == 4
|
|
|
|
raise ArgumentError, "IPv4 address expects 4 bytes but #{address.length} bytes"
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
@address = address
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
2008-06-04 05:37:38 -04:00
|
|
|
# A String representation of this IPv4 address.
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The raw IPv4 address as a String.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :address
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def to_s # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC"))
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def inspect # :nodoc:
|
2001-11-01 00:11:24 -05:00
|
|
|
return "#<#{self.class} #{self.to_s}>"
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Turns this IPv4 address into a Resolv::DNS::Name.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def to_name
|
2002-03-12 03:12:32 -05:00
|
|
|
return DNS::Name.create(
|
|
|
|
'%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def ==(other) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @address == other.address
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def eql?(other) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def hash # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @address.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A Resolv::DNS IPv6 address.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
class IPv6
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# IPv6 address format a:b:c:d:e:f:g:h
|
2001-12-18 13:14:08 -05:00
|
|
|
Regex_8Hex = /\A
|
|
|
|
(?:[0-9A-Fa-f]{1,4}:){7}
|
2003-07-23 21:03:28 -04:00
|
|
|
[0-9A-Fa-f]{1,4}
|
2001-12-18 13:14:08 -05:00
|
|
|
\z/x
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Compressed IPv6 address format a::b
|
|
|
|
|
2001-12-18 13:14:08 -05:00
|
|
|
Regex_CompressedHex = /\A
|
|
|
|
((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
|
|
|
|
((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
|
|
|
|
\z/x
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# IPv4 mapped IPv6 address format a:b:c:d:e:f:w.x.y.z
|
|
|
|
|
2001-12-18 13:14:08 -05:00
|
|
|
Regex_6Hex4Dec = /\A
|
|
|
|
((?:[0-9A-Fa-f]{1,4}:){6,6})
|
|
|
|
(\d+)\.(\d+)\.(\d+)\.(\d+)
|
|
|
|
\z/x
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Compressed IPv4 mapped IPv6 address format a::b:w.x.y.z
|
|
|
|
|
2001-12-18 13:14:08 -05:00
|
|
|
Regex_CompressedHex4Dec = /\A
|
|
|
|
((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
|
|
|
|
((?:[0-9A-Fa-f]{1,4}:)*)
|
|
|
|
(\d+)\.(\d+)\.(\d+)\.(\d+)
|
|
|
|
\z/x
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# A composite IPv6 address Regexp.
|
|
|
|
|
2001-12-18 13:14:08 -05:00
|
|
|
Regex = /
|
2004-04-23 01:22:25 -04:00
|
|
|
(?:#{Regex_8Hex}) |
|
|
|
|
(?:#{Regex_CompressedHex}) |
|
|
|
|
(?:#{Regex_6Hex4Dec}) |
|
|
|
|
(?:#{Regex_CompressedHex4Dec})/x
|
2001-05-30 05:10:30 -04:00
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Creates a new IPv6 address from +arg+ which may be:
|
|
|
|
#
|
|
|
|
# IPv6:: returns +arg+.
|
|
|
|
# String:: +arg+ must match one of the IPv6::Regex* constants
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when IPv6
|
|
|
|
return arg
|
|
|
|
when String
|
|
|
|
address = ''
|
|
|
|
if Regex_8Hex =~ arg
|
|
|
|
arg.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
|
|
|
|
elsif Regex_CompressedHex =~ arg
|
|
|
|
prefix = $1
|
|
|
|
suffix = $2
|
|
|
|
a1 = ''
|
|
|
|
a2 = ''
|
|
|
|
prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
|
|
|
|
suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
|
|
|
|
omitlen = 16 - a1.length - a2.length
|
|
|
|
address << a1 << "\0" * omitlen << a2
|
|
|
|
elsif Regex_6Hex4Dec =~ arg
|
|
|
|
prefix, a, b, c, d = $1, $2.to_i, $3.to_i, $4.to_i, $5.to_i
|
|
|
|
if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d
|
|
|
|
prefix.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
|
|
|
|
address << [a, b, c, d].pack('CCCC')
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("not numeric IPv6 address: " + arg)
|
|
|
|
end
|
|
|
|
elsif Regex_CompressedHex4Dec =~ arg
|
|
|
|
prefix, suffix, a, b, c, d = $1, $2, $3.to_i, $4.to_i, $5.to_i, $6.to_i
|
|
|
|
if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d
|
|
|
|
a1 = ''
|
|
|
|
a2 = ''
|
|
|
|
prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
|
|
|
|
suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
|
|
|
|
omitlen = 12 - a1.length - a2.length
|
|
|
|
address << a1 << "\0" * omitlen << a2 << [a, b, c, d].pack('CCCC')
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("not numeric IPv6 address: " + arg)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("not numeric IPv6 address: " + arg)
|
|
|
|
end
|
|
|
|
return IPv6.new(address)
|
|
|
|
else
|
2004-03-29 02:54:38 -05:00
|
|
|
raise ArgumentError.new("cannot interpret as IPv6 address: #{arg.inspect}")
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def initialize(address) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
unless address.kind_of?(String) && address.length == 16
|
2003-04-21 17:14:08 -04:00
|
|
|
raise ArgumentError.new('IPv6 address must be 16 bytes')
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
@address = address
|
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The raw IPv6 address as a String.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
attr_reader :address
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def to_s # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
address = sprintf("%X:%X:%X:%X:%X:%X:%X:%X", *@address.unpack("nnnnnnnn"))
|
|
|
|
unless address.sub!(/(^|:)0(:0)+(:|$)/, '::')
|
|
|
|
address.sub!(/(^|:)0(:|$)/, '::')
|
|
|
|
end
|
|
|
|
return address
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def inspect # :nodoc:
|
2001-11-01 00:11:24 -05:00
|
|
|
return "#<#{self.class} #{self.to_s}>"
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Turns this IPv6 address into a Resolv::DNS::Name.
|
|
|
|
#--
|
|
|
|
# ip6.arpa should be searched too. [RFC3152]
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def to_name
|
|
|
|
return DNS::Name.new(
|
2007-12-26 08:50:31 -05:00
|
|
|
@address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa'])
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def ==(other) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @address == other.address
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def eql?(other) # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def hash # :nodoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
return @address.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
##
|
|
|
|
# Default resolver to use for Resolv class methods.
|
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
DefaultResolver = self.new
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Address Regexp to use for matching IP addresses.
|
|
|
|
|
2004-04-23 01:22:25 -04:00
|
|
|
AddressRegex = /(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
2005-06-09 18:47:35 -04:00
|
|
|
|