blob: ed4464db5570f7b38ef11a2122b301a7b5e9bcec [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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030class NetworkBinderInterface {
31 public:
32 // Binds a socket to the network that is attached to |address| so that all
33 // packets on the socket |socket_fd| will be sent via that network.
34 // This is needed because some operating systems (like Android) require a
35 // special bind call to put packets on a non-default network interface.
36 virtual NetworkBindingResult BindSocketToNetwork(
37 int socket_fd,
38 const IPAddress& address) = 0;
39 virtual ~NetworkBinderInterface() {}
40};
41
42/*
43 * Receives network-change events via |OnNetworksChanged| and signals the
44 * networks changed event.
45 *
46 * Threading consideration:
47 * It is expected that all upstream operations (from native to Java) are
48 * performed from the worker thread. This includes creating, starting and
49 * stopping the monitor. This avoids the potential race condition when creating
50 * the singleton Java NetworkMonitor class. Downstream operations can be from
51 * any thread, but this class will forward all the downstream operations onto
52 * the worker thread.
53 *
54 * Memory consideration:
55 * NetworkMonitor is owned by the caller (NetworkManager). The global network
56 * monitor factory is owned by the factory itself but needs to be released from
57 * the factory creator.
58 */
59// Generic network monitor interface. It starts and stops monitoring network
60// changes, and fires the SignalNetworksChanged event when networks change.
61class NetworkMonitorInterface {
62 public:
63 NetworkMonitorInterface();
64 virtual ~NetworkMonitorInterface();
65
66 sigslot::signal0<> SignalNetworksChanged;
67
68 virtual void Start() = 0;
69 virtual void Stop() = 0;
70
71 // Implementations should call this method on the base when networks change,
72 // and the base will fire SignalNetworksChanged on the right thread.
73 virtual void OnNetworksChanged() = 0;
74
75 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070076 virtual AdapterType GetVpnUnderlyingAdapterType(
77 const std::string& interface_name) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078};
79
80class NetworkMonitorBase : public NetworkMonitorInterface,
81 public MessageHandler,
82 public sigslot::has_slots<> {
83 public:
84 NetworkMonitorBase();
85 ~NetworkMonitorBase() override;
86
87 void OnNetworksChanged() override;
88
89 void OnMessage(Message* msg) override;
90
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070091 AdapterType GetVpnUnderlyingAdapterType(
92 const std::string& interface_name) override;
93
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094 protected:
95 Thread* worker_thread() { return worker_thread_; }
96
97 private:
98 Thread* worker_thread_;
99};
100
101/*
102 * NetworkMonitorFactory creates NetworkMonitors.
103 */
104class NetworkMonitorFactory {
105 public:
106 // This is not thread-safe; it should be called once (or once per audio/video
107 // call) during the call initialization.
108 static void SetFactory(NetworkMonitorFactory* factory);
109
110 static void ReleaseFactory(NetworkMonitorFactory* factory);
111 static NetworkMonitorFactory* GetFactory();
112
113 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
114
115 virtual ~NetworkMonitorFactory();
116
117 protected:
118 NetworkMonitorFactory();
119};
120
121} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700122
Steve Anton10542f22019-01-11 09:11:00 -0800123#endif // RTC_BASE_NETWORK_MONITOR_H_