2015-11-10 06:48:14 -05:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 09:27:48 -04:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
require 'socket'
|
|
|
|
require 'timeout'
|
2015-05-06 16:30:43 -04:00
|
|
|
require 'io/wait'
|
2001-05-30 05:10:30 -04:00
|
|
|
|
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
|
2013-07-18 09:50:32 -04: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
|
2016-11-21 18:50:31 -05:00
|
|
|
# p ress.map(&:address)
|
2005-06-09 18:47:35 -04:00
|
|
|
# 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.
|
|
|
|
|
2014-10-07 16:00:09 -04:00
|
|
|
class ResolvTimeout < Timeout::Error; end
|
2005-06-09 18:47:35 -04:00
|
|
|
|
|
|
|
##
|
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
|
2019-07-17 01:45:33 -04:00
|
|
|
if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM and
|
|
|
|
begin
|
|
|
|
require 'win32/resolv'
|
|
|
|
DefaultFileName = Win32::Resolv.get_hosts_path || IO::NULL
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2003-07-23 21:03:28 -04:00
|
|
|
end
|
2019-07-17 01:45:33 -04:00
|
|
|
DefaultFileName ||= '/etc/hosts'
|
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
|
2016-08-30 02:22:30 -04:00
|
|
|
@mutex = Thread::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 = {}
|
2017-12-19 23:18:31 -05:00
|
|
|
File.open(@filename, 'rb') {|f|
|
2001-05-30 05:10:30 -04:00
|
|
|
f.each {|line|
|
|
|
|
line.sub!(/#.*/, '')
|
|
|
|
addr, hostname, *aliases = line.split(/\s+/)
|
|
|
|
next unless addr
|
|
|
|
@addr2name[addr] = [] unless @addr2name.include? addr
|
|
|
|
@addr2name[addr] << hostname
|
|
|
|
@addr2name[addr] += aliases
|
|
|
|
@name2addr[hostname] = [] unless @name2addr.include? hostname
|
|
|
|
@name2addr[hostname] << addr
|
|
|
|
aliases.each {|n|
|
|
|
|
@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
|
2018-01-17 21:44:50 -05:00
|
|
|
@name2addr[name]&.each(&proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
2016-11-23 18:57:30 -05:00
|
|
|
@addr2name[address]&.each(&proc)
|
2001-05-30 05:10:30 -04:00
|
|
|
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)
|
2016-08-30 02:22:30 -04:00
|
|
|
@mutex = Thread::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
|
|
|
|
|
2011-10-22 04:46:12 -04:00
|
|
|
# Sets the resolver timeouts. This may be a single positive number
|
|
|
|
# or an array of positive numbers representing timeouts in seconds.
|
|
|
|
# If an array is specified, a DNS request will retry and wait for
|
|
|
|
# each successive interval in the array until a successful response
|
|
|
|
# is received. Specifying +nil+ reverts to the default timeouts:
|
|
|
|
# [ 5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second ]
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# dns.timeouts = 3
|
|
|
|
#
|
|
|
|
def timeouts=(values)
|
|
|
|
@config.timeouts = values
|
|
|
|
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
|
|
|
|
|
2011-05-13 13:52:01 -04:00
|
|
|
def use_ipv6? # :nodoc:
|
2009-01-31 08:40:05 -05:00
|
|
|
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
|
2019-06-08 23:27:49 -04:00
|
|
|
when IPv4, IPv6
|
|
|
|
ptr = address.to_name
|
2001-05-30 05:10:30 -04:00
|
|
|
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)
|
2013-04-06 09:16:36 -04:00
|
|
|
fetch_resource(name, typeclass) {|reply, reply_name|
|
|
|
|
extract_resources(reply, reply_name, typeclass, &proc)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_resource(name, typeclass)
|
2001-05-30 05:10:30 -04:00
|
|
|
lazy_initialize
|
2019-07-07 21:51:42 -04:00
|
|
|
begin
|
|
|
|
requester = make_udp_requester
|
|
|
|
rescue Errno::EACCES
|
|
|
|
# fall back to TCP
|
|
|
|
end
|
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|
|
2019-07-07 21:51:42 -04:00
|
|
|
requester ||= make_tcp_requester(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]]
|
2014-02-07 10:03:34 -05:00
|
|
|
sender = requester.sender(msg, candidate, nameserver, port)
|
|
|
|
next if !sender
|
|
|
|
senders[[candidate, nameserver, port]] = sender
|
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
|
2013-04-06 09:16:36 -04:00
|
|
|
yield(reply, reply_name)
|
2010-10-29 17:01:53 -04:00
|
|
|
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
|
2019-07-07 21:51:42 -04:00
|
|
|
requester&.close
|
2008-08-07 21:58:40 -04:00
|
|
|
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)
|
2016-11-05 04:33:03 -04:00
|
|
|
msg.each_resource {|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)
|
2016-11-05 04:33:03 -04:00
|
|
|
msg.each_resource {|n, ttl, data|
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
2016-11-05 04:33:03 -04:00
|
|
|
msg.each_resource {|n, ttl, data|
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
|
|
|
|
2011-05-13 13:52:01 -04:00
|
|
|
RequestID = {} # :nodoc:
|
2016-08-30 02:22:30 -04:00
|
|
|
RequestIDMutex = Thread::Mutex.new # :nodoc:
|
2008-08-07 21:58:40 -04:00
|
|
|
|
|
|
|
def self.allocate_request_id(host, port) # :nodoc:
|
|
|
|
id = nil
|
|
|
|
RequestIDMutex.synchronize {
|
|
|
|
h = (RequestID[[host, port]] ||= {})
|
|
|
|
begin
|
2018-03-05 22:31:46 -05:00
|
|
|
id = random(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
|
2018-03-05 22:31:46 -05:00
|
|
|
port = random(1024..65535)
|
2010-02-10 20:29:38 -05:00
|
|
|
udpsock.bind(bind_host, port)
|
2014-02-22 12:38:57 -05:00
|
|
|
rescue Errno::EADDRINUSE, # POSIX
|
|
|
|
Errno::EACCES, # SunOS: See PRIV_SYS_NFS in privileges(5)
|
|
|
|
Errno::EPERM # FreeBSD: security.mac.portacl.port_high is configurable. See mac_portacl(4).
|
2008-08-07 21:58:40 -04:00
|
|
|
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)
|
2015-05-28 21:40:26 -04:00
|
|
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
2011-10-31 08:03:49 -04:00
|
|
|
timelimit = start + tout
|
2013-04-06 10:32:34 -04:00
|
|
|
begin
|
|
|
|
sender.send
|
2015-01-07 17:19:19 -05:00
|
|
|
rescue Errno::EHOSTUNREACH, # multi-homed IPv6 may generate this
|
|
|
|
Errno::ENETUNREACH
|
2013-04-06 10:32:34 -04:00
|
|
|
raise ResolvTimeout
|
|
|
|
end
|
2010-02-24 08:44:08 -05:00
|
|
|
while true
|
2015-05-28 21:40:26 -04:00
|
|
|
before_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
2011-10-31 08:03:49 -04:00
|
|
|
timeout = timelimit - before_select
|
2010-02-24 08:44:08 -05:00
|
|
|
if timeout <= 0
|
|
|
|
raise ResolvTimeout
|
|
|
|
end
|
2015-05-06 16:30:43 -04:00
|
|
|
if @socks.size == 1
|
|
|
|
select_result = @socks[0].wait_readable(timeout) ? [ @socks ] : nil
|
|
|
|
else
|
|
|
|
select_result = IO.select(@socks, nil, nil, timeout)
|
|
|
|
end
|
2010-02-10 20:29:38 -05:00
|
|
|
if !select_result
|
2015-05-28 21:40:26 -04:00
|
|
|
after_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
2011-10-31 08:03:49 -04:00
|
|
|
next if after_select < timelimit
|
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
|
2013-04-06 10:32:34 -04:00
|
|
|
if s = sender_for(from, msg)
|
2008-08-07 21:58:40 -04:00
|
|
|
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
|
|
|
|
|
2013-04-06 10:32:34 -04:00
|
|
|
def sender_for(addr, msg)
|
2020-09-11 17:28:20 -04:00
|
|
|
@senders.delete([addr,msg.id])
|
2013-04-06 10:32:34 -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
|
2016-11-22 08:11:43 -05:00
|
|
|
socks&.each(&:close)
|
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
|
2018-03-05 21:48:17 -05:00
|
|
|
@initialized = false
|
|
|
|
@mutex = Thread::Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_initialize
|
|
|
|
@mutex.synchronize {
|
|
|
|
next if @initialized
|
|
|
|
@initialized = true
|
|
|
|
@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]
|
|
|
|
begin
|
|
|
|
sock = UDPSocket.new(af)
|
|
|
|
rescue Errno::EAFNOSUPPORT
|
|
|
|
next # The kernel doesn't support the address family.
|
|
|
|
end
|
|
|
|
@socks << sock
|
|
|
|
@socks_hash[bind_host] = sock
|
|
|
|
sock.do_not_reverse_lookup = true
|
|
|
|
DNS.bind_random_port(sock, bind_host)
|
|
|
|
}
|
2010-02-10 20:29:38 -05:00
|
|
|
}
|
2018-03-05 21:48:17 -05:00
|
|
|
self
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def recv_reply(readable_socks)
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
2010-02-10 20:29:38 -05:00
|
|
|
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)
|
2020-05-29 17:13:30 -04:00
|
|
|
host = Addrinfo.ip(host).ip_address
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
2014-02-07 10:03:34 -05:00
|
|
|
sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
|
|
|
|
return nil if !sock
|
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')
|
|
|
|
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
|
2018-03-05 21:48:17 -05:00
|
|
|
@mutex.synchronize {
|
|
|
|
if @initialized
|
|
|
|
super
|
|
|
|
@senders.each_key {|service, id|
|
|
|
|
DNS.free_request_id(service[0], service[1], id)
|
|
|
|
}
|
|
|
|
@initialized = false
|
|
|
|
end
|
2008-08-07 21:58:40 -04:00
|
|
|
}
|
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
|
2014-02-07 10:03:34 -05:00
|
|
|
raise "@sock is nil." if @sock.nil?
|
2001-05-30 05:10:30 -04:00
|
|
|
@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
|
2018-03-05 21:48:17 -05:00
|
|
|
@mutex = Thread::Mutex.new
|
|
|
|
@initialized = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_initialize
|
|
|
|
@mutex.synchronize {
|
|
|
|
next if @initialized
|
|
|
|
@initialized = true
|
|
|
|
is_ipv6 = @host.index(':')
|
|
|
|
sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET)
|
|
|
|
@socks = [sock]
|
|
|
|
sock.do_not_reverse_lookup = true
|
|
|
|
DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0")
|
|
|
|
sock.connect(@host, @port)
|
|
|
|
}
|
|
|
|
self
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-02-10 20:29:38 -05:00
|
|
|
def recv_reply(readable_socks)
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
2010-02-10 20:29:38 -05:00
|
|
|
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)
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
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
|
2018-03-05 21:48:17 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
if @initialized
|
|
|
|
super
|
|
|
|
@senders.each_key {|from, id|
|
|
|
|
DNS.free_request_id(@host, @port, id)
|
|
|
|
}
|
|
|
|
@initialized = false
|
|
|
|
end
|
|
|
|
end
|
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
|
2014-02-07 10:03:34 -05:00
|
|
|
raise "@sock is nil." if @sock.nil?
|
2001-05-30 05:10:30 -04:00
|
|
|
@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
|
|
|
|
|
2013-04-06 10:32:34 -04:00
|
|
|
class MDNSOneShot < UnconnectedUDP # :nodoc:
|
|
|
|
def sender(msg, data, host, port=Port)
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
2013-04-06 10:32:34 -04:00
|
|
|
id = DNS.allocate_request_id(host, port)
|
|
|
|
request = msg.encode
|
|
|
|
request[0,2] = [id].pack('n')
|
|
|
|
sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
|
|
|
|
return @senders[id] =
|
|
|
|
UnconnectedUDP::Sender.new(request, data, sock, host, port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender_for(addr, msg)
|
2018-03-05 21:48:17 -05:00
|
|
|
lazy_initialize
|
2013-04-06 10:32:34 -04:00
|
|
|
@senders[msg.id]
|
|
|
|
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]
|
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)
|
2016-08-30 02:22:30 -04:00
|
|
|
@mutex = Thread::Mutex.new
|
2004-05-12 04:44:11 -04:00
|
|
|
@config_info = config_info
|
2003-07-23 21:03:28 -04:00
|
|
|
@initialized = nil
|
2011-10-22 04:46:12 -04:00
|
|
|
@timeouts = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def timeouts=(values)
|
|
|
|
if values
|
|
|
|
values = Array(values)
|
|
|
|
values.each do |t|
|
|
|
|
Numeric === t or raise ArgumentError, "#{t.inspect} is not numeric"
|
2013-12-12 01:17:31 -05:00
|
|
|
t > 0.0 or raise ArgumentError, "timeout=#{t} must be positive"
|
2011-10-22 04:46:12 -04:00
|
|
|
end
|
|
|
|
@timeouts = values
|
|
|
|
else
|
|
|
|
@timeouts = nil
|
|
|
|
end
|
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
|
2017-12-19 23:25:01 -05:00
|
|
|
File.open(filename, 'rb') {|f|
|
2004-05-12 04:44:11 -04:00
|
|
|
f.each {|line|
|
|
|
|
line.sub!(/[#;].*/, '')
|
|
|
|
keyword, *args = line.split(/\s+/)
|
|
|
|
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)})
|
2014-11-21 15:04:27 -05:00
|
|
|
fname = Name.create("#{name}.")
|
|
|
|
if !candidates.include?(fname)
|
|
|
|
candidates << fname
|
|
|
|
end
|
2003-07-23 21:03:28 -04:00
|
|
|
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)
|
2011-10-22 04:46:12 -04:00
|
|
|
timeouts = @timeouts || generate_timeouts
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
2014-12-31 03:50:10 -05:00
|
|
|
# case insensivity of DNS labels doesn't apply non-ASCII characters. [RFC 4343]
|
|
|
|
# This assumes @string is given in ASCII compatible encoding.
|
2014-12-31 09:59:19 -05:00
|
|
|
@downcase = string.b.downcase
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
attr_reader :string, :downcase
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return @string
|
|
|
|
end
|
|
|
|
|
2003-07-23 21:03:28 -04:00
|
|
|
def inspect
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2003-07-23 21:03:28 -04:00
|
|
|
end
|
2001-11-01 00:11:24 -05:00
|
|
|
|
2001-05-30 05:10:30 -04:00
|
|
|
def ==(other)
|
2015-01-01 19:10:04 -05:00
|
|
|
return self.class == other.class && @downcase == other.downcase
|
2001-05-30 05:10:30 -04:00
|
|
|
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:
|
2015-01-01 19:10:04 -05:00
|
|
|
labels = labels.map {|label|
|
|
|
|
case label
|
|
|
|
when String then Label::Str.new(label)
|
|
|
|
when Label::Str then label
|
|
|
|
else
|
|
|
|
raise ArgumentError, "unexpected label: #{label.inspect}"
|
|
|
|
end
|
|
|
|
}
|
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:
|
2014-06-24 04:48:46 -04:00
|
|
|
"#<#{self.class}: #{self}#{@absolute ? '.' : ''}>"
|
2005-02-07 10:24:09 -05:00
|
|
|
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
|
2014-12-30 02:16:14 -05:00
|
|
|
return false unless @absolute == other.absolute?
|
2014-12-30 23:45:05 -05:00
|
|
|
return @labels == other.to_a
|
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
|
2015-10-05 09:27:48 -04:00
|
|
|
@data = ''.dup
|
2001-05-30 05:10:30 -04:00
|
|
|
@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
|
2015-11-09 10:37:04 -05:00
|
|
|
if @data.length < 0x4000
|
|
|
|
@names[domain] = @data.length
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
2017-01-13 21:26:49 -05:00
|
|
|
@limit = data.bytesize
|
2001-05-30 05:10:30 -04:00
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
2010-01-02 11:31:00 -05:00
|
|
|
def inspect
|
2017-01-13 21:26:49 -05:00
|
|
|
"\#<#{self.class}: #{@data.byteslice(0, @index).inspect} #{@data.byteslice(@index..-1).inspect}>"
|
2010-01-02 11:31:00 -05:00
|
|
|
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)
|
2014-02-08 01:29:54 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit < @index + len
|
2017-01-13 21:26:49 -05:00
|
|
|
d = @data.byteslice(@index, len)
|
2001-05-30 05:10:30 -04:00
|
|
|
@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
|
2014-02-08 01:29:54 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit <= @index
|
2017-01-13 21:26:49 -05:00
|
|
|
len = @data.getbyte(@index)
|
2004-03-29 02:54:38 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
|
2017-01-13 21:26:49 -05:00
|
|
|
d = @data.byteslice(@index + 1, len)
|
2001-05-30 05:10:30 -04:00
|
|
|
@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
|
|
|
|
|
2014-02-08 02:35:24 -05:00
|
|
|
def get_labels
|
|
|
|
prev_index = @index
|
|
|
|
save_index = nil
|
2001-05-30 05:10:30 -04:00
|
|
|
d = []
|
|
|
|
while true
|
2014-02-08 01:29:54 -05:00
|
|
|
raise DecodeError.new("limit exceeded") if @limit <= @index
|
2017-01-13 21:26:49 -05:00
|
|
|
case @data.getbyte(@index)
|
2001-05-30 05:10:30 -04:00
|
|
|
when 0
|
|
|
|
@index += 1
|
2014-02-08 02:35:24 -05:00
|
|
|
if save_index
|
|
|
|
@index = save_index
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
return d
|
|
|
|
when 192..255
|
|
|
|
idx = self.get_unpack('n')[0] & 0x3fff
|
2014-02-08 02:35:24 -05:00
|
|
|
if prev_index <= idx
|
2001-05-30 05:10:30 -04:00
|
|
|
raise DecodeError.new("non-backward name pointer")
|
|
|
|
end
|
2014-02-08 02:35:24 -05:00
|
|
|
prev_index = idx
|
|
|
|
if !save_index
|
|
|
|
save_index = @index
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
@index = idx
|
|
|
|
else
|
|
|
|
d << self.get_label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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)
|
2017-01-13 21:26:48 -05:00
|
|
|
res = self.get_length16 do
|
|
|
|
begin
|
|
|
|
typeclass.decode_rdata self
|
|
|
|
rescue => e
|
|
|
|
raise DecodeError, e.message, e.backtrace
|
|
|
|
end
|
|
|
|
end
|
2005-06-15 11:32:46 -04:00
|
|
|
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!
|
2015-02-16 21:47:19 -05:00
|
|
|
s_ivars.delete :@ttl
|
2005-06-15 11:32:46 -04:00
|
|
|
o_ivars = other.instance_variables
|
|
|
|
o_ivars.sort!
|
2015-02-16 21:47:19 -05:00
|
|
|
o_ivars.delete :@ttl
|
2005-06-15 11:32:46 -04:00
|
|
|
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
|
2015-02-16 21:47:19 -05:00
|
|
|
vars.delete :@ttl
|
2005-06-15 11:32:46 -04:00
|
|
|
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
|
|
|
##
|
2014-01-18 09:01:43 -05:00
|
|
|
# Returns the concatenated string from +strings+.
|
2005-06-09 18:47:35 -04:00
|
|
|
|
2005-02-05 13:31:20 -05:00
|
|
|
def data
|
2014-01-18 09:01:43 -05:00
|
|
|
@strings.join("")
|
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
|
|
|
|
|
2013-04-06 10:52:48 -04:00
|
|
|
##
|
|
|
|
# Location resource
|
|
|
|
|
|
|
|
class LOC < Resource
|
|
|
|
|
|
|
|
TypeValue = 29 # :nodoc:
|
|
|
|
|
|
|
|
def initialize(version, ssize, hprecision, vprecision, latitude, longitude, altitude)
|
|
|
|
@version = version
|
|
|
|
@ssize = Resolv::LOC::Size.create(ssize)
|
|
|
|
@hprecision = Resolv::LOC::Size.create(hprecision)
|
|
|
|
@vprecision = Resolv::LOC::Size.create(vprecision)
|
|
|
|
@latitude = Resolv::LOC::Coord.create(latitude)
|
|
|
|
@longitude = Resolv::LOC::Coord.create(longitude)
|
|
|
|
@altitude = Resolv::LOC::Alt.create(altitude)
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the version value for this LOC record which should always be 00
|
|
|
|
|
|
|
|
attr_reader :version
|
|
|
|
|
|
|
|
##
|
|
|
|
# The spherical size of this LOC
|
|
|
|
# in meters using scientific notation as 2 integers of XeY
|
|
|
|
|
|
|
|
attr_reader :ssize
|
|
|
|
|
|
|
|
##
|
|
|
|
# The horizontal precision using ssize type values
|
|
|
|
# in meters using scientific notation as 2 integers of XeY
|
|
|
|
# for precision use value/2 e.g. 2m = +/-1m
|
|
|
|
|
|
|
|
attr_reader :hprecision
|
|
|
|
|
|
|
|
##
|
|
|
|
# The vertical precision using ssize type values
|
|
|
|
# in meters using scientific notation as 2 integers of XeY
|
|
|
|
# for precision use value/2 e.g. 2m = +/-1m
|
|
|
|
|
|
|
|
attr_reader :vprecision
|
|
|
|
|
|
|
|
##
|
|
|
|
# The latitude for this LOC where 2**31 is the equator
|
|
|
|
# in thousandths of an arc second as an unsigned 32bit integer
|
|
|
|
|
|
|
|
attr_reader :latitude
|
|
|
|
|
|
|
|
##
|
|
|
|
# The longitude for this LOC where 2**31 is the prime meridian
|
|
|
|
# in thousandths of an arc second as an unsigned 32bit integer
|
|
|
|
|
|
|
|
attr_reader :longitude
|
|
|
|
|
|
|
|
##
|
|
|
|
# The altitude of the LOC above a reference sphere whose surface sits 100km below the WGS84 spheroid
|
|
|
|
# in centimeters as an unsigned 32bit integer
|
|
|
|
|
|
|
|
attr_reader :altitude
|
|
|
|
|
|
|
|
|
|
|
|
def encode_rdata(msg) # :nodoc:
|
|
|
|
msg.put_bytes(@version)
|
|
|
|
msg.put_bytes(@ssize.scalar)
|
|
|
|
msg.put_bytes(@hprecision.scalar)
|
|
|
|
msg.put_bytes(@vprecision.scalar)
|
|
|
|
msg.put_bytes(@latitude.coordinates)
|
|
|
|
msg.put_bytes(@longitude.coordinates)
|
|
|
|
msg.put_bytes(@altitude.altitude)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.decode_rdata(msg) # :nodoc:
|
|
|
|
version = msg.get_bytes(1)
|
|
|
|
ssize = msg.get_bytes(1)
|
|
|
|
hprecision = msg.get_bytes(1)
|
|
|
|
vprecision = msg.get_bytes(1)
|
|
|
|
latitude = msg.get_bytes(4)
|
|
|
|
longitude = msg.get_bytes(4)
|
|
|
|
altitude = msg.get_bytes(4)
|
|
|
|
return self.new(
|
|
|
|
version,
|
|
|
|
Resolv::LOC::Size.new(ssize),
|
|
|
|
Resolv::LOC::Size.new(hprecision),
|
|
|
|
Resolv::LOC::Size.new(vprecision),
|
|
|
|
Resolv::LOC::Coord.new(latitude,"lat"),
|
|
|
|
Resolv::LOC::Coord.new(longitude,"lon"),
|
|
|
|
Resolv::LOC::Alt.new(altitude)
|
|
|
|
)
|
|
|
|
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:
|
2013-04-06 10:52:48 -04:00
|
|
|
NS, CNAME, SOA, PTR, HINFO, MINFO, MX, TXT, LOC, ANY
|
2001-05-30 05:10:30 -04:00
|
|
|
]
|
|
|
|
|
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:
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2001-11-01 00:11:24 -05:00
|
|
|
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
|
|
|
|
|
2020-08-24 15:39:08 -04:00
|
|
|
##
|
|
|
|
# IPv6 link local address format fe80:b:c:d:e:f:g:h%em1
|
|
|
|
Regex_8HexLinkLocal = /\A
|
2020-11-09 02:16:30 -05:00
|
|
|
[Ff][Ee]80
|
2020-08-24 15:39:08 -04:00
|
|
|
(?::[0-9A-Fa-f]{1,4}){7}
|
|
|
|
%[0-9A-Za-z]+
|
|
|
|
\z/x
|
|
|
|
|
|
|
|
##
|
|
|
|
# Compressed IPv6 link local address format fe80::b%em1
|
|
|
|
|
|
|
|
Regex_CompressedHexLinkLocal = /\A
|
2020-11-09 02:16:30 -05:00
|
|
|
[Ff][Ee]80:
|
2020-08-24 15:39:08 -04:00
|
|
|
(?:
|
|
|
|
((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
|
|
|
|
((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
|
|
|
|
|
|
|
|
|
:((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
|
|
|
|
)?
|
2020-11-08 02:41:01 -05:00
|
|
|
:[0-9A-Fa-f]{1,4}%[0-9A-Za-z.]+
|
2020-08-24 15:39:08 -04:00
|
|
|
\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}) |
|
2020-08-24 15:39:08 -04:00
|
|
|
(?:#{Regex_CompressedHex4Dec}) |
|
|
|
|
(?:#{Regex_8HexLinkLocal}) |
|
|
|
|
(?:#{Regex_CompressedHexLinkLocal})
|
|
|
|
/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
|
2015-12-28 15:31:10 -05:00
|
|
|
address = ''.b
|
2001-05-30 05:10:30 -04:00
|
|
|
if Regex_8Hex =~ arg
|
|
|
|
arg.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
|
|
|
|
elsif Regex_CompressedHex =~ arg
|
|
|
|
prefix = $1
|
|
|
|
suffix = $2
|
2015-12-28 15:31:10 -05:00
|
|
|
a1 = ''.b
|
|
|
|
a2 = ''.b
|
2001-05-30 05:10:30 -04:00
|
|
|
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
|
2015-12-28 15:31:10 -05:00
|
|
|
a1 = ''.b
|
|
|
|
a2 = ''.b
|
2001-05-30 05:10:30 -04:00
|
|
|
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:
|
2019-07-29 22:59:54 -04:00
|
|
|
address = sprintf("%x:%x:%x:%x:%x:%x:%x:%x", *@address.unpack("nnnnnnnn"))
|
2001-05-30 05:10:30 -04:00
|
|
|
unless address.sub!(/(^|:)0(:0)+(:|$)/, '::')
|
|
|
|
address.sub!(/(^|:)0(:|$)/, '::')
|
|
|
|
end
|
|
|
|
return address
|
|
|
|
end
|
|
|
|
|
2005-06-09 18:47:35 -04:00
|
|
|
def inspect # :nodoc:
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2001-11-01 00:11:24 -05:00
|
|
|
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
|
|
|
|
|
2013-04-06 10:32:34 -04:00
|
|
|
##
|
|
|
|
# Resolv::MDNS is a one-shot Multicast DNS (mDNS) resolver. It blindly
|
|
|
|
# makes queries to the mDNS addresses without understanding anything about
|
|
|
|
# multicast ports.
|
|
|
|
#
|
|
|
|
# Information taken form the following places:
|
|
|
|
#
|
|
|
|
# * RFC 6762
|
|
|
|
|
|
|
|
class MDNS < DNS
|
|
|
|
|
|
|
|
##
|
|
|
|
# Default mDNS Port
|
|
|
|
|
|
|
|
Port = 5353
|
|
|
|
|
|
|
|
##
|
|
|
|
# Default IPv4 mDNS address
|
|
|
|
|
|
|
|
AddressV4 = '224.0.0.251'
|
|
|
|
|
|
|
|
##
|
|
|
|
# Default IPv6 mDNS address
|
|
|
|
|
|
|
|
AddressV6 = 'ff02::fb'
|
|
|
|
|
|
|
|
##
|
|
|
|
# Default mDNS addresses
|
|
|
|
|
|
|
|
Addresses = [
|
|
|
|
[AddressV4, Port],
|
|
|
|
[AddressV6, Port],
|
|
|
|
]
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new one-shot Multicast DNS (mDNS) resolver.
|
|
|
|
#
|
|
|
|
# +config_info+ can be:
|
|
|
|
#
|
|
|
|
# nil::
|
|
|
|
# Uses the default mDNS addresses
|
|
|
|
#
|
|
|
|
# Hash::
|
|
|
|
# Must contain :nameserver or :nameserver_port like
|
|
|
|
# Resolv::DNS#initialize.
|
|
|
|
|
|
|
|
def initialize(config_info=nil)
|
|
|
|
if config_info then
|
|
|
|
super({ nameserver_port: Addresses }.merge(config_info))
|
|
|
|
else
|
|
|
|
super(nameserver_port: Addresses)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Iterates over all IP addresses for +name+ retrieved from the mDNS
|
|
|
|
# resolver, provided name ends with "local". If the name does not end in
|
|
|
|
# "local" no records will be returned.
|
|
|
|
#
|
|
|
|
# +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
|
|
|
|
# be a Resolv::IPv4 or Resolv::IPv6
|
|
|
|
|
|
|
|
def each_address(name)
|
|
|
|
name = Resolv::DNS::Name.create(name)
|
|
|
|
|
2018-01-11 13:26:49 -05:00
|
|
|
return unless name[-1].to_s == 'local'
|
2013-04-06 10:32:34 -04:00
|
|
|
|
|
|
|
super(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_udp_requester # :nodoc:
|
|
|
|
nameserver_port = @config.nameserver_port
|
|
|
|
Requester::MDNSOneShot.new(*nameserver_port)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-06 10:52:48 -04:00
|
|
|
module LOC
|
|
|
|
|
|
|
|
##
|
|
|
|
# A Resolv::LOC::Size
|
|
|
|
|
|
|
|
class Size
|
|
|
|
|
|
|
|
Regex = /^(\d+\.*\d*)[m]$/
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new LOC::Size from +arg+ which may be:
|
|
|
|
#
|
|
|
|
# LOC::Size:: returns +arg+.
|
|
|
|
# String:: +arg+ must match the LOC::Size::Regex constant
|
|
|
|
|
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when Size
|
|
|
|
return arg
|
|
|
|
when String
|
|
|
|
scalar = ''
|
|
|
|
if Regex =~ arg
|
|
|
|
scalar = [(($1.to_f*(1e2)).to_i.to_s[0].to_i*(2**4)+(($1.to_f*(1e2)).to_i.to_s.length-1))].pack("C")
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("not a properly formed Size string: " + arg)
|
|
|
|
end
|
|
|
|
return Size.new(scalar)
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("cannot interpret as Size: #{arg.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(scalar)
|
|
|
|
@scalar = scalar
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The raw size
|
|
|
|
|
|
|
|
attr_reader :scalar
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
|
|
|
s = @scalar.unpack("H2").join.to_s
|
|
|
|
return ((s[0].to_i)*(10**(s[1].to_i-2))).to_s << "m"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect # :nodoc:
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2013-04-06 10:52:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other) # :nodoc:
|
|
|
|
return @scalar == other.scalar
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other) # :nodoc:
|
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash # :nodoc:
|
|
|
|
return @scalar.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A Resolv::LOC::Coord
|
|
|
|
|
|
|
|
class Coord
|
|
|
|
|
|
|
|
Regex = /^(\d+)\s(\d+)\s(\d+\.\d+)\s([NESW])$/
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new LOC::Coord from +arg+ which may be:
|
|
|
|
#
|
|
|
|
# LOC::Coord:: returns +arg+.
|
|
|
|
# String:: +arg+ must match the LOC::Coord::Regex constant
|
|
|
|
|
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when Coord
|
|
|
|
return arg
|
|
|
|
when String
|
|
|
|
coordinates = ''
|
2016-11-05 11:19:21 -04:00
|
|
|
if Regex =~ arg && $1.to_f < 180
|
|
|
|
m = $~
|
|
|
|
hemi = (m[4][/[NE]/]) || (m[4][/[SW]/]) ? 1 : -1
|
|
|
|
coordinates = [ ((m[1].to_i*(36e5)) + (m[2].to_i*(6e4)) +
|
|
|
|
(m[3].to_f*(1e3))) * hemi+(2**31) ].pack("N")
|
|
|
|
orientation = m[4][/[NS]/] ? 'lat' : 'lon'
|
2013-04-06 10:52:48 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError.new("not a properly formed Coord string: " + arg)
|
|
|
|
end
|
|
|
|
return Coord.new(coordinates,orientation)
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("cannot interpret as Coord: #{arg.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(coordinates,orientation)
|
|
|
|
unless coordinates.kind_of?(String)
|
|
|
|
raise ArgumentError.new("Coord must be a 32bit unsigned integer in hex format: #{coordinates.inspect}")
|
|
|
|
end
|
|
|
|
unless orientation.kind_of?(String) && orientation[/^lon$|^lat$/]
|
|
|
|
raise ArgumentError.new('Coord expects orientation to be a String argument of "lat" or "lon"')
|
|
|
|
end
|
|
|
|
@coordinates = coordinates
|
|
|
|
@orientation = orientation
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The raw coordinates
|
|
|
|
|
|
|
|
attr_reader :coordinates
|
|
|
|
|
|
|
|
## The orientation of the hemisphere as 'lat' or 'lon'
|
|
|
|
|
|
|
|
attr_reader :orientation
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
|
|
|
c = @coordinates.unpack("N").join.to_i
|
|
|
|
val = (c - (2**31)).abs
|
|
|
|
fracsecs = (val % 1e3).to_i.to_s
|
|
|
|
val = val / 1e3
|
|
|
|
secs = (val % 60).to_i.to_s
|
|
|
|
val = val / 60
|
|
|
|
mins = (val % 60).to_i.to_s
|
|
|
|
degs = (val / 60).to_i.to_s
|
|
|
|
posi = (c >= 2**31)
|
|
|
|
case posi
|
|
|
|
when true
|
|
|
|
hemi = @orientation[/^lat$/] ? "N" : "E"
|
|
|
|
else
|
|
|
|
hemi = @orientation[/^lon$/] ? "W" : "S"
|
|
|
|
end
|
|
|
|
return degs << " " << mins << " " << secs << "." << fracsecs << " " << hemi
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect # :nodoc:
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2013-04-06 10:52:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other) # :nodoc:
|
|
|
|
return @coordinates == other.coordinates
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other) # :nodoc:
|
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash # :nodoc:
|
|
|
|
return @coordinates.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A Resolv::LOC::Alt
|
|
|
|
|
|
|
|
class Alt
|
|
|
|
|
|
|
|
Regex = /^([+-]*\d+\.*\d*)[m]$/
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new LOC::Alt from +arg+ which may be:
|
|
|
|
#
|
|
|
|
# LOC::Alt:: returns +arg+.
|
|
|
|
# String:: +arg+ must match the LOC::Alt::Regex constant
|
|
|
|
|
|
|
|
def self.create(arg)
|
|
|
|
case arg
|
|
|
|
when Alt
|
|
|
|
return arg
|
|
|
|
when String
|
|
|
|
altitude = ''
|
|
|
|
if Regex =~ arg
|
|
|
|
altitude = [($1.to_f*(1e2))+(1e7)].pack("N")
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("not a properly formed Alt string: " + arg)
|
|
|
|
end
|
|
|
|
return Alt.new(altitude)
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("cannot interpret as Alt: #{arg.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(altitude)
|
|
|
|
@altitude = altitude
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The raw altitude
|
|
|
|
|
|
|
|
attr_reader :altitude
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
|
|
|
a = @altitude.unpack("N").join.to_i
|
|
|
|
return ((a.to_f/1e2)-1e5).to_s + "m"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect # :nodoc:
|
2014-06-24 04:48:46 -04:00
|
|
|
return "#<#{self.class} #{self}>"
|
2013-04-06 10:52:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other) # :nodoc:
|
|
|
|
return @altitude == other.altitude
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other) # :nodoc:
|
|
|
|
return self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash # :nodoc:
|
|
|
|
return @altitude.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-04-06 10:32:34 -04:00
|
|
|
##
|
|
|
|
# Replaces the resolvers in the default resolver with +new_resolvers+. This
|
|
|
|
# allows resolvers to be changed for resolv-replace.
|
|
|
|
|
|
|
|
def DefaultResolver.replace_resolvers new_resolvers
|
|
|
|
@resolvers = new_resolvers
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|