net-dns 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +6 -14
  2. data/.gitignore +1 -1
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +3 -0
  5. data/.rubocop_defaults.yml +364 -0
  6. data/.rubocop_todo.yml +207 -0
  7. data/.travis.yml +9 -16
  8. data/CHANGELOG.md +12 -1
  9. data/Gemfile +6 -2
  10. data/LICENSE.txt +56 -0
  11. data/README.md +94 -77
  12. data/Rakefile +23 -56
  13. data/bin/console +14 -0
  14. data/demo/check_soa.rb +27 -38
  15. data/demo/threads.rb +3 -7
  16. data/lib/net/dns.rb +4 -11
  17. data/lib/net/dns/core_ext.rb +8 -15
  18. data/lib/net/dns/header.rb +58 -66
  19. data/lib/net/dns/names.rb +25 -23
  20. data/lib/net/dns/packet.rb +136 -139
  21. data/lib/net/dns/question.rb +36 -39
  22. data/lib/net/dns/resolver.rb +103 -113
  23. data/lib/net/dns/resolver/socks.rb +45 -51
  24. data/lib/net/dns/resolver/timeouts.rb +17 -26
  25. data/lib/net/dns/rr.rb +107 -117
  26. data/lib/net/dns/rr/a.rb +46 -55
  27. data/lib/net/dns/rr/aaaa.rb +40 -49
  28. data/lib/net/dns/rr/classes.rb +26 -29
  29. data/lib/net/dns/rr/cname.rb +33 -41
  30. data/lib/net/dns/rr/hinfo.rb +44 -56
  31. data/lib/net/dns/rr/mr.rb +33 -42
  32. data/lib/net/dns/rr/mx.rb +37 -47
  33. data/lib/net/dns/rr/ns.rb +33 -41
  34. data/lib/net/dns/rr/null.rb +8 -11
  35. data/lib/net/dns/rr/ptr.rb +14 -20
  36. data/lib/net/dns/rr/soa.rb +27 -30
  37. data/lib/net/dns/rr/srv.rb +13 -17
  38. data/lib/net/dns/rr/txt.rb +8 -11
  39. data/lib/net/dns/rr/types.rb +97 -99
  40. data/lib/net/dns/version.rb +5 -13
  41. data/net-dns.gemspec +17 -29
  42. data/{fixtures → spec/fixtures}/resolv.conf +0 -0
  43. data/spec/spec_helper.rb +14 -0
  44. data/spec/unit/resolver/dns_timeout_spec.rb +36 -0
  45. data/spec/unit/resolver/tcp_timeout_spec.rb +46 -0
  46. data/spec/unit/resolver/udp_timeout_spec.rb +46 -0
  47. data/test/test_helper.rb +12 -3
  48. data/test/{header_test.rb → unit/header_test.rb} +43 -46
  49. data/test/{names_test.rb → unit/names_test.rb} +1 -1
  50. data/test/{packet_test.rb → unit/packet_test.rb} +3 -5
  51. data/test/{question_test.rb → unit/question_test.rb} +3 -5
  52. data/test/{resolver_test.rb → unit/resolver_test.rb} +10 -13
  53. data/test/{rr → unit/rr}/a_test.rb +10 -17
  54. data/test/{rr → unit/rr}/aaaa_test.rb +7 -14
  55. data/test/{rr → unit/rr}/classes_test.rb +14 -16
  56. data/test/{rr → unit/rr}/cname_test.rb +7 -14
  57. data/test/{rr → unit/rr}/hinfo_test.rb +16 -22
  58. data/test/{rr → unit/rr}/mr_test.rb +12 -18
  59. data/test/{rr → unit/rr}/mx_test.rb +18 -24
  60. data/test/{rr → unit/rr}/ns_test.rb +10 -16
  61. data/test/{rr → unit/rr}/types_test.rb +10 -8
  62. data/test/{rr_test.rb → unit/rr_test.rb} +33 -37
  63. metadata +77 -49
  64. data/test/resolver/timeouts_test.rb +0 -109
@@ -1,8 +1,6 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
-
4
3
  module Names
5
-
6
4
  # Base error class.
7
5
  class Error < StandardError
8
6
  end
@@ -11,7 +9,6 @@ module Net # :nodoc:
11
9
  class ExpandError < Error
12
10
  end
13
11
 
14
-
15
12
  INT16SZ = 2
