henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/socketaddress.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 12 | #include "rtc_base/numerics/safe_conversions.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
| 14 | #if defined(WEBRTC_POSIX) |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <netinet/in.h> |
| 18 | #if defined(OPENBSD) |
| 19 | #include <netinet/in_systm.h> |
| 20 | #endif |
| 21 | #if !defined(__native_client__) |
| 22 | #include <netinet/ip.h> |
| 23 | #endif |
| 24 | #include <arpa/inet.h> |
| 25 | #include <netdb.h> |
| 26 | #include <unistd.h> |
| 27 | #endif |
| 28 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/byteorder.h" |
| 30 | #include "rtc_base/checks.h" |
| 31 | #include "rtc_base/logging.h" |
| 32 | #include "rtc_base/nethelpers.h" |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 33 | #include "rtc_base/strings/string_builder.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | |
| 35 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "rtc_base/win32.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | namespace rtc { |
| 40 | |
| 41 | SocketAddress::SocketAddress() { |
| 42 | Clear(); |
| 43 | } |
| 44 | |
| 45 | SocketAddress::SocketAddress(const std::string& hostname, int port) { |
| 46 | SetIP(hostname); |
| 47 | SetPort(port); |
| 48 | } |
| 49 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 50 | SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | SetIP(IPAddress(ip_as_host_order_integer)); |
| 52 | SetPort(port); |
| 53 | } |
| 54 | |
| 55 | SocketAddress::SocketAddress(const IPAddress& ip, int port) { |
| 56 | SetIP(ip); |
| 57 | SetPort(port); |
| 58 | } |
| 59 | |
| 60 | SocketAddress::SocketAddress(const SocketAddress& addr) { |
| 61 | this->operator=(addr); |
| 62 | } |
| 63 | |
| 64 | void SocketAddress::Clear() { |
| 65 | hostname_.clear(); |
| 66 | literal_ = false; |
| 67 | ip_ = IPAddress(); |
| 68 | port_ = 0; |
| 69 | scope_id_ = 0; |
| 70 | } |
| 71 | |
| 72 | bool SocketAddress::IsNil() const { |
| 73 | return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_; |
| 74 | } |
| 75 | |
| 76 | bool SocketAddress::IsComplete() const { |
| 77 | return (!IPIsAny(ip_)) && (0 != port_); |
| 78 | } |
| 79 | |
| 80 | SocketAddress& SocketAddress::operator=(const SocketAddress& addr) { |
| 81 | hostname_ = addr.hostname_; |
| 82 | ip_ = addr.ip_; |
| 83 | port_ = addr.port_; |
| 84 | literal_ = addr.literal_; |
| 85 | scope_id_ = addr.scope_id_; |
| 86 | return *this; |
| 87 | } |
| 88 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 89 | void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | hostname_.clear(); |
| 91 | literal_ = false; |
| 92 | ip_ = IPAddress(ip_as_host_order_integer); |
| 93 | scope_id_ = 0; |
| 94 | } |
| 95 | |
| 96 | void SocketAddress::SetIP(const IPAddress& ip) { |
| 97 | hostname_.clear(); |
| 98 | literal_ = false; |
| 99 | ip_ = ip; |
| 100 | scope_id_ = 0; |
| 101 | } |
| 102 | |
| 103 | void SocketAddress::SetIP(const std::string& hostname) { |
| 104 | hostname_ = hostname; |
| 105 | literal_ = IPFromString(hostname, &ip_); |
| 106 | if (!literal_) { |
| 107 | ip_ = IPAddress(); |
| 108 | } |
| 109 | scope_id_ = 0; |
| 110 | } |
| 111 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 112 | void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | ip_ = IPAddress(ip_as_host_order_integer); |
| 114 | scope_id_ = 0; |
| 115 | } |
| 116 | |
| 117 | void SocketAddress::SetResolvedIP(const IPAddress& ip) { |
| 118 | ip_ = ip; |
| 119 | scope_id_ = 0; |
| 120 | } |
| 121 | |
| 122 | void SocketAddress::SetPort(int port) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 123 | port_ = rtc::dchecked_cast<uint16_t>(port); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 126 | uint32_t SocketAddress::ip() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | return ip_.v4AddressAsHostOrderInteger(); |
| 128 | } |
| 129 | |
| 130 | const IPAddress& SocketAddress::ipaddr() const { |
| 131 | return ip_; |
| 132 | } |
| 133 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 134 | uint16_t SocketAddress::port() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | return port_; |
| 136 | } |
| 137 | |
| 138 | std::string SocketAddress::HostAsURIString() const { |
| 139 | // If the hostname was a literal IP string, it may need to have square |
| 140 | // brackets added (for SocketAddress::ToString()). |
| 141 | if (!literal_ && !hostname_.empty()) |
| 142 | return hostname_; |
| 143 | if (ip_.family() == AF_INET6) { |
| 144 | return "[" + ip_.ToString() + "]"; |
| 145 | } else { |
| 146 | return ip_.ToString(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | std::string SocketAddress::HostAsSensitiveURIString() const { |
| 151 | // If the hostname was a literal IP string, it may need to have square |
| 152 | // brackets added (for SocketAddress::ToString()). |
| 153 | if (!literal_ && !hostname_.empty()) |
| 154 | return hostname_; |
| 155 | if (ip_.family() == AF_INET6) { |
| 156 | return "[" + ip_.ToSensitiveString() + "]"; |
| 157 | } else { |
| 158 | return ip_.ToSensitiveString(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | std::string SocketAddress::PortAsString() const { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 163 | return std::to_string(port_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | std::string SocketAddress::ToString() const { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 167 | char buf[1024]; |
| 168 | rtc::SimpleStringBuilder sb(buf); |
| 169 | sb << HostAsURIString() << ":" << port(); |
| 170 | return sb.str(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | std::string SocketAddress::ToSensitiveString() const { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 174 | char buf[1024]; |
| 175 | rtc::SimpleStringBuilder sb(buf); |
| 176 | sb << HostAsSensitiveURIString() << ":" << port(); |
| 177 | return sb.str(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool SocketAddress::FromString(const std::string& str) { |
| 181 | if (str.at(0) == '[') { |
| 182 | std::string::size_type closebracket = str.rfind(']'); |
| 183 | if (closebracket != std::string::npos) { |
| 184 | std::string::size_type colon = str.find(':', closebracket); |
| 185 | if (colon != std::string::npos && colon > closebracket) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 186 | SetPort(strtoul(str.substr(colon + 1).c_str(), nullptr, 10)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | SetIP(str.substr(1, closebracket - 1)); |
| 188 | } else { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | } else { |
| 193 | std::string::size_type pos = str.find(':'); |
| 194 | if (std::string::npos == pos) |
| 195 | return false; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 196 | SetPort(strtoul(str.substr(pos + 1).c_str(), nullptr, 10)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 197 | SetIP(str.substr(0, pos)); |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | bool SocketAddress::IsAnyIP() const { |
| 203 | return IPIsAny(ip_); |
| 204 | } |
| 205 | |
| 206 | bool SocketAddress::IsLoopbackIP() const { |
| 207 | return IPIsLoopback(ip_) || (IPIsAny(ip_) && |
| 208 | 0 == strcmp(hostname_.c_str(), "localhost")); |
| 209 | } |
| 210 | |
| 211 | bool SocketAddress::IsPrivateIP() const { |
| 212 | return IPIsPrivate(ip_); |
| 213 | } |
| 214 | |
| 215 | bool SocketAddress::IsUnresolvedIP() const { |
| 216 | return IPIsUnspec(ip_) && !literal_ && !hostname_.empty(); |
| 217 | } |
| 218 | |
| 219 | bool SocketAddress::operator==(const SocketAddress& addr) const { |
| 220 | return EqualIPs(addr) && EqualPorts(addr); |
| 221 | } |
| 222 | |
| 223 | bool SocketAddress::operator<(const SocketAddress& addr) const { |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 224 | if (ip_ != addr.ip_) |
| 225 | return ip_ < addr.ip_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 226 | |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 227 | // We only check hostnames if both IPs are ANY or unspecified. This matches |
| 228 | // EqualIPs(). |
| 229 | if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_) |
| 230 | return hostname_ < addr.hostname_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 231 | |
| 232 | return port_ < addr.port_; |
| 233 | } |
| 234 | |
| 235 | bool SocketAddress::EqualIPs(const SocketAddress& addr) const { |
| 236 | return (ip_ == addr.ip_) && |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 237 | ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool SocketAddress::EqualPorts(const SocketAddress& addr) const { |
| 241 | return (port_ == addr.port_); |
| 242 | } |
| 243 | |
| 244 | size_t SocketAddress::Hash() const { |
| 245 | size_t h = 0; |
| 246 | h ^= HashIP(ip_); |
| 247 | h ^= port_ | (port_ << 16); |
| 248 | return h; |
| 249 | } |
| 250 | |
| 251 | void SocketAddress::ToSockAddr(sockaddr_in* saddr) const { |
| 252 | memset(saddr, 0, sizeof(*saddr)); |
| 253 | if (ip_.family() != AF_INET) { |
| 254 | saddr->sin_family = AF_UNSPEC; |
| 255 | return; |
| 256 | } |
| 257 | saddr->sin_family = AF_INET; |
| 258 | saddr->sin_port = HostToNetwork16(port_); |
| 259 | if (IPIsAny(ip_)) { |
| 260 | saddr->sin_addr.s_addr = INADDR_ANY; |
| 261 | } else { |
| 262 | saddr->sin_addr = ip_.ipv4_address(); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) { |
| 267 | if (saddr.sin_family != AF_INET) |
| 268 | return false; |
| 269 | SetIP(NetworkToHost32(saddr.sin_addr.s_addr)); |
| 270 | SetPort(NetworkToHost16(saddr.sin_port)); |
| 271 | literal_ = false; |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | static size_t ToSockAddrStorageHelper(sockaddr_storage* addr, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 276 | IPAddress ip, |
| 277 | uint16_t port, |
| 278 | int scope_id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 279 | memset(addr, 0, sizeof(sockaddr_storage)); |
henrike@webrtc.org | 1711104 | 2014-09-10 22:10:24 +0000 | [diff] [blame] | 280 | addr->ss_family = static_cast<unsigned short>(ip.family()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 281 | if (addr->ss_family == AF_INET6) { |
| 282 | sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr); |
| 283 | saddr->sin6_addr = ip.ipv6_address(); |
| 284 | saddr->sin6_port = HostToNetwork16(port); |
| 285 | saddr->sin6_scope_id = scope_id; |
| 286 | return sizeof(sockaddr_in6); |
| 287 | } else if (addr->ss_family == AF_INET) { |
| 288 | sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr); |
| 289 | saddr->sin_addr = ip.ipv4_address(); |
| 290 | saddr->sin_port = HostToNetwork16(port); |
| 291 | return sizeof(sockaddr_in); |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const { |
| 297 | return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_); |
| 298 | } |
| 299 | |
| 300 | size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const { |
| 301 | return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_); |
| 302 | } |
| 303 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr, |
| 305 | SocketAddress* out) { |
| 306 | if (!out) { |
| 307 | return false; |
| 308 | } |
| 309 | if (addr.ss_family == AF_INET) { |
| 310 | const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr); |
| 311 | *out = SocketAddress(IPAddress(saddr->sin_addr), |
| 312 | NetworkToHost16(saddr->sin_port)); |
| 313 | return true; |
| 314 | } else if (addr.ss_family == AF_INET6) { |
| 315 | const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr); |
| 316 | *out = SocketAddress(IPAddress(saddr->sin6_addr), |
| 317 | NetworkToHost16(saddr->sin6_port)); |
| 318 | out->SetScopeID(saddr->sin6_scope_id); |
| 319 | return true; |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | SocketAddress EmptySocketAddressWithFamily(int family) { |
| 325 | if (family == AF_INET) { |
| 326 | return SocketAddress(IPAddress(INADDR_ANY), 0); |
| 327 | } else if (family == AF_INET6) { |
| 328 | return SocketAddress(IPAddress(in6addr_any), 0); |
| 329 | } |
| 330 | return SocketAddress(); |
| 331 | } |
| 332 | |
| 333 | } // namespace rtc |