/vendor/net-mdns-0.4/lib/net/dns/resolv.rb
Ruby | 2012 lines | 1514 code | 187 blank | 311 comment | 98 complexity | 062f0bdebffaea05d2e5b28c8176a8d1 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- # net/dns/resolv.rb is a copy of resolv.rb from the ruby library, where it is
- # maintained by Tanaka Akira.
- #
- # It contains modifications I found necessary, some of which have been accepted
- # into ruby 1.8's cvs, and others that I hope will be accepted.
- #
- # net/dns/resolvx.rb contains extensions to resolv.rb (as opposed to modifications),
- # some of these may also be worth accepting into the standard library.
- #
- # Note that until net/dns/resolv.rb is accepted AND released in ruby 1.8.x's
- # resolv.rb I still need a copy in net-mdns. Without it, it would be necessary
- # to install ruby from CVS in order to use net-mdns.
- #
- # = Bug fixes/Required changes
- # - resolv-replace.rb: IPSocket#getaddress fails when passed a Fixnum, such as when
- # calling UDPSocket#bind(Socket:INADDR_ANY, 5353)
- # - MessageEncoder#put_string: would silently create a garbage record if string was
- # longer than 255 characters.
- # - TXT.new: correctly deal with TXT records longer than 255 characters.
- # - TXT#data: correctly concatenate strings into a TXT record longer than 255 characters.
- # - Message#encode/Message#decode: question and answer arrays now contain the
- # mDNS unicast and cacheflush bit, respectively. All APIs, including
- # #each_question and #each_answer, are backwards compatible.
- # - A.new(A#address) failed because IPv4.create() wouldn't accept an address in the
- # form of A#address (4 bytes in network byte order).
- #
- # = Ease-of-use changes
- #
- # - partial rdocifying
- # - Str#inspect: difficult to notice whitespace at beginning of string, added quotes.
- # - Name#==: allow arg to be String or Name, as does Name#create.
- # - Name#subdomain_of?: allow arg to be String or Name, as does Name#create.
- # - Name#subdomain_of?: disregard absolute, it doesn't make sense that:
- # www.example.com subdomain_of? www.example.com. => false
- # www.example.com subdomain_of? www.example.com => true
- # If you can't compare a variable thing to a known thing.. how can you compare a
- # variable thing to a variable thing?
- #
- # I had a lot of bugs using Name comparison related to trailing dots. Name#==
- # is almost impossible to use correctly when comparing against an other which
- # is a Name or a String, and may have come from a DNS Message (in which case it
- # will be absolute), or from input from a user, in which case they probably did
- # not type the trailing dot.
- =begin
- = resolv library
- resolv.rb is a resolver library written in Ruby.
- Since it is written in Ruby, it is thread-aware.
- I.e. it can resolv many hostnames concurrently.
- It is possible to lookup various resources of DNS using DNS module directly.
- == example
- p Resolv.getaddress("www.ruby-lang.org")
- p Resolv.getname("210.251.121.214")
- Resolv::DNS.open {|dns|
- p dns.getresources("www.ruby-lang.org", Resolv::DNS::Resource::IN::A).collect {|r| r.address}
- p dns.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX).collect {|r| [r.exchange.to_s, r.preference]}
- }
- == Resolv class
- === class methods
- --- Resolv.getaddress(name)
- --- Resolv.getaddresses(name)
- --- Resolv.each_address(name) {|address| ...}
- They lookups IP addresses of ((|name|)) which represents a hostname
- as a string by default resolver.
- getaddress returns first entry of lookupped addresses.
- getaddresses returns lookupped addresses as an array.
- each_address iterates over lookupped addresses.
- --- Resolv.getname(address)
- --- Resolv.getnames(address)
- --- Resolv.each_name(address) {|name| ...}
- lookups hostnames of ((|address|)) which represents IP address as a string.
- getname returns first entry of lookupped names.
- getnames returns lookupped names as an array.
- each_names iterates over lookupped names.
- == Resolv::Hosts class
- hostname resolver using /etc/hosts format.
- === class methods
- --- Resolv::Hosts.new(hosts='/etc/hosts')
- === methods
- --- Resolv::Hosts#getaddress(name)
- --- Resolv::Hosts#getaddresses(name)
- --- Resolv::Hosts#each_address(name) {|address| ...}
- address lookup methods.
- --- Resolv::Hosts#getname(address)
- --- Resolv::Hosts#getnames(address)
- --- Resolv::Hosts#each_name(address) {|name| ...}
- hostnames lookup methods.
- == Resolv::DNS class
- DNS stub resolver.
- === class methods
- --- Resolv::DNS.new(config_info=nil)
- ((|config_info|)) should be nil, a string or a hash.
- If nil is given, /etc/resolv.conf and platform specific information is used.
- If a string is given, it should be a filename which format is same as /etc/resolv.conf.
- If a hash is given, it may contains information for nameserver, search and ndots as follows.
- Resolv::DNS.new({:nameserver=>["210.251.121.21"], :search=>["ruby-lang.org"], :ndots=>1})
- --- Resolv::DNS.open(config_info=nil)
- --- Resolv::DNS.open(config_info=nil) {|dns| ...}
- === methods
- --- Resolv::DNS#close
- --- Resolv::DNS#getaddress(name)
- --- Resolv::DNS#getaddresses(name)
- --- Resolv::DNS#each_address(name) {|address| ...}
- address lookup methods.
- ((|name|)) must be a instance of Resolv::DNS::Name or String. Lookupped
- address is represented as an instance of Resolv::IPv4 or Resolv::IPv6.
- --- Resolv::DNS#getname(address)
- --- Resolv::DNS#getnames(address)
- --- Resolv::DNS#each_name(address) {|name| ...}
- hostnames lookup methods.
- ((|address|)) must be a instance of Resolv::IPv4, Resolv::IPv6 or String.
- Lookupped name is represented as an instance of Resolv::DNS::Name.
- --- Resolv::DNS#getresource(name, typeclass)
- --- Resolv::DNS#getresources(name, typeclass)
- --- Resolv::DNS#each_resource(name, typeclass) {|resource| ...}
- They lookup DNS resources of ((|name|)).
- ((|name|)) must be a instance of Resolv::Name or String.
- ((|typeclass|)) should be one of follows:
- * Resolv::DNS::Resource::IN::ANY
- * Resolv::DNS::Resource::IN::NS
- * Resolv::DNS::Resource::IN::CNAME
- * Resolv::DNS::Resource::IN::SOA
- * Resolv::DNS::Resource::IN::HINFO
- * Resolv::DNS::Resource::IN::MINFO
- * Resolv::DNS::Resource::IN::MX
- * Resolv::DNS::Resource::IN::TXT
- * Resolv::DNS::Resource::IN::ANY
- * Resolv::DNS::Resource::IN::A
- * Resolv::DNS::Resource::IN::WKS
- * Resolv::DNS::Resource::IN::PTR
- * Resolv::DNS::Resource::IN::SRV
- * Resolv::DNS::Resource::IN::AAAA
- Lookupped resource is represented as an instance of (a subclass of)
- Resolv::DNS::Resource.
- (Resolv::DNS::Resource::IN::A, etc.)
- == Resolv::DNS::Resource::IN::NS class
- --- name
- == Resolv::DNS::Resource::IN::CNAME class
- --- name
- == Resolv::DNS::Resource::IN::SOA class
- --- mname
- --- rname
- --- serial
- --- refresh
- --- retry
- --- expire
- --- minimum
- == Resolv::DNS::Resource::IN::HINFO class
- --- cpu
- --- os
- == Resolv::DNS::Resource::IN::MINFO class
- --- rmailbx
- --- emailbx
- == Resolv::DNS::Resource::IN::MX class
- --- preference
- --- exchange
- == Resolv::DNS::Resource::IN::TXT class
- --- data
- == Resolv::DNS::Resource::IN::A class
- --- address
- == Resolv::DNS::Resource::IN::WKS class
- --- address
- --- protocol
- --- bitmap
- == Resolv::DNS::Resource::IN::PTR class
- --- name
- == Resolv::DNS::Resource::IN::AAAA class
- --- address
- == Resolv::DNS::Name class
- === class methods
- --- Resolv::DNS::Name.create(name)
- === methods
- --- Resolv::DNS::Name#to_s
- == Resolv::DNS::Resource class
- == Resolv::IPv4 class
- === class methods
- --- Resolv::IPv4.create(address)
- === methods
- --- Resolv::IPv4#to_s
- --- Resolv::IPv4#to_name
- === constants
- --- Resolv::IPv4::Regex
- regular expression for IPv4 address.
- == Resolv::IPv6 class
- === class methods
- --- Resolv::IPv6.create(address)
- === methods
- --- Resolv::IPv6#to_s
- --- Resolv::IPv6#to_name
- === constants
- --- Resolv::IPv6::Regex
- regular expression for IPv6 address.
- == Bugs
- * NIS is not supported.
- * /etc/nsswitch.conf is not supported.
- * IPv6 is not supported.
- =end
- require 'socket'
- require 'fcntl'
- require 'timeout'
- require 'thread'
- class Resolv
- def self.getaddress(name)
- DefaultResolver.getaddress(name)
- end
- def self.getaddresses(name)
- DefaultResolver.getaddresses(name)
- end
- def self.each_address(name, &block)
- DefaultResolver.each_address(name, &block)
- end
- def self.getname(address)
- DefaultResolver.getname(address)
- end
- def self.getnames(address)
- DefaultResolver.getnames(address)
- end
- def self.each_name(address, &proc)
- DefaultResolver.each_name(address, &proc)
- end
- def initialize(resolvers=[Hosts.new, DNS.new])
- @resolvers = resolvers
- end
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("no address for #{name}")
- end
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- def each_address(name)
- if AddressRegex =~ name
- yield name
- return
- end
- yielded = false
- @resolvers.each {|r|
- r.each_address(name) {|address|
- yield address.to_s
- yielded = true
- }
- return if yielded
- }
- end
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("no name for #{address}")
- end
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- def each_name(address)
- yielded = false
- @resolvers.each {|r|
- r.each_name(address) {|name|
- yield name.to_s
- yielded = true
- }
- return if yielded
- }
- end
- class ResolvError < StandardError
- end
- class ResolvTimeout < TimeoutError
- end
- # Resolves names and addresses using the hosts file, "/etc/hosts" or
- # whatever it is on Windows.
- class Hosts
- if /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
- require 'win32/resolv'
- DefaultFileName = Win32::Resolv.get_hosts_path
- else
- DefaultFileName = '/etc/hosts'
- end
- def initialize(filename = DefaultFileName)
- @filename = filename
- @mutex = Mutex.new
- @initialized = nil
- end
- def lazy_initialize # :nodoc:
- @mutex.synchronize {
- unless @initialized
- @name2addr = {}
- @addr2name = {}
- open(@filename) {|f|
- f.each {|line|
- line.sub!(/#.*/, '')
- addr, hostname, *aliases = line.split(/\s+/)
- next unless addr
- addr.untaint
- hostname.untaint
- @addr2name[addr] = [] unless @addr2name.include? addr
- @addr2name[addr] << hostname
- @addr2name[addr] += aliases
- @name2addr[hostname] = [] unless @name2addr.include? hostname
- @name2addr[hostname] << addr
- aliases.each {|n|
- n.untaint
- @name2addr[n] = [] unless @name2addr.include? n
- @name2addr[n] << addr
- }
- }
- }
- @name2addr.each {|name, arr| arr.reverse!}
- @initialized = true
- end
- }
- self
- end
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("#{@filename} has no name: #{name}")
- end
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- def each_address(name, &proc)
- lazy_initialize
- if @name2addr.include?(name)
- @name2addr[name].each(&proc)
- end
- end
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("#{@filename} has no address: #{address}")
- end
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- def each_name(address, &proc)
- lazy_initialize
- if @addr2name.include?(address)
- @addr2name[address].each(&proc)
- end
- end
- end
- class DNS
- # STD0013 (RFC 1035, etc.)
- # ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
- Port = 53
- UDPSize = 512
- DNSThreadGroup = ThreadGroup.new # :nodoc:
- def self.open(*args)
- dns = new(*args)
- return dns unless block_given?
- begin
- yield dns
- ensure
- dns.close
- end
- end
- def initialize(config_info=nil)
- @mutex = Mutex.new
- @config = Config.new(config_info)
- @initialized = nil
- end
- def lazy_initialize # :nodoc:
- @mutex.synchronize {
- unless @initialized
- @config.lazy_initialize
- if nameserver = @config.single?
- @requester = Requester::ConnectedUDP.new(nameserver)
- else
- @requester = Requester::UnconnectedUDP.new
- end
- @initialized = true
- end
- }
- self
- end
- def close
- @mutex.synchronize {
- if @initialized
- @requester.close if @requester
- @requester = nil
- @initialized = false
- end
- }
- end
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("DNS result has no information for #{name}")
- end
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- def each_address(name)
- each_resource(name, Resource::IN::A) {|resource| yield resource.address}
- end
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("DNS result has no information for #{address}")
- end
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- def each_name(address)
- case address
- when Name
- ptr = address
- when IPv4::Regex
- ptr = IPv4.create(address).to_name
- when IPv6::Regex
- ptr = IPv6.create(address).to_name
- else
- raise ResolvError.new("cannot interpret as address: #{address}")
- end
- each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
- end
- def getresource(name, typeclass)
- each_resource(name, typeclass) {|resource| return resource}
- raise ResolvError.new("DNS result has no information for #{name}")
- end
- def getresources(name, typeclass)
- ret = []
- each_resource(name, typeclass) {|resource| ret << resource}
- return ret
- end
- def each_resource(name, typeclass, &proc)
- lazy_initialize
- q = Queue.new
- senders = {}
- begin
- @config.resolv(name) {|candidate, tout, nameserver|
- msg = Message.new
- msg.rd = 1
- msg.add_question(candidate, typeclass)
- unless sender = senders[[candidate, nameserver]]
- sender = senders[[candidate, nameserver]] =
- @requester.sender(msg, candidate, q, nameserver)
- end
- sender.send
- reply = reply_name = nil
- timeout(tout, ResolvTimeout) { reply, reply_name = q.pop }
- case reply.rcode
- when RCode::NoError
- extract_resources(reply, reply_name, typeclass, &proc)
- return
- when RCode::NXDomain
- raise Config::NXDomain.new(reply_name.to_s)
- else
- raise Config::OtherResolvError.new(reply_name.to_s)
- end
- }
- ensure
- @requester.delete(q)
- end
- end
- def extract_resources(msg, name, typeclass) # :nodoc:
- if typeclass < Resource::ANY
- n0 = Name.create(name)
- msg.each_answer {|n, ttl, data|
- yield data if n0 == n
- }
- end
- yielded = false
- n0 = Name.create(name)
- msg.each_answer {|n, ttl, data|
- if n0 == n
- case data
- when typeclass
- yield data
- yielded = true
- when Resource::CNAME
- n0 = data.name
- end
- end
- }
- return if yielded
- msg.each_answer {|n, ttl, data|
- if n0 == n
- case data
- when typeclass
- yield data
- end
- end
- }
- end
- class Requester # :nodoc:
- def initialize
- @senders = {}
- end
- def close
- thread, sock, @thread, @sock = @thread, @sock
- begin
- if thread
- thread.kill
- thread.join
- end
- ensure
- sock.close if sock
- end
- end
- def delete(arg)
- case arg
- when Sender
- @senders.delete_if {|k, s| s == arg }
- when Queue
- @senders.delete_if {|k, s| s.queue == arg }
- else
- raise ArgumentError.new("neither Sender or Queue: #{arg}")
- end
- end
- class Sender # :nodoc:
- def initialize(msg, data, sock, queue)
- @msg = msg
- @data = data
- @sock = sock
- @queue = queue
- end
- attr_reader :queue
- def recv(msg)
- @queue.push([msg, @data])
- end
- end
- class UnconnectedUDP < Requester # :nodoc:
- def initialize
- super()
- @sock = UDPSocket.new
- @sock.fcntl(Fcntl::F_SETFD, 1) if defined? Fcntl::F_SETFD
- @id = {}
- @id.default = -1
- @thread = Thread.new {
- DNSThreadGroup.add Thread.current
- loop {
- reply, from = @sock.recvfrom(UDPSize)
- msg = begin
- Message.decode(reply)
- rescue DecodeError
- STDERR.print("DNS message decoding error: #{reply.inspect}\n")
- next
- end
- if s = @senders[[[from[3],from[1]],msg.id]]
- s.recv msg
- else
- #STDERR.print("non-handled DNS message: #{msg.inspect} from #{from.inspect}\n")
- end
- }
- }
- end
- def sender(msg, data, queue, host, port=Port)
- service = [host, port]
- id = Thread.exclusive {
- @id[service] = (@id[service] + 1) & 0xffff
- }
- request = msg.encode
- request[0,2] = [id].pack('n')
- return @senders[[service, id]] =
- Sender.new(request, data, @sock, host, port, queue)
- end
- class Sender < Requester::Sender # :nodoc:
- def initialize(msg, data, sock, host, port, queue)
- super(msg, data, sock, queue)
- @host = host
- @port = port
- end
- def send
- @sock.send(@msg, 0, @host, @port)
- end
- end
- end
- class ConnectedUDP < Requester # :nodoc:
- def initialize(host, port=Port)
- super()
- @host = host
- @port = port
- @sock = UDPSocket.new
- @sock.connect(host, port)
- @sock.fcntl(Fcntl::F_SETFD, 1) if defined? Fcntl::F_SETFD
- @id = -1
- @thread = Thread.new {
- DNSThreadGroup.add Thread.current
- loop {
- reply = @sock.recv(UDPSize)
- msg = begin
- Message.decode(reply)
- rescue DecodeError
- STDERR.print("DNS message decoding error: #{reply.inspect}")
- next
- end
- if s = @senders[msg.id]
- s.recv msg
- else
- #STDERR.print("non-handled DNS message: #{msg.inspect}")
- end
- }
- }
- end
- def sender(msg, data, queue, host=@host, port=@port)
- unless host == @host && port == @port
- raise RequestError.new("host/port don't match: #{host}:#{port}")
- end
- id = Thread.exclusive { @id = (@id + 1) & 0xffff }
- request = msg.encode
- request[0,2] = [id].pack('n')
- return @senders[id] = Sender.new(request, data, @sock, queue)
- end
- class Sender < Requester::Sender # :nodoc:
- def send
- @sock.send(@msg, 0)
- end
- end
- end
- class TCP < Requester # :nodoc:
- def initialize(host, port=Port)
- super()
- @host = host
- @port = port
- @sock = TCPSocket.new
- @sock.connect(host, port)
- @sock.fcntl(Fcntl::F_SETFD, 1) if defined? Fcntl::F_SETFD
- @id = -1
- @senders = {}
- @thread = Thread.new {
- DNSThreadGroup.add Thread.current
- loop {
- len = @sock.read(2).unpack('n')
- reply = @sock.read(len)
- msg = begin
- Message.decode(reply)
- rescue DecodeError
- STDERR.print("DNS message decoding error: #{reply.inspect}")
- next
- end
- if s = @senders[msg.id]
- s.push msg
- else
- #STDERR.print("non-handled DNS message: #{msg.inspect}")
- end
- }
- }
- end
- def sender(msg, data, queue, host=@host, port=@port)
- unless host == @host && port == @port
- raise RequestError.new("host/port don't match: #{host}:#{port}")
- end
- id = Thread.exclusive { @id = (@id + 1) & 0xffff }
- request = msg.encode
- request[0,2] = [request.length, id].pack('nn')
- return @senders[id] = Sender.new(request, data, @sock, queue)
- end
- class Sender < Requester::Sender # :nodoc:
- def send
- @sock.print(@msg)
- @sock.flush
- end
- end
- end
- class RequestError < StandardError
- end
- end
- # Encapsulates the resolver configuration information.
- #
- # +config_info+ can be nil, a String or a Hash:
- # - nil is the default, configuration is read from /etc/resolv.conf, or
- # from Win32::Resolv.get_resolv_info on Windows.
- # - String is used as the name of the config file to parse instead of
- # /etc/resolv.conf.
- # - Hash must map the :nameserver and/or :search symbol keys to a single
- # String or an array of String to use as the value of those config options.
- class Config
- def initialize(config_info=nil)
- @mutex = Mutex.new
- @config_info = config_info
- @initialized = nil
- end
- def Config.parse_resolv_conf(filename) # :nodoc:
- nameserver = []
- search = nil
- ndots = 1
- open(filename) {|f|
- f.each {|line|
- line.sub!(/[#;].*/, '')
- keyword, *args = line.split(/\s+/)
- args.each { |arg|
- arg.untaint
- }
- next unless keyword
- case keyword
- when 'nameserver'
- nameserver += args
- when 'domain'
- next if args.empty?
- search = [args[0]]
- when 'search'
- next if args.empty?
- search = args
- when 'options'
- args.each {|arg|
- case arg
- when /\Andots:(\d+)\z/
- ndots = $1.to_i
- end
- }
- end
- }
- }
- return { :nameserver => nameserver, :search => search, :ndots => ndots }
- end
- def Config.default_config_hash(filename="/etc/resolv.conf") # :nodoc:
- if File.exist? filename
- config_hash = Config.parse_resolv_conf(filename)
- else
- if /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
- search, nameserver = Win32::Resolv.get_resolv_info
- config_hash = {}
- config_hash[:nameserver] = nameserver if nameserver
- config_hash[:search] = [search].flatten if search
- end
- end
- config_hash
- end
- def lazy_initialize # :nodoc:
- @mutex.synchronize {
- unless @initialized
- @nameserver = []
- @search = nil
- @ndots = 1
- 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]]
- end
- if String === config_hash[:search]
- config_hash[:search] = [config_hash[:search]]
- end
- else
- raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}")
- end
- @nameserver = config_hash[:nameserver] if config_hash.include? :nameserver
- @search = config_hash[:search] if config_hash.include? :search
- @ndots = config_hash[:ndots] if config_hash.include? :ndots
- @nameserver = ['0.0.0.0'] if @nameserver.empty?
- if @search
- @search = @search.map {|arg| Label.split(arg) }
- else
- hostname = Socket.gethostname
- if /\./ =~ hostname
- @search = [Label.split($')]
- else
- @search = [[]]
- end
- end
- if !@nameserver.kind_of?(Array) ||
- !@nameserver.all? {|ns| String === ns }
- raise ArgumentError.new("invalid nameserver config: #{@nameserver.inspect}")
- 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
- @initialized = true
- end
- }
- self
- end
- def single?
- lazy_initialize
- if @nameserver.length == 1
- return @nameserver[0]
- else
- return nil
- end
- end
- def generate_candidates(name) # :nodoc:
- candidates = nil
- name = Name.create(name)
- if name.absolute?
- candidates = [name]
- else
- if @ndots <= name.length - 1
- candidates = [Name.new(name.to_a)]
- else
- candidates = []
- end
- candidates.concat(@search.map {|domain| Name.new(name.to_a + domain)})
- end
- return candidates
- end
- InitialTimeout = 5 # :nodoc:
- def generate_timeouts # :nodoc:
- ts = [InitialTimeout]
- ts << ts[-1] * 2 / @nameserver.length
- ts << ts[-1] * 2
- ts << ts[-1] * 2
- return ts
- end
- def resolv(name) # :nodoc:
- candidates = generate_candidates(name)
- timeouts = generate_timeouts
- begin
- candidates.each {|candidate|
- begin
- timeouts.each {|tout|
- @nameserver.each {|nameserver|
- begin
- yield candidate, tout, nameserver
- rescue ResolvTimeout
- end
- }
- }
- raise ResolvError.new("DNS resolv timeout: #{name}")
- rescue NXDomain
- end
- }
- rescue ResolvError
- end
- end
- class NXDomain < ResolvError
- end
- class OtherResolvError < ResolvError
- end
- end
- module OpCode # :nodoc:
- Query = 0
- IQuery = 1
- Status = 2
- Notify = 4
- Update = 5
- end
- module RCode # :nodoc:
- 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
- class DecodeError < StandardError
- end
- class EncodeError < StandardError
- end
- module Label #:nodoc:
- def self.split(arg)
- labels = []
- arg.scan(/[^\.]+/) {labels << Str.new($&)}
- return labels
- end
- # A String wrapper that compares (and hashes) case insensitively, used to
- # represent DNS labels.
- class Str
- def initialize(string) # :nodoc:
- @string = string
- @downcase = string.downcase
- end
- attr_reader :string, :downcase
- def to_s
- return @string
- end
- def inspect
- return "#<#{self.class} #{self.to_s.inspect}>"
- end
- def ==(other)
- return @downcase == other.downcase
- end
- def eql?(other)
- return self == other
- end
- def hash
- return @downcase.hash
- end
- end
- end
- # A DNS name is a sequence of labels seperated by (or followed by) a dot
- # (".").
- #
- # The labels are defined to be case-insensitive ("example.COM" is the same
- # as "EXAMPLE.com").
- #
- # Names created from DNS messages names are always absolute. Names created
- # from a String are absolute only if they have a trailing dot.
- class Name
- # Create a Name from a Name (in which case +arg+ is returned
- # directly), or from a String (in which case new Name is returned).
- def self.create(arg)
- case arg
- when Name
- return arg
- when String
- return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
- else
- raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
- end
- end
- def initialize(labels, absolute=true) # :nodoc:
- @labels = labels
- @absolute = absolute
- end
- def inspect
- "#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>"
- end
- # Tests if +self+ is absolute, i.e., had a trailing dot.
- # For example:
- # example.com.
- # is absolute, whereas:
- # example.com
- # is not. Absolute names will never have the default search domains
- # added to them during resolution.
- def absolute?
- return @absolute
- end
- # Tests equivalence to +other+, a Name or a String.
- #
- # Names are equivalent if their labels are equal (comparison is
- # case-insensitive) and their "absoluteness" is equal.
- #
- # p Name.create("example.COM") == "EXAMPLE.com" => true
- # p Name.create("example.com.") == "example.com" => false
- def ==(other)
- other = Name.create(other)
- return false unless Name === other
- return @labels == other.to_a && @absolute == other.absolute?
- end
- alias eql? ==
- # Tests subdomain-of relation.
- #
- # 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
- #
- def subdomain_of?(other)
- other = Name.create(other)
- other_len = other.length
- return false if @labels.length <= other_len
- return @labels[-other_len, other_len] == other.to_a
- end
- def hash
- return @labels.hash ^ @absolute.hash
- end
- # Returns the array of labels, each label is a Label::Str.
- def to_a
- return @labels
- end
- # Returns the length of the array of labels.
- def length
- return @labels.length
- end
- # Returns the +i+th label.
- def [](i)
- return @labels[i]
- end
- # Returns the domain name as a string.
- #
- # The domain name doesn't have a trailing dot even if the name object is
- # absolute.
- #
- # 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"
- #
- def to_s
- return @labels.join('.')
- end
- end
- class Message # :nodoc:
- @@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, unicast = false)
- @question << [Name.create(name), typeclass, unicast]
- end
- # Can accept either |name, typeclass| as block arguments
- # (backwards-compatible/DNS-style), or |name, typeclass, unicast|
- # (mDNS-style).
- def each_question # :yields: name, typeclass, unicast
- @question.each {|ary|
- yield ary
- }
- end
- def add_answer(name, ttl, data, cacheflush = false)
- @answer << [Name.create(name), ttl, data, cacheflush]
- end
- # Can accept either |name, ttl, data| as block arguments
- # (backwards-compatible/DNS-style), or |name, ttl, data, cacheflush|
- # (mDNS-style).
- def each_answer # :yields: name, ttl, data, cacheflush
- @answer.each {|ary|
- yield ary
- }
- 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, unicast = q
- hibit = unicast ? (1<<15) : 0x00
- msg.put_name(name)
- msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue|hibit)
- }
- [@answer, @authority, @additional].each {|rr|
- rr.each {|r|
- name, ttl, data, cacheflush = r
- hibit = cacheflush ? (1<<15) : 0x00
- msg.put_name(name)
- msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue|hibit, ttl)
- msg.put_length16 {data.encode_rdata(msg)}
- }
- }
- }.to_s
- end
- class MessageEncoder # :nodoc: used to implement Message.encode
- def initialize
- @data = ''
- @names = {}
- yield self
- end
- def to_s
- return @data
- end
- def put_bytes(d)
- @data << d
- end
- def put_pack(template, *d)
- @data << d.pack(template)
- end
- def put_length16
- length_index = @data.length
- @data << "\0\0"
- data_start = @data.length
- yield
- data_end = @data.length
- @data[length_index, 2] = [data_end - data_start].pack("n")
- end
- def put_string(d)
- raise ArgumentError, "strings longer than 255 characters cannot be encoded" if d.length > 255
- self.put_pack("C", d.length)
- @data << d
- end
- def put_string_list(ds)
- ds.each {|d|
- self.put_string(d)
- }
- end
- def put_name(d)
- put_labels(d.to_a)
- end
- def put_labels(d)
- d.each_index {|i|
- domain = d[i..-1]
- if idx = @names[domain]
- self.put_pack("n", 0xc000 | idx)
- return
- else
- @names[domain] = @data.length
- self.put_label(d[i])
- end
- }
- @data << "\0"
- end
- def put_label(d)
- self.put_string(d.string)
- 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, unicast = msg.get_question
- o.add_question(name, typeclass, unicast)
- }
- (1..ancount).each {
- name, ttl, data, cacheflush = 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
- class MessageDecoder # :nodoc: used to implement Message.decode
- def initialize(data)
- @data = data
- @index = 0
- @limit = data.length
- yield self
- end
- def get_length16
- len, = self.get_unpack('n')
- save_limit = @limit
- @limit = @index + len
- d = yield(len)
- if @index < @limit
- raise DecodeError.new("junk exists")
- elsif @limit < @index
- raise DecodeError.new("limit exceeded")
- end
- @limit = save_limit
- return d
- end
- def get_bytes(len = @limit - @index)
- d = @data[@index, len]
- @index += len
- return d
- end
- def get_unpack(template)
- len = 0
- template.each_byte {|byte|
- 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
- }
- raise DecodeError.new("limit exceeded") if @limit < @index + len
- arr = @data.unpack("@#{@index}#{template}")
- @index += len
- return arr
- end
- def get_string
- len = @data[@index]
- raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
- d = @data[@index + 1, len]
- @index += 1 + len
- return d
- end
- def get_string_list
- strings = []
- while @index < @limit
- strings << self.get_string
- end
- strings
- end
- def get_name
- return Name.new(self.get_labels)
- end
- def get_labels(limit=nil)
- limit = @index if !limit || @index < limit
- d = []
- while true
- case @data[@index]
- when 0
- @index += 1
- return d
- when 192..255
- idx = self.get_unpack('n')[0] & 0x3fff
- if limit <= idx
- raise DecodeError.new("non-backward name pointer")
- end
- save_index = @index
- @index = idx
- d += self.get_labels(limit)
- @index = save_index
- return d
- else
- d << self.get_label
- end
- end
- return d
- end
- def get_label
- return Label::Str.new(self.get_string)
- end
- def get_question
- name = self.get_name
- type, klass = self.get_unpack("nn")
- typeclass = Resource.get_class(type, klass % 0x8000)
- unicast = (klass >> 15) == 1
- return name, typeclass, unicast
- end
- def get_rr
- name = self.get_name
- type, klass, ttl = self.get_unpack('nnN')
- typeclass = Resource.get_class(type, klass % 0x8000)
- data = self.get_length16 {typeclass.decode_rdata(self)}
- cacheflush = (klass >> 15) == 1
- return name, ttl, data, cacheflush
- end
- end
- end
- class Query # :nodoc:
- def encode_rdata(msg)
- raise EncodeError.new("#{self.class} is query.")
- end
- def self.decode_rdata(msg)
- raise DecodeError.new("#{self.class} is query.")
- end
- end
- class Resource < Query
- ClassHash = {} # :nodoc:
- def encode_rdata(msg) # :nodoc:
- raise NotImplementedError.new
- end
- def self.decode_rdata(msg) # :nodoc:
- raise NotImplementedError.new
- end
- def ==(other)
- return self.class == other.class &&
- self.instance_variables == other.instance_variables &&
- self.instance_variables.collect {|name| self.instance_eval name} ==
- other.instance_variables.collect {|name| other.instance_eval name}
- end
- def eql?(other)
- return self == other
- end
- def hash
- h = 0
- self.instance_variables.each {|name|
- h ^= self.instance_eval("#{name}.hash")
- }
- return h
- end
- def self.get_class(type_value, class_value) # :nodoc:
- return ClassHash[[type_value, class_value]] ||
- Generic.create(type_value, class_value)
- end
- class Generic < Resource
- def initialize(data)
- @data = data
- end
- attr_reader :data
- def encode_rdata(msg) # :nodoc:
- msg.put_bytes(data)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(msg.get_bytes)
- end
- def self.create(type_value, class_value) # :nodoc:
- 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
- class DomainName < Resource
- def initialize(name)
- @name = name
- end
- attr_reader :name
- def encode_rdata(msg) # :nodoc:
- msg.put_name(@name)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(msg.get_name)
- end
- end
- # Standard (class generic) RRs
- ClassValue = nil
- class NS < DomainName
- TypeValue = 2
- end
- class CNAME < DomainName
- TypeValue = 5
- end
- class SOA < Resource
- TypeValue = 6
- def initialize(mname, rname, serial, refresh, retry_, expire, minimum)
- @mname = mname
- @rname = rname
- @serial = serial
- @refresh = refresh
- @retry = retry_
- @expire = expire
- @minimum = minimum
- end
- attr_reader :mname, :rname, :serial, :refresh, :retry, :expire, :minimum
- def encode_rdata(msg) # :nodoc:
- msg.put_name(@mname)
- msg.put_name(@rname)
- msg.put_pack('NNNNN', @serial, @refresh, @retry, @expire, @minimum)
- end
- def self.decode_rdata(msg) # :nodoc:
- 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
- class PTR < DomainName
- TypeValue = 12
- end
- class HINFO < Resource
- TypeValue = 13
- def initialize(cpu, os)
- @cpu = cpu
- @os = os
- end
- attr_reader :cpu, :os
- def encode_rdata(msg) # :nodoc:
- msg.put_string(@cpu)
- msg.put_string(@os)
- end
- def self.decode_rdata(msg) # :nodoc:
- cpu = msg.get_string
- os = msg.get_string
- return self.new(cpu, os)
- end
- end
- class MINFO < Resource
- TypeValue = 14
- def initialize(rmailbx, emailbx)
- @rmailbx = rmailbx
- @emailbx = emailbx
- end
- attr_reader :rmailbx, :emailbx
- def encode_rdata(msg) # :nodoc:
- msg.put_name(@rmailbx)
- msg.put_name(@emailbx)
- end
- def self.decode_rdata(msg) # :nodoc:
- rmailbx = msg.get_string
- emailbx = msg.get_string
- return self.new(rmailbx, emailbx)
- end
- end
- class MX < Resource
- TypeValue= 15
- def initialize(preference, exchange)
- @preference = preference
- @exchange = exchange
- end
- attr_reader :preference, :exchange
- def encode_rdata(msg) # :nodoc:
- msg.put_pack('n', @preference)
- msg.put_name(@exchange)
- end
- def self.decode_rdata(msg) # :nodoc:
- preference, = msg.get_unpack('n')
- exchange = msg.get_name
- return self.new(preference, exchange)
- end
- end
- class TXT < Resource
- TypeValue = 16
- # TXT resource records must have one or more character strings, but the
- # string may be zero-length.
- #
- # If only the +first_string+ is supplied, it may be longer than 255
- # characters (internally, it will split into multiple
- # character-strings). If multiple strings are supplied, each string
- # must not be longer than 255 characters.
- def initialize(first_string = '', *rest_strings)
- if first_string.length > 255
- raise ArgumentError, 'TXT strings are longer than 255 characters' if rest_strings.first
- @strings = []
- first_string.scan(/.{1,255}/) { |s| @strings << s }
- else
- @strings = [first_string, *rest_strings].compact
- end
- end
- # Returns an array of all the strings making up the resource data.
- # There may be multiple strings if this is a mDNS record or if the
- # resource data is longer than 255 bytes. In the case of mDNS, each
- # individual string has the form: "key=value".
- attr_reader :strings
- # Returns the resource data as a single string. DNS uses multiple
- # character strings to represent the data of a TXT record if the
- # data is longer than 255 characters.
- def data
- @strings.join
- end
- def encode_rdata(msg) # :nodoc:
- msg.put_string_list(@strings)
- end
- def self.decode_rdata(msg) # :nodoc:
- strings = msg.get_string_list
- return self.new(*strings)
- end
- end
- class ANY < Query
- TypeValue = 255 # :nodoc:
- end
- ClassInsensitiveTypes = [ # :nodoc:
- NS, CNAME, SOA, PTR, HINFO, MINFO, MX, TXT, ANY
- ]
- # ARPA Internet specific RRs
- module IN
- ClassValue = 1
- 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)
- }
- class A < Resource
- ClassHash[[TypeValue = 1, ClassValue = ClassValue]] = self
- def initialize(address)
- @address = IPv4.create(address)
- end
- attr_reader :address
- def encode_rdata(msg) # :nodoc:
- msg.put_bytes(@address.address)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(IPv4.new(msg.get_bytes(4)))
- end
- end
- class WKS < Resource
- ClassHash[[TypeValue = 11, ClassValue = ClassValue]] = self
- def initialize(address, protocol, bitmap)
- @address = IPv4.create(address)
- @protocol = protocol
- @bitmap = bitmap
- end
- attr_reader :address, :protocol, :bitmap
- def encode_rdata(msg) # :nodoc:
- msg.put_bytes(@address.address)
- msg.put_pack("n", @protocol)
- msg.put_bytes(@bitmap)
- end
- def self.decode_rdata(msg) # :nodoc:
- address = IPv4.new(msg.get_bytes(4))
- protocol, = msg.get_unpack("n")
- bitmap = msg.get_bytes
- return self.new(address, protocol, bitmap)
- end
- end
- class AAAA < Resource
- ClassHash[[TypeValue = 28, ClassValue = ClassValue]] = self
- def initialize(address)
- @address = IPv6.create(address)
- end
- attr_reader :address
- def encode_rdata(msg) # :nodoc:
- msg.put_bytes(@address.address)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(IPv6.new(msg.get_bytes(16)))
- end
- end
- # SRV resource record defined in RFC 2782
- #
- # These records identify the hostname and port that a service is
- # available at.
- #
- # The format is:
- # _Service._Proto.Name TTL Class SRV Priority Weight Port Target
- #
- # T…
Large files files are truncated, but you can click here to view the full file