/src/libzmq/src/address.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #include "precompiled.hpp" |
4 | | #include "macros.hpp" |
5 | | #include "address.hpp" |
6 | | #include "ctx.hpp" |
7 | | #include "err.hpp" |
8 | | #include "tcp_address.hpp" |
9 | | #include "udp_address.hpp" |
10 | | #include "ipc_address.hpp" |
11 | | #include "tipc_address.hpp" |
12 | | #include "ws_address.hpp" |
13 | | |
14 | | #if defined ZMQ_HAVE_VSOCK |
15 | | // fix header conflict with VMCI |
16 | | #define sockaddr_vm linux_sockaddr_vm |
17 | | #define VMADDR_PORT_ANY LINUX_VMADDR_PORT_ANY |
18 | | #define VMADDR_CID_ANY LINUX_VMADDR_CID_ANY |
19 | | #include "vsock_address.hpp" |
20 | | #undef sockaddr_vm |
21 | | #undef VMADDR_CID_ANY |
22 | | #undef VMADDR_PORT_ANY |
23 | | #endif |
24 | | |
25 | | #if defined ZMQ_HAVE_VMCI |
26 | | #include "vmci_address.hpp" |
27 | | #endif |
28 | | |
29 | | #include <string> |
30 | | #include <sstream> |
31 | | |
32 | | zmq::address_t::address_t (const std::string &protocol_, |
33 | | const std::string &address_, |
34 | | ctx_t *parent_) : |
35 | 0 | protocol (protocol_), address (address_), parent (parent_) |
36 | 0 | { |
37 | 0 | resolved.dummy = NULL; |
38 | 0 | } |
39 | | |
40 | | zmq::address_t::~address_t () |
41 | 0 | { |
42 | 0 | if (protocol == protocol_name::tcp) { |
43 | 0 | LIBZMQ_DELETE (resolved.tcp_addr); |
44 | 0 | } else if (protocol == protocol_name::udp) { |
45 | 0 | LIBZMQ_DELETE (resolved.udp_addr); |
46 | 0 | } |
47 | 0 | #ifdef ZMQ_HAVE_WS |
48 | 0 | else if (protocol == protocol_name::ws) { |
49 | 0 | LIBZMQ_DELETE (resolved.ws_addr); |
50 | 0 | } |
51 | 0 | #endif |
52 | | |
53 | | #ifdef ZMQ_HAVE_WSS |
54 | | else if (protocol == protocol_name::wss) { |
55 | | LIBZMQ_DELETE (resolved.ws_addr); |
56 | | } |
57 | | #endif |
58 | | |
59 | 0 | #if defined ZMQ_HAVE_IPC |
60 | 0 | else if (protocol == protocol_name::ipc) { |
61 | 0 | LIBZMQ_DELETE (resolved.ipc_addr); |
62 | 0 | } |
63 | 0 | #endif |
64 | 0 | #if defined ZMQ_HAVE_TIPC |
65 | 0 | else if (protocol == protocol_name::tipc) { |
66 | 0 | LIBZMQ_DELETE (resolved.tipc_addr); |
67 | 0 | } |
68 | 0 | #endif |
69 | | #if defined ZMQ_HAVE_VMCI |
70 | | else if (protocol == protocol_name::vmci) { |
71 | | LIBZMQ_DELETE (resolved.vmci_addr); |
72 | | } |
73 | | #endif |
74 | 0 | #if defined ZMQ_HAVE_VSOCK |
75 | 0 | else if (protocol == protocol_name::vsock) { |
76 | 0 | LIBZMQ_DELETE (resolved.vsock_addr); |
77 | 0 | } |
78 | 0 | #endif |
79 | 0 | } |
80 | | |
81 | | int zmq::address_t::to_string (std::string &addr_) const |
82 | 0 | { |
83 | 0 | if (protocol == protocol_name::tcp && resolved.tcp_addr) |
84 | 0 | return resolved.tcp_addr->to_string (addr_); |
85 | 0 | if (protocol == protocol_name::udp && resolved.udp_addr) |
86 | 0 | return resolved.udp_addr->to_string (addr_); |
87 | 0 | #ifdef ZMQ_HAVE_WS |
88 | 0 | if (protocol == protocol_name::ws && resolved.ws_addr) |
89 | 0 | return resolved.ws_addr->to_string (addr_); |
90 | 0 | #endif |
91 | | #ifdef ZMQ_HAVE_WSS |
92 | | if (protocol == protocol_name::wss && resolved.ws_addr) |
93 | | return resolved.ws_addr->to_string (addr_); |
94 | | #endif |
95 | 0 | #if defined ZMQ_HAVE_IPC |
96 | 0 | if (protocol == protocol_name::ipc && resolved.ipc_addr) |
97 | 0 | return resolved.ipc_addr->to_string (addr_); |
98 | 0 | #endif |
99 | 0 | #if defined ZMQ_HAVE_TIPC |
100 | 0 | if (protocol == protocol_name::tipc && resolved.tipc_addr) |
101 | 0 | return resolved.tipc_addr->to_string (addr_); |
102 | 0 | #endif |
103 | | #if defined ZMQ_HAVE_VMCI |
104 | | if (protocol == protocol_name::vmci && resolved.vmci_addr) |
105 | | return resolved.vmci_addr->to_string (addr_); |
106 | | #endif |
107 | 0 | #if defined ZMQ_HAVE_VSOCK |
108 | 0 | if (protocol == protocol_name::vsock && resolved.vsock_addr) |
109 | 0 | return resolved.vsock_addr->to_string (addr_); |
110 | 0 | #endif |
111 | | |
112 | 0 | if (!protocol.empty () && !address.empty ()) { |
113 | 0 | std::stringstream s; |
114 | 0 | s << protocol << "://" << address; |
115 | 0 | addr_ = s.str (); |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | addr_.clear (); |
119 | 0 | return -1; |
120 | 0 | } |
121 | | |
122 | | zmq::zmq_socklen_t zmq::get_socket_address (fd_t fd_, |
123 | | socket_end_t socket_end_, |
124 | | sockaddr_storage *ss_) |
125 | 0 | { |
126 | 0 | zmq_socklen_t sl = static_cast<zmq_socklen_t> (sizeof (*ss_)); |
127 | |
|
128 | 0 | const int rc = |
129 | 0 | socket_end_ == socket_end_local |
130 | 0 | ? getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl) |
131 | 0 | : getpeername (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl); |
132 | |
|
133 | 0 | return rc != 0 ? 0 : sl; |
134 | 0 | } |