File: packet.rb

package info (click to toggle)
ruby-net-dns 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 3,944; makefile: 6
file content (560 lines) | stat: -rw-r--r-- 18,410 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
require 'logger'
require 'net/dns/names'
require 'net/dns/header'
require 'net/dns/question'
require 'net/dns/rr'

module Net
  module DNS
    #
    # = Net::DNS::Packet
    #
    # The Net::DNS::Packet class represents an entire DNS packet,
    # divided in his main section:
    #
    # * Header (instance of Net::DNS::Header)
    # * Question (array of Net::DNS::Question objects)
    # * Answer, Authority, Additional (each formed by an array of Net::DNS::RR
    #   objects)
    #
    # You can use this class whenever you need to create a DNS packet, whether
    # in an user application, in a resolver instance (have a look, for instance,
    # at the <tt>Net::DNS::Resolver#send</tt> method) or for a nameserver.
    #
    # For example:
    #
    #   # Create a packet
    #   packet = Net::DNS::Packet.new("www.example.com")
    #   mx = Net::DNS::Packet.new("example.com", Net::DNS::MX)
    #
    #   # Getting packet binary data, suitable for network transmission
    #   data = packet.data
    #
    # A packet object can be created from binary data too, like an
    # answer packet just received from a network stream:
    #
    #   packet = Net::DNS::Packet::parse(data)
    #
    # Each part of a packet can be gotten by the right accessors:
    #
    #   header = packet.header     # Instance of Net::DNS::Header class
    #   question = packet.question # Instance of Net::DNS::Question class
    #
    #   # Iterate over additional RRs
    #   packet.additional.each do |rr|
    #     puts "Got an #{rr.type} record"
    #   end
    #
    # Some iterators have been written to easy the access of those RRs,
    # which are often the most important. So instead of doing:
    #
    #   packet.answer.each do |rr|
    #     if rr.type == Net::DNS::RR::Types::A
    #       # do something with +rr.address+
    #     end
    #   end
    #
    # we can do:
    #
    #   packet.each_address do |ip|
    #     # do something with +ip+
    #   end
    #
    # Be sure you don't miss all the iterators in the class documentation.
    #
    # == Logging facility
    #
    # As Net::DNS::Resolver class, Net::DNS::Packet class has its own logging
    # facility too. It work in the same way the other one do, so you can
    # maybe want to override it or change the file descriptor.
    #
    #   packet = Net::DNS::Packet.new("www.example.com")
    #   packet.logger = $stderr
    #
    #   # or even
    #   packet.logger = Logger.new("/tmp/packet.log")
    #
    # If the <tt>Net::DNS::Packet</tt> class is directly instantiated by the <tt>Net::DNS::Resolver</tt>
    # class, like the great majority of the time, it will use the same logger facility.
    #
    # Logger level will be set to <tt>Logger::Debug</tt> if <tt>$DEBUG</tt> variable is set.
    #
    class Packet
      include Names

      # Base error class.
      class Error < StandardError
      end

      # Generic Packet Error.
      class PacketError < Error
      end

      attr_reader :header, :question, :answer, :authority, :additional
      attr_reader :answerfrom, :answersize

      # Creates a new instance of <tt>Net::DNS::Packet</tt> class. Arguments are the
      # canonical name of the resource, an optional type field and an optional
      # class field. The record type and class can be omitted; they default
      # to +A+ and +IN+.
      #
      #   packet = Net::DNS::Packet.new("www.example.com")
      #   packet = Net::DNS::Packet.new("example.com", Net::DNS::MX)
      #   packet = Net::DNS::Packet.new("example.com", Net::DNS::TXT, Net::DNS::CH)
      #
      # This class no longer instantiate object from binary data coming from
      # network streams. Please use <tt>Net::DNS::Packet.parse</tt> instead.
      def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
        @header = Net::DNS::Header.new(qdCount: 1)
        @question = [Net::DNS::Question.new(name, type, cls)]
        @answer = []
        @authority = []
        @additional = []
        @logger = Logger.new $stdout
        @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
      end

      # Checks if the packet is a QUERY packet
      def query?
        @header.opCode == Net::DNS::Header::QUERY
      end

      # Returns the packet object in binary data, suitable
      # for sending across a network stream.
      #
      #   packet_data = packet.data
      #   puts "Packet is #{packet_data.size} bytes long"
      #
      def data
        qdcount = ancount = nscount = arcount = 0
        data = @header.data
        headerlength = data.length

        @question.each do |question|
          data += question.data
          qdcount += 1
        end
        @answer.each do |rr|
          data += rr.data # (data.length)
          ancount += 1
        end
        @authority.each do |rr|
          data += rr.data # (data.length)
          nscount += 1
        end
        @additional.each do |rr|
          data += rr.data # (data.length)
          arcount += 1
        end

        @header.qdCount = qdcount
        @header.anCount = ancount
        @header.nsCount = nscount
        @header.arCount = arcount

        @header.data + data[Net::DNS::HFIXEDSZ..data.size]
      end

      # Same as <tt>Net::DNS::Packet#data</tt>, but implements name compression
      # (see RFC1025) for a considerable save of bytes.
      #
      #   packet = Net::DNS::Packet.new("www.example.com")
      #   puts "Size normal is #{packet.data.size} bytes"
      #   puts "Size compressed is #{packet.data_comp.size} bytes"
      #
      def data_comp
        offset = 0
        compnames = {}
        qdcount = ancount = nscount = arcount = 0
        data = @header.data
        headerlength = data.length

        @question.each do |question|
          str, offset, names = question.data
          data += str
          compnames.update(names)
          qdcount += 1
        end

        @answer.each do |rr|
          str, offset, names = rr.data(offset, compnames)
          data += str
          compnames.update(names)
          ancount += 1
        end

        @authority.each do |rr|
          str, offset, names = rr.data(offset, compnames)
          data += str
          compnames.update(names)
          nscount += 1
        end

        @additional.each do |rr|
          str, offset, names = rr.data(offset, compnames)
          data += str
          compnames.update(names)
          arcount += 1
        end

        @header.qdCount = qdcount
        @header.anCount = ancount
        @header.nsCount = nscount
        @header.arCount = arcount

        @header.data + data[Net::DNS::HFIXEDSZ..data.size]
      end

      # Returns a string containing a human-readable representation
      # of this <tt>Net::DNS::Packet</tt> instance.
      def inspect
        retval = ""
        if (@answerfrom != "0.0.0.0:0") && @answerfrom
          retval += ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"
        end

        retval += ";; HEADER SECTION\n"
        retval += @header.inspect

        retval += "\n"
        section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
        retval += ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'}):\n"
        @question.each do |qr|
          retval += ";; " + qr.inspect + "\n"
        end

        unless @answer.empty?
          retval += "\n"
          section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
          retval += ";; #{section} SECTION (#{@header.anCount} record#{@header.anCount == 1 ? '' : 's'}):\n"
          @answer.each do |rr|
            retval += rr.inspect + "\n"
          end
        end

        unless @authority.empty?
          retval += "\n"
          section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
          retval += ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'}):\n"
          @authority.each do |rr|
            retval += rr.inspect + "\n"
          end
        end

        unless @additional.empty?
          retval += "\n"
          retval += ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'}):\n"
          @additional.each do |rr|
            retval += rr.inspect + "\n"
          end
        end

        retval
      end
      alias to_s inspect

      # Delegates to <tt>Net::DNS::Header#truncated?</tt>.
      def truncated?
        @header.truncated?
      end

      # Assigns a <tt>Net::DNS::Header</tt> <tt>object</tt>
      # to this <tt>Net::DNS::Packet</tt> instance.
      def header=(object)
        if object.is_a? Net::DNS::Header
          @header = object
        else
          raise ArgumentError, "Argument must be a Net::DNS::Header object"
        end
      end

      # Assigns a <tt>Net::DNS::Question</tt> <tt>object</tt>
      # to this <tt>Net::DNS::Packet</tt> instance.
      def question=(object)
        case object
        when Array
          if object.all? { |x| x.is_a? Net::DNS::Question }
            @question = object
          else
            raise ArgumentError, "Some of the elements is not an Net::DNS::Question object"
          end
        when Net::DNS::Question
          @question = [object]
        else
          raise ArgumentError, "Invalid argument, not a Question object nor an array of objects"
        end
      end

      # Assigns one or an array of <tt>Net::DNS::RR</tt> <tt>object</tt>s
      # to the answer section of this <tt>Net::DNS::Packet</tt> instance.
      def answer=(object)
        case object
        when Array
          if object.all? { |x| x.is_a? Net::DNS::RR }
            @answer = object
          else
            raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
          end
        when Net::DNS::RR
          @answer = [object]
        else
          raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
        end
      end

      # Assigns one or an array of <tt>Net::DNS::RR</tt> <tt>object</tt>s
      # to the additional section of this <tt>Net::DNS::Packet</tt> instance.
      def additional=(object)
        case object
        when Array
          if object.all? { |x| x.is_a? Net::DNS::RR }
            @additional = object
          else
            raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
          end
        when Net::DNS::RR
          @additional = [object]
        else
          raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
        end
      end

      # Assigns one or an array of <tt>Net::DNS::RR</tt> <tt>object</tt>s
      # to the authority section of this <tt>Net::DNS::Packet</tt> instance.
      def authority=(object)
        case object
        when Array
          if object.all? { |x| x.is_a? Net::DNS::RR }
            @authority = object
          else
            raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
          end
        when Net::DNS::RR
          @authority = [object]
        else
          raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
        end
      end

      # Iterates every address in the +answer+ section
      # of this <tt>Net::DNS::Packet</tt> instance.
      #
      #   packet.each_address do |ip|
      #     ping ip.to_s
      #   end
      #
      # As you can see in the documentation for the <tt>Net::DNS::RR::A</tt> class,
      # the address returned is an instance of <tt>IPAddr</tt> class.
      def each_address(&block)
        @answer.each do |elem|
          next unless elem.class == Net::DNS::RR::A

          yield elem.address
        end
      end

      # Iterates every nameserver in the +answer+ section
      # of this <tt>Net::DNS::Packet</tt> instance.
      #
      #   packet.each_nameserver do |ns|
      #     puts "Nameserver found: #{ns}"
      #   end
      #
      def each_nameserver(&block)
        @answer.each do |elem|
          next unless elem.class == Net::DNS::RR::NS

          yield elem.nsdname
        end
      end

      # Iterates every exchange record in the +answer+ section
      # of this <tt>Net::DNS::Packet</tt> instance.
      #
      #   packet.each_mx do |pref,name|
      #     puts "Mail exchange #{name} has preference #{pref}"
      #   end
      #
      def each_mx(&block)
        @answer.each do |elem|
          next unless elem.class == Net::DNS::RR::MX

          yield elem.preference, elem.exchange
        end
      end

      # Iterates every canonical name in the +answer+ section
      # of this <tt>Net::DNS::Packet</tt> instance.
      #
      #   packet.each_cname do |cname|
      #     puts "Canonical name: #{cname}"
      #   end
      #
      def each_cname(&block)
        @answer.each do |elem|
          next unless elem.class == Net::DNS::RR::CNAME

          yield elem.cname
        end
      end

      # Iterates every pointer in the +answer+ section
      # of this <tt>Net::DNS::Packet</tt> instance.
      #
      #   packet.each_ptr do |ptr|
      #     puts "Pointer for resource: #{ptr}"
      #   end
      #
      def each_ptr(&block)
        @answer.each do |elem|
          next unless elem.class == Net::DNS::RR::PTR

          yield elem.ptrdname
        end
      end

      # Returns the packet size in bytes.
      #
      #   Resolver("www.google.com") do |packet|
      #     puts packet.size + " bytes"}
      #   end
      #   # => 484 bytes
      #
      def size
        data.size
      end

      # Checks whether the query returned a NXDOMAIN error,
      # meaning the queried domain name doesn't exist.
      #
      #   %w[a.com google.com ibm.com d.com].each do |domain|
      #     response = Net::DNS::Resolver.new.send(domain)
      #     puts "#{domain} doesn't exist" if response.nxdomain?
      #   end
      #   # => a.com doesn't exist
      #   # => d.com doesn't exist
      #
      def nxdomain?
        header.rCode.code == Net::DNS::Header::RCode::NAME
      end

      # Creates a new instance of <tt>Net::DNS::Packet</tt> class from binary data,
      # taken out from a network stream. For example:
      #
      #   # udp_socket is an UDPSocket waiting for a response
      #   ans = udp_socket.recvfrom(1500)
      #   packet = Net::DNS::Packet::parse(ans)
      #
      # An optional +from+ argument can be used to specify the information
      # of the sender. If data is passed as is from a Socket#recvfrom call,
      # the method will accept it.
      #
      # Be sure that your network data is clean from any UDP/TCP header,
      # especially when using RAW sockets.
      #
      def self.parse(*args)
        o = allocate
        o.send(:new_from_data, *args)
        o
      end

      private

      # New packet from binary data
      def new_from_data(data, from = nil)
        unless from
          if data.is_a? Array
            data, from = data
          else
            from = [0, 0, "0.0.0.0", "unknown"]
          end
        end

        @answerfrom = from[2] + ":" + from[1].to_s
        @answersize = data.size
        @logger = Logger.new $stdout
        @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN

        #------------------------------------------------------------
        # Header section
        #------------------------------------------------------------
        offset = Net::DNS::HFIXEDSZ
        @header = Net::DNS::Header.parse(data[0..offset - 1])

        @logger.debug ";; HEADER SECTION"
        @logger.debug @header.inspect

        #------------------------------------------------------------
        # Question section
        #------------------------------------------------------------
        section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
        @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'})"

        @question = []
        @header.qdCount.times do
          qobj, offset = parse_question(data, offset)
          @question << qobj
          @logger.debug ";; #{qobj.inspect}"
        end

        #------------------------------------------------------------
        # Answer/prerequisite section
        #------------------------------------------------------------
        section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
        @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'})"

        @answer = []
        @header.anCount.times do
          begin
            rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
            @answer << rrobj
            @logger.debug rrobj.inspect
          rescue NameError => e
            warn "Net::DNS unsupported record type: #{e.message}"
          end
        end

        #------------------------------------------------------------
        # Authority/update section
        #------------------------------------------------------------
        section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
        @logger.debug ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'})"

        @authority = []
        @header.nsCount.times do
          begin
            rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
            @authority << rrobj
            @logger.debug rrobj.inspect
          rescue NameError => e
            warn "Net::DNS unsupported record type: #{e.message}"
          end
        end

        #------------------------------------------------------------
        # Additional section
        #------------------------------------------------------------
        @logger.debug ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'})"

        @additional = []
        @header.arCount.times do
          begin
            rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
            @additional << rrobj
            @logger.debug rrobj.inspect
          rescue NameError => e
            warn "Net::DNS unsupported record type: #{e.message}"
          end
        end
      end

      # Parse question section
      def parse_question(data, offset)
        size = (dn_expand(data, offset)[1] - offset) + (2 * Net::DNS::INT16SZ)
        [Net::DNS::Question.parse(data[offset, size]), offset + size]
      rescue StandardError => e
        raise PacketError, "Caught exception, maybe packet malformed => #{e.message}"
      end
    end
  end
end