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
data/lib/net/dns/rr/mr.rb CHANGED
@@ -1,21 +1,16 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
-
4
3
  class RR
5
-
6
4
  #
7
5
  # = Mail Rename Record (MR)
8
6
  #
9
7
  # Class for DNS MR resource records.
10
8
  #
11
9
  class MR < RR
12
-
13
10
  # Gets the newname value.
14
11
  #
15
12
  # Returns a String.
16
- def newname
17
- @newname
18
- end
13
+ attr_reader :newname
19
14
 
20
15
  # Gets the standardized value for this record,
21
16
  # represented by the value of <tt>newname</tt>.
@@ -25,55 +20,49 @@ module Net # :nodoc:
25
20
  newname.to_s
26
21
  end
27
22
 
28
-
29
23
  private
30
24
 
31
- def subclass_new_from_hash(options)
32
- if options.has_key?(:newname)
33
- @newname = check_name(options[:newname])
34
- else
35
- raise ArgumentError, ":newname field is mandatory"
36
- end
37
- end
25
+ def subclass_new_from_hash(options)
26
+ raise ArgumentError, ":newname field is mandatory" unless options.key?(:newname)
38
27
 
39
- def subclass_new_from_string(str)
40
- @newname = check_name(str)
41
- end
42
-
43
- def subclass_new_from_binary(data, offset)
44
- @newname = dn_expand(data,offset)
45
- offset
46
- end
28
+ @newname = check_name(options[:newname])
29
+ end
47
30
 
31
+ def subclass_new_from_string(str)
32
+ @newname = check_name(str)
33
+ end
48
34
 
49
- def set_type
50
- @type = Net::DNS::RR::Types.new("MR")
51
- end
35
+ def subclass_new_from_binary(data, offset)
36
+ @newname = dn_expand(data, offset)
37
+ offset
38
+ end
52
39
 
53
- def get_inspect
54
- value
55
- end
40
+ def set_type
41
+ @type = Net::DNS::RR::Types.new("MR")
42
+ end
56
43
 
44
+ def get_inspect
45
+ value
46
+ end
57
47
 
58
- def check_name(input)
59
- name = input.to_s
60
- unless name =~ /(\w\.?)+\s*$/
61
- raise ArgumentError, "Invalid Domain Name `#{name}'"
62
- end
63
- name
48
+ def check_name(input)
49
+ name = input.to_s
50
+ unless name =~ /(\w\.?)+\s*$/
51
+ raise ArgumentError, "Invalid Domain Name `#{name}'"
64
52
  end
65
53
 
66
- def build_pack
67
- @newname_pack = pack_name(@newname)
68
- @rdlength = @newname_pack.size
69
- end
54
+ name
55
+ end
70
56
 
71
- def get_data
72
- @newname_pack
73
- end
57
+ def build_pack
58
+ @newname_pack = pack_name(@newname)
59
+ @rdlength = @newname_pack.size
60
+ end
74
61
 
62
+ def get_data
63
+ @newname_pack
64
+ end
75
65
  end
76
-
77
66
  end
78
67
  end
79
68
  end
data/lib/net/dns/rr/mx.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #
6
5
  # = Mail Exchange Record (MX)
7
6
  #
@@ -12,20 +11,15 @@ module Net # :nodoc:
12
11
  # The MX RR is used by SMTP (Mail) Agents to route mail for the domain.
13
12
  #
14
13
  class MX < RR
15
-
16
14
  # Gets the preference value.
17
15
  #
18
16
  # Returns an Integer.
19
- def preference
20
- @preference
21
- end
17
+ attr_reader :preference
22
18
 
23
19
  # Gets the exchange value.
24
20
  #
25
21
  # Returns a String.
26
- def exchange
27
- @exchange
28
- end
22
+ attr_reader :exchange
29
23
 
30
24
  # Gets the standardized value for this record,
31
25
  # represented by the value of <tt>preference</tt> and <tt>exchange</tt>.
@@ -35,58 +29,52 @@ module Net # :nodoc:
35
29
  "#{preference} #{exchange}"
36
30
  end
37
31
 
38
-
39
32
  private
40
33
 
