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 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 17 | #include "rtc_base/network_constants.h" |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | namespace rtc { |
| 20 | |
| 21 | class IPAddress; |
| 22 | |
| 23 | enum 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 Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 31 | // 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 |
| 34 | enum class NetworkPreference { |
| 35 | NEUTRAL = 0, |
| 36 | NOT_PREFERRED = -1, |
| 37 | }; |
| 38 | |
Taylor Brandstetter | 32eb03a | 2020-09-11 17:15:30 +0000 | [diff] [blame] | 39 | const char* NetworkPreferenceToString(NetworkPreference preference); |
| 40 | |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 +0000 | [diff] [blame] | 41 | // This interface is set onto a socket server, |
| 42 | // where only the ip address is known at the time of binding. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | class 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 Brandstetter | 07fc24d | 2020-08-12 01:58:10 +0000 | [diff] [blame] | 69 | * monitor factory is owned by the PeerConnectionFactory. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 70 | */ |
| 71 | // Generic network monitor interface. It starts and stops monitoring network |
| 72 | // changes, and fires the SignalNetworksChanged event when networks change. |
| 73 | class NetworkMonitorInterface { |
| 74 | public: |
| 75 | NetworkMonitorInterface(); |
| 76 | virtual ~NetworkMonitorInterface(); |
| 77 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | virtual void Start() = 0; |
| 79 | virtual void Stop() = 0; |
| 80 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | virtual AdapterType GetAdapterType(const std::string& interface_name) = 0; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 82 | virtual AdapterType GetVpnUnderlyingAdapterType( |
| 83 | const std::string& interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 84 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 85 | virtual NetworkPreference GetNetworkPreference( |
| 86 | const std::string& interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 87 | |
Jonas Oreland | 6ca955a | 2021-03-15 08:27:43 +0000 | [diff] [blame] | 88 | // 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 Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 101 | // 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 Bonadei | 3707793 | 2021-07-27 17:00:58 +0200 | [diff] [blame] | 113 | |
| 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | } // namespace rtc |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 130 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 131 | #endif // RTC_BASE_NETWORK_MONITOR_H_ |