net-dns 0.8.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +6 -14
  2. data/.rspec +1 -0
  3. data/.travis.yml +9 -16
  4. data/CHANGELOG.md +37 -13
  5. data/LICENSE.txt +56 -0
  6. data/README.md +94 -77
  7. data/demo/check_soa.rb +27 -38
  8. data/demo/threads.rb +3 -7
  9. data/lib/net/dns/header.rb +86 -110
  10. data/lib/net/dns/names.rb +31 -31
  11. data/lib/net/dns/packet.rb +148 -158
  12. data/lib/net/dns/question.rb +41 -42
  13. data/lib/net/dns/resolver/socks.rb +47 -55
  14. data/lib/net/dns/resolver/timeouts.rb +19 -30
  15. data/lib/net/dns/resolver.rb +151 -176
  16. data/lib/net/dns/rr/a.rb +45 -55
  17. data/lib/net/dns/rr/aaaa.rb +39 -50
  18. data/lib/net/dns/rr/classes.rb +32 -37
  19. data/lib/net/dns/rr/cname.rb +31 -41
  20. data/lib/net/dns/rr/hinfo.rb +40 -56
  21. data/lib/net/dns/rr/mr.rb +31 -42
  22. data/lib/net/dns/rr/mx.rb +35 -47
  23. data/lib/net/dns/rr/ns.rb +31 -41
  24. data/lib/net/dns/rr/null.rb +10 -15
  25. data/lib/net/dns/rr/ptr.rb +16 -24
  26. data/lib/net/dns/rr/soa.rb +36 -35
  27. data/lib/net/dns/rr/srv.rb +18 -19
  28. data/lib/net/dns/rr/txt.rb +11 -16
  29. data/lib/net/dns/rr/types.rb +118 -109
  30. data/lib/net/dns/rr.rb +107 -117
  31. data/lib/net/dns/version.rb +5 -13
  32. data/lib/net/dns.rb +6 -11
  33. metadata +18 -83
  34. data/.gitignore +0 -8
  35. data/Gemfile +0 -4
  36. data/Rakefile +0 -71
  37. data/fixtures/resolv.conf +0 -4
  38. data/lib/net/dns/core_ext.rb +0 -52
  39. data/net-dns.gemspec +0 -35
  40. data/test/header_test.rb +0 -167
  41. data/test/names_test.rb +0 -21
  42. data/test/packet_test.rb +0 -49
  43. data/test/question_test.rb +0 -83
  44. data/test/resolver/timeouts_test.rb +0 -109
  45. data/test/resolver_test.rb +0 -117
  46. data/test/rr/a_test.rb +0 -113
  47. data/test/rr/aaaa_test.rb +0 -109
  48. data/test/rr/classes_test.rb +0 -85
  49. data/test/rr/cname_test.rb +0 -97
  50. data/test/rr/hinfo_test.rb +0 -117
  51. data/test/rr/mr_test.rb +0 -105
  52. data/test/rr/mx_test.rb +0 -112
  53. data/test/rr/ns_test.rb +0 -86
  54. data/test/rr/types_test.rb +0 -69
  55. data/test/rr_test.rb +0 -131
  56. data/test/test_helper.rb +0 -4
@@ -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,9 +90,13 @@ module Net
91
90
  class PacketError < Error
92
91
  end
93
92
 
94
-
95
- attr_reader :header, :question, :answer, :authority, :additional
96
- attr_reader :answerfrom, :answersize
93
+ attr_reader :header
94
+ attr_reader :question
95
+ attr_reader :answer
96
+ attr_reader :authority
97
+ attr_reader :additional
98
+ attr_reader :answerfrom
99
+ attr_reader :answersize
97
100
 
98
101
  # Creates a new instance of <tt>Net::DNS::Packet</tt> class. Arguments are the
99
102
  # canonical name of the resource, an optional type field and an optional
@@ -107,7 +110,7 @@ module Net
107
110
  # This class no longer instantiate object from binary data coming from
108
111
  # network streams. Please use <tt>Net::DNS::Packet.parse</tt> instead.
109
112
  def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
110
- @header = Net::DNS::Header.new(:qdCount => 1)
113
+ @header = Net::DNS::Header.new(qdCount: 1)
111
114
  @question = [Net::DNS::Question.new(name, type, cls)]
