Skip to content

Commit

Permalink
Unbind socket with real endpoint when binding by wild-card * address
Browse files Browse the repository at this point in the history
  • Loading branch information
lysyloren committed Oct 29, 2014
1 parent acd55c7 commit ed6bf9f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
6 changes: 6 additions & 0 deletions doc/zmq_ipc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ NOTE: IPC pathnames have a maximum size that depends on the operating system.
On Linux, the maximum is 113 characters including the "ipc://" prefix (107
characters for the real path name).

Unbinding wild-card address from a socket
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When wild-card `*` 'endpoint' was used in _zmq_bind()_, the caller should use
real 'endpoind' obtained from the ZMQ_LAST_ENDPOINT socket option to unbind
this 'endpoint' from a socket using _zmq_unbind()_.

Connecting a socket
~~~~~~~~~~~~~~~~~~~
When connecting a 'socket' to a peer address using _zmq_connect()_ with the
Expand Down
5 changes: 5 additions & 0 deletions doc/zmq_tcp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ When using ephemeral ports, the caller should retrieve the actual assigned
port using the ZMQ_LAST_ENDPOINT socket option. See linkzmq:zmq_getsockopt[3]
for details.

Unbinding wild-card addres from a socket
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When wild-card `*` 'endpoint' was used in _zmq_bind()_, the caller should use
real 'endpoind' obtained from the ZMQ_LAST_ENDPOINT socket option to unbind
this 'endpoint' from a socket using _zmq_unbind()_.

Connecting a socket
~~~~~~~~~~~~~~~~~~~
Expand Down
27 changes: 25 additions & 2 deletions doc/zmq_unbind.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ argument.

The 'endpoint' argument is as described in linkzmq:zmq_bind[3]

Unbinding wild-card addres from a socket
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When wild-card `*` 'endpoint' (described in linkzmq:zmq_tcp[7] and
linkzmq:zmq_ipc[7]) was used in _zmq_bind()_, the caller should use
real 'endpoind' obtained from the ZMQ_LAST_ENDPOINT socket option
to unbind this 'endpoint' from a socket.

RETURN VALUE
------------
Expand All @@ -36,8 +42,8 @@ The 0MQ 'context' associated with the specified 'socket' was terminated.
The provided 'socket' was invalid.


EXAMPLE
-------
EXAMPLES
--------
.Unbind a subscriber socket from a TCP transport
----
/* Create a ZMQ_SUB socket */
Expand All @@ -51,6 +57,23 @@ rc = zmq_unbind (socket, "tcp://127.0.0.1:5555");
assert (rc == 0);
----

.Unbind wild-card `*` binded socket
----
/* Create a ZMQ_SUB socket */
void *socket = zmq_socket (context, ZMQ_SUB);
assert (socket);
/* Bind it to the system-assigned ephemeral port using a TCP transport */
rc = zmq_bind (socket, "tcp://127.0.0.1:*");
assert (rc == 0);
/* Obtain real endpoint */
const size_t buf_size = 32;
char buf[buf_size];
rc = zmq_getsockopt (socket, ZMQ_LAST_ENDPOINT, buf, (size_t *)&buf_size);
assert (rc == 0);
/* Unbind socket by real endpoint */
rc = zmq_unbind (socket, buf);
assert (rc == 0);
----

SEE ALSO
--------
Expand Down
4 changes: 2 additions & 2 deletions src/socket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ int zmq::socket_base_t::bind (const char *addr_)
// Save last endpoint URI
listener->get_address (last_endpoint);

add_endpoint (addr_, (own_t *) listener, NULL);
add_endpoint (last_endpoint.c_str (), (own_t *) listener, NULL);
return 0;
}

Expand All @@ -420,7 +420,7 @@ int zmq::socket_base_t::bind (const char *addr_)
// Save last endpoint URI
listener->get_address (last_endpoint);

add_endpoint (addr_, (own_t *) listener, NULL);
add_endpoint (last_endpoint.c_str (), (own_t *) listener, NULL);
return 0;
}
#endif
Expand Down
55 changes: 54 additions & 1 deletion tests/test_term_endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ int main (void)
{
setup_test_environment();
int rc;
char buf[32];
const size_t buf_size = 32;
char buf[buf_size];
const char *ep = "tcp://127.0.0.1:5560";
const char *ep_wc_tcp = "tcp://127.0.0.1:*";
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
const char *ep_wc_ipc = "ipc://*";
#endif

// Create infrastructure.
void *ctx = zmq_ctx_new ();
Expand Down Expand Up @@ -100,5 +105,53 @@ int main (void)
rc = zmq_ctx_term (ctx);
assert (rc == 0);

// Create infrastructure (wild-card binding)
ctx = zmq_ctx_new ();
assert (ctx);
push = zmq_socket (ctx, ZMQ_PUSH);
assert (push);
rc = zmq_bind (push, ep_wc_tcp);
assert (rc == 0);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
pull = zmq_socket (ctx, ZMQ_PULL);
assert (pull);
rc = zmq_bind (pull, ep_wc_ipc);
assert (rc == 0);
#endif

// Unbind sockets binded by wild-card address
rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, (size_t *)&buf_size);
assert (rc == 0);
rc = zmq_unbind (push, buf);
assert (rc == 0);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, (size_t *)&buf_size);
assert (rc == 0);
rc = zmq_unbind (pull, buf);
assert (rc == 0);
#endif

// Create infrastructure (wild-card binding)
ctx = zmq_ctx_new ();
assert (ctx);
push = zmq_socket (ctx, ZMQ_PUSH);
assert (push);
rc = zmq_bind (push, ep_wc_tcp);
assert (rc == 0);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
pull = zmq_socket (ctx, ZMQ_PULL);
assert (pull);
rc = zmq_bind (pull, ep_wc_ipc);
assert (rc == 0);
#endif

// Sockets binded by wild-card address can't be unbinded by wild-card address
rc = zmq_unbind (push, ep_wc_tcp);
assert (rc == -1 && zmq_errno () == ENOENT);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
rc = zmq_unbind (pull, ep_wc_ipc);
assert (rc == -1 && zmq_errno () == ENOENT);
#endif

return 0;
}

0 comments on commit ed6bf9f

Please sign in to comment.