henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/base/socketaddress.h" |
| 29 | |
| 30 | #ifdef POSIX |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #if defined(OPENBSD) |
| 35 | #include <netinet/in_systm.h> |
| 36 | #endif |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 37 | #if !defined(__native_client__) |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | #include <netinet/ip.h> |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 39 | #endif |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | #include <arpa/inet.h> |
| 41 | #include <netdb.h> |
| 42 | #include <unistd.h> |
| 43 | #endif |
| 44 | |
| 45 | #include <sstream> |
| 46 | |
| 47 | #include "talk/base/byteorder.h" |
| 48 | #include "talk/base/common.h" |
| 49 | #include "talk/base/logging.h" |
| 50 | #include "talk/base/nethelpers.h" |
| 51 | |
| 52 | #ifdef WIN32 |
| 53 | #include "talk/base/win32.h" |
| 54 | #endif |
| 55 | |
| 56 | namespace talk_base { |
| 57 | |
| 58 | SocketAddress::SocketAddress() { |
| 59 | Clear(); |
| 60 | } |
| 61 | |
| 62 | SocketAddress::SocketAddress(const std::string& hostname, int port) { |
| 63 | SetIP(hostname); |
| 64 | SetPort(port); |
| 65 | } |
| 66 | |
| 67 | SocketAddress::SocketAddress(uint32 ip_as_host_order_integer, int port) { |
| 68 | SetIP(IPAddress(ip_as_host_order_integer)); |
| 69 | SetPort(port); |
| 70 | } |
| 71 | |
| 72 | SocketAddress::SocketAddress(const IPAddress& ip, int port) { |
| 73 | SetIP(ip); |
| 74 | SetPort(port); |
| 75 | } |
| 76 | |
| 77 | SocketAddress::SocketAddress(const SocketAddress& addr) { |
| 78 | this->operator=(addr); |
| 79 | } |
| 80 | |
| 81 | void SocketAddress::Clear() { |
| 82 | hostname_.clear(); |
| 83 | literal_ = false; |
| 84 | ip_ = IPAddress(); |
| 85 | port_ = 0; |
| 86 | scope_id_ = 0; |
| 87 | } |
| 88 | |
| 89 | bool SocketAddress::IsNil() const { |
| 90 | return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_; |
| 91 | } |
| 92 | |
| 93 | bool SocketAddress::IsComplete() const { |
| 94 | return (!IPIsAny(ip_)) && (0 != port_); |
| 95 | } |
| 96 | |
| 97 | SocketAddress& SocketAddress::operator=(const SocketAddress& addr) { |
| 98 | hostname_ = addr.hostname_; |
| 99 | ip_ = addr.ip_; |
| 100 | port_ = addr.port_; |
| 101 | literal_ = addr.literal_; |
| 102 | scope_id_ = addr.scope_id_; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | void SocketAddress::SetIP(uint32 ip_as_host_order_integer) { |
| 107 | hostname_.clear(); |
| 108 | literal_ = false; |
| 109 | ip_ = IPAddress(ip_as_host_order_integer); |
| 110 | scope_id_ = 0; |
| 111 | } |
| 112 | |
| 113 | void SocketAddress::SetIP(const IPAddress& ip) { |
| 114 | hostname_.clear(); |
| 115 | literal_ = false; |
| 116 | ip_ = ip; |
| 117 | scope_id_ = 0; |
| 118 | } |
| 119 | |
| 120 | void SocketAddress::SetIP(const std::string& hostname) { |
| 121 | hostname_ = hostname; |
| 122 | literal_ = IPFromString(hostname, &ip_); |
| 123 | if (!literal_) { |
| 124 | ip_ = IPAddress(); |
| 125 | } |
| 126 | scope_id_ = 0; |
| 127 | } |
| 128 | |
| 129 | void SocketAddress::SetResolvedIP(uint32 ip_as_host_order_integer) { |
| 130 | ip_ = IPAddress(ip_as_host_order_integer); |
| 131 | scope_id_ = 0; |
| 132 | } |
| 133 | |
| 134 | void SocketAddress::SetResolvedIP(const IPAddress& ip) { |
| 135 | ip_ = ip; |
| 136 | scope_id_ = 0; |
| 137 | } |
| 138 | |
| 139 | void SocketAddress::SetPort(int port) { |
| 140 | ASSERT((0 <= port) && (port < 65536)); |
| 141 | port_ = port; |
| 142 | } |
| 143 | |
| 144 | uint32 SocketAddress::ip() const { |
| 145 | return ip_.v4AddressAsHostOrderInteger(); |
| 146 | } |
| 147 | |
| 148 | const IPAddress& SocketAddress::ipaddr() const { |
| 149 | return ip_; |
| 150 | } |
| 151 | |
| 152 | uint16 SocketAddress::port() const { |
| 153 | return port_; |
| 154 | } |
| 155 | |
| 156 | std::string SocketAddress::HostAsURIString() const { |
| 157 | // If the hostname was a literal IP string, it may need to have square |
| 158 | // brackets added (for SocketAddress::ToString()). |
| 159 | if (!literal_ && !hostname_.empty()) |
| 160 | return hostname_; |
| 161 | if (ip_.family() == AF_INET6) { |
| 162 | return "[" + ip_.ToString() + "]"; |
| 163 | } else { |
| 164 | return ip_.ToString(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | std::string SocketAddress::HostAsSensitiveURIString() const { |
| 169 | // If the hostname was a literal IP string, it may need to have square |
| 170 | // brackets added (for SocketAddress::ToString()). |
| 171 | if (!literal_ && !hostname_.empty()) |
| 172 | return hostname_; |
| 173 | if (ip_.family() == AF_INET6) { |
| 174 | return "[" + ip_.ToSensitiveString() + "]"; |
| 175 | } else { |
| 176 | return ip_.ToSensitiveString(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | std::string SocketAddress::PortAsString() const { |
| 181 | std::ostringstream ost; |
| 182 | ost << port_; |
| 183 | return ost.str(); |
| 184 | } |
| 185 | |
| 186 | std::string SocketAddress::ToString() const { |
| 187 | std::ostringstream ost; |
| 188 | ost << *this; |
| 189 | return ost.str(); |
| 190 | } |
| 191 | |
| 192 | std::string SocketAddress::ToSensitiveString() const { |
| 193 | std::ostringstream ost; |
| 194 | ost << HostAsSensitiveURIString() << ":" << port(); |
| 195 | return ost.str(); |
| 196 | } |
| 197 | |
| 198 | bool SocketAddress::FromString(const std::string& str) { |
| 199 | if (str.at(0) == '[') { |
| 200 | std::string::size_type closebracket = str.rfind(']'); |
| 201 | if (closebracket != std::string::npos) { |
| 202 | std::string::size_type colon = str.find(':', closebracket); |
| 203 | if (colon != std::string::npos && colon > closebracket) { |
| 204 | SetPort(strtoul(str.substr(colon + 1).c_str(), NULL, 10)); |
| 205 | SetIP(str.substr(1, closebracket - 1)); |
| 206 | } else { |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | } else { |
| 211 | std::string::size_type pos = str.find(':'); |
| 212 | if (std::string::npos == pos) |
| 213 | return false; |
| 214 | SetPort(strtoul(str.substr(pos + 1).c_str(), NULL, 10)); |
| 215 | SetIP(str.substr(0, pos)); |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | std::ostream& operator<<(std::ostream& os, const SocketAddress& addr) { |
| 221 | os << addr.HostAsURIString() << ":" << addr.port(); |
| 222 | return os; |
| 223 | } |
| 224 | |
| 225 | bool SocketAddress::IsAnyIP() const { |
| 226 | return IPIsAny(ip_); |
| 227 | } |
| 228 | |
| 229 | bool SocketAddress::IsLoopbackIP() const { |
| 230 | return IPIsLoopback(ip_) || (IPIsAny(ip_) && |
| 231 | 0 == strcmp(hostname_.c_str(), "localhost")); |
| 232 | } |
| 233 | |
| 234 | bool SocketAddress::IsPrivateIP() const { |
| 235 | return IPIsPrivate(ip_); |
| 236 | } |
| 237 | |
| 238 | bool SocketAddress::IsUnresolvedIP() const { |
| 239 | return IPIsUnspec(ip_) && !literal_ && !hostname_.empty(); |
| 240 | } |
| 241 | |
| 242 | bool SocketAddress::operator==(const SocketAddress& addr) const { |
| 243 | return EqualIPs(addr) && EqualPorts(addr); |
| 244 | } |
| 245 | |
| 246 | bool SocketAddress::operator<(const SocketAddress& addr) const { |
| 247 | if (ip_ < addr.ip_) |
| 248 | return true; |
| 249 | else if (addr.ip_ < ip_) |
| 250 | return false; |
| 251 | |
| 252 | // We only check hostnames if both IPs are zero. This matches EqualIPs() |
| 253 | if (addr.IsAnyIP()) { |
| 254 | if (hostname_ < addr.hostname_) |
| 255 | return true; |
| 256 | else if (addr.hostname_ < hostname_) |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | return port_ < addr.port_; |
| 261 | } |
| 262 | |
| 263 | bool SocketAddress::EqualIPs(const SocketAddress& addr) const { |
| 264 | return (ip_ == addr.ip_) && |
| 265 | ((!IPIsAny(ip_)) || (hostname_ == addr.hostname_)); |
| 266 | } |
| 267 | |
| 268 | bool SocketAddress::EqualPorts(const SocketAddress& addr) const { |
| 269 | return (port_ == addr.port_); |
| 270 | } |
| 271 | |
| 272 | size_t SocketAddress::Hash() const { |
| 273 | size_t h = 0; |
| 274 | h ^= HashIP(ip_); |
| 275 | h ^= port_ | (port_ << 16); |
| 276 | return h; |
| 277 | } |
| 278 | |
| 279 | void SocketAddress::ToSockAddr(sockaddr_in* saddr) const { |
| 280 | memset(saddr, 0, sizeof(*saddr)); |
| 281 | if (ip_.family() != AF_INET) { |
| 282 | saddr->sin_family = AF_UNSPEC; |
| 283 | return; |
| 284 | } |
| 285 | saddr->sin_family = AF_INET; |
| 286 | saddr->sin_port = HostToNetwork16(port_); |
| 287 | if (IPIsAny(ip_)) { |
| 288 | saddr->sin_addr.s_addr = INADDR_ANY; |
| 289 | } else { |
| 290 | saddr->sin_addr = ip_.ipv4_address(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) { |
| 295 | if (saddr.sin_family != AF_INET) |
| 296 | return false; |
| 297 | SetIP(NetworkToHost32(saddr.sin_addr.s_addr)); |
| 298 | SetPort(NetworkToHost16(saddr.sin_port)); |
| 299 | literal_ = false; |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | static size_t ToSockAddrStorageHelper(sockaddr_storage* addr, |
| 304 | IPAddress ip, int port, int scope_id) { |
| 305 | memset(addr, 0, sizeof(sockaddr_storage)); |
| 306 | addr->ss_family = ip.family(); |
| 307 | if (addr->ss_family == AF_INET6) { |
| 308 | sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr); |
| 309 | saddr->sin6_addr = ip.ipv6_address(); |
| 310 | saddr->sin6_port = HostToNetwork16(port); |
| 311 | saddr->sin6_scope_id = scope_id; |
| 312 | return sizeof(sockaddr_in6); |
| 313 | } else if (addr->ss_family == AF_INET) { |
| 314 | sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr); |
| 315 | saddr->sin_addr = ip.ipv4_address(); |
| 316 | saddr->sin_port = HostToNetwork16(port); |
| 317 | return sizeof(sockaddr_in); |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const { |
| 323 | return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_); |
| 324 | } |
| 325 | |
| 326 | size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const { |
| 327 | return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_); |
| 328 | } |
| 329 | |
| 330 | std::string SocketAddress::IPToString(uint32 ip_as_host_order_integer) { |
| 331 | return IPAddress(ip_as_host_order_integer).ToString(); |
| 332 | } |
| 333 | |
| 334 | std::string IPToSensitiveString(uint32 ip_as_host_order_integer) { |
| 335 | return IPAddress(ip_as_host_order_integer).ToSensitiveString(); |
| 336 | } |
| 337 | |
| 338 | bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) { |
| 339 | in_addr addr; |
| 340 | if (talk_base::inet_pton(AF_INET, hostname.c_str(), &addr) == 0) |
| 341 | return false; |
| 342 | *ip = NetworkToHost32(addr.s_addr); |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) { |
| 347 | in_addr addr4; |
| 348 | if (talk_base::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) { |
| 349 | if (ip) { |
| 350 | *ip = IPAddress(addr4); |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | in6_addr addr6; |
| 356 | if (talk_base::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) { |
| 357 | if (ip) { |
| 358 | *ip = IPAddress(addr6); |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | uint32 SocketAddress::StringToIP(const std::string& hostname) { |
| 366 | uint32 ip = 0; |
| 367 | StringToIP(hostname, &ip); |
| 368 | return ip; |
| 369 | } |
| 370 | |
| 371 | bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr, |
| 372 | SocketAddress* out) { |
| 373 | if (!out) { |
| 374 | return false; |
| 375 | } |
| 376 | if (addr.ss_family == AF_INET) { |
| 377 | const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr); |
| 378 | *out = SocketAddress(IPAddress(saddr->sin_addr), |
| 379 | NetworkToHost16(saddr->sin_port)); |
| 380 | return true; |
| 381 | } else if (addr.ss_family == AF_INET6) { |
| 382 | const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr); |
| 383 | *out = SocketAddress(IPAddress(saddr->sin6_addr), |
| 384 | NetworkToHost16(saddr->sin6_port)); |
| 385 | out->SetScopeID(saddr->sin6_scope_id); |
| 386 | return true; |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | SocketAddress EmptySocketAddressWithFamily(int family) { |
| 392 | if (family == AF_INET) { |
| 393 | return SocketAddress(IPAddress(INADDR_ANY), 0); |
| 394 | } else if (family == AF_INET6) { |
| 395 | return SocketAddress(IPAddress(in6addr_any), 0); |
| 396 | } |
| 397 | return SocketAddress(); |
| 398 | } |
| 399 | |
| 400 | } // namespace talk_base |