honghaiz | 023f3ef | 2015-10-19 16:39:32 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_NETWORK_MONITOR_H_ |
| 12 | #define RTC_BASE_NETWORK_MONITOR_H_ |
honghaiz | 023f3ef | 2015-10-19 16:39:32 | [diff] [blame] | 13 | |
Mirko Bonadei | 3707793 | 2021-07-27 15:00:58 | [diff] [blame] | 14 | #include <functional> |
| 15 | #include <utility> |
| 16 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 18 | #include "rtc_base/network_constants.h" |
honghaiz | 023f3ef | 2015-10-19 16:39:32 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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 | |
Jonas Oreland | f7721fb | 2020-08-07 09:08:34 | [diff] [blame] | 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 | |
Taylor Brandstetter | 32eb03a | 2020-09-11 17:15:30 | [diff] [blame] | 40 | const char* NetworkPreferenceToString(NetworkPreference preference); |
| 41 | |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 | [diff] [blame] | 42 | // This interface is set onto a socket server, |
| 43 | // where only the ip address is known at the time of binding. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 44 | class NetworkBinderInterface { |
| 45 | public: |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 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. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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; |
| 53 | virtual ~NetworkBinderInterface() {} |
| 54 | }; |
| 55 | |
| 56 | /* |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 57 | * Receives network-change events via `OnNetworksChanged` and signals the |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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 |
Taylor Brandstetter | 07fc24d | 2020-08-12 01:58:10 | [diff] [blame] | 70 | * monitor factory is owned by the PeerConnectionFactory. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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: |
Jonas Oreland | 61dbcd1 | 2022-05-31 09:34:20 | [diff] [blame] | 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 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 98 | NetworkMonitorInterface(); |
| 99 | virtual ~NetworkMonitorInterface(); |
| 100 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 101 | virtual void Start() = 0; |
| 102 | virtual void Stop() = 0; |
| 103 | |
Jonas Oreland | 61dbcd1 | 2022-05-31 09:34:20 | [diff] [blame] | 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; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 23:41:54 | [diff] [blame] | 108 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 109 | // Does `this` NetworkMonitorInterface implement BindSocketToNetwork? |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 | [diff] [blame] | 110 | // 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 Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 118 | absl::string_view interface_name) { |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 | [diff] [blame] | 119 | return NetworkBindingResult::NOT_IMPLEMENTED; |
| 120 | } |
| 121 | |
Mirko Bonadei | 3707793 | 2021-07-27 15:00:58 | [diff] [blame] | 122 | 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 Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 135 | }; |
| 136 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 137 | } // namespace rtc |
honghaiz | 023f3ef | 2015-10-19 16:39:32 | [diff] [blame] | 138 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 139 | #endif // RTC_BASE_NETWORK_MONITOR_H_ |