1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
-
+
-
-
-
+
+
-
-
-
+
-
+
|
require "./rr"
require "./settings"
require "./response"
module AsyncDNS
class Resolver
getter :settings
getter settings : Settings
enum Error
NO_NAME_SERVER
end
private struct Context
def initialize(@id : UInt16, @settings : Settings,
@block : Response | Error ->)
private record Context, id : UInt16, settings : Settings,
block : Response | Error ->
end
end
def initialize
@settings = Settings.new
@queries = Hash(UInt16, Context).new
@tcp_queries = Hash(Socket, Context).new
end
def resolve(query, &block : Response | Error ->)
def resolve(query : Query, &block : Response | Error ->)
id : UInt16
while true
id = Random::Secure.rand(UInt16::MIN..UInt16::MAX)
break unless @queries.has_key?(id)
end
if @settings.nameservers.empty?
yield Error::NO_NAME_SERVER
return
end
send(Context.new(id, settings.dup, block))
end
def send(context : Context)
private def send(context : Context)
@queries[context.@id] = context
# TODO
end
def stop
# TODO
end
end
end
|