xref: /aosp_15_r20/external/cronet/net/base/address_map_linux.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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_BASE_ADDRESS_MAP_LINUX_H_
6 #define NET_BASE_ADDRESS_MAP_LINUX_H_
7 
8 #include <map>
9 #include <optional>
10 #include <unordered_set>
11 
12 #include "base/containers/flat_map.h"
13 #include "base/functional/callback_forward.h"
14 #include "net/base/ip_address.h"
15 #include "net/base/net_export.h"
16 
17 struct ifaddrmsg;
18 
19 namespace net {
20 
21 class AddressMapCacheLinux;
22 namespace internal {
23 class AddressTrackerLinux;
24 }
25 
26 // Various components of //net need to access a real-time-updated AddressMap
27 // (see comments below). For example, AddressSorterPosix (used in DNS
28 // resolution) and GetNetworkList() (used in many places).
29 // The methods defined in this interface should be safe to call from any thread.
30 class NET_EXPORT AddressMapOwnerLinux {
31  public:
32   // A map from net::IPAddress to netlink's ifaddrmsg, which includes
33   // information about the network interface that the IP address is associated
34   // with (e.g. interface index).
35   using AddressMap = std::map<IPAddress, struct ifaddrmsg>;
36 
37   // Represents a diff between one AddressMap and a new one. IPAddresses that
38   // map to std::nullopt have been deleted from the map, and IPAddresses that
39   // map to non-nullopt have been added or updated.
40   using AddressMapDiff =
41       base::flat_map<IPAddress, std::optional<struct ifaddrmsg>>;
42   // Represents a diff between one set of online links and new one. Interface
43   // indices that map to true are newly online and indices that map to false are
44   // newly offline.
45   using OnlineLinksDiff = base::flat_map<int, bool>;
46   // A callback for diffs, to be used by AddressTrackerLinux.
47   using DiffCallback =
48       base::RepeatingCallback<void(const AddressMapDiff& addr_diff,
49                                    const OnlineLinksDiff&)>;
50 
51   AddressMapOwnerLinux() = default;
52 
53   AddressMapOwnerLinux(const AddressMapOwnerLinux&) = delete;
54   AddressMapOwnerLinux& operator=(const AddressMapOwnerLinux&) = delete;
55 
56   virtual ~AddressMapOwnerLinux() = default;
57 
58   // These functions can be called on any thread. Implementations should use
59   // locking if necessary.
60 
61   // Returns the current AddressMap.
62   virtual AddressMap GetAddressMap() const = 0;
63   // Returns set of interface indices for online interfaces.
64   virtual std::unordered_set<int> GetOnlineLinks() const = 0;
65 
66   // These are the concrete implementations of AddressMapOwnerLinux and these
67   // functions serve as an ad-hoc dynamic cast to the concrete implementation,
68   // so this base class is not polluted with methods that end up unimplemented
69   // in one subclass.
70   virtual internal::AddressTrackerLinux* GetAddressTrackerLinux();
71   virtual AddressMapCacheLinux* GetAddressMapCacheLinux();
72 };
73 
74 }  // namespace net
75 
76 #endif  // NET_BASE_ADDRESS_MAP_LINUX_H_
77