41
- def subclass_new_from_hash(options)
42
- if options.has_key?(:preference) && options.has_key?(:exchange)
43
- @preference = options[:preference].to_i
44
- @exchange = options[:exchange]
45
- else
46
- raise ArgumentError, ":preference and :exchange fields are mandatory"
47
- end
48
- end
34
+ def subclass_new_from_hash(options)
35
+ raise ArgumentError, ":preference and :exchange fields are mandatory" unless options.key?(:preference) && options.key?(:exchange)
49
36
 
50
- def subclass_new_from_string(str)
51
- @preference, @exchange = check_mx(str)
52
- end
53
-
54
- def subclass_new_from_binary(data, offset)
55
- @preference = data.unpack("@#{offset} n")[0]
56
- offset += 2
57
- @exchange, offset = dn_expand(data, offset)
58
- offset
59
- end
37
+ @preference = options[:preference].to_i
38
+ @exchange = options[:exchange]
39
+ end
60
40
 
41
+ def subclass_new_from_string(str)
42
+ @preference, @exchange = check_mx(str)
43
+ end
61
44
 
62
- def set_type
63
- @type = Net::DNS::RR::Types.new("MX")
64
- end
45
+ def subclass_new_from_binary(data, offset)
46
+ @preference = data.unpack1("@#{offset} n")
47
+ offset += 2
48
+ @exchange, offset = dn_expand(data, offset)
49
+ offset
50
+ end
65
51
 
66
- def get_inspect
67
- value
68
- end
52
+ def set_type
53
+ @type = Net::DNS::RR::Types.new("MX")
54
+ end
69
55
 
56
+ def get_inspect
57
+ value
58
+ end
70
59
 
71
- def check_mx(input)
72
- str = input.to_s
73
- unless str.strip =~ /^(\d+)\s+(\S+)$/
74
- raise ArgumentError, "Invalid MX section `#{str}'"
75
- end
76
- [$1.to_i, $2]
60
+ def check_mx(input)
61
+ str = input.to_s
62
+ unless str.strip =~ /^(\d+)\s+(\S+)$/
63
+ raise ArgumentError, "Invalid MX section `#{str}'"
77
64
  end
78
65
 
79
- def build_pack
80
- @mx_pack = [@preference].pack("n") + pack_name(@exchange)
81
- @rdlength = @mx_pack.size
82
- end
66
+ [Regexp.last_match(1).to_i, Regexp.last_match(2)]
67
+ end
83
68
 
84
- def get_data
85
- @mx_pack
86
- end
69
+ def build_pack
70
+ @mx_pack = [@preference].pack("n") + pack_name(@exchange)
71
+ @rdlength = @mx_pack.size
72
+ end
87
73
 
74
+ def get_data
75
+ @mx_pack
76
+ end
88
77
  end
89
-
90
78
  end
91
79
  end
92
80
  end
data/lib/net/dns/rr/ns.rb CHANGED
@@ -1,20 +1,16 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #
6
5
  # = Name Server Record (NS)
7
6
  #
8
7
  # Class for DNS NS resource records.
9
8
  #
10
9
  class NS < RR
11
-
12
10
  # Gets the name server value.
13
11
  #
14
12
  # Returns a String.
15
- def nsdname
16
- @nsdname
17
- end
13
+ attr_reader :nsdname
18
14
 
19
15
  # Gets the standardized value for this record,
20
16
  # represented by the value of <tt>nsdname</tt>.
@@ -24,55 +20,49 @@ module Net # :nodoc:
24
20
  nsdname.to_s
25
21
  end
26
22
 
27
-
28
23
  private
29
24
 
30
- def subclass_new_from_hash(options)
31
- if options.has_key?(:nsdname)
32
- @nsdname = check_name(options[:nsdname])
33
- else
34
- raise ArgumentError, ":nsdname field is mandatory"
35
- end
36
- end
25
+ def subclass_new_from_hash(options)
26
+ raise ArgumentError, ":nsdname field is mandatory" unless options.key?(:nsdname)
37
27
 
38
- def subclass_new_from_string(str)
39
- @nsdname = check_name(str)
40
- end
41
-
42
- def subclass_new_from_binary(data, offset)
43
- @nsdname, offset = dn_expand(data, offset)
44
- offset
45
- end
28
+ @nsdname = check_name(options[:nsdname])
29
+ end
46
30
 
