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 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "precompiled.hpp"
31#include "macros.hpp"
32#include "address.hpp"
33#include "ctx.hpp"
34#include "err.hpp"
35#include "tcp_address.hpp"
36#include "udp_address.hpp"
37#include "ipc_address.hpp"
38#include "tipc_address.hpp"
39#include "ws_address.hpp"
40
41#if defined ZMQ_HAVE_VMCI
42#include "vmci_address.hpp"
43#endif
44
45#include <string>
46#include <sstream>
47
48zmq::address_t::address_t (const std::string &protocol_,
49 const std::string &address_,
50 ctx_t *parent_) :
51 protocol (protocol_),
52 address (address_),
53 parent (parent_)
54{
55 resolved.dummy = NULL;
56}
57
58zmq::address_t::~address_t ()
59{
60 if (protocol == protocol_name::tcp) {
61 LIBZMQ_DELETE (resolved.tcp_addr);
62 } else if (protocol == protocol_name::udp) {
63 LIBZMQ_DELETE (resolved.udp_addr);
64 }
65#ifdef ZMQ_HAVE_WS
66 else if (protocol == protocol_name::ws) {
67 LIBZMQ_DELETE (resolved.ws_addr);
68 }
69#endif
70
71#ifdef ZMQ_HAVE_WSS
72 else if (protocol == protocol_name::wss) {
73 LIBZMQ_DELETE (resolved.ws_addr);
74 }
75#endif
76
77#if defined ZMQ_HAVE_IPC
78 else if (protocol == protocol_name::ipc) {
79 LIBZMQ_DELETE (resolved.ipc_addr);
80 }
81#endif
82#if defined ZMQ_HAVE_TIPC
83 else if (protocol == protocol_name::tipc) {
84 LIBZMQ_DELETE (resolved.tipc_addr);
85 }
86#endif
87#if defined ZMQ_HAVE_VMCI
88 else if (protocol == protocol_name::vmci) {
89 LIBZMQ_DELETE (resolved.vmci_addr);
90 }
91#endif
92}
93
94int zmq::address_t::to_string (std::string &addr_) const
95{
96 if (protocol == protocol_name::tcp && resolved.tcp_addr)
97 return resolved.tcp_addr->to_string (addr_);
98 if (protocol == protocol_name::udp && resolved.udp_addr)
99 return resolved.udp_addr->to_string (addr_);
100#ifdef ZMQ_HAVE_WS
101 if (protocol == protocol_name::ws && resolved.ws_addr)
102 return resolved.ws_addr->to_string (addr_);
103#endif
104#ifdef ZMQ_HAVE_WSS
105 if (protocol == protocol_name::wss && resolved.ws_addr)
106 return resolved.ws_addr->to_string (addr_);
107#endif
108#if defined ZMQ_HAVE_IPC
109 if (protocol == protocol_name::ipc && resolved.ipc_addr)
110 return resolved.ipc_addr->to_string (addr_);
111#endif
112#if defined ZMQ_HAVE_TIPC
113 if (protocol == protocol_name::tipc && resolved.tipc_addr)
114 return resolved.tipc_addr->to_string (addr_);
115#endif
116#if defined ZMQ_HAVE_VMCI
117 if (protocol == protocol_name::vmci && resolved.vmci_addr)
118 return resolved.vmci_addr->to_string (addr_);
119#endif
120
121 if (!protocol.empty () && !address.empty ()) {
122 std::stringstream s;
123 s << protocol << "://" << address;
124 addr_ = s.str ();
125 return 0;
126 }
127 addr_.clear ();
128 return -1;
129}
130
131zmq::zmq_socklen_t zmq::get_socket_address (fd_t fd_,
132 socket_end_t socket_end_,
133 sockaddr_storage *ss_)
134{
135 zmq_socklen_t sl = static_cast<zmq_socklen_t> (sizeof (*ss_));
136
137 const int rc =
138 socket_end_ == socket_end_local
139 ? getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl)
140 : getpeername (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl);
141
142 return rc != 0 ? 0 : sl;
143}
144