blob: eb3c3d65fdde86e9d202ec4d87ec58e309370d1d [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"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070015// TODO(deadbeef): Remove this include when downstream code stops using
16// NetworkMonitorFactory::SetFactory.
17#include "rtc_base/network_monitor_factory.h"
Artem Titove41c4332018-07-25 15:04:28 +020018#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/thread.h"
honghaiz023f3ef2015-10-19 09:39:32 -070020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace rtc {
22
23class IPAddress;
24
25enum class NetworkBindingResult {
26 SUCCESS = 0, // No error
27 FAILURE = -1, // Generic error
28 NOT_IMPLEMENTED = -2,
29 ADDRESS_NOT_FOUND = -3,
30 NETWORK_CHANGED = -4
31};
32
Jonas Orelandf7721fb2020-08-07 11:08:34 +020033// NetworkPreference property set by operating system/firmware that has
34// information about connection strength to e.g WIFI router or CELL base towers.
35// GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
36enum class NetworkPreference {
37 NEUTRAL = 0,
38 NOT_PREFERRED = -1,
39};
40
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041class 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
67 * monitor factory is owned by the factory itself but needs to be released from
68 * the factory creator.
69 */
70// Generic network monitor interface. It starts and stops monitoring network
71// changes, and fires the SignalNetworksChanged event when networks change.
72class NetworkMonitorInterface {
73 public:
74 NetworkMonitorInterface();
75 virtual ~NetworkMonitorInterface();
76
77 sigslot::signal0<> SignalNetworksChanged;
78
79 virtual void Start() = 0;
80 virtual void Stop() = 0;
81
82 // Implementations should call this method on the base when networks change,
83 // and the base will fire SignalNetworksChanged on the right thread.
84 virtual void OnNetworksChanged() = 0;
85
86 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070087 virtual AdapterType GetVpnUnderlyingAdapterType(
88 const std::string& interface_name) = 0;
Jonas Orelandf7721fb2020-08-07 11:08:34 +020089 virtual NetworkPreference GetNetworkPreference(
90 const std::string& interface_name) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091};
92
93class NetworkMonitorBase : public NetworkMonitorInterface,
94 public MessageHandler,
95 public sigslot::has_slots<> {
96 public:
97 NetworkMonitorBase();
98 ~NetworkMonitorBase() override;
99
100 void OnNetworksChanged() override;
101
102 void OnMessage(Message* msg) override;
103
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700104 AdapterType GetVpnUnderlyingAdapterType(
105 const std::string& interface_name) override;
106
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 protected:
108 Thread* worker_thread() { return worker_thread_; }
109
110 private:
111 Thread* worker_thread_;
112};
113
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700115
Steve Anton10542f22019-01-11 09:11:00 -0800116#endif // RTC_BASE_NETWORK_MONITOR_H_