112
115
  @answer = []
113
116
  @authority = []
@@ -116,7 +119,6 @@ module Net
116
119
  @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
117
120
  end
118
121
 
119
-
120
122
  # Checks if the packet is a QUERY packet
121
123
  def query?
122
124
  @header.opCode == Net::DNS::Header::QUERY
@@ -129,7 +131,7 @@ module Net
129
131
  # puts "Packet is #{packet_data.size} bytes long"
130
132
  #
131
133
  def data
132
- qdcount=ancount=nscount=arcount=0
134
+ qdcount = ancount = nscount = arcount = 0
133
135
  data = @header.data
134
136
  headerlength = data.length
135
137
 
@@ -138,15 +140,15 @@ module Net
138
140
  qdcount += 1
139
141
  end
140
142
  @answer.each do |rr|
141
- data += rr.data#(data.length)
143
+ data += rr.data # (data.length)
142
144
  ancount += 1
143
145
  end
144
146
  @authority.each do |rr|
145
- data += rr.data#(data.length)
147
+ data += rr.data # (data.length)
146
148
  nscount += 1
147
149
  end
148
150
  @additional.each do |rr|
149
- data += rr.data#(data.length)
151
+ data += rr.data # (data.length)
150
152
  arcount += 1
151
153
  end
152
154
 
@@ -168,33 +170,33 @@ module Net
168
170
  def data_comp
169
171
  offset = 0
170
172
  compnames = {}
171
- qdcount=ancount=nscount=arcount=0
173
+ qdcount = ancount = nscount = arcount = 0
172
174
  data = @header.data
173
175
  headerlength = data.length
174
176
 
175
177
  @question.each do |question|
176
- str,offset,names = question.data
178
+ str, offset, names = question.data
177
179
  data += str
178
180
  compnames.update(names)
179
181
  qdcount += 1
180
182
  end
181
183
 
182
184
  @answer.each do |rr|
183
- str,offset,names = rr.data(offset,compnames)
185
+ str, offset, names = rr.data(offset, compnames)
184
186
  data += str
185
187
  compnames.update(names)
186
188
  ancount += 1
187
189
  end
188
190
 
189
191
  @authority.each do |rr|
190
- str,offset,names = rr.data(offset,compnames)
192
+ str, offset, names = rr.data(offset, compnames)
191
193
  data += str
192
194
  compnames.update(names)
193
195
  nscount += 1
194
196
  end
195
197
 
196
198
  @additional.each do |rr|
197
- str,offset,names = rr.data(offset,compnames)
199
+ str, offset, names = rr.data(offset, compnames)
198
200
  data += str
199
201
  compnames.update(names)
200
202
  arcount += 1
@@ -212,39 +214,39 @@ module Net
212
214
  # of this <tt>Net::DNS::Packet</tt> instance.
213
215
  def inspect
214
216
  retval = ""
215
- if @answerfrom != "0.0.0.0:0" and @answerfrom
216
- retval += ";; Answer received from #@answerfrom (#{@answersize} bytes)\n;;\n"
217
+ if (@answerfrom != "0.0.0.0:0") && @answerfrom
218
+ retval += ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"
217
219
  end
218
220
 
219
221
  retval += ";; HEADER SECTION\n"
220
222
  retval += @header.inspect
221
223
 
222
224
  retval += "\n"
223
- section = (@header.opCode == "UPDATE") ? "ZONE" : "QUESTION"
225
+ section = @header.opCode == "UPDATE" ? "ZONE" : "QUESTION"
224
226
  retval += ";; #{section} SECTION (#{@header.qdCount} record#{@header.qdCount == 1 ? '' : 's'}):\n"
225
227
  @question.each do |qr|
226
228
  retval += ";; " + qr.inspect + "\n"
227
229
  end
228
230
 
229
- unless @answer.size == 0
231
+ unless @answer.empty?
230
232
  retval += "\n"
231
- section = (@header.opCode == "UPDATE") ? "PREREQUISITE" : "ANSWER"
233
+ section = @header.opCode == "UPDATE" ? "PREREQUISITE" : "ANSWER"
232
234
  retval += ";; #{section} SECTION (#{@header.anCount} record#{@header.anCount == 1 ? '' : 's'}):\n"
233
235
  @answer.each do |rr|
234
236
  retval += rr.inspect + "\n"
