Diff

core/s2smanager.lua @ 337:4a1dd1c2c219

We have SRV resolving \o/
author Matthew Wild <mwild1@gmail.com>
date Tue, 18 Nov 2008 22:41:04 +0000
parent 333:8d15b073fdbe
child 343:cae2178b5623
line wrap: on
line diff
--- a/core/s2smanager.lua	Tue Nov 18 22:40:23 2008 +0000
+++ b/core/s2smanager.lua	Tue Nov 18 22:41:04 2008 +0000
@@ -3,7 +3,7 @@
 local sessions = sessions;
 local socket = require "socket";
 local format = string.format;
-local t_insert = table.insert;
+local t_insert, t_sort = table.insert, table.sort;
 local get_traceback = debug.traceback;
 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber
     = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber;
@@ -24,17 +24,19 @@
 
 local dialback_secret = "This is very secret!!! Ha!";
 
-local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "hampton.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" };
+local dns = require "net.dns";
 
 module "s2smanager"
 
+local function compare_srv_priorities(a,b) return a.priority < b.priority or a.weight < b.weight; end
+
 function send_to_host(from_host, to_host, data)
 	if data.name then data = tostring(data); end
 	local host = hosts[from_host].s2sout[to_host];
 	if host then
 		-- We have a connection to this host already
 		if host.type == "s2sout_unauthed" then
-			host.log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now...");
+			(host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now...");
 			if not host.notopen and not host.dialback_key then
 				host.log("debug", "dialback had not been initiated");
 				initiate_dialback(host);
@@ -87,11 +89,31 @@
 		local conn, handler = socket.tcp()
 		
 		--FIXME: Below parameters (ports/ip) are incorrect (use SRV)
-		to_host = srvmap[to_host] or to_host;
+		
+		local connect_host, connect_port = to_host, 5269;
+		
+		local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV");
+		
+		if answer then
+			log("debug", to_host.." has SRV records, handling...");
+			local srv_hosts = {};
+			host_session.srv_hosts = srv_hosts;
+			for _, record in ipairs(answer) do
+				t_insert(srv_hosts, record.srv);
+			end
+			t_sort(srv_hosts, compare_srv_priorities);
+			
+			local srv_choice = srv_hosts[1];
+			if srv_choice then
+				log("debug", "Best record found");
+				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(to_host, 5269);
-		if not success then
+		local success, err = conn:connect(connect_host, connect_port);
+		if not success and err ~= "timeout" then
 			log("warn", "s2s connect() failed: %s", err);
 		end