redns 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/redns/connection.rb +53 -5
- data/lib/redns/message.rb +8 -1
- data/lib/redns/support.rb +4 -0
- data/redns.gemspec +1 -1
- data/test/test_redns_connection.rb +51 -3
- data/test/test_redns_message.rb +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/redns/connection.rb
CHANGED
@@ -3,6 +3,12 @@ require 'socket'
|
|
3
3
|
class ReDNS::Connection < EventMachine::Connection
|
4
4
|
# == Constants ============================================================
|
5
5
|
|
6
|
+
DEFAULT_TIMEOUT = 5
|
7
|
+
|
8
|
+
# == Properties ===========================================================
|
9
|
+
|
10
|
+
attr_accessor :timeout
|
11
|
+
|
6
12
|
# == Extensions ===========================================================
|
7
13
|
|
8
14
|
include EventMachine::Deferrable
|
@@ -10,11 +16,15 @@ class ReDNS::Connection < EventMachine::Connection
|
|
10
16
|
# == Class Methods ========================================================
|
11
17
|
|
12
18
|
def self.instance
|
13
|
-
EventMachine.open_datagram_socket(
|
19
|
+
connection = EventMachine.open_datagram_socket(
|
14
20
|
ReDNS::Support.bind_all_addr,
|
15
21
|
0,
|
16
22
|
self
|
17
23
|
)
|
24
|
+
|
25
|
+
yield(connection) if (block_given?)
|
26
|
+
|
27
|
+
connection
|
18
28
|
end
|
19
29
|
|
20
30
|
# == Instance Methods =====================================================
|
@@ -26,6 +36,23 @@ class ReDNS::Connection < EventMachine::Connection
|
|
26
36
|
|
27
37
|
# Callback tracking is done by matching response IDs in a lookup table
|
28
38
|
@callback = { }
|
39
|
+
|
40
|
+
EventMachine.add_periodic_timer(1) do
|
41
|
+
check_for_timeouts!
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def random_nameserver
|
46
|
+
nameservers[rand(nameservers.length)]
|
47
|
+
end
|
48
|
+
|
49
|
+
def nameservers
|
50
|
+
@nameservers ||= ReDNS::Support.default_nameservers
|
51
|
+
end
|
52
|
+
|
53
|
+
def nameservers=(*list)
|
54
|
+
@nameservers = list.flatten.compact
|
55
|
+
@nameservers = nil if (list.empty?)
|
29
56
|
end
|
30
57
|
|
31
58
|
def port
|
@@ -35,8 +62,8 @@ class ReDNS::Connection < EventMachine::Connection
|
|
35
62
|
def receive_data(data)
|
36
63
|
message = ReDNS::Message.new(ReDNS::Buffer.new(data))
|
37
64
|
|
38
|
-
if (callback = @callback
|
39
|
-
callback.
|
65
|
+
if (callback = @callback.delete(message.id))
|
66
|
+
callback[:callback].call(message.answers.collect { |a| a.rdata.to_s })
|
40
67
|
end
|
41
68
|
end
|
42
69
|
|
@@ -47,12 +74,15 @@ class ReDNS::Connection < EventMachine::Connection
|
|
47
74
|
|
48
75
|
result = send_datagram(
|
49
76
|
message.serialize.to_s,
|
50
|
-
|
77
|
+
random_nameserver,
|
51
78
|
ReDNS::Support.dns_port
|
52
79
|
)
|
53
80
|
|
54
81
|
if (result > 0)
|
55
|
-
@callback[@sequence] =
|
82
|
+
@callback[@sequence] = {
|
83
|
+
:callback => callback,
|
84
|
+
:at => Time.now
|
85
|
+
}
|
56
86
|
else
|
57
87
|
callback.call(nil)
|
58
88
|
end
|
@@ -62,4 +92,22 @@ class ReDNS::Connection < EventMachine::Connection
|
|
62
92
|
|
63
93
|
def unbind
|
64
94
|
end
|
95
|
+
|
96
|
+
def check_for_timeouts!
|
97
|
+
timeout_at = Time.now - (@timeout || DEFAULT_TIMEOUT)
|
98
|
+
|
99
|
+
@callback.keys.each do |k|
|
100
|
+
params = @callback[k]
|
101
|
+
|
102
|
+
if (params and params[:at] < timeout_at)
|
103
|
+
params[:callback].call(nil)
|
104
|
+
@callback.delete(k)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def timeout=(value)
|
110
|
+
@timeout = value.to_i
|
111
|
+
@timeout = DEFAULT_TIMEOUT if (@timeout == 0)
|
112
|
+
end
|
65
113
|
end
|
data/lib/redns/message.rb
CHANGED
@@ -29,7 +29,14 @@ class ReDNS::Message < ReDNS::Fragment
|
|
29
29
|
# == Class Methods ========================================================
|
30
30
|
|
31
31
|
def self.question(name, qtype = nil)
|
32
|
-
|
32
|
+
if (!qtype)
|
33
|
+
if (ReDNS::Support.is_ip?(name))
|
34
|
+
name = ReDNS::Support.addr_to_arpa(name)
|
35
|
+
qtype = :ptr
|
36
|
+
else
|
37
|
+
qtype = :a
|
38
|
+
end
|
39
|
+
end
|
33
40
|
|
34
41
|
message = new(
|
35
42
|
:questions => [
|
data/lib/redns/support.rb
CHANGED
data/redns.gemspec
CHANGED
@@ -18,22 +18,70 @@ class TestReDNSConnection < Test::Unit::TestCase
|
|
18
18
|
|
19
19
|
def test_simple_resolve
|
20
20
|
address = nil
|
21
|
+
reverse = nil
|
22
|
+
nameservers = nil
|
21
23
|
|
22
24
|
EventMachine.run do
|
23
25
|
dns = ReDNS::Connection.instance
|
24
26
|
|
27
|
+
assert dns.nameservers.length > 0
|
28
|
+
|
25
29
|
dns.resolve('example.com') do |result|
|
26
30
|
address = result
|
27
31
|
|
28
|
-
EventMachine.stop_event_loop
|
32
|
+
EventMachine.stop_event_loop if (address and reverse and nameservers)
|
33
|
+
end
|
34
|
+
|
35
|
+
dns.resolve('192.0.32.10') do |result|
|
36
|
+
reverse = result
|
37
|
+
|
38
|
+
EventMachine.stop_event_loop if (address and reverse and nameservers)
|
39
|
+
end
|
40
|
+
|
41
|
+
dns.resolve('example.com', :ns) do |result|
|
42
|
+
nameservers = result
|
43
|
+
|
44
|
+
EventMachine.stop_event_loop if (address and reverse and nameservers)
|
29
45
|
end
|
30
46
|
|
31
|
-
EventMachine.add_timer(
|
47
|
+
EventMachine.add_timer(4) do
|
32
48
|
EventMachine.stop_event_loop
|
33
49
|
end
|
34
50
|
end
|
35
51
|
|
36
|
-
assert address, "Address was not defined, connection may have timed out"
|
37
52
|
assert_equal %w[ 192.0.32.10 ], address
|
53
|
+
assert_equal %w[ www.example.com. ], reverse
|
54
|
+
assert_equal %w[ a.iana-servers.net. b.iana-servers.net. ], nameservers.sort
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_simple_timeout
|
58
|
+
address = :fail
|
59
|
+
|
60
|
+
EventMachine.run do
|
61
|
+
dns = ReDNS::Connection.instance do |c|
|
62
|
+
c.nameservers = %w[ 127.0.0.127 ]
|
63
|
+
c.timeout = 2
|
64
|
+
end
|
65
|
+
|
66
|
+
dns.resolve('example.com') do |result|
|
67
|
+
address = result
|
68
|
+
|
69
|
+
EventMachine.stop_event_loop
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
assert_equal nil, address
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_default_timeout
|
77
|
+
EventMachine.run do
|
78
|
+
dns = ReDNS::Connection.instance do |c|
|
79
|
+
c.timeout = nil
|
80
|
+
end
|
81
|
+
|
82
|
+
assert_equal ReDNS::Connection::DEFAULT_TIMEOUT, dns.timeout
|
83
|
+
|
84
|
+
EventMachine.stop_event_loop
|
85
|
+
end
|
38
86
|
end
|
39
87
|
end
|
data/test/test_redns_message.rb
CHANGED
@@ -144,7 +144,7 @@ class TestReDNSMessage < Test::Unit::TestCase
|
|
144
144
|
|
145
145
|
assert_equal 1, question.questions.length
|
146
146
|
|
147
|
-
assert_equal '
|
147
|
+
assert_equal '1.0.0.127.in-addr.arpa.', question.questions[0].name.to_s
|
148
148
|
assert_equal :ptr, question.questions[0].qtype
|
149
149
|
end
|
150
150
|
|