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::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.
STD0013 (RFC 1035, etc.) ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
end
STDERR.print("non-handled DNS message: #{msg.inspect} from #{from.inspect}\n")
end
STDERR.print("non-handled DNS message: #{msg.inspect}")
end
STDERR.print("non-handled DNS message: #{msg.inspect}")
def subdomain_of?(other)
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 to_s
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"
ClassValue = nil
Standard (class generic) RRs
module IN
ARPA Internet specific RRs
class SRV < Resource
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
The fields specific to SRV are defined in RFC 2782 as meaning:
- priority The priority of this target host. A client MUST attempt to contact the target host with the lowest-numbered priority it can reach; target hosts with the same priority SHOULD be tried in an order defined by the weight field. The range is 0-65535. Note that it is not widely implemented and should be set to zero.
- weight A server selection mechanism. The weight field specifies a relative weight for entries with the same priority. Larger weights SHOULD be given a proportionately higher probability of being selected. The range of this number is 0-65535. Domain administrators SHOULD use Weight 0 when there isn't any server selection to do, to make the RR easier to read for humans (less noisy). Note that it is not widely implemented and should be set to zero.
- port The port on this target host of this service. The range is 0- 65535.
- target The domain name of the target host. A target of "." means that the service is decidedly not available at this domain.
def initialize(priority, weight, port, target)
Create a SRV resource record.
return DNS::Name.new(
ip6.arpa should be searched too. [RFC3152]