Thu, 25 Dec 2014 10:48:06 +0000
Initial commit. Tortoises are fun.
0
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | -- Prosody IM |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | -- Copyright (C) 2008-2010 Matthew Wild |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | -- Copyright (C) 2008-2010 Waqas Hussain |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | -- |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | -- This project is MIT/X11 licensed. Please see the |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | -- COPYING file in the source package for more information. |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | -- |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | local server = require "net.server"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local dns = require "net.dns"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | local log = require "util.logger".init("adns"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | local t_insert, t_remove = table.insert, table.remove; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | local coroutine, tostring, pcall = coroutine, tostring, pcall; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | local function dummy_send(sock, data, i, j) return (j-i)+1; end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | module "adns" |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | function lookup(handler, qname, qtype, qclass) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | return coroutine.wrap(function (peek) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | if peek then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | log("debug", "Records for %s already cached, using those...", qname); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | handler(peek); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | return; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running())); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | local ok, err = dns.query(qname, qtype, qclass); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | if ok then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | if ok then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | log("error", "Error sending DNS query: %s", err); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | ok, err = pcall(handler, nil, err); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | if not ok then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | log("error", "Error in DNS response handler: %s", tostring(err)); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | end)(dns.peek(qname, qtype, qclass)); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | function cancel(handle, call_handler, reason) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | log("warn", "Cancelling DNS lookup for %s", tostring(handle[3])); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | function new_async_socket(sock, resolver) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | local peername = "<unknown>"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | local listener = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | local handler = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | local err; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | function listener.onincoming(conn, data) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | if data then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | dns.feed(handler, data); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | function listener.ondisconnect(conn, err) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | if err then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | log("warn", "DNS socket for %s disconnected: %s", peername, err); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | local servers = resolver.server; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | resolver:servfail(conn); -- Let the magic commence |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | handler, err = server.wrapclient(sock, "dns", 53, listener); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | if not handler then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | return nil, err; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | handler.settimeout = function () end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | handler.setsockname = function (_, ...) return sock:setsockname(...); end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | handler.connect = function (_, ...) return sock:connect(...) end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | handler.send = function (_, data) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | log("debug", "Sending DNS query to %s", peername); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | return sock:send(data); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | return handler; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | dns.socket_wrapper_set(new_async_socket); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | return _M; |