Hi.
I've moved from 3.0 to 3.1, so I've got a chance to test my first patch
more. I've found a stupid bug in it and fixed resolve_interface()
function when it ignored an interface name.
Here is my second patch. Please, comment it. May be you dislike the last
argument in zmq::tcp_address_t::resolve with a default value?
>From 0614ca7d73a84e43785295b3cf630fc5173f7650 Mon Sep 17 00:00:00 2001
From: Sergey Matveychuk <[email protected]>
Date: Fri, 23 Dec 2011 08:35:34 +0400
Subject: [PATCH] Allow to set up a source address for outgoing connections in
zmq_connect() Fix resolve_interface() bug when interface
name is ignored. Signed-off-by: Sergey Matveychuk
<[email protected]>
---
doc/zmq_tcp.txt | 7 ++++---
src/tcp_address.cpp | 40 ++++++++++++++++++++--------------------
src/tcp_address.hpp | 2 +-
src/tcp_connecter.cpp | 18 +++++++++++++++++-
src/tcp_connecter.hpp | 3 +++
5 files changed, 45 insertions(+), 25 deletions(-)
diff --git a/doc/zmq_tcp.txt b/doc/zmq_tcp.txt
index f5499d3..2c1c412 100644
--- a/doc/zmq_tcp.txt
+++ b/doc/zmq_tcp.txt
@@ -17,9 +17,10 @@ your first choice.
ADDRESSING
----------
A 0MQ address string consists of two parts as follows:
-'transport'`://`'endpoint'. The 'transport' part specifies the underlying
-transport protocol to use, and for the TCP transport shall be set to `tcp`.
-The meaning of the 'endpoint' part for the TCP transport is defined below.
+'transport'`://`['source address';]'endpoint'. The 'transport' part specifies
+the underlying transport protocol to use, and for the TCP transport shall be
+set to `tcp`. 'source address' is optional. The meaning of the 'endpoint' part
+for the TCP transport is defined below.
Assigning a local address to a socket
diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp
index de6e0ad..f1ae550 100644
--- a/src/tcp_address.cpp
+++ b/src/tcp_address.cpp
@@ -250,11 +250,8 @@ int zmq::tcp_address_t::resolve_interface (char const *interface_,
int rc = resolve_nic_name (interface_, ipv4only_);
if (rc != 0 && errno != ENODEV)
return rc;
- if (rc == 0) {
- zmq_assert (out_addrlen <= (socklen_t) sizeof (address));
- memcpy (&address, out_addr, out_addrlen);
+ if (rc == 0)
return 0;
- }
// There's no such interface name. Assume literal address.
#if defined ZMQ_HAVE_OPENVMS && defined __ia64
@@ -369,31 +366,34 @@ zmq::tcp_address_t::~tcp_address_t ()
{
}
-int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
+int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_, bool ignore_port_)
{
// Find the ':' at end that separates address from the port number.
const char *delimiter = strrchr (name_, ':');
- if (!delimiter) {
- errno = EINVAL;
- return -1;
- }
-
- // Separate the address/port.
- std::string addr_str (name_, delimiter - name_);
- std::string port_str (delimiter + 1);
+ std::string addr_str;
+ uint16_t port = 0;
+
+ if (!ignore_port_) {
+ if (!delimiter) {
+ errno = EINVAL;
+ return -1;
+ }
+ // Separate the address/port.
+ addr_str = std::string (name_, delimiter - name_);
+ // Parse the port number (0 is not a valid port).
+ port = (uint16_t) atoi (delimiter+1);
+ if (port == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ } else
+ addr_str = name_;
// Remove square brackets around the address, if any.
if (!addr_str.empty () && addr_str [0] == '[' &&
addr_str [addr_str.size () - 1] == ']')
addr_str = addr_str.substr (1, addr_str.size () - 2);
- // Parse the port number (0 is not a valid port).
- uint16_t port = (uint16_t) atoi (port_str.c_str());
- if (port == 0) {
- errno = EINVAL;
- return -1;
- }
-
// Resolve the IP address.
int rc;
if (local_)
diff --git a/src/tcp_address.hpp b/src/tcp_address.hpp
index d4768c7..5e97b50 100644
--- a/src/tcp_address.hpp
+++ b/src/tcp_address.hpp
@@ -45,7 +45,7 @@ namespace zmq
// strcuture. If 'local' is true, names are resolved as local interface
// names. If it is false, names are resolved as remote hostnames.
// If 'ipv4only' is true, the name will never resolve to IPv6 address.
- int resolve (const char* name_, bool local_, bool ipv4only_);
+ int resolve (const char* name_, bool local_, bool ipv4only_, bool ignore_port_=false);
#if defined ZMQ_HAVE_WINDOWS
unsigned short family ();
diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp
index 042e82a..9ac10d6 100644
--- a/src/tcp_connecter.cpp
+++ b/src/tcp_connecter.cpp
@@ -178,7 +178,19 @@ int zmq::tcp_connecter_t::get_new_reconnect_ivl ()
int zmq::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_, ';');
+ int ret = 0;
+ std::string addr_str;
+
+ if (delimiter) {
+ std::string saddr_str (addr_, delimiter - addr_);
+ addr_str = delimiter + 1;
+ ret = source_address.resolve (saddr_str.c_str(), true, options.ipv4only ? true : false, true);
+ } else
+ addr_str = addr_;
+
+ return ret || address.resolve (addr_str.c_str(), false, options.ipv4only ? true : false);
}
int zmq::tcp_connecter_t::open ()
@@ -205,6 +217,10 @@ int zmq::tcp_connecter_t::open ()
// 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 ());
diff --git a/src/tcp_connecter.hpp b/src/tcp_connecter.hpp
index fc3b9f2..9404296 100644
--- a/src/tcp_connecter.hpp
+++ b/src/tcp_connecter.hpp
@@ -87,6 +87,9 @@ namespace zmq
// Address to connect to.
tcp_address_t address;
+ // Source address
+ tcp_address_t source_address;
+
// Underlying socket.
fd_t s;
--
1.7.7.3
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev