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