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. 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/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" 18 19 namespace net { 20 21 class ClientSocketFactory; 22 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; 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; 52 unsigned label; 53 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; 59 }; 60 61 typedef std::map<IPAddressNumber, SourceAddressInfo> SourceAddressMap; 62 63 explicit AddressSorterPosix(ClientSocketFactory* socket_factory); 64 virtual ~AddressSorterPosix(); 65 66 // AddressSorter: 67 virtual void Sort(const AddressList& list, 68 const CallbackType& callback) const OVERRIDE; 69 70 private: 71 friend class AddressSorterPosixTest; 72 73 // NetworkChangeNotifier::IPAddressObserver: 74 virtual void OnIPAddressChanged() OVERRIDE; 75 76 // Fills |info| with values for |address| from policy tables. 77 void FillPolicy(const IPAddressNumber& address, 78 SourceAddressInfo* info) const; 79 80 // Mutable to allow using default values for source addresses which were not 81 // found in most recent OnIPAddressChanged. 82 mutable SourceAddressMap source_map_; 83 84 ClientSocketFactory* socket_factory_; 85 PolicyTable precedence_table_; 86 PolicyTable label_table_; 87 PolicyTable ipv4_scope_table_; 88 89 DISALLOW_COPY_AND_ASSIGN(AddressSorterPosix); 90 }; 91 92 } // namespace net 93 94 #endif // NET_DNS_ADDRESS_SORTER_POSIX_H_ 95