blob: dddc2f60f42a25047b27cf6e6f773e6f9b48260c [file] [log] [blame]
honghaiz023f3ef2015-10-19 16:39:321/*
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 17:11:0011#ifndef RTC_BASE_NETWORK_MONITOR_H_
12#define RTC_BASE_NETWORK_MONITOR_H_
honghaiz023f3ef2015-10-19 16:39:3213
Patrik Höglunde2d6a062017-10-05 12:53:3314#include "rtc_base/network_constants.h"
Artem Titove41c4332018-07-25 13:04:2815#include "rtc_base/third_party/sigslot/sigslot.h"
honghaiz023f3ef2015-10-19 16:39:3216
Henrik Kjellanderec78f1c2017-06-29 05:52:5017namespace 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 09:08:3429// 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:3037const char* NetworkPreferenceToString(NetworkPreference preference);
38
Jonas Oreland6ca955a2021-03-15 08:27:4339// This interface is set onto a socket server,
40// where only the ip address is known at the time of binding.
Henrik Kjellanderec78f1c2017-06-29 05:52:5041class NetworkBinderInterface {
42 public:
43 // Binds a socket to the network that is attached to |address| so that all
44 // packets on the socket |socket_fd| will be sent via that network.
45 // This is needed because some operating systems (like Android) require a
46 // special bind call to put packets on a non-default network interface.
47 virtual NetworkBindingResult BindSocketToNetwork(
48 int socket_fd,
49 const IPAddress& address) = 0;
50 virtual ~NetworkBinderInterface() {}
51};
52
53/*
54 * Receives network-change events via |OnNetworksChanged| and signals the
55 * networks changed event.
56 *
57 * Threading consideration:
58 * It is expected that all upstream operations (from native to Java) are
59 * performed from the worker thread. This includes creating, starting and
60 * stopping the monitor. This avoids the potential race condition when creating
61 * the singleton Java NetworkMonitor class. Downstream operations can be from
62 * any thread, but this class will forward all the downstream operations onto
63 * the worker thread.
64 *
65 * Memory consideration:
66 * NetworkMonitor is owned by the caller (NetworkManager). The global network
Taylor Brandstetter07fc24d2020-08-12 01:58:1067 * monitor factory is owned by the PeerConnectionFactory.
Henrik Kjellanderec78f1c2017-06-29 05:52:5068 */
69// Generic network monitor interface. It starts and stops monitoring network
70// changes, and fires the SignalNetworksChanged event when networks change.
71class NetworkMonitorInterface {
72 public:
73 NetworkMonitorInterface();
74 virtual ~NetworkMonitorInterface();
75
76 sigslot::signal0<> SignalNetworksChanged;
77
78 virtual void Start() = 0;
79 virtual void Stop() = 0;
80
Henrik Kjellanderec78f1c2017-06-29 05:52:5081 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 21:25:3782 virtual AdapterType GetVpnUnderlyingAdapterType(
83 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 23:41:5484
Jonas Orelandf7721fb2020-08-07 09:08:3485 virtual NetworkPreference GetNetworkPreference(
86 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 23:41:5487
Jonas Oreland6ca955a2021-03-15 08:27:4388 // 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 Brandstetterea7fbfb2020-08-19 23:41:54101 // 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 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50113};
114
Henrik Kjellanderec78f1c2017-06-29 05:52:50115} // namespace rtc
honghaiz023f3ef2015-10-19 16:39:32116
Steve Anton10542f22019-01-11 17:11:00117#endif // RTC_BASE_NETWORK_MONITOR_H_