Class: ReDNS::Message

Inherits:
Fragment show all
Defined in:
lib/redns/message.rb

Constant Summary collapse

SECTIONS =

Constants ============================================================

[ :questions, :answers, :nameservers, :additional_records ].freeze

Instance Attribute Summary

Attributes inherited from Fragment

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fragment

attribute, #initialize

Methods included from Support

#addr_to_arpa, #bind_all_addr, #default_nameservers, #default_resolver_address, #dns_port, #inet_aton, #inet_ntoa, #io_nonblock, #io_nonblock?, #io_set_nonblock, #is_ip?

Constructor Details

This class inherits a constructor from ReDNS::Fragment

Class Method Details

.question(name, qtype = nil) {|message| ... } ⇒ Object

Constructs a question that asks for more information about a given resource with an optional query type specified. Query type defaults to :ptr for dotted-quad IP addresses, :a otherwise.

Yields:

  • (message)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/redns/message.rb', line 34

def self.question(name, qtype = nil)
  if (!qtype)
    if (ReDNS::Support.is_ip?(name))
      name = ReDNS::Support.addr_to_arpa(name)
      qtype = :ptr
    else
      qtype = :a
    end
  end
  
  message = new(
    :questions => [
      ReDNS::Question.new(
        :name => name,
        :qtype => qtype
      )
    ]
  )
  
  yield(message) if (block_given?)
  
  message
end

Instance Method Details

#deserialize(buffer) ⇒ Object

Extracts a message from the supplied buffer. Will return the message if successful, nil if an error occurred or no suitable data cound be found in the buffer.



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
# File 'lib/redns/message.rb', line 145

def deserialize(buffer)
  return unless (buffer)
  
	data = buffer.unpack("nnnnnn")
	
	# Abandon efforts to decode if insufficient data is available.
	return if (data.length < 6)
	
	self.id = data.shift

	flags = data.shift
	self.query = (flags & 0x8000 == 0)
	self.opcode = ReDNS::OPCODE_LABEL[(flags & 0x7800) >> 12]
	self.authorative = (flags & 0x0400 != 0)
	self.truncated = (flags & 0x0200 != 0)
	self.recursion_desired = (flags & 0x0100 != 0)
	self.recursion_available = (flags & 0x0080 != 0)
	self.response_code = ReDNS::RCODE_LABEL[flags & 0x000F]
	
	SECTIONS.each do |section|
	  @attributes[:"#{section}_count"] = data.shift
  end
	
	SECTIONS.each do |section|
	  collection = @attributes[section] = [ ]
	  
	  decode_class =
 		  case (section)
 	    when :questions
 	      ReDNS::Question
      else
        ReDNS::Resource
       end
       
	  @attributes[:"#{section}_count"].times do
	    collection << decode_class.new(buffer)
    end
  end
  
  self
end

#empty?Boolean

Returns true if the questions, answers, nameservers and additional records are all empty, false otherwise.

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/redns/message.rb', line 100

def empty?
	questions.empty? and
	  answers.empty? and
	  nameservers.empty? and
	  additional_records.empty?
end

#increment_id!Object

Instance Methods =====================================================



60
61
62
# File 'lib/redns/message.rb', line 60

def increment_id!
  self.id = (self.id + 1) % 0x10000
end

#lengthObject

Returns the length of the encoded DNS request.



94
95
96
# File 'lib/redns/message.rb', line 94

def length
	to_dns.length
end

#response?Boolean

Returns true if this is a response type message, false otherwise, as is the case with query messages.

Returns:

  • (Boolean)


66
67
68
# File 'lib/redns/message.rb', line 66

def response?
  !self.query?
end

#serialize(buffer = ReDNS::Buffer.new) ⇒ Object

Serializes the message into a supplied buffer, or allocates a new one to store it. Returns the buffer used.



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
# File 'lib/redns/message.rb', line 114

def serialize(buffer = ReDNS::Buffer.new)
  buffer.pack(
	  'nnnnnn',
		self.id,
		(
 			(self.query? ? 0 : 0x8000) |
 			(ReDNS::OPCODE[self.opcode] || ReDNS::OPCODE[:unknown]) << 12 |
 			(self.authorative? ? 0x0400 : 0) |
 			(self.truncated? ? 0x0200 : 0) |
 			(self.recursion_desired? ? 0x0100 : 0) |
 			(self.recursion_available? ? 0x0080 : 0) |
 			(ReDNS::RCODE[self.response_code] || ReDNS::RCODE[:noerror])
		),
	  self.questions.length,
		self.answers.length,
		self.nameservers.length,
		self.additional_records.length
 	)

   [ :questions, :answers, :nameservers, :additional_records ].each do |section|
     @attributes[section] and @attributes[section].each do |part|
       part.serialize(buffer)
     end
   end
   
   buffer
end

#to_sObject

Returns a string representation of the message in a format similar to what the dig shell utility produces.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/redns/message.rb', line 72

def to_s
  flags = [ ]
  flags << 'qr' if (response?)
  flags << 'aa' if (authorative?)
  flags << 'tc' if (truncated?)
  flags << 'rd' if (recursion_desired?)
  flags << 'ra' if (recursion_available?)
  
	";; HEADER:\n;; opcode: #{opcode.to_s.upcase} status: #{response_code.to_s.upcase} id: #{id} \n" +
	";; flags: #{flags.join(' ')}; QUERY: #{questions.length}, ANSWER: #{answers.length}, AUTHORITY: #{nameservers.length}, ADDITIONAL: #{additional_records.length}" +
	"\n" +
	";; QUESTION SECTION:\n" +
	questions.collect(&:to_s).join("\n") + "\n" +
	";; ANSWER SECTION:\n" +
	answers.collect(&:to_s).join("\n") + "\n" +
	";; NAMESERVER SECTION:\n" +
	nameservers.collect(&:to_s).join("\n") + "\n" +
	";; ADDITIONAL SECTION:\n" +
	additional_records.collect(&:to_s).join("\n") + "\n"
end

#to_yamlObject

Returns a YAML serialized version of the message.



108
109
110
# File 'lib/redns/message.rb', line 108

def to_yaml
	@attributes.to_yaml
end