lib/net/dns/header.rb in net-dns-0.9.0 vs lib/net/dns/header.rb in net-dns-0.20.0

- old
+ new

@@ -126,20 +126,20 @@ "", "", "", "",].freeze - attr_reader :code, :type, :explanation + attr_reader :code + attr_reader :type + attr_reader :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 + raise ArgumentError, "RCode `#{code}' out of range" unless (0..10).cover? code + + @code = code + @type = RCodeType[code] + @explanation = RCodeErrorString[code] end def to_s @code.to_s end @@ -203,15 +203,13 @@ # :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 + raise ArgumentError, "Wrong argument class `#{arg.class}'" unless arg.is_a? Hash + + new_from_hash(arg) 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. @@ -223,17 +221,15 @@ # # 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 + raise ArgumentError, "Wrong argument class `#{arg.class}'" unless arg.is_a? String + + o = allocate + o.send(:new_from_binary, arg) + o end # Inspect method, prints out all the options and relative values. # # p Net::DNS::Header.new @@ -249,22 +245,22 @@ ";; 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" + "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 @@ -327,15 +323,13 @@ # 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 + raise ArgumentError, "ID `#{val}' out of range" unless (0..65_535).cover? val + + @id = val end # Checks whether the header is a query (+qr+ bit set to 0) # def query? @@ -390,15 +384,13 @@ # 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 + raise WrongOpcodeError, "Wrong opCode value (#{val}), must be QUERY, IQUERY or STATUS" unless (0..2).cover? val + + @opCode = val end # Checks whether the response is authoritative # # if header.auth? @@ -618,45 +610,37 @@ 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 + raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535" unless (0..65_535).cover? val + + @qdCount = val 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 + raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535" unless (0..65_535).cover? val + + @anCount = val 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 + raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535" unless (0..65_535).cover? val + + @nsCount = val 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 + raise WrongCountError, "Wrong number of count: `#{val}' must be 0-65535" unless (0..65_535).cover? val + + @arCount = val end private def new_from_scratch @@ -672,15 +656,15 @@ 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] + @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 + @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]