blob: 605854f6ea11bef50db05ff636517d431f06fdcb [file] [log] [blame]
honghaiz023f3ef2015-10-19 16:39:321/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_NETWORK_MONITOR_H_
12#define RTC_BASE_NETWORK_MONITOR_H_
honghaiz023f3ef2015-10-19 16:39:3213
Mirko Bonadei37077932021-07-27 15:00:5814#include <functional>
15#include <utility>
16
Ali Tofigh7fa90572022-03-17 14:47:4917#include "absl/strings/string_view.h"
Patrik Höglunde2d6a062017-10-05 12:53:3318#include "rtc_base/network_constants.h"
honghaiz023f3ef2015-10-19 16:39:3219
Henrik Kjellanderec78f1c2017-06-29 05:52:5020namespace rtc {
21
22class IPAddress;
23
24enum 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
Jonas Orelandf7721fb2020-08-07 09:08:3432// 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
35enum class NetworkPreference {
36 NEUTRAL = 0,
37 NOT_PREFERRED = -1,
38};
39
Taylor Brandstetter32eb03a2020-09-11 17:15:3040const char* NetworkPreferenceToString(NetworkPreference preference);
41
Jonas Oreland6ca955a2021-03-15 08:27:4342// This interface is set onto a socket server,
43// where only the ip address is known at the time of binding.
Henrik Kjellanderec78f1c2017-06-29 05:52:5044class NetworkBinderInterface {
45 public:
Artem Titov96e3b992021-07-26 14:03:1446 // 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.
Henrik Kjellanderec78f1c2017-06-29 05:52:5048 // 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;
53 virtual ~NetworkBinderInterface() {}
54};
55
56/*
Artem Titov96e3b992021-07-26 14:03:1457 * Receives network-change events via `OnNetworksChanged` and signals the
Henrik Kjellanderec78f1c2017-06-29 05:52:5058 * 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
Taylor Brandstetter07fc24d2020-08-12 01:58:1070 * monitor factory is owned by the PeerConnectionFactory.
Henrik Kjellanderec78f1c2017-06-29 05:52:5071 */
72// Generic network monitor interface. It starts and stops monitoring network
73// changes, and fires the SignalNetworksChanged event when networks change.
74class NetworkMonitorInterface {
75 public:
Jonas Oreland61dbcd12022-05-31 09:34:2076 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
Henrik Kjellanderec78f1c2017-06-29 05:52:5098 NetworkMonitorInterface();
99 virtual ~NetworkMonitorInterface();
100
Henrik Kjellanderec78f1c2017-06-29 05:52:50101 virtual void Start() = 0;
102 virtual void Stop() = 0;
103
Jonas Oreland61dbcd12022-05-31 09:34:20104 // 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;
Taylor Brandstetterea7fbfb2020-08-19 23:41:54108
Artem Titov96e3b992021-07-26 14:03:14109 // Does `this` NetworkMonitorInterface implement BindSocketToNetwork?
Jonas Oreland6ca955a2021-03-15 08:27:43110 // Only Android returns true.
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.
115 virtual NetworkBindingResult BindSocketToNetwork(
116 int socket_fd,
117 const IPAddress& address,
Ali Tofigh7fa90572022-03-17 14:47:49118 absl::string_view interface_name) {
Jonas Oreland6ca955a2021-03-15 08:27:43119 return NetworkBindingResult::NOT_IMPLEMENTED;
120 }
121
Mirko Bonadei37077932021-07-27 15:00:58122 void SetNetworksChangedCallback(std::function<void()> callback) {
123 networks_changed_callback_ = std::move(callback);
124 }
125
126 protected:
127 void InvokeNetworksChangedCallback() {
128 if (networks_changed_callback_) {
129 networks_changed_callback_();
130 }
131 }
132
133 private:
134 std::function<void()> networks_changed_callback_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50135};
136
Henrik Kjellanderec78f1c2017-06-29 05:52:50137} // namespace rtc
honghaiz023f3ef2015-10-19 16:39:32138
Steve Anton10542f22019-01-11 17:11:00139#endif // RTC_BASE_NETWORK_MONITOR_H_