Adding ZMQ_LAST_ENDPOINT for wildcard support on TCP and IPC sockets

This commit is contained in:
Ian Barber
2012-02-08 22:06:46 +00:00
parent 7e8a839a22
commit 7b32c9cb51
9 changed files with 99 additions and 7 deletions

View File

@@ -52,6 +52,7 @@ zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
socket_base_t *socket_, const options_t &options_) :
own_t (io_thread_, options_),
io_object_t (io_thread_),
bound_addr_len (0),
has_file (false),
s (retired_fd),
socket (socket_)
@@ -119,6 +120,17 @@ void zmq::tcp_listener_t::close ()
s = retired_fd;
}
int zmq::tcp_listener_t::get_address (unsigned char *addr, size_t *len)
{
if (bound_addr_len == 0) {
return -1;
}
memcpy (addr, bound_addr, bound_addr_len + 1);
*len = bound_addr_len + 1;
return 0;
}
int zmq::tcp_listener_t::set_address (const char *addr_)
{
// Convert the textual address into address structure.
@@ -168,6 +180,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
errno_assert (rc == 0);
#endif
// Bind the socket to the network interface and port.
rc = bind (s, address.addr (), address.addrlen ());
#ifdef ZMQ_HAVE_WINDOWS
@@ -180,6 +193,25 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
return -1;
#endif
struct sockaddr sa;
socklen_t sl = sizeof(sockaddr);
rc = getsockname (s, &sa, &sl);
if (rc == 0) {
char host[INET6_ADDRSTRLEN];
int port;
if ( sa.sa_family == AF_INET ) {
inet_ntop(AF_INET, &(((struct sockaddr_in *)&sa)->sin_addr), host, INET6_ADDRSTRLEN);
port = ntohs( ((struct sockaddr_in *)&sa)->sin_port);
} else {
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)&sa)->sin6_addr), host, INET6_ADDRSTRLEN);
port = ntohs( ((struct sockaddr_in6 *)&sa)->sin6_port);
}
// Store the address for retrieval by users using wildcards
bound_addr_len = sprintf(bound_addr, "tcp://%s:%d", host, port);
}
// Listen for incomming connections.
rc = listen (s, options.backlog);
#ifdef ZMQ_HAVE_WINDOWS