16
13
 
17
14
  # Expand a compressed name in a DNS Packet object. Please
@@ -23,29 +20,33 @@ module Net # :nodoc:
23
20
  # offset, which indicates the point in the packet in which the
24
21
  # parsing has arrived.
25
22
  #
26
- def dn_expand(packet,offset)
23
+ def dn_expand(packet, offset)
27
24
  name = ""
28
25
  packetlen = packet.size
29
- while true
30
- raise ExpandError, "Offset is greater than packet lenght!" if packetlen < (offset+1)
26
+ loop do
27
+ raise ExpandError, "Offset is greater than packet lenght!" if packetlen < (offset + 1)
28
+
31
29
  len = packet.unpack("@#{offset} C")[0]
32
30
 
33
31
  if len == 0
34
32
  offset += 1
35
33
  break
36
34
  elsif (len & 0xC0) == 0xC0
37
- raise ExpandError, "Packet ended before offset expand" if packetlen < (offset+INT16SZ)
35
+ raise ExpandError, "Packet ended before offset expand" if packetlen < (offset + INT16SZ)
36
+
38
37
  ptr = packet.unpack("@#{offset} n")[0]
39
38
  ptr &= 0x3FFF
40
- name2 = dn_expand(packet,ptr)[0]
41
- raise ExpandError, "Packet is malformed!" if name2 == nil
39
+ name2 = dn_expand(packet, ptr)[0]
40
+ raise ExpandError, "Packet is malformed!" if name2.nil?
41
+
42
42
  name += name2
43
43
  offset += INT16SZ
44
44
  break
45
45
  else
46
46
  offset += 1
47
- raise ExpandError, "No expansion found" if packetlen < (offset+len)
48
- elem = packet[offset..offset+len-1]
47
+ raise ExpandError, "No expansion found" if packetlen < (offset + len)
48
+
49
+ elem = packet[offset..offset + len - 1]
49
50
  name += "#{elem}."
50
51
  offset += len
51
52
  end
@@ -54,16 +55,18 @@ module Net # :nodoc:
54
55
  end
55
56
 
56
57
  def pack_name(name)
57
- if name.size > 255
58
+ if name.size > 255
58
59
  raise ArgumentError, "Name may not exceed 255 chars"
59
60
  end
61
+
60
62
  arr = name.split(".")
61
63
  str = ""
62
64
  arr.each do |elem|
63
65
  if elem.size > 63
64
66
  raise ArgumentError, "Label may not exceed 63 chars"
65
67
  end
66
- str += [elem.size,elem].pack("Ca*")
68
+
69
+ str += [elem.size, elem].pack("Ca*")
67
70
  end
68
71
  str += [0].pack("C")
69
72
  str
@@ -74,22 +77,22 @@ module Net # :nodoc:
74
77
  ar = []
75
78
  string = ""
76
79
  arr.size.times do |i|
77
- x = i+1
80
+ x = i + 1
78
81
  elem = arr[-x]
79
82
  len = elem.size
80
- string = ((string.reverse)+([len,elem].pack("Ca*")).reverse).reverse
83
+ string = (string.reverse + [len, elem].pack("Ca*").reverse).reverse
81
84
  ar.unshift(string)
82
85
  end
83
- return ar
86
+ ar
84
87
  end
85
88
 
86
- def dn_comp(name,offset,compnames)
89
+ def dn_comp(name, offset, compnames)
87
90
  names = {}
88
91
  ptr = 0
89
92
  str = ""
90
93
  arr = names_array(name)
91
94
  arr.each do |entry|
92
- if compnames.has_key?(entry)
95
+ if compnames.key?(entry)
93
96
  ptr = 0xC000 | compnames[entry]
94
97
  str += [ptr].pack("n")
95
98
  offset += INT16SZ
@@ -97,22 +100,21 @@ module Net # :nodoc:
97
100
  else
98
101
  len = entry.unpack("C")[0]
99
102
  elem = entry[1..len]
100
- str += [len,elem].pack("Ca*")
101
- names.update({"#{entry}" => offset})
103
+ str += [len, elem].pack("Ca*")
104
+ names.update(entry.to_s => offset)
102
105
  offset += len
103
106
  end
104
107
  end
105
- return str,offset,names
108
+ [str, offset, names]
106
109
  end
107
110
 
108
111
  def valid?(name)
