blob: b57a20003f3b6aa269ff2af896c5ac6fa2bca6eb [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.
80 virtual void OnNetworksChanged() = 0;
81
82 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070083 virtual AdapterType GetVpnUnderlyingAdapterType(
84 const std::string& interface_name) = 0;
Jonas Orelandf7721fb2020-08-07 11:08:34 +020085 virtual NetworkPreference GetNetworkPreference(
86 const std::string& interface_name) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087};
88
89class NetworkMonitorBase : public NetworkMonitorInterface,
90 public MessageHandler,
91 public sigslot::has_slots<> {
92 public:
93 NetworkMonitorBase();
94 ~NetworkMonitorBase() override;
95
96 void OnNetworksChanged() override;
97
98 void OnMessage(Message* msg) override;
99
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700100 AdapterType GetVpnUnderlyingAdapterType(
101 const std::string& interface_name) override;
102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 protected:
104 Thread* worker_thread() { return worker_thread_; }
105
106 private:
107 Thread* worker_thread_;
108};
109
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700111
Steve Anton10542f22019-01-11 09:11:00 -0800112#endif // RTC_BASE_NETWORK_MONITOR_H_