aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-09-01 01:55:55 +0200
committerKim Alvefur <zash@zash.se>2019-09-01 01:55:55 +0200
commit9afa1ac496bac9d9eaaf687edbdde3f0c7fb66e1 (patch)
tree34d632de89573f0c4aebf7c9282e50aa9105e274 /net
parent66fdfe3c005e8e2fda23efa918e85d890bc632c4 (diff)
downloadprosody-9afa1ac496bac9d9eaaf687edbdde3f0c7fb66e1.tar.gz
prosody-9afa1ac496bac9d9eaaf687edbdde3f0c7fb66e1.zip
net.server: Accept and save an 'extra' field for client connections
This lets code attach some extra data to be attached to client connections.
Diffstat (limited to 'net')
-rw-r--r--net/server_epoll.lua11
-rw-r--r--net/server_event.lua11
-rw-r--r--net/server_select.lua12
3 files changed, 19 insertions, 15 deletions
diff --git a/net/server_epoll.lua b/net/server_epoll.lua
index ef021851..96e7b201 100644
--- a/net/server_epoll.lua
+++ b/net/server_epoll.lua
@@ -560,7 +560,7 @@ function interface:tlshandskake()
end
end
-local function wrapsocket(client, server, read_size, listeners, tls_ctx) -- luasocket object -> interface object
+local function wrapsocket(client, server, read_size, listeners, tls_ctx, extra) -- luasocket object -> interface object
client:settimeout(0);
local conn = setmetatable({
conn = client;
@@ -572,6 +572,7 @@ local function wrapsocket(client, server, read_size, listeners, tls_ctx) -- luas
tls_ctx = tls_ctx or (server and server.tls_ctx);
tls_direct = server and server.tls_direct;
log = logger.init(("conn%s"):format(new_id()));
+ extra = extra;
}, interface_mt);
conn:updatenames();
@@ -701,8 +702,8 @@ local function addserver(addr, port, listeners, read_size, tls_ctx)
end
-- COMPAT
-local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx)
- local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx);
+local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx, extra)
+ local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra);
if not client.peername then
client.peername, client.peerport = addr, port;
end
@@ -715,7 +716,7 @@ local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx)
end
-- New outgoing TCP connection
-local function addclient(addr, port, listeners, read_size, tls_ctx, typ)
+local function addclient(addr, port, listeners, read_size, tls_ctx, typ, extra)
local create;
if not typ then
local n = inet_pton(addr);
@@ -738,7 +739,7 @@ local function addclient(addr, port, listeners, read_size, tls_ctx, typ)
if not ok then return ok, err; end
local ok, err = conn:setpeername(addr, port);
if not ok and err ~= "timeout" then return ok, err; end
- local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx)
+ local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra)
local ok, err = client:init();
if not ok then return ok, err; end
if tls_ctx then
diff --git a/net/server_event.lua b/net/server_event.lua
index fde79d86..950a7a5c 100644
--- a/net/server_event.lua
+++ b/net/server_event.lua
@@ -479,7 +479,7 @@ end
-- End of client interface methods
-local function handleclient( client, ip, port, server, pattern, listener, sslctx ) -- creates an client interface
+local function handleclient( client, ip, port, server, pattern, listener, sslctx, extra ) -- creates an client interface
--vdebug("creating client interfacce...")
local interface = {
type = "client";
@@ -515,6 +515,7 @@ local function handleclient( client, ip, port, server, pattern, listener, sslctx
_serverport = (server and server:port() or nil),
_sslctx = sslctx; -- parameters
_usingssl = false; -- client is using ssl;
+ extra = extra;
}
if not has_luasec then interface.starttls = false; end
interface.id = tostring(interface):match("%x+$");
@@ -749,14 +750,14 @@ local function addserver( addr, port, listener, pattern, sslctx ) -- TODO: chec
});
end
-local function wrapclient( client, ip, port, listeners, pattern, sslctx )
- local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx )
+local function wrapclient( client, ip, port, listeners, pattern, sslctx, extra )
+ local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx, extra )
interface:_start_connection(sslctx)
return interface, client
--function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface
end
-local function addclient( addr, serverport, listener, pattern, sslctx, typ )
+local function addclient( addr, serverport, listener, pattern, sslctx, typ, extra )
if sslctx and not has_luasec then
debug "need luasec, but not available"
return nil, "luasec not found"
@@ -783,7 +784,7 @@ local function addclient( addr, serverport, listener, pattern, sslctx, typ )
local res, err = client:setpeername( addr, serverport ) -- connect
if res or ( err == "timeout" ) then
local ip, port = client:getsockname( )
- local interface = wrapclient( client, ip, serverport, listener, pattern, sslctx )
+ local interface = wrapclient( client, ip, serverport, listener, pattern, sslctx, extra )
debug( "new connection id:", interface.id )
return interface, err
else
diff --git a/net/server_select.lua b/net/server_select.lua
index e14c126e..de183331 100644
--- a/net/server_select.lua
+++ b/net/server_select.lua
@@ -266,7 +266,7 @@ wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, ssldi
return handler
end
-wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, ssldirect ) -- this function wraps a client to a handler object
+wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, ssldirect, extra ) -- this function wraps a client to a handler object
if socket:getfd() >= _maxfd then
out_error("server.lua: Disallowed FD number: "..socket:getfd()) -- PROTIP: Switch to libevent
@@ -316,6 +316,8 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
local handler = bufferqueue -- saves a table ^_^
+ handler.extra = extra
+
handler.dispatch = function( )
return dispatch
end
@@ -1005,8 +1007,8 @@ end
--// EXPERIMENTAL //--
-local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
- local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, sslctx)
+local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, extra )
+ local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, sslctx, extra)
if not handler then return nil, err end
_socketlist[ socket ] = handler
if not sslctx then
@@ -1025,7 +1027,7 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx
return handler, socket
end
-local addclient = function( address, port, listeners, pattern, sslctx, typ )
+local addclient = function( address, port, listeners, pattern, sslctx, typ, extra )
local err
if type( listeners ) ~= "table" then
err = "invalid listener table"
@@ -1062,7 +1064,7 @@ local addclient = function( address, port, listeners, pattern, sslctx, typ )
client:settimeout( 0 )
local ok, err = client:setpeername( address, port )
if ok or err == "timeout" or err == "Operation already in progress" then
- return wrapclient( client, address, port, listeners, pattern, sslctx )
+ return wrapclient( client, address, port, listeners, pattern, sslctx, extra )
else
return nil, err
end