Class: Dnsruby::DNS

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsruby/DNS.rb

Overview

Dnsruby::DNS class

Resolv::DNS performs DNS queries.

class methods

  • Dnsruby::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.
    
      Dnsruby::DNS.new({:nameserver=>["210.251.121.21"], :search=>["ruby-lang.org"], :ndots=>1})
    
  • Dnsruby::DNS.open(config_info=nil)

  • Dnsruby::Resolv::DNS.open(config_info=nil) {|dns| …}

methods

  • Dnsruby::DNS#close

  • Dnsruby::DNS#getaddress(name)

  • Dnsruby::DNS#getaddresses(name)

  • Dnsruby::DNS#each_address(name) {|address| …}

    address lookup methods.
    
    ((|name|)) must be an instance of Dnsruby::Name or String.  Resultant
    address is represented as an instance of Dnsruby::IPv4 or Dnsruby::IPv6.
    
  • Dnsruby::DNS#getname(address)

  • Dnsruby::DNS#getnames(address)

  • Dnsruby::DNS#each_name(address) {|name| …}

    These methods lookup hostnames .
    
    ((|address|)) must be an instance of Dnsruby::IPv4, Dnsruby::IPv6 or String.
    Resultant name is represented as an instance of Dnsruby::Name.
    
  • Dnsruby::DNS#getresource(name, type, class)

  • Dnsruby::DNS#getresources(name, type, class)

  • Dnsruby::DNS#each_resource(name, type, class) {|resource| …}

    These methods lookup DNS resources of ((|name|)).
    ((|name|)) must be a instance of Dnsruby::Name or String.
    
    ((|type|)) must be a member of Dnsruby::Types
    ((|class|)) must be a member of Dnsruby::Classes
    
    Resultant resource is represented as an instance of (a subclass of)
    Dnsruby::RR.
    (Dnsruby::RR::IN::A, etc.)
    

The searchlist and other Config info is applied to the domain name if appropriate. All the nameservers are tried (if there is no timely answer from the first).

This class uses Resolver to perform the queries.

Information taken from the following places :

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_info = nil) ⇒ DNS

Creates a new DNS resolver

config_info can be:

  • nil

    Uses platform default (e.g. /etc/resolv.conf)

  • String

    Path to a file using /etc/resolv.conf’s format

  • Hash

    Must contain :nameserver, :search and :ndots keys

    example :
    
     Dnsruby::DNS.new({:nameserver => ['210.251.121.21'],
                       :search => ['ruby-lang.org'],
                       :ndots => 1})
    


118
119
120
121
122
123
124
125
126
# File 'lib/dnsruby/DNS.rb', line 118

def initialize(config_info=nil)
  @do_caching = true
  @config = Config.new()
  @config.set_config_info(config_info)
  @resolver = Resolver.new(@config)
#      if (@resolver.single_resolvers.length == 0)
#        raise ArgumentError.new("Must pass at least one valid resolver address")
#      end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



128
129
130
# File 'lib/dnsruby/DNS.rb', line 128

def config
  @config
end

#do_cachingObject

Returns the value of attribute do_caching.



81
82
83
# File 'lib/dnsruby/DNS.rb', line 81

def do_caching
  @do_caching
end

Class Method Details

.open(*args) ⇒ Object

Creates a new DNS resolver. See Resolv::DNS.new for argument details.

Yields the created DNS resolver to the block, if given, otherwise returns it.



86
87
88
89
90
91
92
93
94
# File 'lib/dnsruby/DNS.rb', line 86

def self.open(*args)
  dns = new(*args)
  return dns unless block_given?
  begin
    yield dns
  ensure
    dns.close
  end
end

Instance Method Details

#closeObject

Closes the resolver



97
98
99
# File 'lib/dnsruby/DNS.rb', line 97

def close
  @resolver.close
end

#each_address(name) ⇒ Object

Iterates over all IP addresses of name retrieved from the DNS resolver

name can be a Dnsruby::Name or a String. Retrieved address will be a Dnsruby::IPv4 or a Dnsruby::IPv6



153
154
155
# File 'lib/dnsruby/DNS.rb', line 153

def each_address(name)
  each_resource(name) {|resource| yield resource.address}
end

#each_name(address) ⇒ Object

Iterates over all hostnames for address retrieved from the DNS resolver

address must be a Dnsruby::IPv4, Dnsruby::IPv6 or a String. Retrieved name will be a Dnsruby::Name.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dnsruby/DNS.rb', line 180

def each_name(address)
  case address
  when Name
    ptr = address
  when  IPv4, IPv6
    ptr = address.to_name
  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, Types.PTR, Classes.IN) {|resource| yield resource.domainname}
end

#each_resource(name, type = Types.A, klass = Classes.IN, &proc) ⇒ Object

Iterates over all type, klass resources for name

type defaults to Dnsruby::Types.A klass defaults to Dnsruby::Classes.IN

