honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_NETWORK_MONITOR_H_ |
| 12 | #define RTC_BASE_NETWORK_MONITOR_H_ |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 3707793 | 2021-07-27 17:00:58 +0200 | [diff] [blame] | 14 | #include <functional> |
| 15 | #include <utility> |
| 16 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 18 | #include "rtc_base/network_constants.h" |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [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 11:08:34 +0200 | [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 +0000 | [diff] [blame] | 40 | const char* NetworkPreferenceToString(NetworkPreference preference); |
| 41 | |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 +0000 | [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 07:52:50 +0200 | [diff] [blame] | 44 | class NetworkBinderInterface { |
| 45 | public: |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [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 07:52:50 +0200 | [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 16:03:14 +0200 | [diff] [blame] | 57 | * Receives network-change events via `OnNetworksChanged` and signals the |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [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 +0000 | [diff] [blame] | 70 | * monitor factory is owned by the PeerConnectionFactory. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [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: |
| 76 | NetworkMonitorInterface(); |
| 77 | virtual ~NetworkMonitorInterface(); |
| 78 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | virtual void Start() = 0; |
| 80 | virtual void Stop() = 0; |
| 81 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 82 | virtual AdapterType GetAdapterType(absl::string_view interface_name) = 0; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 83 | virtual AdapterType GetVpnUnderlyingAdapterType( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 84 | absl::string_view interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 85 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 86 | virtual NetworkPreference GetNetworkPreference( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 87 | absl::string_view interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 88 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 89 | // Does `this` NetworkMonitorInterface implement BindSocketToNetwork? |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 +0000 | [diff] [blame] | 90 | // Only Android returns true. |
| 91 | virtual bool SupportsBindSocketToNetwork() const { return false; } |
| 92 | |
| 93 | // Bind a socket to an interface specified by ip address and/or interface |
| 94 | // name. Only implemented on Android. |
| 95 | virtual NetworkBindingResult BindSocketToNetwork( |
| 96 | int socket_fd, |
| 97 | const IPAddress& address, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 98 | absl::string_view interface_name) { |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 +0000 | [diff] [blame] | 99 | return NetworkBindingResult::NOT_IMPLEMENTED; |
| 100 | } |
| 101 | |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 102 | // Is this interface available to use? WebRTC shouldn't attempt to use it if |
| 103 | // this returns false. |
| 104 | // |
| 105 | // It's possible for this status to change, in which case |
| 106 | // SignalNetworksChanged will be fired. |
| 107 | // |
| 108 | // These specific use case this was added for was a phone with two SIM cards, |
| 109 | // where attempting to use all interfaces returned from getifaddrs caused the |
| 110 | // connection to be dropped. |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 111 | virtual bool IsAdapterAvailable(absl::string_view interface_name) { |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 112 | return true; |
| 113 | } |
Mirko Bonadei | 3707793 | 2021-07-27 17:00:58 +0200 | [diff] [blame] | 114 | |
| 115 | void SetNetworksChangedCallback(std::function<void()> callback) { |
| 116 | networks_changed_callback_ = std::move(callback); |
| 117 | } |
| 118 | |
| 119 | protected: |
| 120 | void InvokeNetworksChangedCallback() { |
| 121 | if (networks_changed_callback_) { |
| 122 | networks_changed_callback_(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | std::function<void()> networks_changed_callback_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 130 | } // namespace rtc |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 131 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 132 | #endif // RTC_BASE_NETWORK_MONITOR_H_ |