1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_ADDRESS_SORTER_POSIX_H_ 6 #define NET_DNS_ADDRESS_SORTER_POSIX_H_ 7 8 #include <map> 9 #include <vector> 10 11 #include "base/containers/unique_ptr_adapters.h" 12 #include "base/memory/raw_ptr.h" 13 #include "base/threading/thread_checker.h" 14 #include "net/base/ip_address.h" 15 #include "net/base/net_export.h" 16 #include "net/base/network_change_notifier.h" 17 #include "net/dns/address_sorter.h" 18 #include "net/socket/datagram_client_socket.h" 19 20 namespace net { 21 22 class ClientSocketFactory; 23 24 // This implementation uses explicit policy to perform the sorting. It is not 25 // thread-safe and always completes synchronously. 26 class NET_EXPORT_PRIVATE AddressSorterPosix 27 : public AddressSorter, 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[IPAddress::kIPv6AddressSize]; 34 unsigned prefix_length; 35 unsigned value; 36 }; 37 38 typedef std::vector<PolicyEntry> PolicyTable; 39 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, 47 }; 48 49 struct SourceAddressInfo { 50 // Values read from policy tables. 51 AddressScope scope = SCOPE_UNDEFINED; 52 unsigned label = 0; 53 54 // Values from the OS, matter only if more than one source address is used. 55 size_t prefix_length = 0; 56 bool deprecated = false; // vs. preferred RFC4862 57 bool home = false; // vs. care-of RFC6275 58 bool native = false; 59 }; 60 61 typedef std::map<IPAddress, SourceAddressInfo> SourceAddressMap; 62 63 explicit AddressSorterPosix(ClientSocketFactory* socket_factory); 64 65 AddressSorterPosix(const AddressSorterPosix&) = delete; 66 AddressSorterPosix& operator=(const AddressSorterPosix&) = delete; 67 68 ~AddressSorterPosix() override; 69 70 // AddressSorter: 71 void Sort(const std::vector<IPEndPoint>& endpoints, 72 CallbackType callback) const override; 73 74 private: 75 friend class AddressSorterPosixTest; 76 class SortContext; 77 78 // NetworkChangeNotifier::IPAddressObserver: 79 void OnIPAddressChanged() override; 80 // Fills |info| with values for |address| from policy tables. 81 void FillPolicy(const IPAddress& address, SourceAddressInfo* info) const; 82 83 void FinishedSort(SortContext* sort_context) const; 84 85 // Mutable to allow using default values for source addresses which were not 86 // found in most recent OnIPAddressChanged. 87 mutable SourceAddressMap source_map_; 88 89 raw_ptr<ClientSocketFactory> socket_factory_; 90 PolicyTable precedence_table_; 91 PolicyTable label_table_; 92 PolicyTable ipv4_scope_table_; 93 94 // SortContext stores data for an outstanding Sort() that is completing 95 // asynchronously. Mutable to allow pushing a new SortContext when Sort is 96 // called. Since Sort can be called multiple times, a container is necessary 97 // to track different SortContexts. 98 mutable std::set<std::unique_ptr<SortContext>, base::UniquePtrComparator> 99 sort_contexts_; 100 101 THREAD_CHECKER(thread_checker_); 102 }; 103 104 } // namespace net 105 106 #endif // NET_DNS_ADDRESS_SORTER_POSIX_H_ 107