Comparison

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
comparison
equal deleted inserted replaced
336:d97d59cfd1e8 337:4a1dd1c2c219
1 1
2 local hosts = hosts; 2 local hosts = hosts;
3 local sessions = sessions; 3 local sessions = sessions;
4 local socket = require "socket"; 4 local socket = require "socket";
5 local format = string.format; 5 local format = string.format;
6 local t_insert = table.insert; 6 local t_insert, t_sort = table.insert, table.sort;
7 local get_traceback = debug.traceback; 7 local get_traceback = debug.traceback;
8 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber 8 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber
9 = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; 9 = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber;
10 10
11 local connlisteners_get = require "net.connlisteners".get; 11 local connlisteners_get = require "net.connlisteners".get;
22 22
23 local md5_hash = require "util.hashes".md5; 23 local md5_hash = require "util.hashes".md5;
24 24
25 local dialback_secret = "This is very secret!!! Ha!"; 25 local dialback_secret = "This is very secret!!! Ha!";
26 26
27 local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "hampton.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" }; 27 local dns = require "net.dns";
28 28
29 module "s2smanager" 29 module "s2smanager"
30
31 local function compare_srv_priorities(a,b) return a.priority < b.priority or a.weight < b.weight; end
30 32
31 function send_to_host(from_host, to_host, data) 33 function send_to_host(from_host, to_host, data)
32 if data.name then data = tostring(data); end 34 if data.name then data = tostring(data); end
33 local host = hosts[from_host].s2sout[to_host]; 35 local host = hosts[from_host].s2sout[to_host];
34 if host then 36 if host then
35 -- We have a connection to this host already 37 -- We have a connection to this host already
36 if host.type == "s2sout_unauthed" then 38 if host.type == "s2sout_unauthed" then
37 host.log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now..."); 39 (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now...");
38 if not host.notopen and not host.dialback_key then 40 if not host.notopen and not host.dialback_key then
39 host.log("debug", "dialback had not been initiated"); 41 host.log("debug", "dialback had not been initiated");
40 initiate_dialback(host); 42 initiate_dialback(host);
41 end 43 end
42 44
85 local cl = connlisteners_get("xmppserver"); 87 local cl = connlisteners_get("xmppserver");
86 88
87 local conn, handler = socket.tcp() 89 local conn, handler = socket.tcp()
88 90
89 --FIXME: Below parameters (ports/ip) are incorrect (use SRV) 91 --FIXME: Below parameters (ports/ip) are incorrect (use SRV)
90 to_host = srvmap[to_host] or to_host; 92
93 local connect_host, connect_port = to_host, 5269;
94
95 local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV");
96
97 if answer then
98 log("debug", to_host.." has SRV records, handling...");
99 local srv_hosts = {};
100 host_session.srv_hosts = srv_hosts;
101 for _, record in ipairs(answer) do
102 t_insert(srv_hosts, record.srv);
103 end
104 t_sort(srv_hosts, compare_srv_priorities);
105
106 local srv_choice = srv_hosts[1];
107 if srv_choice then
108 log("debug", "Best record found");
109 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
110 log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
111 end
112 end
91 113
92 conn:settimeout(0); 114 conn:settimeout(0);
93 local success, err = conn:connect(to_host, 5269); 115 local success, err = conn:connect(connect_host, connect_port);
94 if not success then 116 if not success and err ~= "timeout" then
95 log("warn", "s2s connect() failed: %s", err); 117 log("warn", "s2s connect() failed: %s", err);
96 end 118 end
97 119
98 conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx ); 120 conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx );
99 host_session.conn = conn; 121 host_session.conn = conn;