Annotate

net/adns.lua @ 5901:1d13f73af58e

certmanager: Default to using the server's cipher preference order by default, as clients have been shown to commonly select weak and insecure ciphers even when they support stronger ones
author Matthew Wild <mwild1@gmail.com>
date Sat, 09 Nov 2013 17:50:19 +0000
parent 5730:411e9e7d8035
child 5776:bd0ff8ae98a8
child 6287:a380b09649e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2710
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2710
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1208
diff changeset
8
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local server = require "net.server";
871
642af1bdf74e net.adns: Load the correct dns lib
Matthew Wild <mwild1@gmail.com>
parents: 870
diff changeset
10 local dns = require "net.dns";
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local log = require "util.logger".init("adns");
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
1787
c4dff34f3d32 net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
14 local t_insert, t_remove = table.insert, table.remove;
973
b091a1a7273b net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents: 886
diff changeset
15 local coroutine, tostring, pcall = coroutine, tostring, pcall;
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
2558
0a65fc0c7bee net.adns: Use different flavour of voodoo to make UDP sockets work smoothly with libevent (no packet merging)
Matthew Wild <mwild1@gmail.com>
parents: 2556
diff changeset
17 local function dummy_send(sock, data, i, j) return (j-i)+1; end
0a65fc0c7bee net.adns: Use different flavour of voodoo to make UDP sockets work smoothly with libevent (no packet merging)
Matthew Wild <mwild1@gmail.com>
parents: 2556
diff changeset
18
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module "adns"
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function lookup(handler, qname, qtype, qclass)
1005
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
22 return coroutine.wrap(function (peek)
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
23 if peek then
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
24 log("debug", "Records for %s already cached, using those...", qname);
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
25 handler(peek);
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
26 return;
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
27 end
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
28 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
3956
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
29 local ok, err = dns.query(qname, qtype, qclass);
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
30 if ok then
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
31 coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
32 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
33 end
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
34 if ok then
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
35 ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
36 else
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
37 log("error", "Error sending DNS query: %s", err);
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
38 ok, err = pcall(handler, nil, err);
32ec833c2edf net.adns: Handle dns.query() failures, and pass error to handler
Matthew Wild <mwild1@gmail.com>
parents: 3326
diff changeset
39 end
973
b091a1a7273b net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents: 886
diff changeset
40 if not ok then
1901
3c52b949e472 net.adns: Bump log-level of DNS handler errors to, er, 'error'
Matthew Wild <mwild1@gmail.com>
parents: 1788
diff changeset
41 log("error", "Error in DNS response handler: %s", tostring(err));
973
b091a1a7273b net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents: 886
diff changeset
42 end
1005
0eed5db7758d net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents: 980
diff changeset
43 end)(dns.peek(qname, qtype, qclass));
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
3325
b3117a1da834 net.dns, net.adns: Move coroutine-calling logic into resolver:cancel()
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
46 function cancel(handle, call_handler, reason)
1208
5f992ddc9685 Add log message when DNS lookup is cancelled
Matthew Wild <mwild1@gmail.com>
parents: 1207
diff changeset
47 log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
3326
fb95015bc646 net.dns, net.adns: Update resolver:cancel() API so that a table doesn't need to be created for each cancellation internal to net.dns
Matthew Wild <mwild1@gmail.com>
parents: 3325
diff changeset
48 dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);
1203
23725bfdeed5 net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents: 1005
diff changeset
49 end
23725bfdeed5 net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents: 1005
diff changeset
50
1787
c4dff34f3d32 net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
51 function new_async_socket(sock, resolver)
2232
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
52 local peername = "<unknown>";
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local listener = {};
2232
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
54 local handler = {};
2128
f107f0205793 net.adns: Update for new net.server API (doesn't work with libevent yet)
Matthew Wild <mwild1@gmail.com>
parents: 1901
diff changeset
55 function listener.onincoming(conn, data)
2652
cbc58fc170ad net.adns: Fix potential traceback on DNS responses with libevent enabled (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2578
diff changeset
56 if data then
cbc58fc170ad net.adns: Fix potential traceback on DNS responses with libevent enabled (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2578
diff changeset
57 dns.feed(handler, data);
cbc58fc170ad net.adns: Fix potential traceback on DNS responses with libevent enabled (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2578
diff changeset
58 end
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
2128
f107f0205793 net.adns: Update for new net.server API (doesn't work with libevent yet)
Matthew Wild <mwild1@gmail.com>
parents: 1901
diff changeset
60 function listener.ondisconnect(conn, err)
2661
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
61 if err then
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
62 log("warn", "DNS socket for %s disconnected: %s", peername, err);
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
63 local servers = resolver.server;
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
64 if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
65 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
66 end
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
67
be4b1e796bd2 net.adns: Don't treat locally-initiated disconnects as fatal with libevent backend in use (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2652
diff changeset
68 resolver:servfail(conn); -- Let the magic commence
1787
c4dff34f3d32 net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
69 end
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
5730
411e9e7d8035 net.dns, net.adns: Make sure errors from net.server are propagated (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 4103
diff changeset
71 handler, err = server.wrapclient(sock, "dns", 53, listener);
2232
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
72 if not handler then
5730
411e9e7d8035 net.dns, net.adns: Make sure errors from net.server are propagated (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 4103
diff changeset
73 return nil, err;
1787
c4dff34f3d32 net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
74 end
2232
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
75
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
76 handler.settimeout = function () end
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
77 handler.setsockname = function (_, ...) return sock:setsockname(...); end
2558
0a65fc0c7bee net.adns: Use different flavour of voodoo to make UDP sockets work smoothly with libevent (no packet merging)
Matthew Wild <mwild1@gmail.com>
parents: 2556
diff changeset
78 handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end
2556
50d1ba86a959 net.adns: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2232
diff changeset
79 handler.connect = function (_, ...) return sock:connect(...) end
2558
0a65fc0c7bee net.adns: Use different flavour of voodoo to make UDP sockets work smoothly with libevent (no packet merging)
Matthew Wild <mwild1@gmail.com>
parents: 2556
diff changeset
80 --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end
3990
764922062c38 net.adns: Log the DNS server that a query is sent to
Matthew Wild <mwild1@gmail.com>
parents: 3956
diff changeset
81 handler.send = function (_, data)
4103
c994524ef953 net.adns: Fix logging to handle unconnected UDP sockets
Matthew Wild <mwild1@gmail.com>
parents: 3990
diff changeset
82 local getpeername = sock.getpeername;
c994524ef953 net.adns: Fix logging to handle unconnected UDP sockets
Matthew Wild <mwild1@gmail.com>
parents: 3990
diff changeset
83 log("debug", "Sending DNS query to %s", (getpeername and getpeername(sock)) or "<unconnected>");
3990
764922062c38 net.adns: Log the DNS server that a query is sent to
Matthew Wild <mwild1@gmail.com>
parents: 3956
diff changeset
84 return sock:send(data);
764922062c38 net.adns: Log the DNS server that a query is sent to
Matthew Wild <mwild1@gmail.com>
parents: 3956
diff changeset
85 end
2232
aa8db84ae69d net.adns: Some cleanup, happens to also make it compatible with libevent
Matthew Wild <mwild1@gmail.com>
parents: 2128
diff changeset
86 return handler;
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
2578
61e5eff54415 net.dns, net.adns: Changed dns:socket_wrapper_set to dns.socket_wrapper_set for consistency.
Waqas Hussain <waqas20@gmail.com>
parents: 2558
diff changeset
89 dns.socket_wrapper_set(new_async_socket);
872
24018ba2f7c0 net.adns: Return _M
Matthew Wild <mwild1@gmail.com>
parents: 871
diff changeset
90
24018ba2f7c0 net.adns: Return _M
Matthew Wild <mwild1@gmail.com>
parents: 871
diff changeset
91 return _M;