From d181a6730f65c5f77ec82b32e6fd4007f2c1194f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 2 Nov 2016 20:22:54 +0100 Subject: [PATCH] Add bind address support for outgoing TCP connections. --- source/eventcore/driver.d | 2 +- source/eventcore/drivers/libasync.d | 2 +- source/eventcore/drivers/posix.d | 17 +++++++++++------ source/eventcore/drivers/winapi.d | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index da7db14..9a2d65f 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -78,7 +78,7 @@ interface EventDriverCore { interface EventDriverSockets { @safe: /*@nogc:*/ nothrow: - StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect); + StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect); StreamListenSocketFD listenStream(scope Address bind_address, AcceptCallback on_accept); void waitForConnections(StreamListenSocketFD sock, AcceptCallback on_accept); ConnectionState getConnectionState(StreamSocketFD sock); diff --git a/source/eventcore/drivers/libasync.d b/source/eventcore/drivers/libasync.d index f161638..f591528 100644 --- a/source/eventcore/drivers/libasync.d +++ b/source/eventcore/drivers/libasync.d @@ -78,7 +78,7 @@ final class LibasyncEventDriverCore : EventDriverCore { final class LibasyncEventDriverSockets : EventDriverSockets { @safe: /*@nogc:*/ nothrow: - override StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect) + override StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect) { assert(false, "TODO!"); } diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index eb136cb..5ab5368 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -200,7 +200,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets this(Loop loop) { m_loop = loop; } - final override StreamSocketFD connectStream(scope Address address, ConnectCallback on_connect) + final override StreamSocketFD connectStream(scope Address address, scope Address bind_address, ConnectCallback on_connect) { auto sock = cast(StreamSocketFD)createSocket(address.addressFamily, SOCK_STREAM); if (sock == -1) return StreamSocketFD.invalid; @@ -208,12 +208,17 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets void invalidateSocket() @nogc @trusted nothrow { closeSocket(sock); sock = StreamSocketFD.invalid; } int bret; - () @trusted { // scope - scope bind_addr = new UnknownAddress; - bind_addr.name.sa_family = cast(ushort)address.addressFamily; - bind_addr.name.sa_data[] = 0; - bret = bind(sock, bind_addr.name, bind_addr.nameLen); + () @trusted { // scope + bind() + if (bind_address !is null) { + bret = bind(sock, bind_address.name, bind_address.nameLen); + } else { + scope bind_addr = new UnknownAddress; + bind_addr.name.sa_family = cast(ushort)address.addressFamily; + bind_addr.name.sa_data[] = 0; + bret = bind(sock, bind_addr.name, bind_addr.nameLen); + } } (); + if (bret != 0) { invalidateSocket(); on_connect(sock, ConnectStatus.bindFailure); diff --git a/source/eventcore/drivers/winapi.d b/source/eventcore/drivers/winapi.d index 209c7ec..3b71698 100644 --- a/source/eventcore/drivers/winapi.d +++ b/source/eventcore/drivers/winapi.d @@ -85,7 +85,7 @@ final class WinAPIEventDriverCore : EventDriverCore { final class WinAPIEventDriverSockets : EventDriverSockets { @safe: /*@nogc:*/ nothrow: - override StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect) + override StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect) { assert(false, "TODO!"); }