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,11 +1,11 @@
1
- require 'rubygems' if "#{RUBY_VERSION}" < "1.9.0"
1
+ require 'rubygems' if RUBY_VERSION.to_s < "1.9.0"
2
2
  require 'net/dns'
3
3
 
4
4
  a = ["ibm.com", "sun.com", "redhat.com"]
5
5
 
6
6
  threads = []
7
7
 
8
- for dom in a
8
+ a.each do |dom|
9
9
  threads << Thread.new(dom) do |domain|
10
10
  res = Net::DNS::Resolver.new
11
11
  res.query(domain, Net::DNS::NS).each_nameserver do |ns|
@@ -15,8 +15,4 @@ for dom in a
15
15
  end
16
16
  end
17
17
 
18
- threads.each do |t|
19
- t.join
20
- end
21
-
22
-
18
+ threads.each(&:join)
@@ -1,10 +1,10 @@
1
- require 'net/dns/core_ext'
2
- require 'net/dns/version'
3
- require 'net/dns/resolver'
1
+ require_relative 'dns/version'
2
+ require_relative 'dns/core_ext'
3
+ require_relative 'dns/resolver'
4
+ require_relative 'dns/rr'
4
5
 
5
6
  module Net
6
7
  module DNS
7
-
8
8
  # Packet size in bytes
9
9
  PACKETSZ = 512
10
10
 
@@ -23,9 +23,7 @@ module Net
23
23
  # Size of a short int
24
24
  INT16SZ = 2
25
25
 
26
-
27
26
  module QueryTypes
28
-
29
27
  SIGZERO = 0
30
28
  A = 1
31
29
  NS = 2
@@ -82,11 +80,9 @@ module Net
82
80
  MAILB = 253
83
81
  MAILA = 254
84
82
  ANY = 255
85
-
86
83
  end
87
84
 
88
85
  module QueryClasses
89
-
90
86
  # Internet class
91
87
  IN = 1
92
88
 
@@ -101,12 +97,9 @@ module Net
101
97
 
102
98
  # Any class
103
99
  ANY = 255
104
-
105
100
  end
106
101
 
107
102
  include QueryTypes
108
103
  include QueryClasses
109
-
110
104
  end
111
-
112
105
  end
@@ -1,8 +1,6 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
-
4
3
  module HashKeys # :nodoc:
5
-
6
4
  # Returns an hash with all the
7
5
  # keys turned into downcase
8
6
  #
@@ -11,17 +9,15 @@ module Net # :nodoc:
11
9
  # #=> {"test"=>1,"foobar"=>2}
12
10
  #
13
11
  def downcase_keys!
14
- hsh = Hash.new
15
- self.each do |key,val|
12
+ hsh = {}
13
+ each do |key, val|
16
14
  hsh[key.downcase] = val
17
15
  end
18
- self.replace(hsh)
16
+ replace(hsh)
19
17
  end
20
-
21
18
  end
22
19
 
23
20
  module HashOperators # :nodoc:
24
-
25
21
  # Performs a sort of group difference
26
22
  # operation on hashes or arrays
27
23
  #
@@ -33,20 +29,17 @@ module Net # :nodoc:
33
29
  #
34
30
  def -(other)
35
31
  case other
36
- when Hash
37
- delete_if { |k,v| other.has_key?(k) }
38
- when Array
39
- delete_if { |k,v| other.include?(k) }
32
+ when Hash
33
+ delete_if { |k, v| other.key?(k) }
34
+ when Array
35
+ delete_if { |k, v| other.include?(k) }
40
36
  end
41
37
  end
42
-
43
38
  end
44
-
45
39
  end
46
40
  end
47
41
 
48
-
49
42
  class Hash # :nodoc:
50
43
  include Net::DNS::HashKeys
51
44
  include Net::DNS::HashOperators
52
- end
45
+ end
@@ -1,6 +1,5 @@
1
1
  module Net
2
2
  module DNS
3
-
4
3
  # DNS packet header class
5
4
  #
6
5
  # The Net::DNS::Header class represents the header portion of a
@@ -37,7 +36,6 @@ module Net
37
36
  # more or less the same.
38
37
  #
39
38
  class Header
40
-
41
39
  # A wrong +count+ parameter has been passed.
42
40
  class WrongCountError < ArgumentError
43
41
  end
