Changeset

7584:98ee3ed105cf

net.server_epoll: Collect and expose local and remote address and ports
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 15:55:50 +0200
parents 7583:0498daffa6f0
children 7585:b64218c816de
files net/server_epoll.lua
diffstat 1 files changed, 25 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/net/server_epoll.lua	Thu Aug 18 15:50:18 2016 +0200
+++ b/net/server_epoll.lua	Thu Aug 18 15:55:50 2016 +0200
@@ -114,11 +114,11 @@
 local interface_mt = { __index = interface };
 
 function interface_mt:__tostring()
-	if self.peer then
+	if self.peername then
 		if self.conn then
 			return ("%d %s [%s]:%d"):format(self:getfd(), tostring(self.conn), self.peer[1], self.peer[2]);
 		else
-			return ("%d [%s]:%d"):format(self:getfd(), self.peer[1], self.peer[2]);
+			return ("%d [%s]:%d"):format(self:getfd(), self.peername, self.peerport);
 		end
 	end
 	return tostring(self:getfd());
@@ -147,7 +147,23 @@
 end
 
 function interface:ip()
-	return self.peer[1];
+	return self.peername or self.sockname;
+end
+
+function interface:port()
+	return self.sockport or self.peerport;
+end
+
+function interface:clientport()
+	return self.sockport;
+end
+
+function interface:serverport()
+	if self.sockport then
+		return self.sockport;
+	elseif self.server then
+		self.server:port();
+	end
 end
 
 function interface:socket()
@@ -396,7 +412,10 @@
 		tls = tls;
 	}, interface_mt);
 	if client.getpeername then
-		conn.peer = {client:getpeername()}
+		conn.peername, conn.peerport = client:getpeername();
+	end
+	if client.getsockname then
+		conn.sockname, conn.sockport = client:getsockname();
 	end
 
 	fds[conn:getfd()] = conn;
@@ -461,7 +480,8 @@
 		_pattern = pattern;
 		onreadable = interface.onacceptable;
 		tls = tls;
-		peer = { addr, port };
+		sockname = addr;
+		sockport = port;
 	}, interface_mt);
 	server:setflags(true, false);
 	fds[server:getfd()] = server;