Comparison

plugins/mod_s2s/s2sout.lib.lua @ 6685:3f05b255937f

Merge 0.9->0.10
author Kim Alvefur <zash@zash.se>
date Wed, 13 May 2015 22:31:59 +0200
parent 6632:855085439f7f
parent 6682:63f5870f9afe
child 6917:65344c3bae7a
comparison
equal deleted inserted replaced
6678:343ca80ceb36 6685:3f05b255937f
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;
22 21
23 local s2s_destroy_session = require "core.s2smanager".destroy_session; 22 local s2s_destroy_session = require "core.s2smanager".destroy_session;
24 23
25 local log = module._log; 24 local log = module._log;
26 25
27 local sources = {}; 26 local anysource = { IPv4 = "0.0.0.0", IPv6 = "::" };
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
28 local has_ipv4, has_ipv6; 46 local has_ipv4, has_ipv6;
29 47
30 local dns_timeout = module:get_option_number("dns_timeout", 15); 48 local dns_timeout = module:get_option_number("dns_timeout", 15);
31 dns.settimeout(dns_timeout); 49 dns.settimeout(dns_timeout);
32 local max_dns_depth = module:get_option_number("dns_max_depth", 3);
33 50
34 local s2sout = {}; 51 local s2sout = {};
35 52
36 local s2s_listener; 53 local s2s_listener;
37 54
176 end 193 end
177 end 194 end
178 195
179 if have_other_result then 196 if have_other_result then
180 if #IPs > 0 then 197 if #IPs > 0 then
181 rfc6724_dest(host_session.ip_hosts, sources); 198 rfc6724_dest(host_session.ip_hosts, get_sources(host_session.ip_hosts));
182 for i = 1, #IPs do 199 for i = 1, #IPs do
183 IPs[i] = {ip = IPs[i], port = connect_port}; 200 IPs[i] = {ip = IPs[i], port = connect_port};
184 end 201 end
185 host_session.ip_choice = 0; 202 host_session.ip_choice = 0;
186 s2sout.try_next_ip(host_session); 203 s2sout.try_next_ip(host_session);
212 end 229 end
213 end 230 end
214 231
215 if have_other_result then 232 if have_other_result then
216 if #IPs > 0 then 233 if #IPs > 0 then
217 rfc6724_dest(host_session.ip_hosts, sources); 234 rfc6724_dest(host_session.ip_hosts, get_sources(host_session.ip_hosts));
218 for i = 1, #IPs do 235 for i = 1, #IPs do
219 IPs[i] = {ip = IPs[i], port = connect_port}; 236 IPs[i] = {ip = IPs[i], port = connect_port};
220 end 237 end
221 host_session.ip_choice = 0; 238 host_session.ip_choice = 0;
222 s2sout.try_next_ip(host_session); 239 s2sout.try_next_ip(host_session);
300 if not s2s_sources then 317 if not s2s_sources then
301 module:log("warn", "s2s not listening on any ports, outgoing connections may fail"); 318 module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
302 return; 319 return;
303 end 320 end
304 for source, _ in pairs(s2s_sources) do 321 for source, _ in pairs(s2s_sources) do
305 if source == "*" or source == "0.0.0.0" then 322 if source:find(":") then
306 for _, addr in ipairs(local_addresses("ipv4", true)) do 323 has_ipv6 = true;
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 324 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
319 has_ipv6 = true;
320 elseif sources[i].proto == "IPv4" then
321 has_ipv4 = true; 325 has_ipv4 = true;
322 end 326 end
323 end 327 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
327 end); 328 end);
328 329
329 return s2sout; 330 return s2sout;