1 /* 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 11 #ifndef RTC_BASE_NETWORK_MONITOR_H_ 12 #define RTC_BASE_NETWORK_MONITOR_H_ 13 14 #include "rtc_base/network_constants.h" 15 #include "rtc_base/third_party/sigslot/sigslot.h" 16 #include "rtc_base/thread.h" 17 18 namespace rtc { 19 20 class IPAddress; 21 22 enum 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 30 class 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; ~NetworkBinderInterface()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. 61 class 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; 76 virtual AdapterType GetVpnUnderlyingAdapterType( 77 const std::string& interface_name) = 0; 78 }; 79 80 class 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 91 AdapterType GetVpnUnderlyingAdapterType( 92 const std::string& interface_name) override; 93 94 protected: worker_thread()95 Thread* worker_thread() { return worker_thread_; } 96 97 private: 98 Thread* worker_thread_; 99 }; 100 101 /* 102 * NetworkMonitorFactory creates NetworkMonitors. 103 */ 104 class 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 122 123 #endif // RTC_BASE_NETWORK_MONITOR_H_ 124