File: header.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 (705 lines) | stat: -rw-r--r-- 22,289 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
module Net
  module DNS
    # DNS packet header class
    #
    # The Net::DNS::Header class represents the header portion of a
    # DNS packet. An Header object is created whenever a new packet
    # is parsed or as user request.
    #
    #   header = Net::DNS::Header.new
    #   # ;; id = 18123
    #   # ;; qr = 0       opCode: 0       aa = 0  tc = 0  rd = 1
    #   # ;; ra = 0       ad = 0  cd = 0  rcode = 0
    #   # ;; qdCount = 1  anCount = 0     nsCount = 0     arCount = 0
    #
    #   header.format
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |             18123             |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |0|   0   |0|0|1|0|0| 0 |   0   |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |               1               |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |               0               |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |               0               |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #   #  |               0               |
    #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    #
    #   # packet is an instance of Net::DNS::Packet
    #   header = packet.header
    #   puts "Answer is #{header.auth? ? '' : 'non'} authoritative"
    #
    # A lot of methods were written to keep a compatibility layer with
    # the Perl version of the library, as long as methods name which are
    # more or less the same.
    #
    class Header
      # A wrong +count+ parameter has been passed.
      class WrongCountError < ArgumentError
      end

      # A wrong +recursive+ parameter has been passed.
      class WrongRecursiveError < ArgumentError
      end

      # An invalid +opCode+ has been specified.
      class WrongOpcodeError < ArgumentError
      end

      # Base error class.
      class Error < StandardError
      end

      # DNS Header RCode handling class
      #
      # It should be used internally by Net::DNS::Header class. However, it's still
      # possible to instantiate it directly.
      #
      #   require 'net/dns/header'
      #   rcode = Net::DNS::Header::RCode.new 0
      #
      # The RCode class represents the RCode field in the Header portion of a
      # DNS packet. This field (called Response Code) is used to get informations
      # about the status of a DNS operation, such as a query or an update. These
      # are the values in the original Mockapetris's standard (RFC1035):
      #
      # * 0               No error condition
      # * 1               Format error - The name server was unable to interpret
      #                   the query.
      # * 2               Server failure - The name server was
      #                   unable to process this query due to a
      #                   problem with the name server.
      # * 3               Name Error - Meaningful only for
      #                   responses from an authoritative name
      #                   server, this code means that the
      #                   domain name referenced in the query does
      #                   not exist.
      # * 4               Not Implemented - The name server does
      #                   not support the requested kind of query.
      # * 5               Refused - The name server refuses to
      #                   perform the specified operation for
      #                   policy reasons.  For example, a name
      #                   server may not wish to provide the
      #                   information to the particular requester,
      #                   or a name server may not wish to perform
      #                   a particular operation (e.g., zone
      #                   transfer) for particular data.
      # * 6-15            Reserved for future use.
      #
      # In the next DNS RFCs, codes 6-15 has been assigned to the following
      # errors:
      #
      # * 6               YXDomain
      # * 7               YXRRSet
      # * 8               NXRRSet
      # * 9               NotAuth
      # * 10              NotZone
      #
      # More RCodes has to come for TSIGs and other operations.
      #
      class RCode
        # Constant for +rcode+ Response Code No Error
        NOERROR = 0
        # Constant for +rcode+ Response Code Format Error
        FORMAT = 1
        # Constant for +rcode+ Response Code Server Format Error
        SERVER = 2
        # Constant for +rcode+ Response Code Name Error
        NAME = 3
        # Constant for +rcode+ Response Code Not Implemented Error
        NOTIMPLEMENTED = 4
        # Constant for +rcode+ Response Code Refused Error
        REFUSED = 5

        RCodeType = %w[NoError FormErr ServFail NXDomain NotImp
                       Refused YXDomain YXRRSet NXRRSet NotAuth NotZone].freeze

        RCodeErrorString = ["No errors",
                            "The name server was unable to interpret the query",
                            "The name server was unable to process this query due to problem with the name server",
                            "Domain name referenced in the query does not exists",
                            "The name server does not support the requested kind of query",
                            "The name server refuses to perform the specified operation for policy reasons",
                            "",
                            "",
                            "",
                            "",
                            "",].freeze

        attr_reader :code, :type, :explanation

        def initialize(code)
          if (0..10).cover? code
            @code         = code
            @type         = RCodeType[code]
            @explanation  = RCodeErrorString[code]
          else
            raise ArgumentError, "RCode `#{code}' out of range"
          end
        end

        def to_s
          @code.to_s
        end
      end

      # Constant for +opCode+ query
      QUERY   = 0
      # Constant for +opCode+ iquery
      IQUERY  = 1
      # Constant for +opCode+ status
      STATUS  = 2
      # Array with given strings
      OPARR = %w[QUERY IQUERY STATUS].freeze

      # Reader for +id+ attribute
      attr_reader :id
      # Reader for the operational code
      attr_reader :opCode
      # Reader for the rCode instance
      attr_reader :rCode
      # Reader for question section entries number
      attr_reader :qdCount
      # Reader for answer section entries number
      attr_reader :anCount
      # Reader for authority section entries number
      attr_reader :nsCount
      # Reader for addictional section entries number
      attr_reader :arCount

      # Creates a new Net::DNS::Header object with the desired values,
      # which can be specified as an Hash argument. When called without
      # arguments, defaults are used. If a data string is passed, values
      # are taken from parsing the string.
      #
      # Examples:
      #
      #   # Create a new Net::DNS::Header object
      #   header = Net::DNS::Header.new
      #
      #   # Create a new Net::DNS::Header object passing values
      #   header = Net::DNS::Header.new(:opCode => 1, :rd => 0)
      #
      #   # Create a new Net::DNS::Header object with binary data
      #   header = Net::DNS::Header.new(data)
      #
      # Default values are:
      #
      #   :id => auto generated
      #   :qr      => 0 # Query response flag
      #   :aa      => 0 # Authoritative answer flag
      #   :tc      => 0 # Truncated packet flag
      #   :ra      => 0 # Recursiond available flag
      #   :rCode   => 0 # Response code (status of the query)
      #   :opCode  => 0 # Operational code (purpose of the query)
      #   :cd      => 0 # Checking disable flag
      #   :ad      => 0 # Only relevant in DNSSEC context
      #   :rd      => 1 # Recursion desired flag
      #   :qdCount => 1 # Number of questions in the dns packet
      #   :anCount => 0 # Number of answer RRs in the dns packet
      #   :nsCount => 0 # Number of authoritative RRs in the dns packet
      #   :arCount => 0 # Number of additional RRs in the dns packet
      #
      # See also each option for a detailed explanation of usage.
      #
      def initialize(arg = {})
        if arg.is_a? Hash
          new_from_hash(arg)
        else
          raise ArgumentError, "Wrong argument class `#{arg.class}'"
        end
      end

      # Creates a new Net::DNS::Header object from binary data, which is
      # passed as a string object as argument.
      # The configurations parameters are taken from parsing the string.
      #
      # Example:
      #
      #   # Create a new Net::DNS::Header object with binary data
      #   header = Net::DNS::Header.new(data)
      #
      #   header.auth?
      #     #=> "true" if it comes from authoritative name server
      #
      def self.parse(arg)
        if arg.is_a? String
          o = allocate
          o.send(:new_from_binary, arg)
          o
        else
          raise ArgumentError, "Wrong argument class `#{arg.class}'"
        end
      end

      # Inspect method, prints out all the options and relative values.
      #
      #   p Net::DNS::Header.new
      #   # ;; id = 18123
      #   # ;; qr = 0       opCode: 0       aa = 0  tc = 0  rd = 1
      #   # ;; ra = 0       ad = 0  cd = 0  rcode = 0
      #   # ;; qdCount = 1  anCount = 0     nsCount = 0     arCount = 0
      #
      # This method will maybe be changed in the future to a more pretty
      # way of display output.
      #
      def inspect
        ";; id = #{@id}\n" +
            if false # @opCode == "UPDATE"
              # do stuff
            else
              ";; qr = #{@qr}\t" \
              "opCode: #{opCode_str}\t" \
              "aa = #{@aa}\t" \
              "tc = #{@tc}\t" \
              "rd = #{@rd}\n" \
              ";; ra = #{@ra}\t" \
              "ad = #{@ad}\t" \
              "cd = #{@cd}\t" \
              "rcode = #{@rCode.type}\n" \
              ";; qdCount = #{@qdCount}\t" \
              "anCount = #{@anCount}\t" \
              "nsCount = #{@nsCount}\t" \
              "arCount = #{@arCount}\n"
            end
      end

      # The Net::DNS::Header#format method prints out the header
      # in a special ascii representation of data, in a way
      # similar to those often found on RFCs.
      #
      #   p Net::DNS::Header.new.format
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |             18123             |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |0|   0   |0|0|1|0|0| 0 |   0   |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |               1               |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |               0               |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |               0               |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #   #  |               0               |
      #   #  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      #
      # This can be very usefull for didactical purpouses :)
      #
      def format
        del = ("+-" * 16) + "+\n"
        len = del.length
        str = del + "|" + @id.to_s.center(len - 3) + "|\n"
        str += del + "|" + @qr.to_s
        str += "|" + @opCode.to_s.center(7)
        str += "|" + @aa.to_s
        str += "|" + @tc.to_s
        str += "|" + @rd.to_s
        str += "|" + @ra.to_s
        str += "|" + @ad.to_s
        str += "|" + @cd.to_s.center(3)
        str += "|" + @rCode.to_s.center(7) + "|\n"
        str += del + "|" + @qdCount.to_s.center(len - 3) + "|\n"
        str += del + "|" + @anCount.to_s.center(len - 3) + "|\n"
        str += del + "|" + @nsCount.to_s.center(len - 3) + "|\n"
        str += del + "|" + @arCount.to_s.center(len - 3) + "|\n" + del
        str
      end

      # Returns the header data in binary format, appropriate
      # for use in a DNS query packet.
      #
      #   hdata = header.data
      #   puts "Header is #{hdata.size} bytes"
      #
      def data
        arr = []
        arr.push(@id)
        arr.push((@qr << 7) | (@opCode << 3) | (@aa << 2) | (@tc << 1) | @rd)
        arr.push((@ra << 7) | (@ad << 5) | (@cd << 4) | @rCode.code)
        arr.push(@qdCount)
        arr.push(@anCount)
        arr.push(@nsCount)
        arr.push(@arCount)
        arr.pack("n C2 n4")
      end

      # Set the ID for the current header. Useful when
      # performing security tests.
      #
      def id=(val)
        if (0..65_535).cover? val
          @id = val
        else
          raise ArgumentError, "ID `#{val}' out of range"
        end
      end

      # Checks whether the header is a query (+qr+ bit set to 0)
      #
      def query?
        @qr == 0
      end

      # Set the +qr+ query response flag to be either +true+ or
      # +false+. You can also use the values 0 and 1. This flag
      # indicates if the DNS packet contains a query or an answer,
      # so it should be set to +true+ in DNS answer packets.
      # If +qr+ is +true+, the packet is a response.
      #
      def qr=(val)
        case val
        when true
          @qr = 1
        when false
          @qr = 0
        when 0, 1
          @qr = val
        else
          raise ArgumentError, ":qr must be true(or 1) or false(or 0)"
        end
      end

      # Checks whether the header is a response
      # (+qr+ bit set to 1)
      #
      def response?
        @qr == 1
      end

      # Returns a string representation of the +opCode+
      #
      #   puts "Packet is a #{header.opCode_str}"
      #   #=> Packet is a QUERY
      #
      def opCode_str
        OPARR[@opCode]
      end

      # Set the +opCode+ variable to a new value. This fields indicates
      # the type of the question present in the DNS packet; +val+ can be
      # one of the values QUERY, IQUERY or STATUS.
      #
      # * QUERY is the standard DNS query
      # * IQUERY is the inverse query
      # * STATUS is used to query the nameserver for its status
      #
      # Example:
      #
      #   include Net::DNS
      #   header = Header.new
      #   header.opCode = Header::STATUS
      #
      def opCode=(val)
        if (0..2).cover? val
          @opCode = val
        else
          raise WrongOpcodeError, "Wrong opCode value (#{val}), must be QUERY, IQUERY or STATUS"
        end
      end

      # Checks whether the response is authoritative
      #
      #   if header.auth?
      #     puts "Response is authoritative"
      #   else
      #     puts "Answer is NOT authoritative"
      #   end
      #
      def auth?
        @aa == 1
      end

      # Set the +aa+ flag (authoritative answer) to either +true+
      # or +false+. You can also use 0 or 1.
      #
      # This flag indicates whether a DNS answer packet contains
      # authoritative data, meaning that is was generated by a
      # nameserver authoritative for the domain of the question.
      #
      # Must only be set to +true+ in DNS answer packets.
      #
      def aa=(val)
        case val
        when true
          @aa = 1
        when false
          @aa = 0
        when 0, 1
          @aa = val
        else
          raise ArgumentError, ":aa must be true(or 1) or false(or 0)"
        end
      end

      # Checks whether the packet was truncated
      #
      #   # Sending packet using UDP
      #   if header.truncated?
      #     puts "Warning, packet has been truncated!"
      #     # Sending packet using TCP
      #   end
      #   # Do something with the answer
      #
      def truncated?
        @tc == 1
      end

      # Set the +tc+ flag (truncated packet) to either +true+
      # ot +false+. You can also use 0 or 1.
      #
      # The truncated flag is used in response packets to indicate
      # that the amount of data to be trasmitted exceedes the
      # maximum allowed by the protocol in use, tipically UDP, and
      # that the data present in the packet has been truncated.
      # A different protocol (such has TCP) need to be used to
      # retrieve full data.
      #
      # Must only be set in DNS answer packets.
      #
      def tc=(val)
        case val
        when true
          @tc = 1
        when false
          @tc = 0
        when 0, 1
          @tc = val
        else
          raise ArgumentError, ":tc must be true(or 1) or false(or 0)"
        end
      end

      # Checks whether the packet has a recursion bit
      # set, meaning that recursion is desired
      #
      def recursive?
        @rd == 1
      end

      # Sets the recursion desidered bit.
      # Remember that recursion query support is
      # optional.
      #
      #   header.recursive = true
      #   hdata = header.data # suitable for sending
      #
      # Consult RFC1034 and RFC1035 for a detailed explanation
      # of how recursion works.
      #
      def recursive=(val)
        case val
        when true
          @rd = 1
        when false
          @rd = 0
        when 1
          @rd = 1
        when 0
          @rd = 0
        else
          raise WrongRecursiveError, "Wrong value (#{val}), please specify true (1) or false (0)"
        end
      end

      # Alias for Header#recursive= to keep compatibility
      # with the Perl version.
      #
      def rd=(val)
        self.recursive = val
      end

      # Checks whether recursion is available.
      # This flag is usually set by nameservers to indicate
      # that they support recursive-type queries.
      #
      def r_available?
        @ra == 1
      end

      # Set the +ra+ flag (recursion available) to either +true+ or
      # +false+. You can also use 0 and 1.
      #
      # This flag must only be set in DNS answer packets.
      #
      def ra=(val)
        case val
        when true
          @ra = 1
        when false
          @ra = 0
        when 0, 1
          @ra = val
        else
          raise ArgumentError, ":ra must be true(or 1) or false(or 0)"
        end
      end

      # Checks whether checking is enabled or disabled.
      #
      # Checking is enabled by default.
      #
      def checking?
        @cd == 0
      end

      # Set the +cd+ flag (checking disabled) to either +true+
      # ot +false+. You can also use 0 or 1.
      #
      def cd=(val)
        case val
        when true
          @cd = 1
        when false
          @cd = 0
        when 0, 1
          @cd = val
        else
          raise ArgumentError, ":cd must be true(or 1) or false(or 0)"
        end
      end

      # Checks whether +ad+ flag has been set.
      #
      # This flag is only relevant in DNSSEC context.
      #
      def verified?
        @ad == 1
      end

      # Set the +ad+ flag  to either +true+
      # ot +false+. You can also use 0 or 1.
      #
      # The AD bit is only set on answers where signatures have
      # been cryptographically verified or the server is
      # authoritative for the data and is allowed to set the bit by policy.
      #
      def ad=(val)
        case val
        when true
          @ad = 1
        when false
          @ad = 0
        when 0, 1
          @ad = val
        else
          raise ArgumentError, ":ad must be true(or 1) or false(or 0)"
        end
      end

      # Returns an error array for the header response code, or
      # +nil+ if no error is generated.
      #
      #   error, cause = header.rCode_str
      #   puts "Error #{error} cause by: #{cause}" if error
      #     #=> Error ForErr caused by: The name server
      #     #=> was unable to interpret the query
      #
      def rCode_str
        [rCode.type, rCode.explanation]
      end

      # Checks for errors in the DNS packet
      #
      #   unless header.error?
      #     puts "No errors in DNS answer packet"
      #   end
      #
      def error?
        @rCode.code > 0
      end

      # Set the rCode value. This should only be done in DNS
      # answer packets.
      #
      def rCode=(val)
        @rCode = RCode.new(val)
      end

      # Sets the number of entries in a question section
      #
      def qdCount=(val)
        if (0..65_535).cover? val
          @qdCount = val
        else
          raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
        end
      end

      # Sets the number of RRs in an answer section
      #
      def anCount=(val)
        if (0..65_535).cover? val
          @anCount = val
        else
          raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
        end
      end

      # Sets the number of RRs in an authority section
      #
      def nsCount=(val)
        if (0..65_535).cover? val
          @nsCount = val
        else
          raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
        end
      end

      # Sets the number of RRs in an addictional section
      #
      def arCount=(val)
        if (0..65_535).cover? val
          @arCount = val
        else
          raise WrongCountError, "Wrong number of count: `#{val}' must be 0-65535"
        end
      end

      private

      def new_from_scratch
        @id = genID # generate ad unique id
        @qr = @aa = @tc = @ra = @ad = @cd = 0
        @rCode = RCode.new(0) # no error
        @anCount = @nsCount = @arCount = 0
        @rd = @qdCount = 1
        @opCode = QUERY # standard query, default message
      end

      def new_from_binary(str)
        unless str.size == Net::DNS::HFIXEDSZ
          raise ArgumentError, "Header binary data has wrong size: `#{str.size}' bytes"
        end

        arr = str.unpack("n C2 n4")
        @id          =  arr[0]
        @qr          = (arr[1] >> 7) & 0x01
        @opCode      = (arr[1] >> 3) & 0x0F
        @aa          = (arr[1] >> 2) & 0x01
        @tc          = (arr[1] >> 1) & 0x01
        @rd          =  arr[1] & 0x1
        @ra          = (arr[2] >> 7) & 0x01
        @ad          = (arr[2] >> 5) & 0x01
        @cd          = (arr[2] >> 4) & 0x01
        @rCode       = RCode.new(arr[2] & 0xf)
        @qdCount     =  arr[3]
        @anCount     =  arr[4]
        @nsCount     =  arr[5]
        @arCount     =  arr[6]
      end

      def new_from_hash(hash)
        new_from_scratch
        hash.each do |key, val|
          eval "self.#{key} = val"
        end
      end

      def genID
        rand(65_535)
      end
    end
  end
end