Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / net / dns / address_sorter_posix.h
blobd23f0497b9fb8548633080546e033562095fa1f8
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.
5 #ifndef NET_DNS_ADDRESS_SORTER_POSIX_H_
6 #define NET_DNS_ADDRESS_SORTER_POSIX_H_
8 #include <map>
9 #include <vector>
11 #include "base/threading/non_thread_safe.h"
12 #include "net/base/address_list.h"
13 #include "net/base/ip_address_number.h"
14 #include "net/base/net_export.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/dns/address_sorter.h"
18 namespace net {
20 class ClientSocketFactory;
22 // This implementation uses explicit policy to perform the sorting. It is not
23 // thread-safe and always completes synchronously.
24 class NET_EXPORT_PRIVATE AddressSorterPosix
25 : public AddressSorter,
26 public base::NonThreadSafe,
27 public NetworkChangeNotifier::IPAddressObserver {
28 public:
29 // Generic policy entry.
30 struct PolicyEntry {
31 // IPv4 addresses must be mapped to IPv6.
32 unsigned char prefix[kIPv6AddressSize];
33 unsigned prefix_length;
34 unsigned value;
37 typedef std::vector<PolicyEntry> PolicyTable;
39 enum AddressScope {
40 SCOPE_UNDEFINED = 0,
41 SCOPE_NODELOCAL = 1,
42 SCOPE_LINKLOCAL = 2,
43 SCOPE_SITELOCAL = 5,
44 SCOPE_ORGLOCAL = 8,
45 SCOPE_GLOBAL = 14,
48 struct SourceAddressInfo {
49 // Values read from policy tables.
50 AddressScope scope;
51 unsigned label;
53 // Values from the OS, matter only if more than one source address is used.
54 unsigned prefix_length;
55 bool deprecated; // vs. preferred RFC4862
56 bool home; // vs. care-of RFC6275
57 bool native;
60 typedef std::map<IPAddressNumber, SourceAddressInfo> SourceAddressMap;
62 explicit AddressSorterPosix(ClientSocketFactory* socket_factory);
63 ~AddressSorterPosix() override;
65 // AddressSorter:
66 void Sort(const AddressList& list,
67 const CallbackType& callback) const override;
69 private:
70 friend class AddressSorterPosixTest;
72 // NetworkChangeNotifier::IPAddressObserver:
73 void OnIPAddressChanged() override;
75 // Fills |info| with values for |address| from policy tables.
76 void FillPolicy(const IPAddressNumber& address,
77 SourceAddressInfo* info) const;
79 // Mutable to allow using default values for source addresses which were not
80 // found in most recent OnIPAddressChanged.
81 mutable SourceAddressMap source_map_;
83 ClientSocketFactory* socket_factory_;
84 PolicyTable precedence_table_;
85 PolicyTable label_table_;
86 PolicyTable ipv4_scope_table_;
88 DISALLOW_COPY_AND_ASSIGN(AddressSorterPosix);
91 } // namespace net
93 #endif // NET_DNS_ADDRESS_SORTER_POSIX_H_