OLD | NEW |
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 <sys/types.h> | 8 #include <sys/types.h> |
8 | 9 |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_tokenizer.h" | 13 #include "base/strings/string_tokenizer.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
15 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
16 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 #if !defined(OS_ANDROID) | 21 #if !defined(OS_ANDROID) |
21 #include <ifaddrs.h> | 22 #include <ifaddrs.h> |
22 #endif | 23 #endif |
23 #include <net/if.h> | 24 #include <net/if.h> |
24 #include <netinet/in.h> | 25 #include <netinet/in.h> |
25 | 26 |
| 27 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 28 #include <netinet/in_var.h> |
| 29 #include <sys/ioctl.h> |
| 30 #endif |
| 31 |
26 #if defined(OS_ANDROID) | 32 #if defined(OS_ANDROID) |
27 #include "net/android/network_library.h" | 33 #include "net/android/network_library.h" |
28 #endif | 34 #endif |
29 | 35 |
30 namespace net { | 36 namespace net { |
31 | 37 |
| 38 namespace { |
| 39 |
| 40 #if !defined(OS_ANDROID) |
| 41 |
| 42 struct NetworkInterfaceInfo { |
| 43 NetworkInterfaceInfo() : permanent(true) { } |
| 44 |
| 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; |
| 48 }; |
| 49 |
| 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; |
| 56 |
| 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); |
| 63 } |
| 64 } |
| 65 |
| 66 // If there are no such interfaces then there's no further work. |
| 67 if (ifaces_with_temp_addrs.empty()) |
| 68 return; |
| 69 |
| 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; |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 #endif |
| 85 |
| 86 } // namespace |
| 87 |
32 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { | 88 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { |
33 *path = base::FilePath(); | 89 *path = base::FilePath(); |
34 std::string& file_path_str = const_cast<std::string&>(path->value()); | 90 std::string& file_path_str = const_cast<std::string&>(path->value()); |
35 file_path_str.clear(); | 91 file_path_str.clear(); |
36 | 92 |
37 if (!url.is_valid()) | 93 if (!url.is_valid()) |
38 return false; | 94 return false; |
39 | 95 |
40 // Firefox seems to ignore the "host" of a file url if there is one. That is, | 96 // Firefox seems to ignore the "host" of a file url if there is one. That is, |
41 // file://foo/bar.txt maps to /bar.txt. | 97 // file://foo/bar.txt maps to /bar.txt. |
(...skipping 14 matching lines...) Expand all Loading... |
56 new_path = old_path; | 112 new_path = old_path; |
57 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); | 113 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); |
58 old_path.swap(new_path); | 114 old_path.swap(new_path); |
59 } while (new_path != old_path); | 115 } while (new_path != old_path); |
60 | 116 |
61 file_path_str.assign(old_path); | 117 file_path_str.assign(old_path); |
62 | 118 |
63 return !file_path_str.empty(); | 119 return !file_path_str.empty(); |
64 } | 120 } |
65 | 121 |
66 bool GetNetworkList(NetworkInterfaceList* networks, | 122 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
67 HostScopeVirtualInterfacePolicy policy) { | |
68 #if defined(OS_ANDROID) | 123 #if defined(OS_ANDROID) |
69 std::string network_list = android::GetNetworkList(); | 124 std::string network_list = android::GetNetworkList(); |
70 base::StringTokenizer network_interfaces(network_list, "\n"); | 125 base::StringTokenizer network_interfaces(network_list, "\n"); |
71 while (network_interfaces.GetNext()) { | 126 while (network_interfaces.GetNext()) { |
72 std::string network_item = network_interfaces.token(); | 127 std::string network_item = network_interfaces.token(); |
73 base::StringTokenizer network_tokenizer(network_item, "\t"); | 128 base::StringTokenizer network_tokenizer(network_item, "\t"); |
74 CHECK(network_tokenizer.GetNext()); | 129 CHECK(network_tokenizer.GetNext()); |
75 std::string name = network_tokenizer.token(); | 130 std::string name = network_tokenizer.token(); |
76 | 131 |
77 CHECK(network_tokenizer.GetNext()); | 132 CHECK(network_tokenizer.GetNext()); |
78 std::string interface_address = network_tokenizer.token(); | 133 std::string interface_address = network_tokenizer.token(); |
79 IPAddressNumber address; | 134 IPAddressNumber address; |
80 size_t network_prefix = 0; | 135 size_t network_prefix = 0; |
81 CHECK(ParseCIDRBlock(network_tokenizer.token(), | 136 CHECK(ParseCIDRBlock(network_tokenizer.token(), |
82 &address, | 137 &address, |
83 &network_prefix)); | 138 &network_prefix)); |
84 | 139 |
85 CHECK(network_tokenizer.GetNext()); | 140 CHECK(network_tokenizer.GetNext()); |
86 uint32 index = 0; | 141 uint32 index = 0; |
87 CHECK(base::StringToUint(network_tokenizer.token(), &index)); | 142 CHECK(base::StringToUint(network_tokenizer.token(), &index)); |
88 | 143 |
89 networks->push_back( | 144 networks->push_back( |
90 NetworkInterface(name, name, index, address, network_prefix)); | 145 NetworkInterface(name, name, index, address, network_prefix)); |
91 } | 146 } |
92 return true; | 147 return true; |
93 #else | 148 #else |
94 // getifaddrs() may require IO operations. | 149 // getifaddrs() may require IO operations. |
95 base::ThreadRestrictions::AssertIOAllowed(); | 150 base::ThreadRestrictions::AssertIOAllowed(); |
96 | 151 |
| 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); |
| 157 } |
| 158 |
97 ifaddrs *interfaces; | 159 ifaddrs *interfaces; |
98 if (getifaddrs(&interfaces) < 0) { | 160 if (getifaddrs(&interfaces) < 0) { |
99 PLOG(ERROR) << "getifaddrs"; | 161 PLOG(ERROR) << "getifaddrs"; |
100 return false; | 162 return false; |
101 } | 163 } |
102 | 164 |
| 165 std::vector<NetworkInterfaceInfo> network_infos; |
| 166 |
103 // Enumerate the addresses assigned to network interfaces which are up. | 167 // Enumerate the addresses assigned to network interfaces which are up. |
104 for (ifaddrs *interface = interfaces; | 168 for (ifaddrs *interface = interfaces; |
105 interface != NULL; | 169 interface != NULL; |
106 interface = interface->ifa_next) { | 170 interface = interface->ifa_next) { |
107 // Skip loopback interfaces, and ones which are down. | 171 // Skip loopback interfaces, and ones which are down. |
108 if (!(IFF_UP & interface->ifa_flags)) | 172 if (!(IFF_UP & interface->ifa_flags)) |
109 continue; | 173 continue; |
110 if (IFF_LOOPBACK & interface->ifa_flags) | 174 if (IFF_LOOPBACK & interface->ifa_flags) |
111 continue; | 175 continue; |
112 // Skip interfaces with no address configured. | 176 // Skip interfaces with no address configured. |
(...skipping 21 matching lines...) Expand all Loading... |
134 addr_in->sin_addr.s_addr == 0) { | 198 addr_in->sin_addr.s_addr == 0) { |
135 continue; | 199 continue; |
136 } | 200 } |
137 } else { | 201 } else { |
138 // Skip non-IP addresses. | 202 // Skip non-IP addresses. |
139 continue; | 203 continue; |
140 } | 204 } |
141 | 205 |
142 const std::string& name = interface->ifa_name; | 206 const std::string& name = interface->ifa_name; |
143 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. | 207 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. |
144 if (policy == EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && | 208 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && |
145 ((name.find("vmnet") != std::string::npos) || | 209 ((name.find("vmnet") != std::string::npos) || |
146 (name.find("vnic") != std::string::npos))) { | 210 (name.find("vnic") != std::string::npos))) { |
147 continue; | 211 continue; |
148 } | 212 } |
| 213 |
| 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); |
| 227 } |
| 228 } |
| 229 #endif |
| 230 |
149 IPEndPoint address; | 231 IPEndPoint address; |
150 if (address.FromSockAddr(addr, addr_size)) { | 232 if (address.FromSockAddr(addr, addr_size)) { |
151 uint8 net_mask = 0; | 233 uint8 net_mask = 0; |
152 if (interface->ifa_netmask) { | 234 if (interface->ifa_netmask) { |
153 // If not otherwise set, assume the same sa_family as ifa_addr. | 235 // If not otherwise set, assume the same sa_family as ifa_addr. |
154 if (interface->ifa_netmask->sa_family == 0) { | 236 if (interface->ifa_netmask->sa_family == 0) { |
155 interface->ifa_netmask->sa_family = addr->sa_family; | 237 interface->ifa_netmask->sa_family = addr->sa_family; |
156 } | 238 } |
157 IPEndPoint netmask; | 239 IPEndPoint netmask; |
158 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { | 240 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { |
159 net_mask = MaskPrefixLength(netmask.address()); | 241 net_mask = MaskPrefixLength(netmask.address()); |
160 } | 242 } |
161 } | 243 } |
| 244 network_info.interface = NetworkInterface( |
| 245 name, name, if_nametoindex(name.c_str()), |
| 246 address.address(), net_mask); |
162 | 247 |
163 networks->push_back( | 248 network_infos.push_back(NetworkInterfaceInfo(network_info)); |
164 NetworkInterface(name, name, if_nametoindex(name.c_str()), | |
165 address.address(), net_mask)); | |
166 } | 249 } |
167 } | 250 } |
| 251 freeifaddrs(interfaces); |
| 252 if (ioctl_socket >= 0) { |
| 253 close(ioctl_socket); |
| 254 } |
168 | 255 |
169 freeifaddrs(interfaces); | 256 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) { |
| 257 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos); |
| 258 } |
170 | 259 |
| 260 for (size_t i = 0; i < network_infos.size(); ++i) { |
| 261 networks->push_back(network_infos[i].interface); |
| 262 } |
171 return true; | 263 return true; |
172 #endif | 264 #endif |
173 } | 265 } |
174 | 266 |
175 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 267 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
176 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 268 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
177 } | 269 } |
178 | 270 |
179 } // namespace net | 271 } // namespace net |
OLD | NEW |