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_BSD) | |
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 address if a temporary address | |
agl
2014/03/27 19:11:40
s/address/addresses/ (just the first instance)
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
| |
51 // is available for same network interface. | |
52 void RemovePermanentIPv6AddressesWhereTemporaryExists( | |
53 std::vector<NetworkInterfaceInfo>* infos) { | |
54 if (!infos || infos->empty()) | |
55 return; | |
56 | |
57 // Pushing all IPv6 temp addresses interface name to a different list. | |
agl
2014/03/27 19:11:40
// Build a set containing the names of interfaces
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
| |
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 the temp ipv6 list is empty, no work to be done further. | |
agl
2014/03/27 19:11:40
// The there are no such interfaces then there's n
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
| |
67 if (ifaces_with_temp_addrs.empty()) | |
68 return; | |
69 | |
70 // Search for permenent address belonging to same network interface. | |
agl
2014/03/27 19:11:40
s/address/addresses.
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
| |
71 for (i = infos->begin(); i != infos->end(); ++i) { | |
72 // IPv4 can have same name and it will be permanent. | |
73 if (i->interface.address.size() == kIPv4AddressSize) | |
74 continue; | |
75 DCHECK_EQ(i->interface.address.size(), kIPv6AddressSize); | |
76 if (i->permanent && ifaces_with_temp_addrs.find(i->interface.name) != | |
77 ifaces_with_temp_addrs.end()) { | |
78 i = infos->erase(i); | |
79 } | |
80 } | |
81 } | |
82 | |
83 #endif | |
84 | |
85 } // namespace | |
86 | |
32 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { | 87 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { |
33 *path = base::FilePath(); | 88 *path = base::FilePath(); |
34 std::string& file_path_str = const_cast<std::string&>(path->value()); | 89 std::string& file_path_str = const_cast<std::string&>(path->value()); |
35 file_path_str.clear(); | 90 file_path_str.clear(); |
36 | 91 |
37 if (!url.is_valid()) | 92 if (!url.is_valid()) |
38 return false; | 93 return false; |
39 | 94 |
40 // Firefox seems to ignore the "host" of a file url if there is one. That is, | 95 // 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. | 96 // file://foo/bar.txt maps to /bar.txt. |
(...skipping 14 matching lines...) Expand all Loading... | |
56 new_path = old_path; | 111 new_path = old_path; |
57 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); | 112 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); |
58 old_path.swap(new_path); | 113 old_path.swap(new_path); |
59 } while (new_path != old_path); | 114 } while (new_path != old_path); |
60 | 115 |
61 file_path_str.assign(old_path); | 116 file_path_str.assign(old_path); |
62 | 117 |
63 return !file_path_str.empty(); | 118 return !file_path_str.empty(); |
64 } | 119 } |
65 | 120 |
66 bool GetNetworkList(NetworkInterfaceList* networks, | 121 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
67 HostScopeVirtualInterfacePolicy policy) { | |
68 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
69 std::string network_list = android::GetNetworkList(); | 123 std::string network_list = android::GetNetworkList(); |
70 base::StringTokenizer network_interfaces(network_list, "\n"); | 124 base::StringTokenizer network_interfaces(network_list, "\n"); |
71 while (network_interfaces.GetNext()) { | 125 while (network_interfaces.GetNext()) { |
72 std::string network_item = network_interfaces.token(); | 126 std::string network_item = network_interfaces.token(); |
73 base::StringTokenizer network_tokenizer(network_item, "\t"); | 127 base::StringTokenizer network_tokenizer(network_item, "\t"); |
74 CHECK(network_tokenizer.GetNext()); | 128 CHECK(network_tokenizer.GetNext()); |
75 std::string name = network_tokenizer.token(); | 129 std::string name = network_tokenizer.token(); |
76 | 130 |
77 CHECK(network_tokenizer.GetNext()); | 131 CHECK(network_tokenizer.GetNext()); |
78 std::string interface_address = network_tokenizer.token(); | 132 std::string interface_address = network_tokenizer.token(); |
79 IPAddressNumber address; | 133 IPAddressNumber address; |
80 size_t network_prefix = 0; | 134 size_t network_prefix = 0; |
81 CHECK(ParseCIDRBlock(network_tokenizer.token(), | 135 CHECK(ParseCIDRBlock(network_tokenizer.token(), |
82 &address, | 136 &address, |
83 &network_prefix)); | 137 &network_prefix)); |
84 | 138 |
85 CHECK(network_tokenizer.GetNext()); | 139 CHECK(network_tokenizer.GetNext()); |
86 uint32 index = 0; | 140 uint32 index = 0; |
87 CHECK(base::StringToUint(network_tokenizer.token(), &index)); | 141 CHECK(base::StringToUint(network_tokenizer.token(), &index)); |
88 | 142 |
89 networks->push_back( | 143 networks->push_back( |
90 NetworkInterface(name, name, index, address, network_prefix)); | 144 NetworkInterface(name, name, index, address, network_prefix)); |
91 } | 145 } |
92 return true; | 146 return true; |
93 #else | 147 #else |
94 // getifaddrs() may require IO operations. | 148 // getifaddrs() may require IO operations. |
95 base::ThreadRestrictions::AssertIOAllowed(); | 149 base::ThreadRestrictions::AssertIOAllowed(); |
96 | 150 |
151 int ioctl_socket = -1; | |
152 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) { | |
153 // we need a socket to query information about temporary address. | |
154 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0); | |
155 DCHECK_GT(ioctl_socket, 0); | |
156 } | |
157 | |
97 ifaddrs *interfaces; | 158 ifaddrs *interfaces; |
98 if (getifaddrs(&interfaces) < 0) { | 159 if (getifaddrs(&interfaces) < 0) { |
99 PLOG(ERROR) << "getifaddrs"; | 160 PLOG(ERROR) << "getifaddrs"; |
100 return false; | 161 return false; |
101 } | 162 } |
102 | 163 |
164 std::vector<NetworkInterfaceInfo> network_infos; | |
165 | |
103 // Enumerate the addresses assigned to network interfaces which are up. | 166 // Enumerate the addresses assigned to network interfaces which are up. |
104 for (ifaddrs *interface = interfaces; | 167 for (ifaddrs *interface = interfaces; |
105 interface != NULL; | 168 interface != NULL; |
106 interface = interface->ifa_next) { | 169 interface = interface->ifa_next) { |
107 // Skip loopback interfaces, and ones which are down. | 170 // Skip loopback interfaces, and ones which are down. |
108 if (!(IFF_UP & interface->ifa_flags)) | 171 if (!(IFF_UP & interface->ifa_flags)) |
109 continue; | 172 continue; |
110 if (IFF_LOOPBACK & interface->ifa_flags) | 173 if (IFF_LOOPBACK & interface->ifa_flags) |
111 continue; | 174 continue; |
112 // Skip interfaces with no address configured. | 175 // Skip interfaces with no address configured. |
(...skipping 21 matching lines...) Expand all Loading... | |
134 addr_in->sin_addr.s_addr == 0) { | 197 addr_in->sin_addr.s_addr == 0) { |
135 continue; | 198 continue; |
136 } | 199 } |
137 } else { | 200 } else { |
138 // Skip non-IP addresses. | 201 // Skip non-IP addresses. |
139 continue; | 202 continue; |
140 } | 203 } |
141 | 204 |
142 const std::string& name = interface->ifa_name; | 205 const std::string& name = interface->ifa_name; |
143 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. | 206 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. |
144 if (policy == EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && | 207 if (policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && |
agl
2014/03/27 19:11:40
( ) around "policy & EXCLUDE_HOST_SCOPE_VIRTUAL_IN
agl
2014/03/27 19:11:40
This will never run because EXCLUDE_HOST_SCOPE_VIR
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
oops. Yea. Changed the value in .h to non-zero.
On
| |
145 ((name.find("vmnet") != std::string::npos) || | 208 ((name.find("vmnet") != std::string::npos) || |
146 (name.find("vnic") != std::string::npos))) { | 209 (name.find("vnic") != std::string::npos))) { |
147 continue; | 210 continue; |
148 } | 211 } |
212 | |
213 NetworkInterfaceInfo network_info; | |
214 #if defined(OS_MACOSX) || defined(OS_BSD) | |
215 // Check if this is a temporary address. Currently this is only supported | |
216 // on Mac. | |
217 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE && | |
agl
2014/03/27 19:11:40
( ) around "policy & INCLUDE_ONLY_TEMP_IPV6_ADDRES
Mallinath (Gone from Chromium)
2014/03/27 19:54:18
Done.
| |
218 ioctl_socket >= 0 && addr->sa_family == AF_INET6) { | |
219 struct in6_ifreq ifr = {}; | |
220 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1); | |
221 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr, | |
222 interface->ifa_addr->sa_len); | |
223 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); | |
224 if (rv >= 0) { | |
225 network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY); | |
226 } | |
227 } | |
228 #endif | |
229 | |
149 IPEndPoint address; | 230 IPEndPoint address; |
150 if (address.FromSockAddr(addr, addr_size)) { | 231 if (address.FromSockAddr(addr, addr_size)) { |
151 uint8 net_mask = 0; | 232 uint8 net_mask = 0; |
152 if (interface->ifa_netmask) { | 233 if (interface->ifa_netmask) { |
153 // If not otherwise set, assume the same sa_family as ifa_addr. | 234 // If not otherwise set, assume the same sa_family as ifa_addr. |
154 if (interface->ifa_netmask->sa_family == 0) { | 235 if (interface->ifa_netmask->sa_family == 0) { |
155 interface->ifa_netmask->sa_family = addr->sa_family; | 236 interface->ifa_netmask->sa_family = addr->sa_family; |
156 } | 237 } |
157 IPEndPoint netmask; | 238 IPEndPoint netmask; |
158 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { | 239 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { |
159 net_mask = MaskPrefixLength(netmask.address()); | 240 net_mask = MaskPrefixLength(netmask.address()); |
160 } | 241 } |
161 } | 242 } |
243 network_info.interface = NetworkInterface( | |
244 name, name, if_nametoindex(name.c_str()), | |
245 address.address(), net_mask); | |
162 | 246 |
163 networks->push_back( | 247 network_infos.push_back(NetworkInterfaceInfo(network_info)); |
164 NetworkInterface(name, name, if_nametoindex(name.c_str()), | |
165 address.address(), net_mask)); | |
166 } | 248 } |
167 } | 249 } |
250 freeifaddrs(interfaces); | |
251 if (ioctl_socket >= 0) { | |
252 close(ioctl_socket); | |
253 } | |
168 | 254 |
169 freeifaddrs(interfaces); | 255 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) { |
256 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos); | |
257 } | |
170 | 258 |
259 for (size_t i = 0; i < network_infos.size(); ++i) { | |
260 networks->push_back(network_infos[i].interface); | |
261 } | |
171 return true; | 262 return true; |
172 #endif | 263 #endif |
173 } | 264 } |
174 | 265 |
175 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 266 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
176 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 267 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
177 } | 268 } |
178 | 269 |
179 } // namespace net | 270 } // namespace net |
OLD | NEW |