Revert of Hook up the Push API with GCM's new ability to own encryption keys. (patchs...
[chromium-blink-merge.git] / net / dns / address_sorter_posix.h
blobcae787f12bbf26b803cb854a67919f4c7d88dfb7
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/net_errors.h"
14 #include "net/base/net_export.h"
15 #include "net/base/net_util.h"
16 #include "net/base/network_change_notifier.h"
17 #include "net/dns/address_sorter.h"
19 namespace net {
21 class ClientSocketFactory;
23 // This implementation uses explicit policy to perform the sorting. It is not
24 // thread-safe and always completes synchronously.
25 class NET_EXPORT_PRIVATE AddressSorterPosix
26 : public AddressSorter,
27 public base::NonThreadSafe,
28 public NetworkChangeNotifier::IPAddressObserver {
29 public:
30 // Generic policy entry.
31 struct PolicyEntry {
32 // IPv4 addresses must be mapped to IPv6.
33 unsigned char prefix[kIPv6AddressSize];
34 unsigned prefix_length;
35 unsigned value;
38 typedef std::vector<PolicyEntry> PolicyTable;
40 enum AddressScope {
41 SCOPE_UNDEFINED = 0,
42 SCOPE_NODELOCAL = 1,
43 SCOPE_LINKLOCAL = 2,
44 SCOPE_SITELOCAL = 5,
45 SCOPE_ORGLOCAL = 8,
46 SCOPE_GLOBAL = 14,
49 struct SourceAddressInfo {
50 // Values read from policy tables.
51 AddressScope scope;
52 unsigned label;
54 // Values from the OS, matter only if more than one source address is used.
55 unsigned prefix_length;
56 bool deprecated; // vs. preferred RFC4862
57 bool home; // vs. care-of RFC6275
58 bool native;
61 typedef std::map<IPAddressNumber, SourceAddressInfo> SourceAddressMap;
63 explicit AddressSorterPosix(ClientSocketFactory* socket_factory);
64 ~AddressSorterPosix() override;
66 // AddressSorter:
67 void Sort(const AddressList& list,
68 const CallbackType& callback) const override;
70 private:
71 friend class AddressSorterPosixTest;
73 // NetworkChangeNotifier::IPAddressObserver:
74 void OnIPAddressChanged() override;
76 // Fills |info| with values for |address| from policy tables.
77 void FillPolicy(const IPAddressNumber& address,
78 SourceAddressInfo* info) const;
80 // Mutable to allow using default values for source addresses which were not
81 // found in most recent OnIPAddressChanged.
82 mutable SourceAddressMap source_map_;
84 ClientSocketFactory* socket_factory_;
85 PolicyTable precedence_table_;
86 PolicyTable label_table_;
87 PolicyTable ipv4_scope_table_;
89 DISALLOW_COPY_AND_ASSIGN(AddressSorterPosix);
92 } // namespace net
94 #endif // NET_DNS_ADDRESS_SORTER_POSIX_H_