| 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 #ifndef NET_DNS_DNS_HOSTS_H_ | 5 #ifndef NET_DNS_DNS_HOSTS_H_ |
| 6 #define NET_DNS_DNS_HOSTS_H_ | 6 #define NET_DNS_DNS_HOSTS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <unordered_map> | |
| 13 #include <utility> | 12 #include <utility> |
| 14 #include <vector> | 13 #include <vector> |
| 15 | 14 |
| 15 #include "base/containers/hash_tables.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/strings/string_piece.h" | |
| 18 #include "net/base/address_family.h" | 17 #include "net/base/address_family.h" |
| 19 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
| 20 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 22 typedef std::pair<std::string, AddressFamily> DnsHostsKey; |
| 23 }; |
| 23 | 24 |
| 24 using DnsHostsKey = std::pair<std::string, AddressFamily>; | 25 namespace BASE_HASH_NAMESPACE { |
| 25 | 26 |
| 26 struct DnsHostsKeyHash { | 27 template<> |
| 27 std::size_t operator()(const DnsHostsKey& key) const { | 28 struct hash<net::DnsHostsKey> { |
| 29 std::size_t operator()(const net::DnsHostsKey& key) const { |
| 28 return base::StringPieceHash()(key.first) + key.second; | 30 return base::StringPieceHash()(key.first) + key.second; |
| 29 } | 31 } |
| 30 }; | 32 }; |
| 31 | 33 |
| 34 } // namespace BASE_HASH_NAMESPACE |
| 35 |
| 36 namespace net { |
| 37 |
| 32 // There are OS-specific variations in how commas in the hosts file behave. | 38 // There are OS-specific variations in how commas in the hosts file behave. |
| 33 enum ParseHostsCommaMode { | 39 enum ParseHostsCommaMode { |
| 34 // Comma is treated as part of a hostname: | 40 // Comma is treated as part of a hostname: |
| 35 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1". | 41 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1". |
| 36 PARSE_HOSTS_COMMA_IS_TOKEN, | 42 PARSE_HOSTS_COMMA_IS_TOKEN, |
| 37 | 43 |
| 38 // Comma is treated as a hostname separator: | 44 // Comma is treated as a hostname separator: |
| 39 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1". | 45 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1". |
| 40 PARSE_HOSTS_COMMA_IS_WHITESPACE, | 46 PARSE_HOSTS_COMMA_IS_WHITESPACE, |
| 41 }; | 47 }; |
| 42 | 48 |
| 43 // Parsed results of a Hosts file. | 49 // Parsed results of a Hosts file. |
| 44 // | 50 // |
| 45 // Although Hosts files map IP address to a list of domain names, for name | 51 // Although Hosts files map IP address to a list of domain names, for name |
| 46 // resolution the desired mapping direction is: domain name to IP address. | 52 // resolution the desired mapping direction is: domain name to IP address. |
| 47 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. | 53 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. |
| 48 // With a Hosts file of: | 54 // With a Hosts file of: |
| 49 // 300.300.300.300 localhost # bad ip | 55 // 300.300.300.300 localhost # bad ip |
| 50 // 127.0.0.1 localhost | 56 // 127.0.0.1 localhost |
| 51 // 10.0.0.1 localhost | 57 // 10.0.0.1 localhost |
| 52 // The expected resolution of localhost is 127.0.0.1. | 58 // The expected resolution of localhost is 127.0.0.1. |
| 53 using DnsHosts = std::unordered_map<DnsHostsKey, IPAddress, DnsHostsKeyHash>; | 59 #if !defined(OS_ANDROID) |
| 60 typedef base::hash_map<DnsHostsKey, IPAddress> DnsHosts; |
| 61 #else |
| 62 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on |
| 63 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.) |
| 64 typedef std::map<DnsHostsKey, IPAddress> DnsHosts; |
| 65 #endif |
| 54 | 66 |
| 55 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results | 67 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results |
| 56 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). | 68 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). |
| 57 // Overrides the OS-specific default handling of commas, so unittests can test | 69 // Overrides the OS-specific default handling of commas, so unittests can test |
| 58 // both modes. | 70 // both modes. |
| 59 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting( | 71 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting( |
| 60 const std::string& contents, | 72 const std::string& contents, |
| 61 DnsHosts* dns_hosts, | 73 DnsHosts* dns_hosts, |
| 62 ParseHostsCommaMode comma_mode); | 74 ParseHostsCommaMode comma_mode); |
| 63 | 75 |
| 64 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results | 76 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results |
| 65 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). | 77 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). |
| 66 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, | 78 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, |
| 67 DnsHosts* dns_hosts); | 79 DnsHosts* dns_hosts); |
| 68 | 80 |
| 69 // As above but reads the file pointed to by |path|. | 81 // As above but reads the file pointed to by |path|. |
| 70 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path, | 82 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path, |
| 71 DnsHosts* dns_hosts); | 83 DnsHosts* dns_hosts); |
| 72 | 84 |
| 73 | 85 |
| 74 | 86 |
| 75 } // namespace net | 87 } // namespace net |
| 76 | 88 |
| 77 #endif // NET_DNS_DNS_HOSTS_H_ | 89 #endif // NET_DNS_DNS_HOSTS_H_ |
| 78 | 90 |
| OLD | NEW |