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(
.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..status == :NXDOMAIN
result = false
break
end
if tmp..status == :NOERROR && tmp..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
|