OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "webrtc/base/messagehandler.h" | 21 #include "webrtc/base/messagehandler.h" |
22 #include "webrtc/base/scoped_ptr.h" | 22 #include "webrtc/base/scoped_ptr.h" |
23 #include "webrtc/base/sigslot.h" | 23 #include "webrtc/base/sigslot.h" |
24 | 24 |
25 #if defined(WEBRTC_POSIX) | 25 #if defined(WEBRTC_POSIX) |
26 struct ifaddrs; | 26 struct ifaddrs; |
27 #endif // defined(WEBRTC_POSIX) | 27 #endif // defined(WEBRTC_POSIX) |
28 | 28 |
29 namespace rtc { | 29 namespace rtc { |
30 | 30 |
| 31 extern const char kPublicIPv4Host[]; |
| 32 extern const char kPublicIPv6Host[]; |
| 33 |
31 class Network; | 34 class Network; |
32 class NetworkMonitorInterface; | 35 class NetworkMonitorInterface; |
33 class Thread; | 36 class Thread; |
34 | 37 |
35 enum AdapterType { | 38 enum AdapterType { |
36 // This enum resembles the one in Chromium net::ConnectionType. | 39 // This enum resembles the one in Chromium net::ConnectionType. |
37 ADAPTER_TYPE_UNKNOWN = 0, | 40 ADAPTER_TYPE_UNKNOWN = 0, |
38 ADAPTER_TYPE_ETHERNET = 1 << 0, | 41 ADAPTER_TYPE_ETHERNET = 1 << 0, |
39 ADAPTER_TYPE_WIFI = 1 << 1, | 42 ADAPTER_TYPE_WIFI = 1 << 1, |
40 ADAPTER_TYPE_CELLULAR = 1 << 2, | 43 ADAPTER_TYPE_CELLULAR = 1 << 2, |
41 ADAPTER_TYPE_VPN = 1 << 3, | 44 ADAPTER_TYPE_VPN = 1 << 3, |
42 ADAPTER_TYPE_LOOPBACK = 1 << 4 | 45 ADAPTER_TYPE_LOOPBACK = 1 << 4 |
43 }; | 46 }; |
44 | 47 |
45 // By default, ignore loopback interfaces on the host. | 48 // By default, ignore loopback interfaces on the host. |
46 const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK; | 49 const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK; |
47 | 50 |
48 // Makes a string key for this network. Used in the network manager's maps. | 51 // Makes a string key for this network. Used in the network manager's maps. |
49 // Network objects are keyed on interface name, network prefix and the | 52 // Network objects are keyed on interface name, network prefix and the |
50 // length of that prefix. | 53 // length of that prefix. |
51 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, | 54 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, |
52 int prefix_length); | 55 int prefix_length); |
53 | 56 |
| 57 class DefaultLocalAddressProvider { |
| 58 public: |
| 59 virtual ~DefaultLocalAddressProvider() = default; |
| 60 // The default local address is the local address used in multi-homed endpoint |
| 61 // when the any address (0.0.0.0 or ::) is used as the local address. |
| 62 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0; |
| 63 }; |
| 64 |
54 // Generic network manager interface. It provides list of local | 65 // Generic network manager interface. It provides list of local |
55 // networks. | 66 // networks. |
56 class NetworkManager { | 67 class NetworkManager : public DefaultLocalAddressProvider { |
57 public: | 68 public: |
58 typedef std::vector<Network*> NetworkList; | 69 typedef std::vector<Network*> NetworkList; |
59 | 70 |
60 // This enum indicates whether adapter enumeration is allowed. | 71 // This enum indicates whether adapter enumeration is allowed. |
61 enum EnumerationPermission { | 72 enum EnumerationPermission { |
62 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network | 73 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network |
63 // from GetNetworks means that there is no network | 74 // from GetNetworks means that there is no network |
64 // available. | 75 // available. |
65 ENUMERATION_BLOCKED, // Adapter enumeration is disabled. | 76 ENUMERATION_BLOCKED, // Adapter enumeration is disabled. |
66 // GetAnyAddressNetworks() should be used instead. | 77 // GetAnyAddressNetworks() should be used instead. |
67 }; | 78 }; |
68 | 79 |
69 NetworkManager(); | 80 NetworkManager(); |
70 virtual ~NetworkManager(); | 81 ~NetworkManager() override; |
71 | 82 |
72 // Called when network list is updated. | 83 // Called when network list is updated. |
73 sigslot::signal0<> SignalNetworksChanged; | 84 sigslot::signal0<> SignalNetworksChanged; |
74 | 85 |
75 // Indicates a failure when getting list of network interfaces. | 86 // Indicates a failure when getting list of network interfaces. |
76 sigslot::signal0<> SignalError; | 87 sigslot::signal0<> SignalError; |
77 | 88 |
78 // Start/Stop monitoring of network interfaces | 89 // Start/Stop monitoring of network interfaces |
79 // list. SignalNetworksChanged or SignalError is emitted immediately | 90 // list. SignalNetworksChanged or SignalError is emitted immediately |
80 // after StartUpdating() is called. After that SignalNetworksChanged | 91 // after StartUpdating() is called. After that SignalNetworksChanged |
(...skipping 11 matching lines...) Expand all Loading... |
92 // return the current permission state of GetNetworks() | 103 // return the current permission state of GetNetworks() |
93 virtual EnumerationPermission enumeration_permission() const; | 104 virtual EnumerationPermission enumeration_permission() const; |
94 | 105 |
95 // "AnyAddressNetwork" is a network which only contains single "any address" | 106 // "AnyAddressNetwork" is a network which only contains single "any address" |
96 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is | 107 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is |
97 // useful as binding to such interfaces allow default routing behavior like | 108 // useful as binding to such interfaces allow default routing behavior like |
98 // http traffic. | 109 // http traffic. |
99 // TODO(guoweis): remove this body when chromium implements this. | 110 // TODO(guoweis): remove this body when chromium implements this. |
100 virtual void GetAnyAddressNetworks(NetworkList* networks) {} | 111 virtual void GetAnyAddressNetworks(NetworkList* networks) {} |
101 | 112 |
| 113 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 114 |
102 // Dumps a list of networks available to LS_INFO. | 115 // Dumps a list of networks available to LS_INFO. |
103 virtual void DumpNetworks(bool include_ignored) {} | 116 virtual void DumpNetworks(bool include_ignored) {} |
104 | 117 |
105 struct Stats { | 118 struct Stats { |
106 int ipv4_network_count; | 119 int ipv4_network_count; |
107 int ipv6_network_count; | 120 int ipv6_network_count; |
108 Stats() { | 121 Stats() { |
109 ipv4_network_count = 0; | 122 ipv4_network_count = 0; |
110 ipv6_network_count = 0; | 123 ipv6_network_count = 0; |
111 } | 124 } |
112 }; | 125 }; |
113 }; | 126 }; |
114 | 127 |
115 // Base class for NetworkManager implementations. | 128 // Base class for NetworkManager implementations. |
116 class NetworkManagerBase : public NetworkManager { | 129 class NetworkManagerBase : public NetworkManager { |
117 public: | 130 public: |
118 NetworkManagerBase(); | 131 NetworkManagerBase(); |
119 ~NetworkManagerBase() override; | 132 ~NetworkManagerBase() override; |
120 | 133 |
121 void GetNetworks(std::vector<Network*>* networks) const override; | 134 void GetNetworks(std::vector<Network*>* networks) const override; |
122 void GetAnyAddressNetworks(NetworkList* networks) override; | 135 void GetAnyAddressNetworks(NetworkList* networks) override; |
123 bool ipv6_enabled() const { return ipv6_enabled_; } | 136 bool ipv6_enabled() const { return ipv6_enabled_; } |
124 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } | 137 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } |
125 | 138 |
126 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } | 139 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } |
127 int max_ipv6_networks() { return max_ipv6_networks_; } | 140 int max_ipv6_networks() { return max_ipv6_networks_; } |
128 | 141 |
129 EnumerationPermission enumeration_permission() const override; | 142 EnumerationPermission enumeration_permission() const override; |
130 | 143 |
| 144 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 145 |
131 protected: | 146 protected: |
132 typedef std::map<std::string, Network*> NetworkMap; | 147 typedef std::map<std::string, Network*> NetworkMap; |
133 // Updates |networks_| with the networks listed in |list|. If | 148 // Updates |networks_| with the networks listed in |list|. If |
134 // |network_map_| already has a Network object for a network listed | 149 // |network_map_| already has a Network object for a network listed |
135 // in the |list| then it is reused. Accept ownership of the Network | 150 // in the |list| then it is reused. Accept ownership of the Network |
136 // objects in the |list|. |changed| will be set to true if there is | 151 // objects in the |list|. |changed| will be set to true if there is |
137 // any change in the network list. | 152 // any change in the network list. |
138 void MergeNetworkList(const NetworkList& list, bool* changed); | 153 void MergeNetworkList(const NetworkList& list, bool* changed); |
139 | 154 |
140 // |stats| will be populated even if |*changed| is false. | 155 // |stats| will be populated even if |*changed| is false. |
141 void MergeNetworkList(const NetworkList& list, | 156 void MergeNetworkList(const NetworkList& list, |
142 bool* changed, | 157 bool* changed, |
143 NetworkManager::Stats* stats); | 158 NetworkManager::Stats* stats); |
144 | 159 |
145 void set_enumeration_permission(EnumerationPermission state) { | 160 void set_enumeration_permission(EnumerationPermission state) { |
146 enumeration_permission_ = state; | 161 enumeration_permission_ = state; |
147 } | 162 } |
148 | 163 |
| 164 void set_default_local_addresses(const IPAddress& ipv4, |
| 165 const IPAddress& ipv6); |
| 166 |
149 private: | 167 private: |
150 friend class NetworkTest; | 168 friend class NetworkTest; |
151 | 169 |
152 EnumerationPermission enumeration_permission_; | 170 EnumerationPermission enumeration_permission_; |
153 | 171 |
154 NetworkList networks_; | 172 NetworkList networks_; |
155 int max_ipv6_networks_; | 173 int max_ipv6_networks_; |
156 | 174 |
157 NetworkMap networks_map_; | 175 NetworkMap networks_map_; |
158 bool ipv6_enabled_; | 176 bool ipv6_enabled_; |
159 | 177 |
160 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; | 178 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; |
161 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; | 179 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; |
| 180 |
| 181 IPAddress default_local_ipv4_address_; |
| 182 IPAddress default_local_ipv6_address_; |
162 }; | 183 }; |
163 | 184 |
164 // Basic implementation of the NetworkManager interface that gets list | 185 // Basic implementation of the NetworkManager interface that gets list |
165 // of networks using OS APIs. | 186 // of networks using OS APIs. |
166 class BasicNetworkManager : public NetworkManagerBase, | 187 class BasicNetworkManager : public NetworkManagerBase, |
167 public MessageHandler, | 188 public MessageHandler, |
168 public sigslot::has_slots<> { | 189 public sigslot::has_slots<> { |
169 public: | 190 public: |
170 BasicNetworkManager(); | 191 BasicNetworkManager(); |
171 ~BasicNetworkManager() override; | 192 ~BasicNetworkManager() override; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 NetworkList* networks) const; | 234 NetworkList* networks) const; |
214 #endif // defined(WEBRTC_POSIX) | 235 #endif // defined(WEBRTC_POSIX) |
215 | 236 |
216 // Creates a network object for each network available on the machine. | 237 // Creates a network object for each network available on the machine. |
217 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; | 238 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; |
218 | 239 |
219 // Determines if a network should be ignored. This should only be determined | 240 // Determines if a network should be ignored. This should only be determined |
220 // based on the network's property instead of any individual IP. | 241 // based on the network's property instead of any individual IP. |
221 bool IsIgnoredNetwork(const Network& network) const; | 242 bool IsIgnoredNetwork(const Network& network) const; |
222 | 243 |
| 244 // This function connects a UDP socket to a public address and returns the |
| 245 // local address associated it. Since it binds to the "any" address |
| 246 // internally, it returns the default local address on a multi-homed endpoint. |
| 247 IPAddress QueryDefaultLocalAddress(int family) const; |
| 248 |
223 private: | 249 private: |
224 friend class NetworkTest; | 250 friend class NetworkTest; |
225 | 251 |
226 // Creates a network monitor and listens for network updates. | 252 // Creates a network monitor and listens for network updates. |
227 void StartNetworkMonitor(); | 253 void StartNetworkMonitor(); |
228 // Stops and removes the network monitor. | 254 // Stops and removes the network monitor. |
229 void StopNetworkMonitor(); | 255 void StopNetworkMonitor(); |
230 // Called when it receives updates from the network monitor. | 256 // Called when it receives updates from the network monitor. |
231 void OnNetworksChanged(); | 257 void OnNetworksChanged(); |
232 | 258 |
233 // Updates the networks and reschedules the next update. | 259 // Updates the networks and reschedules the next update. |
234 void UpdateNetworksContinually(); | 260 void UpdateNetworksContinually(); |
235 // Only updates the networks; does not reschedule the next update. | 261 // Only updates the networks; does not reschedule the next update. |
236 void UpdateNetworksOnce(); | 262 void UpdateNetworksOnce(); |
237 | 263 |
238 Thread* thread_; | 264 Thread* thread_; |
239 bool sent_first_update_; | 265 bool sent_first_update_; |
240 int start_count_; | 266 int start_count_; |
241 std::vector<std::string> network_ignore_list_; | 267 std::vector<std::string> network_ignore_list_; |
242 int network_ignore_mask_; | 268 int network_ignore_mask_; |
243 bool ignore_non_default_routes_; | 269 bool ignore_non_default_routes_; |
244 scoped_ptr<NetworkMonitorInterface> network_monitor_; | 270 scoped_ptr<NetworkMonitorInterface> network_monitor_; |
245 }; | 271 }; |
246 | 272 |
247 // Represents a Unix-type network interface, with a name and single address. | 273 // Represents a Unix-type network interface, with a name and single address. |
248 class Network { | 274 class Network { |
249 public: | 275 public: |
250 Network(const std::string& name, const std::string& description, | 276 Network(const std::string& name, |
251 const IPAddress& prefix, int prefix_length); | 277 const std::string& description, |
| 278 const IPAddress& prefix, |
| 279 int prefix_length); |
252 | 280 |
253 Network(const std::string& name, const std::string& description, | 281 Network(const std::string& name, |
254 const IPAddress& prefix, int prefix_length, AdapterType type); | 282 const std::string& description, |
| 283 const IPAddress& prefix, |
| 284 int prefix_length, |
| 285 AdapterType type); |
255 ~Network(); | 286 ~Network(); |
256 | 287 |
| 288 const DefaultLocalAddressProvider* default_local_address_provider() { |
| 289 return default_local_address_provider_; |
| 290 } |
| 291 void set_default_local_address_provider( |
| 292 const DefaultLocalAddressProvider* provider) { |
| 293 default_local_address_provider_ = provider; |
| 294 } |
| 295 |
257 // Returns the name of the interface this network is associated wtih. | 296 // Returns the name of the interface this network is associated wtih. |
258 const std::string& name() const { return name_; } | 297 const std::string& name() const { return name_; } |
259 | 298 |
260 // Returns the OS-assigned name for this network. This is useful for | 299 // Returns the OS-assigned name for this network. This is useful for |
261 // debugging but should not be sent over the wire (for privacy reasons). | 300 // debugging but should not be sent over the wire (for privacy reasons). |
262 const std::string& description() const { return description_; } | 301 const std::string& description() const { return description_; } |
263 | 302 |
264 // Returns the prefix for this network. | 303 // Returns the prefix for this network. |
265 const IPAddress& prefix() const { return prefix_; } | 304 const IPAddress& prefix() const { return prefix_; } |
266 // Returns the length, in bits, of this network's prefix. | 305 // Returns the length, in bits, of this network's prefix. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 void set_ignored(bool ignored) { ignored_ = ignored; } | 355 void set_ignored(bool ignored) { ignored_ = ignored; } |
317 | 356 |
318 AdapterType type() const { return type_; } | 357 AdapterType type() const { return type_; } |
319 int preference() const { return preference_; } | 358 int preference() const { return preference_; } |
320 void set_preference(int preference) { preference_ = preference; } | 359 void set_preference(int preference) { preference_ = preference; } |
321 | 360 |
322 // Debugging description of this network | 361 // Debugging description of this network |
323 std::string ToString() const; | 362 std::string ToString() const; |
324 | 363 |
325 private: | 364 private: |
| 365 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr; |
326 std::string name_; | 366 std::string name_; |
327 std::string description_; | 367 std::string description_; |
328 IPAddress prefix_; | 368 IPAddress prefix_; |
329 int prefix_length_; | 369 int prefix_length_; |
330 std::string key_; | 370 std::string key_; |
331 std::vector<InterfaceAddress> ips_; | 371 std::vector<InterfaceAddress> ips_; |
332 int scope_id_; | 372 int scope_id_; |
333 bool ignored_; | 373 bool ignored_; |
334 AdapterType type_; | 374 AdapterType type_; |
335 int preference_; | 375 int preference_; |
336 | 376 |
337 friend class NetworkManager; | 377 friend class NetworkManager; |
338 }; | 378 }; |
339 | 379 |
340 } // namespace rtc | 380 } // namespace rtc |
341 | 381 |
342 #endif // WEBRTC_BASE_NETWORK_H_ | 382 #endif // WEBRTC_BASE_NETWORK_H_ |
OLD | NEW |