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 <sys/types.h> | 7 #include <sys/types.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_tokenizer.h" | 12 #include "base/strings/string_tokenizer.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 #if !defined(OS_ANDROID) | 20 #if !defined(OS_ANDROID) |
21 #include <ifaddrs.h> | 21 #include <ifaddrs.h> |
22 #endif | 22 #endif |
23 #include <net/if.h> | 23 #include <net/if.h> |
24 #include <netinet/in.h> | 24 #include <netinet/in.h> |
25 | 25 |
26 #if defined(OS_MACOSX) || defined(OS_BSD) | |
27 #include <netinet/in_var.h> | |
28 #include <sys/ioctl.h> | |
29 #endif | |
30 | |
26 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
27 #include "net/android/network_library.h" | 32 #include "net/android/network_library.h" |
28 #endif | 33 #endif |
29 | 34 |
30 namespace net { | 35 namespace net { |
31 | 36 |
37 namespace { | |
38 | |
39 #if !defined(OS_ANDROID) | |
40 | |
41 struct NET_EXPORT NetworkInterfaceInfo { | |
42 NetworkInterfaceInfo() : permanent(true) {} | |
43 | |
44 bool permanent; // IPv6 has notion of temporary address. If the address is | |
45 // IPv6 and it's temporary this field will be set to false. | |
agl
2014/03/27 14:52:53
remove "set to"
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
46 NetworkInterface interface; | |
47 }; | |
48 | |
49 // This method will remove permanent IPv6 address, if a temporary address | |
agl
2014/03/27 14:52:53
remove comma
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
50 // is available for same network interface. | |
51 void PruneIPv6Addresses(std::vector<NetworkInterfaceInfo>* infos) { | |
agl
2014/03/27 14:52:53
I would go nuts with the name in order to make thi
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
I Agree.
On 2014/03/27 14:52:53, agl wrote:
| |
52 // First find a temporary address. If available find permanent address | |
53 // belonging to same interface. If available remove it. | |
54 | |
55 // Pushing all IPv6 temp addresses to a different list. | |
56 std::vector<NetworkInterfaceInfo> temp_ipv6_list; | |
57 std::vector<NetworkInterfaceInfo>::iterator iter = infos->begin(); | |
agl
2014/03/27 14:52:53
If you just just |i| for iter then you can use |j|
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
58 for (; iter != infos->end(); ++iter) { | |
agl
2014/03/27 14:52:53
do the assignment of iter in the first for clause.
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
59 if (iter->permanent && iter->interface.address.size() == kIPv6AddressSize) { | |
agl
2014/03/27 14:52:53
This is pushing permanent addresses into a list ca
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Yea, I discovered that later.
On 2014/03/27 14:52:
| |
60 temp_ipv6_list.push_back(*iter); | |
61 iter = infos->erase(iter); | |
62 } | |
63 } | |
64 | |
65 // Search for permenent address belonging to same network interface. | |
66 iter = temp_ipv6_list.begin(); | |
agl
2014/03/27 14:52:53
move into the for loop.
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
67 for (; iter != temp_ipv6_list.end(); ++iter) { | |
68 std::string name = iter->interface.name; | |
agl
2014/03/27 14:52:53
const &?
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
not needed.
On 2014/03/27 14:52:53, agl wrote:
| |
69 std::vector<NetworkInterfaceInfo>::iterator piter = infos->begin(); | |
70 for (; piter != infos->end(); ++piter) { | |
71 if (piter->interface.address.size() == kIPv4AddressSize) | |
72 continue; | |
73 if (piter->interface.name != iter->interface.name) | |
agl
2014/03/27 14:52:53
RHS should be |name|?
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
removed now.
On 2014/03/27 14:52:53, agl wrote:
| |
74 continue; | |
75 DCHECK_EQ(piter->interface.address.size(), kIPv6AddressSize); | |
76 piter = infos->erase(piter); | |
77 } | |
78 } | |
agl
2014/03/27 14:52:53
Isn't it easier to do one pass building a set?:
s
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Cool, that makes life bit easy.
On 2014/03/27 14:5
| |
79 | |
80 // Push temp addresses back to original list. | |
81 iter = temp_ipv6_list.begin(); | |
82 for (; iter != temp_ipv6_list.end(); ++iter) { | |
83 infos->push_back(*iter); | |
84 } | |
85 | |
86 // TODO(mallinath) Above logic seems to be it "quirky", find better solution. | |
87 } | |
88 | |
89 #endif | |
90 | |
91 } // namespace | |
92 | |
32 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { | 93 bool FileURLToFilePath(const GURL& url, base::FilePath* path) { |
33 *path = base::FilePath(); | 94 *path = base::FilePath(); |
34 std::string& file_path_str = const_cast<std::string&>(path->value()); | 95 std::string& file_path_str = const_cast<std::string&>(path->value()); |
35 file_path_str.clear(); | 96 file_path_str.clear(); |
36 | 97 |
37 if (!url.is_valid()) | 98 if (!url.is_valid()) |
38 return false; | 99 return false; |
39 | 100 |
40 // Firefox seems to ignore the "host" of a file url if there is one. That is, | 101 // 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. | 102 // file://foo/bar.txt maps to /bar.txt. |
(...skipping 14 matching lines...) Expand all Loading... | |
56 new_path = old_path; | 117 new_path = old_path; |
57 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); | 118 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); |
58 old_path.swap(new_path); | 119 old_path.swap(new_path); |
59 } while (new_path != old_path); | 120 } while (new_path != old_path); |
60 | 121 |
61 file_path_str.assign(old_path); | 122 file_path_str.assign(old_path); |
62 | 123 |
63 return !file_path_str.empty(); | 124 return !file_path_str.empty(); |
64 } | 125 } |
65 | 126 |
66 bool GetNetworkList(NetworkInterfaceList* networks, | 127 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
67 HostScopeVirtualInterfacePolicy policy) { | |
68 #if defined(OS_ANDROID) | 128 #if defined(OS_ANDROID) |
69 std::string network_list = android::GetNetworkList(); | 129 std::string network_list = android::GetNetworkList(); |
70 base::StringTokenizer network_interfaces(network_list, "\n"); | 130 base::StringTokenizer network_interfaces(network_list, "\n"); |
71 while (network_interfaces.GetNext()) { | 131 while (network_interfaces.GetNext()) { |
72 std::string network_item = network_interfaces.token(); | 132 std::string network_item = network_interfaces.token(); |
73 base::StringTokenizer network_tokenizer(network_item, "\t"); | 133 base::StringTokenizer network_tokenizer(network_item, "\t"); |
74 CHECK(network_tokenizer.GetNext()); | 134 CHECK(network_tokenizer.GetNext()); |
75 std::string name = network_tokenizer.token(); | 135 std::string name = network_tokenizer.token(); |
76 | 136 |
77 CHECK(network_tokenizer.GetNext()); | 137 CHECK(network_tokenizer.GetNext()); |
78 std::string interface_address = network_tokenizer.token(); | 138 std::string interface_address = network_tokenizer.token(); |
79 IPAddressNumber address; | 139 IPAddressNumber address; |
80 size_t network_prefix = 0; | 140 size_t network_prefix = 0; |
81 CHECK(ParseCIDRBlock(network_tokenizer.token(), | 141 CHECK(ParseCIDRBlock(network_tokenizer.token(), |
82 &address, | 142 &address, |
83 &network_prefix)); | 143 &network_prefix)); |
84 | 144 |
85 CHECK(network_tokenizer.GetNext()); | 145 CHECK(network_tokenizer.GetNext()); |
86 uint32 index = 0; | 146 uint32 index = 0; |
87 CHECK(base::StringToUint(network_tokenizer.token(), &index)); | 147 CHECK(base::StringToUint(network_tokenizer.token(), &index)); |
88 | 148 |
89 networks->push_back( | 149 networks->push_back( |
90 NetworkInterface(name, name, index, address, network_prefix)); | 150 NetworkInterface(name, name, index, address, network_prefix)); |
91 } | 151 } |
92 return true; | 152 return true; |
93 #else | 153 #else |
94 // getifaddrs() may require IO operations. | 154 // getifaddrs() may require IO operations. |
95 base::ThreadRestrictions::AssertIOAllowed(); | 155 base::ThreadRestrictions::AssertIOAllowed(); |
96 | 156 |
157 int ioctl_socket = -1; | |
158 if (policy & INCLUDE_IPV6_TEMP_ADDRESS_IF_AVAILABLE) { | |
159 // we need a socket to query information about temporary address. | |
160 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0); | |
161 DCHECK_GT(ioctl_socket, 0); | |
162 } | |
163 | |
97 ifaddrs *interfaces; | 164 ifaddrs *interfaces; |
98 if (getifaddrs(&interfaces) < 0) { | 165 if (getifaddrs(&interfaces) < 0) { |
99 PLOG(ERROR) << "getifaddrs"; | 166 PLOG(ERROR) << "getifaddrs"; |
100 return false; | 167 return false; |
101 } | 168 } |
102 | 169 |
170 std::vector<NetworkInterfaceInfo> network_infos; | |
171 | |
103 // Enumerate the addresses assigned to network interfaces which are up. | 172 // Enumerate the addresses assigned to network interfaces which are up. |
104 for (ifaddrs *interface = interfaces; | 173 for (ifaddrs *interface = interfaces; |
105 interface != NULL; | 174 interface != NULL; |
106 interface = interface->ifa_next) { | 175 interface = interface->ifa_next) { |
107 // Skip loopback interfaces, and ones which are down. | 176 // Skip loopback interfaces, and ones which are down. |
108 if (!(IFF_UP & interface->ifa_flags)) | 177 if (!(IFF_UP & interface->ifa_flags)) |
109 continue; | 178 continue; |
110 if (IFF_LOOPBACK & interface->ifa_flags) | 179 if (IFF_LOOPBACK & interface->ifa_flags) |
111 continue; | 180 continue; |
112 // Skip interfaces with no address configured. | 181 // Skip interfaces with no address configured. |
(...skipping 21 matching lines...) Expand all Loading... | |
134 addr_in->sin_addr.s_addr == 0) { | 203 addr_in->sin_addr.s_addr == 0) { |
135 continue; | 204 continue; |
136 } | 205 } |
137 } else { | 206 } else { |
138 // Skip non-IP addresses. | 207 // Skip non-IP addresses. |
139 continue; | 208 continue; |
140 } | 209 } |
141 | 210 |
142 const std::string& name = interface->ifa_name; | 211 const std::string& name = interface->ifa_name; |
143 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. | 212 // Filter out VMware interfaces, typically named vmnet1 and vmnet8. |
144 if (policy == EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && | 213 if (policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && |
145 ((name.find("vmnet") != std::string::npos) || | 214 ((name.find("vmnet") != std::string::npos) || |
146 (name.find("vnic") != std::string::npos))) { | 215 (name.find("vnic") != std::string::npos))) { |
147 continue; | 216 continue; |
148 } | 217 } |
218 | |
219 NetworkInterfaceInfo network_info; | |
220 #if defined(OS_MACOSX) || defined(OS_BSD) | |
221 // Check if this is a temporary address. Currently this is only supported | |
222 // on Mac. | |
223 if (policy & INCLUDE_IPV6_TEMP_ADDRESS_IF_AVAILABLE && | |
224 ioctl_socket > 0 && addr->sa_family == AF_INET6) { | |
agl
2014/03/27 14:52:53
ioctl_socket >= 0
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
225 struct in6_ifreq ifr = {}; | |
226 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1); | |
227 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr, | |
228 interface->ifa_addr->sa_len); | |
229 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); | |
230 if (rv >= 0) { | |
231 network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY); | |
232 } | |
233 } | |
234 #endif | |
235 | |
149 IPEndPoint address; | 236 IPEndPoint address; |
150 if (address.FromSockAddr(addr, addr_size)) { | 237 if (address.FromSockAddr(addr, addr_size)) { |
151 uint8 net_mask = 0; | 238 uint8 net_mask = 0; |
152 if (interface->ifa_netmask) { | 239 if (interface->ifa_netmask) { |
153 // If not otherwise set, assume the same sa_family as ifa_addr. | 240 // If not otherwise set, assume the same sa_family as ifa_addr. |
154 if (interface->ifa_netmask->sa_family == 0) { | 241 if (interface->ifa_netmask->sa_family == 0) { |
155 interface->ifa_netmask->sa_family = addr->sa_family; | 242 interface->ifa_netmask->sa_family = addr->sa_family; |
156 } | 243 } |
157 IPEndPoint netmask; | 244 IPEndPoint netmask; |
158 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { | 245 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { |
159 net_mask = MaskPrefixLength(netmask.address()); | 246 net_mask = MaskPrefixLength(netmask.address()); |
160 } | 247 } |
161 } | 248 } |
249 network_info.interface = NetworkInterface( | |
250 name, name, if_nametoindex(name.c_str()), | |
251 address.address(), net_mask); | |
162 | 252 |
163 networks->push_back( | 253 network_infos.push_back(network_info); |
164 NetworkInterface(name, name, if_nametoindex(name.c_str()), | |
165 address.address(), net_mask)); | |
166 } | 254 } |
167 } | 255 } |
256 freeifaddrs(interfaces); | |
257 close(ioctl_socket); | |
agl
2014/03/27 14:52:53
guard this by an if - we shouldn't call close(-1)
Mallinath (Gone from Chromium)
2014/03/27 19:04:18
Done.
| |
168 | 258 |
169 freeifaddrs(interfaces); | 259 if (policy & INCLUDE_IPV6_TEMP_ADDRESS_IF_AVAILABLE) { |
260 PruneIPv6Addresses(&network_infos); | |
261 } | |
170 | 262 |
263 for (size_t i = 0; i < network_infos.size(); ++i) { | |
264 networks->push_back(network_infos[i].interface); | |
265 } | |
171 return true; | 266 return true; |
172 #endif | 267 #endif |
173 } | 268 } |
174 | 269 |
175 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 270 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
176 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 271 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
177 } | 272 } |
178 | 273 |
179 } // namespace net | 274 } // namespace net |
OLD | NEW |