File: DNS.rb

package info (click to toggle)
dnsruby 1.54-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,184 kB
  • sloc: ruby: 15,095; makefile: 3
file content (305 lines) | stat: -rw-r--r-- 10,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#--
#Copyright 2007 Nominet UK
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License. 
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0 
#
#Unless required by applicable law or agreed to in writing, software 
#distributed under the License is distributed on an "AS IS" BASIS, 
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
#See the License for the specific language governing permissions and 
#limitations under the License.
#++
require 'Dnsruby/Hosts'
require 'Dnsruby/Config'
require "Dnsruby/Resolver"
module Dnsruby

  #== 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 :
  #* STD0013 
  #* RFC 1035, etc.
  #* ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
  #* etc.
  class DNS

    attr_accessor :do_caching
    
    #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.
    def self.open(*args)
      dns = new(*args)
      return dns unless block_given?
      begin
        yield dns
      ensure
        dns.close
      end
    end
    
    #Closes the resolver
    def close
      @resolver.close
    end
    
    
    def to_s
      return "DNS : " + @config.to_s
    end
    
    #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})
    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
    
    attr_reader :config    
    
    #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
    def getaddress(name)
      each_address(name) {|address| return address}
      raise ResolvError.new("DNS result has no information for #{name}")
    end
    
    #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
    def getaddresses(name)
      ret = []
      each_address(name) {|address| ret << address}
      return ret
    end
    
    #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
    def each_address(name)
      each_resource(name) {|resource| yield resource.address}
    end
    
    #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.
    def getname(address)
      each_name(address) {|name| return name}
      raise ResolvError.new("DNS result has no information for #{address}")
    end
    
    #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.
    def getnames(address)
      ret = []
      each_name(address) {|name| ret << name}
      return ret
    end
    
    #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.
    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
    
    #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
    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
    
    #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
    def getresources(name, type=Types.A, klass=Classes.IN)
      ret = []
      each_resource(name, type, klass) {|resource| ret << resource}
      return ret
    end
    
    #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
    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
    
    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

    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
    
  end
end
#--
#@TODO@ Asynchronous interface. Some sort of Deferrable?
#++