blob: 1e150fe86182853b59d8aea4708494fd24f31f67 [file] [log] [blame]
honghaiz023f3ef2015-10-19 09:39:32 -07001/*
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 09:11:00 -080011#ifndef RTC_BASE_NETWORK_MONITOR_H_
12#define RTC_BASE_NETWORK_MONITOR_H_
honghaiz023f3ef2015-10-19 09:39:32 -070013
Mirko Bonadei37077932021-07-27 17:00:58 +020014#include <functional>
15#include <utility>
16
Patrik Höglunde2d6a062017-10-05 14:53:33 +020017#include "rtc_base/network_constants.h"
honghaiz023f3ef2015-10-19 09:39:32 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019namespace rtc {
20
21class IPAddress;
22
23enum class NetworkBindingResult {
24 SUCCESS = 0, // No error
25 FAILURE = -1, // Generic error
26 NOT_IMPLEMENTED = -2,
27 ADDRESS_NOT_FOUND = -3,
28 NETWORK_CHANGED = -4
29};
30
Jonas Orelandf7721fb2020-08-07 11:08:34 +020031// NetworkPreference property set by operating system/firmware that has
32// information about connection strength to e.g WIFI router or CELL base towers.
33// GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
34enum class NetworkPreference {
35 NEUTRAL = 0,
36 NOT_PREFERRED = -1,
37};
38
Taylor Brandstetter32eb03a2020-09-11 17:15:30 +000039const char* NetworkPreferenceToString(NetworkPreference preference);
40
Jonas Oreland6ca955a2021-03-15 08:27:43 +000041// This interface is set onto a socket server,
42// where only the ip address is known at the time of binding.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043class NetworkBinderInterface {
44 public:
45 // Binds a socket to the network that is attached to |address| so that all
46 // packets on the socket |socket_fd| will be sent via that network.
47 // This is needed because some operating systems (like Android) require a
48 // special bind call to put packets on a non-default network interface.
49 virtual NetworkBindingResult BindSocketToNetwork(
50 int socket_fd,
51 const IPAddress& address) = 0;
52 virtual ~NetworkBinderInterface() {}
53};
54
55/*
56 * Receives network-change events via |OnNetworksChanged| and signals the
57 * networks changed event.
58 *
59 * Threading consideration:
60 * It is expected that all upstream operations (from native to Java) are
61 * performed from the worker thread. This includes creating, starting and
62 * stopping the monitor. This avoids the potential race condition when creating
63 * the singleton Java NetworkMonitor class. Downstream operations can be from
64 * any thread, but this class will forward all the downstream operations onto
65 * the worker thread.
66 *
67 * Memory consideration:
68 * NetworkMonitor is owned by the caller (NetworkManager). The global network
Taylor Brandstetter07fc24d2020-08-12 01:58:10 +000069 * monitor factory is owned by the PeerConnectionFactory.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 */
71// Generic network monitor interface. It starts and stops monitoring network
72// changes, and fires the SignalNetworksChanged event when networks change.
73class NetworkMonitorInterface {
74 public:
75 NetworkMonitorInterface();
76 virtual ~NetworkMonitorInterface();
77
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 virtual void Start() = 0;
79 virtual void Stop() = 0;
80
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070082 virtual AdapterType GetVpnUnderlyingAdapterType(
83 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070084
Jonas Orelandf7721fb2020-08-07 11:08:34 +020085 virtual NetworkPreference GetNetworkPreference(
86 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070087
Jonas Oreland6ca955a2021-03-15 08:27:43 +000088 // Does |this| NetworkMonitorInterface implement BindSocketToNetwork?
89 // Only Android returns true.
90 virtual bool SupportsBindSocketToNetwork() const { return false; }
91
92 // Bind a socket to an interface specified by ip address and/or interface
93 // name. Only implemented on Android.
94 virtual NetworkBindingResult BindSocketToNetwork(
95 int socket_fd,
96 const IPAddress& address,
97 const std::string& interface_name) {
98 return NetworkBindingResult::NOT_IMPLEMENTED;
99 }
100
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700101 // Is this interface available to use? WebRTC shouldn't attempt to use it if
102 // this returns false.
103 //
104 // It's possible for this status to change, in which case
105 // SignalNetworksChanged will be fired.
106 //
107 // These specific use case this was added for was a phone with two SIM cards,
108 // where attempting to use all interfaces returned from getifaddrs caused the
109 // connection to be dropped.
110 virtual bool IsAdapterAvailable(const std::string& interface_name) {
111 return true;
112 }
Mirko Bonadei37077932021-07-27 17:00:58 +0200113
114 void SetNetworksChangedCallback(std::function<void()> callback) {
115 networks_changed_callback_ = std::move(callback);
116 }
117
118 protected:
119 void InvokeNetworksChangedCallback() {
120 if (networks_changed_callback_) {
121 networks_changed_callback_();
122 }
123 }
124
125 private:
126 std::function<void()> networks_changed_callback_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127};
128
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_NETWORK_MONITOR_H_