0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-02 03:08:04 +08:00

Merge pull request #2111 from bluca/ipv6_multi_endpoint

Problem: zmq_connect with IPv6 "source:port;dest:port" format is broken
This commit is contained in:
Constantin Rack 2016-09-17 21:34:46 +02:00 committed by GitHub
commit 50e277bd8f
2 changed files with 216 additions and 3 deletions

View File

@ -843,7 +843,7 @@ int zmq::socket_base_t::connect (const char *addr_)
while (isalnum (*check)
|| isxdigit (*check)
|| *check == '.' || *check == '-' || *check == ':' || *check == '%'
|| *check == ';' || *check == ']' || *check == '_'
|| *check == ';' || *check == '[' || *check == ']' || *check == '_'
) {
check++;
}

View File

@ -29,9 +29,8 @@
#include "testutil.hpp"
int main (void)
void test_single_connect_ipv4 (void)
{
setup_test_environment();
void *ctx = zmq_ctx_new ();
assert (ctx);
@ -47,6 +46,12 @@ int main (void)
bounce (sb, sc);
rc = zmq_disconnect (sc, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_unbind (sb, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_close (sc);
assert (rc == 0);
@ -55,6 +60,214 @@ int main (void)
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
void test_multi_connect_ipv4 (void)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb0 = zmq_socket (ctx, ZMQ_REP);
assert (sb0);
int rc = zmq_bind (sb0, "tcp://127.0.0.1:5560");
assert (rc == 0);
void *sb1 = zmq_socket (ctx, ZMQ_REP);
assert (sb1);
rc = zmq_bind (sb1, "tcp://127.0.0.1:5561");
assert (rc == 0);
void *sb2 = zmq_socket (ctx, ZMQ_REP);
assert (sb2);
rc = zmq_bind (sb2, "tcp://127.0.0.1:5562");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_REQ);
assert (sc);
rc = zmq_connect (sc, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_connect (sc, "tcp://127.0.0.1:5561");
assert (rc == 0);
rc = zmq_connect (sc, "tcp://127.0.0.1:5564;127.0.0.1:5562");
assert (rc == 0);
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
rc = zmq_disconnect (sc, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_disconnect (sc, "tcp://127.0.0.1:5564;127.0.0.1:5562");
assert (rc == 0);
rc = zmq_disconnect (sc, "tcp://127.0.0.1:5561");
assert (rc == 0);
rc = zmq_unbind (sb0, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_unbind (sb1, "tcp://127.0.0.1:5561");
assert (rc == 0);
rc = zmq_unbind (sb2, "tcp://127.0.0.1:5562");
assert (rc == 0);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb0);
assert (rc == 0);
rc = zmq_close (sb1);
assert (rc == 0);
rc = zmq_close (sb2);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
void test_single_connect_ipv6 (void)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
if (!is_ipv6_available ()) {
zmq_ctx_term (ctx);
return;
}
void *sb = zmq_socket (ctx, ZMQ_REP);
assert (sb);
int ipv6 = 1;
int rc = zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_bind (sb, "tcp://[::1]:5560");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_REQ);
assert (sc);
rc = zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_connect (sc, "tcp://[::1]:5560");
assert (rc == 0);
bounce (sb, sc);
rc = zmq_disconnect (sc, "tcp://[::1]:5560");
assert (rc == 0);
rc = zmq_unbind (sb, "tcp://[::1]:5560");
assert (rc == 0);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
void test_multi_connect_ipv6 (void)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
if (!is_ipv6_available ()) {
zmq_ctx_term (ctx);
return;
}
void *sb0 = zmq_socket (ctx, ZMQ_REP);
assert (sb0);
int ipv6 = 1;
int rc = zmq_setsockopt (sb0, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_bind (sb0, "tcp://[::1]:5560");
assert (rc == 0);
void *sb1 = zmq_socket (ctx, ZMQ_REP);
assert (sb1);
rc = zmq_setsockopt (sb1, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_bind (sb1, "tcp://[::1]:5561");
assert (rc == 0);
void *sb2 = zmq_socket (ctx, ZMQ_REP);
assert (sb2);
rc = zmq_setsockopt (sb2, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_bind (sb2, "tcp://[::1]:5562");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_REQ);
assert (sc);
rc = zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int));
assert (rc == 0);
rc = zmq_connect (sc, "tcp://[::1]:5560");
assert (rc == 0);
rc = zmq_connect (sc, "tcp://[::1]:5561");
assert (rc == 0);
rc = zmq_connect (sc, "tcp://[::1]:5564;[::1]:5562");
assert (rc == 0);
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
rc = zmq_disconnect (sc, "tcp://[::1]:5560");
assert (rc == 0);
rc = zmq_disconnect (sc, "tcp://[::1]:5564;[::1]:5562");
assert (rc == 0);
rc = zmq_disconnect (sc, "tcp://[::1]:5561");
assert (rc == 0);
rc = zmq_unbind (sb0, "tcp://[::1]:5560");
assert (rc == 0);
rc = zmq_unbind (sb1, "tcp://[::1]:5561");
assert (rc == 0);
rc = zmq_unbind (sb2, "tcp://[::1]:5562");
assert (rc == 0);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb0);
assert (rc == 0);
rc = zmq_close (sb1);
assert (rc == 0);
rc = zmq_close (sb2);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
int main (void)
{
setup_test_environment ();
test_single_connect_ipv4 ();
test_multi_connect_ipv4 ();
test_single_connect_ipv6 ();
test_multi_connect_ipv6 ();
return 0 ;
}