blob: 9967ff323792de50a67acb6bfd73dc137334abee [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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.orgf0488722014-05-13 18:00:26 +000012#include <netinet/in.h>
Yves Gerey665174f2018-06-19 15:03:05 +020013#include <sys/socket.h>
14#include <sys/types.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#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 Bonadei92ea95e2017-09-15 06:47:31 +020028#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 Bonadeie0623852018-02-01 11:17:40 +010034
35#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/win32.h"
Mirko Bonadeie0623852018-02-01 11:17:40 +010037#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
39namespace rtc {
40
41// Prefixes used for categorizing IPv6 addresses.
Yves Gerey665174f2018-06-19 15:03:05 +020042static const in6_addr kV4MappedPrefix = {
43 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0}}};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}};
45static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}};
46static const in6_addr kV4CompatibilityPrefix = {{{0}}};
47static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}};
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +010048static const in6_addr kPrivateNetworkPrefix = {{{0xFD}}};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +010050static bool IPIsHelper(const IPAddress& ip,
Yves Gerey665174f2018-06-19 15:03:05 +020051 const in6_addr& tomatch,
52 int length);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053static in_addr ExtractMappedAddress(const in6_addr& addr);
54
Peter Boström0c4e06b2015-10-07 12:23:21 +020055uint32_t IPAddress::v4AddressAsHostOrderInteger() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 if (family_ == AF_INET) {
57 return NetworkToHost32(u_.ip4.s_addr);
58 } else {
59 return 0;
60 }
61}
62
Guo-wei Shieh11477022015-08-15 09:28:41 -070063bool IPAddress::IsNil() const {
64 return IPIsUnspec(*this);
65}
66
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067size_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 Gerey665174f2018-06-19 15:03:05 +020077bool IPAddress::operator==(const IPAddress& other) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 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 Gerey665174f2018-06-19 15:03:05 +020090bool IPAddress::operator!=(const IPAddress& other) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091 return !((*this) == other);
92}
93
Yves Gerey665174f2018-06-19 15:03:05 +020094bool IPAddress::operator>(const IPAddress& other) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 return (*this) != other && !((*this) < other);
96}
97
Yves Gerey665174f2018-06-19 15:03:05 +020098bool IPAddress::operator<(const IPAddress& other) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 // 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 Gerey665174f2018-06-19 15:03:05 +0200113 NetworkToHost32(other.u_.ip4.s_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 }
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.orgf0488722014-05-13 18:00:26 +0000123in6_addr IPAddress::ipv6_address() const {
124 return u_.ip6;
125}
126
127in_addr IPAddress::ipv4_address() const {
128 return u_.ip4;
129}
130
131std::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
146std::string IPAddress::ToSensitiveString() const {
Peter Boströmcdb38e52015-11-26 00:35:49 +0100147#if !defined(NDEBUG)
148 // Return non-stripped in debug.
149 return ToString();
150#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 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 Ulanovbeed8282016-01-13 18:14:49 -0800162 std::string result;
163 result.resize(INET6_ADDRSTRLEN);
164 in6_addr addr = ipv6_address();
Niels Mölleraba06332018-10-16 15:14:15 +0200165 size_t len = snprintf(&(result[0]), result.size(), "%x:%x:%x:x:x:x:x:x",
166 (addr.s6_addr[0] << 8) + addr.s6_addr[1],
167 (addr.s6_addr[2] << 8) + addr.s6_addr[3],
168 (addr.s6_addr[4] << 8) + addr.s6_addr[5]);
Sergey Ulanovbeed8282016-01-13 18:14:49 -0800169 result.resize(len);
170 return result;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 }
172 }
173 return std::string();
Peter Boströmcdb38e52015-11-26 00:35:49 +0100174#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175}
176
177IPAddress IPAddress::Normalized() const {
178 if (family_ != AF_INET6) {
179 return *this;
180 }
181 if (!IPIsV4Mapped(*this)) {
182 return *this;
183 }
184 in_addr addr = ExtractMappedAddress(u_.ip6);
185 return IPAddress(addr);
186}
187
188IPAddress IPAddress::AsIPv6Address() const {
189 if (family_ != AF_INET) {
190 return *this;
191 }
192 in6_addr v6addr = kV4MappedPrefix;
193 ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr));
194 return IPAddress(v6addr);
195}
196
Yves Gerey665174f2018-06-19 15:03:05 +0200197bool InterfaceAddress::operator==(const InterfaceAddress& other) const {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000198 return ipv6_flags_ == other.ipv6_flags() &&
Yves Gerey665174f2018-06-19 15:03:05 +0200199 static_cast<const IPAddress&>(*this) == other;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000200}
201
Yves Gerey665174f2018-06-19 15:03:05 +0200202bool InterfaceAddress::operator!=(const InterfaceAddress& other) const {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000203 return !((*this) == other);
204}
205
206const InterfaceAddress& InterfaceAddress::operator=(
Yves Gerey665174f2018-06-19 15:03:05 +0200207 const InterfaceAddress& other) {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000208 ipv6_flags_ = other.ipv6_flags_;
209 static_cast<IPAddress&>(*this) = other;
210 return *this;
211}
212
Jonas Olsson74395342018-04-03 12:22:07 +0200213std::string InterfaceAddress::ToString() const {
214 std::string result = IPAddress::ToString();
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000215
Jonas Olsson74395342018-04-03 12:22:07 +0200216 if (family() == AF_INET6)
217 result += "|flags:0x" + rtc::ToHex(ipv6_flags());
218
219 return result;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000220}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100222static bool IPIsPrivateNetworkV4(const IPAddress& ip) {
223 uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
224 return ((ip_in_host_order >> 24) == 10) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200225 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) ||
226 ((ip_in_host_order >> 16) == ((192 << 8) | 168));
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100227}
228
229static bool IPIsPrivateNetworkV6(const IPAddress& ip) {
230 return IPIsHelper(ip, kPrivateNetworkPrefix, 8);
231}
232
233bool IPIsPrivateNetwork(const IPAddress& ip) {
234 switch (ip.family()) {
235 case AF_INET: {
236 return IPIsPrivateNetworkV4(ip);
237 }
238 case AF_INET6: {
239 return IPIsPrivateNetworkV6(ip);
240 }
241 }
242 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243}
244
245in_addr ExtractMappedAddress(const in6_addr& in6) {
246 in_addr ipv4;
247 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr));
248 return ipv4;
249}
250
251bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) {
252 if (!info || !info->ai_addr) {
253 return false;
254 }
255 if (info->ai_addr->sa_family == AF_INET) {
256 sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
257 *out = IPAddress(addr->sin_addr);
258 return true;
259 } else if (info->ai_addr->sa_family == AF_INET6) {
260 sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr);
261 *out = IPAddress(addr->sin6_addr);
262 return true;
263 }
264 return false;
265}
266
267bool IPFromString(const std::string& str, IPAddress* out) {
268 if (!out) {
269 return false;
270 }
271 in_addr addr;
272 if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) {
273 in6_addr addr6;
274 if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) {
275 *out = IPAddress();
276 return false;
277 }
278 *out = IPAddress(addr6);
279 } else {
280 *out = IPAddress(addr);
281 }
282 return true;
283}
284
Yves Gerey665174f2018-06-19 15:03:05 +0200285bool IPFromString(const std::string& str, int flags, InterfaceAddress* out) {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000286 IPAddress ip;
287 if (!IPFromString(str, &ip)) {
288 return false;
289 }
290
291 *out = InterfaceAddress(ip, flags);
292 return true;
293}
294
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295bool IPIsAny(const IPAddress& ip) {
296 switch (ip.family()) {
297 case AF_INET:
298 return ip == IPAddress(INADDR_ANY);
299 case AF_INET6:
guoweis@webrtc.org59ae5ff2015-03-01 23:45:16 +0000300 return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301 case AF_UNSPEC:
302 return false;
303 }
304 return false;
305}
306
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100307static bool IPIsLoopbackV4(const IPAddress& ip) {
308 uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
309 return ((ip_in_host_order >> 24) == 127);
310}
311
312static bool IPIsLoopbackV6(const IPAddress& ip) {
313 return ip == IPAddress(in6addr_loopback);
314}
315
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316bool IPIsLoopback(const IPAddress& ip) {
317 switch (ip.family()) {
318 case AF_INET: {
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100319 return IPIsLoopbackV4(ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000320 }
321 case AF_INET6: {
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100322 return IPIsLoopbackV6(ip);
Yuwei Huangb181f712018-01-22 17:01:28 -0800323 }
324 }
325 return false;
326}
327
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328bool IPIsPrivate(const IPAddress& ip) {
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100329 return IPIsLinkLocal(ip) || IPIsLoopback(ip) || IPIsPrivateNetwork(ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330}
331
332bool IPIsUnspec(const IPAddress& ip) {
333 return ip.family() == AF_UNSPEC;
334}
335
336size_t HashIP(const IPAddress& ip) {
337 switch (ip.family()) {
338 case AF_INET: {
339 return ip.ipv4_address().s_addr;
340 }
341 case AF_INET6: {
342 in6_addr v6addr = ip.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200343 const uint32_t* v6_as_ints =
344 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3];
346 }
347 }
348 return 0;
349}
350
351IPAddress TruncateIP(const IPAddress& ip, int length) {
352 if (length < 0) {
353 return IPAddress();
354 }
355 if (ip.family() == AF_INET) {
356 if (length > 31) {
357 return ip;
358 }
359 if (length == 0) {
360 return IPAddress(INADDR_ANY);
361 }
362 int mask = (0xFFFFFFFF << (32 - length));
Peter Boström0c4e06b2015-10-07 12:23:21 +0200363 uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000364 in_addr masked;
365 masked.s_addr = HostToNetwork32(host_order_ip & mask);
366 return IPAddress(masked);
367 } else if (ip.family() == AF_INET6) {
368 if (length > 127) {
369 return ip;
370 }
371 if (length == 0) {
372 return IPAddress(in6addr_any);
373 }
374 in6_addr v6addr = ip.ipv6_address();
375 int position = length / 32;
376 int inner_length = 32 - (length - (position * 32));
377 // Note: 64bit mask constant needed to allow possible 32-bit left shift.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200378 uint32_t inner_mask = 0xFFFFFFFFLL << inner_length;
379 uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000380 for (int i = 0; i < 4; ++i) {
381 if (i == position) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200382 uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask);
384 } else if (i > position) {
385 v6_as_ints[i] = 0;
386 }
387 }
388 return IPAddress(v6addr);
389 }
390 return IPAddress();
391}
392
393int CountIPMaskBits(IPAddress mask) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200394 uint32_t word_to_count = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395 int bits = 0;
396 switch (mask.family()) {
397 case AF_INET: {
398 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr);
399 break;
400 }
401 case AF_INET6: {
402 in6_addr v6addr = mask.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 const uint32_t* v6_as_ints =
404 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000405 int i = 0;
406 for (; i < 4; ++i) {
407 if (v6_as_ints[i] != 0xFFFFFFFF) {
408 break;
409 }
410 }
411 if (i < 4) {
412 word_to_count = NetworkToHost32(v6_as_ints[i]);
413 }
414 bits = (i * 32);
415 break;
416 }
Yves Gerey665174f2018-06-19 15:03:05 +0200417 default: { return 0; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 }
419 if (word_to_count == 0) {
420 return bits;
421 }
422
423 // Public domain bit-twiddling hack from:
424 // http://graphics.stanford.edu/~seander/bithacks.html
425 // Counts the trailing 0s in the word.
426 unsigned int zeroes = 32;
tereliusd802b5b2016-03-01 11:07:34 -0800427 // This could also be written word_to_count &= -word_to_count, but
428 // MSVC emits warning C4146 when negating an unsigned number.
429 word_to_count &= ~word_to_count + 1; // Isolate lowest set bit.
Yves Gerey665174f2018-06-19 15:03:05 +0200430 if (word_to_count)
431 zeroes--;
432 if (word_to_count & 0x0000FFFF)
433 zeroes -= 16;
434 if (word_to_count & 0x00FF00FF)
435 zeroes -= 8;
436 if (word_to_count & 0x0F0F0F0F)
437 zeroes -= 4;
438 if (word_to_count & 0x33333333)
439 zeroes -= 2;
440 if (word_to_count & 0x55555555)
441 zeroes -= 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000442
443 return bits + (32 - zeroes);
444}
445
446bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) {
447 // Helper method for checking IP prefix matches (but only on whole byte
448 // lengths). Length is in bits.
449 in6_addr addr = ip.ipv6_address();
450 return ::memcmp(&addr, &tomatch, (length >> 3)) == 0;
451}
452
453bool IPIs6Bone(const IPAddress& ip) {
454 return IPIsHelper(ip, k6BonePrefix, 16);
455}
456
457bool IPIs6To4(const IPAddress& ip) {
458 return IPIsHelper(ip, k6To4Prefix, 16);
459}
460
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100461static bool IPIsLinkLocalV4(const IPAddress& ip) {
462 uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
463 return ((ip_in_host_order >> 16) == ((169 << 8) | 254));
464}
465
466static bool IPIsLinkLocalV6(const IPAddress& ip) {
467 // Can't use the helper because the prefix is 10 bits.
468 in6_addr addr = ip.ipv6_address();
469 return (addr.s6_addr[0] == 0xFE) && ((addr.s6_addr[1] & 0xC0) == 0x80);
470}
471
472bool IPIsLinkLocal(const IPAddress& ip) {
473 switch (ip.family()) {
474 case AF_INET: {
475 return IPIsLinkLocalV4(ip);
476 }
477 case AF_INET6: {
478 return IPIsLinkLocalV6(ip);
479 }
480 }
481 return false;
482}
483
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000484// According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An
485// address which contains MAC will have its 11th and 12th bytes as FF:FE as well
486// as the U/L bit as 1.
487bool IPIsMacBased(const IPAddress& ip) {
488 in6_addr addr = ip.ipv6_address();
489 return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF &&
490 addr.s6_addr[12] == 0xFE);
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000491}
492
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000493bool IPIsSiteLocal(const IPAddress& ip) {
494 // Can't use the helper because the prefix is 10 bits.
495 in6_addr addr = ip.ipv6_address();
496 return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0;
497}
498
499bool IPIsULA(const IPAddress& ip) {
500 // Can't use the helper because the prefix is 7 bits.
501 in6_addr addr = ip.ipv6_address();
502 return (addr.s6_addr[0] & 0xFE) == 0xFC;
503}
504
505bool IPIsTeredo(const IPAddress& ip) {
506 return IPIsHelper(ip, kTeredoPrefix, 32);
507}
508
509bool IPIsV4Compatibility(const IPAddress& ip) {
510 return IPIsHelper(ip, kV4CompatibilityPrefix, 96);
511}
512
513bool IPIsV4Mapped(const IPAddress& ip) {
514 return IPIsHelper(ip, kV4MappedPrefix, 96);
515}
516
517int IPAddressPrecedence(const IPAddress& ip) {
518 // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo.
519 if (ip.family() == AF_INET) {
520 return 30;
521 } else if (ip.family() == AF_INET6) {
522 if (IPIsLoopback(ip)) {
523 return 60;
524 } else if (IPIsULA(ip)) {
525 return 50;
526 } else if (IPIsV4Mapped(ip)) {
527 return 30;
528 } else if (IPIs6To4(ip)) {
529 return 20;
530 } else if (IPIsTeredo(ip)) {
531 return 10;
532 } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) {
533 return 1;
534 } else {
535 // A 'normal' IPv6 address.
536 return 40;
537 }
538 }
539 return 0;
540}
541
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700542IPAddress GetLoopbackIP(int family) {
543 if (family == AF_INET) {
544 return rtc::IPAddress(INADDR_LOOPBACK);
545 }
546 if (family == AF_INET6) {
547 return rtc::IPAddress(in6addr_loopback);
548 }
549 return rtc::IPAddress();
550}
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800551
552IPAddress GetAnyIP(int family) {
553 if (family == AF_INET) {
554 return rtc::IPAddress(INADDR_ANY);
555 }
556 if (family == AF_INET6) {
557 return rtc::IPAddress(in6addr_any);
558 }
559 return rtc::IPAddress();
560}
561
tereliusd802b5b2016-03-01 11:07:34 -0800562} // namespace rtc