dns_hosts.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.
  4. #ifndef NET_DNS_DNS_HOSTS_H_
  5. #define NET_DNS_DNS_HOSTS_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/files/file_path.h"
  13. #include "base/strings/string_piece.h"
  14. #include "net/base/address_family.h"
  15. #include "net/base/ip_address.h"
  16. #include "net/base/net_export.h"
  17. namespace net {
  18. using DnsHostsKey = std::pair<std::string, AddressFamily>;
  19. struct DnsHostsKeyHash {
  20. std::size_t operator()(const DnsHostsKey& key) const {
  21. return base::StringPieceHash()(key.first) + key.second;
  22. }
  23. };
  24. // There are OS-specific variations in how commas in the hosts file behave.
  25. enum ParseHostsCommaMode {
  26. // Comma is treated as part of a hostname:
  27. // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1".
  28. PARSE_HOSTS_COMMA_IS_TOKEN,
  29. // Comma is treated as a hostname separator:
  30. // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1".
  31. PARSE_HOSTS_COMMA_IS_WHITESPACE,
  32. };
  33. // Parsed results of a Hosts file.
  34. //
  35. // Although Hosts files map IP address to a list of domain names, for name
  36. // resolution the desired mapping direction is: domain name to IP address.
  37. // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
  38. // With a Hosts file of:
  39. // 300.300.300.300 localhost # bad ip
  40. // 127.0.0.1 localhost
  41. // 10.0.0.1 localhost
  42. // The expected resolution of localhost is 127.0.0.1.
  43. using DnsHosts = std::unordered_map<DnsHostsKey, IPAddress, DnsHostsKeyHash>;
  44. // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
  45. // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
  46. // Overrides the OS-specific default handling of commas, so unittests can test
  47. // both modes.
  48. void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting(
  49. const std::string& contents,
  50. DnsHosts* dns_hosts,
  51. ParseHostsCommaMode comma_mode);
  52. // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
  53. // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
  54. void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents,
  55. DnsHosts* dns_hosts);
  56. // Test-injectable HOSTS parser.
  57. class NET_EXPORT_PRIVATE DnsHostsParser {
  58. public:
  59. virtual ~DnsHostsParser();
  60. // Parses HOSTS and stores results in `dns_hosts`, with addresses in the order
  61. // in which they were read. Invalid lines are ignored (as in most
  62. // implementations).
  63. virtual bool ParseHosts(DnsHosts* hosts) const = 0;
  64. };
  65. // Implementation of `DnsHostsParser` that reads HOSTS from a given file.
  66. class NET_EXPORT_PRIVATE DnsHostsFileParser : public DnsHostsParser {
  67. public:
  68. explicit DnsHostsFileParser(base::FilePath hosts_file_path);
  69. ~DnsHostsFileParser() override;
  70. DnsHostsFileParser(const DnsHostsFileParser&) = delete;
  71. DnsHostsFileParser& operator=(const DnsHostsFileParser&) = delete;
  72. // DnsHostsParser:
  73. bool ParseHosts(DnsHosts* dns_hosts) const override;
  74. private:
  75. const base::FilePath hosts_file_path_;
  76. };
  77. } // namespace net
  78. #endif // NET_DNS_DNS_HOSTS_H_