@@ -54,7 +52,6 @@ module Net
54
52
  class Error < StandardError
55
53
  end
56
54
 
57
-
58
55
  # DNS Header RCode handling class
59
56
  #
60
57
  # It should be used internally by Net::DNS::Header class. However, it's still
@@ -103,7 +100,6 @@ module Net
103
100
  # More RCodes has to come for TSIGs and other operations.
104
101
  #
105
102
  class RCode
106
-
107
103
  # Constant for +rcode+ Response Code No Error
108
104
  NOERROR = 0
109
105
  # Constant for +rcode+ Response Code Format Error
@@ -117,27 +113,25 @@ module Net
117
113
  # Constant for +rcode+ Response Code Refused Error
118
114
  REFUSED = 5
119
115
 
120
-
121
-
122
116
  RCodeType = %w[NoError FormErr ServFail NXDomain NotImp
123
- Refused YXDomain YXRRSet NXRRSet NotAuth NotZone]
117
+ Refused YXDomain YXRRSet NXRRSet NotAuth NotZone].freeze
124
118
 
125
119
  RCodeErrorString = ["No errors",
126
- "The name server was unable to interpret the query",
127
- "The name server was unable to process this query due to problem with the name server",
128
- "Domain name referenced in the query does not exists",
129
- "The name server does not support the requested kind of query",
130
- "The name server refuses to perform the specified operation for policy reasons",
131
- "",
132
- "",
133
- "",
134
- "",
135
- ""]
120
+ "The name server was unable to interpret the query",
121
+ "The name server was unable to process this query due to problem with the name server",
122
+ "Domain name referenced in the query does not exists",
123
+ "The name server does not support the requested kind of query",
124
+ "The name server refuses to perform the specified operation for policy reasons",
125
+ "",
126
+ "",
127
+ "",
128
+ "",
129
+ "",].freeze
136
130
 
137
131
  attr_reader :code, :type, :explanation
138
132
 
139
133
  def initialize(code)
140
- if (0..10).include? code
134
+ if (0..10).cover? code
141
135
  @code = code
142
136
  @type = RCodeType[code]
143
137
  @explanation = RCodeErrorString[code]
@@ -151,7 +145,6 @@ module Net
151
145
  end
152
146
  end
153
147
 
154
-
155
148
  # Constant for +opCode+ query
156
149
  QUERY = 0
157
150
  # Constant for +opCode+ iquery
@@ -159,7 +152,7 @@ module Net
159
152
  # Constant for +opCode+ status
160
153
  STATUS = 2
161
154
  # Array with given strings
162
- OPARR = %w[QUERY IQUERY STATUS]
155
+ OPARR = %w[QUERY IQUERY STATUS].freeze
163
156
 
164
157
  # Reader for +id+ attribute
165
158
  attr_reader :id
@@ -212,7 +205,7 @@ module Net
212
205
  # See also each option for a detailed explanation of usage.
213
206
  #
214
207
  def initialize(arg = {})
215
- if arg.kind_of? Hash
208
+ if arg.is_a? Hash
216
209
  new_from_hash(arg)
217
210
  else
218
211
  raise ArgumentError, "Wrong argument class `#{arg.class}'"
@@ -232,7 +225,7 @@ module Net
232
225
  # #=> "true" if it comes from authoritative name server
233
226
  #
234
227
  def self.parse(arg)
235
- if arg.kind_of? String
228
+ if arg.is_a? String
236
229
  o = allocate
237
230
  o.send(:new_from_binary, arg)
238
231
  o
@@ -253,24 +246,24 @@ module Net
253
246
  # way of display output.
254
247
  #
255
248
  def inspect
