[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_DNS_DNS_HOSTS_H_ |
| 6 | #define NET_DNS_DNS_HOSTS_H_ |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 14 | #include "net/base/address_family.h" |
| 15 | #include "net/base/net_export.h" |
| 16 | #include "net/base/net_util.h" // can't forward-declare IPAddressNumber |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | // Parsed results of a Hosts file. |
| 21 | // |
| 22 | // Although Hosts files map IP address to a list of domain names, for name |
| 23 | // resolution the desired mapping direction is: domain name to IP address. |
| 24 | // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. |
| 25 | // With a Hosts file of: |
| 26 | // 300.300.300.300 localhost # bad ip |
| 27 | // 127.0.0.1 localhost |
| 28 | // 10.0.0.1 localhost |
| 29 | // The expected resolution of localhost is 127.0.0.1. |
| 30 | typedef std::pair<std::string, AddressFamily> DnsHostsKey; |
| 31 | typedef std::map<DnsHostsKey, IPAddressNumber> DnsHosts; |
| 32 | |
| 33 | // Parses |contents| (as read from /etc/hosts or equivalent) and stores results |
| 34 | // in |dns_hosts|. Invalid lines are ignored (as in most implementations). |
| 35 | void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, |
| 36 | DnsHosts* dns_hosts); |
| 37 | |
[email protected] | d6f9e9cb | 2012-06-14 04:28:37 | [diff] [blame] | 38 | // As above but reads the file pointed to by |path|. |
| 39 | bool NET_EXPORT_PRIVATE ParseHostsFile(const FilePath& path, |
| 40 | DnsHosts* dns_hosts); |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 41 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 42 | |
| 43 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 44 | } // namespace net |
| 45 | |
| 46 | #endif // NET_DNS_DNS_HOSTS_H_ |
| 47 | |