lib/net/dns/header.rb in net-dns-0.8.0 vs lib/net/dns/header.rb in net-dns-0.9.0
- old
+ new
@@ -1,8 +1,7 @@
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.
@@ -35,11 +34,10 @@
# 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.
@@ -52,11 +50,10 @@
# 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.
#
@@ -101,11 +98,10 @@
# * 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
@@ -115,31 +111,29 @@
# 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]
+ 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",
- "",
- "",
- "",
- "",
- ""]
+ "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).include? code
+ if (0..10).cover? code
@code = code
@type = RCodeType[code]
@explanation = RCodeErrorString[code]
else
raise ArgumentError, "RCode `#{code}' out of range"
@@ -149,19 +143,18 @@
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]
+ OPARR = %w[QUERY IQUERY STATUS].freeze
# Reader for +id+ attribute
attr_reader :id
# Reader for the operational code
attr_reader :opCode
@@ -210,11 +203,11 @@
# :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.kind_of? Hash
+ if arg.is_a? Hash
new_from_hash(arg)
else
raise ArgumentError, "Wrong argument class `#{arg.class}'"
end
end
@@ -230,11 +223,11 @@
#
# header.auth?
# #=> "true" if it comes from authoritative name server
#
def self.parse(arg)
- if arg.kind_of? String
+ if arg.is_a? String
o = allocate
o.send(:new_from_binary, arg)
o
else
raise ArgumentError, "Wrong argument class `#{arg.class}'"
@@ -251,28 +244,28 @@
#
# 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
+ ";; 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.
@@ -295,24 +288,24 @@
# 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 + "|" + @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 += 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.
@@ -321,12 +314,12 @@
# 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((@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")
@@ -334,11 +327,11 @@
# Set the ID for the current header. Useful when
# performing security tests.
#
def id=(val)
- if (0..65535).include? val
+ if (0..65_535).cover? val
@id = val
else
raise ArgumentError, "ID `#{val}' out of range"
end
end
@@ -359,11 +352,11 @@
case val
when true
@qr = 1
when false
@qr = 0
- when 0,1
+ when 0, 1
@qr = val
else
raise ArgumentError, ":qr must be true(or 1) or false(or 0)"
end
end
@@ -397,11 +390,11 @@
# include Net::DNS
# header = Header.new
# header.opCode = Header::STATUS
#
def opCode=(val)
- if (0..2).include? val
+ if (0..2).cover? val
@opCode = val
else
raise WrongOpcodeError, "Wrong opCode value (#{val}), must be QUERY, IQUERY or STATUS"
end
end
@@ -431,11 +424,11 @@
case val
when true
@aa = 1
when false
@aa = 0
- when 0,1
+ when 0, 1
@aa = val
else
raise ArgumentError, ":aa must be true(or 1) or false(or 0)"
end
end
@@ -469,11 +462,11 @@
case val
when true
@tc = 1
when false
@tc = 0
- when 0,1
+ when 0, 1
@tc = val
else
raise ArgumentError, ":tc must be true(or 1) or false(or 0)"
end
end
@@ -534,11 +527,11 @@
case val
when true
@ra = 1
when false
@ra = 0
- when 0,1
+ when 0, 1
@ra = val
else
raise ArgumentError, ":ra must be true(or 1) or false(or 0)"
end
end
@@ -558,11 +551,11 @@
case val
when true
@cd = 1
when false
@cd = 0
- when 0,1
+ when 0, 1
@cd = val
else
raise ArgumentError, ":cd must be true(or 1) or false(or 0)"
end
end
@@ -586,11 +579,11 @@
case val
when true
@ad = 1
when false
@ad = 0
- when 0,1
+ when 0, 1
@ad = val
else
raise ArgumentError, ":ad must be true(or 1) or false(or 0)"
end
end
@@ -602,11 +595,11 @@
# puts "Error #{error} cause by: #{cause}" if error
# #=> Error ForErr caused by: The name server
# #=> was unable to interpret the query
#
def rCode_str
- return rCode.type, rCode.explanation
+ [rCode.type, rCode.explanation]
end
# Checks for errors in the DNS packet
#
# unless header.error?
@@ -625,48 +618,48 @@
end
# Sets the number of entries in a question section
#
def qdCount=(val)
- if (0..65535).include? 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..65535).include? 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..65535).include? 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..65535).include? val
+ if (0..65_535).cover? val
@arCount = val
else
raise WrongCountError, "Wrong number of count: `#{val}' must be 0-65535"
end
end
- private
+ private
def new_from_scratch
@id = genID # generate ad unique id
@qr = @aa = @tc = @ra = @ad = @cd = 0
@rCode = RCode.new(0) # no error
@@ -677,10 +670,11 @@
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
@@ -696,18 +690,16 @@
@arCount = arr[6]
end
def new_from_hash(hash)
new_from_scratch
- hash.each do |key,val|
- eval "self.#{key.to_s} = val"
+ hash.each do |key, val|
+ eval "self.#{key} = val"
end
end
def genID
- rand(65535)
+ rand(65_535)
end
-
end
-
end
end