Class: DNS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/faildns/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
# File 'lib/faildns/client.rb', line 29

def initialize (options={})
  @options = options

  @servers = @options[:servers] || []

  if block_given?
    yield self
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/faildns/client.rb', line 27

def options
  @options
end

#serversObject (readonly)

Returns the value of attribute servers.



27
28
29
# File 'lib/faildns/client.rb', line 27

def servers
  @servers
end

Instance Method Details

#query(message, timeout = 10) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/faildns/client.rb', line 94

def query (message, timeout=10)
  result = []
  socket = UDPSocket.new

  @servers.each {|server|
    socket.connect(server.to_s, 53)

    socket.print message.pack

    if (tmp = Timeout.timeout(timeout) { socket.recvfrom(512) } rescue nil)
      DNS.debug tmp, { :level => 9 }

      result.push Message.parse(tmp[0])
    end
  }

  return result
end

#resolve(domain, timeout = 10, tries = 3) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/faildns/client.rb', line 39

def resolve (domain, timeout=10, tries=3)
  result = nil
  socket = UDPSocket.new

  id = (rand * 100000).to_i % 65536

  1.upto(tries) {
    @servers.each {|server|
      socket.connect(server.to_s, 53)

      socket.print Message.new(
        Header.new {|h|
          h.id = id

          h.type  = :QUERY
          h.class = :QUERY

          h.recursive!

          h.questions = 1
        },

        [Question.new {|q|
          q.name = domain

          q.class = :IN
          q.type  = :A
        }]
      ).pack

      if (tmp = Timeout.timeout(timeout) { socket.recvfrom(512) } rescue nil)
        DNS.debug tmp, { :level => 9 }

        tmp = Message.parse(tmp[0])

        if tmp.header.status == :NXDOMAIN
          result = false
          break
        end

        if tmp.header.status == :NOERROR && tmp.header.id == id
          result = tmp.answers.find {|answer| answer.type == :A}.data.to_s rescue nil
          break
        end
      end
    }

    if !result.nil?
      break
    end
  }

  return result
end