31
+ def subclass_new_from_string(str)
32
+ @nsdname = check_name(str)
33
+ end
47
34
 
48
- def set_type
49
- @type = Net::DNS::RR::Types.new("NS")
50
- end
35
+ def subclass_new_from_binary(data, offset)
36
+ @nsdname, offset = dn_expand(data, offset)
37
+ offset
38
+ end
51
39
 
52
- def get_inspect
53
- value
54
- end
40
+ def set_type
41
+ @type = Net::DNS::RR::Types.new("NS")
42
+ end
55
43
 
44
+ def get_inspect
45
+ value
46
+ end
56
47
 
57
- def check_name(input)
58
- name = input.to_s
59
- unless name =~ /(\w\.?)+\s*$/ and name =~ /[a-zA-Z]/
60
- raise ArgumentError, "Invalid Name Server `#{name}'"
61
- end
62
- name
48
+ def check_name(input)
49
+ name = input.to_s
50
+ unless name =~ /(\w\.?)+\s*$/ && name =~ /[a-zA-Z]/
51
+ raise ArgumentError, "Invalid Name Server `#{name}'"
63
52
  end
64
53
 
65
- def build_pack
66
- @nsdname_pack = pack_name(@nsdname)
67
- @rdlength = @nsdname_pack.size
68
- end
54
+ name
55
+ end
69
56
 
70
- def get_data
71
- @nsdname_pack
72
- end
57
+ def build_pack
58
+ @nsdname_pack = pack_name(@nsdname)
59
+ @rdlength = @nsdname_pack.size
60
+ end
73
61
 
62
+ def get_data
63
+ @nsdname_pack
64
+ end
74
65
  end
75
-
76
66
  end
77
67
  end
78
68
  end
@@ -1,7 +1,6 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #------------------------------------------------------------
6
5
  # RR type NULL
7
6
  #------------------------------------------------------------
@@ -20,34 +19,30 @@ module Net # :nodoc:
20
19
  end
21
20
 
22
21
  def get_inspect
23
- "#@null"
22
+ @null.to_s
24
23
  end
25
24
 
26
25
  def subclass_new_from_hash(args)
27
- if args.has_key? :null
28
- @null = args[:null]
29
- else
30
- raise ArgumentError, ":null field is mandatory but missing"
31
- end
26
+ raise ArgumentError, ":null field is mandatory but missing" unless args.key? :null
27
+
28
+ @null = args[:null]
32
29
  end
33
30
 
34
31
  def subclass_new_from_string(str)
35
32
  @null = str.strip
36
33
  end
37
34
 
38
- def subclass_new_from_binary(data,offset)
39
- @null = data[offset..offset+@rdlength]
40
- return offset + @rdlength
35
+ def subclass_new_from_binary(data, offset)
36
+ @null = data[offset..offset + @rdlength]
37
+ offset + @rdlength
41
38
  end
42
39
 
43
40
  private
44
41
 
45
- def set_type
46
- @type = Net::DNS::RR::Types.new("NULL")
47
- end
48
-
42
+ def set_type
43
+ @type = Net::DNS::RR::Types.new("NULL")
44
+ end
49
45
  end
50
-
51
46
  end
52
47
  end
53
48
  end
@@ -1,18 +1,16 @@
1
1
  module Net
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #
6
5
  # = Pointer Record (PTR)
7
6
  #
8
7
  # Class for DNS Pointer (PTR) resource records.
9
8
  #
10
- # Pointer records are the opposite of A and AAAA RRs
9
+ # Pointer records are the opposite of A and AAAA RRs
11
10
  # and are used in Reverse Map zone files to map
12
11
  # an IP address (IPv4 or IPv6) to a host name.
13
12
  #
14
13
  class PTR < RR
15
-
16
14
  # Gets the PTR value.
17
15
  #
18
16
  # Returns a String.
@@ -20,7 +18,7 @@ module Net
20
18
  @ptrdname.to_s
21
19
  end
22
20
 
23
- alias_method :ptr, :ptrdname
21
+ alias ptr ptrdname
24
22
 
25
23
  # Gets the standardized value for this record,
26
24
  # represented by the value of <tt>ptrdname</tt>.
