Roll src/third_party/WebKit 50770f1:51f611e (svn 202060:202061)
[chromium-blink-merge.git] / net / dns / dns_hosts.h
blobc35857e1f5753495bf614ba68e08534bf462b155
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
3 // found in the LICENSE file.
5 #ifndef NET_DNS_DNS_HOSTS_H_
6 #define NET_DNS_DNS_HOSTS_H_
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/files/file_path.h"
16 #include "net/base/address_family.h"
17 #include "net/base/ip_address_number.h"
18 #include "net/base/net_export.h"
20 namespace net {
21 typedef std::pair<std::string, AddressFamily> DnsHostsKey;
24 namespace BASE_HASH_NAMESPACE {
26 template<>
27 struct hash<net::DnsHostsKey> {
28 std::size_t operator()(const net::DnsHostsKey& key) const {
29 hash<base::StringPiece> string_piece_hash;
30 return string_piece_hash(key.first) + key.second;
34 } // namespace BASE_HASH_NAMESPACE
36 namespace net {
38 // There are OS-specific variations in how commas in the hosts file behave.
39 enum ParseHostsCommaMode {
40 // Comma is treated as part of a hostname:
41 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1".
42 PARSE_HOSTS_COMMA_IS_TOKEN,
44 // Comma is treated as a hostname separator:
45 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1".
46 PARSE_HOSTS_COMMA_IS_WHITESPACE,
49 // Parsed results of a Hosts file.
51 // Although Hosts files map IP address to a list of domain names, for name
52 // resolution the desired mapping direction is: domain name to IP address.
53 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
54 // With a Hosts file of:
55 // 300.300.300.300 localhost # bad ip
56 // 127.0.0.1 localhost
57 // 10.0.0.1 localhost
58 // The expected resolution of localhost is 127.0.0.1.
59 #if !defined(OS_ANDROID)
60 typedef base::hash_map<DnsHostsKey, IPAddressNumber> 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, IPAddressNumber> DnsHosts;
65 #endif
67 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
68 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
69 // Overrides the OS-specific default handling of commas, so unittests can test
70 // both modes.
71 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting(
72 const std::string& contents,
73 DnsHosts* dns_hosts,
74 ParseHostsCommaMode comma_mode);
76 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
77 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
78 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents,
79 DnsHosts* dns_hosts);
81 // As above but reads the file pointed to by |path|.
82 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path,
83 DnsHosts* dns_hosts);
87 } // namespace net
89 #endif // NET_DNS_DNS_HOSTS_H_