OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <algorithm> | |
8 #include <map> | |
9 #include <unicode/regex.h> | 7 #include <unicode/regex.h> |
10 #include <unicode/ucnv.h> | 8 #include <unicode/ucnv.h> |
11 #include <unicode/uidna.h> | 9 #include <unicode/uidna.h> |
12 #include <unicode/ulocdata.h> | 10 #include <unicode/ulocdata.h> |
13 #include <unicode/uniset.h> | 11 #include <unicode/uniset.h> |
14 #include <unicode/uscript.h> | 12 #include <unicode/uscript.h> |
15 #include <unicode/uset.h> | 13 #include <unicode/uset.h> |
| 14 #include <algorithm> |
| 15 #include <map> |
16 | 16 |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 | 18 |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 #include <windows.h> | 20 #include <windows.h> |
21 #include <winsock2.h> | 21 #include <winsock2.h> |
22 #include <ws2tcpip.h> | |
23 #include <wspiapi.h> // Needed for Win2k compat. | 22 #include <wspiapi.h> // Needed for Win2k compat. |
24 #elif defined(OS_POSIX) | 23 #elif defined(OS_POSIX) |
25 #include <fcntl.h> | 24 #include <fcntl.h> |
26 #include <ifaddrs.h> | 25 #include <ifaddrs.h> |
27 #include <netdb.h> | 26 #include <netdb.h> |
28 #include <net/if.h> | 27 #include <net/if.h> |
29 #include <netinet/in.h> | 28 #include <netinet/in.h> |
30 #include <sys/socket.h> | |
31 #endif | 29 #endif |
32 | 30 |
33 #include "base/base64.h" | 31 #include "base/base64.h" |
34 #include "base/basictypes.h" | 32 #include "base/basictypes.h" |
35 #include "base/file_path.h" | 33 #include "base/file_path.h" |
36 #include "base/file_util.h" | 34 #include "base/file_util.h" |
37 #include "base/i18n/file_util_icu.h" | 35 #include "base/i18n/file_util_icu.h" |
38 #include "base/i18n/icu_string_conversions.h" | 36 #include "base/i18n/icu_string_conversions.h" |
39 #include "base/i18n/time_formatting.h" | 37 #include "base/i18n/time_formatting.h" |
40 #include "base/json/string_escape.h" | 38 #include "base/json/string_escape.h" |
(...skipping 1531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1572 | 1570 |
1573 std::string GetHostAndOptionalPort(const GURL& url) { | 1571 std::string GetHostAndOptionalPort(const GURL& url) { |
1574 // For IPv6 literals, GURL::host() already includes the brackets | 1572 // For IPv6 literals, GURL::host() already includes the brackets |
1575 // so it is safe to just append a colon. | 1573 // so it is safe to just append a colon. |
1576 if (url.has_port()) | 1574 if (url.has_port()) |
1577 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); | 1575 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); |
1578 return url.host(); | 1576 return url.host(); |
1579 } | 1577 } |
1580 | 1578 |
1581 std::string NetAddressToString(const struct addrinfo* net_address) { | 1579 std::string NetAddressToString(const struct addrinfo* net_address) { |
| 1580 return NetAddressToString(net_address->ai_addr, net_address->ai_addrlen); |
| 1581 } |
| 1582 |
| 1583 std::string NetAddressToString(const struct sockaddr* net_address, |
| 1584 socklen_t address_len) { |
1582 #if defined(OS_WIN) | 1585 #if defined(OS_WIN) |
1583 EnsureWinsockInit(); | 1586 EnsureWinsockInit(); |
1584 #endif | 1587 #endif |
1585 | 1588 |
1586 // This buffer is large enough to fit the biggest IPv6 string. | 1589 // This buffer is large enough to fit the biggest IPv6 string. |
1587 char buffer[INET6_ADDRSTRLEN]; | 1590 char buffer[INET6_ADDRSTRLEN]; |
1588 | 1591 |
1589 int result = getnameinfo(net_address->ai_addr, | 1592 int result = getnameinfo(net_address, address_len, buffer, sizeof(buffer), |
1590 net_address->ai_addrlen, buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST); | 1593 NULL, 0, NI_NUMERICHOST); |
1591 | 1594 |
1592 if (result != 0) { | 1595 if (result != 0) { |
1593 DVLOG(1) << "getnameinfo() failed with " << result; | 1596 DVLOG(1) << "getnameinfo() failed with " << result << ": " |
| 1597 << gai_strerror(result); |
1594 buffer[0] = '\0'; | 1598 buffer[0] = '\0'; |
1595 } | 1599 } |
1596 return std::string(buffer); | 1600 return std::string(buffer); |
1597 } | 1601 } |
1598 | 1602 |
1599 std::string NetAddressToStringWithPort(const struct addrinfo* net_address) { | 1603 std::string NetAddressToStringWithPort(const struct addrinfo* net_address) { |
1600 std::string ip_address_string = NetAddressToString(net_address); | 1604 return NetAddressToStringWithPort( |
| 1605 net_address->ai_addr, net_address->ai_addrlen); |
| 1606 } |
| 1607 std::string NetAddressToStringWithPort(const struct sockaddr* net_address, |
| 1608 socklen_t address_len) { |
| 1609 std::string ip_address_string = NetAddressToString(net_address, address_len); |
1601 if (ip_address_string.empty()) | 1610 if (ip_address_string.empty()) |
1602 return std::string(); // Failed. | 1611 return std::string(); // Failed. |
1603 | 1612 |
1604 int port = GetPortFromAddrinfo(net_address); | 1613 int port = GetPortFromSockaddr(net_address, address_len); |
1605 | 1614 |
1606 if (ip_address_string.find(':') != std::string::npos) { | 1615 if (ip_address_string.find(':') != std::string::npos) { |
1607 // Surround with square brackets to avoid ambiguity. | 1616 // Surround with square brackets to avoid ambiguity. |
1608 return base::StringPrintf("[%s]:%d", ip_address_string.c_str(), port); | 1617 return base::StringPrintf("[%s]:%d", ip_address_string.c_str(), port); |
1609 } | 1618 } |
1610 | 1619 |
1611 return base::StringPrintf("%s:%d", ip_address_string.c_str(), port); | 1620 return base::StringPrintf("%s:%d", ip_address_string.c_str(), port); |
1612 } | 1621 } |
1613 | 1622 |
1614 std::string GetHostName() { | 1623 std::string GetHostName() { |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2032 unsigned char mask = 0xFF << (8 - remaining_bits); | 2041 unsigned char mask = 0xFF << (8 - remaining_bits); |
2033 int i = num_entire_bytes_in_prefix; | 2042 int i = num_entire_bytes_in_prefix; |
2034 if ((ip_number[i] & mask) != (ip_prefix[i] & mask)) | 2043 if ((ip_number[i] & mask) != (ip_prefix[i] & mask)) |
2035 return false; | 2044 return false; |
2036 } | 2045 } |
2037 | 2046 |
2038 return true; | 2047 return true; |
2039 } | 2048 } |
2040 | 2049 |
2041 // Returns the port field of the sockaddr in |info|. | 2050 // Returns the port field of the sockaddr in |info|. |
2042 uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info) { | 2051 uint16* GetPortFieldFromAddrinfo(struct addrinfo* info) { |
| 2052 const struct addrinfo* const_info = info; |
| 2053 const uint16* port_field = GetPortFieldFromAddrinfo(const_info); |
| 2054 return const_cast<uint16*>(port_field); |
| 2055 } |
| 2056 |
| 2057 const uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info) { |
2043 DCHECK(info); | 2058 DCHECK(info); |
2044 if (info->ai_family == AF_INET) { | 2059 const struct sockaddr* address = info->ai_addr; |
2045 DCHECK_EQ(sizeof(sockaddr_in), static_cast<size_t>(info->ai_addrlen)); | 2060 DCHECK(address); |
2046 struct sockaddr_in* sockaddr = | 2061 DCHECK_EQ(info->ai_family, address->sa_family); |
2047 reinterpret_cast<struct sockaddr_in*>(info->ai_addr); | 2062 return GetPortFieldFromSockaddr(address, info->ai_addrlen); |
| 2063 } |
| 2064 |
| 2065 int GetPortFromAddrinfo(const struct addrinfo* info) { |
| 2066 const uint16* port_field = GetPortFieldFromAddrinfo(info); |
| 2067 if (!port_field) |
| 2068 return -1; |
| 2069 return ntohs(*port_field); |
| 2070 } |
| 2071 |
| 2072 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, |
| 2073 socklen_t address_len) { |
| 2074 if (address->sa_family == AF_INET) { |
| 2075 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); |
| 2076 const struct sockaddr_in* sockaddr = |
| 2077 reinterpret_cast<const struct sockaddr_in*>(address); |
2048 return &sockaddr->sin_port; | 2078 return &sockaddr->sin_port; |
2049 } else if (info->ai_family == AF_INET6) { | 2079 } else if (address->sa_family == AF_INET6) { |
2050 DCHECK_EQ(sizeof(sockaddr_in6), static_cast<size_t>(info->ai_addrlen)); | 2080 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); |
2051 struct sockaddr_in6* sockaddr = | 2081 const struct sockaddr_in6* sockaddr = |
2052 reinterpret_cast<struct sockaddr_in6*>(info->ai_addr); | 2082 reinterpret_cast<const struct sockaddr_in6*>(address); |
2053 return &sockaddr->sin6_port; | 2083 return &sockaddr->sin6_port; |
2054 } else { | 2084 } else { |
2055 NOTREACHED(); | 2085 NOTREACHED(); |
2056 return NULL; | 2086 return NULL; |
2057 } | 2087 } |
2058 } | 2088 } |
2059 | 2089 |
2060 int GetPortFromAddrinfo(const struct addrinfo* info) { | 2090 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { |
2061 uint16* port_field = GetPortFieldFromAddrinfo(info); | 2091 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); |
2062 if (!port_field) | 2092 if (!port_field) |
2063 return -1; | 2093 return -1; |
2064 return ntohs(*port_field); | 2094 return ntohs(*port_field); |
2065 } | 2095 } |
2066 | 2096 |
2067 } // namespace net | 2097 } // namespace net |
OLD | NEW |