109
112
  if name =~ /^([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i
110
- return name
113
+ name
111
114
  else
112
115
  raise ArgumentError, "Invalid FQDN: #{name}"
113
116
  end
114
117
  end
115
-
116
118
  end
117
119
  end
118
120
  end
@@ -6,7 +6,6 @@ require 'net/dns/rr'
6
6
 
7
7
  module Net
8
8
  module DNS
9
-
10
9
  #
11
10
  # = Net::DNS::Packet
12
11
  #
@@ -91,7 +90,6 @@ module Net
91
90
  class PacketError < Error
92
91
  end
93
92
 
94
-
95
93
  attr_reader :header, :question, :answer, :authority, :additional
96
94
  attr_reader :answerfrom, :answersize
97
95
 
@@ -107,7 +105,7 @@ module Net
107
105
  # This class no longer instantiate object from binary data coming from
108
106
  # network streams. Please use <tt>Net::DNS::Packet.parse</tt> instead.
109
107
  def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
110
- @header = Net::DNS::Header.new(:qdCount => 1)
108
+ @header = Net::DNS::Header.new(qdCount: 1)
111
109
  @question = [Net::DNS::Question.new(name, type, cls)]
112
110
  @answer = []
113
111
  @authority = []
@@ -116,7 +114,6 @@ module Net
116
114
  @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
117
115
  end
118
116
 
119
-
120
117
  # Checks if the packet is a QUERY packet
121
118
  def query?
122
119
  @header.opCode == Net::DNS::Header::QUERY
@@ -129,7 +126,7 @@ module Net
129
126
  # puts "Packet is #{packet_data.size} bytes long"
130
127
  #
131
128
  def data
132
- qdcount=ancount=nscount=arcount=0
129
+ qdcount = ancount = nscount = arcount = 0
133
130
  data = @header.data
134
131
  headerlength = data.length
135
132
 
@@ -138,15 +135,15 @@ module Net
138
135
  qdcount += 1
139
136
  end
140
137
  @answer.each do |rr|
141
- data += rr.data#(data.length)
138
+ data += rr.data # (data.length)
142
139
  ancount += 1
143
140
  end
144
141
  @authority.each do |rr|
145
- data += rr.data#(data.length)
142
+ data += rr.data # (data.length)
146
143
  nscount += 1
147
144
  end
148
145
  @additional.each do |rr|
149
- data += rr.data#(data.length)
146
+ data += rr.data # (data.length)
150
147
  arcount += 1
151
148
  end
152
149
 
@@ -168,33 +165,33 @@ module Net
168
165
  def data_comp
169
166
  offset = 0
170
167
  compnames = {}
171
- qdcount=ancount=nscount=arcount=0
168
+ qdcount = ancount = nscount = arcount = 0
172
169
  data = @header.data
173
170
  headerlength = data.length
174
171
 
175
172
  @question.each do |question|
176
- str,offset,names = question.data
173
+ str, offset, names = question.data
177
174
  data += str
178
175
  compnames.update(names)
179
176
  qdcount += 1
180
177
  end
181
178
 
182
179
  @answer.each do |rr|
183
- str,offset,names = rr.data(offset,compnames)
180
+ str, offset, names = rr.data(offset, compnames)
184
181
  data += str
185
182
  compnames.update(names)
186
183
  ancount += 1
187
184
  end
188
185
 
189
186
  @authority.each do |rr|
190
- str,offset,names = rr.data(offset,compnames)
187
+ str, offset, names = rr.data(offset, compnames)
191
188
  data += str
192
189
  compnames.update(names)
193
190
  nscount += 1
194
191
  end
195
192
 
196
193
  @additional.each do |rr|
197
- str,offset,names = rr.data(offset,compnames)
194
+ str, offset, names = rr.data(offset, compnames)
198
195
  data += str
199
196
  compnames.update(names)
200
197
  arcount += 1
@@ -212,39 +209,39 @@ module Net
212
209
  # of this <tt>Net::DNS::Packet</tt> instance.
213
210
  def inspect
214
211
  retval = ""
215
- if @answerfrom != "0.0.0.0:0" and @answerfrom
216
- retval += ";; Answer received from #@answerfrom (#{@answersize} bytes)\n;;\n"
212
+ if (@answerfrom != "0.0.0.0:0") && @answerfrom
213
+ retval += ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"
217
214
  end
218
215
 
219
216
  retval += ";; HEADER SECTION\n"
220
217
  retval += @header.inspect
221
218
 
222
219
  retval += "\n"
223
- section = (@header.opCode == "UPDATE") ? "ZONE" : "QUESTION"
220
+ section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
224
221
  retval += ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'}):\n"
225
222
  @question.each do |qr|
226
223
  retval += ";; " + qr.inspect + "\n"
227
224
  end
228
225
 
229
- unless @answer.size == 0
226
+ unless @answer.empty?
230
227
  retval += "\n"
231
- section = (@header.opCode == "UPDATE") ? "PREREQUISITE" : "ANSWER"
228
+ section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
232
229
  retval += ";; #{section} SECTION (#{@header.anCount} record#{@header.anCount == 1 ? '' : 's'}):\n"
233
230
  @answer.each do |rr|
234
231
  retval += rr.inspect + "\n"
235
232
  end
236
233
  end
237
234
 
238
- unless @authority.size == 0
235
+ unless @authority.empty?
239
236
  retval += "\n"
240
- section = (@header.opCode == "UPDATE") ? "UPDATE" : "AUTHORITY"
237
+ section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
241
238
  retval += ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'}):\n"
242
239
  @authority.each do |rr|
243
240
  retval += rr.inspect + "\n"
244
241
  end
245
242
  end
246
243
 
247
- unless @additional.size == 0
244
+ unless @additional.empty?
248
245
  retval += "\n"
249
246
  retval += ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'}):\n"
250
247
  @additional.each do |rr|
@@ -254,7 +251,7 @@ module Net
254
251
 
255
252
  retval
256
253
  end
257
- alias_method :to_s, :inspect
254
+ alias to_s inspect
258
255
 
259
256
  # Delegates to <tt>Net::DNS::Header#truncated?</tt>.
260
257
  def truncated?
@@ -264,7 +261,7 @@ module Net
264
261
  # Assigns a <tt>Net::DNS::Header</tt> <tt>object</tt>
265
262
  # to this <tt>Net::DNS::Packet</tt> instance.
266
263
  def header=(object)
267
- if object.kind_of? Net::DNS::Header
264
+ if object.is_a? Net::DNS::Header
268
265
  @header = object
269
266
  else
270
267
  raise ArgumentError, "Argument must be a Net::DNS::Header object"
@@ -276,7 +273,7 @@ module Net
276
273
  def question=(object)
277
274
  case object
278
275
  when Array
279
- if object.all? {|x| x.kind_of? Net::DNS::Question}
276
+ if object.all? { |x| x.is_a? Net::DNS::Question }
280
277
  @question = object
281
278
  else
282
279
  raise ArgumentError, "Some of the elements is not an Net::DNS::Question object"
@@ -292,16 +289,16 @@ module Net
292
289
  # to the answer section of this <tt>Net::DNS::Packet</tt> instance.
293
290
  def answer=(object)
294
291
  case object
295
- when Array
296
- if object.all? {|x| x.kind_of? Net::DNS::RR}
297
- @answer = object
298
- else
299
- raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
300
- end
301
- when Net::DNS::RR
302
- @answer = [object]
292
+ when Array
293
+ if object.all? { |x| x.is_a? Net::DNS::RR }
294
+ @answer = object
303
295
  else
304
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
296
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
297
+ end
298
+ when Net::DNS::RR
299
+ @answer = [object]
300
+ else
301
+ raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
305
302
  end
306
303
  end
307
304
 
@@ -309,16 +306,16 @@ module Net
309
306
  # to the additional section of this <tt>Net::DNS::Packet</tt> instance.
310
307
  def additional=(object)
311
308
  case object
312
- when Array
313
- if object.all? {|x| x.kind_of? Net::DNS::RR}
314
- @additional = object
315
- else
316
- raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
317
- end
318
- when Net::DNS::RR
319
- @additional = [object]
309
+ when Array
310
+ if object.all? { |x| x.is_a? Net::DNS::RR }
311
+ @additional = object
320
312
  else
321
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
313
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
314
+ end
315
+ when Net::DNS::RR
316
+ @additional = [object]
317
+ else
318
+ raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
322
319
  end
323
320
  end
324
321
 
@@ -326,16 +323,16 @@ module Net
326
323
  # to the authority section of this <tt>Net::DNS::Packet</tt> instance.
327
324
  def authority=(object)
328
325
  case object
329
- when Array
330
- if object.all? {|x| x.kind_of? Net::DNS::RR}
331
- @authority = object
332
- else
333
- raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
334
- end
335
- when Net::DNS::RR
336
- @authority = [object]
326
+ when Array
327
+ if object.all? { |x| x.is_a? Net::DNS::RR }
328
+ @authority = object
337
329
  else
338
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
330
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
331
+ end
332
+ when Net::DNS::RR
333
+ @authority = [object]
334
+ else
335
+ raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
339
336
  end
340
337
  end
341
338
 
@@ -351,6 +348,7 @@ module Net
351
348
  def each_address(&block)
352
349
  @answer.each do |elem|
353
350
  next unless elem.class == Net::DNS::RR::A
351
+
354
352
  yield elem.address
355
353
  end
356
354
  end
@@ -365,6 +363,7 @@ module Net
365
363
  def each_nameserver(&block)
366
364
  @answer.each do |elem|
367
365
  next unless elem.class == Net::DNS::RR::NS
366
+
368
367
  yield elem.nsdname
369
368
  end
370
369
  end
@@ -379,6 +378,7 @@ module Net
379
378
  def each_mx(&block)
380
379
  @answer.each do |elem|
381
380
  next unless elem.class == Net::DNS::RR::MX
381
+
382
382
  yield elem.preference, elem.exchange
383
383
  end
384
384
  end
@@ -393,6 +393,7 @@ module Net
393
393
  def each_cname(&block)
394
394
  @answer.each do |elem|
395
395
  next unless elem.class == Net::DNS::RR::CNAME
396
+
396
397
  yield elem.cname
397
398
  end
398
399
  end
@@ -407,6 +408,7 @@ module Net
407
408
  def each_ptr(&block)
408
409
  @answer.each do |elem|
409
410
  next unless elem.class == Net::DNS::RR::PTR
411
+
410
412
  yield elem.ptrdname
411
413
  end
412
414
  end
@@ -436,7 +438,6 @@ module Net
436
438
  header.rCode.code == Net::DNS::Header::RCode::NAME
437
439
  end
438
440
 
439
-
440
441
  # Creates a new instance of <tt>Net::DNS::Packet</tt> class from binary data,
441
442
  # taken out from a network stream. For example:
442
443
  #
@@ -459,105 +460,101 @@ module Net
459
460
 
460
461
  private
461
462
 
462
- # New packet from binary data
463
- def new_from_data(data, from = nil)
464
- unless from
465
- if data.kind_of? Array
466
- data, from = data
467
- else
468
- from = [0, 0, "0.0.0.0", "unknown"]
469
- end
463
+ # New packet from binary data
464
+ def new_from_data(data, from = nil)
465
+ unless from
466
+ if data.is_a? Array
467
+ data, from = data
468
+ else
469
+ from = [0, 0, "0.0.0.0", "unknown"]
470
470
  end
471
+ end
471
472
 
472
- @answerfrom = from[2] + ":" + from[1].to_s
473
- @answersize = data.size
474
- @logger = Logger.new $stdout
475
- @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
476
-
477
- #------------------------------------------------------------
478
- # Header section
479
- #------------------------------------------------------------
480
- offset = Net::DNS::HFIXEDSZ
481
- @header = Net::DNS::Header.parse(data[0..offset-1])
482
-
483
- @logger.debug ";; HEADER SECTION"
484
- @logger.debug @header.inspect
485
-
486
- #------------------------------------------------------------
487
- # Question section
488
- #------------------------------------------------------------
489
- section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
490
- @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '': 's'})"
491
-
492
- @question = []
493
- @header.qdCount.times do
494
- qobj,offset = parse_question(data,offset)
495
- @question << qobj
496
- @logger.debug ";; #{qobj.inspect}"
497
- end
473
+ @answerfrom = from[2] + ":" + from[1].to_s
474
+ @answersize = data.size
475
+ @logger = Logger.new $stdout
476
+ @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
498
477
 
