Changeset

434:0d7ba3742f7a

(Basic) IDNA and SRV fallback support
author Matthew Wild <mwild1@gmail.com>
date Wed, 26 Nov 2008 23:44:30 +0000
parents 433:afbf29498123
children 435:4087aa611de2
files core/s2smanager.lua net/xmppserver_listener.lua
diffstat 2 files changed, 62 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/core/s2smanager.lua	Wed Nov 26 23:12:05 2008 +0000
+++ b/core/s2smanager.lua	Wed Nov 26 23:44:30 2008 +0000
@@ -8,6 +8,7 @@
 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 @@
 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 log;
+		do
+			local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$");
+			log = logger_init(conn_name);
+			host_session.log = log;
+		end
+		
+		attempt_connection(host_session);
+		
+		return host_session;
+end
 
-		local connect_host, connect_port = to_host, 5269;
-		
-		local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV");
+
+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 @@
 			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([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], 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([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host));
+	return true;
 end
 
 function streamopened(session, attr)
@@ -159,7 +180,7 @@
 		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
--- a/net/xmppserver_listener.lua	Wed Nov 26 23:12:05 2008 +0000
+++ b/net/xmppserver_listener.lua	Wed Nov 26 23:44:30 2008 +0000
@@ -7,7 +7,7 @@
 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 @@
 	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;