From fba46bce304eeeec0e3eedf9792dc071c2f39f85 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 26 Nov 2008 23:44:30 +0000 Subject: (Basic) IDNA and SRV fallback support --- core/s2smanager.lua | 87 ++++++++++++++++++++++++++++----------------- net/xmppserver_listener.lua | 11 ++++-- 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 6d8f3a00..df8c3937 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -8,6 +8,7 @@ local get_traceback = debug.traceback; local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; +local idna_to_ascii = require "util.encodings".idna.to_ascii; local connlisteners_get = require "net.connlisteners".get; local wraptlsclient = require "net.server".wraptlsclient; local modulemanager = require "core.modulemanager"; @@ -84,13 +85,28 @@ end function new_outgoing(from_host, to_host) local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; hosts[from_host].s2sout[to_host] = host_session; - local cl = connlisteners_get("xmppserver"); - local conn, handler = socket.tcp() - - local connect_host, connect_port = to_host, 5269; + local log; + do + local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); + log = logger_init(conn_name); + host_session.log = log; + end - local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV"); + attempt_connection(host_session); + + return host_session; +end + + +function attempt_connection(host_session, err) + local from_host, to_host = host_session.from_host, host_session.to_host; + local conn, handler = socket.tcp() + + local connect_host, connect_port = idna_to_ascii(to_host), 5269; + + if not err then -- This is our first attempt + local answer = dns.lookup("_xmpp-server._tcp."..connect_host..".", "SRV"); if answer then log("debug", to_host.." has SRV records, handling..."); @@ -102,38 +118,43 @@ function new_outgoing(from_host, to_host) t_sort(srv_hosts, compare_srv_priorities); local srv_choice = srv_hosts[1]; + host_session.srv_choice = 1; if srv_choice then connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); end end - - conn:settimeout(0); - local success, err = conn:connect(connect_host, connect_port); - if not success and err ~= "timeout" then - log("warn", "s2s connect() failed: %s", err); - end - - conn = wraptlsclient(cl, conn, connect_host, connect_port, 0, 1, hosts[from_host].ssl_ctx ); - host_session.conn = conn; - - -- Register this outgoing connection so that xmppserver_listener knows about it - -- otherwise it will assume it is a new incoming connection - cl.register_outgoing(conn, host_session); - - local log; - do - local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); - log = logger_init(conn_name); - host_session.log = log; - end - - local w = conn.write; - host_session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end - - conn.write(format([[]], from_host, to_host)); - - return host_session; + elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV + host_session.srv_choice = host_session.srv_choice + 1; + local srv_choice = host_session.srv_hosts[host_session.srv_choice]; + connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; + host_session.log("debug", "Attempt #%d: This time to %s:%d", host_session.srv_choice, connect_host, connect_port); + else + host_session.log("debug", "Out of connection options, can't connect to %s", tostring(host_session.to_host)); + -- We're out of options + return false; + end + + -- Ok, we're going to try to connect + conn:settimeout(0); + local success, err = conn:connect(connect_host, connect_port); + if not success and err ~= "timeout" then + log("warn", "s2s connect() failed: %s", err); + end + + local cl = connlisteners_get("xmppserver"); + conn = wraptlsclient(cl, conn, connect_host, connect_port, 0, 1, hosts[from_host].ssl_ctx ); + host_session.conn = conn; + + -- Register this outgoing connection so that xmppserver_listener knows about it + -- otherwise it will assume it is a new incoming connection + cl.register_outgoing(conn, host_session); + + local w = conn.write; + host_session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end + + conn.write(format([[]], from_host, to_host)); + return true; end function streamopened(session, attr) @@ -159,7 +180,7 @@ function streamopened(session, attr) send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve - session:close("host-unknown"); + session:close({ condition = "host-unknown"; text = "This host does not serve "..session.to_host }); return; end if session.version >= 1.0 then diff --git a/net/xmppserver_listener.lua b/net/xmppserver_listener.lua index 15ce0b23..cd9a3460 100644 --- a/net/xmppserver_listener.lua +++ b/net/xmppserver_listener.lua @@ -7,7 +7,7 @@ local s2s_new_incoming = require "core.s2smanager".new_incoming; local s2s_streamopened = require "core.s2smanager".streamopened; local s2s_streamclosed = require "core.s2smanager".streamclosed; local s2s_destroy_session = require "core.s2smanager".destroy_session; - +local s2s_attempt_connect = require "core.s2smanager".attempt_connection; local stream_callbacks = { streamopened = s2s_streamopened, streamclosed = s2s_streamclosed }; local connlisteners_register = require "net.connlisteners".register; @@ -109,10 +109,15 @@ function xmppserver.listener(conn, data) end end -function xmppserver.disconnect(conn) +function xmppserver.disconnect(conn, err) local session = sessions[conn]; if session then - (session.log or log)("info", "s2s disconnected: %s->%s", tostring(session.from_host), tostring(session.to_host)); + if err and err ~= "closed" and session.srv_hosts then + if s2s_attempt_connect(session, err) then + return; -- Session lives for now + end + end + (session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err)); s2s_destroy_session(session); sessions[conn] = nil; session = nil; -- cgit v1.2.3