Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / net / base / net_util_posix.cc
blob95b963e7a60d385452c1940dd11b269a005ec562
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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
7 #include <set>
8 #include <sys/types.h>
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "base/strings/string_util.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "net/base/escape.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "url/gurl.h"
21 #if !defined(OS_ANDROID)
22 #include <ifaddrs.h>
23 #endif
24 #include <net/if.h>
25 #include <netinet/in.h>
27 #if defined(OS_MACOSX) && !defined(OS_IOS)
28 #include <netinet/in_var.h>
29 #include <sys/ioctl.h>
30 #endif
32 #if defined(OS_ANDROID)
33 #include "net/android/network_library.h"
34 #endif
36 namespace net {
38 namespace {
40 #if !defined(OS_ANDROID)
42 struct NetworkInterfaceInfo {
43 NetworkInterfaceInfo() : permanent(true) { }
45 bool permanent; // IPv6 has notion of temporary address. If the address is
46 // IPv6 and it's temporary this field will be false.
47 NetworkInterface interface;
50 // This method will remove permanent IPv6 addresses if a temporary address
51 // is available for same network interface.
52 void RemovePermanentIPv6AddressesWhereTemporaryExists(
53 std::vector<NetworkInterfaceInfo>* infos) {
54 if (!infos || infos->empty())
55 return;
57 // Build a set containing the names of interfaces with a temp IPv6 address
58 std::set<std::string> ifaces_with_temp_addrs;
59 std::vector<NetworkInterfaceInfo>::iterator i;
60 for (i = infos->begin(); i != infos->end(); ++i) {
61 if (!i->permanent && i->interface.address.size() == kIPv6AddressSize) {
62 ifaces_with_temp_addrs.insert(i->interface.name);
66 // If there are no such interfaces then there's no further work.
67 if (ifaces_with_temp_addrs.empty())
68 return;
70 // Search for permenent addresses belonging to same network interface.
71 for (i = infos->begin(); i != infos->end(); ) {
72 // If the address is IPv6 and it's permanent and there is temporary
73 // address for it, then we can remove this address.
74 if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent &&
75 (ifaces_with_temp_addrs.find(i->interface.name) !=
76 ifaces_with_temp_addrs.end())) {
77 i = infos->erase(i);
78 } else {
79 ++i;
84 #endif
86 } // namespace
88 bool FileURLToFilePath(const GURL& url, base::FilePath* path) {
89 *path = base::FilePath();
90 std::string& file_path_str = const_cast<std::string&>(path->value());
91 file_path_str.clear();
93 if (!url.is_valid())
94 return false;
96 // Firefox seems to ignore the "host" of a file url if there is one. That is,
97 // file://foo/bar.txt maps to /bar.txt.
98 // TODO(dhg): This should probably take into account UNCs which could
99 // include a hostname other than localhost or blank
100 std::string old_path = url.path();
102 if (old_path.empty())
103 return false;
105 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
106 old_path = UnescapeURLComponent(old_path,
107 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
109 // Collapse multiple path slashes into a single path slash.
110 std::string new_path;
111 do {
112 new_path = old_path;
113 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
114 old_path.swap(new_path);
115 } while (new_path != old_path);
117 file_path_str.assign(old_path);
119 return !file_path_str.empty();
122 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
123 #if defined(OS_ANDROID)
124 std::string network_list = android::GetNetworkList();
125 base::StringTokenizer network_interfaces(network_list, "\n");
126 while (network_interfaces.GetNext()) {
127 std::string network_item = network_interfaces.token();
128 base::StringTokenizer network_tokenizer(network_item, "\t");
129 CHECK(network_tokenizer.GetNext());
130 std::string name = network_tokenizer.token();
132 CHECK(network_tokenizer.GetNext());
133 std::string interface_address = network_tokenizer.token();
134 IPAddressNumber address;
135 size_t network_prefix = 0;
136 CHECK(ParseCIDRBlock(network_tokenizer.token(),
137 &address,
138 &network_prefix));
140 CHECK(network_tokenizer.GetNext());
141 uint32 index = 0;
142 CHECK(base::StringToUint(network_tokenizer.token(), &index));
144 networks->push_back(
145 NetworkInterface(name, name, index, address, network_prefix));
147 return true;
148 #else
149 // getifaddrs() may require IO operations.
150 base::ThreadRestrictions::AssertIOAllowed();
152 int ioctl_socket = -1;
153 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
154 // we need a socket to query information about temporary address.
155 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0);
156 DCHECK_GT(ioctl_socket, 0);
159 ifaddrs *interfaces;
160 if (getifaddrs(&interfaces) < 0) {
161 PLOG(ERROR) << "getifaddrs";
162 return false;
165 std::vector<NetworkInterfaceInfo> network_infos;
167 // Enumerate the addresses assigned to network interfaces which are up.
168 for (ifaddrs *interface = interfaces;
169 interface != NULL;
170 interface = interface->ifa_next) {
171 // Skip loopback interfaces, and ones which are down.
172 if (!(IFF_UP & interface->ifa_flags))
173 continue;
174 if (IFF_LOOPBACK & interface->ifa_flags)
175 continue;
176 // Skip interfaces with no address configured.
177 struct sockaddr* addr = interface->ifa_addr;
178 if (!addr)
179 continue;
181 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
182 // configured on non-loopback interfaces.
183 int addr_size = 0;
184 if (addr->sa_family == AF_INET6) {
185 struct sockaddr_in6* addr_in6 =
186 reinterpret_cast<struct sockaddr_in6*>(addr);
187 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
188 addr_size = sizeof(*addr_in6);
189 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) ||
190 IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
191 continue;
193 } else if (addr->sa_family == AF_INET) {
194 struct sockaddr_in* addr_in =
195 reinterpret_cast<struct sockaddr_in*>(addr);
196 addr_size = sizeof(*addr_in);
197 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
198 addr_in->sin_addr.s_addr == 0) {
199 continue;
201 } else {
202 // Skip non-IP addresses.
203 continue;
206 const std::string& name = interface->ifa_name;
207 // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
208 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) &&
209 ((name.find("vmnet") != std::string::npos) ||
210 (name.find("vnic") != std::string::npos))) {
211 continue;
214 NetworkInterfaceInfo network_info;
215 #if defined(OS_MACOSX) && !defined(OS_IOS)
216 // Check if this is a temporary address. Currently this is only supported
217 // on Mac.
218 if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) &&
219 ioctl_socket >= 0 && addr->sa_family == AF_INET6) {
220 struct in6_ifreq ifr = {};
221 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1);
222 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr,
223 interface->ifa_addr->sa_len);
224 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr);
225 if (rv >= 0) {
226 network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY);
229 #endif
231 IPEndPoint address;
232 if (address.FromSockAddr(addr, addr_size)) {
233 uint8 net_mask = 0;
234 if (interface->ifa_netmask) {
235 // If not otherwise set, assume the same sa_family as ifa_addr.
236 if (interface->ifa_netmask->sa_family == 0) {
237 interface->ifa_netmask->sa_family = addr->sa_family;
239 IPEndPoint netmask;
240 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
241 net_mask = MaskPrefixLength(netmask.address());
244 network_info.interface = NetworkInterface(
245 name, name, if_nametoindex(name.c_str()),
246 address.address(), net_mask);
248 network_infos.push_back(NetworkInterfaceInfo(network_info));
251 freeifaddrs(interfaces);
252 if (ioctl_socket >= 0) {
253 close(ioctl_socket);
256 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
257 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos);
260 for (size_t i = 0; i < network_infos.size(); ++i) {
261 networks->push_back(network_infos[i].interface);
263 return true;
264 #endif
267 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
268 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
271 } // namespace net