Class: ReDNS::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/redns/connection.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

Constants ============================================================

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#timeoutObject

Properties ===========================================================



10
11
12
# File 'lib/redns/connection.rb', line 10

def timeout
  @timeout
end

Class Method Details

.instance {|connection| ... } ⇒ Object

Class Methods ========================================================

Yields:

  • (connection)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redns/connection.rb', line 18

def self.instance
  connection = EventMachine.open_datagram_socket(
    ReDNS::Support.bind_all_addr,
    0,
    self
  )
  
  yield(connection) if (block_given?)
  
  connection
end

Instance Method Details

#check_for_timeouts!Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/redns/connection.rb', line 107

def check_for_timeouts!
  timeout_at = Time.now - (@timeout || DEFAULT_TIMEOUT)
  
  @callback.keys.each do |k|
    params = @callback[k]

    if (params and params[:at] < timeout_at)
      params[:callback].call(nil)
      @callback.delete(k)
    end
  end
end

#nameserversObject



49
50
51
# File 'lib/redns/connection.rb', line 49

def nameservers
  @nameservers ||= ReDNS::Support.default_nameservers
end

#nameservers=(*list) ⇒ Object



53
54
55
56
# File 'lib/redns/connection.rb', line 53

def nameservers=(*list)
  @nameservers = list.flatten.compact
  @nameservers = nil if (list.empty?)
end

#portObject



58
59
60
# File 'lib/redns/connection.rb', line 58

def port
  Socket.unpack_sockaddr_in(get_sockname)[0]
end

#post_initObject

Instance Methods =====================================================



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/redns/connection.rb', line 32

def post_init
  # Sequence numbers do not have to be cryptographically secure, but having
  # a healthy amount of randomness is a good thing.
  @sequence = (rand(0x10000) ^ (object_id ^ (Time.now.to_f * 100000).to_i)) % 0x10000
  
  # Callback tracking is done by matching response IDs in a lookup table
  @callback = { }
  
  EventMachine.add_periodic_timer(1) do
    check_for_timeouts!
  end
end

#random_nameserverObject



45
46
47
# File 'lib/redns/connection.rb', line 45

def random_nameserver
  nameservers[rand(nameservers.length)]
end

#receive_data(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redns/connection.rb', line 62

def receive_data(data)
  message = ReDNS::Message.new(ReDNS::Buffer.new(data))
  
  if (callback = @callback.delete(message.id))
    answers = message.answers

    # If the request was made for a specific type of record...
    if (type = callback[:type])
      # ...only include that type of answer in the result set.
      answers = answers.select { |a| a.rtype == type }
    end

    callback[:callback].call(
      answers.collect { |a| a.rdata.to_s }
    )
  end
end

#resolve(query, type = nil, &callback) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/redns/connection.rb', line 80

def resolve(query, type = nil, &callback)
  message = ReDNS::Message.question(query, type) do |m|
    m.id = @sequence
  end

  result = send_datagram(
    message.serialize.to_s,
    random_nameserver,
    ReDNS::Support.dns_port
  )

  if (result > 0)
    @callback[@sequence] = {
      :callback => callback,
      :type => type,
      :at => Time.now
    }
  else
    callback.call(nil)
  end

  @sequence += 1
end

#unbindObject



104
105
# File 'lib/redns/connection.rb', line 104

def unbind
end