mod_s2s: Collect s2s sources from portmanager and get local address if necessary
authorFlorian Zeitz <florob@babelmonkeys.de>
Sun, 11 Mar 2012 20:16:57 +0100
changeset 4602 ba898af15de5
parent 4601 4007b9f9aa5f
child 4603 6900c9484834
child 4621 0445a543ae57
mod_s2s: Collect s2s sources from portmanager and get local address if necessary
plugins/s2s/s2sout.lib.lua
--- a/plugins/s2s/s2sout.lib.lua	Sun Mar 11 20:15:42 2012 +0100
+++ b/plugins/s2s/s2sout.lib.lua	Sun Mar 11 20:16:57 2012 +0100
@@ -8,6 +8,7 @@
 
 --- Module containing all the logic for connecting to a remote server
 
+local portmanager = require "core.portmanager";
 local wrapclient = require "net.server".wrapclient;
 local initialize_filters = require "util.filters".initialize;
 local idna_to_ascii = require "util.encodings".idna.to_ascii;
@@ -21,7 +22,7 @@
 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 
-local cfg_sources = config.get("*", "core", "s2s_interfaces") or socket.local_addresses and socket.local_addresses() or { "*" };
+local sources = {};
 
 local max_dns_depth = module:get_option_number("dns_max_depth", 3);
 
@@ -158,7 +159,6 @@
 end
 
 function s2sout.try_connect(host_session, connect_host, connect_port, err)
-	local sources;
 	host_session.connecting = true;
 
 	if not err then
@@ -167,17 +167,6 @@
 		local handle4, handle6;
 		local has_other = false;
 
-		if not sources then
-			sources = {};
-			for i, source in ipairs(cfg_sources) do
-				if source == "*" then
-					sources[i] = new_ip("0.0.0.0", "IPv4");
-				else
-					sources[i] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
-				end
-			end
-		end
-
 		handle4 = adns.lookup(function (reply, err)
 			handle4 = nil;
 
@@ -323,4 +312,32 @@
 	return true;
 end
 
+module:hook_global("service-added", function (event)
+	if event.name ~= "s2s" then return end
+
+	local s2s_sources = portmanager.get_active_services():get("s2s");
+
+	for source, _ in pairs(s2s_sources) do
+		if source == "*" or source == "0.0.0.0" then
+			if not socket.local_addresses then
+				sources[#sources + 1] = new_ip("0.0.0.0", "IPv4");
+			else
+				for _, addr in ipairs(socket.local_addresses("ipv4", true)) do
+					sources[#sources + 1] = new_ip(addr, "IPv4");
+				end
+			end
+		elseif source == "::" then
+			if not socket.local_addresses then
+				sources[#sources + 1] = new_ip("::", "IPv4");
+			else
+				for _, addr in ipairs(socket.local_addresses("ipv6", true)) do
+					sources[#sources + 1] = new_ip(addr, "IPv6");
+				end
+			end
+		else
+			sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
+		end
+	end
+end);
+
 return s2sout;