mirror of
https://github.com/zeromq/libzmq.git
synced 2025-10-15 23:20:09 +02:00
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:
@@ -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;
|
||||
|
Reference in New Issue
Block a user