mirror of
https://github.com/zeromq/libzmq.git
synced 2025-02-23 23:16:41 +01:00
Problem: get_socket_address and get_socket_name not available throughout libzmq and restricted to local address
Solution: move to address.hpp/.cpp and generalize
This commit is contained in:
parent
01371398e9
commit
aec9b130f7
@ -108,3 +108,17 @@ int zmq::address_t::to_string (std::string &addr_) const
|
|||||||
addr_.clear ();
|
addr_.clear ();
|
||||||
return -1;
|
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<zmq_socklen_t> (sizeof (*ss_));
|
||||||
|
|
||||||
|
const int rc =
|
||||||
|
socket_end_ == socket_end_local
|
||||||
|
? getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl)
|
||||||
|
: getpeername (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl);
|
||||||
|
|
||||||
|
return rc != 0 ? 0 : sl;
|
||||||
|
}
|
||||||
|
@ -30,8 +30,16 @@
|
|||||||
#ifndef __ZMQ_ADDRESS_HPP_INCLUDED__
|
#ifndef __ZMQ_ADDRESS_HPP_INCLUDED__
|
||||||
#define __ZMQ_ADDRESS_HPP_INCLUDED__
|
#define __ZMQ_ADDRESS_HPP_INCLUDED__
|
||||||
|
|
||||||
|
#include "fd.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef ZMQ_HAVE_WINDOWS
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#else
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace zmq
|
namespace zmq
|
||||||
{
|
{
|
||||||
class ctx_t;
|
class ctx_t;
|
||||||
@ -97,6 +105,37 @@ struct address_t
|
|||||||
|
|
||||||
int to_string (std::string &addr_) const;
|
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 <typename T>
|
||||||
|
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<struct sockaddr *> (&ss), sl);
|
||||||
|
std::string address_string;
|
||||||
|
addr.to_string (address_string);
|
||||||
|
return address_string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
#include "err.hpp"
|
#include "err.hpp"
|
||||||
#include "ip.hpp"
|
#include "ip.hpp"
|
||||||
#include "socket_base.hpp"
|
#include "socket_base.hpp"
|
||||||
|
#include "address.hpp"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@ -151,12 +152,12 @@ void zmq::ipc_listener_t::in_event ()
|
|||||||
create_engine (fd);
|
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<ipc_address_t> (fd_);
|
return zmq::get_socket_name<ipc_address_t> (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
|
// Create addr on stack for auto-cleanup
|
||||||
std::string addr (addr_);
|
std::string addr (addr_);
|
||||||
|
@ -48,10 +48,10 @@ class ipc_listener_t : public stream_listener_base_t
|
|||||||
const options_t &options_);
|
const options_t &options_);
|
||||||
|
|
||||||
// Set address to listen on.
|
// Set address to listen on.
|
||||||
int set_address (const char *addr_);
|
int set_local_address (const char *addr_);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string get_socket_name (fd_t fd_) const;
|
std::string get_local_socket_name (fd_t fd_) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Handlers for I/O events.
|
// Handlers for I/O events.
|
||||||
|
@ -612,7 +612,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_)
|
|||||||
tcp_listener_t *listener =
|
tcp_listener_t *listener =
|
||||||
new (std::nothrow) tcp_listener_t (io_thread, this, options);
|
new (std::nothrow) tcp_listener_t (io_thread, this, options);
|
||||||
alloc_assert (listener);
|
alloc_assert (listener);
|
||||||
rc = listener->set_address (address.c_str ());
|
rc = listener->set_local_address (address.c_str ());
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
LIBZMQ_DELETE (listener);
|
LIBZMQ_DELETE (listener);
|
||||||
event_bind_failed (make_unconnected_bind_endpoint_pair (address),
|
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
|
// Save last endpoint URI
|
||||||
listener->get_address (_last_endpoint);
|
listener->get_local_address (_last_endpoint);
|
||||||
|
|
||||||
add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint),
|
add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint),
|
||||||
static_cast<own_t *> (listener), NULL);
|
static_cast<own_t *> (listener), NULL);
|
||||||
@ -635,7 +635,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_)
|
|||||||
ipc_listener_t *listener =
|
ipc_listener_t *listener =
|
||||||
new (std::nothrow) ipc_listener_t (io_thread, this, options);
|
new (std::nothrow) ipc_listener_t (io_thread, this, options);
|
||||||
alloc_assert (listener);
|
alloc_assert (listener);
|
||||||
int rc = listener->set_address (address.c_str ());
|
int rc = listener->set_local_address (address.c_str ());
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
LIBZMQ_DELETE (listener);
|
LIBZMQ_DELETE (listener);
|
||||||
event_bind_failed (make_unconnected_bind_endpoint_pair (address),
|
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
|
// Save last endpoint URI
|
||||||
listener->get_address (_last_endpoint);
|
listener->get_local_address (_last_endpoint);
|
||||||
|
|
||||||
add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint),
|
add_endpoint (make_unconnected_bind_endpoint_pair (_last_endpoint),
|
||||||
static_cast<own_t *> (listener), NULL);
|
static_cast<own_t *> (listener), NULL);
|
||||||
@ -657,7 +657,7 @@ int zmq::socket_base_t::bind (const char *endpoint_uri_)
|
|||||||
tipc_listener_t *listener =
|
tipc_listener_t *listener =
|
||||||
new (std::nothrow) tipc_listener_t (io_thread, this, options);
|
new (std::nothrow) tipc_listener_t (io_thread, this, options);
|
||||||
alloc_assert (listener);
|
alloc_assert (listener);
|
||||||
int rc = listener->set_address (address.c_str ());
|
int rc = listener->set_local_address (address.c_str ());
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
LIBZMQ_DELETE (listener);
|
LIBZMQ_DELETE (listener);
|
||||||
event_bind_failed (make_unconnected_bind_endpoint_pair (address),
|
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
|
// 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?
|
// TODO shouldn't this use _last_endpoint as in the other cases?
|
||||||
add_endpoint (make_unconnected_bind_endpoint_pair (endpoint_uri_),
|
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 =
|
vmci_listener_t *listener =
|
||||||
new (std::nothrow) vmci_listener_t (io_thread, this, options);
|
new (std::nothrow) vmci_listener_t (io_thread, this, options);
|
||||||
alloc_assert (listener);
|
alloc_assert (listener);
|
||||||
int rc = listener->set_address (address.c_str ());
|
int rc = listener->set_local_address (address.c_str ());
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
LIBZMQ_DELETE (listener);
|
LIBZMQ_DELETE (listener);
|
||||||
event_bind_failed (address, zmq_errno ());
|
event_bind_failed (address, zmq_errno ());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
listener->get_address (_last_endpoint);
|
listener->get_local_address (_last_endpoint);
|
||||||
|
|
||||||
add_endpoint (_last_endpoint.c_str (), static_cast<own_t *> (listener),
|
add_endpoint (_last_endpoint.c_str (), static_cast<own_t *> (listener),
|
||||||
NULL);
|
NULL);
|
||||||
|
@ -51,24 +51,12 @@ zmq::stream_listener_base_t::~stream_listener_base_t ()
|
|||||||
zmq_assert (!_handle);
|
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;
|
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<zmq_socklen_t> (sizeof (*ss_));
|
|
||||||
|
|
||||||
const int rc =
|
|
||||||
getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl);
|
|
||||||
|
|
||||||
return rc != 0 ? 0 : sl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void zmq::stream_listener_base_t::process_plug ()
|
void zmq::stream_listener_base_t::process_plug ()
|
||||||
{
|
{
|
||||||
// Start polling for incoming connections.
|
// 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)
|
void zmq::stream_listener_base_t::create_engine (fd_t fd)
|
||||||
{
|
{
|
||||||
const endpoint_uri_pair_t endpoint_pair (_endpoint, get_socket_name (fd),
|
const endpoint_uri_pair_t endpoint_pair (
|
||||||
endpoint_type_bind);
|
_endpoint, get_local_socket_name (fd), endpoint_type_bind);
|
||||||
|
|
||||||
stream_engine_t *engine =
|
stream_engine_t *engine =
|
||||||
new (std::nothrow) stream_engine_t (fd, options, endpoint_pair);
|
new (std::nothrow) stream_engine_t (fd, options, endpoint_pair);
|
||||||
|
@ -43,12 +43,6 @@ namespace zmq
|
|||||||
class io_thread_t;
|
class io_thread_t;
|
||||||
class socket_base_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
|
class stream_listener_base_t : public own_t, public io_object_t
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -58,25 +52,10 @@ class stream_listener_base_t : public own_t, public io_object_t
|
|||||||
~stream_listener_base_t ();
|
~stream_listener_base_t ();
|
||||||
|
|
||||||
// Get the bound address for use with wildcards
|
// Get the bound address for use with wildcards
|
||||||
int get_address (std::string &addr_) const;
|
int get_local_address (std::string &addr_) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static zmq_socklen_t get_socket_address (fd_t fd_, sockaddr_storage *ss_);
|
virtual std::string get_local_socket_name (fd_t fd_) const = 0;
|
||||||
virtual std::string get_socket_name (fd_t fd_) const = 0;
|
|
||||||
|
|
||||||
template <typename T> 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<struct sockaddr *> (&ss), sl);
|
|
||||||
std::string address_string;
|
|
||||||
addr.to_string (address_string);
|
|
||||||
return address_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Handlers for incoming commands.
|
// Handlers for incoming commands.
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
#include "ip.hpp"
|
#include "ip.hpp"
|
||||||
#include "tcp.hpp"
|
#include "tcp.hpp"
|
||||||
#include "socket_base.hpp"
|
#include "socket_base.hpp"
|
||||||
|
#include "address.hpp"
|
||||||
|
|
||||||
#ifndef ZMQ_HAVE_WINDOWS
|
#ifndef ZMQ_HAVE_WINDOWS
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -93,12 +94,12 @@ void zmq::tcp_listener_t::in_event ()
|
|||||||
create_engine (fd);
|
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<tcp_address_t> (fd_);
|
return zmq::get_socket_name<tcp_address_t> (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.
|
// Convert the textual address into address structure.
|
||||||
int rc = _address.resolve (addr_, true, options.ipv6);
|
int rc = _address.resolve (addr_, true, options.ipv6);
|
||||||
|
@ -44,10 +44,10 @@ class tcp_listener_t : public stream_listener_base_t
|
|||||||
const options_t &options_);
|
const options_t &options_);
|
||||||
|
|
||||||
// Set address to listen on.
|
// Set address to listen on.
|
||||||
int set_address (const char *addr_);
|
int set_local_address (const char *addr_);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string get_socket_name (fd_t fd_) const;
|
std::string get_local_socket_name (fd_t fd_) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Handlers for I/O events.
|
// Handlers for I/O events.
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
#include "err.hpp"
|
#include "err.hpp"
|
||||||
#include "ip.hpp"
|
#include "ip.hpp"
|
||||||
#include "socket_base.hpp"
|
#include "socket_base.hpp"
|
||||||
|
#include "address.hpp"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@ -77,12 +78,12 @@ void zmq::tipc_listener_t::in_event ()
|
|||||||
create_engine (fd);
|
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<tipc_address_t> (fd_);
|
return zmq::get_socket_name<tipc_address_t> (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
|
// Convert str to address struct
|
||||||
int rc = _address.resolve (addr_);
|
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 random Port Identity, update address object to reflect the assigned address
|
||||||
if (_address.is_random ()) {
|
if (_address.is_random ()) {
|
||||||
struct sockaddr_storage ss;
|
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)
|
if (sl == 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
@ -50,10 +50,10 @@ class tipc_listener_t : public stream_listener_base_t
|
|||||||
const options_t &options_);
|
const options_t &options_);
|
||||||
|
|
||||||
// Set address to listen on.
|
// Set address to listen on.
|
||||||
int set_address (const char *addr_);
|
int set_local_address (const char *addr_);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string get_socket_name (fd_t fd_) const;
|
std::string get_local_socket_name (fd_t fd_) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Handlers for I/O events.
|
// Handlers for I/O events.
|
||||||
|
@ -125,7 +125,7 @@ void zmq::vmci_listener_t::in_event ()
|
|||||||
socket->event_accepted (endpoint, fd);
|
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;
|
struct sockaddr_storage ss;
|
||||||
#ifdef ZMQ_HAVE_HPUX
|
#ifdef ZMQ_HAVE_HPUX
|
||||||
@ -143,7 +143,7 @@ int zmq::vmci_listener_t::get_address (std::string &addr_)
|
|||||||
return addr.to_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
|
// Create addr on stack for auto-cleanup
|
||||||
std::string addr (addr_);
|
std::string addr (addr_);
|
||||||
|
@ -56,10 +56,10 @@ class vmci_listener_t : public own_t, public io_object_t
|
|||||||
~vmci_listener_t ();
|
~vmci_listener_t ();
|
||||||
|
|
||||||
// Set address to listen on.
|
// 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
|
// Get the bound address for use with wildcards
|
||||||
int get_address (std::string &addr_);
|
int get_local_address (std::string &addr_);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Handlers for incoming commands.
|
// Handlers for incoming commands.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user