From 2e5b13ee9817a46e00ad0dbe1b36f67ae0ef3476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 10 Mar 2017 20:25:28 +0100 Subject: [PATCH] Add EventDriverSockets.getRemoteAddress. --- source/eventcore/driver.d | 3 +++ source/eventcore/drivers/posix/sockets.d | 12 ++++++++++-- source/eventcore/drivers/winapi/sockets.d | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index ac662ea..d890bd9 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -159,6 +159,9 @@ interface EventDriverSockets { /// Retrieves the bind address of a socket. bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst); + /// Retrieves the address of the connected peer. + bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst); + /// Sets the `TCP_NODELAY` option on a socket void setTCPNoDelay(StreamSocketFD socket, bool enable); diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index ff47419..33d5f62 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -213,7 +213,16 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return m_loop.m_fds[sock].streamSocket.state; } - bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst) + final override bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst) + { + socklen_t addr_len = dst.nameLen; + if (() @trusted { return getpeername(cast(sock_t)sock, dst.name, &addr_len); } () != 0) + return false; + dst.cap(addr_len); + return true; + } + + final override bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst) { socklen_t addr_len = dst.nameLen; if (() @trusted { return getsockname(cast(sock_t)sock, dst.name, &addr_len); } () != 0) @@ -222,7 +231,6 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return true; } - final override void setTCPNoDelay(StreamSocketFD socket, bool enable) { int opt = enable; diff --git a/source/eventcore/drivers/winapi/sockets.d b/source/eventcore/drivers/winapi/sockets.d index 555e5ef..8e228d4 100644 --- a/source/eventcore/drivers/winapi/sockets.d +++ b/source/eventcore/drivers/winapi/sockets.d @@ -40,6 +40,11 @@ final class WinAPIEventDriverSockets : EventDriverSockets { assert(false, "TODO!"); } + override bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst) + { + assert(false, "TODO!"); + } + override void setTCPNoDelay(StreamSocketFD socket, bool enable) { assert(false, "TODO!");