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 
17 namespace rtc {
18 
19 class IPAddress;
20 
21 enum class NetworkBindingResult {
22   SUCCESS = 0,   // No error
23   FAILURE = -1,  // Generic error
24   NOT_IMPLEMENTED = -2,
25   ADDRESS_NOT_FOUND = -3,
26   NETWORK_CHANGED = -4
27 };
28 
29 // NetworkPreference property set by operating system/firmware that has
30 // information about connection strength to e.g WIFI router or CELL base towers.
31 // GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
32 enum class NetworkPreference {
33   NEUTRAL = 0,
34   NOT_PREFERRED = -1,
35 };
36 
37 const char* NetworkPreferenceToString(NetworkPreference preference);
38 
39 class NetworkBinderInterface {
40  public:
41   // Binds a socket to the network that is attached to |address| so that all
42   // packets on the socket |socket_fd| will be sent via that network.
43   // This is needed because some operating systems (like Android) require a
44   // special bind call to put packets on a non-default network interface.
45   virtual NetworkBindingResult BindSocketToNetwork(
46       int socket_fd,
47       const IPAddress& address) = 0;
~NetworkBinderInterface()48   virtual ~NetworkBinderInterface() {}
49 };
50 
51 /*
52  * Receives network-change events via |OnNetworksChanged| and signals the
53  * networks changed event.
54  *
55  * Threading consideration:
56  * It is expected that all upstream operations (from native to Java) are
57  * performed from the worker thread. This includes creating, starting and
58  * stopping the monitor. This avoids the potential race condition when creating
59  * the singleton Java NetworkMonitor class. Downstream operations can be from
60  * any thread, but this class will forward all the downstream operations onto
61  * the worker thread.
62  *
63  * Memory consideration:
64  * NetworkMonitor is owned by the caller (NetworkManager). The global network
65  * monitor factory is owned by the PeerConnectionFactory.
66  */
67 // Generic network monitor interface. It starts and stops monitoring network
68 // changes, and fires the SignalNetworksChanged event when networks change.
69 class NetworkMonitorInterface {
70  public:
71   NetworkMonitorInterface();
72   virtual ~NetworkMonitorInterface();
73 
74   sigslot::signal0<> SignalNetworksChanged;
75 
76   virtual void Start() = 0;
77   virtual void Stop() = 0;
78 
79   virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
80   virtual AdapterType GetVpnUnderlyingAdapterType(
81       const std::string& interface_name) = 0;
82 
83   virtual NetworkPreference GetNetworkPreference(
84       const std::string& interface_name) = 0;
85 
86   // Is this interface available to use? WebRTC shouldn't attempt to use it if
87   // this returns false.
88   //
89   // It's possible for this status to change, in which case
90   // SignalNetworksChanged will be fired.
91   //
92   // These specific use case this was added for was a phone with two SIM cards,
93   // where attempting to use all interfaces returned from getifaddrs caused the
94   // connection to be dropped.
IsAdapterAvailable(const std::string & interface_name)95   virtual bool IsAdapterAvailable(const std::string& interface_name) {
96     return true;
97   }
98 };
99 
100 }  // namespace rtc
101 
102 #endif  // RTC_BASE_NETWORK_MONITOR_H_
103