From 6d5ef2f7f9dafa7cd299c39a0e9f2873cb6f1289 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 15 Jun 2011 23:44:18 +0200 Subject: connlistener, server_select, prosody: Add support for binding to multiple addresses --- net/connlisteners.lua | 16 +++++++++++++--- net/server_select.lua | 24 ++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) (limited to 'net') diff --git a/net/connlisteners.lua b/net/connlisteners.lua index 7da25c62..6a227c9d 100644 --- a/net/connlisteners.lua +++ b/net/connlisteners.lua @@ -12,6 +12,8 @@ local listeners_dir = (CFG_SOURCEDIR or ".").."/net/"; local server = require "net.server"; local log = require "util.logger".init("connlisteners"); local tostring = tostring; +local type = type +local ipairs = ipairs local dofile, xpcall, error = dofile, xpcall, error @@ -55,7 +57,8 @@ function start(name, udata) error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0); end - local interface = (udata and udata.interface) or h.default_interface or "*"; + local interfaces = (udata and udata.interface) or h.default_interface or "*"; + if type(interfaces) == "string" then interfaces = {interfaces}; end local port = (udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0); local mode = (udata and udata.mode) or h.default_mode or 1; local ssl = (udata and udata.ssl) or nil; @@ -64,8 +67,15 @@ function start(name, udata) if autossl and not ssl then return nil, "no ssl context"; end - - return server.addserver(interface, port, h, mode, autossl and ssl or nil); + + ok, err = true, {}; + for _, interface in ipairs(interfaces) do + local handler + handler, err[interface] = server.addserver(interface, port, h, mode, autossl and ssl or nil); + ok = ok and handler; + end + + return ok, err; end return _M; diff --git a/net/server_select.lua b/net/server_select.lua index 13a910f8..4a22d2ed 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -701,19 +701,19 @@ addserver = function( addr, port, listeners, pattern, sslctx ) -- this function end if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then err = "invalid port" - elseif _server[ port ] then - err = "listeners on port '" .. port .. "' already exist" + elseif _server[ addr..":"..port ] then + err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist" elseif sslctx and not luasec then err = "luasec not found" end if err then - out_error( "server.lua, port ", port, ": ", err ) + out_error( "server.lua, [", addr, "]:", port, ": ", err ) return nil, err end addr = addr or "*" local server, err = socket_bind( addr, port ) if err then - out_error( "server.lua, port ", port, ": ", err ) + out_error( "server.lua, [", addr, "]:", port, ": ", err ) return nil, err end local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket @@ -723,23 +723,23 @@ addserver = function( addr, port, listeners, pattern, sslctx ) -- this function end server:settimeout( 0 ) _readlistlen = addsocket(_readlist, server, _readlistlen) - _server[ port ] = handler + _server[ addr..":"..port ] = handler _socketlist[ server ] = handler - out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '", addr, ":", port, "'" ) + out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '[", addr, "]:", port, "'" ) return handler end -getserver = function ( port ) - return _server[ port ]; +getserver = function ( addr, port ) + return _server[ addr..":"..port ]; end -removeserver = function( port ) - local handler = _server[ port ] +removeserver = function( addr, port ) + local handler = _server[ addr..":"..port ] if not handler then - return nil, "no server found on port '" .. tostring( port ) .. "'" + return nil, "no server found on '[" .. addr .. "]:" .. tostring( port ) .. "'" end handler:close( ) - _server[ port ] = nil + _server[ addr..":"..port ] = nil return true end -- cgit v1.2.3