235
237
  end
236
238
  end
237
239
 
238
- unless @authority.size == 0
240
+ unless @authority.empty?
239
241
  retval += "\n"
240
- section = (@header.opCode == "UPDATE") ? "UPDATE" : "AUTHORITY"
242
+ section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
241
243
  retval += ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'}):\n"
242
244
  @authority.each do |rr|
243
245
  retval += rr.inspect + "\n"
244
246
  end
245
247
  end
246
248
 
247
- unless @additional.size == 0
249
+ unless @additional.empty?
248
250
  retval += "\n"
249
251
  retval += ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'}):\n"
250
252
  @additional.each do |rr|
@@ -254,7 +256,7 @@ module Net
254
256
 
255
257
  retval
256
258
  end
257
- alias_method :to_s, :inspect
259
+ alias to_s inspect
258
260
 
259
261
  # Delegates to <tt>Net::DNS::Header#truncated?</tt>.
260
262
  def truncated?
@@ -264,11 +266,9 @@ module Net
264
266
  # Assigns a <tt>Net::DNS::Header</tt> <tt>object</tt>
265
267
  # to this <tt>Net::DNS::Packet</tt> instance.
266
268
  def header=(object)
267
- if object.kind_of? Net::DNS::Header
268
- @header = object
269
- else
270
- raise ArgumentError, "Argument must be a Net::DNS::Header object"
271
- end
269
+ raise ArgumentError, "Argument must be a Net::DNS::Header object" unless object.is_a? Net::DNS::Header
270
+
271
+ @header = object
272
272
  end
273
273
 
274
274
  # Assigns a <tt>Net::DNS::Question</tt> <tt>object</tt>
@@ -276,11 +276,10 @@ module Net
276
276
  def question=(object)
277
277
  case object
278
278
  when Array
279
- if object.all? {|x| x.kind_of? Net::DNS::Question}
280
- @question = object
281
- else
282
- raise ArgumentError, "Some of the elements is not an Net::DNS::Question object"
283
- end
279
+ raise ArgumentError, "Some of the elements is not an Net::DNS::Question object" unless object.all? { |x| x.is_a? Net::DNS::Question }
280
+
281
+ @question = object
282
+
284
283
  when Net::DNS::Question
285
284
  @question = [object]
286
285
  else
@@ -292,16 +291,15 @@ module Net
292
291
  # to the answer section of this <tt>Net::DNS::Packet</tt> instance.
293
292
  def answer=(object)
294
293
  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]
303
- else
304
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
294
+ when Array
295
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object" unless object.all? { |x| x.is_a? Net::DNS::RR }
296
+
297
+ @answer = object
298
+
299
+ when Net::DNS::RR
300
+ @answer = [object]
301
+ else
302
+ raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
305
303
  end
306
304
  end
307
305
 
@@ -309,16 +307,15 @@ module Net
309
307
  # to the additional section of this <tt>Net::DNS::Packet</tt> instance.
310
308
  def additional=(object)
311
309
  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]
320
- else
321
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
310
+ when Array
311
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object" unless object.all? { |x| x.is_a? Net::DNS::RR }
312
+
313
+ @additional = object
314
+
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,15 @@ 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]
337
- else
338
- raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
326
+ when Array
327
+ raise ArgumentError, "Some of the elements is not an Net::DNS::RR object" unless object.all? { |x| x.is_a? Net::DNS::RR }
328
+
329
+ @authority = object
330
+
331
+ when Net::DNS::RR
332
+ @authority = [object]
333
+ else
334
+ raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
339
335
  end
340
336
  end
341
337
 
@@ -348,9 +344,10 @@ module Net
348
344
  #
349
345
  # As you can see in the documentation for the <tt>Net::DNS::RR::A</tt> class,
350
346
  # the address returned is an instance of <tt>IPAddr</tt> class.
351
- def each_address(&block)
347
+ def each_address
352
348
  @answer.each do |elem|
353
349
  next unless elem.class == Net::DNS::RR::A
350
+
354
351
  yield elem.address
355
352
  end
356
353
  end
@@ -362,9 +359,10 @@ module Net
362
359
  # puts "Nameserver found: #{ns}"
363
360
  # end
364
361
  #
365
- def each_nameserver(&block)
362
+ def each_nameserver
366
363
  @answer.each do |elem|
