net-dns 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/AUTHORS.rdoc +7 -0
- data/CHANGELOG.rdoc +34 -0
- data/README.rdoc +26 -14
- data/Rakefile +23 -30
- data/{THANKS → THANKS.rdoc} +0 -0
- data/VERSION.yml +3 -2
- data/demo/check_soa.rb +6 -11
- data/lib/net/{dns/dns.rb → dns.rb} +5 -12
- data/lib/net/dns/core_ext.rb +52 -0
- data/lib/net/dns/header.rb +55 -49
- data/lib/net/dns/names/names.rb +20 -10
- data/lib/net/dns/packet.rb +33 -26
- data/lib/net/dns/question.rb +60 -27
- data/lib/net/dns/resolver.rb +101 -156
- data/lib/net/dns/resolver/timeouts.rb +71 -65
- data/lib/net/dns/rr.rb +131 -166
- data/lib/net/dns/rr/a.rb +20 -26
- data/lib/net/dns/rr/aaaa.rb +15 -20
- data/lib/net/dns/rr/classes.rb +1 -1
- data/lib/net/dns/rr/cname.rb +8 -14
- data/lib/net/dns/rr/hinfo.rb +8 -14
- data/lib/net/dns/rr/mr.rb +8 -14
- data/lib/net/dns/rr/mx.rb +11 -18
- data/lib/net/dns/rr/ns.rb +8 -14
- data/lib/net/dns/rr/null.rb +7 -14
- data/lib/net/dns/rr/ptr.rb +9 -15
- data/lib/net/dns/rr/soa.rb +9 -15
- data/lib/net/dns/rr/srv.rb +10 -19
- data/lib/net/dns/rr/txt.rb +9 -20
- data/lib/net/dns/rr/types.rb +51 -58
- data/lib/net/dns/version.rb +22 -0
- data/test/{net/dns/test_header.rb → header_test.rb} +20 -20
- data/test/{net/dns/test_packet.rb → packet_test.rb} +2 -2
- data/test/question_test.rb +84 -0
- data/test/resolver/timeouts_test.rb +109 -0
- data/test/{net/dns/test_resolver.rb → resolver_test.rb} +6 -6
- data/test/rr/a_test.rb +66 -0
- data/test/{net/dns/rr/test_classes.rb → rr/classes_test.rb} +5 -5
- data/test/rr/ns_test.rb +64 -0
- data/test/rr/types_test.rb +69 -0
- data/test/{net/dns/test_rr.rb → rr_test.rb} +10 -12
- data/test/test_helper.rb +4 -0
- metadata +50 -35
- data/AUTHORS +0 -10
- data/CHANGELOG +0 -7
- data/INSTALL +0 -8
- data/net-dns.gemspec +0 -92
- data/test/net/dns/resolver/test_timeouts.rb +0 -59
- data/test/net/dns/rr/test_a.rb +0 -72
- data/test/net/dns/rr/test_ns.rb +0 -66
- data/test/net/dns/rr/test_types.rb +0 -124
- data/test/net/dns/test_question.rb +0 -54
data/lib/net/dns/names/names.rb
CHANGED
@@ -1,8 +1,21 @@
|
|
1
|
-
module Net
|
1
|
+
module Net
|
2
2
|
module DNS
|
3
3
|
|
4
|
-
module Names
|
5
|
-
|
4
|
+
module Names
|
5
|
+
|
6
|
+
# Argument Error for class Net::DNS::Names.
|
7
|
+
class ArgumentError < ArgumentError
|
8
|
+
end
|
9
|
+
|
10
|
+
# Base error class.
|
11
|
+
class Error < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
# Generic Names Error.
|
15
|
+
class ExpandError < Error
|
16
|
+
end
|
17
|
+
|
18
|
+
|
6
19
|
INT16SZ = 2
|
7
20
|
|
8
21
|
# Expand a compressed name in a DNS Packet object. Please
|
@@ -18,7 +31,7 @@ module Net # :nodoc:
|
|
18
31
|
name = ""
|
19
32
|
packetlen = packet.size
|
20
33
|
while true
|
21
|
-
raise ExpandError, "
|
34
|
+
raise ExpandError, "Offset is greater than packet lenght!" if packetlen < (offset+1)
|
22
35
|
len = packet.unpack("@#{offset} C")[0]
|
23
36
|
|
24
37
|
if len == 0
|
@@ -101,9 +114,6 @@ module Net # :nodoc:
|
|
101
114
|
end
|
102
115
|
end
|
103
116
|
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
class ExpandError < StandardError # :nodoc:
|
109
|
-
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/lib/net/dns/packet.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'logger'
|
2
2
|
require 'net/dns/names/names'
|
3
|
-
require 'net/dns
|
3
|
+
require 'net/dns'
|
4
4
|
require 'net/dns/header'
|
5
5
|
require 'net/dns/question'
|
6
6
|
require 'net/dns/rr'
|
@@ -94,8 +94,8 @@ module Net # :nodoc:
|
|
94
94
|
# which are listed here to keep a light and browsable main documentation.
|
95
95
|
# We have:
|
96
96
|
#
|
97
|
-
#
|
98
|
-
#
|
97
|
+
# ArgumentError:: Argument Error for class Net::DNS::Packet
|
98
|
+
# PacketError:: Generic Packet Error
|
99
99
|
#
|
100
100
|
# =Copyright
|
101
101
|
#
|
@@ -105,12 +105,24 @@ module Net # :nodoc:
|
|
105
105
|
# it and/or modify it under the same terms as Ruby itself.
|
106
106
|
#
|
107
107
|
class Packet
|
108
|
-
|
109
108
|
include Names
|
110
109
|
|
110
|
+
# Argument Error for class Net::DNS::Packet.
|
111
|
+
class ArgumentError < ArgumentError
|
112
|
+
end
|
113
|
+
|
114
|
+
# Base error class.
|
115
|
+
class Error < StandardError
|
116
|
+
end
|
117
|
+
|
118
|
+
# Generic Packet Error.
|
119
|
+
class PacketError < Error
|
120
|
+
end
|
121
|
+
|
122
|
+
|
111
123
|
attr_reader :header, :question, :answer, :authority, :additional
|
112
124
|
attr_reader :answerfrom, :answersize
|
113
|
-
|
125
|
+
|
114
126
|
# Create a new instance of Net::DNS::Packet class. Arguments are the
|
115
127
|
# canonical name of the resourse, an optional type field and an optional
|
116
128
|
# class field. The record type and class can be omitted; they default
|
@@ -305,7 +317,7 @@ module Net # :nodoc:
|
|
305
317
|
if object.kind_of? Net::DNS::Header
|
306
318
|
@header = object
|
307
319
|
else
|
308
|
-
raise
|
320
|
+
raise ArgumentError, "Argument must be a Net::DNS::Header object"
|
309
321
|
end
|
310
322
|
end
|
311
323
|
|
@@ -318,12 +330,12 @@ module Net # :nodoc:
|
|
318
330
|
if object.all? {|x| x.kind_of? Net::DNS::Question}
|
319
331
|
@question = object
|
320
332
|
else
|
321
|
-
raise
|
333
|
+
raise ArgumentError, "Some of the elements is not an Net::DNS::Question object"
|
322
334
|
end
|
323
335
|
when Net::DNS::Question
|
324
336
|
@question = [object]
|
325
337
|
else
|
326
|
-
raise
|
338
|
+
raise ArgumentError, "Invalid argument, not a Question object nor an array of objects"
|
327
339
|
end
|
328
340
|
end
|
329
341
|
|
@@ -337,12 +349,12 @@ module Net # :nodoc:
|
|
337
349
|
if object.all? {|x| x.kind_of? Net::DNS::RR}
|
338
350
|
@answer = object
|
339
351
|
else
|
340
|
-
raise
|
352
|
+
raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
|
341
353
|
end
|
342
354
|
when Net::DNS::RR
|
343
355
|
@answer = [object]
|
344
356
|
else
|
345
|
-
raise
|
357
|
+
raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
|
346
358
|
end
|
347
359
|
end
|
348
360
|
|
@@ -356,12 +368,12 @@ module Net # :nodoc:
|
|
356
368
|
if object.all? {|x| x.kind_of? Net::DNS::RR}
|
357
369
|
@additional = object
|
358
370
|
else
|
359
|
-
raise
|
371
|
+
raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
|
360
372
|
end
|
361
373
|
when Net::DNS::RR
|
362
374
|
@additional = [object]
|
363
375
|
else
|
364
|
-
raise
|
376
|
+
raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
|
365
377
|
end
|
366
378
|
end
|
367
379
|
|
@@ -375,12 +387,12 @@ module Net # :nodoc:
|
|
375
387
|
if object.all? {|x| x.kind_of? Net::DNS::RR}
|
376
388
|
@authority = object
|
377
389
|
else
|
378
|
-
raise
|
390
|
+
raise ArgumentError, "Some of the elements is not an Net::DNS::RR object"
|
379
391
|
end
|
380
392
|
when Net::DNS::RR
|
381
393
|
@authority = [object]
|
382
394
|
else
|
383
|
-
raise
|
395
|
+
raise ArgumentError, "Invalid argument, not a RR object nor an array of objects"
|
384
396
|
end
|
385
397
|
end
|
386
398
|
|
@@ -468,7 +480,7 @@ module Net # :nodoc:
|
|
468
480
|
data.size
|
469
481
|
end
|
470
482
|
|
471
|
-
#
|
483
|
+
# Checks whether a query has returned a NXDOMAIN error,
|
472
484
|
# meaning the domain name queried doesn't exists.
|
473
485
|
#
|
474
486
|
# %w[a.com google.com ibm.com d.com].each do |domain|
|
@@ -479,7 +491,7 @@ module Net # :nodoc:
|
|
479
491
|
# #=> d.com doesn't exist
|
480
492
|
#
|
481
493
|
def nxdomain?
|
482
|
-
header.rCode == Net::DNS::Header::NAME
|
494
|
+
header.rCode.code == Net::DNS::Header::RCode::NAME
|
483
495
|
end
|
484
496
|
|
485
497
|
private
|
@@ -566,16 +578,11 @@ module Net # :nodoc:
|
|
566
578
|
def parse_question(data,offset)
|
567
579
|
size = (dn_expand(data,offset)[1]-offset) + 2*Net::DNS::INT16SZ
|
568
580
|
return [Net::DNS::Question.parse(data[offset,size]), offset+size]
|
569
|
-
rescue StandardError =>
|
570
|
-
raise PacketError, "Caught exception, maybe packet malformed => #{
|
581
|
+
rescue StandardError => e
|
582
|
+
raise PacketError, "Caught exception, maybe packet malformed => #{e.message}"
|
571
583
|
end
|
572
584
|
|
573
|
-
end
|
585
|
+
end
|
574
586
|
|
575
|
-
end
|
576
|
-
end
|
577
|
-
|
578
|
-
class PacketError < StandardError # :nodoc:
|
579
|
-
end
|
580
|
-
class PacketArgumentError < ArgumentError # :nodoc:
|
581
|
-
end
|
587
|
+
end
|
588
|
+
end
|
data/lib/net/dns/question.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
|
2
|
-
# $Id: Question.rb,v 1.8 2006/07/28 19:00:03 bluemonk Exp $
|
3
|
-
#+++
|
4
|
-
|
5
|
-
require 'net/dns/dns'
|
1
|
+
require 'net/dns'
|
6
2
|
require 'net/dns/names/names'
|
7
3
|
require 'net/dns/rr/types'
|
8
4
|
require 'net/dns/rr/classes'
|
9
5
|
|
6
|
+
|
10
7
|
module Net # :nodoc:
|
11
8
|
module DNS
|
12
9
|
|
@@ -49,8 +46,8 @@ module Net # :nodoc:
|
|
49
46
|
# which are listed here to keep a light and browsable main documentation.
|
50
47
|
# We have:
|
51
48
|
#
|
52
|
-
#
|
53
|
-
#
|
49
|
+
# ArgumentError:: Argument Error for class Net::DNS::Question
|
50
|
+
# NameError:: An error in the +name+ part of a Question entry
|
54
51
|
#
|
55
52
|
# =Copyright
|
56
53
|
#
|
@@ -60,9 +57,20 @@ module Net # :nodoc:
|
|
60
57
|
# it and/or modify it under the same terms as Ruby itself.
|
61
58
|
#
|
62
59
|
class Question
|
63
|
-
|
64
60
|
include Net::DNS::Names
|
65
61
|
|
62
|
+
# Argument Error for class Net::DNS::Question
|
63
|
+
class ArgumentError < ArgumentError
|
64
|
+
end
|
65
|
+
|
66
|
+
# Base error class.
|
67
|
+
class Error < StandardError
|
68
|
+
end
|
69
|
+
|
70
|
+
# An error in the +name+ part of a Question entry
|
71
|
+
class NameError < Error
|
72
|
+
end
|
73
|
+
|
66
74
|
# +name+ part of a Question entry
|
67
75
|
attr_reader :qName
|
68
76
|
# +type+ part of a Question entry
|
@@ -97,15 +105,11 @@ module Net # :nodoc:
|
|
97
105
|
# #=> Queried for example.com type A
|
98
106
|
#
|
99
107
|
def self.parse(arg)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
o
|
104
|
-
else
|
105
|
-
raise QuestionArgumentError, "Wrong argument format, must be a String"
|
106
|
-
end
|
108
|
+
o = allocate
|
109
|
+
o.send(:new_from_binary, arg.to_s)
|
110
|
+
o
|
107
111
|
end
|
108
|
-
|
112
|
+
|
109
113
|
# Known inspect method with nice formatting
|
110
114
|
def inspect
|
111
115
|
if @qName.size > 29 then
|
@@ -148,6 +152,40 @@ module Net # :nodoc:
|
|
148
152
|
[[str,@qType.to_i,@qClass.to_i].pack("a*nn"),offset,names]
|
149
153
|
end
|
150
154
|
|
155
|
+
|
156
|
+
#
|
157
|
+
# call-seq:
|
158
|
+
# question.inspect -> string
|
159
|
+
#
|
160
|
+
# Returns a printable version of question with nice formatting.
|
161
|
+
#
|
162
|
+
# q = Net::DNS::Question.new("google.com.", Net::DNS::A)
|
163
|
+
# q.inspect # => "google.com. IN A "
|
164
|
+
#
|
165
|
+
def inspect
|
166
|
+
if @qName.size > 29 then
|
167
|
+
len = @qName.size + 1
|
168
|
+
else
|
169
|
+
len = 29
|
170
|
+
end
|
171
|
+
[@qName, @qClass.to_s, @qType.to_s].pack("A#{len} A8 A8")
|
172
|
+
end
|
173
|
+
|
174
|
+
#
|
175
|
+
# call-seq:
|
176
|
+
# question.to_s -> string
|
177
|
+
#
|
178
|
+
# Returns a string representation of question.
|
179
|
+
# It is the same as <tt>inspect</tt>.
|
180
|
+
#
|
181
|
+
# q = Net::DNS::Question.new("google.com.", Net::DNS::A)
|
182
|
+
# q.inspect # => "google.com. IN A "
|
183
|
+
#
|
184
|
+
def to_s
|
185
|
+
"#{self.inspect}"
|
186
|
+
end
|
187
|
+
|
188
|
+
|
151
189
|
private
|
152
190
|
|
153
191
|
def build_qName(str)
|
@@ -167,12 +205,12 @@ module Net # :nodoc:
|
|
167
205
|
def check_name(name)
|
168
206
|
name.strip!
|
169
207
|
if name =~ /[^\w\.\-_]/
|
170
|
-
raise
|
208
|
+
raise NameError, "Question name #{name.inspect} not valid"
|
171
209
|
else
|
172
210
|
name
|
173
211
|
end
|
174
212
|
rescue
|
175
|
-
raise
|
213
|
+
raise NameError, "Question name #{name.inspect} not valid"
|
176
214
|
end
|
177
215
|
|
178
216
|
def new_from_binary(data)
|
@@ -181,15 +219,10 @@ module Net # :nodoc:
|
|
181
219
|
@qType = Net::DNS::RR::Types.new type
|
182
220
|
@qClass = Net::DNS::RR::Classes.new cls
|
183
221
|
rescue StandardError => e
|
184
|
-
raise
|
222
|
+
raise ArgumentError, "Invalid data: #{data.inspect}\n{e.backtrace}"
|
185
223
|
end
|
186
224
|
|
187
|
-
end
|
225
|
+
end
|
188
226
|
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
class QuestionArgumentError < ArgumentError # :nodoc:
|
193
|
-
end
|
194
|
-
class QuestionNameError < StandardError # :nodoc:
|
195
|
-
end
|
227
|
+
end
|
228
|
+
end
|
data/lib/net/dns/resolver.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
#
|
2
|
-
# $Id: Resolver.rb,v 1.11 2006/07/30 16:55:35 bluemonk Exp $
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
1
|
require 'rbconfig'
|
8
2
|
require 'socket'
|
9
3
|
require 'timeout'
|
@@ -15,18 +9,18 @@ require 'net/dns/resolver/timeouts'
|
|
15
9
|
alias old_send send
|
16
10
|
|
17
11
|
#
|
18
|
-
# Resolver helper method
|
12
|
+
# = Resolver helper method
|
19
13
|
#
|
20
|
-
# Calling the resolver directly
|
14
|
+
# Calling the resolver directly:
|
21
15
|
#
|
22
16
|
# require 'net/dns/resolver'
|
23
17
|
# puts Resolver("www.google.com").answer.size
|
24
|
-
#
|
18
|
+
# #=> 5
|
25
19
|
#
|
26
|
-
# An optional block can be passed yielding the Net::DNS::Packet object
|
20
|
+
# An optional block can be passed yielding the Net::DNS::Packet object.
|
27
21
|
#
|
28
22
|
# Resolver("www.google.com") {|packet| puts packet.size + " bytes"}
|
29
|
-
#
|
23
|
+
# #=> 484 bytes
|
30
24
|
#
|
31
25
|
def Resolver(name,type=Net::DNS::A,cls=Net::DNS::IN,&blk)
|
32
26
|
obj = Net::DNS::Resolver.start(name,type,cls)
|
@@ -42,16 +36,8 @@ module Net # :nodoc:
|
|
42
36
|
|
43
37
|
include Logger::Severity
|
44
38
|
|
45
|
-
# =
|
46
|
-
#
|
47
|
-
# Net::DNS::Resolver - DNS resolver class
|
48
|
-
#
|
49
|
-
# =Synopsis
|
39
|
+
# = Net::DNS::Resolver - DNS resolver class
|
50
40
|
#
|
51
|
-
# require 'net/dns/resolver'
|
52
|
-
#
|
53
|
-
# =Description
|
54
|
-
#
|
55
41
|
# The Net::DNS::Resolver class implements a complete DNS resolver written
|
56
42
|
# in pure Ruby, without a single C line of code. It has all of the
|
57
43
|
# tipical properties of an evoluted resolver, and a bit of OO which
|
@@ -63,9 +49,7 @@ module Net # :nodoc:
|
|
63
49
|
# the Perl version are still missing, but guys, at least this is
|
64
50
|
# readable code!
|
65
51
|
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# =Environment
|
52
|
+
# == Environment
|
69
53
|
#
|
70
54
|
# The Following Environment variables can also be used to configure
|
71
55
|
# the resolver:
|
@@ -109,7 +93,18 @@ module Net # :nodoc:
|
|
109
93
|
# % setenv RES_OPTIONS "retrans:3 retry:2 debug"
|
110
94
|
#
|
111
95
|
class Resolver
|
112
|
-
|
96
|
+
|
97
|
+
# Argument Error for class Net::DNS::Resolver.
|
98
|
+
class ArgumentError < ArgumentError
|
99
|
+
end
|
100
|
+
|
101
|
+
class Error < StandardError
|
102
|
+
end
|
103
|
+
|
104
|
+
class NoResponseError < Error
|
105
|
+
end
|
106
|
+
|
107
|
+
|
113
108
|
# An hash with the defaults values of almost all the
|
114
109
|
# configuration parameters of a resolver object. See
|
115
110
|
# the description for each parameter to have an
|
@@ -131,8 +126,9 @@ module Net # :nodoc:
|
|
131
126
|
:use_tcp => false,
|
132
127
|
:ignore_truncated => false,
|
133
128
|
:packet_size => 512,
|
134
|
-
:tcp_timeout => TcpTimeout.new(
|
135
|
-
:udp_timeout => UdpTimeout.new(
|
129
|
+
:tcp_timeout => TcpTimeout.new(5),
|
130
|
+
:udp_timeout => UdpTimeout.new(5),
|
131
|
+
}
|
136
132
|
|
137
133
|
# Create a new resolver object.
|
138
134
|
#
|
@@ -152,7 +148,7 @@ module Net # :nodoc:
|
|
152
148
|
# :recursive => false,
|
153
149
|
# :retry => 10)
|
154
150
|
#
|
155
|
-
#
|
151
|
+
# == Config file
|
156
152
|
#
|
157
153
|
# Net::DNS::Resolver uses a config file to read the usual
|
158
154
|
# values a resolver needs, such as nameserver list and
|
@@ -191,42 +187,42 @@ module Net # :nodoc:
|
|
191
187
|
# Explicit arguments to Resolver::new override both the system's defaults
|
192
188
|
# and the values of the custom configuration file, if any.
|
193
189
|
#
|
194
|
-
#
|
190
|
+
# == Parameters
|
195
191
|
#
|
196
192
|
# The following arguments to Resolver::new are supported:
|
197
193
|
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
194
|
+
# * nameservers: an array reference of nameservers to query.
|
195
|
+
# * searchlist: an array reference of domains.
|
196
|
+
# * recurse
|
197
|
+
# * debug
|
198
|
+
# * domain
|
199
|
+
# * port
|
200
|
+
# * srcaddr
|
201
|
+
# * srcport
|
202
|
+
# * tcp_timeout
|
203
|
+
# * udp_timeout
|
204
|
+
# * retrans
|
205
|
+
# * retry
|
206
|
+
# * usevc
|
207
|
+
# * stayopen
|
208
|
+
# * igntc
|
209
|
+
# * defnames
|
210
|
+
# * dnsrch
|
211
|
+
# * persistent_tcp
|
212
|
+
# * persistent_udp
|
213
|
+
# * dnssec
|
218
214
|
#
|
219
215
|
# For more information on any of these options, please consult the
|
220
216
|
# method of the same name.
|
221
217
|
#
|
222
|
-
#
|
218
|
+
# == Disclaimer
|
223
219
|
#
|
224
220
|
# Part of the above documentation is taken from the one in the
|
225
221
|
# Net::DNS::Resolver Perl module.
|
226
222
|
#
|
227
223
|
def initialize(config = {})
|
228
|
-
raise
|
229
|
-
# config.
|
224
|
+
raise ArgumentError, "Argument has to be Hash" unless config.kind_of? Hash
|
225
|
+
# config.downcase_keys!
|
230
226
|
@config = Defaults.merge config
|
231
227
|
@raw = false
|
232
228
|
|
@@ -262,33 +258,33 @@ module Net # :nodoc:
|
|
262
258
|
begin
|
263
259
|
eval "self.#{key.to_s} = val"
|
264
260
|
rescue NoMethodError
|
265
|
-
raise
|
261
|
+
raise ArgumentError, "Option #{key} not valid"
|
266
262
|
end
|
267
263
|
end
|
268
264
|
end
|
269
265
|
|
270
|
-
# Get the resolver
|
266
|
+
# Get the resolver search list, returned as an array of entries.
|
271
267
|
#
|
272
268
|
# res.searchlist
|
273
|
-
#
|
269
|
+
# #=> ["example.com","a.example.com","b.example.com"]
|
274
270
|
#
|
275
271
|
def searchlist
|
276
272
|
@config[:searchlist].inspect
|
277
273
|
end
|
278
274
|
|
279
275
|
# Set the resolver searchlist.
|
280
|
-
# +arg+ can be a single string or an array of strings
|
276
|
+
# +arg+ can be a single string or an array of strings.
|
281
277
|
#
|
282
278
|
# res.searchstring = "example.com"
|
283
279
|
# res.searchstring = ["example.com","a.example.com","b.example.com"]
|
284
280
|
#
|
285
|
-
# Note that you can also append a new name to the searchlist
|
281
|
+
# Note that you can also append a new name to the searchlist.
|
286
282
|
#
|
287
283
|
# res.searchlist << "c.example.com"
|
288
284
|
# res.searchlist
|
289
|
-
#
|
285
|
+
# #=> ["example.com","a.example.com","b.example.com","c.example.com"]
|
290
286
|
#
|
291
|
-
# The default is an empty array
|
287
|
+
# The default is an empty array.
|
292
288
|
#
|
293
289
|
def searchlist=(arg)
|
294
290
|
case arg
|
@@ -299,11 +295,11 @@ module Net # :nodoc:
|
|
299
295
|
@config[:searchlist] = arg if arg.all? {|x| valid? x}
|
300
296
|
@logger.info "Searchlist changed to value #{@config[:searchlist].inspect}"
|
301
297
|
else
|
302
|
-
raise
|
298
|
+
raise ArgumentError, "Wrong argument format, neither String nor Array"
|
303
299
|
end
|
304
300
|
end
|
305
301
|
|
306
|
-
# Get the list of resolver nameservers, in a dotted decimal format
|
302
|
+
# Get the list of resolver nameservers, in a dotted decimal format-
|
307
303
|
#
|
308
304
|
# res.nameservers
|
309
305
|
# #=> ["192.168.0.1","192.168.0.2"]
|
@@ -317,17 +313,17 @@ module Net # :nodoc:
|
|
317
313
|
end
|
318
314
|
alias_method :nameserver, :nameservers
|
319
315
|
|
320
|
-
# Set the list of resolver nameservers
|
321
|
-
# +arg+ can be a single ip address or an array of addresses
|
316
|
+
# Set the list of resolver nameservers.
|
317
|
+
# +arg+ can be a single ip address or an array of addresses.
|
322
318
|
#
|
323
319
|
# res.nameservers = "192.168.0.1"
|
324
320
|
# res.nameservers = ["192.168.0.1","192.168.0.2"]
|
325
321
|
#
|
326
|
-
# If you want you can specify the addresses as IPAddr instances
|
322
|
+
# If you want you can specify the addresses as IPAddr instances.
|
327
323
|
#
|
328
324
|
# ip = IPAddr.new("192.168.0.3")
|
329
325
|
# res.nameservers << ip
|
330
|
-
#
|
326
|
+
# #=> ["192.168.0.1","192.168.0.2","192.168.0.3"]
|
331
327
|
#
|
332
328
|
# The default is 127.0.0.1 (localhost)
|
333
329
|
#
|
@@ -357,30 +353,27 @@ module Net # :nodoc:
|
|
357
353
|
when IPAddr
|
358
354
|
x
|
359
355
|
else
|
360
|
-
raise
|
356
|
+
raise ArgumentError, "Wrong argument format"
|
361
357
|
end
|
362
358
|
end
|
363
359
|
@logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}"
|
364
360
|
else
|
365
|
-
raise
|
361
|
+
raise ArgumentError, "Wrong argument format, neither String, Array nor IPAddr"
|
366
362
|
end
|
367
363
|
end
|
368
364
|
alias_method("nameserver=","nameservers=")
|
369
365
|
|
370
|
-
# Return a string with the default domain
|
371
|
-
#
|
366
|
+
# Return a string with the default domain.
|
372
367
|
def domain
|
373
368
|
@config[:domain].inspect
|
374
369
|
end
|
375
370
|
|
376
|
-
# Set the domain for the query
|
377
|
-
#
|
371
|
+
# Set the domain for the query.
|
378
372
|
def domain=(name)
|
379
373
|
@config[:domain] = name if valid? name
|
380
374
|
end
|
381
375
|
|
382
|
-
# Return the defined size of the packet
|
383
|
-
#
|
376
|
+
# Return the defined size of the packet.
|
384
377
|
def packet_size
|
385
378
|
@config[:packet_size]
|
386
379
|
end
|
@@ -405,11 +398,11 @@ module Net # :nodoc:
|
|
405
398
|
@config[:port] = num
|
406
399
|
@logger.info "Port number changed to #{num}"
|
407
400
|
else
|
408
|
-
raise
|
401
|
+
raise ArgumentError, "Wrong port number #{num}"
|
409
402
|
end
|
410
403
|
end
|
411
404
|
|
412
|
-
# Get the value of the source port number
|
405
|
+
# Get the value of the source port number.
|
413
406
|
#
|
414
407
|
# puts "Sending queries using port #{res.source_port}"
|
415
408
|
#
|
@@ -436,7 +429,7 @@ module Net # :nodoc:
|
|
436
429
|
if (0..65535).include?(num)
|
437
430
|
@config[:source_port] = num
|
438
431
|
else
|
439
|
-
raise
|
432
|
+
raise ArgumentError, "Wrong port number #{num}"
|
440
433
|
end
|
441
434
|
end
|
442
435
|
alias srcport= source_port=
|
@@ -450,8 +443,7 @@ module Net # :nodoc:
|
|
450
443
|
end
|
451
444
|
alias srcaddr source_address
|
452
445
|
|
453
|
-
# Set the local source address from which the resolver sends its
|
454
|
-
# queries.
|
446
|
+
# Set the local source address from which the resolver sends its queries.
|
455
447
|
#
|
456
448
|
# res.source_address = "172.16.100.1"
|
457
449
|
# res.source_address = IPAddr.new("172.16.100.1")
|
@@ -472,12 +464,11 @@ module Net # :nodoc:
|
|
472
464
|
# root priviledges, as raw sockets will be used to generate packets.
|
473
465
|
# The class will then generate an exception if you're not root.
|
474
466
|
#
|
475
|
-
# The default is 0.0.0.0, meaning any local address (chosen on routing
|
476
|
-
# needs).
|
467
|
+
# The default is 0.0.0.0, meaning any local address (chosen on routing needs).
|
477
468
|
#
|
478
469
|
def source_address=(addr)
|
479
470
|
unless addr.respond_to? :to_s
|
480
|
-
raise
|
471
|
+
raise ArgumentError, "Wrong address argument #{addr}"
|
481
472
|
end
|
482
473
|
|
483
474
|
begin
|
@@ -513,26 +504,24 @@ module Net # :nodoc:
|
|
513
504
|
alias srcaddr= source_address=
|
514
505
|
|
515
506
|
# Return the retrasmission interval (in seconds) the resolvers has
|
516
|
-
# been set on
|
517
|
-
#
|
507
|
+
# been set on.
|
518
508
|
def retry_interval
|
519
509
|
@config[:retry_interval]
|
520
510
|
end
|
521
511
|
alias retrans retry_interval
|
522
512
|
|
523
|
-
# Set the retrasmission interval in seconds. Default 5 seconds
|
524
|
-
#
|
513
|
+
# Set the retrasmission interval in seconds. Default 5 seconds.
|
525
514
|
def retry_interval=(num)
|
526
515
|
if num > 0
|
527
516
|
@config[:retry_interval] = num
|
528
517
|
@logger.info "Retransmission interval changed to #{num} seconds"
|
529
518
|
else
|
530
|
-
raise
|
519
|
+
raise ArgumentError, "Interval must be positive"
|
531
520
|
end
|
532
521
|
end
|
533
522
|
alias retrans= retry_interval=
|
534
523
|
|
535
|
-
# The number of times the resolver will try a query
|
524
|
+
# The number of times the resolver will try a query.
|
536
525
|
#
|
537
526
|
# puts "Will try a max of #{res.retry_number} queries"
|
538
527
|
#
|
@@ -541,14 +530,13 @@ module Net # :nodoc:
|
|
541
530
|
end
|
542
531
|
|
543
532
|
# Set the number of times the resolver will try a query.
|
544
|
-
# Default 4 times
|
545
|
-
#
|
533
|
+
# Default 4 times.
|
546
534
|
def retry_number=(num)
|
547
535
|
if num.kind_of? Integer and num > 0
|
548
536
|
@config[:retry_number] = num
|
549
537
|
@logger.info "Retrasmissions number changed to #{num}"
|
550
538
|
else
|
551
|
-
raise
|
539
|
+
raise ArgumentError, "Retry value must be a positive integer"
|
552
540
|
end
|
553
541
|
end
|
554
542
|
alias_method('retry=', 'retry_number=')
|
@@ -577,12 +565,12 @@ module Net # :nodoc:
|
|
577
565
|
@config[:recursive] = bool
|
578
566
|
@logger.info("Recursive state changed to #{bool}")
|
579
567
|
else
|
580
|
-
raise
|
568
|
+
raise ArgumentError, "Argument must be boolean"
|
581
569
|
end
|
582
570
|
end
|
583
571
|
alias_method :recurse=, :recursive=
|
584
572
|
|
585
|
-
# Return a string
|
573
|
+
# Return a string representing the resolver state, suitable
|
586
574
|
# for printing on the screen.
|
587
575
|
#
|
588
576
|
# puts "Resolver state:"
|
@@ -629,11 +617,11 @@ module Net # :nodoc:
|
|
629
617
|
@config[:defname] = bool
|
630
618
|
@logger.info("Defname state changed to #{bool}")
|
631
619
|
else
|
632
|
-
raise
|
620
|
+
raise ArgumentError, "Argument must be boolean"
|
633
621
|
end
|
634
622
|
end
|
635
623
|
|
636
|
-
# Get the state of the dns_search flag
|
624
|
+
# Get the state of the dns_search flag.
|
637
625
|
def dns_search
|
638
626
|
@config[:dns_search]
|
639
627
|
end
|
@@ -642,14 +630,13 @@ module Net # :nodoc:
|
|
642
630
|
# Set the flag +dns_search+ in a boolean state. If +dns_search+
|
643
631
|
# is true, when using the Resolver#search method will be applied
|
644
632
|
# the search list. Default is true.
|
645
|
-
#
|
646
633
|
def dns_search=(bool)
|
647
634
|
case bool
|
648
635
|
when TrueClass,FalseClass
|
649
636
|
@config[:dns_search] = bool
|
650
637
|
@logger.info("DNS search state changed to #{bool}")
|
651
638
|
else
|
652
|
-
raise
|
639
|
+
raise ArgumentError, "Argument must be boolean"
|
653
640
|
end
|
654
641
|
end
|
655
642
|
alias_method("dnsrch=","dns_search=")
|
@@ -678,7 +665,7 @@ module Net # :nodoc:
|
|
678
665
|
@config[:use_tcp] = bool
|
679
666
|
@logger.info("Use tcp flag changed to #{bool}")
|
680
667
|
else
|
681
|
-
raise
|
668
|
+
raise ArgumentError, "Argument must be boolean"
|
682
669
|
end
|
683
670
|
end
|
684
671
|
alias usevc= use_tcp=
|
@@ -694,7 +681,7 @@ module Net # :nodoc:
|
|
694
681
|
@config[:ignore_truncated] = bool
|
695
682
|
@logger.info("Ignore truncated flag changed to #{bool}")
|
696
683
|
else
|
697
|
-
raise
|
684
|
+
raise ArgumentError, "Argument must be boolean"
|
698
685
|
end
|
699
686
|
end
|
700
687
|
|
@@ -712,8 +699,7 @@ module Net # :nodoc:
|
|
712
699
|
# puts "You set a timeout of " + res.tcp_timeout.pretty_to_s
|
713
700
|
# #=> You set a timeout of 2 minutes and 30 seconds
|
714
701
|
#
|
715
|
-
# If the timeout is infinite, a string "infinite" will
|
716
|
-
# be returned.
|
702
|
+
# If the timeout is infinite, a string "infinite" will be returned.
|
717
703
|
#
|
718
704
|
def tcp_timeout
|
719
705
|
@config[:tcp_timeout].to_s
|
@@ -725,7 +711,8 @@ module Net # :nodoc:
|
|
725
711
|
# The value is stored internally as a +TcpTimeout+ object, see
|
726
712
|
# the description for Resolver#tcp_timeout
|
727
713
|
#
|
728
|
-
# Default is
|
714
|
+
# Default is 5 seconds.
|
715
|
+
#
|
729
716
|
def tcp_timeout=(secs)
|
730
717
|
@config[:tcp_timeout] = TcpTimeout.new(secs)
|
731
718
|
@logger.info("New TCP timeout value: #{@config[:tcp_timeout]} seconds")
|
@@ -734,7 +721,7 @@ module Net # :nodoc:
|
|
734
721
|
# Return an object representing the value of the stored UDP
|
735
722
|
# timeout the resolver will use in is queries. This object
|
736
723
|
# is an instance of the class +UdpTimeout+, and two methods
|
737
|
-
# are available for printing
|
724
|
+
# are available for printing information: UdpTimeout#to_s
|
738
725
|
# and UdpTimeout#pretty_to_s.
|
739
726
|
#
|
740
727
|
# Here's some example:
|
@@ -756,10 +743,11 @@ module Net # :nodoc:
|
|
756
743
|
# will be performed using UDP. A value of 0 means that
|
757
744
|
# the timeout will not be used, and the resolver will use
|
758
745
|
# only +retry_number+ and +retry_interval+ parameters.
|
759
|
-
#
|
746
|
+
#
|
747
|
+
# Default is 5 seconds.
|
760
748
|
#
|
761
749
|
# The value is stored internally as a +UdpTimeout+ object, see
|
762
|
-
# the description for Resolver#udp_timeout
|
750
|
+
# the description for Resolver#udp_timeout.
|
763
751
|
#
|
764
752
|
def udp_timeout=(secs)
|
765
753
|
@config[:udp_timeout] = UdpTimeout.new(secs)
|
@@ -803,7 +791,7 @@ module Net # :nodoc:
|
|
803
791
|
@logger.close
|
804
792
|
@logger = logger
|
805
793
|
else
|
806
|
-
raise
|
794
|
+
raise ArgumentError, "Argument must be an instance of Logger class"
|
807
795
|
end
|
808
796
|
end
|
809
797
|
|
@@ -995,8 +983,9 @@ module Net # :nodoc:
|
|
995
983
|
ans = self.old_send(method,packet,packet_data)
|
996
984
|
|
997
985
|
unless ans
|
998
|
-
|
999
|
-
|
986
|
+
message = "No response from nameservers list"
|
987
|
+
@logger.fatal(message)
|
988
|
+
raise NoResponseError, message
|
1000
989
|
end
|
1001
990
|
|
1002
991
|
@logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
|
@@ -1033,8 +1022,6 @@ module Net # :nodoc:
|
|
1033
1022
|
# use, but automatically sort the results based on preferences
|
1034
1023
|
# and returns an ordered array.
|
1035
1024
|
#
|
1036
|
-
# Example:
|
1037
|
-
#
|
1038
1025
|
# res = Net::DNS::Resolver.new
|
1039
1026
|
# res.mx("google.com")
|
1040
1027
|
#
|
@@ -1046,13 +1033,10 @@ module Net # :nodoc:
|
|
1046
1033
|
return arr.sort_by {|a| a.preference}
|
1047
1034
|
end
|
1048
1035
|
|
1049
|
-
#
|
1050
1036
|
# Quick resolver method. Bypass the configuration using
|
1051
1037
|
# the defaults.
|
1052
1038
|
#
|
1053
|
-
#
|
1054
|
-
#
|
1055
|
-
# puts Net::DNS::Resolver.start "www.google.com"
|
1039
|
+
# Net::DNS::Resolver.start "www.google.com"
|
1056
1040
|
#
|
1057
1041
|
def self.start(*params)
|
1058
1042
|
self.new.search(*params)
|
@@ -1060,8 +1044,7 @@ module Net # :nodoc:
|
|
1060
1044
|
|
1061
1045
|
private
|
1062
1046
|
|
1063
|
-
#
|
1064
|
-
#
|
1047
|
+
# Parses a configuration file specified as the argument.
|
1065
1048
|
def parse_config_file
|
1066
1049
|
if self.class.platform_windows?
|
1067
1050
|
require 'win32/resolv'
|
@@ -1084,7 +1067,7 @@ module Net # :nodoc:
|
|
1084
1067
|
end
|
1085
1068
|
end
|
1086
1069
|
|
1087
|
-
#
|
1070
|
+
# Parses environment variables.
|
1088
1071
|
def parse_environment_variables
|
1089
1072
|
if ENV['RES_NAMESERVERS']
|
1090
1073
|
self.nameservers = ENV['RES_NAMESERVERS'].split(" ")
|
@@ -1101,7 +1084,7 @@ module Net # :nodoc:
|
|
1101
1084
|
begin
|
1102
1085
|
eval("self.#{name} = #{val}")
|
1103
1086
|
rescue NoMethodError
|
1104
|
-
raise
|
1087
|
+
raise ArgumentError, "Invalid ENV option #{name}"
|
1105
1088
|
end
|
1106
1089
|
end
|
1107
1090
|
end
|
@@ -1219,7 +1202,7 @@ module Net # :nodoc:
|
|
1219
1202
|
|
1220
1203
|
def valid?(name)
|
1221
1204
|
if name =~ /[^-\w\.]/
|
1222
|
-
raise
|
1205
|
+
raise ArgumentError, "Invalid domain name #{name}"
|
1223
1206
|
else
|
1224
1207
|
true
|
1225
1208
|
end
|
@@ -1239,44 +1222,6 @@ module Net # :nodoc:
|
|
1239
1222
|
|
1240
1223
|
end
|
1241
1224
|
|
1242
|
-
end # class Resolver
|
1243
|
-
end # module DNS
|
1244
|
-
end # module Net
|
1245
|
-
|
1246
|
-
class ResolverArgumentError < ArgumentError # :nodoc:
|
1247
|
-
end
|
1248
|
-
class NoResponseError < StandardError # :nodoc:
|
1249
|
-
end
|
1250
|
-
|
1251
|
-
module ExtendHash # :nodoc:
|
1252
|
-
# Returns an hash with all the
|
1253
|
-
# keys turned into downcase
|
1254
|
-
#
|
1255
|
-
# hsh = {"Test" => 1, "FooBar" => 2}
|
1256
|
-
# hsh.key_downcase!
|
1257
|
-
# #=> {"test"=>1,"foobar"=>2}
|
1258
|
-
#
|
1259
|
-
def key_downcase!
|
1260
|
-
hsh = Hash.new
|
1261
|
-
self.each do |key,val|
|
1262
|
-
hsh[key.downcase] = val
|
1263
1225
|
end
|
1264
|
-
self.replace(hsh)
|
1265
1226
|
end
|
1266
|
-
end
|
1267
|
-
|
1268
|
-
class Hash # :nodoc:
|
1269
|
-
include ExtendHash
|
1270
|
-
end
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1227
|
+
end
|