| 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 |
| 11 | #if defined(WEBRTC_POSIX) |
| 12 | #include <netinet/in.h> |
| 13 | #include <sys/socket.h> |
| 14 |
| 15 | #include "absl/strings/string_view.h" |
| 16 | #ifdef OPENBSD |
| 17 | #include <netinet/in_systm.h> |
| 18 | #endif |
| 19 | #ifndef __native_client__ |
| 20 | #include <netinet/ip.h> |
| 21 | #endif |
| 22 | #include <netdb.h> |
| 23 | #endif |
| 24 |
| 25 | #include "rtc_base/byte_order.h" |
| 26 | #include "rtc_base/ip_address.h" |
| 27 | #include "rtc_base/net_helpers.h" |
| 28 | #include "rtc_base/string_utils.h" |
| 29 |
| 30 | #if defined(WEBRTC_WIN) |
| 31 | #include "rtc_base/win32.h" |
| 32 | #endif // WEBRTC_WIN |
| 33 |
| 34 | namespace rtc { |
| 35 |
| 36 | // Prefixes used for categorizing IPv6 addresses. |
| 37 | static const in6_addr kV4MappedPrefix = { |
| 38 | {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0}}}; |
| 39 | static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}}; |
| 40 | static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}}; |
| 41 | static const in6_addr kV4CompatibilityPrefix = {{{0}}}; |
| 42 | static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}}; |
| 43 | static const in6_addr kPrivateNetworkPrefix = {{{0xFD}}}; |
| 44 |
| 45 | static bool IPIsHelper(const IPAddress& ip, |
| 46 | const in6_addr& tomatch, |
| 47 | int length); |
| 48 | static in_addr ExtractMappedAddress(const in6_addr& addr); |
| 49 |
| 50 | uint32_t IPAddress::v4AddressAsHostOrderInteger() const { |
| 51 | if (family_ == AF_INET) { |
| 52 | return NetworkToHost32(u_.ip4.s_addr); |
| 53 | } else { |
| 54 | return 0; |
| 55 | } |
| 56 | } |
| 57 |
| 58 | int IPAddress::overhead() const { |
| 59 | switch (family_) { |
| 60 | case AF_INET: // IPv4 |
| 61 | return 20; |
| 62 | case AF_INET6: // IPv6 |
| 63 | return 40; |
| 64 | default: |
| 65 | return 0; |
| 66 | } |
| 67 | } |
| 68 |
| 69 | bool IPAddress::IsNil() const { |
| 70 | return IPIsUnspec(*this); |
| 71 | } |
| 72 |
| 73 | size_t IPAddress::Size() const { |
| 74 | switch (family_) { |
| 75 | case AF_INET: |
| 76 | return sizeof(in_addr); |
| 77 | case AF_INET6: |
| 78 | return sizeof(in6_addr); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 |
| 83 | bool IPAddress::operator==(const IPAddress& other) const { |
| 84 | if (family_ != other.family_) { |
| 85 | return false; |
| 86 | } |
| 87 | if (family_ == AF_INET) { |
| 88 | return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0; |
| 89 | } |
| 90 | if (family_ == AF_INET6) { |
| 91 | return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0; |
| 92 | } |
| 93 | return family_ == AF_UNSPEC; |
| 94 | } |
| 95 |
| 96 | bool IPAddress::operator!=(const IPAddress& other) const { |
| 97 | return !((*this) == other); |
| 98 | } |
| 99 |
| 100 | bool IPAddress::operator>(const IPAddress& other) const { |
| 101 | return (*this) != other && !((*this) < other); |
| 102 | } |
| 103 |
| 104 | bool IPAddress::operator<(const IPAddress& other) const { |
| 105 | // IPv4 is 'less than' IPv6 |
| 106 | if (family_ != other.family_) { |
| 107 | if (family_ == AF_UNSPEC) { |
| 108 | return true; |
| 109 | } |
| 110 | if (family_ == AF_INET && other.family_ == AF_INET6) { |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | // Comparing addresses of the same family. |
| 116 | switch (family_) { |
| 117 | case AF_INET: { |
| 118 | return NetworkToHost32(u_.ip4.s_addr) < |
| 119 | NetworkToHost32(other.u_.ip4.s_addr); |
| 120 | } |
| 121 | case AF_INET6: { |
| 122 | return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0; |
| 123 | } |
| 124 | } |
| 125 | // Catches AF_UNSPEC and invalid addresses. |
| 126 | return false; |
| 127 | } |
| 128 |
| 129 | in6_addr IPAddress::ipv6_address() const { |
| 130 | return u_.ip6; |
| 131 | } |
| 132 |
| 133 | in_addr IPAddress::ipv4_address() const { |
| 134 | return u_.ip4; |
| 135 | } |
| 136 |
| 137 | std::string IPAddress::ToString() const { |
| 138 | if (family_ != AF_INET && family_ != AF_INET6) { |
| 139 | return std::string(); |
| 140 | } |
| 141 | char buf[INET6_ADDRSTRLEN] = {0}; |
| 142 | const void* src = &u_.ip4; |
| 143 | if (family_ == AF_INET6) { |
| 144 | src = &u_.ip6; |
| 145 | } |
| 146 | if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) { |
| 147 | return std::string(); |
| 148 | } |
| 149 | return std::string(buf); |
| 150 | } |
| 151 |
| 152 | std::string IPAddress::ToSensitiveString() const { |
| 153 | switch (family_) { |
| 154 | case AF_INET: { |
| 155 | std::string address = ToString(); |
| 156 | size_t find_pos = address.rfind('.'); |
| 157 | if (find_pos == std::string::npos) |
| 158 | return std::string(); |
| 159 | address.resize(find_pos); |
| 160 | address += ".x"; |
| 161 | return address; |
| 162 | } |
| 163 | case AF_INET6: { |
| 164 | std::string result; |
| 165 | result.resize(INET6_ADDRSTRLEN); |
| 166 | in6_addr addr = ipv6_address(); |
| 167 | size_t len = snprintf(&(result[0]), result.size(), "%x:%x:%x:x:x:x:x:x", |
| 168 | (addr.s6_addr[0] << 8) + addr.s6_addr[1], |
| 169 | (addr.s6_addr[2] << 8) + addr.s6_addr[3], |
| 170 | (addr.s6_addr[4] << 8) + addr.s6_addr[5]); |
| 171 | result.resize(len); |
| 172 | return result; |
| 173 | } |
| 174 | } |
| 175 | return std::string(); |
| 176 | } |
| 177 |
| 178 | IPAddress IPAddress::Normalized() const { |
| 179 | if (family_ != AF_INET6) { |
| 180 | return *this; |
| 181 | } |
| 182 | if (!IPIsV4Mapped(*this)) { |
| 183 | return *this; |
| 184 | } |
| 185 | in_addr addr = ExtractMappedAddress(u_.ip6); |
| 186 | return IPAddress(addr); |
| 187 | } |
| 188 |
| 189 | IPAddress IPAddress::AsIPv6Address() const { |
| 190 | if (family_ != AF_INET) { |
| 191 | return *this; |
| 192 | } |
| 193 | in6_addr v6addr = kV4MappedPrefix; |
| 194 | ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr)); |
| 195 | return IPAddress(v6addr); |
| 196 | } |
| 197 |
| 198 | bool InterfaceAddress::operator==(const InterfaceAddress& other) const { |
| 199 | return ipv6_flags_ == other.ipv6_flags() && |
| 200 | static_cast<const IPAddress&>(*this) == other; |
| 201 | } |
| 202 |
| 203 | bool InterfaceAddress::operator!=(const InterfaceAddress& other) const { |
| 204 | return !((*this) == other); |
| 205 | } |
| 206 |
| 207 | const InterfaceAddress& InterfaceAddress::operator=( |
| 208 | const InterfaceAddress& other) { |
| 209 | ipv6_flags_ = other.ipv6_flags_; |
| 210 | static_cast<IPAddress&>(*this) = other; |
| 211 | return *this; |
| 212 | } |
| 213 |
| 214 | std::string InterfaceAddress::ToString() const { |
| 215 | std::string result = IPAddress::ToString(); |
| 216 |
| 217 | if (family() == AF_INET6) |
| 218 | result += "|flags:0x" + rtc::ToHex(ipv6_flags()); |
| 219 |
| 220 | return result; |
| 221 | } |
| 222 |
| 223 | static bool IPIsPrivateNetworkV4(const IPAddress& ip) { |
| 224 | uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger(); |
| 225 | return ((ip_in_host_order >> 24) == 10) || |
| 226 | ((ip_in_host_order >> 20) == ((172 << 4) | 1)) || |
| 227 | ((ip_in_host_order >> 16) == ((192 << 8) | 168)); |
| 228 | } |
| 229 |
| 230 | static bool IPIsPrivateNetworkV6(const IPAddress& ip) { |
| 231 | return IPIsHelper(ip, kPrivateNetworkPrefix, 8); |
| 232 | } |
| 233 |
| 234 | bool IPIsPrivateNetwork(const IPAddress& ip) { |
| 235 | switch (ip.family()) { |
| 236 | case AF_INET: { |
| 237 | return IPIsPrivateNetworkV4(ip); |
| 238 | } |
| 239 | case AF_INET6: { |
| 240 | return IPIsPrivateNetworkV6(ip); |
| 241 | } |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 |
| 246 | static bool IPIsSharedNetworkV4(const IPAddress& ip) { |
| 247 | uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger(); |
| 248 | return (ip_in_host_order >> 22) == ((100 << 2) | 1); |
| 249 | } |
| 250 |
| 251 | bool IPIsSharedNetwork(const IPAddress& ip) { |
| 252 | if (ip.family() == AF_INET) { |
| 253 | return IPIsSharedNetworkV4(ip); |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 |
| 258 | in_addr ExtractMappedAddress(const in6_addr& in6) { |
| 259 | in_addr ipv4; |
| 260 | ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr)); |
| 261 | return ipv4; |
| 262 | } |
| 263 |
| 264 | bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) { |
| 265 | if (!info || !info->ai_addr) { |
| 266 | return false; |
| 267 | } |
| 268 | if (info->ai_addr->sa_family == AF_INET) { |
| 269 | sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr); |
| 270 | *out = IPAddress(addr->sin_addr); |
| 271 | return true; |
| 272 | } else if (info->ai_addr->sa_family == AF_INET6) { |
| 273 | sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr); |
| 274 | *out = IPAddress(addr->sin6_addr); |
| 275 | return true; |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 |
| 280 | bool IPFromString(absl::string_view str, IPAddress* out) { |
| 281 | if (!out) { |
| 282 | return false; |
| 283 | } |
| 284 | in_addr addr; |
| 285 | if (rtc::inet_pton(AF_INET, str, &addr) == 0) { |
| 286 | in6_addr addr6; |
| 287 | if (rtc::inet_pton(AF_INET6, str, &addr6) == 0) { |
| 288 | *out = IPAddress(); |
| 289 | return false; |
| 290 | } |
| 291 | *out = IPAddress(addr6); |
| 292 | } else { |
| 293 | *out = IPAddress(addr); |
| 294 | } |
| 295 | return true; |
| 296 | } |
| 297 |
| 298 | bool IPFromString(absl::string_view str, int flags, InterfaceAddress* out) { |
| 299 | IPAddress ip; |
| 300 | if (!IPFromString(str, &ip)) { |
| 301 | return false; |
| 302 | } |
| 303 |
| 304 | *out = InterfaceAddress(ip, flags); |
| 305 | return true; |
| 306 | } |
| 307 |
| 308 | bool IPIsAny(const IPAddress& ip) { |
| 309 | switch (ip.family()) { |
| 310 | case AF_INET: |
| 311 | return ip == IPAddress(INADDR_ANY); |
| 312 | case AF_INET6: |
| 313 | return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix); |
| 314 | case AF_UNSPEC: |
| 315 | return false; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 |
| 320 | static bool IPIsLoopbackV4(const IPAddress& ip) { |
| 321 | uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger(); |
| 322 | return ((ip_in_host_order >> 24) == 127); |
| 323 | } |
| 324 |
| 325 | static bool IPIsLoopbackV6(const IPAddress& ip) { |
| 326 | return ip == IPAddress(in6addr_loopback); |
| 327 | } |
| 328 |
| 329 | bool IPIsLoopback(const IPAddress& ip) { |
| 330 | switch (ip.family()) { |
| 331 | case AF_INET: { |
| 332 | return IPIsLoopbackV4(ip); |
| 333 | } |
| 334 | case AF_INET6: { |
| 335 | return IPIsLoopbackV6(ip); |
| 336 | } |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 |
| 341 | bool IPIsPrivate(const IPAddress& ip) { |
| 342 | return IPIsLinkLocal(ip) || IPIsLoopback(ip) || IPIsPrivateNetwork(ip) || |
| 343 | IPIsSharedNetwork(ip); |
| 344 | } |
| 345 |
| 346 | bool IPIsUnspec(const IPAddress& ip) { |
| 347 | return ip.family() == AF_UNSPEC; |
| 348 | } |
| 349 |
| 350 | size_t HashIP(const IPAddress& ip) { |
| 351 | switch (ip.family()) { |
| 352 | case AF_INET: { |
| 353 | return ip.ipv4_address().s_addr; |
| 354 | } |
| 355 | case AF_INET6: { |
| 356 | in6_addr v6addr = ip.ipv6_address(); |
| 357 | const uint32_t* v6_as_ints = |
| 358 | reinterpret_cast<const uint32_t*>(&v6addr.s6_addr); |
| 359 | return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3]; |
| 360 | } |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 |
| 365 | IPAddress TruncateIP(const IPAddress& ip, int length) { |
| 366 | if (length < 0) { |
| 367 | return IPAddress(); |
| 368 | } |
| 369 | if (ip.family() == AF_INET) { |
| 370 | if (length > 31) { |
| 371 | return ip; |
| 372 | } |
| 373 | if (length == 0) { |
| 374 | return IPAddress(INADDR_ANY); |
| 375 | } |
| 376 | int mask = (0xFFFFFFFF << (32 - length)); |
| 377 | uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr); |
| 378 | in_addr masked; |
| 379 | masked.s_addr = HostToNetwork32(host_order_ip & mask); |
| 380 | return IPAddress(masked); |
| 381 | } else if (ip.family() == AF_INET6) { |
| 382 | if (length > 127) { |
| 383 | return ip; |
| 384 | } |
| 385 | if (length == 0) { |
| 386 | return IPAddress(in6addr_any); |
| 387 | } |
| 388 | in6_addr v6addr = ip.ipv6_address(); |
| 389 | int position = length / 32; |
| 390 | int inner_length = 32 - (length - (position * 32)); |
| 391 | // Note: 64bit mask constant needed to allow possible 32-bit left shift. |
| 392 | uint32_t inner_mask = 0xFFFFFFFFLL << inner_length; |
| 393 | uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr); |
| 394 | for (int i = 0; i < 4; ++i) { |
| 395 | if (i == position) { |
| 396 | uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]); |
| 397 | v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask); |
| 398 | } else if (i > position) { |
| 399 | v6_as_ints[i] = 0; |
| 400 | } |
| 401 | } |
| 402 | return IPAddress(v6addr); |
| 403 | } |
| 404 | return IPAddress(); |
| 405 | } |
| 406 |
| 407 | int CountIPMaskBits(const IPAddress& mask) { |
| 408 | uint32_t word_to_count = 0; |
| 409 | int bits = 0; |
| 410 | switch (mask.family()) { |
| 411 | case AF_INET: { |
| 412 | word_to_count = NetworkToHost32(mask.ipv4_address().s_addr); |
| 413 | break; |
| 414 | } |
| 415 | case AF_INET6: { |
| 416 | in6_addr v6addr = mask.ipv6_address(); |
| 417 | const uint32_t* v6_as_ints = |
| 418 | reinterpret_cast<const uint32_t*>(&v6addr.s6_addr); |
| 419 | int i = 0; |
| 420 | for (; i < 4; ++i) { |
| 421 | if (v6_as_ints[i] != 0xFFFFFFFF) { |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | if (i < 4) { |
| 426 | word_to_count = NetworkToHost32(v6_as_ints[i]); |
| 427 | } |
| 428 | bits = (i * 32); |
| 429 | break; |
| 430 | } |
| 431 | default: { |
| 432 | return 0; |
| 433 | } |
| 434 | } |
| 435 | if (word_to_count == 0) { |
| 436 | return bits; |
| 437 | } |
| 438 |
| 439 | // Public domain bit-twiddling hack from: |
| 440 | // http://graphics.stanford.edu/~seander/bithacks.html |
| 441 | // Counts the trailing 0s in the word. |
| 442 | unsigned int zeroes = 32; |
| 443 | // This could also be written word_to_count &= -word_to_count, but |
| 444 | // MSVC emits warning C4146 when negating an unsigned number. |
| 445 | word_to_count &= ~word_to_count + 1; // Isolate lowest set bit. |
| 446 | if (word_to_count) |
| 447 | zeroes--; |
| 448 | if (word_to_count & 0x0000FFFF) |
| 449 | zeroes -= 16; |
| 450 | if (word_to_count & 0x00FF00FF) |
| 451 | zeroes -= 8; |
| 452 | if (word_to_count & 0x0F0F0F0F) |
| 453 | zeroes -= 4; |
| 454 | if (word_to_count & 0x33333333) |
| 455 | zeroes -= 2; |
| 456 | if (word_to_count & 0x55555555) |
| 457 | zeroes -= 1; |
| 458 |
| 459 | return bits + (32 - zeroes); |
| 460 | } |
| 461 |
| 462 | bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) { |
| 463 | // Helper method for checking IP prefix matches (but only on whole byte |
| 464 | // lengths). Length is in bits. |
| 465 | in6_addr addr = ip.ipv6_address(); |
| 466 | return ::memcmp(&addr, &tomatch, (length >> 3)) == 0; |
| 467 | } |
| 468 |
| 469 | bool IPIs6Bone(const IPAddress& ip) { |
| 470 | return IPIsHelper(ip, k6BonePrefix, 16); |
| 471 | } |
| 472 |
| 473 | bool IPIs6To4(const IPAddress& ip) { |
| 474 | return IPIsHelper(ip, k6To4Prefix, 16); |
| 475 | } |
| 476 |
| 477 | static bool IPIsLinkLocalV4(const IPAddress& ip) { |
| 478 | uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger(); |
| 479 | return ((ip_in_host_order >> 16) == ((169 << 8) | 254)); |
| 480 | } |
| 481 |
| 482 | static bool IPIsLinkLocalV6(const IPAddress& ip) { |
| 483 | // Can't use the helper because the prefix is 10 bits. |
| 484 | in6_addr addr = ip.ipv6_address(); |
| 485 | return (addr.s6_addr[0] == 0xFE) && ((addr.s6_addr[1] & 0xC0) == 0x80); |
| 486 | } |
| 487 |
| 488 | bool IPIsLinkLocal(const IPAddress& ip) { |
| 489 | switch (ip.family()) { |
| 490 | case AF_INET: { |
| 491 | return IPIsLinkLocalV4(ip); |
| 492 | } |
| 493 | case AF_INET6: { |
| 494 | return IPIsLinkLocalV6(ip); |
| 495 | } |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 |
| 500 | // According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An |
| 501 | // address which contains MAC will have its 11th and 12th bytes as FF:FE as well |
| 502 | // as the U/L bit as 1. |
| 503 | bool IPIsMacBased(const IPAddress& ip) { |
| 504 | in6_addr addr = ip.ipv6_address(); |
| 505 | return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF && |
| 506 | addr.s6_addr[12] == 0xFE); |
| 507 | } |
| 508 |
| 509 | bool IPIsSiteLocal(const IPAddress& ip) { |
| 510 | // Can't use the helper because the prefix is 10 bits. |
| 511 | in6_addr addr = ip.ipv6_address(); |
| 512 | return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0; |
| 513 | } |
| 514 |
| 515 | bool IPIsULA(const IPAddress& ip) { |
| 516 | // Can't use the helper because the prefix is 7 bits. |
| 517 | in6_addr addr = ip.ipv6_address(); |
| 518 | return (addr.s6_addr[0] & 0xFE) == 0xFC; |
| 519 | } |
| 520 |
| 521 | bool IPIsTeredo(const IPAddress& ip) { |
| 522 | return IPIsHelper(ip, kTeredoPrefix, 32); |
| 523 | } |
| 524 |
| 525 | bool IPIsV4Compatibility(const IPAddress& ip) { |
| 526 | return IPIsHelper(ip, kV4CompatibilityPrefix, 96); |
| 527 | } |
| 528 |
| 529 | bool IPIsV4Mapped(const IPAddress& ip) { |
| 530 | return IPIsHelper(ip, kV4MappedPrefix, 96); |
| 531 | } |
| 532 |
| 533 | int IPAddressPrecedence(const IPAddress& ip) { |
| 534 | // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo. |
| 535 | if (ip.family() == AF_INET) { |
| 536 | return 30; |
| 537 | } else if (ip.family() == AF_INET6) { |
| 538 | if (IPIsLoopback(ip)) { |
| 539 | return 60; |
| 540 | } else if (IPIsULA(ip)) { |
| 541 | return 50; |
| 542 | } else if (IPIsV4Mapped(ip)) { |
| 543 | return 30; |
| 544 | } else if (IPIs6To4(ip)) { |
| 545 | return 20; |
| 546 | } else if (IPIsTeredo(ip)) { |
| 547 | return 10; |
| 548 | } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) { |
| 549 | return 1; |
| 550 | } else { |
| 551 | // A 'normal' IPv6 address. |
| 552 | return 40; |
| 553 | } |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 |
| 558 | IPAddress GetLoopbackIP(int family) { |
| 559 | if (family == AF_INET) { |
| 560 | return rtc::IPAddress(INADDR_LOOPBACK); |
| 561 | } |
| 562 | if (family == AF_INET6) { |
| 563 | return rtc::IPAddress(in6addr_loopback); |
| 564 | } |
| 565 | return rtc::IPAddress(); |
| 566 | } |
| 567 |
| 568 | IPAddress GetAnyIP(int family) { |
| 569 | if (family == AF_INET) { |
| 570 | return rtc::IPAddress(INADDR_ANY); |
| 571 | } |
| 572 | if (family == AF_INET6) { |
| 573 | return rtc::IPAddress(in6addr_any); |
| 574 | } |
| 575 | return rtc::IPAddress(); |
| 576 | } |
| 577 |
| 578 | } // namespace rtc |
| 579 |