Chromium Code Reviews| 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 char kPublicIPv4Host[]; | |
|
juberti
2015/10/28 23:57:40
Do these need to be exported? They seem like imple
guoweis_webrtc
2015/10/29 03:41:00
I made them exported so chrome could use the same
| |
| 32 extern 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 DefaultAddressProvider { | |
| 58 public: | |
| 59 virtual ~DefaultAddressProvider() = default; | |
| 60 virtual bool GetDefaultPrivateAddress(int family, | |
|
juberti
2015/10/28 23:57:40
The address may not be private. Perhaps GetDefault
guoweis_webrtc
2015/10/29 03:41:00
Done.
| |
| 61 IPAddress* ipaddr) const = 0; | |
| 62 }; | |
| 63 | |
| 54 // Generic network manager interface. It provides list of local | 64 // Generic network manager interface. It provides list of local |
| 55 // networks. | 65 // networks. |
| 56 class NetworkManager { | 66 class NetworkManager : public DefaultAddressProvider { |
| 57 public: | 67 public: |
| 58 typedef std::vector<Network*> NetworkList; | 68 typedef std::vector<Network*> NetworkList; |
| 59 | 69 |
| 60 // This enum indicates whether adapter enumeration is allowed. | 70 // This enum indicates whether adapter enumeration is allowed. |
| 61 enum EnumerationPermission { | 71 enum EnumerationPermission { |
| 62 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network | 72 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network |
| 63 // from GetNetworks means that there is no network | 73 // from GetNetworks means that there is no network |
| 64 // available. | 74 // available. |
| 65 ENUMERATION_BLOCKED, // Adapter enumeration is disabled. | 75 ENUMERATION_BLOCKED, // Adapter enumeration is disabled. |
| 66 // GetAnyAddressNetworks() should be used instead. | 76 // GetAnyAddressNetworks() should be used instead. |
| 67 }; | 77 }; |
| 68 | 78 |
| 69 NetworkManager(); | 79 NetworkManager(); |
| 70 virtual ~NetworkManager(); | 80 ~NetworkManager() override; |
| 71 | 81 |
| 72 // Called when network list is updated. | 82 // Called when network list is updated. |
| 73 sigslot::signal0<> SignalNetworksChanged; | 83 sigslot::signal0<> SignalNetworksChanged; |
| 74 | 84 |
| 75 // Indicates a failure when getting list of network interfaces. | 85 // Indicates a failure when getting list of network interfaces. |
| 76 sigslot::signal0<> SignalError; | 86 sigslot::signal0<> SignalError; |
| 77 | 87 |
| 78 // Start/Stop monitoring of network interfaces | 88 // Start/Stop monitoring of network interfaces |
| 79 // list. SignalNetworksChanged or SignalError is emitted immediately | 89 // list. SignalNetworksChanged or SignalError is emitted immediately |
| 80 // after StartUpdating() is called. After that SignalNetworksChanged | 90 // after StartUpdating() is called. After that SignalNetworksChanged |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 92 // return the current permission state of GetNetworks() | 102 // return the current permission state of GetNetworks() |
| 93 virtual EnumerationPermission enumeration_permission() const; | 103 virtual EnumerationPermission enumeration_permission() const; |
| 94 | 104 |
| 95 // "AnyAddressNetwork" is a network which only contains single "any address" | 105 // "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 | 106 // 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 | 107 // useful as binding to such interfaces allow default routing behavior like |
| 98 // http traffic. | 108 // http traffic. |
| 99 // TODO(guoweis): remove this body when chromium implements this. | 109 // TODO(guoweis): remove this body when chromium implements this. |
| 100 virtual void GetAnyAddressNetworks(NetworkList* networks) {} | 110 virtual void GetAnyAddressNetworks(NetworkList* networks) {} |
| 101 | 111 |
| 112 bool GetDefaultPrivateAddress(int family, IPAddress* ipaddr) const override; | |
| 113 | |
| 102 // Dumps a list of networks available to LS_INFO. | 114 // Dumps a list of networks available to LS_INFO. |
| 103 virtual void DumpNetworks(bool include_ignored) {} | 115 virtual void DumpNetworks(bool include_ignored) {} |
| 104 | 116 |
| 105 struct Stats { | 117 struct Stats { |
| 106 int ipv4_network_count; | 118 int ipv4_network_count; |
| 107 int ipv6_network_count; | 119 int ipv6_network_count; |
| 108 Stats() { | 120 Stats() { |
| 109 ipv4_network_count = 0; | 121 ipv4_network_count = 0; |
| 110 ipv6_network_count = 0; | 122 ipv6_network_count = 0; |
| 111 } | 123 } |
| 112 }; | 124 }; |
| 113 }; | 125 }; |
| 114 | 126 |
| 115 // Base class for NetworkManager implementations. | 127 // Base class for NetworkManager implementations. |
| 116 class NetworkManagerBase : public NetworkManager { | 128 class NetworkManagerBase : public NetworkManager { |
| 117 public: | 129 public: |
| 118 NetworkManagerBase(); | 130 NetworkManagerBase(); |
| 119 ~NetworkManagerBase() override; | 131 ~NetworkManagerBase() override; |
| 120 | 132 |
| 121 void GetNetworks(std::vector<Network*>* networks) const override; | 133 void GetNetworks(std::vector<Network*>* networks) const override; |
| 122 void GetAnyAddressNetworks(NetworkList* networks) override; | 134 void GetAnyAddressNetworks(NetworkList* networks) override; |
| 123 bool ipv6_enabled() const { return ipv6_enabled_; } | 135 bool ipv6_enabled() const { return ipv6_enabled_; } |
| 124 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } | 136 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } |
| 125 | 137 |
| 126 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } | 138 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } |
| 127 int max_ipv6_networks() { return max_ipv6_networks_; } | 139 int max_ipv6_networks() { return max_ipv6_networks_; } |
| 128 | 140 |
| 129 EnumerationPermission enumeration_permission() const override; | 141 EnumerationPermission enumeration_permission() const override; |
| 130 | 142 |
| 143 bool GetDefaultPrivateAddress(int family, IPAddress* ipaddr) const override; | |
| 144 | |
| 131 protected: | 145 protected: |
| 132 typedef std::map<std::string, Network*> NetworkMap; | 146 typedef std::map<std::string, Network*> NetworkMap; |
| 133 // Updates |networks_| with the networks listed in |list|. If | 147 // Updates |networks_| with the networks listed in |list|. If |
| 134 // |network_map_| already has a Network object for a network listed | 148 // |network_map_| already has a Network object for a network listed |
| 135 // in the |list| then it is reused. Accept ownership of the Network | 149 // 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 | 150 // objects in the |list|. |changed| will be set to true if there is |
| 137 // any change in the network list. | 151 // any change in the network list. |
| 138 void MergeNetworkList(const NetworkList& list, bool* changed); | 152 void MergeNetworkList(const NetworkList& list, bool* changed); |
| 139 | 153 |
| 140 // |stats| will be populated even if |*changed| is false. | 154 // |stats| will be populated even if |*changed| is false. |
| 141 void MergeNetworkList(const NetworkList& list, | 155 void MergeNetworkList(const NetworkList& list, |
| 142 bool* changed, | 156 bool* changed, |
| 143 NetworkManager::Stats* stats); | 157 NetworkManager::Stats* stats); |
| 144 | 158 |
| 145 void set_enumeration_permission(EnumerationPermission state) { | 159 void set_enumeration_permission(EnumerationPermission state) { |
| 146 enumeration_permission_ = state; | 160 enumeration_permission_ = state; |
| 147 } | 161 } |
| 148 | 162 |
| 163 void set_default_private_address(const IPAddress& ip); | |
| 164 | |
| 149 private: | 165 private: |
| 150 friend class NetworkTest; | 166 friend class NetworkTest; |
| 151 | 167 |
| 152 EnumerationPermission enumeration_permission_; | 168 EnumerationPermission enumeration_permission_; |
| 153 | 169 |
| 154 NetworkList networks_; | 170 NetworkList networks_; |
| 155 int max_ipv6_networks_; | 171 int max_ipv6_networks_; |
| 156 | 172 |
| 157 NetworkMap networks_map_; | 173 NetworkMap networks_map_; |
| 158 bool ipv6_enabled_; | 174 bool ipv6_enabled_; |
| 159 | 175 |
| 160 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; | 176 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; |
| 161 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; | 177 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; |
| 178 | |
| 179 IPAddress default_ipv4_address_; | |
| 180 IPAddress default_ipv6_address_; | |
| 162 }; | 181 }; |
| 163 | 182 |
| 164 // Basic implementation of the NetworkManager interface that gets list | 183 // Basic implementation of the NetworkManager interface that gets list |
| 165 // of networks using OS APIs. | 184 // of networks using OS APIs. |
| 166 class BasicNetworkManager : public NetworkManagerBase, | 185 class BasicNetworkManager : public NetworkManagerBase, |
| 167 public MessageHandler, | 186 public MessageHandler, |
| 168 public sigslot::has_slots<> { | 187 public sigslot::has_slots<> { |
| 169 public: | 188 public: |
| 170 BasicNetworkManager(); | 189 BasicNetworkManager(); |
| 171 ~BasicNetworkManager() override; | 190 ~BasicNetworkManager() override; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 NetworkList* networks) const; | 232 NetworkList* networks) const; |
| 214 #endif // defined(WEBRTC_POSIX) | 233 #endif // defined(WEBRTC_POSIX) |
| 215 | 234 |
| 216 // Creates a network object for each network available on the machine. | 235 // Creates a network object for each network available on the machine. |
| 217 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; | 236 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; |
| 218 | 237 |
| 219 // Determines if a network should be ignored. This should only be determined | 238 // Determines if a network should be ignored. This should only be determined |
| 220 // based on the network's property instead of any individual IP. | 239 // based on the network's property instead of any individual IP. |
| 221 bool IsIgnoredNetwork(const Network& network) const; | 240 bool IsIgnoredNetwork(const Network& network) const; |
| 222 | 241 |
| 242 IPAddress UpdateDefaultPrivateAddress(int family); | |
| 243 | |
| 223 private: | 244 private: |
| 224 friend class NetworkTest; | 245 friend class NetworkTest; |
| 225 | 246 |
| 226 // Creates a network monitor and listens for network updates. | 247 // Creates a network monitor and listens for network updates. |
| 227 void StartNetworkMonitor(); | 248 void StartNetworkMonitor(); |
| 228 // Stops and removes the network monitor. | 249 // Stops and removes the network monitor. |
| 229 void StopNetworkMonitor(); | 250 void StopNetworkMonitor(); |
| 230 // Called when it receives updates from the network monitor. | 251 // Called when it receives updates from the network monitor. |
| 231 void OnNetworksChanged(); | 252 void OnNetworksChanged(); |
| 232 | 253 |
| 233 // Updates the networks and reschedules the next update. | 254 // Updates the networks and reschedules the next update. |
| 234 void UpdateNetworksContinually(); | 255 void UpdateNetworksContinually(); |
| 235 // Only updates the networks; does not reschedule the next update. | 256 // Only updates the networks; does not reschedule the next update. |
| 236 void UpdateNetworksOnce(); | 257 void UpdateNetworksOnce(); |
| 237 | 258 |
| 238 Thread* thread_; | 259 Thread* thread_; |
| 239 bool sent_first_update_; | 260 bool sent_first_update_; |
| 240 int start_count_; | 261 int start_count_; |
| 241 std::vector<std::string> network_ignore_list_; | 262 std::vector<std::string> network_ignore_list_; |
| 242 int network_ignore_mask_; | 263 int network_ignore_mask_; |
| 243 bool ignore_non_default_routes_; | 264 bool ignore_non_default_routes_; |
| 244 scoped_ptr<NetworkMonitorInterface> network_monitor_; | 265 scoped_ptr<NetworkMonitorInterface> network_monitor_; |
| 245 }; | 266 }; |
| 246 | 267 |
| 247 // Represents a Unix-type network interface, with a name and single address. | 268 // Represents a Unix-type network interface, with a name and single address. |
| 248 class Network { | 269 class Network { |
| 249 public: | 270 public: |
| 250 Network(const std::string& name, const std::string& description, | 271 Network(const std::string& name, |
| 251 const IPAddress& prefix, int prefix_length); | 272 const std::string& description, |
| 273 const IPAddress& prefix, | |
| 274 int prefix_length); | |
| 252 | 275 |
| 253 Network(const std::string& name, const std::string& description, | 276 Network(const std::string& name, |
| 254 const IPAddress& prefix, int prefix_length, AdapterType type); | 277 const std::string& description, |
| 278 const IPAddress& prefix, | |
| 279 int prefix_length, | |
| 280 AdapterType type); | |
| 255 ~Network(); | 281 ~Network(); |
| 256 | 282 |
| 283 const DefaultAddressProvider* default_address_provider() { return provider_; } | |
| 284 void set_default_address_provider(const DefaultAddressProvider* provider) { | |
| 285 provider_ = provider; | |
| 286 } | |
| 287 | |
| 257 // Returns the name of the interface this network is associated wtih. | 288 // Returns the name of the interface this network is associated wtih. |
| 258 const std::string& name() const { return name_; } | 289 const std::string& name() const { return name_; } |
| 259 | 290 |
| 260 // Returns the OS-assigned name for this network. This is useful for | 291 // 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). | 292 // debugging but should not be sent over the wire (for privacy reasons). |
| 262 const std::string& description() const { return description_; } | 293 const std::string& description() const { return description_; } |
| 263 | 294 |
| 264 // Returns the prefix for this network. | 295 // Returns the prefix for this network. |
| 265 const IPAddress& prefix() const { return prefix_; } | 296 const IPAddress& prefix() const { return prefix_; } |
| 266 // Returns the length, in bits, of this network's prefix. | 297 // 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; } | 347 void set_ignored(bool ignored) { ignored_ = ignored; } |
| 317 | 348 |
| 318 AdapterType type() const { return type_; } | 349 AdapterType type() const { return type_; } |
| 319 int preference() const { return preference_; } | 350 int preference() const { return preference_; } |
| 320 void set_preference(int preference) { preference_ = preference; } | 351 void set_preference(int preference) { preference_ = preference; } |
| 321 | 352 |
| 322 // Debugging description of this network | 353 // Debugging description of this network |
| 323 std::string ToString() const; | 354 std::string ToString() const; |
| 324 | 355 |
| 325 private: | 356 private: |
| 357 const DefaultAddressProvider* provider_ = nullptr; | |
| 326 std::string name_; | 358 std::string name_; |
| 327 std::string description_; | 359 std::string description_; |
| 328 IPAddress prefix_; | 360 IPAddress prefix_; |
| 329 int prefix_length_; | 361 int prefix_length_; |
| 330 std::string key_; | 362 std::string key_; |
| 331 std::vector<InterfaceAddress> ips_; | 363 std::vector<InterfaceAddress> ips_; |
| 332 int scope_id_; | 364 int scope_id_; |
| 333 bool ignored_; | 365 bool ignored_; |
| 334 AdapterType type_; | 366 AdapterType type_; |
| 335 int preference_; | 367 int preference_; |
| 336 | 368 |
| 337 friend class NetworkManager; | 369 friend class NetworkManager; |
| 338 }; | 370 }; |
| 339 | 371 |
| 340 } // namespace rtc | 372 } // namespace rtc |
| 341 | 373 |
| 342 #endif // WEBRTC_BASE_NETWORK_H_ | 374 #endif // WEBRTC_BASE_NETWORK_H_ |
| OLD | NEW |