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_
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
21 typedef std::pair
<std::string
, AddressFamily
> DnsHostsKey
;
24 namespace BASE_HASH_NAMESPACE
{
25 #if defined(COMPILER_GCC)
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
;
43 } // namespace BASE_HASH_NAMESPACE
47 // Parsed results of a Hosts file.
49 // Although Hosts files map IP address to a list of domain names, for name
50 // resolution the desired mapping direction is: domain name to IP address.
51 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
52 // With a Hosts file of:
53 // 300.300.300.300 localhost # bad ip
54 // 127.0.0.1 localhost
56 // The expected resolution of localhost is 127.0.0.1.
57 #if !defined(OS_ANDROID)
58 typedef base::hash_map
<DnsHostsKey
, IPAddressNumber
> DnsHosts
;
60 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on
61 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.)
62 typedef std::map
<DnsHostsKey
, IPAddressNumber
> DnsHosts
;
65 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
66 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
67 void NET_EXPORT_PRIVATE
ParseHosts(const std::string
& contents
,
70 // As above but reads the file pointed to by |path|.
71 bool NET_EXPORT_PRIVATE
ParseHostsFile(const base::FilePath
& path
,
78 #endif // NET_DNS_DNS_HOSTS_H_