Mercurial > prosody-hg
comparison net/adns.lua @ 1787:c4dff34f3d32
net.adns: Utilise new net.dns API to handle DNS network errors
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 18 Sep 2009 02:46:11 +0100 |
| parents | 569d58d21612 |
| children | 45779d67c26c |
comparison
equal
deleted
inserted
replaced
| 1786:4016d8bc30b8 | 1787:c4dff34f3d32 |
|---|---|
| 9 local server = require "net.server"; | 9 local server = require "net.server"; |
| 10 local dns = require "net.dns"; | 10 local dns = require "net.dns"; |
| 11 | 11 |
| 12 local log = require "util.logger".init("adns"); | 12 local log = require "util.logger".init("adns"); |
| 13 | 13 |
| 14 local t_insert, t_remove = table.insert, table.remove; | |
| 14 local coroutine, tostring, pcall = coroutine, tostring, pcall; | 15 local coroutine, tostring, pcall = coroutine, tostring, pcall; |
| 15 | 16 |
| 16 module "adns" | 17 module "adns" |
| 17 | 18 |
| 18 function lookup(handler, qname, qtype, qclass) | 19 function lookup(handler, qname, qtype, qclass) |
| 39 if call_handler then | 40 if call_handler then |
| 40 coroutine.resume(handle[4]); | 41 coroutine.resume(handle[4]); |
| 41 end | 42 end |
| 42 end | 43 end |
| 43 | 44 |
| 44 function new_async_socket(sock) | 45 function new_async_socket(sock, resolver) |
| 45 local newconn = {}; | 46 local newconn, peername = {}, "<unknown>"; |
| 46 local listener = {}; | 47 local listener = {}; |
| 47 function listener.incoming(conn, data) | 48 function listener.incoming(conn, data) |
| 48 dns.feed(sock, data); | 49 dns.feed(sock, data); |
| 49 end | 50 end |
| 50 function listener.disconnect() | 51 function listener.disconnect(conn, err) |
| 52 log("warn", "DNS socket for %s disconnected: %s", peername, err); | |
| 53 local servers = resolver.server; | |
| 54 if resolver.socketset[newconn.handler] == resolver.best_server and resolver.best_server == #servers then | |
| 55 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]); | |
| 56 end | |
| 57 | |
| 58 resolver:servfail(conn); -- Let the magic commence | |
| 51 end | 59 end |
| 52 newconn.handler, newconn._socket = server.wrapclient(sock, "dns", 53, listener); | 60 newconn.handler, newconn._socket = server.wrapclient(sock, "dns", 53, listener); |
| 61 if not newconn.handler then | |
| 62 log("warn", "handler is nil"); | |
| 63 end | |
| 64 if not newconn._socket then | |
| 65 log("warn", "socket is nil"); | |
| 66 end | |
| 53 newconn.handler.settimeout = function () end | 67 newconn.handler.settimeout = function () end |
| 54 newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end | 68 newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end |
| 55 newconn.handler.setpeername = function (_, ...) local ret = sock:setpeername(...); _.setsend(sock.send); return ret; end | 69 newconn.handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _.setsend(sock.send); return ret; end |
| 56 newconn.handler.connect = function (_, ...) return sock:connect(...) end | 70 newconn.handler.connect = function (_, ...) return sock:connect(...) end |
| 57 newconn.handler.send = function (_, data) _.write(data); return _.sendbuffer(); end | 71 newconn.handler.send = function (_, data) _.write(data); return _.sendbuffer(); end |
| 58 return newconn.handler; | 72 return newconn.handler; |
| 59 end | 73 end |
| 60 | 74 |
