comparison core/s2smanager.lua @ 4541:05f5ec99da77

Merge with trunk
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jan 2012 22:55:49 +0000
parents a81d045e7d16
children 694491140a67 3dce04129693
comparison
equal deleted inserted replaced
4530:40905e7bf680 4541:05f5ec99da77
14 local add_task = require "util.timer".add_task; 14 local add_task = require "util.timer".add_task;
15 local socket = require "socket"; 15 local socket = require "socket";
16 local format = string.format; 16 local format = string.format;
17 local t_insert, t_sort = table.insert, table.sort; 17 local t_insert, t_sort = table.insert, table.sort;
18 local get_traceback = debug.traceback; 18 local get_traceback = debug.traceback;
19 local tostring, pairs, ipairs, getmetatable, newproxy, next, error, tonumber, setmetatable 19 local tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable
20 = tostring, pairs, ipairs, getmetatable, newproxy, next, error, tonumber, setmetatable; 20 = tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable;
21 21
22 local idna_to_ascii = require "util.encodings".idna.to_ascii; 22 local idna_to_ascii = require "util.encodings".idna.to_ascii;
23 local connlisteners_get = require "net.connlisteners".get; 23 local connlisteners_get = require "net.connlisteners".get;
24 local initialize_filters = require "util.filters".initialize; 24 local initialize_filters = require "util.filters".initialize;
25 local wrapclient = require "net.server".wrapclient; 25 local wrapclient = require "net.server".wrapclient;
26 local modulemanager = require "core.modulemanager";
27 local st = require "stanza"; 26 local st = require "stanza";
28 local stanza = st.stanza; 27 local stanza = st.stanza;
29 local nameprep = require "util.encodings".stringprep.nameprep; 28 local nameprep = require "util.encodings".stringprep.nameprep;
30 local cert_verify_identity = require "util.x509".verify_identity; 29 local cert_verify_identity = require "util.x509".verify_identity;
31 local new_ip = require "util.ip".new_ip; 30 local new_ip = require "util.ip".new_ip;
293 end 292 end
294 293
295 return try_connect(host_session, connect_host, connect_port); 294 return try_connect(host_session, connect_host, connect_port);
296 end 295 end
297 296
298 function try_next_ip(host_session, connect_port) 297 function try_next_ip(host_session)
299 host_session.connecting = nil; 298 host_session.connecting = nil;
300 host_session.ip_choice = host_session.ip_choice + 1; 299 host_session.ip_choice = host_session.ip_choice + 1;
301 local ip = host_session.ip_hosts[host_session.ip_choice]; 300 local ip = host_session.ip_hosts[host_session.ip_choice];
302 local ok, err= make_connect(host_session, ip, connect_port); 301 local ok, err= make_connect(host_session, ip.ip, ip.port);
303 if not ok then 302 if not ok then
304 if not attempt_connection(host_session, err or "closed") then 303 if not attempt_connection(host_session, err or "closed") then
305 err = err and (": "..err) or ""; 304 err = err and (": "..err) or "";
306 destroy_session(host_session, "Connection failed"..err); 305 destroy_session(host_session, "Connection failed"..err);
307 end 306 end
318 local has_other = false; 317 local has_other = false;
319 318
320 if not sources then 319 if not sources then
321 sources = {}; 320 sources = {};
322 local cfg_sources = config.get("*", "core", "interface") or connlisteners_get("xmppserver").default_interface; 321 local cfg_sources = config.get("*", "core", "interface") or connlisteners_get("xmppserver").default_interface;
322 if type(cfg_sources) == "string" then
323 cfg_sources = { cfg_sources };
324 end
323 for i, source in ipairs(cfg_sources) do 325 for i, source in ipairs(cfg_sources) do
324 if source == "*" then 326 if source == "*" then
325 sources[i] = new_ip("0.0.0.0", "IPv4"); 327 sources[i] = new_ip("0.0.0.0", "IPv4");
326 else 328 else
327 sources[i] = new_ip(source, (source:find(":") and "IPv6") or "IPv4"); 329 sources[i] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
352 end 354 end
353 355
354 if has_other then 356 if has_other then
355 if #IPs > 0 then 357 if #IPs > 0 then
356 rfc3484_dest(host_session.ip_hosts, sources); 358 rfc3484_dest(host_session.ip_hosts, sources);
359 for i = 1, #IPs do
360 IPs[i] = {ip = IPs[i], port = connect_port};
361 end
357 host_session.ip_choice = 0; 362 host_session.ip_choice = 0;
358 try_next_ip(host_session, connect_port); 363 try_next_ip(host_session);
359 else 364 else
360 log("debug", "DNS lookup failed to get a response for %s", connect_host); 365 log("debug", "DNS lookup failed to get a response for %s", connect_host);
361 host_session.ip_hosts = nil; 366 host_session.ip_hosts = nil;
362 if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can 367 if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can
363 log("debug", "No other records to try for %s - destroying", host_session.to_host); 368 log("debug", "No other records to try for %s - destroying", host_session.to_host);
381 end 386 end
382 387
383 if has_other then 388 if has_other then
384 if #IPs > 0 then 389 if #IPs > 0 then
385 rfc3484_dest(host_session.ip_hosts, sources); 390 rfc3484_dest(host_session.ip_hosts, sources);
391 for i = 1, #IPs do
392 IPs[i] = {ip = IPs[i], port = connect_port};
393 end
386 host_session.ip_choice = 0; 394 host_session.ip_choice = 0;
387 try_next_ip(host_session, connect_port); 395 try_next_ip(host_session);
388 else 396 else
389 log("debug", "DNS lookup failed to get a response for %s", connect_host); 397 log("debug", "DNS lookup failed to get a response for %s", connect_host);
390 host_session.ip_hosts = nil; 398 host_session.ip_hosts = nil;
391 if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can 399 if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can
392 log("debug", "No other records to try for %s - destroying", host_session.to_host); 400 log("debug", "No other records to try for %s - destroying", host_session.to_host);
399 end 407 end
400 end, connect_host, "AAAA", "IN"); 408 end, connect_host, "AAAA", "IN");
401 409
402 return true; 410 return true;
403 elseif host_session.ip_hosts and #host_session.ip_hosts > host_session.ip_choice then -- Not our first attempt, and we also have IPs left to try 411 elseif host_session.ip_hosts and #host_session.ip_hosts > host_session.ip_choice then -- Not our first attempt, and we also have IPs left to try
404 try_next_ip(host_session, connect_port); 412 try_next_ip(host_session);
405 else 413 else
406 host_session.ip_hosts = nil; 414 host_session.ip_hosts = nil;
407 if not attempt_connection(host_session, "out of IP addresses") then -- Retry if we can 415 if not attempt_connection(host_session, "out of IP addresses") then -- Retry if we can
408 log("debug", "No other records to try for %s - destroying", host_session.to_host); 416 log("debug", "No other records to try for %s - destroying", host_session.to_host);
409 err = err and (": "..err) or ""; 417 err = err and (": "..err) or "";