blob: 4a3002f4279219215a707410428a5aecd7627db4 [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
Patrik Höglunde2d6a062017-10-05 14:53:33 +020014#include "rtc_base/network_constants.h"
Artem Titove41c4332018-07-25 15:04:28 +020015#include "rtc_base/third_party/sigslot/sigslot.h"
honghaiz023f3ef2015-10-19 09:39:32 -070016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
19class IPAddress;
20
21enum 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 Orelandf7721fb2020-08-07 11:08:34 +020029// 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
32enum class NetworkPreference {
33 NEUTRAL = 0,
34 NOT_PREFERRED = -1,
35};
36
Taylor Brandstetter32eb03a2020-09-11 17:15:30 +000037const char* NetworkPreferenceToString(NetworkPreference preference);
38
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039class 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 Brandstetter07fc24d2020-08-12 01:58:10 +000065 * monitor factory is owned by the PeerConnectionFactory.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 */
67// Generic network monitor interface. It starts and stops monitoring network
68// changes, and fires the SignalNetworksChanged event when networks change.
69class 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 Kjellanderec78f1c2017-06-29 07:52:50 +020079 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070080 virtual AdapterType GetVpnUnderlyingAdapterType(
81 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070082
Jonas Orelandf7721fb2020-08-07 11:08:34 +020083 virtual NetworkPreference GetNetworkPreference(
84 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070085
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 Kjellanderec78f1c2017-06-29 07:52:50 +020098};
99
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700101
Steve Anton10542f22019-01-11 09:11:00 -0800102#endif // RTC_BASE_NETWORK_MONITOR_H_