summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_hosts.h
blob: 91c35baae72a01f4a7674247c2c1948c77993d4f (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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_
#pragma once

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "base/callback.h"
#include "base/file_path.h"
#include "net/base/address_family.h"
#include "net/base/net_export.h"
#include "net/base/net_util.h"  // can't forward-declare IPAddressNumber
#include "net/dns/serial_worker.h"

namespace net {

// 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.
typedef std::pair<std::string, AddressFamily> DnsHostsKey;
typedef std::map<DnsHostsKey, IPAddressNumber> DnsHosts;

// 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);

// A SerialWorker that reads a HOSTS file and runs Callback.
// Call WorkNow() to indicate file needs to be re-read.
// Call Cancel() to disable the callback.
class NET_EXPORT_PRIVATE DnsHostsReader
    : NON_EXPORTED_BASE(public SerialWorker) {
 public:
  typedef base::Callback<void(const DnsHosts& hosts)> CallbackType;

  DnsHostsReader(const FilePath& path, const CallbackType& callback);

 private:
  virtual ~DnsHostsReader();

  virtual void DoWork() OVERRIDE;
  virtual void OnWorkFinished() OVERRIDE;

  FilePath path_;
  CallbackType callback_;
  // Written in DoWork, read in OnWorkFinished, no locking necessary.
  DnsHosts dns_hosts_;
  bool success_;

  DISALLOW_COPY_AND_ASSIGN(DnsHostsReader);
};


}  // namespace net

#endif  // NET_DNS_DNS_HOSTS_H_