| 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 <string> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "macros.hpp" |
| 35 | #include "ws_address.hpp" |
| 36 | #include "stdint.hpp" |
| 37 | #include "err.hpp" |
| 38 | #include "ip.hpp" |
| 39 | |
| 40 | #ifndef ZMQ_HAVE_WINDOWS |
| 41 | #include <sys/types.h> |
| 42 | #include <arpa/inet.h> |
| 43 | #include <netinet/tcp.h> |
| 44 | #include <net/if.h> |
| 45 | #include <netdb.h> |
| 46 | #include <ctype.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdlib.h> |
| 49 | #endif |
| 50 | |
| 51 | #include <limits.h> |
| 52 | |
| 53 | zmq::ws_address_t::ws_address_t () |
| 54 | { |
| 55 | memset (&_address, 0, sizeof (_address)); |
| 56 | } |
| 57 | |
| 58 | zmq::ws_address_t::ws_address_t (const sockaddr *sa_, socklen_t sa_len_) |
| 59 | { |
| 60 | zmq_assert (sa_ && sa_len_ > 0); |
| 61 | |
| 62 | memset (&_address, 0, sizeof (_address)); |
| 63 | if (sa_->sa_family == AF_INET |
| 64 | && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv4))) |
| 65 | memcpy (&_address.ipv4, sa_, sizeof (_address.ipv4)); |
| 66 | else if (sa_->sa_family == AF_INET6 |
| 67 | && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv6))) |
| 68 | memcpy (&_address.ipv6, sa_, sizeof (_address.ipv6)); |
| 69 | |
| 70 | _path = std::string ("/" ); |
| 71 | |
| 72 | char hbuf[NI_MAXHOST]; |
| 73 | const int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, |
| 74 | 0, NI_NUMERICHOST); |
| 75 | if (rc != 0) |
| 76 | _host = std::string ("localhost" ); |
| 77 | |
| 78 | std::ostringstream os; |
| 79 | |
| 80 | if (_address.family () == AF_INET6) |
| 81 | os << std::string ("[" ); |
| 82 | |
| 83 | os << std::string (hbuf); |
| 84 | |
| 85 | if (_address.family () == AF_INET6) |
| 86 | os << std::string ("]" ); |
| 87 | |
| 88 | _host = os.str (); |
| 89 | } |
| 90 | |
| 91 | int zmq::ws_address_t::resolve (const char *name_, bool local_, bool ipv6_) |
| 92 | { |
| 93 | // find the host part, It's important to use str*r*chr to only get |
| 94 | // the latest colon since IPv6 addresses use colons as delemiters. |
| 95 | const char *delim = strrchr (name_, ':'); |
| 96 | if (delim == NULL) { |
| 97 | errno = EINVAL; |
| 98 | return -1; |
| 99 | } |
| 100 | _host = std::string (name_, delim - name_); |
| 101 | |
| 102 | // find the path part, which is optional |
| 103 | delim = strrchr (name_, '/'); |
| 104 | if (delim) |
| 105 | _path = std::string (delim); |
| 106 | else |
| 107 | _path = std::string ("/" ); |
| 108 | // remove the path, otherwise resolving the port will fail with wildcard |
| 109 | std::string host_port = std::string (name_, delim - name_); |
| 110 | |
| 111 | ip_resolver_options_t resolver_opts; |
| 112 | resolver_opts.bindable (local_) |
| 113 | .allow_dns (!local_) |
| 114 | .allow_nic_name (local_) |
| 115 | .ipv6 (ipv6_) |
| 116 | .allow_path (true) |
| 117 | .expect_port (true); |
| 118 | |
| 119 | ip_resolver_t resolver (resolver_opts); |
| 120 | |
| 121 | return resolver.resolve (&_address, host_port.c_str ()); |
| 122 | } |
| 123 | |
| 124 | int zmq::ws_address_t::to_string (std::string &addr_) const |
| 125 | { |
| 126 | std::ostringstream os; |
| 127 | os << std::string ("ws://" ) << host () << std::string (":" ) |
| 128 | << _address.port (); |
| 129 | addr_ = os.str (); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | const sockaddr *zmq::ws_address_t::addr () const |
| 135 | { |
| 136 | return _address.as_sockaddr (); |
| 137 | } |
| 138 | |
| 139 | socklen_t zmq::ws_address_t::addrlen () const |
| 140 | { |
| 141 | return _address.sockaddr_len (); |
| 142 | } |
| 143 | |
| 144 | const char *zmq::ws_address_t::host () const |
| 145 | { |
| 146 | return _host.c_str (); |
| 147 | } |
| 148 | |
| 149 | const char *zmq::ws_address_t::path () const |
| 150 | { |
| 151 | return _path.c_str (); |
| 152 | } |
| 153 | |
| 154 | #if defined ZMQ_HAVE_WINDOWS |
| 155 | unsigned short zmq::ws_address_t::family () const |
| 156 | #else |
| 157 | sa_family_t zmq::ws_address_t::family () const |
| 158 | #endif |
| 159 | { |
| 160 | return _address.family (); |
| 161 | } |
| 162 | |