| 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 | #ifndef __ZMQ_ADDRESS_HPP_INCLUDED__ |
| 31 | #define __ZMQ_ADDRESS_HPP_INCLUDED__ |
| 32 | |
| 33 | #include "fd.hpp" |
| 34 | |
| 35 | #include <string> |
| 36 | |
| 37 | #ifndef ZMQ_HAVE_WINDOWS |
| 38 | #include <sys/socket.h> |
| 39 | #else |
| 40 | #include <ws2tcpip.h> |
| 41 | #endif |
| 42 | |
| 43 | namespace zmq |
| 44 | { |
| 45 | class ctx_t; |
| 46 | class tcp_address_t; |
| 47 | 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; |
| 55 | #endif |
| 56 | #if defined ZMQ_HAVE_VMCI |
| 57 | class vmci_address_t; |
| 58 | #endif |
| 59 | |
| 60 | namespace protocol_name |
| 61 | { |
| 62 | static const char inproc[] = "inproc" ; |
| 63 | static const char tcp[] = "tcp" ; |
| 64 | static const char udp[] = "udp" ; |
| 65 | #ifdef ZMQ_HAVE_WS |
| 66 | static const char ws[] = "ws" ; |
| 67 | #endif |
| 68 | #ifdef ZMQ_HAVE_WSS |
| 69 | static const char wss[] = "wss" ; |
| 70 | #endif |
| 71 | #if defined ZMQ_HAVE_IPC |
| 72 | static const char ipc[] = "ipc" ; |
| 73 | #endif |
| 74 | #if defined ZMQ_HAVE_TIPC |
| 75 | static const char tipc[] = "tipc" ; |
| 76 | #endif |
| 77 | #if defined ZMQ_HAVE_VMCI |
| 78 | static const char vmci[] = "vmci" ; |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | struct address_t |
| 83 | { |
| 84 | address_t (const std::string &protocol_, |
| 85 | const std::string &address_, |
| 86 | ctx_t *parent_); |
| 87 | |
| 88 | ~address_t (); |
| 89 | |
| 90 | const std::string protocol; |
| 91 | 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 | union |
| 97 | { |
| 98 | void *dummy; |
| 99 | tcp_address_t *tcp_addr; |
| 100 | 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; |
| 110 | #endif |
| 111 | #if defined ZMQ_HAVE_VMCI |
| 112 | vmci_address_t *vmci_addr; |
| 113 | #endif |
| 114 | } resolved; |
| 115 | |
| 116 | int to_string (std::string &addr_) const; |
| 117 | }; |
| 118 | |
| 119 | #if defined(ZMQ_HAVE_HPUX) || defined(ZMQ_HAVE_VXWORKS) \ |
| 120 | || defined(ZMQ_HAVE_WINDOWS) |
| 121 | typedef int zmq_socklen_t; |
| 122 | #else |
| 123 | typedef socklen_t zmq_socklen_t; |
| 124 | #endif |
| 125 | |
| 126 | enum socket_end_t |
| 127 | { |
| 128 | socket_end_local, |
| 129 | socket_end_remote |
| 130 | }; |
| 131 | |
| 132 | zmq_socklen_t |
| 133 | get_socket_address (fd_t fd_, socket_end_t socket_end_, sockaddr_storage *ss_); |
| 134 | |
| 135 | template <typename T> |
| 136 | std::string get_socket_name (fd_t fd_, socket_end_t socket_end_) |
| 137 | { |
| 138 | struct sockaddr_storage ss; |
| 139 | const zmq_socklen_t sl = get_socket_address (fd_, socket_end_, &ss); |
| 140 | if (sl == 0) { |
| 141 | return std::string (); |
| 142 | } |
| 143 | |
| 144 | const T addr (reinterpret_cast<struct sockaddr *> (&ss), sl); |
| 145 | std::string address_string; |
| 146 | addr.to_string (address_string); |
| 147 | return address_string; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #endif |
| 152 | |