net-dns 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +12 -0
- data/Rakefile +12 -39
- data/VERSION.yml +1 -1
- data/lib/net/dns/packet.rb +5 -5
- data/lib/net/dns/question.rb +3 -14
- data/lib/net/dns/resolver.rb +31 -28
- data/lib/net/dns/rr/ptr.rb +12 -7
- data/test/resolver_test.rb +54 -10
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
= Changelog
|
2
2
|
|
3
|
+
|
4
|
+
== Release 0.6.1
|
5
|
+
|
6
|
+
* ADDED: Net::DNS::Packet#to_s method (alias of #inspect)
|
7
|
+
|
8
|
+
* FIXED: typo in lib/net/dns/rr/ptr.rb [Thanks to Chris Lundquist]
|
9
|
+
|
10
|
+
* FIXED: warning: method redefined; discarding old inspect [Thanks to Kevin Baker] (closes #3)
|
11
|
+
|
12
|
+
* FIXED: issue with rescue ArgumentError (closes #5) and with IPAddr handling (closes #6)
|
13
|
+
|
14
|
+
|
3
15
|
== Release 0.6.0
|
4
16
|
|
5
17
|
*WARNING:* If you are upgrading from a previous minor release, check out the Compatibility issue list below.
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ begin
|
|
16
16
|
|
17
17
|
gemspec.add_development_dependency "rcov"
|
18
18
|
end
|
19
|
-
Jeweler::
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
20
|
rescue LoadError
|
21
21
|
puts "Jeweler not available."
|
22
22
|
end
|
@@ -57,48 +57,21 @@ Rake::RDocTask.new do |rdoc|
|
|
57
57
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
File.read(File.expand_path('~/.rubyforge/user-config.yml'))
|
72
|
-
)
|
73
|
-
|
74
|
-
host = "#{config['username']}@rubyforge.org"
|
75
|
-
remote_dir = "/var/www/gforge-projects/net-dns"
|
76
|
-
local_dir = 'rdoc'
|
77
|
-
|
78
|
-
Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
rescue LoadError
|
83
|
-
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
84
|
-
end
|
85
|
-
|
86
|
-
def egrep(pattern)
|
87
|
-
Dir['**/*.rb'].each do |fn|
|
88
|
-
count = 0
|
89
|
-
open(fn) do |f|
|
90
|
-
while line = f.gets
|
91
|
-
count += 1
|
92
|
-
if line =~ pattern
|
93
|
-
puts "#{fn}:#{count}:#{line}"
|
60
|
+
desc "Look for TODO and FIXME tags in the code"
|
61
|
+
task :todo do
|
62
|
+
def egrep(pattern)
|
63
|
+
Dir['**/*.rb'].each do |fn|
|
64
|
+
count = 0
|
65
|
+
open(fn) do |f|
|
66
|
+
while line = f.gets
|
67
|
+
count += 1
|
68
|
+
if line =~ pattern
|
69
|
+
puts "#{fn}:#{count}:#{line}"
|
70
|
+
end
|
94
71
|
end
|
95
72
|
end
|
96
73
|
end
|
97
74
|
end
|
98
|
-
end
|
99
|
-
|
100
|
-
desc "Look for TODO and FIXME tags in the code"
|
101
|
-
task :todo do
|
102
75
|
egrep /(FIXME|TODO|TBD)/
|
103
76
|
end
|
104
77
|
|
data/VERSION.yml
CHANGED
data/lib/net/dns/packet.rb
CHANGED
@@ -130,14 +130,14 @@ module Net # :nodoc:
|
|
130
130
|
#
|
131
131
|
# packet = Net::DNS::Packet.new("www.example.com")
|
132
132
|
# packet = Net::DNS::Packet.new("example.com", Net::DNS::MX)
|
133
|
-
# packet = Net::DNS::Packet.new("example.com",Net::DNS::TXT,Net::DNS::CH)
|
133
|
+
# packet = Net::DNS::Packet.new("example.com", Net::DNS::TXT, Net::DNS::CH)
|
134
134
|
#
|
135
135
|
# This class no longer instantiate object from binary data coming from
|
136
136
|
# network streams. Please use Net::DNS::Packet.new_from_data instead.
|
137
137
|
#
|
138
|
-
def initialize(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
138
|
+
def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
|
139
139
|
@header = Net::DNS::Header.new(:qdCount => 1)
|
140
|
-
@question = [Net::DNS::Question.new(name,type,cls)]
|
140
|
+
@question = [Net::DNS::Question.new(name, type, cls)]
|
141
141
|
@answer = []
|
142
142
|
@authority = []
|
143
143
|
@additional = []
|
@@ -302,7 +302,7 @@ module Net # :nodoc:
|
|
302
302
|
|
303
303
|
retval
|
304
304
|
end
|
305
|
-
|
305
|
+
alias_method :to_s, :inspect
|
306
306
|
|
307
307
|
# Wrapper to Header#truncated?
|
308
308
|
#
|
@@ -585,4 +585,4 @@ module Net # :nodoc:
|
|
585
585
|
end
|
586
586
|
|
587
587
|
end
|
588
|
-
end
|
588
|
+
end
|
data/lib/net/dns/question.rb
CHANGED
@@ -90,10 +90,10 @@ module Net # :nodoc:
|
|
90
90
|
# If not specified, +type+ and +cls+ arguments defaults
|
91
91
|
# to Net::DNS::A and Net::DNS::IN respectively.
|
92
92
|
#
|
93
|
-
def initialize(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
93
|
+
def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
|
94
94
|
@qName = check_name name
|
95
|
-
@qType = Net::DNS::RR::Types.new
|
96
|
-
@qClass = Net::DNS::RR::Classes.new
|
95
|
+
@qType = Net::DNS::RR::Types.new(type)
|
96
|
+
@qClass = Net::DNS::RR::Classes.new(cls)
|
97
97
|
end
|
98
98
|
|
99
99
|
# Return a new Net::DNS::Question object created by
|
@@ -110,16 +110,6 @@ module Net # :nodoc:
|
|
110
110
|
o
|
111
111
|
end
|
112
112
|
|
113
|
-
# Known inspect method with nice formatting
|
114
|
-
def inspect
|
115
|
-
if @qName.size > 29 then
|
116
|
-
len = @qName.size + 1
|
117
|
-
else
|
118
|
-
len = 29
|
119
|
-
end
|
120
|
-
[@qName,@qClass.to_s,@qType.to_s].pack("A#{len} A8 A8")
|
121
|
-
end
|
122
|
-
|
123
113
|
# Outputs binary data from a Question object
|
124
114
|
#
|
125
115
|
# question.data
|
@@ -132,7 +122,6 @@ module Net # :nodoc:
|
|
132
122
|
# Return the binary data of the objects, plus an offset
|
133
123
|
# and an Hash with references to compressed names. For use in
|
134
124
|
# Net::DNS::Packet compressed packet creation.
|
135
|
-
#
|
136
125
|
def comp_data
|
137
126
|
arr = @qName.split(".")
|
138
127
|
str = pack_name(@qName)
|
data/lib/net/dns/resolver.rb
CHANGED
@@ -843,6 +843,8 @@ module Net # :nodoc:
|
|
843
843
|
#
|
844
844
|
def search(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
845
845
|
|
846
|
+
return query(name,type,cls) if name.class == IPAddr
|
847
|
+
|
846
848
|
# If the name contains at least one dot then try it as is first.
|
847
849
|
if name.include? "."
|
848
850
|
@logger.debug "Search(#{name},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})"
|
@@ -891,7 +893,9 @@ module Net # :nodoc:
|
|
891
893
|
# method instead.
|
892
894
|
#
|
893
895
|
def query(name,type=Net::DNS::A,cls=Net::DNS::IN)
|
894
|
-
|
896
|
+
|
897
|
+
return send(name,type,cls) if name.class == IPAddr
|
898
|
+
|
895
899
|
# If the name doesn't contain any dots then append the default domain.
|
896
900
|
if name !~ /\./ and name !~ /:/ and @config[:defnames]
|
897
901
|
name += "." + @config[:domain]
|
@@ -912,38 +916,38 @@ module Net # :nodoc:
|
|
912
916
|
#
|
913
917
|
# Returns a Net::DNS::Packet object.
|
914
918
|
#
|
915
|
-
# #
|
916
|
-
# send_packet = Net::DNS::Packet.new("host.example.com",Net::DNS::NS,Net::DNS::HS)
|
919
|
+
# # Executes the query with a +Packet+ object
|
920
|
+
# send_packet = Net::DNS::Packet.new("host.example.com", Net::DNS::NS, Net::DNS::HS)
|
917
921
|
# packet = res.send(send_packet)
|
918
922
|
#
|
919
|
-
# #
|
923
|
+
# # Executes the query with a host, type and cls
|
920
924
|
# packet = res.send("host.example.com")
|
921
|
-
# packet = res.send("host.example.com",Net::DNS::NS)
|
922
|
-
# packet = res.send("host.example.com",Net::DNS::NS,Net::DNS::HS)
|
925
|
+
# packet = res.send("host.example.com", Net::DNS::NS)
|
926
|
+
# packet = res.send("host.example.com", Net::DNS::NS, Net::DNS::HS)
|
923
927
|
#
|
924
928
|
# If the name is an IP address (Ipv4 or IPv6), in the form of a string
|
925
929
|
# or a IPAddr object, then an appropriate PTR query will be performed:
|
926
930
|
#
|
927
931
|
# ip = IPAddr.new("172.16.100.2")
|
928
932
|
# packet = res.send(ip)
|
929
|
-
#
|
933
|
+
#
|
934
|
+
# packet = res.send("172.16.100.2")
|
930
935
|
#
|
931
936
|
# Use +packet.header.ancount+ or +packet.answer+ to find out if there
|
932
937
|
# were any records in the answer section.
|
933
938
|
#
|
934
|
-
def send(argument,type=Net::DNS::A,cls=Net::DNS::IN)
|
939
|
+
def send(argument, type = Net::DNS::A, cls = Net::DNS::IN)
|
935
940
|
if @config[:nameservers].size == 0
|
936
941
|
raise ResolverError, "No nameservers specified!"
|
937
942
|
end
|
938
943
|
|
939
944
|
method = :send_udp
|
940
|
-
|
941
|
-
|
942
|
-
packet = argument
|
945
|
+
packet = if argument.kind_of? Net::DNS::Packet
|
946
|
+
argument
|
943
947
|
else
|
944
|
-
|
948
|
+
make_query_packet(argument, type, cls)
|
945
949
|
end
|
946
|
-
|
950
|
+
|
947
951
|
# Store packet_data for performance improvements,
|
948
952
|
# so methods don't keep on calling Packet#data
|
949
953
|
packet_data = packet.data
|
@@ -1100,7 +1104,7 @@ module Net # :nodoc:
|
|
1100
1104
|
@config[:nameservers] << arr
|
1101
1105
|
end
|
1102
1106
|
|
1103
|
-
def make_query_packet(string,type,cls)
|
1107
|
+
def make_query_packet(string, type, cls)
|
1104
1108
|
case string
|
1105
1109
|
when IPAddr
|
1106
1110
|
name = string.reverse
|
@@ -1108,9 +1112,9 @@ module Net # :nodoc:
|
|
1108
1112
|
@logger.warn "PTR query required for address #{string}, changing type to PTR"
|
1109
1113
|
when /\d/ # Contains a number, try to see if it's an IP or IPv6 address
|
1110
1114
|
begin
|
1111
|
-
name = IPAddr.new(string).reverse
|
1112
|
-
type = Net::DNS::PTR
|
1113
|
-
rescue ArgumentError
|
1115
|
+
name = IPAddr.new(string.chomp(".")).reverse
|
1116
|
+
type = Net::DNS::PTR
|
1117
|
+
rescue ::ArgumentError
|
1114
1118
|
name = string if valid? string
|
1115
1119
|
end
|
1116
1120
|
else
|
@@ -1118,7 +1122,7 @@ module Net # :nodoc:
|
|
1118
1122
|
end
|
1119
1123
|
|
1120
1124
|
# Create the packet
|
1121
|
-
packet = Net::DNS::Packet.new(name,type,cls)
|
1125
|
+
packet = Net::DNS::Packet.new(name, type, cls)
|
1122
1126
|
|
1123
1127
|
if packet.query?
|
1124
1128
|
packet.header.recursive = @config[:recursive] ? 1 : 0
|
@@ -1127,10 +1131,9 @@ module Net # :nodoc:
|
|
1127
1131
|
# DNSSEC and TSIG stuff to be inserted here
|
1128
1132
|
|
1129
1133
|
packet
|
1130
|
-
|
1131
1134
|
end
|
1132
1135
|
|
1133
|
-
def send_tcp(packet,packet_data)
|
1136
|
+
def send_tcp(packet, packet_data)
|
1134
1137
|
|
1135
1138
|
ans = nil
|
1136
1139
|
length = [packet_data.size].pack("n")
|
@@ -1178,7 +1181,7 @@ module Net # :nodoc:
|
|
1178
1181
|
end
|
1179
1182
|
end
|
1180
1183
|
|
1181
|
-
def send_udp(packet,packet_data)
|
1184
|
+
def send_udp(packet, packet_data)
|
1182
1185
|
socket = UDPSocket.new
|
1183
1186
|
socket.bind(@config[:source_address].to_s,@config[:source_port])
|
1184
1187
|
|
@@ -1193,7 +1196,7 @@ module Net # :nodoc:
|
|
1193
1196
|
end
|
1194
1197
|
break if ans
|
1195
1198
|
rescue TimeoutError
|
1196
|
-
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
|
1199
|
+
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
|
1197
1200
|
next
|
1198
1201
|
end
|
1199
1202
|
end
|
@@ -1207,10 +1210,10 @@ module Net # :nodoc:
|
|
1207
1210
|
true
|
1208
1211
|
end
|
1209
1212
|
end
|
1210
|
-
|
1211
|
-
|
1213
|
+
|
1214
|
+
|
1212
1215
|
class << self
|
1213
|
-
|
1216
|
+
|
1214
1217
|
# Returns true if running on a Windows platform.
|
1215
1218
|
#
|
1216
1219
|
# Note. This method doesn't rely on the RUBY_PLATFORM constant
|
@@ -1219,9 +1222,9 @@ module Net # :nodoc:
|
|
1219
1222
|
def platform_windows?
|
1220
1223
|
!!(Config::CONFIG["host_os"] =~ /msdos|mswin|djgpp|mingw/i)
|
1221
1224
|
end
|
1222
|
-
|
1225
|
+
|
1223
1226
|
end
|
1224
|
-
|
1227
|
+
|
1225
1228
|
end
|
1226
1229
|
end
|
1227
|
-
end
|
1230
|
+
end
|
data/lib/net/dns/rr/ptr.rb
CHANGED
@@ -2,9 +2,15 @@ module Net
|
|
2
2
|
module DNS
|
3
3
|
class RR
|
4
4
|
|
5
|
-
|
6
|
-
#
|
7
|
-
|
5
|
+
#
|
6
|
+
# = Pointer Record (PTR)
|
7
|
+
#
|
8
|
+
# Class for DNS Pointer (PTR) resource records.
|
9
|
+
#
|
10
|
+
# Pointer records are the opposite of A and AAAA RRs
|
11
|
+
# and are used in Reverse Map zone files to map
|
12
|
+
# an IP address (IPv4 or IPv6) to a host name.
|
13
|
+
#
|
8
14
|
class PTR < RR
|
9
15
|
|
10
16
|
# Getter for PTR resource
|
@@ -53,13 +59,12 @@ module Net
|
|
53
59
|
|
54
60
|
private
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
|
62
|
+
def set_type
|
63
|
+
@type = Net::DNS::RR::Types.new("PTR")
|
64
|
+
end
|
59
65
|
|
60
66
|
end # class PTR
|
61
67
|
|
62
68
|
end # class RR
|
63
69
|
end # module DNS
|
64
70
|
end # module Net
|
65
|
-
|
data/test/resolver_test.rb
CHANGED
@@ -7,22 +7,66 @@ end
|
|
7
7
|
|
8
8
|
|
9
9
|
class ResolverTest < Test::Unit::TestCase
|
10
|
-
|
10
|
+
|
11
11
|
def test_initialize
|
12
12
|
assert_nothing_raised { Net::DNS::Resolver.new }
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def test_initialize_with_config
|
16
16
|
assert_nothing_raised { Net::DNS::Resolver.new({}) }
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def test_initialize_with_invalid_config_should_raise_argumenterror
|
20
20
|
assert_raise(Net::DNS::Resolver::ArgumentError) { Net::DNS::Resolver.new("") }
|
21
21
|
assert_raise(Net::DNS::Resolver::ArgumentError) { Net::DNS::Resolver.new(0) }
|
22
22
|
assert_raise(Net::DNS::Resolver::ArgumentError) { Net::DNS::Resolver.new(:foo) }
|
23
23
|
end
|
24
|
-
|
25
|
-
|
24
|
+
|
25
|
+
|
26
|
+
# I know private methods are supposed to not be tested directly
|
27
|
+
# but since this library lacks unit tests, for now let me test them in this way.
|
28
|
+
|
29
|
+
def _make_query_packet(*args)
|
30
|
+
# FIXME: horrible hack for horrible hack
|
31
|
+
Net::DNS::Resolver.new.old_send(:make_query_packet, *args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_make_query_packet_from_ipaddr
|
35
|
+
packet = _make_query_packet(IPAddr.new("192.168.1.1"), Net::DNS::A, cls = Net::DNS::IN)
|
36
|
+
assert_equal "1.1.168.192.in-addr.arpa", packet.question.first.qName
|
37
|
+
assert_equal Net::DNS::PTR.to_i, packet.question.first.qType.to_i
|
38
|
+
assert_equal Net::DNS::IN.to_i, packet.question.first.qClass.to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_make_query_packet_from_string_like_ipv4
|
42
|
+
packet = _make_query_packet("192.168.1.1", Net::DNS::A, cls = Net::DNS::IN)
|
43
|
+
assert_equal "1.1.168.192.in-addr.arpa", packet.question.first.qName
|
44
|
+
assert_equal Net::DNS::PTR.to_i, packet.question.first.qType.to_i
|
45
|
+
assert_equal Net::DNS::IN.to_i, packet.question.first.qClass.to_i
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_make_query_packet_from_string_like_ipv6
|
49
|
+
packet = _make_query_packet("2001:1ac0::200:0:a5d1:6004:2", Net::DNS::A, cls = Net::DNS::IN)
|
50
|
+
assert_equal "2.0.0.0.4.0.0.6.1.d.5.a.0.0.0.0.0.0.2.0.0.0.0.0.0.c.a.1.1.0.0.2.ip6.arpa", packet.question.first.qName
|
51
|
+
assert_equal Net::DNS::PTR.to_i, packet.question.first.qType.to_i
|
52
|
+
assert_equal Net::DNS::IN.to_i, packet.question.first.qClass.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_make_query_packet_from_string_like_hostname
|
56
|
+
packet = _make_query_packet("ns2.google.com", Net::DNS::A, cls = Net::DNS::IN)
|
57
|
+
assert_equal "ns2.google.com", packet.question.first.qName
|
58
|
+
assert_equal Net::DNS::A.to_i, packet.question.first.qType.to_i
|
59
|
+
assert_equal Net::DNS::IN.to_i, packet.question.first.qClass.to_i
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_make_query_packet_from_string_like_hostname_with_number
|
63
|
+
packet = _make_query_packet("ns.google.com", Net::DNS::A, cls = Net::DNS::IN)
|
64
|
+
assert_equal "ns.google.com", packet.question.first.qName
|
65
|
+
assert_equal Net::DNS::A.to_i, packet.question.first.qType.to_i
|
66
|
+
assert_equal Net::DNS::IN.to_i, packet.question.first.qClass.to_i
|
67
|
+
end
|
68
|
+
|
69
|
+
|
26
70
|
RubyPlatforms = [
|
27
71
|
["darwin9.0", false], # Mac OS X
|
28
72
|
["darwin", false], # JRuby on Mac OS X
|
@@ -31,7 +75,7 @@ class ResolverTest < Test::Unit::TestCase
|
|
31
75
|
["mswin32", true], # ruby 1.8.6 (2008-03-03 patchlevel 114) [i386-mswin32]
|
32
76
|
["mswin32", true], # ruby 1.8.6 (2008-04-22 rev 6555) [x86-jruby1.1.1]
|
33
77
|
]
|
34
|
-
|
78
|
+
|
35
79
|
def test_self_platform_windows_question
|
36
80
|
RubyPlatforms.each do |platform, is_windows|
|
37
81
|
assert_equal is_windows,
|
@@ -39,10 +83,10 @@ class ResolverTest < Test::Unit::TestCase
|
|
39
83
|
"Expected `#{is_windows}' with platform `#{platform}'"
|
40
84
|
end
|
41
85
|
end
|
42
|
-
|
43
|
-
|
86
|
+
|
87
|
+
|
44
88
|
protected
|
45
|
-
|
89
|
+
|
46
90
|
def override_platform(new_platform, &block)
|
47
91
|
raise LocalJumpError, "no block given" unless block_given?
|
48
92
|
old_platform = Config::CONFIG["host_os"]
|
@@ -52,5 +96,5 @@ class ResolverTest < Test::Unit::TestCase
|
|
52
96
|
Config::CONFIG["host_os"] = old_platform
|
53
97
|
result
|
54
98
|
end
|
55
|
-
|
99
|
+
|
56
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-dns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marco Ceresa
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2010-01-18 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|