diff --git a/src/address.cpp b/src/address.cpp index 9b559890..021686d6 100644 --- a/src/address.cpp +++ b/src/address.cpp @@ -108,3 +108,17 @@ int zmq::address_t::to_string (std::string &addr_) const addr_.clear (); return -1; } + +zmq::zmq_socklen_t zmq::get_socket_address (fd_t fd_, + socket_end_t socket_end_, + sockaddr_storage *ss_) +{ + zmq_socklen_t sl = static_cast (sizeof (*ss_)); + + const int rc = + socket_end_ == socket_end_local + ? getsockname (fd_, reinterpret_cast (ss_), &sl) + : getpeername (fd_, reinterpret_cast (ss_), &sl); + + return rc != 0 ? 0 : sl; +} diff --git a/src/address.hpp b/src/address.hpp index 34ca5766..b20a10e5 100644 --- a/src/address.hpp +++ b/src/address.hpp @@ -30,8 +30,16 @@ #ifndef __ZMQ_ADDRESS_HPP_INCLUDED__ #define __ZMQ_ADDRESS_HPP_INCLUDED__ +#include "fd.hpp" + #include +#ifndef ZMQ_HAVE_WINDOWS +#include +#else +#include +#endif + namespace zmq { class ctx_t; @@ -97,6 +105,37 @@ struct address_t int to_string (std::string &addr_) const; }; + +#if defined(ZMQ_HAVE_HPUX) || defined(ZMQ_HAVE_VXWORKS) \ + || defined(ZMQ_HAVE_WINDOWS) +typedef int zmq_socklen_t; +#else +typedef socklen_t zmq_socklen_t; +#endif + +enum socket_end_t +{ + socket_end_local, + socket_end_remote +}; + +zmq_socklen_t +get_socket_address (fd_t fd_, socket_end_t socket_end_, sockaddr_storage *ss_); + +template +std::string get_socket_name (fd_t fd_, socket_end_t socket_end_) +{ + struct sockaddr_storage ss; + const zmq_socklen_t sl = get_socket_address (fd_, socket_end_, &ss); + if (sl == 0) { + return std::string (); + } + + const T addr (reinterpret_cast (&ss), sl); + std::string address_string; + addr.to_string (address_string); + return address_string; +} } #endif diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 702c1f64..1149cc42 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -43,6 +43,7 @@ #include "err.hpp" #include "ip.hpp" #include "socket_base.hpp" +#include "address.hpp" #include #include @@ -151,12 +152,12 @@ void zmq::ipc_listener_t::in_event () create_engine (fd); } -std::string zmq::ipc_listener_t::get_socket_name (zmq::fd_t fd_) const +std::string zmq::ipc_listener_t::get_local_socket_name (zmq::fd_t fd_) const { - return stream_listener_base_t::get_socket_name (fd_); + return zmq::get_socket_name (fd_, socket_end_local); } -int zmq::ipc_listener_t::set_address (const char *addr_) +int zmq::ipc_listener_t::set_local_address (const char *addr_) { // Create addr on stack for auto-cleanup std::string addr (addr_); diff --git a/src/ipc_listener.hpp b/src/ipc_listener.hpp index 81dba6eb..68cb1945 100644 --- a/src/ipc_listener.hpp +++ b/src/ipc_listener.hpp @@ -48,10 +48,10 @@ class ipc_listener_t : public stream_listener_base_t const options_t &options_); // Set address to listen on. - int set_address (const char *addr_); + int set_local_address (const char *addr_); protected: - std::string get_socket_name (fd_t fd_) const; + std::string get_local_socket_name (fd_t fd_) const; private: // Handlers for I/O events. diff --git a/src/socket_base.cpp b/src/socket_base.cpp index bb5458d7..d1f82eb9 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -612,7 +612,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) tcp_listener_t *listener = new (std::nothrow) tcp_listener_t (io_thread, this, options); alloc_assert (listener); - rc = listener->set_address (address.c_str ()); + rc = listener->set_local_address (address.c_str ()); if (rc != 0) { LIBZMQ_DELETE (listener); event_bind_failed (make_unconnected_bind_endpoint_pair (address), @@ -621,7 +621,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) } // Save last endpoint URI - listener->get_address (_last_endpoint); + listener->get_local_address (_last_endpoint); add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint), static_cast (listener), NULL); @@ -635,7 +635,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) ipc_listener_t *listener = new (std::nothrow) ipc_listener_t (io_thread, this, options); alloc_assert (listener); - int rc = listener->set_address (address.c_str ()); + int rc = listener->set_local_address (address.c_str ()); if (rc != 0) { LIBZMQ_DELETE (listener); event_bind_failed (make_unconnected_bind_endpoint_pair (address), @@ -644,7 +644,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) } // Save last endpoint URI - listener->get_address (_last_endpoint); + listener->get_local_address (_last_endpoint); add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint), static_cast (listener), NULL); @@ -657,7 +657,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) tipc_listener_t *listener = new (std::nothrow) tipc_listener_t (io_thread, this, options); alloc_assert (listener); - int rc = listener->set_address (address.c_str ()); + int rc = listener->set_local_address (address.c_str ()); if (rc != 0) { LIBZMQ_DELETE (listener); event_bind_failed (make_unconnected_bind_endpoint_pair (address), @@ -666,7 +666,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) } // Save last endpoint URI - listener->get_address (_last_endpoint); + listener->get_local_address (_last_endpoint); // TODO shouldn't this use _last_endpoint as in the other cases? add_endpoint (make_unconnected_bind_endpoint_pair (endpoint_uri_), @@ -680,14 +680,14 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_) vmci_listener_t *listener = new (std::nothrow) vmci_listener_t (io_thread, this, options); alloc_assert (listener); - int rc = listener->set_address (address.c_str ()); + int rc = listener->set_local_address (address.c_str ()); if (rc != 0) { LIBZMQ_DELETE (listener); event_bind_failed (address, zmq_errno ()); return -1; } - listener->get_address (_last_endpoint); + listener->get_local_address (_last_endpoint); add_endpoint (_last_endpoint.c_str (), static_cast (listener), NULL); diff --git a/src/stream_listener_base.cpp b/src/stream_listener_base.cpp index 878f38da..341610c5 100644 --- a/src/stream_listener_base.cpp +++ b/src/stream_listener_base.cpp @@ -51,24 +51,12 @@ zmq::stream_listener_base_t::~stream_listener_base_t () zmq_assert (!_handle); } -int zmq::stream_listener_base_t::get_address (std::string &addr_) const +int zmq::stream_listener_base_t::get_local_address (std::string &addr_) const { - addr_ = get_socket_name (_s); + addr_ = get_local_socket_name (_s); return addr_.empty () ? -1 : 0; } -zmq::zmq_socklen_t -zmq::stream_listener_base_t::get_socket_address (fd_t fd_, - sockaddr_storage *ss_) -{ - zmq_socklen_t sl = static_cast (sizeof (*ss_)); - - const int rc = - getsockname (fd_, reinterpret_cast (ss_), &sl); - - return rc != 0 ? 0 : sl; -} - void zmq::stream_listener_base_t::process_plug () { // Start polling for incoming connections. @@ -104,8 +92,8 @@ int zmq::stream_listener_base_t::close () void zmq::stream_listener_base_t::create_engine (fd_t fd) { - const endpoint_uri_pair_t endpoint_pair (_endpoint, get_socket_name (fd), - endpoint_type_bind); + const endpoint_uri_pair_t endpoint_pair ( + _endpoint, get_local_socket_name (fd), endpoint_type_bind); stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options, endpoint_pair); diff --git a/src/stream_listener_base.hpp b/src/stream_listener_base.hpp index a48d2dc5..3bf8186c 100644 --- a/src/stream_listener_base.hpp +++ b/src/stream_listener_base.hpp @@ -43,12 +43,6 @@ namespace zmq class io_thread_t; class socket_base_t; -#if defined(ZMQ_HAVE_HPUX) || defined(ZMQ_HAVE_VXWORKS) -typedef int zmq_socklen_t; -#else -typedef socklen_t zmq_socklen_t; -#endif - class stream_listener_base_t : public own_t, public io_object_t { public: @@ -58,25 +52,10 @@ class stream_listener_base_t : public own_t, public io_object_t ~stream_listener_base_t (); // Get the bound address for use with wildcards - int get_address (std::string &addr_) const; + int get_local_address (std::string &addr_) const; protected: - static zmq_socklen_t get_socket_address (fd_t fd_, sockaddr_storage *ss_); - virtual std::string get_socket_name (fd_t fd_) const = 0; - - template static std::string get_socket_name (fd_t fd_) - { - struct sockaddr_storage ss; - const zmq_socklen_t sl = get_socket_address (fd_, &ss); - if (sl == 0) { - return std::string (); - } - - const T addr (reinterpret_cast (&ss), sl); - std::string address_string; - addr.to_string (address_string); - return address_string; - } + virtual std::string get_local_socket_name (fd_t fd_) const = 0; private: // Handlers for incoming commands. diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index f4daf6ea..151fa44e 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -40,6 +40,7 @@ #include "ip.hpp" #include "tcp.hpp" #include "socket_base.hpp" +#include "address.hpp" #ifndef ZMQ_HAVE_WINDOWS #include @@ -93,12 +94,12 @@ void zmq::tcp_listener_t::in_event () create_engine (fd); } -std::string zmq::tcp_listener_t::get_socket_name (zmq::fd_t fd_) const +std::string zmq::tcp_listener_t::get_local_socket_name (zmq::fd_t fd_) const { - return stream_listener_base_t::get_socket_name (fd_); + return zmq::get_socket_name (fd_, socket_end_local); } -int zmq::tcp_listener_t::set_address (const char *addr_) +int zmq::tcp_listener_t::set_local_address (const char *addr_) { // Convert the textual address into address structure. int rc = _address.resolve (addr_, true, options.ipv6); diff --git a/src/tcp_listener.hpp b/src/tcp_listener.hpp index 7aeccf67..b740b20b 100644 --- a/src/tcp_listener.hpp +++ b/src/tcp_listener.hpp @@ -44,10 +44,10 @@ class tcp_listener_t : public stream_listener_base_t const options_t &options_); // Set address to listen on. - int set_address (const char *addr_); + int set_local_address (const char *addr_); protected: - std::string get_socket_name (fd_t fd_) const; + std::string get_local_socket_name (fd_t fd_) const; private: // Handlers for I/O events. diff --git a/src/tipc_listener.cpp b/src/tipc_listener.cpp index c550ae0e..38fc8ebf 100644 --- a/src/tipc_listener.cpp +++ b/src/tipc_listener.cpp @@ -43,6 +43,7 @@ #include "err.hpp" #include "ip.hpp" #include "socket_base.hpp" +#include "address.hpp" #include #include @@ -77,12 +78,12 @@ void zmq::tipc_listener_t::in_event () create_engine (fd); } -std::string zmq::tipc_listener_t::get_socket_name (zmq::fd_t fd_) const +std::string zmq::tipc_listener_t::get_local_socket_name (zmq::fd_t fd_) const { - return stream_listener_base_t::get_socket_name (fd_); + return zmq::get_socket_name (fd_, socket_end_local); } -int zmq::tipc_listener_t::set_address (const char *addr_) +int zmq::tipc_listener_t::set_local_address (const char *addr_) { // Convert str to address struct int rc = _address.resolve (addr_); @@ -104,7 +105,7 @@ int zmq::tipc_listener_t::set_address (const char *addr_) // If random Port Identity, update address object to reflect the assigned address if (_address.is_random ()) { struct sockaddr_storage ss; - const zmq_socklen_t sl = get_socket_address (_s, &ss); + const zmq_socklen_t sl = get_socket_address (_s, socket_end_local, &ss); if (sl == 0) goto error; diff --git a/src/tipc_listener.hpp b/src/tipc_listener.hpp index 4ed0971e..1ac958c2 100644 --- a/src/tipc_listener.hpp +++ b/src/tipc_listener.hpp @@ -50,10 +50,10 @@ class tipc_listener_t : public stream_listener_base_t const options_t &options_); // Set address to listen on. - int set_address (const char *addr_); + int set_local_address (const char *addr_); protected: - std::string get_socket_name (fd_t fd_) const; + std::string get_local_socket_name (fd_t fd_) const; private: // Handlers for I/O events. diff --git a/src/vmci_listener.cpp b/src/vmci_listener.cpp index 207f60f1..7514e1bf 100644 --- a/src/vmci_listener.cpp +++ b/src/vmci_listener.cpp @@ -125,7 +125,7 @@ void zmq::vmci_listener_t::in_event () socket->event_accepted (endpoint, fd); } -int zmq::vmci_listener_t::get_address (std::string &addr_) +int zmq::vmci_listener_t::get_local_address (std::string &addr_) { struct sockaddr_storage ss; #ifdef ZMQ_HAVE_HPUX @@ -143,7 +143,7 @@ int zmq::vmci_listener_t::get_address (std::string &addr_) return addr.to_string (addr_); } -int zmq::vmci_listener_t::set_address (const char *addr_) +int zmq::vmci_listener_t::set_local_address (const char *addr_) { // Create addr on stack for auto-cleanup std::string addr (addr_); diff --git a/src/vmci_listener.hpp b/src/vmci_listener.hpp index b179ceb8..cad68ebc 100644 --- a/src/vmci_listener.hpp +++ b/src/vmci_listener.hpp @@ -56,10 +56,10 @@ class vmci_listener_t : public own_t, public io_object_t ~vmci_listener_t (); // Set address to listen on. - int set_address (const char *addr_); + int set_local_address (const char *addr_); // Get the bound address for use with wildcards - int get_address (std::string &addr_); + int get_local_address (std::string &addr_); private: // Handlers for incoming commands.