499
- #------------------------------------------------------------
500
- # Answer/prerequisite section
501
- #------------------------------------------------------------
502
- section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
503
- @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '': 's'})"
504
-
505
- @answer = []
506
- @header.anCount.times do
507
- begin
508
- rrobj,offset = Net::DNS::RR.parse_packet(data,offset)
509
- @answer << rrobj
510
- @logger.debug rrobj.inspect
511
- rescue NameError => e
512
- warn "Net::DNS unsupported record type: #{e.message}"
513
- end
514
- end
478
+ #------------------------------------------------------------
479
+ # Header section
480
+ #------------------------------------------------------------
481
+ offset = Net::DNS::HFIXEDSZ
482
+ @header = Net::DNS::Header.parse(data[0..offset - 1])
483
+
484
+ @logger.debug ";; HEADER SECTION"
485
+ @logger.debug @header.inspect
486
+
487
+ #------------------------------------------------------------
488
+ # Question section
489
+ #------------------------------------------------------------
490
+ section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
491
+ @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'})"
492
+
493
+ @question = []
494
+ @header.qdCount.times do
495
+ qobj, offset = parse_question(data, offset)
496
+ @question << qobj
497
+ @logger.debug ";; #{qobj.inspect}"
498
+ end
515
499
 
516
- #------------------------------------------------------------
517
- # Authority/update section
518
- #------------------------------------------------------------
519
- section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
520
- @logger.debug ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '': 's'})"
521
-
522
- @authority = []
523
- @header.nsCount.times do
524
- begin
525
- rrobj,offset = Net::DNS::RR.parse_packet(data,offset)
526
- @authority << rrobj
527
- @logger.debug rrobj.inspect
528
- rescue NameError => e
529
- warn "Net::DNS unsupported record type: #{e.message}"
530
- end
531
- end
500
+ #------------------------------------------------------------
501
+ # Answer/prerequisite section
502
+ #------------------------------------------------------------
503
+ section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
504
+ @logger.debug ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'})"
532
505
 