Yielded resource is represented as a Dnsruby::RR instance, e.g. Dnsruby::RR::IN::A



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/dnsruby/DNS.rb', line 228

def each_resource(name, type=Types.A, klass=Classes.IN, &proc)
  type = Types.new(type)
  klass = Classes.new(klass)
  reply, reply_name = send_query(name, type, klass)
  case reply.rcode.code
  when RCode::NOERROR
    extract_resources(reply, reply_name, type, klass, &proc)
    return
    #       when RCode::NXDomain
    #         Dnsruby.log.debug("RCode::NXDomain returned - raising error")
    #         raise Config::NXDomain.new(reply_name.to_s)
  else
    Dnsruby.log.error{"Unexpected rcode : #{reply.rcode.string}"}
    raise Config::OtherResolvError.new(reply_name.to_s)
  end
end

#extract_resources(msg, name, type, klass) ⇒ Object

:nodoc:



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/dnsruby/DNS.rb', line 245

def extract_resources(msg, name, type, klass) # :nodoc:
  if type == Types.ANY
    n0 = Name.create(name)
    msg.each_answer {|rec|
      yield rec if n0 == rec.name
    }
  end
  yielded = false
  n0 = Name.create(name)
  msg.each_answer {|rec|
    if n0 == rec.name
      case rec.type
      when type
        if (rec.klass == klass)
          yield rec
          yielded = true
        end
      when Types.CNAME
        n0 = rec.domainname
      end
    end
  }
  return if yielded
  msg.each_answer {|rec|
    if n0 == rec.name
      case rec.type
      when type
        if (rec.klass == klass)
          yield rec
        end
      end
    end
  }
end

#getaddress(name) ⇒ Object

Gets the first IP address of name from the DNS resolver

name can be a Dnsruby::Name or a String. Retrieved address will be a Dnsruby::IPv4 or a Dnsruby::IPv6

Raises:



134
135
136
137
# File 'lib/dnsruby/DNS.rb', line 134

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

#getaddresses(name) ⇒ Object

Gets all IP addresses of name from the DNS resolver

name can be a Dnsruby::Name or a String. Retrieved address will be a Dnsruby::IPv4 or a Dnsruby::IPv6



143
144
145
146
147
# File 'lib/dnsruby/DNS.rb', line 143

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

#getname(address) ⇒ Object

Gets the first hostname for address from the DNS resolver

address must be a Dnsruby::IPv4, Dnsruby::IPv6 or a String. Retrieved name will be a Dnsruby::Name.

Raises:



161
162
163
164
# File 'lib/dnsruby/DNS.rb', line 161

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

#getnames(address) ⇒ Object

Gets all hostnames for address from the DNS resolver

address must be a Dnsruby::IPv4, Dnsruby::IPv6 or a String. Retrieved name will be a Dnsruby::Name.



170
171
172
173
174
# File 'lib/dnsruby/DNS.rb', line 170

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

#getresource(name, type = Types.A, klass = Classes.IN) ⇒ Object

Look up the first type, klass resource for name

type defaults to Dnsruby::Types.A klass defaults to Dnsruby::Classes.IN

Returned resource is represented as a Dnsruby::RR instance, e.g. Dnsruby::RR::IN::A

Raises:



203
204
205
206
# File 'lib/dnsruby/DNS.rb', line 203

def getresource(name, type=Types.A, klass=Classes.IN)
  each_resource(name, type, klass) {|resource| return resource}
  raise ResolvError.new("DNS result has no information for #{name}")
end

#getresources(name, type = Types.A, klass = Classes.IN) ⇒ Object

Look up all type, klass resources for name

type defaults to Dnsruby::Types.A klass defaults to Dnsruby::Classes.IN

Returned resource is represented as a Dnsruby::RR instance, e.g. Dnsruby::RR::IN::A



215
216
217
218
219
# File 'lib/dnsruby/DNS.rb', line 215

def getresources(name, type=Types.A, klass=Classes.IN)
  ret = []
  each_resource(name, type, klass) {|resource| ret << resource}
  return ret
end

#send_query(name, type = Types.A, klass = Classes.IN) ⇒ Object

:nodoc:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/dnsruby/DNS.rb', line 280

def send_query(name, type=Types.A, klass=Classes.IN) # :nodoc:
  candidates = @config.generate_candidates(name)
  exception = nil
  candidates.each do |candidate|
    q = Queue.new
    msg = Message.new
    msg.header.rd = 1
    msg.add_question(candidate, type, klass)
    msg.do_validation = false
    msg.header.cd = false
    msg.do_caching = do_caching
    @resolver.do_validation = false
    @resolver.send_async(msg, q)
    _id, ret, exception = q.pop
    if (exception == nil && ret && ret.rcode == RCode.NOERROR)
      return ret, ret.question[0].qname
    end
  end
  raise exception
end

#to_sObject



102
103
104
# File 'lib/dnsruby/DNS.rb', line 102

def to_s
  return "DNS : " + @config.to_s
end