net.server_select: Close all connections when quitting (and not just stepping), match...
[prosody.git] / net / server_select.lua
index 6191986340058c036ca5cce8d45f0aa236b060bd..87921df4badc579d21fa70b83cd92af02ac6f414 100644 (file)
@@ -51,6 +51,7 @@ local coroutine_yield = coroutine.yield
 local has_luasec, luasec = pcall ( require , "ssl" )
 local luasocket = use "socket" or require "socket"
 local luasocket_gettime = luasocket.gettime
+local getaddrinfo = luasocket.dns.getaddrinfo
 
 --// extern lib methods //--
 
@@ -910,6 +911,7 @@ loop = function(once) -- this is the main loop of the program
                socket_sleep( _sleeptime )
        until quitting;
        if once and quitting == "once" then quitting = nil; return; end
+       closeall();
        return "quitting"
 end
 
@@ -942,23 +944,37 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx
        return handler, socket
 end
 
-local addclient = function( address, port, listeners, pattern, sslctx )
+local addclient = function( address, port, listeners, pattern, sslctx, typ )
        local err
        if type( listeners ) ~= "table" then
                err = "invalid listener table"
-       elseif type ( addr ) ~= "string" then
+       elseif type ( address ) ~= "string" then
                err = "invalid address"
        elseif type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then
                err = "invalid port"
        elseif sslctx and not has_luasec then
                err = "luasec not found"
        end
+       if not typ then
+               local addrinfo, err = getaddrinfo(address)
+               if not addrinfo then return nil, err end
+               if addrinfo[1] and addrinfo[1].family == "inet6" then
+                       typ = "tcp6"
+               else
+                       typ = "tcp"
+               end
+       end
+       local create = luasocket[typ]
+       if type( create ) ~= "function"  then
+               err = "invalid socket type"
+       end
+
        if err then
                out_error( "server.lua, addclient: ", err )
                return nil, err
        end
 
-       local client, err = luasocket.tcp( )
+       local client, err = create( )
        if err then
                return nil, err
        end