@@ -30,7 +28,6 @@ module Net
30
28
  ptrdname.to_s
31
29
  end
32
30
 
33
-
34
31
  private
35
32
 
36
33
  def build_pack
@@ -43,11 +40,9 @@ module Net
43
40
  end
44
41
 
45
42
  def subclass_new_from_hash(args)
46
- if args.has_key?(:ptrdname) or args.has_key?(:ptr)
47
- @ptrdname = args[:ptrdname]
48
- else
49
- raise ArgumentError, ":ptrdname or :ptr field is mandatory"
50
- end
43
+ raise ArgumentError, ":ptrdname or :ptr field is mandatory" unless args.key?(:ptrdname) || args.key?(:ptr)
44
+
45
+ @ptrdname = args[:ptrdname]
51
46
  end
52
47
 
53
48
  def subclass_new_from_string(str)
@@ -61,23 +56,20 @@ module Net
61
56
 
62
57
  private
63
58
 
64
- def set_type
65
- @type = Net::DNS::RR::Types.new("PTR")
66
- end
67
-
68
- def get_inspect
69
- value
70
- end
71
-
59
+ def set_type
60
+ @type = Net::DNS::RR::Types.new("PTR")
61
+ end
72
62
 
73
- def check_name(input)
74
- IPAddr.new(str)
75
- rescue
76
- raise ArgumentError, "Invalid PTR Section `#{input}'"
77
- end
63
+ def get_inspect
64
+ value
65
+ end
78
66
 
67
+ def check_name(input)
68
+ IPAddr.new(str)
69
+ rescue StandardError
70
+ raise ArgumentError, "Invalid PTR Section `#{input}'"
71
+ end
79
72
  end
80
-
81
73
  end
82
74
  end
83
75
  end
@@ -1,19 +1,24 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #------------------------------------------------------------
6
5
  # RR type SOA
7
6
  #------------------------------------------------------------
8
7
  class SOA < RR
9
- attr_reader :mname, :rname, :serial, :refresh, :retry, :expire, :minimum
10
-
8
+ attr_reader :mname
9
+ attr_reader :rname
10
+ attr_reader :serial
11
+ attr_reader :refresh
12
+ attr_reader :retry
13
+ attr_reader :expire
14
+ attr_reader :minimum
15
+
11
16
  private
12
-
17
+
13
18
  def build_pack
14
19
  @soa_pack = pack_name(@mname)
15
20
  @soa_pack += pack_name(@rname)
16
- @soa_pack += [@serial,@refresh,@retry,@expire,@minimum].pack("N5")
21
+ @soa_pack += [@serial, @refresh, @retry, @expire, @minimum].pack("N5")
17
22
  end
18
23
 
19
24
  def get_data
@@ -21,58 +26,54 @@ module Net # :nodoc:
21
26
  end
22
27
 
23
28
  def get_inspect
24
- "#@mname #@rname #@serial #@refresh #@retry #@expire #@minimum"
29
+ "#{@mname} #{@rname} #{@serial} #{@refresh} #{@retry} #{@expire} #{@minimum}"
25
30
  end
26
31
 
27
32
  def subclass_new_from_hash(args)
28
- if args.has_key? :rdata
33
+ if args.key? :rdata
29
34
  subclass_new_from_string(args[:rdata])
30
35
  else
31
- [:mname,:rname,:serial,:refresh,:retry,:expire,:minimum].each do |key|
32
- raise ArgumentError, "Missing field :#{key}" unless args.has_key? key
36
+ %i[mname rname serial refresh retry expire minimum].each do |key|
37
+ raise ArgumentError, "Missing field :#{key}" unless args.key? key
33
38
  end
34
- @mname = args[:mname] if valid? args[:mname]
39
+ @mname = args[:mname] if valid? args[:mname]
35
40
  @rname = args[:rname] if valid? args[:rname]
36
- @serial = args[:serial] if number? args[:serial]
37
- @refresh = args[:refresh] if number? args[:refresh]
38
- @retry = args[:retry] if number? args[:retry]
39
- @expire = args[:expire] if number? args[:expire]
40
- @minimum = args[:minimum] if number? args[:minimum]
41
+ @serial = args[:serial] if number? args[:serial]
42
+ @refresh = args[:refresh] if number? args[:refresh]
43
+ @retry = args[:retry] if number? args[:retry]
44
+ @expire = args[:expire] if number? args[:expire]
45
+ @minimum = args[:minimum] if number? args[:minimum]
41
46
  end
