From f9f25ccc2b4a365edc00d922a2aa056cca7fe861 Mon Sep 17 00:00:00 2001 From: Sergey Matveychuk Date: Thu, 16 Feb 2012 10:03:02 +0900 Subject: Allow to set up a source address for outgoing connections in zmq_connect() Signed-off-by: Sergey Matveychuk Signed-off-by: Martin Sustrik --- src/tcp_connecter.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/tcp_connecter.cpp') diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 2844a3b..36ddfa7 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -178,7 +178,23 @@ int xs::tcp_connecter_t::get_new_reconnect_ivl () int xs::tcp_connecter_t::set_address (const char *addr_) { - return address.resolve (addr_, false, options.ipv4only ? true : false); + // Find the ';'. It separates source address address from a destination. + const char *delimiter = strchr (addr_, ';'); + + std::string addr_str; + if (delimiter) { + std::string saddr_str (addr_, delimiter - addr_); + addr_str = delimiter + 1; + int rc = source_address.resolve (saddr_str.c_str(), true, + options.ipv4only ? true : false, true); + if (rc != 0) + return -1; + } + else + addr_str = addr_; + + return address.resolve (addr_str.c_str(), false, + options.ipv4only ? true : false); } int xs::tcp_connecter_t::open () @@ -202,9 +218,13 @@ int xs::tcp_connecter_t::open () if (address.family () == AF_INET6) enable_ipv4_mapping (s); - // Set the socket to non-blocking mode so that we get async connect(). + // Set the socket to non-blocking mode so that we get async connect(). unblock_socket (s); + // Set a source address for conversations. + if (source_address.family ()) + ::bind (s, source_address.addr (), source_address.addrlen ()); + // Connect to the remote peer. int rc = ::connect (s, address.addr (), address.addrlen ()); -- cgit v1.2.3