Tue, 09 Mar 2010 18:19:50 +0500
mod_compression: More robust stanza processing.
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1208
diff
changeset
|
1 | -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1208
diff
changeset
|
2 | -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1208
diff
changeset
|
3 | -- Copyright (C) 2008-2009 Waqas Hussain |
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 | |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | module "adns" |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | function lookup(handler, qname, qtype, qclass) |
1010
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
20 | return coroutine.wrap(function (peek) |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
21 | if peek then |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
22 | 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
|
23 | handler(peek); |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
24 | return; |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
25 | end |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
26 | log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running())); |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | dns.query(qname, qtype, qclass); |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
28 | coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply |
1010
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
29 | log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); |
973
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
30 | local ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); |
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
31 | 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
|
32 | 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
|
33 | end |
1010
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
34 | 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
|
35 | end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
37 | function cancel(handle, call_handler) |
1208
5f992ddc9685
Add log message when DNS lookup is cancelled
Matthew Wild <mwild1@gmail.com>
parents:
1207
diff
changeset
|
38 | log("warn", "Cancelling DNS lookup for %s", tostring(handle[3])); |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
39 | dns.cancel(handle); |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
40 | if call_handler then |
1207
6842bacddfcb
net.adns: Fix for calling handler when cancelling DNS lookup
Matthew Wild <mwild1@gmail.com>
parents:
1203
diff
changeset
|
41 | coroutine.resume(handle[4]); |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
42 | end |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
43 | end |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1010
diff
changeset
|
44 | |
1787
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
45 | function new_async_socket(sock, resolver) |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
46 | local newconn, peername = {}, "<unknown>"; |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | local listener = {}; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | function listener.incoming(conn, data) |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | dns.feed(sock, data); |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | end |
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 listener.disconnect(conn, err) |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
52 | log("warn", "DNS socket for %s disconnected: %s", peername, err); |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
53 | local servers = resolver.server; |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
54 | if resolver.socketset[newconn.handler] == resolver.best_server and resolver.best_server == #servers then |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
55 | log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]); |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
56 | end |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
57 | |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
58 | resolver:servfail(conn); -- Let the magic commence |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | newconn.handler, newconn._socket = server.wrapclient(sock, "dns", 53, listener); |
1787
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
61 | if not newconn.handler then |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
62 | log("warn", "handler is nil"); |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
63 | end |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
64 | if not newconn._socket then |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
65 | log("warn", "socket is nil"); |
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
66 | end |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | newconn.handler.settimeout = function () end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end |
1787
c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
69 | newconn.handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _.setsend(sock.send); return ret; end |
2710
1600aff0839f
net.adns: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
1901
diff
changeset
|
70 | newconn.handler.connect = function (_, ...) return sock:connect(...) end |
1600aff0839f
net.adns: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
1901
diff
changeset
|
71 | newconn.handler.send = function (_, data) _.write(data); return _.sendbuffer(); end |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | return newconn.handler; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | dns:socket_wrapper_set(new_async_socket); |
872 | 76 | |
77 | return _M; |