42
47
  end
43
-
48
+
44
49
  def number?(num)
45
- if num.kind_of? Integer and num > 0
46
- true
47
- else
48
- raise ArgumentError, "Wrong format field: #{num} not a number or less than zero"
49
- end
50
+ raise ArgumentError, "Wrong format field: #{num} not a number or less than zero" unless num.is_a?(Integer) && (num > 0)
51
+
52
+ true
50
53
  end
51
54
 
52
55
  def subclass_new_from_string(str)
53
- mname,rname,serial,refresh,ret,expire,minimum = str.strip.split(" ")
56
+ mname, rname, serial, refresh, ret, expire, minimum = str.strip.split(" ")
54
57
  @mname = mname if valid? mname
55
58
  @rname = rname if valid? rname
56
- @serial,@refresh,@retry,@expire,@minimum = [serial,refresh,ret,expire,minimum].collect do |i|
59
+ @serial, @refresh, @retry, @expire, @minimum = [serial, refresh, ret, expire, minimum].collect do |i|
57
60
  i.to_i if valid? i.to_i
58
61
  end
59
62
  end
60
63
 
61
- def subclass_new_from_binary(data,offset)
62
- @mname,offset = dn_expand(data,offset)
63
- @rname,offset = dn_expand(data,offset)
64
- @serial,@refresh,@retry,@expire,@minimum = data.unpack("@#{offset} N5")
65
- return offset + 5*Net::DNS::INT32SZ
64
+ def subclass_new_from_binary(data, offset)
65
+ @mname, offset = dn_expand(data, offset)
66
+ @rname, offset = dn_expand(data, offset)
67
+ @serial, @refresh, @retry, @expire, @minimum = data.unpack("@#{offset} N5")
68
+ offset + (5 * Net::DNS::INT32SZ)
66
69
  end
67
-
70
+
68
71
  private
69
-
70
- def set_type
71
- @type = Net::DNS::RR::Types.new("SOA")
72
- end
73
-
72
+
73
+ def set_type
74
+ @type = Net::DNS::RR::Types.new("SOA")
75
+ end
74
76
  end
75
-
76
77
  end
77
78
  end
78
79
  end
@@ -1,45 +1,44 @@
1
1
  module Net # :nodoc:
2
2
  module DNS
3
3
  class RR
4
-
5
4
  #------------------------------------------------------------
6
5
  # RR type SRV
7
6
  #------------------------------------------------------------
8
7
  class SRV < RR
9
-
10
- attr_reader :priority, :weight, :port, :host
11
-
8
+ attr_reader :priority
9
+ attr_reader :weight
10
+ attr_reader :port
11
+ attr_reader :host
12
+
12
13
  private
13
-
14
+
14
15
  def build_pack
15
16
  str = ""
16
17
  end
17
-
18
- def subclass_new_from_binary(data,offset)
18
+
19
+ def subclass_new_from_binary(data, offset)
19
20
  off_end = offset + @rdlength
20
21
  @priority, @weight, @port = data.unpack("@#{offset} n n n")
21
- offset+=6
22
+ offset += 6
22
23
 
23
- @host=[]
24
+ @host = []
24
25
  while offset < off_end
25
- len = data.unpack("@#{offset} C")[0]
26
+ len = data.unpack1("@#{offset} C")
26
27
  offset += 1
27
- str = data[offset..offset+len-1]
28
+ str = data[offset..offset + len - 1]
28
29
  offset += len
29
30
  @host << str
30
31
  end
31
- @host=@host.join(".")
32
+ @host = @host.join(".")
32
33
  offset
33
34
  end
34
-
35
+
35
36
  private
36
-
37
- def set_type
38
- @type = Net::DNS::RR::Types.new("SRV")
39
- end
40
-
37
+
38
+ def set_type
39
+ @type = Net::DNS::RR::Types.new("SRV")
40
+ end
41
41
  end
42
42
  end
43
-
44
43
  end
45
44
  end