533
- #------------------------------------------------------------
534
- # Additional section
535
- #------------------------------------------------------------
536
- @logger.debug ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '': 's'})"
537
-
538
- @additional = []
539
- @header.arCount.times do
540
- begin
541
- rrobj,offset = Net::DNS::RR.parse_packet(data,offset)
542
- @additional << rrobj
543
- @logger.debug rrobj.inspect
544
- rescue NameError => e
545
- warn "Net::DNS supported record type: #{e.message}"
546
- end
506
+ @answer = []
507
+ @header.anCount.times do
508
+ begin
509
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
510
+ @answer << rrobj
511
+ @logger.debug rrobj.inspect
512
+ rescue NameError => e
513
+ warn "Net::DNS unsupported record type: #{e.message}"
547
514
  end
515
+ end
516
+
517
+ #------------------------------------------------------------
518
+ # Authority/update section
519
+ #------------------------------------------------------------
520
+ section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
521
+ @logger.debug ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'})"
548
522
 
523
+ @authority = []
524
+ @header.nsCount.times do
525
+ begin
526
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
527
+ @authority << rrobj
528
+ @logger.debug rrobj.inspect
529
+ rescue NameError => e
530
+ warn "Net::DNS unsupported record type: #{e.message}"
531
+ end
549
532
  end
