blob: a84a30aed2bc3368665b7d4314775e63bf05561f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_NETWORKMONITOR_H_
12#define RTC_BASE_NETWORKMONITOR_H_
honghaiz023f3ef2015-10-19 09:39:32 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/logging.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020015#include "rtc_base/network_constants.h"
Artem Titove41c4332018-07-25 15:04:28 +020016#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/thread.h"
honghaiz023f3ef2015-10-19 09:39:32 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019namespace rtc {
20
21class IPAddress;
22
23enum class NetworkBindingResult {
24 SUCCESS = 0, // No error
25 FAILURE = -1, // Generic error
26 NOT_IMPLEMENTED = -2,
27 ADDRESS_NOT_FOUND = -3,
28 NETWORK_CHANGED = -4
29};
30
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031class NetworkBinderInterface {
32 public:
33 // Binds a socket to the network that is attached to |address| so that all
34 // packets on the socket |socket_fd| will be sent via that network.
35 // This is needed because some operating systems (like Android) require a
36 // special bind call to put packets on a non-default network interface.
37 virtual NetworkBindingResult BindSocketToNetwork(
38 int socket_fd,
39 const IPAddress& address) = 0;
40 virtual ~NetworkBinderInterface() {}
41};
42
43/*
44 * Receives network-change events via |OnNetworksChanged| and signals the
45 * networks changed event.
46 *
47 * Threading consideration:
48 * It is expected that all upstream operations (from native to Java) are
49 * performed from the worker thread. This includes creating, starting and
50 * stopping the monitor. This avoids the potential race condition when creating
51 * the singleton Java NetworkMonitor class. Downstream operations can be from
52 * any thread, but this class will forward all the downstream operations onto
53 * the worker thread.
54 *
55 * Memory consideration:
56 * NetworkMonitor is owned by the caller (NetworkManager). The global network
57 * monitor factory is owned by the factory itself but needs to be released from
58 * the factory creator.
59 */
60// Generic network monitor interface. It starts and stops monitoring network
61// changes, and fires the SignalNetworksChanged event when networks change.
62class NetworkMonitorInterface {
63 public:
64 NetworkMonitorInterface();
65 virtual ~NetworkMonitorInterface();
66
67 sigslot::signal0<> SignalNetworksChanged;
68
69 virtual void Start() = 0;
70 virtual void Stop() = 0;
71
72 // Implementations should call this method on the base when networks change,
73 // and the base will fire SignalNetworksChanged on the right thread.
74 virtual void OnNetworksChanged() = 0;
75
76 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070077 virtual AdapterType GetVpnUnderlyingAdapterType(
78 const std::string& interface_name) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079};
80
81class NetworkMonitorBase : public NetworkMonitorInterface,
82 public MessageHandler,
83 public sigslot::has_slots<> {
84 public:
85 NetworkMonitorBase();
86 ~NetworkMonitorBase() override;
87
88 void OnNetworksChanged() override;
89
90 void OnMessage(Message* msg) override;
91
Qingsi Wangde2ed7d2018-04-27 14:25:37 -070092 AdapterType GetVpnUnderlyingAdapterType(
93 const std::string& interface_name) override;
94
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 protected:
96 Thread* worker_thread() { return worker_thread_; }
97
98 private:
99 Thread* worker_thread_;
100};
101
102/*
103 * NetworkMonitorFactory creates NetworkMonitors.
104 */
105class NetworkMonitorFactory {
106 public:
107 // This is not thread-safe; it should be called once (or once per audio/video
108 // call) during the call initialization.
109 static void SetFactory(NetworkMonitorFactory* factory);
110
111 static void ReleaseFactory(NetworkMonitorFactory* factory);
112 static NetworkMonitorFactory* GetFactory();
113
114 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
115
116 virtual ~NetworkMonitorFactory();
117
118 protected:
119 NetworkMonitorFactory();
120};
121
122} // namespace rtc
honghaiz023f3ef2015-10-19 09:39:32 -0700123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // RTC_BASE_NETWORKMONITOR_H_