address.hpp 3.91 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29 30 31 32

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ZMQ_ADDRESS_HPP_INCLUDED__
#define __ZMQ_ADDRESS_HPP_INCLUDED__

33 34
#include "fd.hpp"

35 36
#include <string>

37 38 39 40 41 42
#ifndef ZMQ_HAVE_WINDOWS
#include <sys/socket.h>
#else
#include <ws2tcpip.h>
#endif

43 44
namespace zmq
{
45 46 47
class ctx_t;
class tcp_address_t;
class udp_address_t;
48
class ws_address_t;
49
class wss_address_t;
50
#if defined ZMQ_HAVE_IPC
51
class ipc_address_t;
52
#endif
53
#if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_VXWORKS
54
class tipc_address_t;
Ilya Kulakov's avatar
Ilya Kulakov committed
55 56
#endif
#if defined ZMQ_HAVE_VMCI
57
class vmci_address_t;
Mikko Koppanen's avatar
Mikko Koppanen committed
58
#endif
59 60 61

namespace protocol_name
{
62
static const char inproc[] = "inproc";
63 64
static const char tcp[] = "tcp";
static const char udp[] = "udp";
65
#ifdef ZMQ_HAVE_WS
66
static const char ws[] = "ws";
67
#endif
68 69 70
#ifdef ZMQ_HAVE_WSS
static const char wss[] = "wss";
#endif
71
#if defined ZMQ_HAVE_IPC
72 73 74 75 76 77 78 79 80 81
static const char ipc[] = "ipc";
#endif
#if defined ZMQ_HAVE_TIPC
static const char tipc[] = "tipc";
#endif
#if defined ZMQ_HAVE_VMCI
static const char vmci[] = "vmci";
#endif
}

82 83 84 85 86
struct address_t
{
    address_t (const std::string &protocol_,
               const std::string &address_,
               ctx_t *parent_);
87

88
    ~address_t ();
89

90 91
    const std::string protocol;
    const std::string address;
92
    ctx_t *const parent;
93

94
    //  Protocol specific resolved address
95
    //  All members must be pointers to allow for consistent initialization
96 97
    union
    {
98
        void *dummy;
99 100
        tcp_address_t *tcp_addr;
        udp_address_t *udp_addr;
101
#ifdef ZMQ_HAVE_WS
102
        ws_address_t *ws_addr;
103
        wss_address_t *wss_addr;
104
#endif
105
#if defined ZMQ_HAVE_IPC
106
        ipc_address_t *ipc_addr;
107
#endif
108
#if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_VXWORKS
109
        tipc_address_t *tipc_addr;
Ilya Kulakov's avatar
Ilya Kulakov committed
110 111
#endif
#if defined ZMQ_HAVE_VMCI
112
        vmci_address_t *vmci_addr;
Mikko Koppanen's avatar
Mikko Koppanen committed
113
#endif
114
    } resolved;
115

116 117
    int to_string (std::string &addr_) const;
};
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

#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;
}
149 150 151
}

#endif