summaryrefslogtreecommitdiff
path: root/chromium/net/dns/address_sorter_posix.h
blob: e370f226003bb5a4f7f5a5f5447770694f03af98 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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_ADDRESS_SORTER_POSIX_H_
#define NET_DNS_ADDRESS_SORTER_POSIX_H_

#include <map>
#include <vector>

#include "base/macros.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/address_list.h"
#include "net/base/ip_address.h"
#include "net/base/net_export.h"
#include "net/base/network_change_notifier.h"
#include "net/dns/address_sorter.h"

namespace net {

class ClientSocketFactory;

// This implementation uses explicit policy to perform the sorting. It is not
// thread-safe and always completes synchronously.
class NET_EXPORT_PRIVATE AddressSorterPosix
    : public AddressSorter,
      public base::NonThreadSafe,
      public NetworkChangeNotifier::IPAddressObserver {
 public:
  // Generic policy entry.
  struct PolicyEntry {
    // IPv4 addresses must be mapped to IPv6.
    unsigned char prefix[IPAddress::kIPv6AddressSize];
    unsigned prefix_length;
    unsigned value;
  };

  typedef std::vector<PolicyEntry> PolicyTable;

  enum AddressScope {
    SCOPE_UNDEFINED = 0,
    SCOPE_NODELOCAL = 1,
    SCOPE_LINKLOCAL = 2,
    SCOPE_SITELOCAL = 5,
    SCOPE_ORGLOCAL = 8,
    SCOPE_GLOBAL = 14,
  };

  struct SourceAddressInfo {
    // Values read from policy tables.
    AddressScope scope;
    unsigned label;

    // Values from the OS, matter only if more than one source address is used.
    unsigned prefix_length;
    bool deprecated;  // vs. preferred RFC4862
    bool home;        // vs. care-of RFC6275
    bool native;
  };

  typedef std::map<IPAddress, SourceAddressInfo> SourceAddressMap;

  explicit AddressSorterPosix(ClientSocketFactory* socket_factory);
  ~AddressSorterPosix() override;

  // AddressSorter:
  void Sort(const AddressList& list,
            const CallbackType& callback) const override;

 private:
  friend class AddressSorterPosixTest;

  // NetworkChangeNotifier::IPAddressObserver:
  void OnIPAddressChanged() override;

  // Fills |info| with values for |address| from policy tables.
  void FillPolicy(const IPAddress& address, SourceAddressInfo* info) const;

  // Mutable to allow using default values for source addresses which were not
  // found in most recent OnIPAddressChanged.
  mutable SourceAddressMap source_map_;

  ClientSocketFactory* socket_factory_;
  PolicyTable precedence_table_;
  PolicyTable label_table_;
  PolicyTable ipv4_scope_table_;

  DISALLOW_COPY_AND_ASSIGN(AddressSorterPosix);
};

}  // namespace net

#endif  // NET_DNS_ADDRESS_SORTER_POSIX_H_