Class: Gem::Resolv

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/vendor/resolv/lib/resolv.rb

Overview

Gem::Resolv is a thread-aware DNS resolver library written in Ruby. Gem::Resolv can handle multiple DNS requests concurrently without blocking the entire Ruby interpreter.

See also resolv-replace.rb to replace the libc resolver with Gem::Resolv.

Gem::Resolv can look up various DNS resources using the DNS module directly.

Examples:

p Gem::Resolv.getaddress "www.ruby-lang.org"
p Gem::Resolv.getname "210.251.121.214"

Gem::Resolv::DNS.open do |dns|
  ress = dns.getresources "www.ruby-lang.org", Gem::Resolv::DNS::Resource::IN::A
  p ress.map(&:address)
  ress = dns.getresources "ruby-lang.org", Gem::Resolv::DNS::Resource::IN::MX
  p ress.map { |r| [r.exchange.to_s, r.preference] }
end

Bugs

  • NIS is not supported.

  • /etc/nsswitch.conf is not supported.

Defined Under Namespace

Modules: LOC Classes: DNS, Hosts, IPv4, IPv6, MDNS, ResolvError, ResolvTimeout

Constant Summary collapse

VERSION =
"0.6.0"
DefaultResolver =

Default resolver to use for Gem::Resolv class methods.

self.new
AddressRegex =

Address Regexp to use for matching IP addresses.

/(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolvers = (arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil)) ⇒ Resolv

Creates a new Gem::Resolv using resolvers.

If resolvers is not given, a hash, or nil, uses a Hosts resolver and and a DNS resolver. If resolvers is a hash, uses the hash as configuration for the DNS resolver.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 87

def initialize(resolvers=(arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil))
  if !keyword_not_set && !arg_not_set
    warn "Support for separate use_ipv6 keyword is deprecated, as it is ignored if an argument is provided. Do not provide a positional argument if using the use_ipv6 keyword argument.", uplevel: 1
  end

  @resolvers = case resolvers
  when Hash, nil
    [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(resolvers || {}))]
  else
    resolvers
  end
end

Class Method Details

.each_address(name, &block) ⇒ Object

Iterates over all IP addresses for name.



55
56
57
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 55

def self.each_address(name, &block)
  DefaultResolver.each_address(name, &block)
end

.each_name(address, &proc) ⇒ Object

Iterates over all hostnames for address.



76
77
78
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 76

def self.each_name(address, &proc)
  DefaultResolver.each_name(address, &proc)
end

.getaddress(name) ⇒ Object

Looks up the first IP address for name.



41
42
43
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 41

def self.getaddress(name)
  DefaultResolver.getaddress(name)
end

.getaddresses(name) ⇒ Object

Looks up all IP address for name.



48
49
50
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 48

def self.getaddresses(name)
  DefaultResolver.getaddresses(name)
end

.getname(address) ⇒ Object

Looks up the hostname of address.



62
63
64
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 62

def self.getname(address)
  DefaultResolver.getname(address)
end

.getnames(address) ⇒ Object

Looks up all hostnames for address.



69
70
71
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 69

def self.getnames(address)
  DefaultResolver.getnames(address)
end

Instance Method Details

#each_address(name) ⇒ Object

Iterates over all IP addresses for name.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 120

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

#each_name(address) ⇒ Object

Iterates over all hostnames for address.



155
156
157
158
159
160
161
162
163
164
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 155

def each_name(address)
  yielded = false
  @resolvers.each {|r|
    r.each_name(address) {|name|
      yield name.to_s
      yielded = true
    }
    return if yielded
  }
end

#getaddress(name) ⇒ Object

Looks up the first IP address for name.

Raises:



103
104
105
106
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 103

def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("no address for #{name}")
end

#getaddresses(name) ⇒ Object

Looks up all IP address for name.



111
112
113
114
115
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 111

def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end

#getname(address) ⇒ Object

Looks up the hostname of address.

Raises:



138
139
140
141
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 138

def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("no name for #{address}")
end

#getnames(address) ⇒ Object

Looks up all hostnames for address.



146
147
148
149
150
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 146

def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end