File: dns_hosts.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (96 lines) | stat: -rw-r--r-- 3,191 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_DNS_HOSTS_H_
#define NET_DNS_DNS_HOSTS_H_

#include <stddef.h>

#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>

#include "base/files/file_path.h"
#include "net/base/address_family.h"
#include "net/base/ip_address.h"
#include "net/base/net_export.h"

namespace net {

using DnsHostsKey = std::pair<std::string, AddressFamily>;

struct DnsHostsKeyHash {
  std::size_t operator()(const DnsHostsKey& key) const {
    return std::hash<std::string_view>()(key.first) + key.second;
  }
};

// There are OS-specific variations in how commas in the hosts file behave.
enum ParseHostsCommaMode {
  // Comma is treated as part of a hostname:
  // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1".
  PARSE_HOSTS_COMMA_IS_TOKEN,

  // Comma is treated as a hostname separator:
  // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1".
  PARSE_HOSTS_COMMA_IS_WHITESPACE,
};

// Parsed results of a Hosts file.
//
// Although Hosts files map IP address to a list of domain names, for name
// resolution the desired mapping direction is: domain name to IP address.
// When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
// With a Hosts file of:
// 300.300.300.300 localhost # bad ip
// 127.0.0.1 localhost
// 10.0.0.1 localhost
// The expected resolution of localhost is 127.0.0.1.
using DnsHosts = std::unordered_map<DnsHostsKey, IPAddress, DnsHostsKeyHash>;

// Parses |contents| (as read from /etc/hosts or equivalent) and stores results
// in |dns_hosts|. Invalid lines are ignored (as in most implementations).
// Overrides the OS-specific default handling of commas, so unittests can test
// both modes.
void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting(
    const std::string& contents,
    DnsHosts* dns_hosts,
    ParseHostsCommaMode comma_mode);

// Parses |contents| (as read from /etc/hosts or equivalent) and stores results
// in |dns_hosts|. Invalid lines are ignored (as in most implementations).
void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents,
                                   DnsHosts* dns_hosts);

// Test-injectable HOSTS parser.
class NET_EXPORT_PRIVATE DnsHostsParser {
 public:
  virtual ~DnsHostsParser();

  // Parses HOSTS and stores results in `dns_hosts`, with addresses in the order
  // in which they were read. Invalid lines are ignored (as in most
  // implementations).
  virtual bool ParseHosts(DnsHosts* hosts) const = 0;
};

// Implementation of `DnsHostsParser` that reads HOSTS from a given file.
class NET_EXPORT_PRIVATE DnsHostsFileParser : public DnsHostsParser {
 public:
  explicit DnsHostsFileParser(base::FilePath hosts_file_path);
  ~DnsHostsFileParser() override;

  DnsHostsFileParser(const DnsHostsFileParser&) = delete;
  DnsHostsFileParser& operator=(const DnsHostsFileParser&) = delete;

  // DnsHostsParser:
  bool ParseHosts(DnsHosts* dns_hosts) const override;

 private:
  const base::FilePath hosts_file_path_;
};

}  // namespace net

#endif  // NET_DNS_DNS_HOSTS_H_