blob: c85624d24a7729d28c2f6efe2c0eb1d7243fdcaa [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)
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"
henrikgac921d72015-12-14 02:07:03 -080030#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031#include "webrtc/base/nethelpers.h"
32#include "webrtc/base/logging.h"
33#include "webrtc/base/win32.h"
34
35namespace rtc {
36
37// Prefixes used for categorizing IPv6 addresses.
38static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39 0xFF, 0xFF, 0}}};
40static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}};
41static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}};
42static const in6_addr kV4CompatibilityPrefix = {{{0}}};
43static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}};
44
Peter Boström0c4e06b2015-10-07 12:23:21 +020045static bool IsPrivateV4(uint32_t ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046static in_addr ExtractMappedAddress(const in6_addr& addr);
47
Peter Boström0c4e06b2015-10-07 12:23:21 +020048uint32_t IPAddress::v4AddressAsHostOrderInteger() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 if (family_ == AF_INET) {
50 return NetworkToHost32(u_.ip4.s_addr);
51 } else {
52 return 0;
53 }
54}
55
Guo-wei Shieh11477022015-08-15 09:28:41 -070056bool IPAddress::IsNil() const {
57 return IPIsUnspec(*this);
58}
59
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060size_t IPAddress::Size() const {
61 switch (family_) {
62 case AF_INET:
63 return sizeof(in_addr);
64 case AF_INET6:
65 return sizeof(in6_addr);
66 }
67 return 0;
68}
69
70
71bool IPAddress::operator==(const IPAddress &other) const {
72 if (family_ != other.family_) {
73 return false;
74 }
75 if (family_ == AF_INET) {
76 return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0;
77 }
78 if (family_ == AF_INET6) {
79 return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0;
80 }
81 return family_ == AF_UNSPEC;
82}
83
84bool IPAddress::operator!=(const IPAddress &other) const {
85 return !((*this) == other);
86}
87
88bool IPAddress::operator >(const IPAddress &other) const {
89 return (*this) != other && !((*this) < other);
90}
91
92bool IPAddress::operator <(const IPAddress &other) const {
93 // IPv4 is 'less than' IPv6
94 if (family_ != other.family_) {
95 if (family_ == AF_UNSPEC) {
96 return true;
97 }
98 if (family_ == AF_INET && other.family_ == AF_INET6) {
99 return true;
100 }
101 return false;
102 }
103 // Comparing addresses of the same family.
104 switch (family_) {
105 case AF_INET: {
106 return NetworkToHost32(u_.ip4.s_addr) <
107 NetworkToHost32(other.u_.ip4.s_addr);
108 }
109 case AF_INET6: {
110 return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0;
111 }
112 }
113 // Catches AF_UNSPEC and invalid addresses.
114 return false;
115}
116
117std::ostream& operator<<(std::ostream& os, const IPAddress& ip) {
118 os << ip.ToString();
119 return os;
120}
121
122in6_addr IPAddress::ipv6_address() const {
123 return u_.ip6;
124}
125
126in_addr IPAddress::ipv4_address() const {
127 return u_.ip4;
128}
129
130std::string IPAddress::ToString() const {
131 if (family_ != AF_INET && family_ != AF_INET6) {
132 return std::string();
133 }
134 char buf[INET6_ADDRSTRLEN] = {0};
135 const void* src = &u_.ip4;
136 if (family_ == AF_INET6) {
137 src = &u_.ip6;
138 }
139 if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) {
140 return std::string();
141 }
142 return std::string(buf);
143}
144
145std::string IPAddress::ToSensitiveString() const {
Peter Boströmcdb38e52015-11-26 00:35:49 +0100146#if !defined(NDEBUG)
147 // Return non-stripped in debug.
148 return ToString();
149#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150 switch (family_) {
151 case AF_INET: {
152 std::string address = ToString();
153 size_t find_pos = address.rfind('.');
154 if (find_pos == std::string::npos)
155 return std::string();
156 address.resize(find_pos);
157 address += ".x";
158 return address;
159 }
160 case AF_INET6: {
henrikgac921d72015-12-14 02:07:03 -0800161 // Remove the last 5 groups (80 bits).
162 std::string address = TruncateIP(*this, 128 - 80).ToString();
163
164 // If all three remaining groups are written out explicitly in the string,
165 // remove one of the two trailing colons before appending the stripped
166 // groups as "x"s. There should be max 4 colons (2 between the 3 groups +
167 // 2 trailing) in the truncated address string.
168 size_t number_of_colons = std::count(address.begin(), address.end(), ':');
169 RTC_CHECK_LE(number_of_colons, 4u);
170 if (number_of_colons > 3)
171 address.resize(address.length() - 1);
172
173 return address + "x:x:x:x:x";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 }
175 }
176 return std::string();
Peter Boströmcdb38e52015-11-26 00:35:49 +0100177#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178}
179
180IPAddress IPAddress::Normalized() const {
181 if (family_ != AF_INET6) {
182 return *this;
183 }
184 if (!IPIsV4Mapped(*this)) {
185 return *this;
186 }
187 in_addr addr = ExtractMappedAddress(u_.ip6);
188 return IPAddress(addr);
189}
190
191IPAddress IPAddress::AsIPv6Address() const {
192 if (family_ != AF_INET) {
193 return *this;
194 }
195 in6_addr v6addr = kV4MappedPrefix;
196 ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr));
197 return IPAddress(v6addr);
198}
199
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000200bool InterfaceAddress::operator==(const InterfaceAddress &other) const {
201 return ipv6_flags_ == other.ipv6_flags() &&
202 static_cast<const IPAddress&>(*this) == other;
203}
204
205bool InterfaceAddress::operator!=(const InterfaceAddress &other) const {
206 return !((*this) == other);
207}
208
209const InterfaceAddress& InterfaceAddress::operator=(
210 const InterfaceAddress& other) {
211 ipv6_flags_ = other.ipv6_flags_;
212 static_cast<IPAddress&>(*this) = other;
213 return *this;
214}
215
216std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) {
217 os << static_cast<const IPAddress&>(ip);
218
219 if (ip.family() == AF_INET6)
220 os << "|flags:0x" << std::hex << ip.ipv6_flags();
221
222 return os;
223}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224
Peter Boström0c4e06b2015-10-07 12:23:21 +0200225bool IsPrivateV4(uint32_t ip_in_host_order) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 return ((ip_in_host_order >> 24) == 127) ||
227 ((ip_in_host_order >> 24) == 10) ||
228 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) ||
229 ((ip_in_host_order >> 16) == ((192 << 8) | 168)) ||
230 ((ip_in_host_order >> 16) == ((169 << 8) | 254));
231}
232
233in_addr ExtractMappedAddress(const in6_addr& in6) {
234 in_addr ipv4;
235 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr));
236 return ipv4;
237}
238
239bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) {
240 if (!info || !info->ai_addr) {
241 return false;
242 }
243 if (info->ai_addr->sa_family == AF_INET) {
244 sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
245 *out = IPAddress(addr->sin_addr);
246 return true;
247 } else if (info->ai_addr->sa_family == AF_INET6) {
248 sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr);
249 *out = IPAddress(addr->sin6_addr);
250 return true;
251 }
252 return false;
253}
254
255bool IPFromString(const std::string& str, IPAddress* out) {
256 if (!out) {
257 return false;
258 }
259 in_addr addr;
260 if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) {
261 in6_addr addr6;
262 if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) {
263 *out = IPAddress();
264 return false;
265 }
266 *out = IPAddress(addr6);
267 } else {
268 *out = IPAddress(addr);
269 }
270 return true;
271}
272
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000273bool IPFromString(const std::string& str, int flags,
274 InterfaceAddress* out) {
275 IPAddress ip;
276 if (!IPFromString(str, &ip)) {
277 return false;
278 }
279
280 *out = InterfaceAddress(ip, flags);
281 return true;
282}
283
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284bool IPIsAny(const IPAddress& ip) {
285 switch (ip.family()) {
286 case AF_INET:
287 return ip == IPAddress(INADDR_ANY);
288 case AF_INET6:
guoweis@webrtc.org59ae5ff2015-03-01 23:45:16 +0000289 return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 case AF_UNSPEC:
291 return false;
292 }
293 return false;
294}
295
296bool IPIsLoopback(const IPAddress& ip) {
297 switch (ip.family()) {
298 case AF_INET: {
299 return ip == IPAddress(INADDR_LOOPBACK);
300 }
301 case AF_INET6: {
302 return ip == IPAddress(in6addr_loopback);
303 }
304 }
305 return false;
306}
307
308bool IPIsPrivate(const IPAddress& ip) {
309 switch (ip.family()) {
310 case AF_INET: {
311 return IsPrivateV4(ip.v4AddressAsHostOrderInteger());
312 }
313 case AF_INET6: {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000314 return IPIsLinkLocal(ip) || IPIsLoopback(ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 }
316 }
317 return false;
318}
319
320bool IPIsUnspec(const IPAddress& ip) {
321 return ip.family() == AF_UNSPEC;
322}
323
324size_t HashIP(const IPAddress& ip) {
325 switch (ip.family()) {
326 case AF_INET: {
327 return ip.ipv4_address().s_addr;
328 }
329 case AF_INET6: {
330 in6_addr v6addr = ip.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200331 const uint32_t* v6_as_ints =
332 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3];
334 }
335 }
336 return 0;
337}
338
339IPAddress TruncateIP(const IPAddress& ip, int length) {
340 if (length < 0) {
341 return IPAddress();
342 }
343 if (ip.family() == AF_INET) {
344 if (length > 31) {
345 return ip;
346 }
347 if (length == 0) {
348 return IPAddress(INADDR_ANY);
349 }
350 int mask = (0xFFFFFFFF << (32 - length));
Peter Boström0c4e06b2015-10-07 12:23:21 +0200351 uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352 in_addr masked;
353 masked.s_addr = HostToNetwork32(host_order_ip & mask);
354 return IPAddress(masked);
355 } else if (ip.family() == AF_INET6) {
356 if (length > 127) {
357 return ip;
358 }
359 if (length == 0) {
360 return IPAddress(in6addr_any);
361 }
362 in6_addr v6addr = ip.ipv6_address();
363 int position = length / 32;
364 int inner_length = 32 - (length - (position * 32));
365 // Note: 64bit mask constant needed to allow possible 32-bit left shift.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200366 uint32_t inner_mask = 0xFFFFFFFFLL << inner_length;
367 uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368 for (int i = 0; i < 4; ++i) {
369 if (i == position) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200370 uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask);
372 } else if (i > position) {
373 v6_as_ints[i] = 0;
374 }
375 }
376 return IPAddress(v6addr);
377 }
378 return IPAddress();
379}
380
381int CountIPMaskBits(IPAddress mask) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200382 uint32_t word_to_count = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383 int bits = 0;
384 switch (mask.family()) {
385 case AF_INET: {
386 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr);
387 break;
388 }
389 case AF_INET6: {
390 in6_addr v6addr = mask.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200391 const uint32_t* v6_as_ints =
392 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000393 int i = 0;
394 for (; i < 4; ++i) {
395 if (v6_as_ints[i] != 0xFFFFFFFF) {
396 break;
397 }
398 }
399 if (i < 4) {
400 word_to_count = NetworkToHost32(v6_as_ints[i]);
401 }
402 bits = (i * 32);
403 break;
404 }
405 default: {
406 return 0;
407 }
408 }
409 if (word_to_count == 0) {
410 return bits;
411 }
412
413 // Public domain bit-twiddling hack from:
414 // http://graphics.stanford.edu/~seander/bithacks.html
415 // Counts the trailing 0s in the word.
416 unsigned int zeroes = 32;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200417 word_to_count &= -static_cast<int32_t>(word_to_count);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 if (word_to_count) zeroes--;
419 if (word_to_count & 0x0000FFFF) zeroes -= 16;
420 if (word_to_count & 0x00FF00FF) zeroes -= 8;
421 if (word_to_count & 0x0F0F0F0F) zeroes -= 4;
422 if (word_to_count & 0x33333333) zeroes -= 2;
423 if (word_to_count & 0x55555555) zeroes -= 1;
424
425 return bits + (32 - zeroes);
426}
427
428bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) {
429 // Helper method for checking IP prefix matches (but only on whole byte
430 // lengths). Length is in bits.
431 in6_addr addr = ip.ipv6_address();
432 return ::memcmp(&addr, &tomatch, (length >> 3)) == 0;
433}
434
435bool IPIs6Bone(const IPAddress& ip) {
436 return IPIsHelper(ip, k6BonePrefix, 16);
437}
438
439bool IPIs6To4(const IPAddress& ip) {
440 return IPIsHelper(ip, k6To4Prefix, 16);
441}
442
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000443bool IPIsLinkLocal(const IPAddress& ip) {
444 // Can't use the helper because the prefix is 10 bits.
445 in6_addr addr = ip.ipv6_address();
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000446 return addr.s6_addr[0] == 0xFE && addr.s6_addr[1] == 0x80;
447}
448
449// According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An
450// address which contains MAC will have its 11th and 12th bytes as FF:FE as well
451// as the U/L bit as 1.
452bool IPIsMacBased(const IPAddress& ip) {
453 in6_addr addr = ip.ipv6_address();
454 return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF &&
455 addr.s6_addr[12] == 0xFE);
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000456}
457
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000458bool IPIsSiteLocal(const IPAddress& ip) {
459 // Can't use the helper because the prefix is 10 bits.
460 in6_addr addr = ip.ipv6_address();
461 return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0;
462}
463
464bool IPIsULA(const IPAddress& ip) {
465 // Can't use the helper because the prefix is 7 bits.
466 in6_addr addr = ip.ipv6_address();
467 return (addr.s6_addr[0] & 0xFE) == 0xFC;
468}
469
470bool IPIsTeredo(const IPAddress& ip) {
471 return IPIsHelper(ip, kTeredoPrefix, 32);
472}
473
474bool IPIsV4Compatibility(const IPAddress& ip) {
475 return IPIsHelper(ip, kV4CompatibilityPrefix, 96);
476}
477
478bool IPIsV4Mapped(const IPAddress& ip) {
479 return IPIsHelper(ip, kV4MappedPrefix, 96);
480}
481
482int IPAddressPrecedence(const IPAddress& ip) {
483 // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo.
484 if (ip.family() == AF_INET) {
485 return 30;
486 } else if (ip.family() == AF_INET6) {
487 if (IPIsLoopback(ip)) {
488 return 60;
489 } else if (IPIsULA(ip)) {
490 return 50;
491 } else if (IPIsV4Mapped(ip)) {
492 return 30;
493 } else if (IPIs6To4(ip)) {
494 return 20;
495 } else if (IPIsTeredo(ip)) {
496 return 10;
497 } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) {
498 return 1;
499 } else {
500 // A 'normal' IPv6 address.
501 return 40;
502 }
503 }
504 return 0;
505}
506
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700507IPAddress GetLoopbackIP(int family) {
508 if (family == AF_INET) {
509 return rtc::IPAddress(INADDR_LOOPBACK);
510 }
511 if (family == AF_INET6) {
512 return rtc::IPAddress(in6addr_loopback);
513 }
514 return rtc::IPAddress();
515}
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800516
517IPAddress GetAnyIP(int family) {
518 if (family == AF_INET) {
519 return rtc::IPAddress(INADDR_ANY);
520 }
521 if (family == AF_INET6) {
522 return rtc::IPAddress(in6addr_any);
523 }
524 return rtc::IPAddress();
525}
526
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700527} // Namespace rtc