256
- ";; id = #@id\n" +
257
- if false # @opCode == "UPDATE"
258
- #do stuff
259
- else
260
- ";; qr = #@qr\t" +
261
- "opCode: #{opCode_str}\t" +
262
- "aa = #@aa\t" +
263
- "tc = #@tc\t" +
264
- "rd = #@rd\n" +
265
- ";; ra = #@ra\t" +
266
- "ad = #@ad\t" +
267
- "cd = #@cd\t" +
268
- "rcode = #{@rCode.type}\n" +
269
- ";; qdCount = #@qdCount\t"+
270
- "anCount = #@anCount\t"+
271
- "nsCount = #@nsCount\t"+
272
- "arCount = #@arCount\n"
273
- end
249
+ ";; id = #{@id}\n" +
250
+ if false # @opCode == "UPDATE"
251
+ # do stuff
252
+ else
253
+ ";; qr = #{@qr}\t" \
254
+ "opCode: #{opCode_str}\t" \
255
+ "aa = #{@aa}\t" \
256
+ "tc = #{@tc}\t" \
257
+ "rd = #{@rd}\n" \
258
+ ";; ra = #{@ra}\t" \
259
+ "ad = #{@ad}\t" \
260
+ "cd = #{@cd}\t" \
261
+ "rcode = #{@rCode.type}\n" \
262
+ ";; qdCount = #{@qdCount}\t" \
263
+ "anCount = #{@anCount}\t" \
264
+ "nsCount = #{@nsCount}\t" \
265
+ "arCount = #{@arCount}\n"
266
+ end
274
267
  end
275
268
 
276
269
  # The Net::DNS::Header#format method prints out the header
@@ -297,7 +290,7 @@ module Net
297
290
  def format
298
291
  del = ("+-" * 16) + "+\n"
299
292
  len = del.length
300
- str = del + "|" + @id.to_s.center(len-3) + "|\n"
293
+ str = del + "|" + @id.to_s.center(len - 3) + "|\n"
301
294
  str += del + "|" + @qr.to_s
302
295
  str += "|" + @opCode.to_s.center(7)
303
296
  str += "|" + @aa.to_s
@@ -307,10 +300,10 @@ module Net
307
300
  str += "|" + @ad.to_s
308
301
  str += "|" + @cd.to_s.center(3)
309
302
  str += "|" + @rCode.to_s.center(7) + "|\n"
310
- str += del + "|" + @qdCount.to_s.center(len-3) + "|\n"
311
- str += del + "|" + @anCount.to_s.center(len-3) + "|\n"
312
- str += del + "|" + @nsCount.to_s.center(len-3) + "|\n"
313
- str += del + "|" + @arCount.to_s.center(len-3) + "|\n" + del
303
+ str += del + "|" + @qdCount.to_s.center(len - 3) + "|\n"
304
+ str += del + "|" + @anCount.to_s.center(len - 3) + "|\n"
305
+ str += del + "|" + @nsCount.to_s.center(len - 3) + "|\n"
306
+ str += del + "|" + @arCount.to_s.center(len - 3) + "|\n" + del
314
307
  str
315
308
  end
316
309
 
@@ -323,8 +316,8 @@ module Net
323
316
  def data
324
317
  arr = []
325
318
  arr.push(@id)
326
- arr.push((@qr<<7)|(@opCode<<3)|(@aa<<2)|(@tc<<1)|@rd)
327
- arr.push((@ra<<7)|(@ad<<5)|(@cd<<4)|@rCode.code)
319
+ arr.push((@qr << 7) | (@opCode << 3) | (@aa << 2) | (@tc << 1) | @rd)
320
+ arr.push((@ra << 7) | (@ad << 5) | (@cd << 4) | @rCode.code)
328
321
  arr.push(@qdCount)
329
322
  arr.push(@anCount)
330
323
  arr.push(@nsCount)
@@ -336,7 +329,7 @@ module Net
336
329
  # performing security tests.
337
330
  #
338
331
  def id=(val)
339
- if (0..65535).include? val
332
+ if (0..65_535).cover? val
340
333
  @id = val
341
334
  else
342
335
  raise ArgumentError, "ID `#{val}' out of range"
@@ -361,7 +354,7 @@ module Net
361
354
  @qr = 1
362
355
  when false
363
356
  @qr = 0
364
- when 0,1
357
+ when 0, 1
365
358
  @qr = val
366
359
  else
367
360
  raise ArgumentError, ":qr must be true(or 1) or false(or 0)"
@@ -399,7 +392,7 @@ module Net
399
392
  # header.opCode = Header::STATUS
400
393
  #
401
394
  def opCode=(val)
402
- if (0..2).include? val
395
+ if (0..2).cover? val
403
396
  @opCode = val
404
397
  else
405
398
  raise WrongOpcodeError, "Wrong opCode value (#{val}), must be QUERY, IQUERY or STATUS"
@@ -433,7 +426,7 @@ module Net
433
426
  @aa = 1
