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 CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -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[message.id])
39
- callback.yield(message.answers.collect { |a| a.rdata.to_s })
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
- ReDNS::Support.default_resolver_address,
77
+ random_nameserver,
51
78
  ReDNS::Support.dns_port
52
79
  )
53
80
 
54
81
  if (result > 0)
55
- @callback[@sequence] = callback
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
- qtype ||= ReDNS::Support.is_ip?(name) ? :ptr : :a
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
@@ -52,6 +52,10 @@ module ReDNS::Support
52
52
  def dns_port
53
53
  53
54
54
  end
55
+
56
+ def default_nameservers
57
+ ReDNS::Resolver.servers
58
+ end
55
59
 
56
60
  def default_resolver_address
57
61
  ReDNS::Resolver.servers.first
data/redns.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redns}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["tadman"]
@@ -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(40) do
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
@@ -144,7 +144,7 @@ class TestReDNSMessage < Test::Unit::TestCase
144
144
 
145
145
  assert_equal 1, question.questions.length
146
146
 
147
- assert_equal '127.0.0.1', question.questions[0].name.to_s
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
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - tadman