lib/net/dns/header.rb in net-dns-0.6.1 vs lib/net/dns/header.rb in net-dns-0.7.0
- old
+ new
@@ -1,26 +1,23 @@
-require 'net/dns'
+module Net
+ module DNS
-
-module Net # :nodoc:
- module DNS
-
#
# =Name
#
# Net::DNS::Header - DNS packet header class
#
# =Synopsis
- #
+ #
# require 'net/dns/header'
#
# =Description
- #
- # The Net::DNS::Header class represents the header portion of a
+ #
+ # 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
@@ -44,58 +41,35 @@
# 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.
+ # more or less the same.
#
- # =Error classes
- #
- # Some error classes has been defined for the Net::DNS::Header class,
- # which are listed here to keep a light and browsable main documentation.
- # We have:
- #
- # ArgumentError:: Argument Error for class Net::DNS::Packet
- # WrongCountError:: A wrong +count+ parameter has been passed
- # WrongRecursiveError:: A wrong +recursive+ parameter has been passed
- # WrongOpcodeError:: A not valid +opCode+ has been specified
- # DuplicateIDError:: The requested ID is already in use
- #
- # =Copyright
- #
- # Copyright (c) 2006 Marco Ceresa
- #
- # All rights reserved. This program is free software; you may redistribute
- # it and/or modify it under the same terms as Ruby itself.
- #
class Header
-
- # Argument Error for class Net::DNS::Header.
- class ArgumentError < ArgumentError
- end
-
- # A wrong +count+ parameter has been passed-
+
+ # A wrong +count+ parameter has been passed.
class WrongCountError < ArgumentError
end
-
- # A wrong +recursive+ parameter has been passed-
+
+ # 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
-
+
# The requested ID is already in use.
class DuplicateIDError < Error
end
-
+
#
# = Name
#
# Net::DNS::Header::RCode - DNS Header RCode handling class
#
@@ -107,17 +81,17 @@
# require 'net/dns/header'
# rcode = Net::DNS::Header::RCode.new 0
#
# =Description
#
- # 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
+ # 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
+ # * 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
@@ -135,11 +109,11 @@
# 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
+ # In the next DNS RFCs, codes 6-15 has been assigned to the following
# errors:
#
# * 6 YXDomain
# * 7 YXRRSet
# * 8 NXRRSet
@@ -147,11 +121,11 @@
# * 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
@@ -162,14 +136,14 @@
NOTIMPLEMENTED = 4
# Constant for +rcode+ Response Code Refused Error
REFUSED = 5
-
- RCodeType = %w[NoError FormErr ServFail NXDomain NotImp
- Refused YXDomain YXRRSet NXRRSet NotAuth NotZone]
-
+
+ RCodeType = %w[NoError FormErr ServFail NXDomain NotImp
+ Refused YXDomain YXRRSet NXRRSet NotAuth NotZone]
+
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",
@@ -177,40 +151,40 @@
"",
"",
"",
"",
""]
-
+
attr_reader :code, :type, :explanation
def initialize(code)
if (0..10).include? code
@code = code
@type = RCodeType[code]
- @explanation = RCodeErrorString[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]
@@id_arr = []
-
- # Reader for +id+ attribute
+
+ # Reader for +id+ attribute
attr_reader :id
# Reader for the operational code
attr_reader :opCode
# Reader for the rCode instance
attr_reader :rCode
@@ -220,14 +194,14 @@
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
+ # 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
@@ -264,44 +238,44 @@
else
raise ArgumentError, "Wrong argument class `#{arg.class}'"
end
end
- # Creates a new Net::DNS::Header object from binary data, which is
+ # 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
+ # header.auth?
+ # #=> "true" if it comes from authoritative name server
#
def self.parse(arg)
if arg.kind_of? 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
+ def inspect
";; id = #@id\n" +
if false # @opCode == "UPDATE"
#do stuff
else
";; qr = #@qr\t" +
@@ -317,15 +291,15 @@
"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.
- #
+ # 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 |
@@ -339,11 +313,11 @@
# # | 0 |
# # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# This can be very usefull for didactical purpouses :)
#
- def format
+ 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)
@@ -358,12 +332,12 @@
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
+
+ # Returns the header data in binary format, appropriate
# for use in a DNS query packet.
#
# hdata = header.data
# puts "Header is #{hdata.size} bytes"
#
@@ -391,19 +365,19 @@
@@id_arr.push 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
+ # 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)
@@ -417,11 +391,11 @@
else
raise ArgumentError, ":qr must be true(or 1) or false(or 0)"
end
end
- # Checks whether the header is a response
+ # Checks whether the header is a response
# (+qr+ bit set to 1)
#
def response?
@qr == 1
end
@@ -434,19 +408,19 @@
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.
+ # 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)
@@ -468,14 +442,14 @@
def auth?
@aa == 1
end
# Set the +aa+ flag (authoritative answer) to either +true+
- # or +false+. You can also use 0 or 1.
+ # 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
+ # 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)
@@ -488,11 +462,11 @@
@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!"
@@ -502,18 +476,18 @@
#
def truncated?
@tc == 1
end
- # Set the +tc+ flag (truncated packet) to either +true+
+ # 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
+ # 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)
@@ -526,11 +500,11 @@
@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
@@ -565,11 +539,11 @@
# 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?
@@ -600,11 +574,11 @@
#
def checking?
@cd == 0
end
- # Set the +cd+ flag (checking disabled) to either +true+
+ # 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
@@ -624,15 +598,15 @@
#
def verified?
@ad == 1
end
- # Set the +ad+ flag to either +true+
+ # 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
+ # 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
@@ -643,11 +617,11 @@
@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
@@ -665,18 +639,18 @@
# end
#
def error?
@rCode.code > 0
end
-
- # Set the rCode value. This should only be done in DNS
+
+ # 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..65535).include? val
@qdCount = val
@@ -714,20 +688,20 @@
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
+ @rCode = RCode.new(0) # no error
@anCount = @nsCount = @arCount = 0
@rd = @qdCount = 1
- @opCode = QUERY # standard query, default message
+ @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")
@@ -744,24 +718,24 @@
@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.to_s} = val"
end
end
-
+
def genID
while (@@id_arr.include?(q = rand(65535)))
end
@@id_arr.push(q)
q
end
end
end
-end
\ No newline at end of file
+end