550
533
 
534
+ #------------------------------------------------------------
535
+ # Additional section
536
+ #------------------------------------------------------------
537
+ @logger.debug ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'})"
551
538
 
552
- # Parse question section
553
- def parse_question(data,offset)
554
- size = (dn_expand(data, offset)[1] - offset) + (2 * Net::DNS::INT16SZ)
555
- return [Net::DNS::Question.parse(data[offset, size]), offset + size]
556
- rescue StandardError => e
557
- raise PacketError, "Caught exception, maybe packet malformed => #{e.message}"
539
+ @additional = []
540
+ @header.arCount.times do
541
+ begin
542
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
543
+ @additional << rrobj
544
+ @logger.debug rrobj.inspect
545
+ rescue NameError => e
546
+ warn "Net::DNS unsupported record type: #{e.message}"
547
+ end
558
548
  end
549
+ end
559
550
 
551
+ # Parse question section
552
+ def parse_question(data, offset)
553
+ size = (dn_expand(data, offset)[1] - offset) + (2 * Net::DNS::INT16SZ)
554
+ [Net::DNS::Question.parse(data[offset, size]), offset + size]
555
+ rescue StandardError => e
556
+ raise PacketError, "Caught exception, maybe packet malformed => #{e.message}"
557
+ end
560
558
  end
561
-
562
559
  end
563
560
  end