blob: 3b74d60036565463886e94258e0a93570a6fae5e [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"
30#include "webrtc/base/nethelpers.h"
31#include "webrtc/base/logging.h"
32#include "webrtc/base/win32.h"
33
34namespace rtc {
35
36// Prefixes used for categorizing IPv6 addresses.
37static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38 0xFF, 0xFF, 0}}};
39static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}};
40static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}};
41static const in6_addr kV4CompatibilityPrefix = {{{0}}};
42static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}};
43
Peter Boström0c4e06b2015-10-07 12:23:21 +020044static bool IsPrivateV4(uint32_t ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045static in_addr ExtractMappedAddress(const in6_addr& addr);
46
Peter Boström0c4e06b2015-10-07 12:23:21 +020047uint32_t IPAddress::v4AddressAsHostOrderInteger() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 if (family_ == AF_INET) {
49 return NetworkToHost32(u_.ip4.s_addr);
50 } else {
51 return 0;
52 }
53}
54
Guo-wei Shieh11477022015-08-15 09:28:41 -070055bool IPAddress::IsNil() const {
56 return IPIsUnspec(*this);
57}
58
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059size_t IPAddress::Size() const {
60 switch (family_) {
61 case AF_INET:
62 return sizeof(in_addr);
63 case AF_INET6:
64 return sizeof(in6_addr);
65 }
66 return 0;
67}
68
69
70bool IPAddress::operator==(const IPAddress &other) const {
71 if (family_ != other.family_) {
72 return false;
73 }
74 if (family_ == AF_INET) {
75 return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0;
76 }
77 if (family_ == AF_INET6) {
78 return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0;
79 }
80 return family_ == AF_UNSPEC;
81}
82
83bool IPAddress::operator!=(const IPAddress &other) const {
84 return !((*this) == other);
85}
86
87bool IPAddress::operator >(const IPAddress &other) const {
88 return (*this) != other && !((*this) < other);
89}
90
91bool IPAddress::operator <(const IPAddress &other) const {
92 // IPv4 is 'less than' IPv6
93 if (family_ != other.family_) {
94 if (family_ == AF_UNSPEC) {
95 return true;
96 }
97 if (family_ == AF_INET && other.family_ == AF_INET6) {
98 return true;
99 }
100 return false;
101 }
102 // Comparing addresses of the same family.
103 switch (family_) {
104 case AF_INET: {
105 return NetworkToHost32(u_.ip4.s_addr) <
106 NetworkToHost32(other.u_.ip4.s_addr);
107 }
108 case AF_INET6: {
109 return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0;
110 }
111 }
112 // Catches AF_UNSPEC and invalid addresses.
113 return false;
114}
115
116std::ostream& operator<<(std::ostream& os, const IPAddress& ip) {
117 os << ip.ToString();
118 return os;
119}
120
121in6_addr IPAddress::ipv6_address() const {
122 return u_.ip6;
123}
124
125in_addr IPAddress::ipv4_address() const {
126 return u_.ip4;
127}
128
129std::string IPAddress::ToString() const {
130 if (family_ != AF_INET && family_ != AF_INET6) {
131 return std::string();
132 }
133 char buf[INET6_ADDRSTRLEN] = {0};
134 const void* src = &u_.ip4;
135 if (family_ == AF_INET6) {
136 src = &u_.ip6;
137 }
138 if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) {
139 return std::string();
140 }
141 return std::string(buf);
142}
143
144std::string IPAddress::ToSensitiveString() const {
Peter Boströmcdb38e52015-11-26 00:35:49 +0100145#if !defined(NDEBUG)
146 // Return non-stripped in debug.
147 return ToString();
148#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149 switch (family_) {
150 case AF_INET: {
151 std::string address = ToString();
152 size_t find_pos = address.rfind('.');
153 if (find_pos == std::string::npos)
154 return std::string();
155 address.resize(find_pos);
156 address += ".x";
157 return address;
158 }
159 case AF_INET6: {
160 // TODO(grunell): Return a string of format 1:2:3:x:x:x:x:x or such
161 // instead of zeroing out.
162 return TruncateIP(*this, 128 - 80).ToString();
163 }
164 }
165 return std::string();
Peter Boströmcdb38e52015-11-26 00:35:49 +0100166#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167}
168
169IPAddress IPAddress::Normalized() const {
170 if (family_ != AF_INET6) {
171 return *this;
172 }
173 if (!IPIsV4Mapped(*this)) {
174 return *this;
175 }
176 in_addr addr = ExtractMappedAddress(u_.ip6);
177 return IPAddress(addr);
178}
179
180IPAddress IPAddress::AsIPv6Address() const {
181 if (family_ != AF_INET) {
182 return *this;
183 }
184 in6_addr v6addr = kV4MappedPrefix;
185 ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr));
186 return IPAddress(v6addr);
187}
188
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000189bool InterfaceAddress::operator==(const InterfaceAddress &other) const {
190 return ipv6_flags_ == other.ipv6_flags() &&
191 static_cast<const IPAddress&>(*this) == other;
192}
193
194bool InterfaceAddress::operator!=(const InterfaceAddress &other) const {
195 return !((*this) == other);
196}
197
198const InterfaceAddress& InterfaceAddress::operator=(
199 const InterfaceAddress& other) {
200 ipv6_flags_ = other.ipv6_flags_;
201 static_cast<IPAddress&>(*this) = other;
202 return *this;
203}
204
205std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) {
206 os << static_cast<const IPAddress&>(ip);
207
208 if (ip.family() == AF_INET6)
209 os << "|flags:0x" << std::hex << ip.ipv6_flags();
210
211 return os;
212}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214bool IsPrivateV4(uint32_t ip_in_host_order) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215 return ((ip_in_host_order >> 24) == 127) ||
216 ((ip_in_host_order >> 24) == 10) ||
217 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) ||
218 ((ip_in_host_order >> 16) == ((192 << 8) | 168)) ||
219 ((ip_in_host_order >> 16) == ((169 << 8) | 254));
220}
221
222in_addr ExtractMappedAddress(const in6_addr& in6) {
223 in_addr ipv4;
224 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr));
225 return ipv4;
226}
227
228bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) {
229 if (!info || !info->ai_addr) {
230 return false;
231 }
232 if (info->ai_addr->sa_family == AF_INET) {
233 sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
234 *out = IPAddress(addr->sin_addr);
235 return true;
236 } else if (info->ai_addr->sa_family == AF_INET6) {
237 sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr);
238 *out = IPAddress(addr->sin6_addr);
239 return true;
240 }
241 return false;
242}
243
244bool IPFromString(const std::string& str, IPAddress* out) {
245 if (!out) {
246 return false;
247 }
248 in_addr addr;
249 if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) {
250 in6_addr addr6;
251 if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) {
252 *out = IPAddress();
253 return false;
254 }
255 *out = IPAddress(addr6);
256 } else {
257 *out = IPAddress(addr);
258 }
259 return true;
260}
261
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000262bool IPFromString(const std::string& str, int flags,
263 InterfaceAddress* out) {
264 IPAddress ip;
265 if (!IPFromString(str, &ip)) {
266 return false;
267 }
268
269 *out = InterfaceAddress(ip, flags);
270 return true;
271}
272
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000273bool IPIsAny(const IPAddress& ip) {
274 switch (ip.family()) {
275 case AF_INET:
276 return ip == IPAddress(INADDR_ANY);
277 case AF_INET6:
guoweis@webrtc.org59ae5ff2015-03-01 23:45:16 +0000278 return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 case AF_UNSPEC:
280 return false;
281 }
282 return false;
283}
284
285bool IPIsLoopback(const IPAddress& ip) {
286 switch (ip.family()) {
287 case AF_INET: {
288 return ip == IPAddress(INADDR_LOOPBACK);
289 }
290 case AF_INET6: {
291 return ip == IPAddress(in6addr_loopback);
292 }
293 }
294 return false;
295}
296
297bool IPIsPrivate(const IPAddress& ip) {
298 switch (ip.family()) {
299 case AF_INET: {
300 return IsPrivateV4(ip.v4AddressAsHostOrderInteger());
301 }
302 case AF_INET6: {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000303 return IPIsLinkLocal(ip) || IPIsLoopback(ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 }
305 }
306 return false;
307}
308
309bool IPIsUnspec(const IPAddress& ip) {
310 return ip.family() == AF_UNSPEC;
311}
312
313size_t HashIP(const IPAddress& ip) {
314 switch (ip.family()) {
315 case AF_INET: {
316 return ip.ipv4_address().s_addr;
317 }
318 case AF_INET6: {
319 in6_addr v6addr = ip.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200320 const uint32_t* v6_as_ints =
321 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3];
323 }
324 }
325 return 0;
326}
327
328IPAddress TruncateIP(const IPAddress& ip, int length) {
329 if (length < 0) {
330 return IPAddress();
331 }
332 if (ip.family() == AF_INET) {
333 if (length > 31) {
334 return ip;
335 }
336 if (length == 0) {
337 return IPAddress(INADDR_ANY);
338 }
339 int mask = (0xFFFFFFFF << (32 - length));
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340 uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341 in_addr masked;
342 masked.s_addr = HostToNetwork32(host_order_ip & mask);
343 return IPAddress(masked);
344 } else if (ip.family() == AF_INET6) {
345 if (length > 127) {
346 return ip;
347 }
348 if (length == 0) {
349 return IPAddress(in6addr_any);
350 }
351 in6_addr v6addr = ip.ipv6_address();
352 int position = length / 32;
353 int inner_length = 32 - (length - (position * 32));
354 // Note: 64bit mask constant needed to allow possible 32-bit left shift.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200355 uint32_t inner_mask = 0xFFFFFFFFLL << inner_length;
356 uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000357 for (int i = 0; i < 4; ++i) {
358 if (i == position) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200359 uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000360 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask);
361 } else if (i > position) {
362 v6_as_ints[i] = 0;
363 }
364 }
365 return IPAddress(v6addr);
366 }
367 return IPAddress();
368}
369
370int CountIPMaskBits(IPAddress mask) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200371 uint32_t word_to_count = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372 int bits = 0;
373 switch (mask.family()) {
374 case AF_INET: {
375 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr);
376 break;
377 }
378 case AF_INET6: {
379 in6_addr v6addr = mask.ipv6_address();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200380 const uint32_t* v6_as_ints =
381 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382 int i = 0;
383 for (; i < 4; ++i) {
384 if (v6_as_ints[i] != 0xFFFFFFFF) {
385 break;
386 }
387 }
388 if (i < 4) {
389 word_to_count = NetworkToHost32(v6_as_ints[i]);
390 }
391 bits = (i * 32);
392 break;
393 }
394 default: {
395 return 0;
396 }
397 }
398 if (word_to_count == 0) {
399 return bits;
400 }
401
402 // Public domain bit-twiddling hack from:
403 // http://graphics.stanford.edu/~seander/bithacks.html
404 // Counts the trailing 0s in the word.
405 unsigned int zeroes = 32;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200406 word_to_count &= -static_cast<int32_t>(word_to_count);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 if (word_to_count) zeroes--;
408 if (word_to_count & 0x0000FFFF) zeroes -= 16;
409 if (word_to_count & 0x00FF00FF) zeroes -= 8;
410 if (word_to_count & 0x0F0F0F0F) zeroes -= 4;
411 if (word_to_count & 0x33333333) zeroes -= 2;
412 if (word_to_count & 0x55555555) zeroes -= 1;
413
414 return bits + (32 - zeroes);
415}
416
417bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) {
418 // Helper method for checking IP prefix matches (but only on whole byte
419 // lengths). Length is in bits.
420 in6_addr addr = ip.ipv6_address();
421 return ::memcmp(&addr, &tomatch, (length >> 3)) == 0;
422}
423
424bool IPIs6Bone(const IPAddress& ip) {
425 return IPIsHelper(ip, k6BonePrefix, 16);
426}
427
428bool IPIs6To4(const IPAddress& ip) {
429 return IPIsHelper(ip, k6To4Prefix, 16);
430}
431
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000432bool IPIsLinkLocal(const IPAddress& ip) {
433 // Can't use the helper because the prefix is 10 bits.
434 in6_addr addr = ip.ipv6_address();
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000435 return addr.s6_addr[0] == 0xFE && addr.s6_addr[1] == 0x80;
436}
437
438// According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An
439// address which contains MAC will have its 11th and 12th bytes as FF:FE as well
440// as the U/L bit as 1.
441bool IPIsMacBased(const IPAddress& ip) {
442 in6_addr addr = ip.ipv6_address();
443 return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF &&
444 addr.s6_addr[12] == 0xFE);
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000445}
446
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447bool IPIsSiteLocal(const IPAddress& ip) {
448 // Can't use the helper because the prefix is 10 bits.
449 in6_addr addr = ip.ipv6_address();
450 return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0;
451}
452
453bool IPIsULA(const IPAddress& ip) {
454 // Can't use the helper because the prefix is 7 bits.
455 in6_addr addr = ip.ipv6_address();
456 return (addr.s6_addr[0] & 0xFE) == 0xFC;
457}
458
459bool IPIsTeredo(const IPAddress& ip) {
460 return IPIsHelper(ip, kTeredoPrefix, 32);
461}
462
463bool IPIsV4Compatibility(const IPAddress& ip) {
464 return IPIsHelper(ip, kV4CompatibilityPrefix, 96);
465}
466
467bool IPIsV4Mapped(const IPAddress& ip) {
468 return IPIsHelper(ip, kV4MappedPrefix, 96);
469}
470
471int IPAddressPrecedence(const IPAddress& ip) {
472 // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo.
473 if (ip.family() == AF_INET) {
474 return 30;
475 } else if (ip.family() == AF_INET6) {
476 if (IPIsLoopback(ip)) {
477 return 60;
478 } else if (IPIsULA(ip)) {
479 return 50;
480 } else if (IPIsV4Mapped(ip)) {
481 return 30;
482 } else if (IPIs6To4(ip)) {
483 return 20;
484 } else if (IPIsTeredo(ip)) {
485 return 10;
486 } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) {
487 return 1;
488 } else {
489 // A 'normal' IPv6 address.
490 return 40;
491 }
492 }
493 return 0;
494}
495
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700496IPAddress GetLoopbackIP(int family) {
497 if (family == AF_INET) {
498 return rtc::IPAddress(INADDR_LOOPBACK);
499 }
500 if (family == AF_INET6) {
501 return rtc::IPAddress(in6addr_loopback);
502 }
503 return rtc::IPAddress();
504}
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800505
506IPAddress GetAnyIP(int family) {
507 if (family == AF_INET) {
508 return rtc::IPAddress(INADDR_ANY);
509 }
510 if (family == AF_INET6) {
511 return rtc::IPAddress(in6addr_any);
512 }
513 return rtc::IPAddress();
514}
515
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700516} // Namespace rtc