2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# $Id: Resolver.rb,v 1.11 2006/07/30 16:55:35 bluemonk Exp $
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'timeout'
|
|
|
|
require 'ipaddr'
|
|
|
|
require 'logger'
|
|
|
|
require 'net/dns/packet'
|
|
|
|
require 'net/dns/resolver/timeouts'
|
|
|
|
|
|
|
|
alias old_send send
|
|
|
|
|
|
|
|
module Net # :nodoc:
|
2009-11-02 18:20:02 +00:00
|
|
|
module DNS
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
include Logger::Severity
|
|
|
|
|
|
|
|
# =Name
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# Net::DNS::Resolver - DNS resolver class
|
|
|
|
#
|
|
|
|
# =Synopsis
|
|
|
|
#
|
|
|
|
# require 'net/dns/resolver'
|
|
|
|
#
|
|
|
|
# =Description
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# The Net::DNS::Resolver class implements a complete DNS resolver written
|
2009-11-02 18:20:02 +00:00
|
|
|
# in pure Ruby, without a single C line of code. It has all of the
|
|
|
|
# tipical properties of an evoluted resolver, and a bit of OO which
|
|
|
|
# comes from having used Ruby.
|
|
|
|
#
|
|
|
|
# This project started as a porting of the Net::DNS Perl module,
|
2008-07-23 17:32:05 +00:00
|
|
|
# written by Martin Fuhr, but turned out (in the last months) to be
|
|
|
|
# an almost complete rewriting. Well, maybe some of the features of
|
|
|
|
# the Perl version are still missing, but guys, at least this is
|
2009-11-02 18:20:02 +00:00
|
|
|
# readable code!
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
# FIXME
|
|
|
|
#
|
|
|
|
# =Environment
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# The Following Environment variables can also be used to configure
|
2008-07-23 17:32:05 +00:00
|
|
|
# the resolver:
|
|
|
|
#
|
|
|
|
# * +RES_NAMESERVERS+: A space-separated list of nameservers to query.
|
|
|
|
#
|
|
|
|
# # Bourne Shell
|
|
|
|
# $ RES_NAMESERVERS="192.168.1.1 192.168.2.2 192.168.3.3"
|
|
|
|
# $ export RES_NAMESERVERS
|
|
|
|
#
|
|
|
|
# # C Shell
|
|
|
|
# % setenv RES_NAMESERVERS "192.168.1.1 192.168.2.2 192.168.3.3"
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# * +RES_SEARCHLIST+: A space-separated list of domains to put in the
|
2008-07-23 17:32:05 +00:00
|
|
|
# search list.
|
|
|
|
#
|
|
|
|
# # Bourne Shell
|
|
|
|
# $ RES_SEARCHLIST="example.com sub1.example.com sub2.example.com"
|
|
|
|
# $ export RES_SEARCHLIST
|
|
|
|
#
|
|
|
|
# # C Shell
|
|
|
|
# % setenv RES_SEARCHLIST "example.com sub1.example.com sub2.example.com"
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# * +LOCALDOMAIN+: The default domain.
|
|
|
|
#
|
|
|
|
# # Bourne Shell
|
|
|
|
# $ LOCALDOMAIN=example.com
|
|
|
|
# $ export LOCALDOMAIN
|
|
|
|
#
|
|
|
|
# # C Shell
|
|
|
|
# % setenv LOCALDOMAIN example.com
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# * +RES_OPTIONS+: A space-separated list of resolver options to set.
|
2008-07-23 17:32:05 +00:00
|
|
|
# Options that take values are specified as option:value.
|
|
|
|
#
|
|
|
|
# # Bourne Shell
|
|
|
|
# $ RES_OPTIONS="retrans:3 retry:2 debug"
|
|
|
|
# $ export RES_OPTIONS
|
|
|
|
#
|
|
|
|
# # C Shell
|
|
|
|
# % setenv RES_OPTIONS "retrans:3 retry:2 debug"
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
class Resolver
|
|
|
|
|
|
|
|
# An hash with the defaults values of almost all the
|
|
|
|
# configuration parameters of a resolver object. See
|
2009-11-02 18:20:02 +00:00
|
|
|
# the description for each parameter to have an
|
2008-07-23 17:32:05 +00:00
|
|
|
# explanation of its usage.
|
|
|
|
Defaults = {
|
|
|
|
:config_file => "/etc/resolv.conf",
|
|
|
|
:log_file => $stdout,
|
|
|
|
:port => 53,
|
|
|
|
:searchlist => [],
|
|
|
|
:nameservers => [IPAddr.new("127.0.0.1")],
|
|
|
|
:domain => "",
|
|
|
|
:source_port => 0,
|
|
|
|
:source_address => IPAddr.new("0.0.0.0"),
|
|
|
|
:retry_interval => 5,
|
|
|
|
:retry_number => 4,
|
|
|
|
:recursive => true,
|
|
|
|
:defname => true,
|
|
|
|
:dns_search => true,
|
|
|
|
:use_tcp => false,
|
|
|
|
:ignore_truncated => false,
|
|
|
|
:packet_size => 512,
|
|
|
|
:tcp_timeout => TcpTimeout.new(120),
|
|
|
|
:udp_timeout => UdpTimeout.new(0)}
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Create a new resolver object.
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
|
|
|
# Argument +config+ can either be empty or be an hash with
|
2008-07-23 17:32:05 +00:00
|
|
|
# some configuration parameters. To know what each parameter
|
|
|
|
# do, look at the description of each.
|
|
|
|
# Some example:
|
|
|
|
#
|
|
|
|
# # Use the sistem defaults
|
|
|
|
# res = Net::DNS::Resolver.new
|
|
|
|
#
|
|
|
|
# # Specify a configuration file
|
|
|
|
# res = Net::DNS::Resolver.new(:config_file => '/my/dns.conf')
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# # Set some option
|
|
|
|
# res = Net::DNS::Resolver.new(:nameservers => "172.16.1.1",
|
|
|
|
# :recursive => false,
|
|
|
|
# :retry => 10)
|
|
|
|
#
|
|
|
|
# ===Config file
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# Net::DNS::Resolver uses a config file to read the usual
|
|
|
|
# values a resolver needs, such as nameserver list and
|
2009-11-02 18:20:02 +00:00
|
|
|
# domain names. On UNIX systems the defaults are read from the
|
2008-07-23 17:32:05 +00:00
|
|
|
# following files, in the order indicated:
|
|
|
|
#
|
|
|
|
# * /etc/resolv.conf
|
|
|
|
# * $HOME/.resolv.conf
|
|
|
|
# * ./.resolv.conf
|
|
|
|
#
|
|
|
|
# The following keywords are recognized in resolver configuration files:
|
|
|
|
#
|
|
|
|
# * domain: the default domain.
|
|
|
|
# * search: a space-separated list of domains to put in the search list.
|
|
|
|
# * nameserver: a space-separated list of nameservers to query.
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Files except for /etc/resolv.conf must be owned by the effective userid
|
|
|
|
# running the program or they won't be read. In addition, several environment
|
|
|
|
# variables can also contain configuration information; see Environment
|
2008-07-23 17:32:05 +00:00
|
|
|
# in the main description for Resolver class.
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
|
|
|
# On Windows Systems, an attempt is made to determine the system defaults
|
|
|
|
# using the registry. This is still a work in progress; systems with many
|
2008-07-23 17:32:05 +00:00
|
|
|
# dynamically configured network interfaces may confuse Net::DNS.
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# You can include a configuration file of your own when creating a resolver
|
2008-07-23 17:32:05 +00:00
|
|
|
# object:
|
|
|
|
#
|
|
|
|
# # Use my own configuration file
|
|
|
|
# my $res = Net::DNS::Resolver->new(config_file => '/my/dns.conf');
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# This is supported on both UNIX and Windows. Values pulled from a custom
|
|
|
|
# configuration file override the the system's defaults, but can still be
|
2008-07-23 17:32:05 +00:00
|
|
|
# overridden by the other arguments to Resolver::new.
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Explicit arguments to Resolver::new override both the system's defaults
|
2008-07-23 17:32:05 +00:00
|
|
|
# and the values of the custom configuration file, if any.
|
|
|
|
#
|
|
|
|
# ===Parameters
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# The following arguments to Resolver::new are supported:
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# - nameservers: an array reference of nameservers to query.
|
|
|
|
# - searchlist: an array reference of domains.
|
|
|
|
# - recurse
|
|
|
|
# - debug
|
|
|
|
# - domain
|
|
|
|
# - port
|
|
|
|
# - srcaddr
|
|
|
|
# - srcport
|
|
|
|
# - tcp_timeout
|
|
|
|
# - udp_timeout
|
|
|
|
# - retrans
|
|
|
|
# - retry
|
|
|
|
# - usevc
|
|
|
|
# - stayopen
|
|
|
|
# - igntc
|
|
|
|
# - defnames
|
|
|
|
# - dnsrch
|
|
|
|
# - persistent_tcp
|
|
|
|
# - persistent_udp
|
|
|
|
# - dnssec
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# For more information on any of these options, please consult the
|
2008-07-23 17:32:05 +00:00
|
|
|
# method of the same name.
|
|
|
|
#
|
|
|
|
# ===Disclaimer
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
|
|
|
# Part of the above documentation is taken from the one in the
|
2008-07-23 17:32:05 +00:00
|
|
|
# Net::DNS::Resolver Perl module.
|
|
|
|
#
|
|
|
|
def initialize(config = {})
|
|
|
|
raise ResolverArgumentError, "Argument has to be Hash" unless config.kind_of? Hash
|
|
|
|
# config.key_downcase!
|
|
|
|
@config = Defaults.merge config
|
|
|
|
@raw = false
|
|
|
|
|
|
|
|
# New logger facility
|
|
|
|
@logger = Logger.new(@config[:log_file])
|
|
|
|
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# Resolver configuration will be set in order from:
|
|
|
|
# 1) initialize arguments
|
|
|
|
# 2) ENV variables
|
|
|
|
# 3) config file
|
|
|
|
# 4) defaults (and /etc/resolv.conf for config)
|
|
|
|
#------------------------------------------------------------
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# Parsing config file
|
|
|
|
#------------------------------------------------------------
|
|
|
|
parse_config_file
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# Parsing ENV variables
|
|
|
|
#------------------------------------------------------------
|
|
|
|
parse_environment_variables
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# Parsing arguments
|
|
|
|
#------------------------------------------------------------
|
|
|
|
config.each do |key,val|
|
|
|
|
next if key == :log_file or key == :config_file
|
|
|
|
begin
|
|
|
|
eval "self.#{key.to_s} = val"
|
|
|
|
rescue NoMethodError
|
|
|
|
raise ResolverArgumentError, "Option #{key} not valid"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Get the resolver searchlist, returned as an array of entries
|
|
|
|
#
|
|
|
|
# res.searchlist
|
|
|
|
# #=> ["example.com","a.example.com","b.example.com"]
|
|
|
|
#
|
|
|
|
def searchlist
|
|
|
|
@config[:searchlist].inspect
|
|
|
|
end
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# Set the resolver searchlist.
|
2008-07-23 17:32:05 +00:00
|
|
|
# +arg+ can be a single string or an array of strings
|
|
|
|
#
|
|
|
|
# res.searchstring = "example.com"
|
|
|
|
# res.searchstring = ["example.com","a.example.com","b.example.com"]
|
|
|
|
#
|
|
|
|
# Note that you can also append a new name to the searchlist
|
|
|
|
#
|
|
|
|
# res.searchlist << "c.example.com"
|
|
|
|
# res.searchlist
|
|
|
|
# #=> ["example.com","a.example.com","b.example.com","c.example.com"]
|
|
|
|
#
|
|
|
|
# The default is an empty array
|
|
|
|
#
|
|
|
|
def searchlist=(arg)
|
|
|
|
case arg
|
|
|
|
when String
|
|
|
|
@config[:searchlist] = [arg] if valid? arg
|
|
|
|
@logger.info "Searchlist changed to value #{@config[:searchlist].inspect}"
|
|
|
|
when Array
|
|
|
|
@config[:searchlist] = arg if arg.all? {|x| valid? x}
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Searchlist changed to value #{@config[:searchlist].inspect}"
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Wrong argument format, neither String nor Array"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Get the list of resolver nameservers, in a dotted decimal format
|
|
|
|
#
|
|
|
|
# res.nameservers
|
|
|
|
# #=> ["192.168.0.1","192.168.0.2"]
|
|
|
|
#
|
|
|
|
def nameservers
|
|
|
|
arr = []
|
2009-11-02 18:20:02 +00:00
|
|
|
@config[:nameservers].each do |x|
|
2008-07-23 17:32:05 +00:00
|
|
|
arr << x.to_s
|
|
|
|
end
|
|
|
|
arr
|
|
|
|
end
|
|
|
|
alias_method :nameserver, :nameservers
|
|
|
|
|
|
|
|
# Set the list of resolver nameservers
|
|
|
|
# +arg+ can be a single ip address or an array of addresses
|
|
|
|
#
|
|
|
|
# res.nameservers = "192.168.0.1"
|
|
|
|
# res.nameservers = ["192.168.0.1","192.168.0.2"]
|
|
|
|
#
|
|
|
|
# If you want you can specify the addresses as IPAddr instances
|
|
|
|
#
|
|
|
|
# ip = IPAddr.new("192.168.0.3")
|
|
|
|
# res.nameservers << ip
|
|
|
|
# #=> ["192.168.0.1","192.168.0.2","192.168.0.3"]
|
|
|
|
#
|
|
|
|
# The default is 127.0.0.1 (localhost)
|
|
|
|
#
|
|
|
|
def nameservers=(arg)
|
|
|
|
case arg
|
|
|
|
when String
|
|
|
|
begin
|
|
|
|
@config[:nameservers] = [IPAddr.new(arg)]
|
|
|
|
@logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}"
|
|
|
|
rescue ArgumentError # arg is in the name form, not IP
|
|
|
|
nameservers_from_name(arg)
|
|
|
|
end
|
|
|
|
when IPAddr
|
|
|
|
@config[:nameservers] = [arg]
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}"
|
2008-07-23 17:32:05 +00:00
|
|
|
when Array
|
|
|
|
@config[:nameservers] = []
|
|
|
|
arg.each do |x|
|
|
|
|
@config[:nameservers] << case x
|
|
|
|
when String
|
|
|
|
begin
|
|
|
|
IPAddr.new(x)
|
|
|
|
rescue ArgumentError
|
|
|
|
nameservers_from_name(arg)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
when IPAddr
|
|
|
|
x
|
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Wrong argument format"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}"
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Wrong argument format, neither String, Array nor IPAddr"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method("nameserver=","nameservers=")
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Return a string with the default domain
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
def domain
|
|
|
|
@config[:domain].inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the domain for the query
|
|
|
|
#
|
|
|
|
def domain=(name)
|
|
|
|
@config[:domain] = name if valid? name
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Return the defined size of the packet
|
|
|
|
#
|
|
|
|
def packet_size
|
|
|
|
@config[:packet_size]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the port number to which the resolver sends queries.
|
|
|
|
#
|
|
|
|
# puts "Sending queries to port #{res.port}"
|
|
|
|
#
|
|
|
|
def port
|
|
|
|
@config[:port]
|
|
|
|
end
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# Set the port number to which the resolver sends queries. This can be useful
|
|
|
|
# for testing a nameserver running on a non-standard port.
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
# res.port = 10053
|
|
|
|
#
|
|
|
|
# The default is port 53.
|
|
|
|
#
|
|
|
|
def port=(num)
|
|
|
|
if (0..65535).include? num
|
|
|
|
@config[:port] = num
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Port number changed to #{num}"
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Wrong port number #{num}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the value of the source port number
|
|
|
|
#
|
|
|
|
# puts "Sending queries using port #{res.source_port}"
|
|
|
|
#
|
|
|
|
def source_port
|
|
|
|
@config[:source_port]
|
|
|
|
end
|
|
|
|
alias srcport source_port
|
|
|
|
|
|
|
|
# Set the local source port from which the resolver sends its queries.
|
|
|
|
#
|
|
|
|
# res.source_port = 40000
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Note that if you want to set a port you need root priviledges, as
|
|
|
|
# raw sockets will be used to generate packets. The class will then
|
2008-07-23 17:32:05 +00:00
|
|
|
# generate the exception ResolverPermissionError if you're not root.
|
|
|
|
#
|
|
|
|
# The default is 0, which means that the port will be chosen by the
|
|
|
|
# underlaying layers.
|
|
|
|
#
|
|
|
|
def source_port=(num)
|
|
|
|
unless root?
|
|
|
|
raise ResolverPermissionError, "Are you root?"
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
if (0..65535).include?(num)
|
2008-07-23 17:32:05 +00:00
|
|
|
@config[:source_port] = num
|
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Wrong port number #{num}"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias srcport= source_port=
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Get the local address from which the resolver sends queries
|
|
|
|
#
|
|
|
|
# puts "Sending queries using source address #{res.source_address}"
|
|
|
|
#
|
|
|
|
def source_address
|
|
|
|
@config[:source_address].to_s
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias srcaddr source_address
|
2008-07-23 17:32:05 +00:00
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# Set the local source address from which the resolver sends its
|
2008-07-23 17:32:05 +00:00
|
|
|
# queries.
|
|
|
|
#
|
|
|
|
# res.source_address = "172.16.100.1"
|
|
|
|
# res.source_address = IPAddr.new("172.16.100.1")
|
|
|
|
#
|
|
|
|
# You can specify +arg+ as either a string containing the ip address
|
|
|
|
# or an instance of IPAddr class.
|
|
|
|
#
|
|
|
|
# Normally this can be used to force queries out a specific interface
|
2009-11-02 18:20:02 +00:00
|
|
|
# on a multi-homed host. In this case, you should of course need to
|
2008-07-23 17:32:05 +00:00
|
|
|
# know the addresses of the interfaces.
|
|
|
|
#
|
|
|
|
# Another way to use this option is for some kind of spoofing attacks
|
2009-11-02 18:20:02 +00:00
|
|
|
# towards weak nameservers, to probe the security of your network.
|
|
|
|
# This includes specifing ranged attacks such as DoS and others. For
|
2008-07-23 17:32:05 +00:00
|
|
|
# a paper on DNS security, checks http://www.marcoceresa.com/security/
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Note that if you want to set a non-binded source address you need
|
|
|
|
# root priviledges, as raw sockets will be used to generate packets.
|
2008-07-23 17:32:05 +00:00
|
|
|
# The class will then generate an exception if you're not root.
|
|
|
|
#
|
|
|
|
# The default is 0.0.0.0, meaning any local address (chosen on routing
|
|
|
|
# needs).
|
|
|
|
#
|
|
|
|
def source_address=(addr)
|
|
|
|
unless addr.respond_to? :to_s
|
|
|
|
raise ResolverArgumentError, "Wrong address argument #{addr}"
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
port = rand(64000)+1024
|
|
|
|
@logger.warn "Try to determine state of source address #{addr} with port #{port}"
|
|
|
|
a = TCPServer.new(addr.to_s,port)
|
|
|
|
rescue SystemCallError => e
|
|
|
|
case e.errno
|
|
|
|
when 98 # Port already in use!
|
|
|
|
@logger.warn "Port already in use"
|
|
|
|
retry
|
|
|
|
when 99 # Address is not valid: raw socket
|
|
|
|
@raw = true
|
|
|
|
@logger.warn "Using raw sockets"
|
|
|
|
else
|
|
|
|
raise SystemCallError, e
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
a.close
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
case addr
|
|
|
|
when String
|
|
|
|
@config[:source_address] = IPAddr.new(string)
|
|
|
|
@logger.info "Using new source address: #{@config[:source_address]}"
|
|
|
|
when IPAddr
|
|
|
|
@config[:source_address] = addr
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Using new source address: #{@config[:source_address]}"
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown dest_address format"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias srcaddr= source_address=
|
|
|
|
|
|
|
|
# Return the retrasmission interval (in seconds) the resolvers has
|
2008-07-23 17:32:05 +00:00
|
|
|
# been set on
|
|
|
|
#
|
|
|
|
def retry_interval
|
|
|
|
@config[:retry_interval]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias retrans retry_interval
|
2008-07-23 17:32:05 +00:00
|
|
|
|
|
|
|
# Set the retrasmission interval in seconds. Default 5 seconds
|
|
|
|
#
|
|
|
|
def retry_interval=(num)
|
|
|
|
if num > 0
|
|
|
|
@config[:retry_interval] = num
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Retransmission interval changed to #{num} seconds"
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Interval must be positive"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias retrans= retry_interval=
|
2008-07-23 17:32:05 +00:00
|
|
|
|
|
|
|
# The number of times the resolver will try a query
|
|
|
|
#
|
|
|
|
# puts "Will try a max of #{res.retry_number} queries"
|
|
|
|
#
|
|
|
|
def retry_number
|
|
|
|
@config[:retry_number]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the number of times the resolver will try a query.
|
|
|
|
# Default 4 times
|
|
|
|
#
|
|
|
|
def retry_number=(num)
|
|
|
|
if num.kind_of? Integer and num > 0
|
|
|
|
@config[:retry_number] = num
|
|
|
|
@logger.info "Retrasmissions number changed to #{num}"
|
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Retry value must be a positive integer"
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
end
|
|
|
|
alias_method('retry=', 'retry_number=')
|
2008-07-23 17:32:05 +00:00
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# This method will return true if the resolver is configured to
|
2008-07-23 17:32:05 +00:00
|
|
|
# perform recursive queries.
|
|
|
|
#
|
|
|
|
# print "The resolver will perform a "
|
|
|
|
# print res.recursive? ? "" : "not "
|
|
|
|
# puts "recursive query"
|
|
|
|
#
|
|
|
|
def recursive?
|
|
|
|
@config[:recursive]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias_method :recurse, :recursive?
|
|
|
|
alias_method :recursive, :recursive?
|
2008-07-23 17:32:05 +00:00
|
|
|
|
|
|
|
# Sets whether or not the resolver should perform recursive
|
|
|
|
# queries. Default is true.
|
|
|
|
#
|
|
|
|
# res.recursive = false # perform non-recursive query
|
|
|
|
#
|
|
|
|
def recursive=(bool)
|
|
|
|
case bool
|
|
|
|
when TrueClass,FalseClass
|
|
|
|
@config[:recursive] = bool
|
|
|
|
@logger.info("Recursive state changed to #{bool}")
|
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be boolean"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias_method :recurse=, :recursive=
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Return a string rapresenting the resolver state, suitable
|
|
|
|
# for printing on the screen.
|
|
|
|
#
|
|
|
|
# puts "Resolver state:"
|
|
|
|
# puts res.state
|
|
|
|
#
|
|
|
|
def state
|
2009-11-02 18:20:02 +00:00
|
|
|
str = ";; RESOLVER state:\n;; "
|
2008-07-23 17:32:05 +00:00
|
|
|
i = 1
|
|
|
|
@config.each do |key,val|
|
|
|
|
if key == :log_file or key == :config_file
|
|
|
|
str << "#{key}: #{val} \t"
|
|
|
|
else
|
|
|
|
str << "#{key}: #{eval(key.to_s)} \t"
|
|
|
|
end
|
|
|
|
str << "\n;; " if i % 2 == 0
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
str
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias print state
|
|
|
|
alias inspect state
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Checks whether the +defname+ flag has been activate.
|
|
|
|
def defname?
|
|
|
|
@config[:defname]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias defname defname?
|
2008-07-23 17:32:05 +00:00
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# Set the flag +defname+ in a boolean state. if +defname+ is true,
|
|
|
|
# calls to Resolver#query will append the default domain to names
|
2008-07-23 17:32:05 +00:00
|
|
|
# that contain no dots.
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# # Domain example.com
|
|
|
|
# res.defname = true
|
|
|
|
# res.query("machine1")
|
|
|
|
# #=> This will perform a query for machine1.example.com
|
|
|
|
#
|
|
|
|
# Default is true.
|
|
|
|
#
|
|
|
|
def defname=(bool)
|
|
|
|
case bool
|
|
|
|
when TrueClass,FalseClass
|
|
|
|
@config[:defname] = bool
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("Defname state changed to #{bool}")
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be boolean"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the state of the dns_search flag
|
|
|
|
def dns_search
|
|
|
|
@config[:dns_search]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias_method :dnsrch, :dns_search
|
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Set the flag +dns_search+ in a boolean state. If +dns_search+
|
|
|
|
# is true, when using the Resolver#search method will be applied
|
|
|
|
# the search list. Default is true.
|
|
|
|
#
|
|
|
|
def dns_search=(bool)
|
|
|
|
case bool
|
|
|
|
when TrueClass,FalseClass
|
|
|
|
@config[:dns_search] = bool
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("DNS search state changed to #{bool}")
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be boolean"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method("dnsrch=","dns_search=")
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Get the state of the use_tcp flag.
|
|
|
|
#
|
|
|
|
def use_tcp?
|
|
|
|
@config[:use_tcp]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias_method :usevc, :use_tcp?
|
|
|
|
alias_method :use_tcp, :use_tcp?
|
2008-07-23 17:32:05 +00:00
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# If +use_tcp+ is true, the resolver will perform all queries
|
|
|
|
# using TCP virtual circuits instead of UDP datagrams, which
|
2008-07-23 17:32:05 +00:00
|
|
|
# is the default for the DNS protocol.
|
|
|
|
#
|
|
|
|
# res.use_tcp = true
|
|
|
|
# res.query "host.example.com"
|
|
|
|
# #=> Sending TCP segments...
|
|
|
|
#
|
|
|
|
# Default is false.
|
|
|
|
#
|
|
|
|
def use_tcp=(bool)
|
|
|
|
case bool
|
|
|
|
when TrueClass,FalseClass
|
|
|
|
@config[:use_tcp] = bool
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("Use tcp flag changed to #{bool}")
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be boolean"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
alias usevc= use_tcp=
|
2008-07-23 17:32:05 +00:00
|
|
|
|
|
|
|
def ignore_truncated?
|
|
|
|
@config[:ignore_truncated]
|
|
|
|
end
|
|
|
|
alias_method :ignore_truncated, :ignore_truncated?
|
|
|
|
|
|
|
|
def ignore_truncated=(bool)
|
|
|
|
case bool
|
|
|
|
when TrueClass,FalseClass
|
|
|
|
@config[:ignore_truncated] = bool
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("Ignore truncated flag changed to #{bool}")
|
2008-07-23 17:32:05 +00:00
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be boolean"
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Return an object representing the value of the stored TCP
|
2008-07-23 17:32:05 +00:00
|
|
|
# timeout the resolver will use in is queries. This object
|
|
|
|
# is an instance of the class +TcpTimeout+, and two methods
|
|
|
|
# are available for printing informations: TcpTimeout#to_s
|
|
|
|
# and TcpTimeout#pretty_to_s.
|
|
|
|
#
|
|
|
|
# Here's some example:
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# puts "Timeout of #{res.tcp_timeout} seconds" # implicit to_s
|
|
|
|
# #=> Timeout of 150 seconds
|
|
|
|
#
|
|
|
|
# puts "You set a timeout of " + res.tcp_timeout.pretty_to_s
|
|
|
|
# #=> You set a timeout of 2 minutes and 30 seconds
|
|
|
|
#
|
|
|
|
# If the timeout is infinite, a string "infinite" will
|
|
|
|
# be returned.
|
|
|
|
#
|
|
|
|
def tcp_timeout
|
|
|
|
@config[:tcp_timeout].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the value of TCP timeout for resolver queries that
|
|
|
|
# will be performed using TCP. A value of 0 means that
|
|
|
|
# the timeout will be infinite.
|
|
|
|
# The value is stored internally as a +TcpTimeout+ object, see
|
|
|
|
# the description for Resolver#tcp_timeout
|
|
|
|
#
|
|
|
|
# Default is 120 seconds
|
|
|
|
def tcp_timeout=(secs)
|
|
|
|
@config[:tcp_timeout] = TcpTimeout.new(secs)
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("New TCP timeout value: #{@config[:tcp_timeout]} seconds")
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# Return an object representing the value of the stored UDP
|
2008-07-23 17:32:05 +00:00
|
|
|
# timeout the resolver will use in is queries. This object
|
|
|
|
# is an instance of the class +UdpTimeout+, and two methods
|
|
|
|
# are available for printing informations: UdpTimeout#to_s
|
|
|
|
# and UdpTimeout#pretty_to_s.
|
|
|
|
#
|
|
|
|
# Here's some example:
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# puts "Timeout of #{res.udp_timeout} seconds" # implicit to_s
|
|
|
|
# #=> Timeout of 150 seconds
|
|
|
|
#
|
|
|
|
# puts "You set a timeout of " + res.udp_timeout.pretty_to_s
|
|
|
|
# #=> You set a timeout of 2 minutes and 30 seconds
|
|
|
|
#
|
|
|
|
# If the timeout is zero, a string "not defined" will
|
|
|
|
# be returned.
|
|
|
|
#
|
|
|
|
def udp_timeout
|
|
|
|
@config[:udp_timeout].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the value of UDP timeout for resolver queries that
|
|
|
|
# will be performed using UDP. A value of 0 means that
|
2009-11-02 18:20:02 +00:00
|
|
|
# the timeout will not be used, and the resolver will use
|
2008-07-23 17:32:05 +00:00
|
|
|
# only +retry_number+ and +retry_interval+ parameters.
|
|
|
|
# That is the default.
|
|
|
|
#
|
|
|
|
# The value is stored internally as a +UdpTimeout+ object, see
|
|
|
|
# the description for Resolver#udp_timeout
|
|
|
|
#
|
|
|
|
def udp_timeout=(secs)
|
|
|
|
@config[:udp_timeout] = UdpTimeout.new(secs)
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info("New UDP timeout value: #{@config[:udp_timeout]} seconds")
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set a new log file for the logger facility of the resolver
|
|
|
|
# class. Could be a file descriptor too:
|
|
|
|
#
|
|
|
|
# res.log_file = $stderr
|
|
|
|
#
|
|
|
|
# Note that a new logging facility will be create, destroing
|
|
|
|
# the old one, which will then be impossibile to recover.
|
|
|
|
#
|
|
|
|
def log_file=(log)
|
|
|
|
@logger.close
|
|
|
|
@config[:log_file] = log
|
|
|
|
@logger = Logger.new(@config[:log_file])
|
|
|
|
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# This one permits to have a personal logger facility to handle
|
|
|
|
# resolver messages, instead of new built-in one, which is set up
|
|
|
|
# for a +$stdout+ (or +$stderr+) use.
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
2008-07-23 17:32:05 +00:00
|
|
|
# If you want your own logging facility you can create a new instance
|
|
|
|
# of the +Logger+ class:
|
|
|
|
#
|
|
|
|
# log = Logger.new("/tmp/resolver.log","weekly",2*1024*1024)
|
|
|
|
# log.level = Logger::DEBUG
|
|
|
|
# log.progname = "ruby_resolver"
|
|
|
|
#
|
|
|
|
# and then pass it to the resolver:
|
|
|
|
#
|
|
|
|
# res.logger = log
|
|
|
|
#
|
|
|
|
# Note that this will destroy the precedent logger.
|
|
|
|
#
|
|
|
|
def logger=(logger)
|
|
|
|
if logger.kind_of? Logger
|
|
|
|
@logger.close
|
|
|
|
@logger = logger
|
|
|
|
else
|
|
|
|
raise ResolverArgumentError, "Argument must be an instance of Logger class"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the log level for the built-in logging facility.
|
|
|
|
#
|
|
|
|
# The log level can be one of the following:
|
2009-11-02 18:20:02 +00:00
|
|
|
#
|
|
|
|
# - +Net::DNS::DEBUG+
|
|
|
|
# - +Net::DNS::INFO+
|
|
|
|
# - +Net::DNS::WARN+
|
|
|
|
# - +Net::DNS::ERROR+
|
|
|
|
# - +Net::DNS::FATAL+
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
# Note that if the global variable $DEBUG is set (like when the
|
|
|
|
# -d switch is used at the command line) the logger level is
|
|
|
|
# automatically set at DEGUB.
|
|
|
|
#
|
|
|
|
# For further informations, see Logger documentation in the
|
|
|
|
# Ruby standard library.
|
|
|
|
#
|
|
|
|
def log_level=(level)
|
|
|
|
@logger.level = level
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Performs a DNS query for the given name, applying the searchlist if
|
2008-07-23 17:32:05 +00:00
|
|
|
# appropriate. The search algorithm is as follows:
|
|
|
|
#
|
|
|
|
# 1. If the name contains at least one dot, try it as is.
|
2009-11-02 18:20:02 +00:00
|
|
|
# 2. If the name doesn't end in a dot then append each item in the search
|
2008-07-23 17:32:05 +00:00
|
|
|
# list to the name. This is only done if +dns_search+ is true.
|
|
|
|
# 3. If the name doesn't contain any dots, try it as is.
|
|
|
|
#
|
|
|
|
# The record type and class can be omitted; they default to +A+ and +IN+.
|
|
|
|
#
|
|
|
|
# packet = res.search('mailhost')
|
|
|
|
# packet = res.search('mailhost.example.com')
|
|
|
|
# packet = res.search('example.com', Net::DNS::MX)
|
|
|
|
# packet = res.search('user.passwd.example.com', Net::DNS::TXT, Net::DNS::HS)
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# If the name is an IP address (Ipv4 or IPv6), in the form of a string
|
2008-07-23 17:32:05 +00:00
|
|
|
# or a +IPAddr+ object, then an appropriate PTR query will be performed:
|
|
|
|
#
|
|
|
|
# ip = IPAddr.new("172.16.100.2")
|
|
|
|
# packet = res.search(ip)
|
|
|
|
# packet = res.search("192.168.10.254")
|
|
|
|
#
|
|
|
|
# Returns a Net::DNS::Packet object. If you need to examine the response packet
|
|
|
|
# whether it contains any answers or not, use the send() method instead.
|
|
|
|
#
|
|
|
|
def search(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
# If the name contains at least one dot then try it as is first.
|
2008-07-23 17:32:05 +00:00
|
|
|
if name.include? "."
|
|
|
|
@logger.debug "Search(#{name},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})"
|
|
|
|
ans = query(name,type,cls)
|
|
|
|
return ans if ans.header.anCount > 0
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# If the name doesn't end in a dot then apply the search list.
|
|
|
|
if name !~ /\.$/ and @config[:dns_search]
|
|
|
|
@config[:searchlist].each do |domain|
|
|
|
|
newname = name + "." + domain
|
|
|
|
@logger.debug "Search(#{newname},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})"
|
|
|
|
ans = query(newname,type,cls)
|
|
|
|
return ans if ans.header.anCount > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Finally, if the name has no dots then try it as is.
|
|
|
|
@logger.debug "Search(#{name},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})"
|
|
|
|
query(name+".",type,cls)
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Performs a DNS query for the given name; the search list
|
|
|
|
# is not applied. If the name doesn't contain any dots and
|
2008-07-23 17:32:05 +00:00
|
|
|
# +defname+ is true then the default domain will be appended.
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# The record type and class can be omitted; they default to +A+
|
|
|
|
# and +IN+. If the name looks like an IP address (IPv4 or IPv6),
|
2008-07-23 17:32:05 +00:00
|
|
|
# then an appropriate PTR query will be performed.
|
|
|
|
#
|
|
|
|
# packet = res.query('mailhost')
|
|
|
|
# packet = res.query('mailhost.example.com')
|
|
|
|
# packet = res.query('example.com', Net::DNS::MX)
|
|
|
|
# packet = res.query('user.passwd.example.com', Net::DNS::TXT, Net::DNS::HS)
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# If the name is an IP address (Ipv4 or IPv6), in the form of a string
|
2008-07-23 17:32:05 +00:00
|
|
|
# or a +IPAddr+ object, then an appropriate PTR query will be performed:
|
|
|
|
#
|
|
|
|
# ip = IPAddr.new("172.16.100.2")
|
|
|
|
# packet = res.query(ip)
|
|
|
|
# packet = res.query("192.168.10.254")
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Returns a Net::DNS::Packet object. If you need to examine the response
|
|
|
|
# packet whether it contains any answers or not, use the Resolver#send
|
2008-07-23 17:32:05 +00:00
|
|
|
# method instead.
|
|
|
|
#
|
|
|
|
def query(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# If the name doesn't contain any dots then append the default domain.
|
2008-07-23 17:32:05 +00:00
|
|
|
if name !~ /\./ and name !~ /:/ and @config[:defnames]
|
|
|
|
name += "." + @config[:domain]
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
@logger.debug "Query(#{name},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})"
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
send(name,type,cls)
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Performs a DNS query for the given name. Neither the
|
2008-07-23 17:32:05 +00:00
|
|
|
# searchlist nor the default domain will be appended.
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# The argument list can be either a Net::DNS::Packet object
|
|
|
|
# or a name string plus optional type and class, which if
|
|
|
|
# omitted default to +A+ and +IN+.
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
# Returns a Net::DNS::Packet object.
|
|
|
|
#
|
|
|
|
# # Sending a +Packet+ object
|
|
|
|
# send_packet = Net::DNS::Packet.new("host.example.com",Net::DNS::NS,Net::DNS::HS)
|
|
|
|
# packet = res.send(send_packet)
|
|
|
|
#
|
|
|
|
# # Performing a query
|
|
|
|
# packet = res.send("host.example.com")
|
|
|
|
# packet = res.send("host.example.com",Net::DNS::NS)
|
|
|
|
# packet = res.send("host.example.com",Net::DNS::NS,Net::DNS::HS)
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# If the name is an IP address (Ipv4 or IPv6), in the form of a string
|
2008-07-23 17:32:05 +00:00
|
|
|
# or a IPAddr object, then an appropriate PTR query will be performed:
|
|
|
|
#
|
|
|
|
# ip = IPAddr.new("172.16.100.2")
|
|
|
|
# packet = res.send(ip)
|
|
|
|
# packet = res.send("192.168.10.254")
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Use +packet.header.ancount+ or +packet.answer+ to find out if there
|
2008-07-23 17:32:05 +00:00
|
|
|
# were any records in the answer section.
|
|
|
|
#
|
|
|
|
def send(argument,type=Net::DNS::A,cls=Net::DNS::IN)
|
|
|
|
if @config[:nameservers].size == 0
|
|
|
|
raise ResolverError, "No nameservers specified!"
|
|
|
|
end
|
|
|
|
|
|
|
|
method = :send_udp
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
if argument.kind_of? Net::DNS::Packet
|
|
|
|
packet = argument
|
|
|
|
else
|
|
|
|
packet = make_query_packet(argument,type,cls)
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Store packet_data for performance improvements,
|
|
|
|
# so methods don't keep on calling Packet#data
|
|
|
|
packet_data = packet.data
|
|
|
|
packet_size = packet_data.size
|
|
|
|
|
|
|
|
# Choose whether use TCP, UDP or RAW
|
|
|
|
if packet_size > @config[:packet_size] # Must use TCP, either plain or raw
|
|
|
|
if @raw # Use raw sockets?
|
|
|
|
@logger.info "Sending #{packet_size} bytes using TCP over RAW socket"
|
|
|
|
method = :send_raw_tcp
|
|
|
|
else
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Sending #{packet_size} bytes using TCP"
|
2008-07-23 17:32:05 +00:00
|
|
|
method = :send_tcp
|
|
|
|
end
|
|
|
|
else # Packet size is inside the boundaries
|
|
|
|
if @raw # Use raw sockets?
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Sending #{packet_size} bytes using UDP over RAW socket"
|
2008-07-23 17:32:05 +00:00
|
|
|
method = :send_raw_udp
|
|
|
|
elsif use_tcp? # User requested TCP
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Sending #{packet_size} bytes using TCP"
|
2008-07-23 17:32:05 +00:00
|
|
|
method = :send_tcp
|
|
|
|
else # Finally use UDP
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Sending #{packet_size} bytes using UDP"
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
if type == Net::DNS::AXFR
|
2008-07-23 17:32:05 +00:00
|
|
|
if @raw
|
|
|
|
@logger.warn "AXFR query, switching to TCP over RAW socket"
|
|
|
|
method = :send_raw_tcp
|
|
|
|
else
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.warn "AXFR query, switching to TCP"
|
2008-07-23 17:32:05 +00:00
|
|
|
method = :send_tcp
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
ans = self.old_send(method,packet,packet_data)
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
unless ans
|
|
|
|
@logger.fatal "No response from nameservers list: aborting"
|
|
|
|
raise NoResponseError
|
|
|
|
end
|
|
|
|
|
|
|
|
@logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
|
|
|
|
response = Net::DNS::Packet.parse(ans[0],ans[1])
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
if response.header.truncated? and not ignore_truncated?
|
|
|
|
@logger.warn "Packet truncated, retrying using TCP"
|
|
|
|
self.use_tcp = true
|
|
|
|
begin
|
|
|
|
return send(argument,type,cls)
|
|
|
|
ensure
|
|
|
|
self.use_tcp = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Performs a zone transfer for the zone passed as a parameter.
|
|
|
|
#
|
2012-08-17 01:40:24 +00:00
|
|
|
# Returns a list of Net::DNS::Packet (not answers!)
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
def axfr(name,cls=Net::DNS::IN)
|
|
|
|
@logger.info "Requested AXFR transfer, zone #{name} class #{cls}"
|
2012-08-17 01:40:24 +00:00
|
|
|
if @config[:nameservers].size == 0
|
|
|
|
raise Resolver::Error, "No nameservers specified!"
|
|
|
|
end
|
|
|
|
|
|
|
|
method = :query_tcp
|
|
|
|
packet = make_query_packet(name, Net::DNS::AXFR, cls)
|
|
|
|
|
|
|
|
# Store packet_data for performance improvements,
|
|
|
|
# so methods don't keep on calling Packet#data
|
|
|
|
packet_data = packet.data
|
|
|
|
packet_size = packet_data.size
|
|
|
|
|
|
|
|
if @raw
|
|
|
|
@logger.warn "AXFR query, switching to TCP over RAW socket"
|
|
|
|
method = :send_raw_tcp
|
|
|
|
else
|
|
|
|
@logger.warn "AXFR query, switching to TCP"
|
|
|
|
method = :query_tcp
|
|
|
|
end
|
|
|
|
|
|
|
|
answers = []
|
|
|
|
length = [packet_data.size].pack("n")
|
|
|
|
|
|
|
|
@config[:nameservers].each do |ns|
|
|
|
|
begin
|
|
|
|
socket = Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)
|
|
|
|
socket.bind(Socket.pack_sockaddr_in(@config[:source_port],@config[:source_address].to_s))
|
|
|
|
|
|
|
|
sockaddr = Socket.pack_sockaddr_in(@config[:port],ns.to_s)
|
|
|
|
|
|
|
|
@config[:tcp_timeout].timeout do
|
|
|
|
catch "next nameserver" do
|
|
|
|
socket.connect(sockaddr)
|
|
|
|
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
|
|
|
|
socket.write(length+packet_data)
|
|
|
|
soa = 0
|
|
|
|
while soa < 2 do
|
|
|
|
buffer = ""
|
|
|
|
ans = socket.recv(Net::DNS::INT16SZ)
|
|
|
|
if ans.size == 0
|
|
|
|
if soa == 1
|
|
|
|
break #No records in zone
|
|
|
|
else
|
|
|
|
@logger.warn "Couldn't recv from nameserver #{ns}, trying next."
|
|
|
|
throw "next nameserver"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
len = ans.unpack("n")[0]
|
|
|
|
|
|
|
|
@logger.info "Receiving #{len} bytes..."
|
|
|
|
|
|
|
|
if len == 0
|
|
|
|
@logger.warn "Receiving 0 length packet from nameserver #{ns}, trying next."
|
|
|
|
throw "next nameserver"
|
|
|
|
end
|
|
|
|
|
|
|
|
while (buffer.size < len)
|
|
|
|
left = len - buffer.size
|
|
|
|
temp,from = socket.recvfrom(left)
|
|
|
|
buffer += temp
|
|
|
|
end
|
|
|
|
|
|
|
|
unless buffer.size == len
|
|
|
|
@logger.warn "Malformed packet from nameserver #{ns}, trying next."
|
|
|
|
throw "next nameserver"
|
|
|
|
end
|
|
|
|
@logger.info "Received #{buffer.size} bytes from #{ns.to_s}:#{@config[:port].to_s}"
|
|
|
|
begin
|
|
|
|
response = Net::DNS::Packet.parse(buffer,["",@config[:port],ns.to_s,ns.to_s])
|
|
|
|
if response.answer[0].type == "SOA"
|
|
|
|
soa += 1
|
|
|
|
end
|
|
|
|
answers << response
|
|
|
|
rescue NameError => e
|
|
|
|
@logger.warn "Error parsing axfr response: #{e.message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if soa == 2
|
|
|
|
answers.pop #Remove duplicate SOA
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue TimeoutError
|
|
|
|
@logger.warn "Nameserver #{ns} not responding within TCP timeout, trying next one"
|
|
|
|
answers = []
|
|
|
|
next
|
|
|
|
ensure
|
|
|
|
socket.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless answers
|
|
|
|
message = "No response from nameservers list"
|
|
|
|
@logger.fatal(message)
|
|
|
|
raise NoResponseError, message
|
|
|
|
end
|
|
|
|
return answers
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# Performs an MX query for the domain name passed as parameter.
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
2009-11-02 18:20:02 +00:00
|
|
|
# It actually uses the same methods a normal Resolver query would
|
|
|
|
# use, but automatically sort the results based on preferences
|
2008-07-23 17:32:05 +00:00
|
|
|
# and returns an ordered array.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# res = Net::DNS::Resolver.new
|
|
|
|
# res.mx("google.com")
|
|
|
|
#
|
|
|
|
def mx(name,cls=Net::DNS::IN)
|
|
|
|
arr = []
|
|
|
|
send(name, Net::DNS::MX, cls).answer.each do |entry|
|
|
|
|
arr << entry if entry.type == 'MX'
|
|
|
|
end
|
|
|
|
return arr.sort_by {|a| a.preference}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-11-02 18:20:02 +00:00
|
|
|
|
|
|
|
# Parse a configuration file specified as the argument.
|
2008-07-23 17:32:05 +00:00
|
|
|
#
|
|
|
|
def parse_config_file
|
2008-07-25 22:42:04 +00:00
|
|
|
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
|
2008-07-23 17:32:05 +00:00
|
|
|
require 'win32/resolv'
|
|
|
|
arr = Win32::Resolv.get_resolv_info
|
|
|
|
self.domain = arr[0]
|
|
|
|
self.nameservers = arr[1]
|
|
|
|
else
|
|
|
|
IO.foreach(@config[:config_file]) do |line|
|
|
|
|
line.gsub!(/\s*[;#].*/,"")
|
|
|
|
next unless line =~ /\S/
|
|
|
|
case line
|
|
|
|
when /^\s*domain\s+(\S+)/
|
|
|
|
self.domain = $1
|
|
|
|
when /^\s*search\s+(.*)/
|
|
|
|
self.searchlist = $1.split(" ")
|
|
|
|
when /^\s*nameserver\s+(.*)/
|
|
|
|
self.nameservers = $1.split(" ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# Parse environment variables
|
|
|
|
def parse_environment_variables
|
|
|
|
if ENV['RES_NAMESERVERS']
|
|
|
|
self.nameservers = ENV['RES_NAMESERVERS'].split(" ")
|
|
|
|
end
|
|
|
|
if ENV['RES_SEARCHLIST']
|
|
|
|
self.searchlist = ENV['RES_SEARCHLIST'].split(" ")
|
|
|
|
end
|
|
|
|
if ENV['LOCALDOMAIN']
|
|
|
|
self.domain = ENV['LOCALDOMAIN']
|
|
|
|
end
|
|
|
|
if ENV['RES_OPTIONS']
|
|
|
|
ENV['RES_OPTIONS'].split(" ").each do |opt|
|
|
|
|
name,val = opt.split(":")
|
|
|
|
begin
|
|
|
|
eval("self.#{name} = #{val}")
|
|
|
|
rescue NoMethodError
|
|
|
|
raise ResolverArgumentError, "Invalid ENV option #{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nameservers_from_name(arg)
|
|
|
|
arr = []
|
|
|
|
arg.split(" ").each do |name|
|
|
|
|
Resolver.new.search(name).each_address do |ip|
|
|
|
|
arr << ip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@config[:nameservers] << arr
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_query_packet(string,type,cls)
|
|
|
|
case string
|
|
|
|
when IPAddr
|
|
|
|
name = string.reverse
|
|
|
|
type = Net::DNS::PTR
|
|
|
|
@logger.warn "PTR query required for address #{string}, changing type to PTR"
|
|
|
|
when /\d/ # Contains a number, try to see if it's an IP or IPv6 address
|
|
|
|
begin
|
|
|
|
name = IPAddr.new(string).reverse
|
2009-11-02 18:20:02 +00:00
|
|
|
type = Net::DNS::PTR
|
2008-07-23 17:32:05 +00:00
|
|
|
rescue ArgumentError
|
|
|
|
name = string if valid? string
|
|
|
|
end
|
|
|
|
else
|
|
|
|
name = string if valid? string
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the packet
|
|
|
|
packet = Net::DNS::Packet.new(name,type,cls)
|
|
|
|
|
|
|
|
if packet.query?
|
|
|
|
packet.header.recursive = @config[:recursive] ? 1 : 0
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
# DNSSEC and TSIG stuff to be inserted here
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
packet
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_tcp(packet,packet_data)
|
|
|
|
|
|
|
|
ans = nil
|
|
|
|
length = [packet_data.size].pack("n")
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
@config[:nameservers].each do |ns|
|
|
|
|
begin
|
|
|
|
buffer = ""
|
|
|
|
socket = Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)
|
|
|
|
socket.bind(Socket.pack_sockaddr_in(@config[:source_port],@config[:source_address].to_s))
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
sockaddr = Socket.pack_sockaddr_in(@config[:port],ns.to_s)
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
@config[:tcp_timeout].timeout do
|
2009-11-02 18:20:02 +00:00
|
|
|
socket.connect(sockaddr)
|
2008-07-23 17:32:05 +00:00
|
|
|
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
|
|
|
|
socket.write(length+packet_data)
|
|
|
|
ans = socket.recv(Net::DNS::INT16SZ)
|
|
|
|
len = ans.unpack("n")[0]
|
|
|
|
|
|
|
|
@logger.info "Receiving #{len} bytes..."
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
if len == 0
|
|
|
|
@logger.warn "Receiving 0 lenght packet from nameserver #{ns}, trying next."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
while (buffer.size < len)
|
|
|
|
left = len - buffer.size
|
|
|
|
temp,from = socket.recvfrom(left)
|
|
|
|
buffer += temp
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
unless buffer.size == len
|
|
|
|
@logger.warn "Malformed packet from nameserver #{ns}, trying next."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return [buffer,["",@config[:port],ns.to_s,ns.to_s]]
|
2009-11-02 18:20:02 +00:00
|
|
|
rescue Timeout::Error
|
|
|
|
@logger.warn "Nameserver #{ns} not responding within TCP timeout, trying next one"
|
2008-07-23 17:32:05 +00:00
|
|
|
next
|
|
|
|
ensure
|
2009-11-02 18:20:02 +00:00
|
|
|
socket.close
|
2008-07-23 17:32:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
def send_udp(packet,packet_data)
|
|
|
|
socket = UDPSocket.new
|
|
|
|
socket.bind(@config[:source_address].to_s,@config[:source_port])
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
ans = nil
|
|
|
|
response = ""
|
|
|
|
@config[:nameservers].each do |ns|
|
|
|
|
begin
|
|
|
|
@config[:udp_timeout].timeout do
|
2009-11-02 18:20:02 +00:00
|
|
|
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
|
2008-07-23 17:32:05 +00:00
|
|
|
socket.send(packet_data,0,ns.to_s,@config[:port])
|
|
|
|
ans = socket.recvfrom(@config[:packet_size])
|
|
|
|
end
|
|
|
|
break if ans
|
2009-11-02 18:20:02 +00:00
|
|
|
rescue Timeout::Error
|
|
|
|
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
|
2008-07-23 17:32:05 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ans
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(name)
|
|
|
|
if name =~ /[^-\w\.]/
|
|
|
|
raise ResolverArgumentError, "Invalid domain name #{name}"
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:20:02 +00:00
|
|
|
|
2008-07-23 17:32:05 +00:00
|
|
|
end # class Resolver
|
|
|
|
end # module DNS
|
|
|
|
end # module Net
|
|
|
|
|
|
|
|
class ResolverArgumentError < ArgumentError # :nodoc:
|
|
|
|
end
|
|
|
|
class NoResponseError < StandardError # :nodoc:
|
|
|
|
end
|
|
|
|
|
2009-11-02 18:20:02 +00:00
|
|
|
module ExtendHash # :nodoc:
|
|
|
|
# Returns an hash with all the
|
2008-07-23 17:32:05 +00:00
|
|
|
# keys turned into downcase
|
|
|
|
#
|
|
|
|
# hsh = {"Test" => 1, "FooBar" => 2}
|
|
|
|
# hsh.key_downcase!
|
|
|
|
# #=> {"test"=>1,"foobar"=>2}
|
|
|
|
#
|
|
|
|
def key_downcase!
|
|
|
|
hsh = Hash.new
|
|
|
|
self.each do |key,val|
|
|
|
|
hsh[key.downcase] = val
|
|
|
|
end
|
|
|
|
self.replace(hsh)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Hash # :nodoc:
|
|
|
|
include ExtendHash
|
2009-11-02 18:20:02 +00:00
|
|
|
end
|
|
|
|
|