Comparison

plugins/mod_s2s/s2sout.lib.lua @ 7098:5286e79c6829

Merge 0.9->0.10
author Kim Alvefur <zash@zash.se>
date Fri, 22 Jan 2016 14:49:05 +0100
parent 6917:65344c3bae7a
parent 7092:bee63de49663
child 7561:ab8324d3b899
comparison
equal deleted inserted replaced
7090:7aa37d70944b 7098:5286e79c6829
16 local rfc6724_dest = require "util.rfc6724".destination; 16 local rfc6724_dest = require "util.rfc6724".destination;
17 local socket = require "socket"; 17 local socket = require "socket";
18 local adns = require "net.adns"; 18 local adns = require "net.adns";
19 local dns = require "net.dns"; 19 local dns = require "net.dns";
20 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs; 20 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs;
21 local local_addresses = require "util.net".local_addresses;
21 22
22 local s2s_destroy_session = require "core.s2smanager".destroy_session; 23 local s2s_destroy_session = require "core.s2smanager".destroy_session;
23 24
24 local log = module._log; 25 local log = module._log;
25 26
26 local anysource = { IPv4 = "0.0.0.0", IPv6 = "::" }; 27 local sources = {};
27 local function get_sources(addrs)
28 local sources = {};
29 for _, IP in ipairs(addrs) do
30 local sock;
31 if IP.proto == "IPv4" then
32 sock = socket.udp();
33 elseif IP.proto == "IPv6" then
34 sock = socket.udp6();
35 end
36 sock:setpeername(IP.addr, 9);
37 local localaddr = sock:getsockname() or anysource[IP.proto];
38 sock:close();
39 if not sources[localaddr] then
40 sources[localaddr] = true;
41 t_insert(sources, new_ip(localaddr, IP.proto));
42 end
43 end
44 return sources;
45 end
46 local has_ipv4, has_ipv6; 28 local has_ipv4, has_ipv6;
47 29
48 local dns_timeout = module:get_option_number("dns_timeout", 15); 30 local dns_timeout = module:get_option_number("dns_timeout", 15);
49 dns.settimeout(dns_timeout); 31 dns.settimeout(dns_timeout);
50 32
194 end 176 end
195 end 177 end
196 178
197 if have_other_result then 179 if have_other_result then
198 if #IPs > 0 then 180 if #IPs > 0 then
199 rfc6724_dest(host_session.ip_hosts, get_sources(host_session.ip_hosts)); 181 rfc6724_dest(host_session.ip_hosts, sources);
200 for i = 1, #IPs do 182 for i = 1, #IPs do
201 IPs[i] = {ip = IPs[i], port = connect_port}; 183 IPs[i] = {ip = IPs[i], port = connect_port};
202 end 184 end
203 host_session.ip_choice = 0; 185 host_session.ip_choice = 0;
204 s2sout.try_next_ip(host_session); 186 s2sout.try_next_ip(host_session);
230 end 212 end
231 end 213 end
232 214
233 if have_other_result then 215 if have_other_result then
234 if #IPs > 0 then 216 if #IPs > 0 then
235 rfc6724_dest(host_session.ip_hosts, get_sources(host_session.ip_hosts)); 217 rfc6724_dest(host_session.ip_hosts, sources);
236 for i = 1, #IPs do 218 for i = 1, #IPs do
237 IPs[i] = {ip = IPs[i], port = connect_port}; 219 IPs[i] = {ip = IPs[i], port = connect_port};
238 end 220 end
239 host_session.ip_choice = 0; 221 host_session.ip_choice = 0;
240 s2sout.try_next_ip(host_session); 222 s2sout.try_next_ip(host_session);
318 if not s2s_sources then 300 if not s2s_sources then
319 module:log("warn", "s2s not listening on any ports, outgoing connections may fail"); 301 module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
320 return; 302 return;
321 end 303 end
322 for source, _ in pairs(s2s_sources) do 304 for source, _ in pairs(s2s_sources) do
323 if source:find(":") then 305 if source == "*" or source == "0.0.0.0" then
306 for _, addr in ipairs(local_addresses("ipv4", true)) do
307 sources[#sources + 1] = new_ip(addr, "IPv4");
308 end
309 elseif source == "::" then
310 for _, addr in ipairs(local_addresses("ipv6", true)) do
311 sources[#sources + 1] = new_ip(addr, "IPv6");
312 end
313 else
314 sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
315 end
316 end
317 for i = 1,#sources do
318 if sources[i].proto == "IPv6" then
324 has_ipv6 = true; 319 has_ipv6 = true;
325 else 320 elseif sources[i].proto == "IPv4" then
326 has_ipv4 = true; 321 has_ipv4 = true;
327 end 322 end
328 end 323 end
324 if not (has_ipv4 or has_ipv6) then
325 module:log("warn", "No local IPv4 or IPv6 addresses detected, outgoing connections may fail");
326 end
329 end); 327 end);
330 328
331 return s2sout; 329 return s2sout;