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_
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"
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
{
29 // Generic policy entry.
31 // IPv4 addresses must be mapped to IPv6.
32 unsigned char prefix
[kIPv6AddressSize
];
33 unsigned prefix_length
;
37 typedef std::vector
<PolicyEntry
> PolicyTable
;
48 struct SourceAddressInfo
{
49 // Values read from policy tables.
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
60 typedef std::map
<IPAddressNumber
, SourceAddressInfo
> SourceAddressMap
;
62 explicit AddressSorterPosix(ClientSocketFactory
* socket_factory
);
63 ~AddressSorterPosix() override
;
66 void Sort(const AddressList
& list
,
67 const CallbackType
& callback
) const override
;
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
);
93 #endif // NET_DNS_ADDRESS_SORTER_POSIX_H_