Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / dns / dns_hosts.h
blob66161059f1222f72323acdaf0f1d19f3b485b5a3
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/net_export.h"
18 #include "net/base/net_util.h" // can't forward-declare IPAddressNumber
20 namespace net {
21 typedef std::pair<std::string, AddressFamily> DnsHostsKey;
24 namespace BASE_HASH_NAMESPACE {
25 #if defined(COMPILER_GCC)
27 template<>
28 struct hash<net::DnsHostsKey> {
29 std::size_t operator()(const net::DnsHostsKey& key) const {
30 hash<base::StringPiece> string_piece_hash;
31 return string_piece_hash(key.first) + key.second;
35 #elif defined(COMPILER_MSVC)
37 inline size_t hash_value(const net::DnsHostsKey& key) {
38 return hash_value(key.first) + key.second;
41 #endif // COMPILER
43 } // namespace BASE_HASH_NAMESPACE
45 namespace net {
47 // There are OS-specific variations in how commas in the hosts file behave.
48 enum ParseHostsCommaMode {
49 // Comma is treated as part of a hostname:
50 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1".
51 PARSE_HOSTS_COMMA_IS_TOKEN,
53 // Comma is treated as a hostname separator:
54 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1".
55 PARSE_HOSTS_COMMA_IS_WHITESPACE,
58 // Parsed results of a Hosts file.
60 // Although Hosts files map IP address to a list of domain names, for name
61 // resolution the desired mapping direction is: domain name to IP address.
62 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
63 // With a Hosts file of:
64 // 300.300.300.300 localhost # bad ip
65 // 127.0.0.1 localhost
66 // 10.0.0.1 localhost
67 // The expected resolution of localhost is 127.0.0.1.
68 #if !defined(OS_ANDROID)
69 typedef base::hash_map<DnsHostsKey, IPAddressNumber> DnsHosts;
70 #else
71 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on
72 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.)
73 typedef std::map<DnsHostsKey, IPAddressNumber> DnsHosts;
74 #endif
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 // Overrides the OS-specific default handling of commas, so unittests can test
79 // both modes.
80 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting(
81 const std::string& contents,
82 DnsHosts* dns_hosts,
83 ParseHostsCommaMode comma_mode);
85 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
86 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
87 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents,
88 DnsHosts* dns_hosts);
90 // As above but reads the file pointed to by |path|.
91 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path,
92 DnsHosts* dns_hosts);
96 } // namespace net
98 #endif // NET_DNS_DNS_HOSTS_H_