| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <iterator> | 10 #include <iterator> |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 16 #include <windows.h> | 16 #include <windows.h> |
| 17 #include <iphlpapi.h> | 17 #include <iphlpapi.h> |
| 18 #include <winsock2.h> | 18 #include <winsock2.h> |
| 19 #include <ws2bth.h> |
| 19 #pragma comment(lib, "iphlpapi.lib") | 20 #pragma comment(lib, "iphlpapi.lib") |
| 20 #elif defined(OS_POSIX) | 21 #elif defined(OS_POSIX) |
| 21 #include <fcntl.h> | 22 #include <fcntl.h> |
| 22 #include <netdb.h> | 23 #include <netdb.h> |
| 23 #include <netinet/in.h> | 24 #include <netinet/in.h> |
| 24 #include <unistd.h> | 25 #include <unistd.h> |
| 25 #if !defined(OS_NACL) | 26 #if !defined(OS_NACL) |
| 26 #include <net/if.h> | 27 #include <net/if.h> |
| 27 #if !defined(OS_ANDROID) | 28 #if !defined(OS_ANDROID) |
| 28 #include <ifaddrs.h> | 29 #include <ifaddrs.h> |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 if (port) | 518 if (port) |
| 518 *port = base::NetToHost16(addr->sin_port); | 519 *port = base::NetToHost16(addr->sin_port); |
| 519 return true; | 520 return true; |
| 520 } | 521 } |
| 521 | 522 |
| 522 if (sock_addr->sa_family == AF_INET6) { | 523 if (sock_addr->sa_family == AF_INET6) { |
| 523 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) | 524 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) |
| 524 return false; | 525 return false; |
| 525 const struct sockaddr_in6* addr = | 526 const struct sockaddr_in6* addr = |
| 526 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); | 527 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); |
| 527 *address = reinterpret_cast<const unsigned char*>(&addr->sin6_addr); | 528 *address = reinterpret_cast<const uint8*>(&addr->sin6_addr); |
| 528 *address_len = kIPv6AddressSize; | 529 *address_len = kIPv6AddressSize; |
| 529 if (port) | 530 if (port) |
| 530 *port = base::NetToHost16(addr->sin6_port); | 531 *port = base::NetToHost16(addr->sin6_port); |
| 531 return true; | 532 return true; |
| 532 } | 533 } |
| 533 | 534 |
| 535 #if defined(OS_WIN) |
| 536 if (sock_addr->sa_family == AF_BTH) { |
| 537 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) |
| 538 return false; |
| 539 const SOCKADDR_BTH* addr = |
| 540 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr); |
| 541 *address = reinterpret_cast<const uint8*>(&addr->btAddr); |
| 542 *address_len = kBluetoothAddressSize; |
| 543 if (port) |
| 544 *port = addr->port; |
| 545 return true; |
| 546 } |
| 547 #endif |
| 548 |
| 534 return false; // Unrecognized |sa_family|. | 549 return false; // Unrecognized |sa_family|. |
| 535 } | 550 } |
| 536 | 551 |
| 537 std::string IPAddressToString(const uint8* address, | 552 std::string IPAddressToString(const uint8* address, |
| 538 size_t address_len) { | 553 size_t address_len) { |
| 539 std::string str; | 554 std::string str; |
| 540 url_canon::StdStringCanonOutput output(&str); | 555 url_canon::StdStringCanonOutput output(&str); |
| 541 | 556 |
| 542 if (address_len == kIPv4AddressSize) { | 557 if (address_len == kIPv4AddressSize) { |
| 543 url_canon::AppendIPv4Address(address, &output); | 558 url_canon::AppendIPv4Address(address, &output); |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 } | 999 } |
| 985 return a1.size() * CHAR_BIT; | 1000 return a1.size() * CHAR_BIT; |
| 986 } | 1001 } |
| 987 | 1002 |
| 988 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1003 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
| 989 IPAddressNumber all_ones(mask.size(), 0xFF); | 1004 IPAddressNumber all_ones(mask.size(), 0xFF); |
| 990 return CommonPrefixLength(mask, all_ones); | 1005 return CommonPrefixLength(mask, all_ones); |
| 991 } | 1006 } |
| 992 | 1007 |
| 993 } // namespace net | 1008 } // namespace net |
| OLD | NEW |