Skip to content

Commit

Permalink
Allowing value 0, and moving code to get_address functions based on f…
Browse files Browse the repository at this point in the history
…eedback
  • Loading branch information
ianbarber committed Feb 11, 2012
1 parent 91bf494 commit 770f843
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gerard Toonstra <[email protected]>
Ghislain Putois <[email protected]>
Gonzalo Diethelm <[email protected]>
Guido Goldstein <[email protected]>
Ian Barber <[email protected]>
Ilja Golshtein <[email protected]>
Ivo Danihelka <[email protected]>
Jacob Rideout <[email protected]>
Expand Down
21 changes: 12 additions & 9 deletions src/ipc_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 0 additions & 4 deletions src/ipc_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/tcp_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 25 additions & 25 deletions src/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/tcp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down

0 comments on commit 770f843

Please sign in to comment.