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 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 14 | #include "rtc_base/network_constants.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 15 | #include "rtc_base/third_party/sigslot/sigslot.h" |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | namespace rtc { |
| 18 | |
| 19 | class IPAddress; |
| 20 | |
| 21 | enum class NetworkBindingResult { |
| 22 | SUCCESS = 0, // No error |
| 23 | FAILURE = -1, // Generic error |
| 24 | NOT_IMPLEMENTED = -2, |
| 25 | ADDRESS_NOT_FOUND = -3, |
| 26 | NETWORK_CHANGED = -4 |
| 27 | }; |
| 28 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 29 | // NetworkPreference property set by operating system/firmware that has |
| 30 | // information about connection strength to e.g WIFI router or CELL base towers. |
| 31 | // GENERATED_JAVA_ENUM_PACKAGE: org.webrtc |
| 32 | enum class NetworkPreference { |
| 33 | NEUTRAL = 0, |
| 34 | NOT_PREFERRED = -1, |
| 35 | }; |
| 36 | |
Taylor Brandstetter | 32eb03a | 2020-09-11 17:15:30 +0000 | [diff] [blame] | 37 | const char* NetworkPreferenceToString(NetworkPreference preference); |
| 38 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 39 | class NetworkBinderInterface { |
| 40 | public: |
| 41 | // Binds a socket to the network that is attached to |address| so that all |
| 42 | // packets on the socket |socket_fd| will be sent via that network. |
| 43 | // This is needed because some operating systems (like Android) require a |
| 44 | // special bind call to put packets on a non-default network interface. |
| 45 | virtual NetworkBindingResult BindSocketToNetwork( |
| 46 | int socket_fd, |
| 47 | const IPAddress& address) = 0; |
| 48 | virtual ~NetworkBinderInterface() {} |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Receives network-change events via |OnNetworksChanged| and signals the |
| 53 | * networks changed event. |
| 54 | * |
| 55 | * Threading consideration: |
| 56 | * It is expected that all upstream operations (from native to Java) are |
| 57 | * performed from the worker thread. This includes creating, starting and |
| 58 | * stopping the monitor. This avoids the potential race condition when creating |
| 59 | * the singleton Java NetworkMonitor class. Downstream operations can be from |
| 60 | * any thread, but this class will forward all the downstream operations onto |
| 61 | * the worker thread. |
| 62 | * |
| 63 | * Memory consideration: |
| 64 | * NetworkMonitor is owned by the caller (NetworkManager). The global network |
Taylor Brandstetter | 07fc24d | 2020-08-12 01:58:10 +0000 | [diff] [blame] | 65 | * monitor factory is owned by the PeerConnectionFactory. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | */ |
| 67 | // Generic network monitor interface. It starts and stops monitoring network |
| 68 | // changes, and fires the SignalNetworksChanged event when networks change. |
| 69 | class NetworkMonitorInterface { |
| 70 | public: |
| 71 | NetworkMonitorInterface(); |
| 72 | virtual ~NetworkMonitorInterface(); |
| 73 | |
| 74 | sigslot::signal0<> SignalNetworksChanged; |
| 75 | |
| 76 | virtual void Start() = 0; |
| 77 | virtual void Stop() = 0; |
| 78 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | virtual AdapterType GetAdapterType(const std::string& interface_name) = 0; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 80 | virtual AdapterType GetVpnUnderlyingAdapterType( |
| 81 | const std::string& interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 82 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 83 | virtual NetworkPreference GetNetworkPreference( |
| 84 | const std::string& interface_name) = 0; |
Taylor Brandstetter | ea7fbfb | 2020-08-19 16:41:54 -0700 | [diff] [blame] | 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 | // These specific use case this was added for was a phone with two SIM cards, |
| 93 | // where attempting to use all interfaces returned from getifaddrs caused the |
| 94 | // connection to be dropped. |
| 95 | virtual bool IsAdapterAvailable(const std::string& interface_name) { |
| 96 | return true; |
| 97 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 100 | } // namespace rtc |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 101 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 102 | #endif // RTC_BASE_NETWORK_MONITOR_H_ |