OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 // Prefixes used for categorizing IPv6 addresses. | 36 // Prefixes used for categorizing IPv6 addresses. |
37 static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 37 static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
38 0xFF, 0xFF, 0}}}; | 38 0xFF, 0xFF, 0}}}; |
39 static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}}; | 39 static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}}; |
40 static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}}; | 40 static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}}; |
41 static const in6_addr kV4CompatibilityPrefix = {{{0}}}; | 41 static const in6_addr kV4CompatibilityPrefix = {{{0}}}; |
42 static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}}; | 42 static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}}; |
43 | 43 |
44 bool IPAddress::strip_sensitive_ = false; | 44 bool IPAddress::strip_sensitive_ = false; |
45 | 45 |
46 static bool IsPrivateV4(uint32 ip); | 46 static bool IsPrivateV4(uint32_t ip); |
47 static in_addr ExtractMappedAddress(const in6_addr& addr); | 47 static in_addr ExtractMappedAddress(const in6_addr& addr); |
48 | 48 |
49 uint32 IPAddress::v4AddressAsHostOrderInteger() const { | 49 uint32_t IPAddress::v4AddressAsHostOrderInteger() const { |
50 if (family_ == AF_INET) { | 50 if (family_ == AF_INET) { |
51 return NetworkToHost32(u_.ip4.s_addr); | 51 return NetworkToHost32(u_.ip4.s_addr); |
52 } else { | 52 } else { |
53 return 0; | 53 return 0; |
54 } | 54 } |
55 } | 55 } |
56 | 56 |
57 bool IPAddress::IsNil() const { | 57 bool IPAddress::IsNil() const { |
58 return IPIsUnspec(*this); | 58 return IPIsUnspec(*this); |
59 } | 59 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 208 |
209 std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) { | 209 std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) { |
210 os << static_cast<const IPAddress&>(ip); | 210 os << static_cast<const IPAddress&>(ip); |
211 | 211 |
212 if (ip.family() == AF_INET6) | 212 if (ip.family() == AF_INET6) |
213 os << "|flags:0x" << std::hex << ip.ipv6_flags(); | 213 os << "|flags:0x" << std::hex << ip.ipv6_flags(); |
214 | 214 |
215 return os; | 215 return os; |
216 } | 216 } |
217 | 217 |
218 bool IsPrivateV4(uint32 ip_in_host_order) { | 218 bool IsPrivateV4(uint32_t ip_in_host_order) { |
219 return ((ip_in_host_order >> 24) == 127) || | 219 return ((ip_in_host_order >> 24) == 127) || |
220 ((ip_in_host_order >> 24) == 10) || | 220 ((ip_in_host_order >> 24) == 10) || |
221 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) || | 221 ((ip_in_host_order >> 20) == ((172 << 4) | 1)) || |
222 ((ip_in_host_order >> 16) == ((192 << 8) | 168)) || | 222 ((ip_in_host_order >> 16) == ((192 << 8) | 168)) || |
223 ((ip_in_host_order >> 16) == ((169 << 8) | 254)); | 223 ((ip_in_host_order >> 16) == ((169 << 8) | 254)); |
224 } | 224 } |
225 | 225 |
226 in_addr ExtractMappedAddress(const in6_addr& in6) { | 226 in_addr ExtractMappedAddress(const in6_addr& in6) { |
227 in_addr ipv4; | 227 in_addr ipv4; |
228 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr)); | 228 ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr)); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 return ip.family() == AF_UNSPEC; | 314 return ip.family() == AF_UNSPEC; |
315 } | 315 } |
316 | 316 |
317 size_t HashIP(const IPAddress& ip) { | 317 size_t HashIP(const IPAddress& ip) { |
318 switch (ip.family()) { | 318 switch (ip.family()) { |
319 case AF_INET: { | 319 case AF_INET: { |
320 return ip.ipv4_address().s_addr; | 320 return ip.ipv4_address().s_addr; |
321 } | 321 } |
322 case AF_INET6: { | 322 case AF_INET6: { |
323 in6_addr v6addr = ip.ipv6_address(); | 323 in6_addr v6addr = ip.ipv6_address(); |
324 const uint32* v6_as_ints = | 324 const uint32_t* v6_as_ints = |
325 reinterpret_cast<const uint32*>(&v6addr.s6_addr); | 325 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr); |
326 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3]; | 326 return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3]; |
327 } | 327 } |
328 } | 328 } |
329 return 0; | 329 return 0; |
330 } | 330 } |
331 | 331 |
332 IPAddress TruncateIP(const IPAddress& ip, int length) { | 332 IPAddress TruncateIP(const IPAddress& ip, int length) { |
333 if (length < 0) { | 333 if (length < 0) { |
334 return IPAddress(); | 334 return IPAddress(); |
335 } | 335 } |
336 if (ip.family() == AF_INET) { | 336 if (ip.family() == AF_INET) { |
337 if (length > 31) { | 337 if (length > 31) { |
338 return ip; | 338 return ip; |
339 } | 339 } |
340 if (length == 0) { | 340 if (length == 0) { |
341 return IPAddress(INADDR_ANY); | 341 return IPAddress(INADDR_ANY); |
342 } | 342 } |
343 int mask = (0xFFFFFFFF << (32 - length)); | 343 int mask = (0xFFFFFFFF << (32 - length)); |
344 uint32 host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr); | 344 uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr); |
345 in_addr masked; | 345 in_addr masked; |
346 masked.s_addr = HostToNetwork32(host_order_ip & mask); | 346 masked.s_addr = HostToNetwork32(host_order_ip & mask); |
347 return IPAddress(masked); | 347 return IPAddress(masked); |
348 } else if (ip.family() == AF_INET6) { | 348 } else if (ip.family() == AF_INET6) { |
349 if (length > 127) { | 349 if (length > 127) { |
350 return ip; | 350 return ip; |
351 } | 351 } |
352 if (length == 0) { | 352 if (length == 0) { |
353 return IPAddress(in6addr_any); | 353 return IPAddress(in6addr_any); |
354 } | 354 } |
355 in6_addr v6addr = ip.ipv6_address(); | 355 in6_addr v6addr = ip.ipv6_address(); |
356 int position = length / 32; | 356 int position = length / 32; |
357 int inner_length = 32 - (length - (position * 32)); | 357 int inner_length = 32 - (length - (position * 32)); |
358 // Note: 64bit mask constant needed to allow possible 32-bit left shift. | 358 // Note: 64bit mask constant needed to allow possible 32-bit left shift. |
359 uint32 inner_mask = 0xFFFFFFFFLL << inner_length; | 359 uint32_t inner_mask = 0xFFFFFFFFLL << inner_length; |
360 uint32* v6_as_ints = | 360 uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr); |
361 reinterpret_cast<uint32*>(&v6addr.s6_addr); | |
362 for (int i = 0; i < 4; ++i) { | 361 for (int i = 0; i < 4; ++i) { |
363 if (i == position) { | 362 if (i == position) { |
364 uint32 host_order_inner = NetworkToHost32(v6_as_ints[i]); | 363 uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]); |
365 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask); | 364 v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask); |
366 } else if (i > position) { | 365 } else if (i > position) { |
367 v6_as_ints[i] = 0; | 366 v6_as_ints[i] = 0; |
368 } | 367 } |
369 } | 368 } |
370 return IPAddress(v6addr); | 369 return IPAddress(v6addr); |
371 } | 370 } |
372 return IPAddress(); | 371 return IPAddress(); |
373 } | 372 } |
374 | 373 |
375 int CountIPMaskBits(IPAddress mask) { | 374 int CountIPMaskBits(IPAddress mask) { |
376 uint32 word_to_count = 0; | 375 uint32_t word_to_count = 0; |
377 int bits = 0; | 376 int bits = 0; |
378 switch (mask.family()) { | 377 switch (mask.family()) { |
379 case AF_INET: { | 378 case AF_INET: { |
380 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr); | 379 word_to_count = NetworkToHost32(mask.ipv4_address().s_addr); |
381 break; | 380 break; |
382 } | 381 } |
383 case AF_INET6: { | 382 case AF_INET6: { |
384 in6_addr v6addr = mask.ipv6_address(); | 383 in6_addr v6addr = mask.ipv6_address(); |
385 const uint32* v6_as_ints = | 384 const uint32_t* v6_as_ints = |
386 reinterpret_cast<const uint32*>(&v6addr.s6_addr); | 385 reinterpret_cast<const uint32_t*>(&v6addr.s6_addr); |
387 int i = 0; | 386 int i = 0; |
388 for (; i < 4; ++i) { | 387 for (; i < 4; ++i) { |
389 if (v6_as_ints[i] != 0xFFFFFFFF) { | 388 if (v6_as_ints[i] != 0xFFFFFFFF) { |
390 break; | 389 break; |
391 } | 390 } |
392 } | 391 } |
393 if (i < 4) { | 392 if (i < 4) { |
394 word_to_count = NetworkToHost32(v6_as_ints[i]); | 393 word_to_count = NetworkToHost32(v6_as_ints[i]); |
395 } | 394 } |
396 bits = (i * 32); | 395 bits = (i * 32); |
397 break; | 396 break; |
398 } | 397 } |
399 default: { | 398 default: { |
400 return 0; | 399 return 0; |
401 } | 400 } |
402 } | 401 } |
403 if (word_to_count == 0) { | 402 if (word_to_count == 0) { |
404 return bits; | 403 return bits; |
405 } | 404 } |
406 | 405 |
407 // Public domain bit-twiddling hack from: | 406 // Public domain bit-twiddling hack from: |
408 // http://graphics.stanford.edu/~seander/bithacks.html | 407 // http://graphics.stanford.edu/~seander/bithacks.html |
409 // Counts the trailing 0s in the word. | 408 // Counts the trailing 0s in the word. |
410 unsigned int zeroes = 32; | 409 unsigned int zeroes = 32; |
411 word_to_count &= -static_cast<int32>(word_to_count); | 410 word_to_count &= -static_cast<int32_t>(word_to_count); |
412 if (word_to_count) zeroes--; | 411 if (word_to_count) zeroes--; |
413 if (word_to_count & 0x0000FFFF) zeroes -= 16; | 412 if (word_to_count & 0x0000FFFF) zeroes -= 16; |
414 if (word_to_count & 0x00FF00FF) zeroes -= 8; | 413 if (word_to_count & 0x00FF00FF) zeroes -= 8; |
415 if (word_to_count & 0x0F0F0F0F) zeroes -= 4; | 414 if (word_to_count & 0x0F0F0F0F) zeroes -= 4; |
416 if (word_to_count & 0x33333333) zeroes -= 2; | 415 if (word_to_count & 0x33333333) zeroes -= 2; |
417 if (word_to_count & 0x55555555) zeroes -= 1; | 416 if (word_to_count & 0x55555555) zeroes -= 1; |
418 | 417 |
419 return bits + (32 - zeroes); | 418 return bits + (32 - zeroes); |
420 } | 419 } |
421 | 420 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 IPAddress GetLoopbackIP(int family) { | 500 IPAddress GetLoopbackIP(int family) { |
502 if (family == AF_INET) { | 501 if (family == AF_INET) { |
503 return rtc::IPAddress(INADDR_LOOPBACK); | 502 return rtc::IPAddress(INADDR_LOOPBACK); |
504 } | 503 } |
505 if (family == AF_INET6) { | 504 if (family == AF_INET6) { |
506 return rtc::IPAddress(in6addr_loopback); | 505 return rtc::IPAddress(in6addr_loopback); |
507 } | 506 } |
508 return rtc::IPAddress(); | 507 return rtc::IPAddress(); |
509 } | 508 } |
510 } // Namespace rtc | 509 } // Namespace rtc |
OLD | NEW |