henrike@webrtc.org | f7795df | 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 | |
| 11 | #if defined(WEBRTC_POSIX) |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #ifdef OPENBSD |
| 16 | #include <netinet/in_systm.h> |
| 17 | #endif |
| 18 | #ifndef __native_client__ |
| 19 | #include <netinet/ip.h> |
| 20 | #endif |
| 21 | #include <arpa/inet.h> |
| 22 | #include <netdb.h> |
| 23 | #include <unistd.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "webrtc/base/ipaddress.h" |
| 29 | #include "webrtc/base/byteorder.h" |
| 30 | #include "webrtc/base/nethelpers.h" |
| 31 | #include "webrtc/base/logging.h" |
| 32 | #include "webrtc/base/win32.h" |
| 33 | |
| 34 | namespace rtc { |
| 35 | |
| 36 | // Prefixes used for categorizing IPv6 addresses. |
| 37 | static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 38 | 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 | |
| 44 | bool IPAddress::strip_sensitive_ = false; |
| 45 | |
| 46 | static bool IsPrivateV4(uint32 ip); |
| 47 | static in_addr ExtractMappedAddress(const in6_addr& addr); |
| 48 | |
| 49 | uint32 IPAddress::v4AddressAsHostOrderInteger() const { |
| 50 | if (family_ == AF_INET) { |
| 51 | return NetworkToHost32(u_.ip4.s_addr); |
| 52 | } else { |
| 53 | return 0; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | size_t IPAddress::Size() const { |
| 58 | switch (family_) { |
| 59 | case AF_INET: |
| 60 | return sizeof(in_addr); |
| 61 | case AF_INET6: |
| 62 | return sizeof(in6_addr); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | bool IPAddress::operator==(const IPAddress &other) const { |
| 69 | if (family_ != other.family_) { |
| 70 | return false; |
| 71 | } |
| 72 | if (family_ == AF_INET) { |
| 73 | return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0; |
| 74 | } |
| 75 | if (family_ == AF_INET6) { |
| 76 | return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0; |
| 77 | } |
| 78 | return family_ == AF_UNSPEC; |
| 79 | } |
| 80 | |
| 81 | bool IPAddress::operator!=(const IPAddress &other) const { |
| 82 | return !((*this) == other); |
| 83 | } |
| 84 | |
| 85 | bool IPAddress::operator >(const IPAddress &other) const { |
| 86 | return (*this) != other && !((*this) < other); |
| 87 | } |
| 88 | |
| 89 | bool IPAddress::operator <(const IPAddress &other) const { |
| 90 | // IPv4 is 'less than' IPv6 |
| 91 | if (family_ != other.family_) { |
| 92 | if (family_ == AF_UNSPEC) { |
| 93 | return true; |
| 94 | } |
| 95 | if (family_ == AF_INET && other.family_ == AF_INET6) { |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | // Comparing addresses of the same family. |
| 101 | switch (family_) { |
| 102 | case AF_INET: { |
| 103 | return NetworkToHost32(u_.ip4.s_addr) < |
| 104 | NetworkToHost32(other.u_.ip4.s_addr); |
| 105 | } |
| 106 | case AF_INET6: { |
| 107 | return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0; |
| 108 | } |
| 109 | } |
| 110 | // Catches AF_UNSPEC and invalid addresses. |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | std::ostream& operator<<(std::ostream& os, const IPAddress& ip) { |
| 115 | os << ip.ToString(); |
| 116 | return os; |
| 117 | } |
| 118 | |
| 119 | in6_addr IPAddress::ipv6_address() const { |
| 120 | return u_.ip6; |
| 121 | } |
| 122 | |
| 123 | in_addr IPAddress::ipv4_address() const { |
| 124 | return u_.ip4; |
| 125 | } |
| 126 | |
| 127 | std::string IPAddress::ToString() const { |
| 128 | if (family_ != AF_INET && family_ != AF_INET6) { |
| 129 | return std::string(); |
| 130 | } |
| 131 | char buf[INET6_ADDRSTRLEN] = {0}; |
| 132 | const void* src = &u_.ip4; |
| 133 | if (family_ == AF_INET6) { |
| 134 | src = &u_.ip6; |
| 135 | } |
| 136 | if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) { |
| 137 | return std::string(); |
| 138 | } |
| 139 | return std::string(buf); |
| 140 | } |
| 141 | |
| 142 | std::string IPAddress::ToSensitiveString() const { |
| 143 | if (!strip_sensitive_) |
| 144 | return ToString(); |
| 145 | |
| 146 | switch (family_) { |
| 147 | case AF_INET: { |
| 148 | std::string address = ToString(); |
| 149 | size_t find_pos = address.rfind('.'); |
| 150 | if (find_pos == std::string::npos) |
| 151 | return std::string(); |
| 152 | address.resize(find_pos); |
| 153 | address += ".x"; |
| 154 | return address; |
| 155 | } |
| 156 | case AF_INET6: { |
| 157 | // TODO(grunell): Return a string of format 1:2:3:x:x:x:x:x or such |
| 158 | // instead of zeroing out. |
| 159 | return TruncateIP(*this, 128 - 80).ToString(); |
| 160 | } |
| 161 | } |
| 162 | return std::string(); |
| 163 | } |
| 164 | |
| 165 | IPAddress IPAddress::Normalized() const { |
| 166 | if (family_ != AF_INET6) { |
| 167 | return *this; |
| 168 | } |
| 169 | if (!IPIsV4Mapped(*this)) { |
| 170 | return *this; |
| 171 | } |
| 172 | in_addr addr = ExtractMappedAddress(u_.ip6); |
| 173 | return IPAddress(addr); |
| 174 | } |
| 175 | |
| 176 | IPAddress IPAddress::AsIPv6Address() const { |
| 177 | if (family_ != AF_INET) { |
| 178 | return *this; |
| 179 | } |
| 180 | in6_addr v6addr = kV4MappedPrefix; |
| 181 | ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr)); |
| 182 | return IPAddress(v6addr); |
| 183 | } |
| 184 | |
| 185 | void IPAddress::set_strip_sensitive(bool enable) { |
| 186 | strip_sensitive_ = enable; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | bool IsPrivateV4(uint32 ip_in_host_order) { |
| 191 | return ((ip_in_host_order >> 24) == 127) || |
| 192 | ((ip_in_host_order >> 24) == 10) || |
| 193 | ((ip_in_host_order >> 20) == ((172 << 4) | 1)) || |
| 194 | ((ip_in_host_order >> 16) == ((192 << 8) | 168)) || |
| 195 | ((ip_in_host_order >> 16) == ((169 << 8) | 254)); |
| 196 | } |
| 197 | |
| 198 | in_addr ExtractMappedAddress(const in6_addr& in6) { |
| 199 | in_addr ipv4; |
| 200 | ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr)); |
| 201 | return ipv4; |
| 202 | } |
| 203 | |
| 204 | bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) { |
| 205 | if (!info || !info->ai_addr) { |
| 206 | return false; |
| 207 | } |
| 208 | if (info->ai_addr->sa_family == AF_INET) { |
| 209 | sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr); |
| 210 | *out = IPAddress(addr->sin_addr); |
| 211 | return true; |
| 212 | } else if (info->ai_addr->sa_family == AF_INET6) { |
| 213 | sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr); |
| 214 | *out = IPAddress(addr->sin6_addr); |
| 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | bool IPFromString(const std::string& str, IPAddress* out) { |
| 221 | if (!out) { |
| 222 | return false; |
| 223 | } |
| 224 | in_addr addr; |
| 225 | if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) { |
| 226 | in6_addr addr6; |
| 227 | if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) { |
| 228 | *out = IPAddress(); |
| 229 | return false; |
| 230 | } |
| 231 | *out = IPAddress(addr6); |
| 232 | } else { |
| 233 | *out = IPAddress(addr); |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | bool IPIsAny(const IPAddress& ip) { |
| 239 | switch (ip.family()) { |
| 240 | case AF_INET: |
| 241 | return ip == IPAddress(INADDR_ANY); |
| 242 | case AF_INET6: |
| 243 | return ip == IPAddress(in6addr_any); |
| 244 | case AF_UNSPEC: |
| 245 | return false; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | bool IPIsLoopback(const IPAddress& ip) { |
| 251 | switch (ip.family()) { |
| 252 | case AF_INET: { |
| 253 | return ip == IPAddress(INADDR_LOOPBACK); |
| 254 | } |
| 255 | case AF_INET6: { |
| 256 | return ip == IPAddress(in6addr_loopback); |
| 257 | } |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | bool IPIsPrivate(const IPAddress& ip) { |
| 263 | switch (ip.family()) { |
| 264 | case AF_INET: { |
| 265 | return IsPrivateV4(ip.v4AddressAsHostOrderInteger()); |
| 266 | } |
| 267 | case AF_INET6: { |
| 268 | in6_addr v6 = ip.ipv6_address(); |
| 269 | return (v6.s6_addr[0] == 0xFE && v6.s6_addr[1] == 0x80) || |
| 270 | IPIsLoopback(ip); |
| 271 | } |
| 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | bool IPIsUnspec(const IPAddress& ip) { |
| 277 | return ip.family() == AF_UNSPEC; |
| 278 | } |
| 279 | |
| 280 | size_t HashIP(const IPAddress& ip) { |
| 281 | switch (ip.family()) { |
| 282 | case AF_INET: { |
| 283 | return ip.ipv4_address().s_addr; |
| 284 | } |
| 285 | case AF_INET6: { |
| 286 | in6_addr v6addr = ip.ipv6_address(); |
| 287 | const uint32* v6_as_ints = |
| 288 | reinterpret_cast<const uint32*>(&v6addr.s6_addr); |
| 289 | return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3]; |
| 290 | } |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | IPAddress TruncateIP(const IPAddress& ip, int length) { |
| 296 | if (length < 0) { |
| 297 | return IPAddress(); |
| 298 | } |
| 299 | if (ip.family() == AF_INET) { |
| 300 | if (length > 31) { |
| 301 | return ip; |
| 302 | } |
| 303 | if (length == 0) { |
| 304 | return IPAddress(INADDR_ANY); |
| 305 | } |
| 306 | int mask = (0xFFFFFFFF << (32 - length)); |
| 307 | uint32 host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr); |
| 308 | in_addr masked; |
| 309 | masked.s_addr = HostToNetwork32(host_order_ip & mask); |
| 310 | return IPAddress(masked); |
| 311 | } else if (ip.family() == AF_INET6) { |
| 312 | if (length > 127) { |
| 313 | return ip; |
| 314 | } |
| 315 | if (length == 0) { |
| 316 | return IPAddress(in6addr_any); |
| 317 | } |
| 318 | in6_addr v6addr = ip.ipv6_address(); |
| 319 | int position = length / 32; |
| 320 | int inner_length = 32 - (length - (position * 32)); |
| 321 | // Note: 64bit mask constant needed to allow possible 32-bit left shift. |
| 322 | uint32 inner_mask = 0xFFFFFFFFLL << inner_length; |
| 323 | uint32* v6_as_ints = |
| 324 | reinterpret_cast<uint32*>(&v6addr.s6_addr); |
| 325 | for (int i = 0; i < 4; ++i) { |
| 326 | if (i == position) { |
| 327 | uint32 host_order_inner = NetworkToHost32(v6_as_ints[i]); |
| 328 | v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask); |
| 329 | } else if (i > position) { |
| 330 | v6_as_ints[i] = 0; |
| 331 | } |
| 332 | } |
| 333 | return IPAddress(v6addr); |
| 334 | } |
| 335 | return IPAddress(); |
| 336 | } |
| 337 | |
| 338 | int CountIPMaskBits(IPAddress mask) { |
| 339 | uint32 word_to_count = 0; |
| 340 | int bits = 0; |
| 341 | switch (mask.family()) { |
| 342 | case AF_INET: { |
| 343 | word_to_count = NetworkToHost32(mask.ipv4_address().s_addr); |
| 344 | break; |
| 345 | } |
| 346 | case AF_INET6: { |
| 347 | in6_addr v6addr = mask.ipv6_address(); |
| 348 | const uint32* v6_as_ints = |
| 349 | reinterpret_cast<const uint32*>(&v6addr.s6_addr); |
| 350 | int i = 0; |
| 351 | for (; i < 4; ++i) { |
| 352 | if (v6_as_ints[i] != 0xFFFFFFFF) { |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | if (i < 4) { |
| 357 | word_to_count = NetworkToHost32(v6_as_ints[i]); |
| 358 | } |
| 359 | bits = (i * 32); |
| 360 | break; |
| 361 | } |
| 362 | default: { |
| 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | if (word_to_count == 0) { |
| 367 | return bits; |
| 368 | } |
| 369 | |
| 370 | // Public domain bit-twiddling hack from: |
| 371 | // http://graphics.stanford.edu/~seander/bithacks.html |
| 372 | // Counts the trailing 0s in the word. |
| 373 | unsigned int zeroes = 32; |
| 374 | word_to_count &= -static_cast<int32>(word_to_count); |
| 375 | if (word_to_count) zeroes--; |
| 376 | if (word_to_count & 0x0000FFFF) zeroes -= 16; |
| 377 | if (word_to_count & 0x00FF00FF) zeroes -= 8; |
| 378 | if (word_to_count & 0x0F0F0F0F) zeroes -= 4; |
| 379 | if (word_to_count & 0x33333333) zeroes -= 2; |
| 380 | if (word_to_count & 0x55555555) zeroes -= 1; |
| 381 | |
| 382 | return bits + (32 - zeroes); |
| 383 | } |
| 384 | |
| 385 | bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) { |
| 386 | // Helper method for checking IP prefix matches (but only on whole byte |
| 387 | // lengths). Length is in bits. |
| 388 | in6_addr addr = ip.ipv6_address(); |
| 389 | return ::memcmp(&addr, &tomatch, (length >> 3)) == 0; |
| 390 | } |
| 391 | |
| 392 | bool IPIs6Bone(const IPAddress& ip) { |
| 393 | return IPIsHelper(ip, k6BonePrefix, 16); |
| 394 | } |
| 395 | |
| 396 | bool IPIs6To4(const IPAddress& ip) { |
| 397 | return IPIsHelper(ip, k6To4Prefix, 16); |
| 398 | } |
| 399 | |
| 400 | bool IPIsSiteLocal(const IPAddress& ip) { |
| 401 | // Can't use the helper because the prefix is 10 bits. |
| 402 | in6_addr addr = ip.ipv6_address(); |
| 403 | return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0; |
| 404 | } |
| 405 | |
| 406 | bool IPIsULA(const IPAddress& ip) { |
| 407 | // Can't use the helper because the prefix is 7 bits. |
| 408 | in6_addr addr = ip.ipv6_address(); |
| 409 | return (addr.s6_addr[0] & 0xFE) == 0xFC; |
| 410 | } |
| 411 | |
| 412 | bool IPIsTeredo(const IPAddress& ip) { |
| 413 | return IPIsHelper(ip, kTeredoPrefix, 32); |
| 414 | } |
| 415 | |
| 416 | bool IPIsV4Compatibility(const IPAddress& ip) { |
| 417 | return IPIsHelper(ip, kV4CompatibilityPrefix, 96); |
| 418 | } |
| 419 | |
| 420 | bool IPIsV4Mapped(const IPAddress& ip) { |
| 421 | return IPIsHelper(ip, kV4MappedPrefix, 96); |
| 422 | } |
| 423 | |
| 424 | int IPAddressPrecedence(const IPAddress& ip) { |
| 425 | // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo. |
| 426 | if (ip.family() == AF_INET) { |
| 427 | return 30; |
| 428 | } else if (ip.family() == AF_INET6) { |
| 429 | if (IPIsLoopback(ip)) { |
| 430 | return 60; |
| 431 | } else if (IPIsULA(ip)) { |
| 432 | return 50; |
| 433 | } else if (IPIsV4Mapped(ip)) { |
| 434 | return 30; |
| 435 | } else if (IPIs6To4(ip)) { |
| 436 | return 20; |
| 437 | } else if (IPIsTeredo(ip)) { |
| 438 | return 10; |
| 439 | } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) { |
| 440 | return 1; |
| 441 | } else { |
| 442 | // A 'normal' IPv6 address. |
| 443 | return 40; |
| 444 | } |
| 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | } // Namespace talk base |