xref: /aosp_15_r20/external/webrtc/rtc_base/network_monitor.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef RTC_BASE_NETWORK_MONITOR_H_
12 #define RTC_BASE_NETWORK_MONITOR_H_
13 
14 #include <functional>
15 #include <utility>
16 
17 #include "absl/strings/string_view.h"
18 #include "rtc_base/network_constants.h"
19 
20 namespace rtc {
21 
22 class IPAddress;
23 
24 enum class NetworkBindingResult {
25   SUCCESS = 0,   // No error
26   FAILURE = -1,  // Generic error
27   NOT_IMPLEMENTED = -2,
28   ADDRESS_NOT_FOUND = -3,
29   NETWORK_CHANGED = -4
30 };
31 
32 // NetworkPreference property set by operating system/firmware that has
33 // information about connection strength to e.g WIFI router or CELL base towers.
34 // GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
35 enum class NetworkPreference {
36   NEUTRAL = 0,
37   NOT_PREFERRED = -1,
38 };
39 
40 const char* NetworkPreferenceToString(NetworkPreference preference);
41 
42 // This interface is set onto a socket server,
43 // where only the ip address is known at the time of binding.
44 class NetworkBinderInterface {
45  public:
46   // Binds a socket to the network that is attached to `address` so that all
47   // packets on the socket `socket_fd` will be sent via that network.
48   // This is needed because some operating systems (like Android) require a
49   // special bind call to put packets on a non-default network interface.
50   virtual NetworkBindingResult BindSocketToNetwork(
51       int socket_fd,
52       const IPAddress& address) = 0;
~NetworkBinderInterface()53   virtual ~NetworkBinderInterface() {}
54 };
55 
56 /*
57  * Receives network-change events via `OnNetworksChanged` and signals the
58  * networks changed event.
59  *
60  * Threading consideration:
61  * It is expected that all upstream operations (from native to Java) are
62  * performed from the worker thread. This includes creating, starting and
63  * stopping the monitor. This avoids the potential race condition when creating
64  * the singleton Java NetworkMonitor class. Downstream operations can be from
65  * any thread, but this class will forward all the downstream operations onto
66  * the worker thread.
67  *
68  * Memory consideration:
69  * NetworkMonitor is owned by the caller (NetworkManager). The global network
70  * monitor factory is owned by the PeerConnectionFactory.
71  */
72 // Generic network monitor interface. It starts and stops monitoring network
73 // changes, and fires the SignalNetworksChanged event when networks change.
74 class NetworkMonitorInterface {
75  public:
76   struct InterfaceInfo {
77     // The type of adapter if known.
78     AdapterType adapter_type;
79 
80     // Is ADAPTER_TYPE_UNKNOWN unless adapter_type == ADAPTER_TYPE_VPN.
81     AdapterType underlying_type_for_vpn = ADAPTER_TYPE_UNKNOWN;
82 
83     // The OS/firmware specific preference of this interface.
84     NetworkPreference network_preference = NetworkPreference::NEUTRAL;
85 
86     // Is this interface available to use? WebRTC shouldn't attempt to use it if
87     // this returns false.
88     //
89     // It's possible for this status to change, in which case
90     // SignalNetworksChanged will be fired.
91     //
92     // The specific use case this was added for was a phone with two SIM
93     // cards, where attempting to use all interfaces returned from getifaddrs
94     // caused the connection to be dropped.
95     bool available = true;
96   };
97 
98   NetworkMonitorInterface();
99   virtual ~NetworkMonitorInterface();
100 
101   virtual void Start() = 0;
102   virtual void Stop() = 0;
103 
104   // Get information about an interface.
105   // If the interface is not known, the return struct will have set
106   // `adapter_type` to ADAPTER_TYPE_UNKNOWN and `available` to false.
107   virtual InterfaceInfo GetInterfaceInfo(absl::string_view interface_name) = 0;
108 
109   // Does `this` NetworkMonitorInterface implement BindSocketToNetwork?
110   // Only Android returns true.
SupportsBindSocketToNetwork()111   virtual bool SupportsBindSocketToNetwork() const { return false; }
112 
113   // Bind a socket to an interface specified by ip address and/or interface
114   // name. Only implemented on Android.
BindSocketToNetwork(int socket_fd,const IPAddress & address,absl::string_view interface_name)115   virtual NetworkBindingResult BindSocketToNetwork(
116       int socket_fd,
117       const IPAddress& address,
118       absl::string_view interface_name) {
119     return NetworkBindingResult::NOT_IMPLEMENTED;
120   }
121 
SetNetworksChangedCallback(std::function<void ()> callback)122   void SetNetworksChangedCallback(std::function<void()> callback) {
123     networks_changed_callback_ = std::move(callback);
124   }
125 
126  protected:
InvokeNetworksChangedCallback()127   void InvokeNetworksChangedCallback() {
128     if (networks_changed_callback_) {
129       networks_changed_callback_();
130     }
131   }
132 
133  private:
134   std::function<void()> networks_changed_callback_;
135 };
136 
137 }  // namespace rtc
138 
139 #endif  // RTC_BASE_NETWORK_MONITOR_H_
140