Problem: 2 connects with same sourceip:port to different destip:port fail

Solution: during a connect with a TCP endpoint if a source address is
passed set the SO_REUSEADDR flag on the socket before the bind system
call.
Add unit test to cover this case for both IPv4 and IPv6.
This commit is contained in:
Luca Boccassi
2016-10-23 21:30:51 +01:00
parent 669ff41d52
commit 25bf30bebe
2 changed files with 164 additions and 0 deletions

View File

@@ -314,6 +314,18 @@ int zmq::tcp_connecter_t::open ()
// Set a source address for conversations
if (tcp_addr->has_src_addr ()) {
// Allow reusing of the address, to connect to different servers
// using the same source port on the client.
int flag = 1;
#ifdef ZMQ_HAVE_WINDOWS
rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (const char*) &flag,
sizeof (int));
wsa_assert (rc != SOCKET_ERROR);
#else
rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
errno_assert (rc == 0);
#endif
rc = ::bind (s, tcp_addr->src_addr (), tcp_addr->src_addrlen ());
if (rc == -1)
return -1;