Allowing value 0, and moving code to get_address functions based on feedback

This commit is contained in:
Ian Barber 2012-02-11 15:09:03 +00:00
parent 91bf4944da
commit 770f84331f
6 changed files with 41 additions and 44 deletions

View File

@ -33,6 +33,7 @@ Gerard Toonstra <gtoonstra@gmail.com>
Ghislain Putois <ghpu@infonie.fr>
Gonzalo Diethelm <gdiethelm@dcv.cl>
Guido Goldstein <zmq@a-nugget.de>
Ian Barber <ian.barber@gmail.com>
Ilja Golshtein <ilejncs@narod.ru>
Ivo Danihelka <ivo@danihelka.net>
Jacob Rideout <jacob.rideout@returnpath.net>

View File

@ -97,12 +97,18 @@ void zmq::ipc_listener_t::in_event ()
int zmq::ipc_listener_t::get_address (unsigned char *addr, size_t *len)
{
if (bound_addr_len == 0) {
return -1;
struct sockaddr_un sun;
int rc;
// Get the details of the IPC socket
socklen_t sl = sizeof(sockaddr_un);
rc = getsockname (s, (sockaddr *)&sun, &sl);
if (rc != 0) {
return rc;
}
memcpy (addr, bound_addr, bound_addr_len + 1);
*len = bound_addr_len + 1;
// Store the address for retrieval by users using wildcards
*len = sprintf((char *)addr, "ipc://%s", sun.sun_path);
return 0;
}
@ -141,10 +147,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
rc = listen (s, options.backlog);
if (rc != 0)
return -1;
// Return the bound address
bound_addr_len = sprintf(bound_addr, "ipc://%s", addr_);
return 0;
}

View File

@ -68,10 +68,6 @@ namespace zmq
// newly created connection. The function may return retired_fd
// if the connection was dropped while waiting in the listen backlog.
fd_t accept ();
// Store the connected endpoint for binds to port 0
char bound_addr[256];
size_t bound_addr_len;
// True, if the undelying file for UNIX domain socket exists.
bool has_file;

View File

@ -388,7 +388,8 @@ int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
addr_str = addr_str.substr (1, addr_str.size () - 2);
uint16_t port;
if (port_str[0] == '*') {
// Allow 0 specifically, to detect invalid port error in atoi if not
if (port_str[0] == '*' || port_str[0] == '0') {
// Resolve wildcard to 0 to allow autoselection of port
port = 0;
} else {

View File

@ -52,7 +52,6 @@ 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_)
@ -121,13 +120,33 @@ void zmq::tcp_listener_t::close ()
}
int zmq::tcp_listener_t::get_address (unsigned char *addr, size_t *len)
{
if (bound_addr_len == 0) {
return -1;
{
struct sockaddr sa;
char host[INET6_ADDRSTRLEN];
int port, rc;
// Get the details of the TCP socket
socklen_t sl = sizeof(sockaddr);
rc = getsockname (s, &sa, &sl);
if (rc != 0) {
return rc;
}
// Split the retrieval between IPv4 and v6 addresses
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);
// Store the address for retrieval by users using wildcards
*len = sprintf((char *)addr, "tcp://%s:%d", host, 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
*len = sprintf((char *)*addr, "tcp://[%s]:%d", host, port);
}
memcpy (addr, bound_addr, bound_addr_len + 1);
*len = bound_addr_len + 1;
return 0;
}
@ -193,25 +212,6 @@ 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

View File

@ -45,7 +45,7 @@ namespace zmq
// Set address to listen on.
int set_address (const char *addr_);
// Get the bound address for use with wildcards
// Get the bound address for use with wildcard
int get_address(unsigned char *addr, size_t *len);
private:
@ -67,10 +67,6 @@ namespace zmq
// Address to listen on.
tcp_address_t address;
// Store the connected endpoint for binds to port 0
char bound_addr[256];
size_t bound_addr_len;
// True, if the undelying file for UNIX domain socket exists.
bool has_file;