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