367
364
  next unless elem.class == Net::DNS::RR::NS
365
+
368
366
  yield elem.nsdname
369
367
  end
370
368
  end
@@ -376,9 +374,10 @@ module Net
376
374
  # puts "Mail exchange #{name} has preference #{pref}"
377
375
  # end
378
376
  #
379
- def each_mx(&block)
377
+ def each_mx
380
378
  @answer.each do |elem|
381
379
  next unless elem.class == Net::DNS::RR::MX
380
+
382
381
  yield elem.preference, elem.exchange
383
382
  end
384
383
  end
@@ -390,9 +389,10 @@ module Net
390
389
  # puts "Canonical name: #{cname}"
391
390
  # end
392
391
  #
393
- def each_cname(&block)
392
+ def each_cname
394
393
  @answer.each do |elem|
395
394
  next unless elem.class == Net::DNS::RR::CNAME
395
+
396
396
  yield elem.cname
397
397
  end
398
398
  end
@@ -404,9 +404,10 @@ module Net
404
404
  # puts "Pointer for resource: #{ptr}"
405
405
  # end
406
406
  #
407
- def each_ptr(&block)
407
+ def each_ptr
408
408
  @answer.each do |elem|
409
409
  next unless elem.class == Net::DNS::RR::PTR
410
+
410
411
  yield elem.ptrdname
411
412
  end
412
413
  end
@@ -436,7 +437,6 @@ module Net
436
437
  header.rCode.code == Net::DNS::Header::RCode::NAME
437
438
  end
438
439
 
439
-
440
440
  # Creates a new instance of <tt>Net::DNS::Packet</tt> class from binary data,
441
441
  # taken out from a network stream. For example:
442
442
  #
@@ -459,105 +459,95 @@ module Net
459
459
 
460
460
  private
461
461
 
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
462
+ # New packet from binary data
463
+ def new_from_data(data, from = nil)
464
+ unless from
465
+ if data.is_a? Array
466
+ data, from = data
467
+ else
468
+ from = [0, 0, "0.0.0.0", "unknown"]
470
469
  end
470
+ end
471
471
 
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
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
498
476
 
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
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
515
498
 
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
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'})"
532
504
 
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
547
- end
505
+ @answer = []
506
+ @header.anCount.times do
507
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
508
+ @answer << rrobj
509
+ @logger.debug rrobj.inspect
510
+ rescue NameError => e
511
+ warn "Net::DNS unsupported record type: #{e.message}"
512
+ end
548
513
 
514
+ #------------------------------------------------------------
515
+ # Authority/update section
516
+ #------------------------------------------------------------
517
+ section = @header.opCode == "UPDATE" ? "UPDATE" : "AUTHORITY"
518
+ @logger.debug ";; #{section} SECTION (#{@header.nsCount} record#{@header.nsCount == 1 ? '' : 's'})"
519
+
520
+ @authority = []
521
+ @header.nsCount.times do
522
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
523
+ @authority << rrobj
524
+ @logger.debug rrobj.inspect
525
+ rescue NameError => e
526
+ warn "Net::DNS unsupported record type: #{e.message}"
549
527
  end
550
528
 
529
+ #------------------------------------------------------------
530
+ # Additional section
531
+ #------------------------------------------------------------
532
+ @logger.debug ";; ADDITIONAL SECTION (#{@header.arCount} record#{@header.arCount == 1 ? '' : 's'})"
551
533
 
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}"
534
+ @additional = []
535
+ @header.arCount.times do
536
+ rrobj, offset = Net::DNS::RR.parse_packet(data, offset)
537
+ @additional << rrobj
538
+ @logger.debug rrobj.inspect
539
+ rescue NameError => e
540
+ warn "Net::DNS unsupported record type: #{e.message}"
558
541
  end
542
+ end
559
543
 
544
+ # Parse question section
545
+ def parse_question(data, offset)
546
+ size = (dn_expand(data, offset)[1] - offset) + (2 * Net::DNS::INT16SZ)
547
+ [Net::DNS::Question.parse(data[offset, size]), offset + size]
548
+ rescue StandardError => e
549
+ raise PacketError, "Caught exception, maybe packet malformed => #{e.message}"
550
+ end
560
551
  end
561
-
562
552
  end
563
553
  end