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

Side by Side Diff: net/base/net_util_posix.cc

Issue 670183003: Update from chromium 62675d9fb31fb8cedc40f68e78e8445a74f362e7 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 2 months 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
« no previous file with comments | « net/base/net_util_posix.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <set> 7 #include <set>
8 #include <sys/types.h> 8 #include <sys/types.h>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_tokenizer.h" 14 #include "base/strings/string_tokenizer.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
17 #include "net/base/escape.h" 17 #include "net/base/escape.h"
18 #include "net/base/ip_endpoint.h" 18 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 #if !defined(OS_ANDROID) && !defined(OS_NACL) 22 #if !defined(OS_NACL)
23 #if defined(OS_MACOSX)
23 #include <ifaddrs.h> 24 #include <ifaddrs.h>
25 #else
26 #include "net/base/address_tracker_linux.h"
27 #include "net/base/net_util_posix.h"
28 #endif // OS_MACOSX
24 #include <net/if.h> 29 #include <net/if.h>
25 #include <netinet/in.h> 30 #include <netinet/in.h>
26 #endif 31 #endif // !defined(OS_NACL)
27 32
28 #if defined(OS_MACOSX) && !defined(OS_IOS) 33 #if defined(OS_MACOSX) && !defined(OS_IOS)
29 #include <net/if_media.h> 34 #include <net/if_media.h>
30 #include <netinet/in_var.h> 35 #include <netinet/in_var.h>
31 #include <sys/ioctl.h> 36 #include <sys/ioctl.h>
32 #endif 37 #endif
33 38
34 #if defined(OS_ANDROID)
35 #include "net/android/network_library.h"
36 #endif
37
38 namespace net { 39 namespace net {
39 40
40 namespace { 41 namespace {
41 42
42 #if !defined(OS_ANDROID) 43 // The application layer can pass |policy| defined in net_util.h to
44 // request filtering out certain type of interfaces.
45 bool ShouldIgnoreInterface(const std::string& name, int policy) {
46 // Filter out VMware interfaces, typically named vmnet1 and vmnet8,
47 // which might not be useful for use cases like WebRTC.
48 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) &&
49 ((name.find("vmnet") != std::string::npos) ||
50 (name.find("vnic") != std::string::npos))) {
51 return true;
52 }
53
54 return false;
55 }
56
57 // Check if the address is unspecified (i.e. made of zeroes) or loopback.
58 bool IsLoopbackOrUnspecifiedAddress(const sockaddr* addr) {
59 if (addr->sa_family == AF_INET6) {
60 const struct sockaddr_in6* addr_in6 =
61 reinterpret_cast<const struct sockaddr_in6*>(addr);
62 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
63 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
64 return true;
65 }
66 } else if (addr->sa_family == AF_INET) {
67 const struct sockaddr_in* addr_in =
68 reinterpret_cast<const struct sockaddr_in*>(addr);
69 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
70 addr_in->sin_addr.s_addr == 0) {
71 return true;
72 }
73 } else {
74 // Skip non-IP addresses.
75 return true;
76 }
77 return false;
78 }
79
80 #if defined(OS_MACOSX)
43 81
44 struct NetworkInterfaceInfo { 82 struct NetworkInterfaceInfo {
45 NetworkInterfaceInfo() : permanent(true) { } 83 NetworkInterfaceInfo() : permanent(true) { }
46 84
47 bool permanent; // IPv6 has notion of temporary address. If the address is 85 bool permanent; // IPv6 has notion of temporary address. If the address is
48 // IPv6 and it's temporary this field will be false. 86 // IPv6 and it's temporary this field will be false.
49 NetworkInterface interface; 87 NetworkInterface interface;
50 }; 88 };
51 89
52 // This method will remove permanent IPv6 addresses if a temporary address 90 // This method will remove permanent IPv6 addresses if a temporary address
(...skipping 23 matching lines...) Expand all
76 if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent && 114 if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent &&
77 (ifaces_with_temp_addrs.find(i->interface.name) != 115 (ifaces_with_temp_addrs.find(i->interface.name) !=
78 ifaces_with_temp_addrs.end())) { 116 ifaces_with_temp_addrs.end())) {
79 i = infos->erase(i); 117 i = infos->erase(i);
80 } else { 118 } else {
81 ++i; 119 ++i;
82 } 120 }
83 } 121 }
84 } 122 }
85 123
86 #endif 124 #if !defined(OS_IOS)
87
88 #if defined(OS_MACOSX) && !defined(OS_IOS)
89
90 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType( 125 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
91 int addr_family, const std::string& interface_name) { 126 int addr_family, const std::string& interface_name) {
92 NetworkChangeNotifier::ConnectionType type = 127 NetworkChangeNotifier::ConnectionType type =
93 NetworkChangeNotifier::CONNECTION_UNKNOWN; 128 NetworkChangeNotifier::CONNECTION_UNKNOWN;
94 129
95 struct ifmediareq ifmr = {}; 130 struct ifmediareq ifmr = {};
96 strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1); 131 strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1);
97 132
98 int s = socket(addr_family, SOCK_DGRAM, 0); 133 int s = socket(addr_family, SOCK_DGRAM, 0);
99 if (s == -1) { 134 if (s == -1) {
100 return type; 135 return type;
101 } 136 }
102 137
103 if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) { 138 if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) {
104 if (ifmr.ifm_current & IFM_IEEE80211) { 139 if (ifmr.ifm_current & IFM_IEEE80211) {
105 type = NetworkChangeNotifier::CONNECTION_WIFI; 140 type = NetworkChangeNotifier::CONNECTION_WIFI;
106 } else if (ifmr.ifm_current & IFM_ETHER) { 141 } else if (ifmr.ifm_current & IFM_ETHER) {
107 type = NetworkChangeNotifier::CONNECTION_ETHERNET; 142 type = NetworkChangeNotifier::CONNECTION_ETHERNET;
108 } 143 }
109 } 144 }
110 close(s); 145 close(s);
111 return type; 146 return type;
112 } 147 }
113 148
149 #endif // !defined(OS_IOS)
150 #elif !defined(OS_NACL) // OS_MACOSX
151
152 // Convert platform native IPv6 address attributes to net IP address
153 // attributes and drop ones that can't be used by the application
154 // layer.
155 bool TryConvertNativeToNetIPAttributes(int native_attributes,
156 int* net_attributes) {
157 // For Linux/ChromeOS/Android, we disallow addresses with attributes
158 // IFA_F_OPTIMISTIC, IFA_F_DADFAILED, and IFA_F_TENTATIVE as these
159 // are still progressing through duplicated address detection (DAD)
160 // and shouldn't be used by the application layer until DAD process
161 // is completed.
162 if (native_attributes & (
163 #if !defined(OS_ANDROID)
164 IFA_F_OPTIMISTIC | IFA_F_DADFAILED |
165 #endif // !OS_ANDROID
166 IFA_F_TENTATIVE)) {
167 return false;
168 }
169
170 if (native_attributes & IFA_F_TEMPORARY) {
171 *net_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY;
172 }
173
174 if (native_attributes & IFA_F_DEPRECATED) {
175 *net_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED;
176 }
177
178 return true;
179 }
180 #endif // OS_MACOSX
181 } // namespace
182
183 namespace internal {
184
185 #if !defined(OS_MACOSX) && !defined(OS_NACL)
186
187 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) {
188 #if defined(OS_ANDROID)
189 return ip.begin();
190 #else
191 return ip.data();
192 #endif
193 }
194
195 bool GetNetworkListImpl(
196 NetworkInterfaceList* networks,
197 int policy,
198 const base::hash_set<int>& online_links,
199 const internal::AddressTrackerLinux::AddressMap& address_map,
200 GetInterfaceNameFunction get_interface_name) {
201 std::map<int, std::string> ifnames;
202
203 for (internal::AddressTrackerLinux::AddressMap::const_iterator it =
204 address_map.begin();
205 it != address_map.end();
206 ++it) {
207 // Ignore addresses whose links are not online.
208 if (online_links.find(it->second.ifa_index) == online_links.end())
209 continue;
210
211 sockaddr_storage sock_addr;
212 socklen_t sock_len = sizeof(sockaddr_storage);
213
214 // Convert to sockaddr for next check.
215 if (!IPEndPoint(it->first, 0)
216 .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addr), &sock_len)) {
217 continue;
218 }
219
220 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
221 if (IsLoopbackOrUnspecifiedAddress(reinterpret_cast<sockaddr*>(&sock_addr)))
222 continue;
223
224 int ip_attributes = IP_ADDRESS_ATTRIBUTE_NONE;
225
226 if (it->second.ifa_family == AF_INET6) {
227 // Ignore addresses whose attributes are not actionable by
228 // the application layer.
229 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags,
230 &ip_attributes))
231 continue;
232 }
233
234 // Find the name of this link.
235 std::map<int, std::string>::const_iterator itname =
236 ifnames.find(it->second.ifa_index);
237 std::string ifname;
238 if (itname == ifnames.end()) {
239 char buffer[IF_NAMESIZE] = {0};
240 if (get_interface_name(it->second.ifa_index, buffer)) {
241 ifname = ifnames[it->second.ifa_index] = buffer;
242 } else {
243 // Ignore addresses whose interface name can't be retrieved.
244 continue;
245 }
246 } else {
247 ifname = itname->second;
248 }
249
250 // Based on the interface name and policy, determine whether we
251 // should ignore it.
252 if (ShouldIgnoreInterface(ifname, policy))
253 continue;
254
255 networks->push_back(
256 NetworkInterface(ifname,
257 ifname,
258 it->second.ifa_index,
259 NetworkChangeNotifier::CONNECTION_UNKNOWN,
260 it->first,
261 it->second.ifa_prefixlen,
262 ip_attributes));
263 }
264
265 return true;
266 }
114 #endif 267 #endif
115 268
116 } // namespace 269 } // namespace internal
117 270
118 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { 271 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
272 if (networks == NULL)
273 return false;
119 #if defined(OS_NACL) 274 #if defined(OS_NACL)
120 NOTIMPLEMENTED(); 275 NOTIMPLEMENTED();
121 return false; 276 return false;
122 #elif defined(OS_ANDROID) 277 #elif !defined(OS_MACOSX)
123 std::string network_list = android::GetNetworkList();
124 base::StringTokenizer network_interfaces(network_list, "\n");
125 while (network_interfaces.GetNext()) {
126 std::string network_item = network_interfaces.token();
127 base::StringTokenizer network_tokenizer(network_item, "\t");
128 CHECK(network_tokenizer.GetNext());
129 std::string name = network_tokenizer.token();
130 278
131 CHECK(network_tokenizer.GetNext()); 279 internal::AddressTrackerLinux tracker;
132 std::string interface_address = network_tokenizer.token(); 280 tracker.Init();
133 IPAddressNumber address;
134 size_t network_prefix = 0;
135 CHECK(ParseCIDRBlock(network_tokenizer.token(),
136 &address,
137 &network_prefix));
138 281
139 CHECK(network_tokenizer.GetNext()); 282 return internal::GetNetworkListImpl(networks,
140 uint32 index = 0; 283 policy,
141 CHECK(base::StringToUint(network_tokenizer.token(), &index)); 284 tracker.GetOnlineLinks(),
285 tracker.GetAddressMap(),
286 &if_indextoname);
142 287
143 networks->push_back( 288 #else // Only OS_MACOSX and OS_IOS will run the code below
144 NetworkInterface(name, 289
145 name,
146 index,
147 NetworkChangeNotifier::CONNECTION_UNKNOWN,
148 address,
149 network_prefix,
150 IP_ADDRESS_ATTRIBUTE_NONE));
151 }
152 return true;
153 #else
154 // getifaddrs() may require IO operations. 290 // getifaddrs() may require IO operations.
155 base::ThreadRestrictions::AssertIOAllowed(); 291 base::ThreadRestrictions::AssertIOAllowed();
156 292
157 #if defined(OS_MACOSX) && !defined(OS_IOS) 293 #if !defined(OS_IOS)
158 int ioctl_socket = -1; 294 int ioctl_socket = -1;
159 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) { 295 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
160 // we need a socket to query information about temporary address. 296 // we need a socket to query information about temporary address.
161 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0); 297 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0);
162 DCHECK_GT(ioctl_socket, 0); 298 DCHECK_GT(ioctl_socket, 0);
163 } 299 }
164 #endif 300 #endif
165 301
166 ifaddrs *interfaces; 302 ifaddrs* interfaces;
167 if (getifaddrs(&interfaces) < 0) { 303 if (getifaddrs(&interfaces) < 0) {
168 PLOG(ERROR) << "getifaddrs"; 304 PLOG(ERROR) << "getifaddrs";
169 return false; 305 return false;
170 } 306 }
171 307
172 std::vector<NetworkInterfaceInfo> network_infos; 308 std::vector<NetworkInterfaceInfo> network_infos;
173 309
174 // Enumerate the addresses assigned to network interfaces which are up. 310 // Enumerate the addresses assigned to network interfaces which are up.
175 for (ifaddrs *interface = interfaces; 311 for (ifaddrs *interface = interfaces;
176 interface != NULL; 312 interface != NULL;
177 interface = interface->ifa_next) { 313 interface = interface->ifa_next) {
178 // Skip loopback interfaces, and ones which are down. 314 // Skip loopback interfaces, and ones which are down.
179 if (!(IFF_UP & interface->ifa_flags)) 315 if (!(IFF_UP & interface->ifa_flags))
180 continue; 316 continue;
181 if (IFF_LOOPBACK & interface->ifa_flags) 317 if (IFF_LOOPBACK & interface->ifa_flags)
182 continue; 318 continue;
183 // Skip interfaces with no address configured. 319 // Skip interfaces with no address configured.
184 struct sockaddr* addr = interface->ifa_addr; 320 struct sockaddr* addr = interface->ifa_addr;
185 if (!addr) 321 if (!addr)
186 continue; 322 continue;
187 323
188 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses 324 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
189 // configured on non-loopback interfaces. 325 // configured on non-loopback interfaces.
326 if (IsLoopbackOrUnspecifiedAddress(addr))
327 continue;
328
190 int addr_size = 0; 329 int addr_size = 0;
191 if (addr->sa_family == AF_INET6) { 330 if (addr->sa_family == AF_INET6) {
192 struct sockaddr_in6* addr_in6 = 331 addr_size = sizeof(sockaddr_in6);
193 reinterpret_cast<struct sockaddr_in6*>(addr);
194 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
195 addr_size = sizeof(*addr_in6);
196 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) ||
197 IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
198 continue;
199 }
200 } else if (addr->sa_family == AF_INET) { 332 } else if (addr->sa_family == AF_INET) {
201 struct sockaddr_in* addr_in = 333 addr_size = sizeof(sockaddr_in);
202 reinterpret_cast<struct sockaddr_in*>(addr);
203 addr_size = sizeof(*addr_in);
204 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
205 addr_in->sin_addr.s_addr == 0) {
206 continue;
207 }
208 } else {
209 // Skip non-IP addresses.
210 continue;
211 } 334 }
212 335
213 const std::string& name = interface->ifa_name; 336 const std::string& name = interface->ifa_name;
214 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. 337 // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
215 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && 338 if (ShouldIgnoreInterface(name, policy)) {
216 ((name.find("vmnet") != std::string::npos) ||
217 (name.find("vnic") != std::string::npos))) {
218 continue; 339 continue;
219 } 340 }
220 341
221 NetworkInterfaceInfo network_info; 342 NetworkInterfaceInfo network_info;
222 NetworkChangeNotifier::ConnectionType connection_type = 343 NetworkChangeNotifier::ConnectionType connection_type =
223 NetworkChangeNotifier::CONNECTION_UNKNOWN; 344 NetworkChangeNotifier::CONNECTION_UNKNOWN;
224 #if defined(OS_MACOSX) && !defined(OS_IOS) 345 #if !defined(OS_IOS)
225 // Check if this is a temporary address. Currently this is only supported 346 // Check if this is a temporary address. Currently this is only supported
226 // on Mac. 347 // on Mac.
227 if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) && 348 if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) &&
228 ioctl_socket >= 0 && addr->sa_family == AF_INET6) { 349 ioctl_socket >= 0 && addr->sa_family == AF_INET6) {
229 struct in6_ifreq ifr = {}; 350 struct in6_ifreq ifr = {};
230 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1); 351 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1);
231 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr, 352 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr,
232 interface->ifa_addr->sa_len); 353 interface->ifa_addr->sa_len);
233 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); 354 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr);
234 if (rv >= 0) { 355 if (rv >= 0) {
(...skipping 22 matching lines...) Expand all
257 if_nametoindex(name.c_str()), 378 if_nametoindex(name.c_str()),
258 connection_type, 379 connection_type,
259 address.address(), 380 address.address(),
260 net_mask, 381 net_mask,
261 IP_ADDRESS_ATTRIBUTE_NONE); 382 IP_ADDRESS_ATTRIBUTE_NONE);
262 383
263 network_infos.push_back(NetworkInterfaceInfo(network_info)); 384 network_infos.push_back(NetworkInterfaceInfo(network_info));
264 } 385 }
265 } 386 }
266 freeifaddrs(interfaces); 387 freeifaddrs(interfaces);
267 #if defined(OS_MACOSX) && !defined(OS_IOS) 388 #if !defined(OS_IOS)
268 if (ioctl_socket >= 0) { 389 if (ioctl_socket >= 0) {
269 close(ioctl_socket); 390 close(ioctl_socket);
270 } 391 }
271 #endif 392 #endif
272 393
273 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) { 394 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
274 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos); 395 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos);
275 } 396 }
276 397
277 for (size_t i = 0; i < network_infos.size(); ++i) { 398 for (size_t i = 0; i < network_infos.size(); ++i) {
278 networks->push_back(network_infos[i].interface); 399 networks->push_back(network_infos[i].interface);
279 } 400 }
280 return true; 401 return true;
281 #endif 402 #endif
282 } 403 }
283 404
284 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { 405 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
285 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; 406 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
286 } 407 }
287 408
288 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { 409 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) {
289 return scoped_ptr<ScopedWifiOptions>(); 410 return scoped_ptr<ScopedWifiOptions>();
290 } 411 }
291 412
292 413
293 } // namespace net 414 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util_posix.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698