blob: 1098219ee0a616ad0d177064f97c78f6de466fe3 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/thread.h"
honghaiz023f3ef2015-10-19 09:39:32 -070017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20class IPAddress;
21
22enum class NetworkBindingResult {
23 SUCCESS = 0, // No error
24 FAILURE = -1, // Generic error
25 NOT_IMPLEMENTED = -2,
26 ADDRESS_NOT_FOUND = -3,
27 NETWORK_CHANGED = -4
28};
29
Jonas Orelandf7721fb2020-08-07 11:08:34 +020030// NetworkPreference property set by operating system/firmware that has
31// information about connection strength to e.g WIFI router or CELL base towers.
32// GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
33enum class NetworkPreference {
34 NEUTRAL = 0,
35 NOT_PREFERRED = -1,
36};
37
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038class NetworkBinderInterface {
39 public:
40 // Binds a socket to the network that is attached to |address| so that all
41 // packets on the socket |socket_fd| will be sent via that network.
42 // This is needed because some operating systems (like Android) require a
43 // special bind call to put packets on a non-default network interface.
44 virtual NetworkBindingResult BindSocketToNetwork(
45 int socket_fd,
46 const IPAddress& address) = 0;
47 virtual ~NetworkBinderInterface() {}
48};
49
50/*
51 * Receives network-change events via |OnNetworksChanged| and signals the
52 * networks changed event.
53 *
54 * Threading consideration:
55 * It is expected that all upstream operations (from native to Java) are
56 * performed from the worker thread. This includes creating, starting and
57 * stopping the monitor. This avoids the potential race condition when creating
58 * the singleton Java NetworkMonitor class. Downstream operations can be from
59 * any thread, but this class will forward all the downstream operations onto
60 * the worker thread.
61 *
62 * Memory consideration:
63 * NetworkMonitor is owned by the caller (NetworkManager). The global network
Taylor Brandstetter07fc24d2020-08-12 01:58:10 +000064 * monitor factory is owned by the PeerConnectionFactory.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065 */
66// Generic network monitor interface. It starts and stops monitoring network
67// changes, and fires the SignalNetworksChanged event when networks change.
68class NetworkMonitorInterface {
69 public:
70 NetworkMonitorInterface();
71 virtual ~NetworkMonitorInterface();
72
73 sigslot::signal0<> SignalNetworksChanged;
74
75 virtual void Start() = 0;
76 virtual void Stop() = 0;
77
78 // Implementations should call this method on the base when networks change,
79 // and the base will fire SignalNetworksChanged on the right thread.
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070080 // TODO(deadbeef): This is an implementation detail of NetworkMonitorBase,
81 // it doesn't belong here.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082 virtual void OnNetworksChanged() = 0;
83
84 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070085 virtual AdapterType GetVpnUnderlyingAdapterType(
86 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070087
Jonas Orelandf7721fb2020-08-07 11:08:34 +020088 virtual NetworkPreference GetNetworkPreference(
89 const std::string& interface_name) = 0;
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070090
91 // Is this interface available to use? WebRTC shouldn't attempt to use it if
92 // this returns false.
93 //
94 // It's possible for this status to change, in which case
95 // SignalNetworksChanged will be fired.
96 //
97 // These specific use case this was added for was a phone with two SIM cards,
98 // where attempting to use all interfaces returned from getifaddrs caused the
99 // connection to be dropped.
100 virtual bool IsAdapterAvailable(const std::string& interface_name) {
101 return true;
102 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103};
104
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700105// TODO(deadbeef): This class has marginal value, all it does is post a task
106// to call SignalNetworksChanged on the worker thread. Should fold it into
107// AndroidNetworkMonitor.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108class NetworkMonitorBase : public NetworkMonitorInterface,
109 public MessageHandler,
110 public sigslot::has_slots<> {
111 public:
112 NetworkMonitorBase();
113 ~NetworkMonitorBase() override;
114
115 void OnNetworksChanged() override;
116
117 void OnMessage(Message* msg) override;
118
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700119 AdapterType GetVpnUnderlyingAdapterType(
120 const std::string& interface_name) override;
121
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 protected:
123 Thread* worker_thread() { return worker_thread_; }
124
125 private:
126 Thread* worker_thread_;
127};
128
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_NETWORK_MONITOR_H_