blob: 768a7092a3c797db17c86748cc15b9387cd3a1dd [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
44bool IPAddress::strip_sensitive_ = false;
45
46static bool IsPrivateV4(uint32 ip);
47static in_addr ExtractMappedAddress(const in6_addr& addr);
48
49uint32 IPAddress::v4AddressAsHostOrderInteger() const {
50 if (family_ == AF_INET) {
51 return NetworkToHost32(u_.ip4.s_addr);
52 } else {
53 return 0;
54 }
55}
56
Guo-wei Shieh11477022015-08-15 09:28:41 -070057bool IPAddress::IsNil() const {
58 return IPIsUnspec(*this);
59}
60
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061size_t IPAddress::Size() const {
62 switch (family_) {
63 case AF_INET:
64 return sizeof(in_addr);
65 case AF_INET6:
66 return sizeof(in6_addr);
67 }
68 return 0;
69}
70
71
72bool IPAddress::operator==(const IPAddress &other) const {
73 if (family_ != other.family_) {
74 return false;
75 }
76 if (family_ == AF_INET) {
77 return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0;
78 }
79 if (family_ == AF_INET6) {
80 return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0;
81 }
82 return family_ == AF_UNSPEC;
83}
84
85bool IPAddress::operator!=(const IPAddress &other) const {
86 return !((*this) == other);
87}
88
89bool IPAddress::operator >(const IPAddress &other) const {
90 return (*this) != other && !((*this) < other);
91}
92
93bool IPAddress::operator <(const IPAddress &other) const {
94 // IPv4 is 'less than' IPv6
95 if (family_ != other.family_) {
96 if (family_ == AF_UNSPEC) {
97 return true;
98 }
99 if (family_ == AF_INET && other.family_ == AF_INET6) {
100 return true;
101 }
102 return false;
103 }
104 // Comparing addresses of the same family.
105 switch (family_) {
106 case AF_INET: {
107 return NetworkToHost32(u_.ip4.s_addr) <
108 NetworkToHost32(other.u_.ip4.s_addr);
109 }
110 case AF_INET6: {
111 return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0;
112 }
113 }
114 // Catches AF_UNSPEC and invalid addresses.
115 return false;
116}
117
118std::ostream& operator<<(std::ostream& os, const IPAddress& ip) {
119 os << ip.ToString();
120 return os;
121}
122
123in6_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 {
147 if (!strip_sensitive_)
148 return ToString();
149
150 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: {
161 // TODO(grunell): Return a string of format 1:2:3:x:x:x:x:x or such
162 // instead of zeroing out.
163 return TruncateIP(*this, 128 - 80).ToString();
164 }
165 }
166 return std::string();
167}
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
189void IPAddress::set_strip_sensitive(bool enable) {
190 strip_sensitive_ = enable;
191}
192
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000193bool InterfaceAddress::operator==(const InterfaceAddress &other) const {
194 return ipv6_flags_ == other.ipv6_flags() &&
195 static_cast<const IPAddress&>(*this) == other;
196}
197
198bool InterfaceAddress::operator!=(const InterfaceAddress &other) const {
199 return !((*this) == other);
200}
201
202const InterfaceAddress& InterfaceAddress::operator=(
203 const InterfaceAddress& other) {
204 ipv6_flags_ = other.ipv6_flags_;
205 static_cast<IPAddress&>(*this) = other;
206 return *this;
207}
208
209std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) {
210 os << static_cast<const IPAddress&>(ip);
211
212 if (ip.family() == AF_INET6)
213 os << "|flags:0x" << std::hex << ip.ipv6_flags();
214
215 return os;
216}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000217
218bool IsPrivateV4(uint32 ip_in_host_order) {
219 return ((ip_in_host_order >> 24) == 127) ||
220 ((ip_in_host_order >> 24) == 10) ||
221 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) ||
222 ((ip_in_host_order >> 16) == ((192 << 8) | 168)) ||
223 ((ip_in_host_order >> 16) == ((169 << 8) | 254));
224}
225
226in_addr ExtractMappedAddress(const in6_addr& in6) {
227 in_addr ipv4;
228 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr));
229 return ipv4;
230}
231
232bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) {
233 if (!info || !info->ai_addr) {
234 return false;
235 }
236 if (info->ai_addr->sa_family == AF_INET) {
237 sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
238 *out = IPAddress(addr->sin_addr);
239 return true;
240 } else if (info->ai_addr->sa_family == AF_INET6) {
241 sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr);
242 *out = IPAddress(addr->sin6_addr);
243 return true;
244 }
245 return false;
246}
247
248bool IPFromString(const std::string& str, IPAddress* out) {
249 if (!out) {
250 return false;
251 }
252 in_addr addr;
253 if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) {
254 in6_addr addr6;
255 if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) {
256 *out = IPAddress();
257 return false;
258 }
259 *out = IPAddress(addr6);
260 } else {
261 *out = IPAddress(addr);
262 }
263 return true;
264}
265
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000266bool IPFromString(const std::string& str, int flags,
267 InterfaceAddress* out) {
268 IPAddress ip;
269 if (!IPFromString(str, &ip)) {
270 return false;
271 }
272
273 *out = InterfaceAddress(ip, flags);
274 return true;
275}
276
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277bool IPIsAny(const IPAddress& ip) {
278 switch (ip.family()) {
279 case AF_INET:
280 return ip == IPAddress(INADDR_ANY);
281 case AF_INET6:
guoweis@webrtc.org59ae5ff2015-03-01 23:45:16 +0000282 return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 case AF_UNSPEC:
284 return false;
285 }
286 return false;
287}
288
289bool IPIsLoopback(const IPAddress& ip) {
290 switch (ip.family()) {
291 case AF_INET: {
292 return ip == IPAddress(INADDR_LOOPBACK);
293 }
294 case AF_INET6: {
295 return ip == IPAddress(in6addr_loopback);
296 }
297 }
298 return false;
299}
300
301bool IPIsPrivate(const IPAddress& ip) {
302 switch (ip.family()) {
303 case AF_INET: {
304 return IsPrivateV4(ip.v4AddressAsHostOrderInteger());
305 }
306 case AF_INET6: {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000307 return IPIsLinkLocal(ip) || IPIsLoopback(ip);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308 }
309 }
310 return false;
311}
312
313bool IPIsUnspec(const IPAddress& ip) {
314 return ip.family() == AF_UNSPEC;
315}
316
317size_t HashIP(const IPAddress& ip) {
318 switch (ip.family()) {
319 case AF_INET: {
320 return ip.ipv4_address().s_addr;
321 }
322 case AF_INET6: {
323 in6_addr v6addr = ip.ipv6_address();
324 const uint32* v6_as_ints =
325 reinterpret_cast<const uint32*>(&v6addr.s6_addr);
326 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3];
327 }
328 }
329 return 0;
330}
331
332IPAddress TruncateIP(const IPAddress& ip, int length) {
333 if (length < 0) {
334 return IPAddress();
335 }
336 if (ip.family() == AF_INET) {
337 if (length > 31) {
338 return ip;
339 }
340 if (length == 0) {
341 return IPAddress(INADDR_ANY);
342 }
343 int mask = (0xFFFFFFFF << (32 - length));
344 uint32 host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr);
345 in_addr masked;
346 masked.s_addr = HostToNetwork32(host_order_ip & mask);
347 return IPAddress(masked);
348 } else if (ip.family() == AF_INET6) {
349 if (length > 127) {
350 return ip;
351 }
352 if (length == 0) {
353 return IPAddress(in6addr_any);
354 }
355 in6_addr v6addr = ip.ipv6_address();
356 int position = length / 32;
357 int inner_length = 32 - (length - (position * 32));
358 // Note: 64bit mask constant needed to allow possible 32-bit left shift.
359 uint32 inner_mask = 0xFFFFFFFFLL << inner_length;
360 uint32* v6_as_ints =
361 reinterpret_cast<uint32*>(&v6addr.s6_addr);
362 for (int i = 0; i < 4; ++i) {
363 if (i == position) {
364 uint32 host_order_inner = NetworkToHost32(v6_as_ints[i]);
365 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask);
366 } else if (i > position) {
367 v6_as_ints[i] = 0;
368 }
369 }
370 return IPAddress(v6addr);
371 }
372 return IPAddress();
373}
374
375int CountIPMaskBits(IPAddress mask) {
376 uint32 word_to_count = 0;
377 int bits = 0;
378 switch (mask.family()) {
379 case AF_INET: {
380 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr);
381 break;
382 }
383 case AF_INET6: {
384 in6_addr v6addr = mask.ipv6_address();
385 const uint32* v6_as_ints =
386 reinterpret_cast<const uint32*>(&v6addr.s6_addr);
387 int i = 0;
388 for (; i < 4; ++i) {
389 if (v6_as_ints[i] != 0xFFFFFFFF) {
390 break;
391 }
392 }
393 if (i < 4) {
394 word_to_count = NetworkToHost32(v6_as_ints[i]);
395 }
396 bits = (i * 32);
397 break;
398 }
399 default: {
400 return 0;
401 }
402 }
403 if (word_to_count == 0) {
404 return bits;
405 }
406
407 // Public domain bit-twiddling hack from:
408 // http://graphics.stanford.edu/~seander/bithacks.html
409 // Counts the trailing 0s in the word.
410 unsigned int zeroes = 32;
411 word_to_count &= -static_cast<int32>(word_to_count);
412 if (word_to_count) zeroes--;
413 if (word_to_count & 0x0000FFFF) zeroes -= 16;
414 if (word_to_count & 0x00FF00FF) zeroes -= 8;
415 if (word_to_count & 0x0F0F0F0F) zeroes -= 4;
416 if (word_to_count & 0x33333333) zeroes -= 2;
417 if (word_to_count & 0x55555555) zeroes -= 1;
418
419 return bits + (32 - zeroes);
420}
421
422bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) {
423 // Helper method for checking IP prefix matches (but only on whole byte
424 // lengths). Length is in bits.
425 in6_addr addr = ip.ipv6_address();
426 return ::memcmp(&addr, &tomatch, (length >> 3)) == 0;
427}
428
429bool IPIs6Bone(const IPAddress& ip) {
430 return IPIsHelper(ip, k6BonePrefix, 16);
431}
432
433bool IPIs6To4(const IPAddress& ip) {
434 return IPIsHelper(ip, k6To4Prefix, 16);
435}
436
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000437bool IPIsLinkLocal(const IPAddress& ip) {
438 // Can't use the helper because the prefix is 10 bits.
439 in6_addr addr = ip.ipv6_address();
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000440 return addr.s6_addr[0] == 0xFE && addr.s6_addr[1] == 0x80;
441}
442
443// According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An
444// address which contains MAC will have its 11th and 12th bytes as FF:FE as well
445// as the U/L bit as 1.
446bool IPIsMacBased(const IPAddress& ip) {
447 in6_addr addr = ip.ipv6_address();
448 return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF &&
449 addr.s6_addr[12] == 0xFE);
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000450}
451
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452bool IPIsSiteLocal(const IPAddress& ip) {
453 // Can't use the helper because the prefix is 10 bits.
454 in6_addr addr = ip.ipv6_address();
455 return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0;
456}
457
458bool IPIsULA(const IPAddress& ip) {
459 // Can't use the helper because the prefix is 7 bits.
460 in6_addr addr = ip.ipv6_address();
461 return (addr.s6_addr[0] & 0xFE) == 0xFC;
462}
463
464bool IPIsTeredo(const IPAddress& ip) {
465 return IPIsHelper(ip, kTeredoPrefix, 32);
466}
467
468bool IPIsV4Compatibility(const IPAddress& ip) {
469 return IPIsHelper(ip, kV4CompatibilityPrefix, 96);
470}
471
472bool IPIsV4Mapped(const IPAddress& ip) {
473 return IPIsHelper(ip, kV4MappedPrefix, 96);
474}
475
476int IPAddressPrecedence(const IPAddress& ip) {
477 // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo.
478 if (ip.family() == AF_INET) {
479 return 30;
480 } else if (ip.family() == AF_INET6) {
481 if (IPIsLoopback(ip)) {
482 return 60;
483 } else if (IPIsULA(ip)) {
484 return 50;
485 } else if (IPIsV4Mapped(ip)) {
486 return 30;
487 } else if (IPIs6To4(ip)) {
488 return 20;
489 } else if (IPIsTeredo(ip)) {
490 return 10;
491 } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) {
492 return 1;
493 } else {
494 // A 'normal' IPv6 address.
495 return 40;
496 }
497 }
498 return 0;
499}
500
501} // Namespace talk base