summaryrefslogtreecommitdiff
path: root/src/tcp_connecter.cpp
diff options
context:
space:
mode:
authorSergey Matveychuk <sem33@yandex-team.ru>2012-02-16 10:03:02 +0900
committerMartin Sustrik <sustrik@250bpm.com>2012-02-16 10:03:02 +0900
commitf9f25ccc2b4a365edc00d922a2aa056cca7fe861 (patch)
tree5fb03cec4eab12a7cdc8623f38aaba563320b034 /src/tcp_connecter.cpp
parentc71e11bed498926efdcdf022bc560a350e45251c (diff)
Allow to set up a source address for outgoing connections in zmq_connect()
Signed-off-by: Sergey Matveychuk <sem33@yandex-team.ru> Signed-off-by: Martin Sustrik <sustik@250bpm.com>
Diffstat (limited to 'src/tcp_connecter.cpp')
-rw-r--r--src/tcp_connecter.cpp24
1 files changed, 22 insertions, 2 deletions
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 ());