434
427
  when false
435
428
  @aa = 0
436
- when 0,1
429
+ when 0, 1
437
430
  @aa = val
438
431
  else
439
432
  raise ArgumentError, ":aa must be true(or 1) or false(or 0)"
@@ -471,7 +464,7 @@ module Net
471
464
  @tc = 1
472
465
  when false
473
466
  @tc = 0
474
- when 0,1
467
+ when 0, 1
475
468
  @tc = val
476
469
  else
477
470
  raise ArgumentError, ":tc must be true(or 1) or false(or 0)"
@@ -536,7 +529,7 @@ module Net
536
529
  @ra = 1
537
530
  when false
538
531
  @ra = 0
539
- when 0,1
532
+ when 0, 1
540
533
  @ra = val
541
534
  else
542
535
  raise ArgumentError, ":ra must be true(or 1) or false(or 0)"
@@ -560,7 +553,7 @@ module Net
560
553
  @cd = 1
561
554
  when false
562
555
  @cd = 0
563
- when 0,1
556
+ when 0, 1
564
557
  @cd = val
565
558
  else
566
559
  raise ArgumentError, ":cd must be true(or 1) or false(or 0)"
@@ -588,7 +581,7 @@ module Net
588
581
  @ad = 1
589
582
  when false
590
583
  @ad = 0
591
- when 0,1
584
+ when 0, 1
592
585
  @ad = val
593
586
  else
594
587
  raise ArgumentError, ":ad must be true(or 1) or false(or 0)"
@@ -604,7 +597,7 @@ module Net
604
597
  # #=> was unable to interpret the query
605
598
  #
606
599
  def rCode_str
607
- return rCode.type, rCode.explanation
600
+ [rCode.type, rCode.explanation]
608
601
  end
609
602
 
610
603
  # Checks for errors in the DNS packet
@@ -627,7 +620,7 @@ module Net
627
620
  # Sets the number of entries in a question section
628
621
  #
629
622
  def qdCount=(val)
630
- if (0..65535).include? val
623
+ if (0..65_535).cover? val
631
624
  @qdCount = val
632
625
  else
633
626
  raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
@@ -637,7 +630,7 @@ module Net
637
630
  # Sets the number of RRs in an answer section
638
631
  #
639
632
  def anCount=(val)
640
- if (0..65535).include? val
633
+ if (0..65_535).cover? val
641
634
  @anCount = val
642
635
  else
643
636
  raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
@@ -647,7 +640,7 @@ module Net
647
640
  # Sets the number of RRs in an authority section
648
641
  #
649
642
  def nsCount=(val)
650
- if (0..65535).include? val
643
+ if (0..65_535).cover? val
651
644
  @nsCount = val
652
645
  else
653
646
  raise WrongCountError, "Wrong number of count (#{val}), must be 0-65535"
@@ -657,14 +650,14 @@ module Net
657
650
  # Sets the number of RRs in an addictional section
658
651
  #
659
652
  def arCount=(val)
660
- if (0..65535).include? val
653
+ if (0..65_535).cover? val
661
654
  @arCount = val
662
655
  else
663
656
  raise WrongCountError, "Wrong number of count: `#{val}' must be 0-65535"
664
657
  end
665
658
  end
666
659
 
667
- private
660
+ private
668
661
 
669
662
  def new_from_scratch
670
663
  @id = genID # generate ad unique id
@@ -679,6 +672,7 @@ module Net
679
672
  unless str.size == Net::DNS::HFIXEDSZ
680
673
  raise ArgumentError, "Header binary data has wrong size: `#{str.size}' bytes"
681
674
  end
675
+
682
676
  arr = str.unpack("n C2 n4")
683
677
  @id = arr[0]
684
678
  @qr = (arr[1] >> 7) & 0x01
@@ -698,16 +692,14 @@ module Net
698
692
 
699
693
  def new_from_hash(hash)
700
694
  new_from_scratch
701
- hash.each do |key,val|
702
- eval "self.#{key.to_s} = val"
695
+ hash.each do |key, val|
696
+ eval "self.#{key} = val"
703
697
  end
704
698
  end
705
699
 
706
700
  def genID
707
- rand(65535)
701
+ rand(65_535)
708
702
  end
709
-
710
703
  end
711
-
712
704
  end
713
705
  end