Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: webrtc/base/network.h

Issue 1411253008: WebRTC should generate default private address even when adapter enumeration is disabled. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: rename set_default_private_address to set_default_local_address Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 DefaultAddressProvider {
pthatcher1 2015/11/10 18:52:34 The DefaultAddressProvider has a GetDefaultLocalAd
guoweis_webrtc 2015/11/10 20:37:29 Done.
58 public:
59 virtual ~DefaultAddressProvider() = default;
60 // Default local address is the interface used to route to public internet on
61 // a multi-homed endpoint.
pthatcher1 2015/11/10 18:52:34 This could be more clear. Perhaps "The default lo
guoweis_webrtc 2015/11/10 20:37:29 Done.
pthatcher1 2015/11/10 21:29:01 It doesn't appear changed.
62 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
pthatcher1 2015/11/10 18:52:34 Would this be more simple as GetDefaultLocalAddres
guoweis_webrtc 2015/11/10 20:37:29 I like returning a bool which makes the caller to
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 DefaultAddressProvider {
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
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_address(const IPAddress& ip);
pthatcher1 2015/11/10 18:52:34 This is kind of awkward that we have a "set_foo" w
guoweis_webrtc 2015/11/10 20:37:29 Done.
165
149 private: 166 private:
150 friend class NetworkTest; 167 friend class NetworkTest;
151 168
152 EnumerationPermission enumeration_permission_; 169 EnumerationPermission enumeration_permission_;
153 170
154 NetworkList networks_; 171 NetworkList networks_;
155 int max_ipv6_networks_; 172 int max_ipv6_networks_;
156 173
157 NetworkMap networks_map_; 174 NetworkMap networks_map_;
158 bool ipv6_enabled_; 175 bool ipv6_enabled_;
159 176
160 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; 177 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_;
161 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; 178 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_;
179
180 IPAddress default_ipv4_address_;
181 IPAddress default_ipv6_address_;
pthatcher1 2015/11/10 18:52:34 To be consistent, this should be default_local_ipX
guoweis_webrtc 2015/11/10 20:37:29 Done.
162 }; 182 };
163 183
164 // Basic implementation of the NetworkManager interface that gets list 184 // Basic implementation of the NetworkManager interface that gets list
165 // of networks using OS APIs. 185 // of networks using OS APIs.
166 class BasicNetworkManager : public NetworkManagerBase, 186 class BasicNetworkManager : public NetworkManagerBase,
167 public MessageHandler, 187 public MessageHandler,
168 public sigslot::has_slots<> { 188 public sigslot::has_slots<> {
169 public: 189 public:
170 BasicNetworkManager(); 190 BasicNetworkManager();
171 ~BasicNetworkManager() override; 191 ~BasicNetworkManager() override;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 NetworkList* networks) const; 233 NetworkList* networks) const;
214 #endif // defined(WEBRTC_POSIX) 234 #endif // defined(WEBRTC_POSIX)
215 235
216 // Creates a network object for each network available on the machine. 236 // Creates a network object for each network available on the machine.
217 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; 237 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
218 238
219 // Determines if a network should be ignored. This should only be determined 239 // Determines if a network should be ignored. This should only be determined
220 // based on the network's property instead of any individual IP. 240 // based on the network's property instead of any individual IP.
221 bool IsIgnoredNetwork(const Network& network) const; 241 bool IsIgnoredNetwork(const Network& network) const;
222 242
243 IPAddress QueryDefaultAddress(int family) const;
pthatcher1 2015/11/10 18:52:34 This could use a comment to make clear the differe
guoweis_webrtc 2015/11/10 20:37:29 Done.
244
223 private: 245 private:
224 friend class NetworkTest; 246 friend class NetworkTest;
225 247
226 // Creates a network monitor and listens for network updates. 248 // Creates a network monitor and listens for network updates.
227 void StartNetworkMonitor(); 249 void StartNetworkMonitor();
228 // Stops and removes the network monitor. 250 // Stops and removes the network monitor.
229 void StopNetworkMonitor(); 251 void StopNetworkMonitor();
230 // Called when it receives updates from the network monitor. 252 // Called when it receives updates from the network monitor.
231 void OnNetworksChanged(); 253 void OnNetworksChanged();
232 254
233 // Updates the networks and reschedules the next update. 255 // Updates the networks and reschedules the next update.
234 void UpdateNetworksContinually(); 256 void UpdateNetworksContinually();
235 // Only updates the networks; does not reschedule the next update. 257 // Only updates the networks; does not reschedule the next update.
236 void UpdateNetworksOnce(); 258 void UpdateNetworksOnce();
237 259
238 Thread* thread_; 260 Thread* thread_;
239 bool sent_first_update_; 261 bool sent_first_update_;
240 int start_count_; 262 int start_count_;
241 std::vector<std::string> network_ignore_list_; 263 std::vector<std::string> network_ignore_list_;
242 int network_ignore_mask_; 264 int network_ignore_mask_;
243 bool ignore_non_default_routes_; 265 bool ignore_non_default_routes_;
244 scoped_ptr<NetworkMonitorInterface> network_monitor_; 266 scoped_ptr<NetworkMonitorInterface> network_monitor_;
245 }; 267 };
246 268
247 // Represents a Unix-type network interface, with a name and single address. 269 // Represents a Unix-type network interface, with a name and single address.
248 class Network { 270 class Network {
249 public: 271 public:
250 Network(const std::string& name, const std::string& description, 272 Network(const std::string& name,
251 const IPAddress& prefix, int prefix_length); 273 const std::string& description,
274 const IPAddress& prefix,
275 int prefix_length);
252 276
253 Network(const std::string& name, const std::string& description, 277 Network(const std::string& name,
254 const IPAddress& prefix, int prefix_length, AdapterType type); 278 const std::string& description,
279 const IPAddress& prefix,
280 int prefix_length,
281 AdapterType type);
255 ~Network(); 282 ~Network();
256 283
284 const DefaultAddressProvider* default_address_provider() { return provider_; }
pthatcher1 2015/11/10 18:52:34 Between Network and DefaultAddressProvider, you em
guoweis_webrtc 2015/11/10 20:37:29 This is to reflect the fact that a single network
285 void set_default_address_provider(const DefaultAddressProvider* provider) {
286 provider_ = provider;
287 }
288
257 // Returns the name of the interface this network is associated wtih. 289 // Returns the name of the interface this network is associated wtih.
258 const std::string& name() const { return name_; } 290 const std::string& name() const { return name_; }
259 291
260 // Returns the OS-assigned name for this network. This is useful for 292 // 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). 293 // debugging but should not be sent over the wire (for privacy reasons).
262 const std::string& description() const { return description_; } 294 const std::string& description() const { return description_; }
263 295
264 // Returns the prefix for this network. 296 // Returns the prefix for this network.
265 const IPAddress& prefix() const { return prefix_; } 297 const IPAddress& prefix() const { return prefix_; }
266 // Returns the length, in bits, of this network's prefix. 298 // Returns the length, in bits, of this network's prefix.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 void set_ignored(bool ignored) { ignored_ = ignored; } 348 void set_ignored(bool ignored) { ignored_ = ignored; }
317 349
318 AdapterType type() const { return type_; } 350 AdapterType type() const { return type_; }
319 int preference() const { return preference_; } 351 int preference() const { return preference_; }
320 void set_preference(int preference) { preference_ = preference; } 352 void set_preference(int preference) { preference_ = preference; }
321 353
322 // Debugging description of this network 354 // Debugging description of this network
323 std::string ToString() const; 355 std::string ToString() const;
324 356
325 private: 357 private:
358 const DefaultAddressProvider* provider_ = nullptr;
pthatcher1 2015/11/10 18:52:34 To be consistent, this should be default_address_p
guoweis_webrtc 2015/11/10 20:37:29 Done.
326 std::string name_; 359 std::string name_;
327 std::string description_; 360 std::string description_;
328 IPAddress prefix_; 361 IPAddress prefix_;
329 int prefix_length_; 362 int prefix_length_;
330 std::string key_; 363 std::string key_;
331 std::vector<InterfaceAddress> ips_; 364 std::vector<InterfaceAddress> ips_;
332 int scope_id_; 365 int scope_id_;
333 bool ignored_; 366 bool ignored_;
334 AdapterType type_; 367 AdapterType type_;
335 int preference_; 368 int preference_;
336 369
337 friend class NetworkManager; 370 friend class NetworkManager;
338 }; 371 };
339 372
340 } // namespace rtc 373 } // namespace rtc
341 374
342 #endif // WEBRTC_BASE_NETWORK_H_ 375 #endif // WEBRTC_BASE_NETWORK_H_
OLDNEW
« no previous file with comments | « webrtc/base/ipaddress.cc ('k') | webrtc/base/network.cc » ('j') | webrtc/base/network.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698