OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/network_interfaces_win.h" | 5 #include "net/base/network_interfaces_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
17 #include "base/win/scoped_handle.h" | 17 #include "base/win/scoped_handle.h" |
18 #include "base/win/windows_version.h" | 18 #include "base/win/windows_version.h" |
mmenke
2016/05/17 11:25:23
This header is no longer needed.
| |
19 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // Converts Windows defined types to NetworkInterfaceType. | 28 // Converts Windows defined types to NetworkInterfaceType. |
29 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { | 29 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { |
30 // Bail out for pre-Vista versions of Windows which are documented to give | |
31 // inaccurate results like returning Ethernet for WiFi. | |
32 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx | |
33 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
34 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
35 | |
36 NetworkChangeNotifier::ConnectionType type = | 30 NetworkChangeNotifier::ConnectionType type = |
37 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 31 NetworkChangeNotifier::CONNECTION_UNKNOWN; |
38 if (ifType == IF_TYPE_ETHERNET_CSMACD) { | 32 if (ifType == IF_TYPE_ETHERNET_CSMACD) { |
39 type = NetworkChangeNotifier::CONNECTION_ETHERNET; | 33 type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
40 } else if (ifType == IF_TYPE_IEEE80211) { | 34 } else if (ifType == IF_TYPE_IEEE80211) { |
41 type = NetworkChangeNotifier::CONNECTION_WIFI; | 35 type = NetworkChangeNotifier::CONNECTION_WIFI; |
42 } | 36 } |
43 // TODO(mallinath) - Cellular? | 37 // TODO(mallinath) - Cellular? |
44 return type; | 38 return type; |
45 } | 39 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 ::GetProcAddress(module, "WlanFreeMemory")); | 123 ::GetProcAddress(module, "WlanFreeMemory")); |
130 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | 124 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( |
131 ::GetProcAddress(module, "WlanCloseHandle")); | 125 ::GetProcAddress(module, "WlanCloseHandle")); |
132 initialized = open_handle_func && enum_interfaces_func && | 126 initialized = open_handle_func && enum_interfaces_func && |
133 query_interface_func && set_interface_func && | 127 query_interface_func && set_interface_func && |
134 free_memory_func && close_handle_func; | 128 free_memory_func && close_handle_func; |
135 } | 129 } |
136 | 130 |
137 bool GetNetworkListImpl(NetworkInterfaceList* networks, | 131 bool GetNetworkListImpl(NetworkInterfaceList* networks, |
138 int policy, | 132 int policy, |
139 bool is_xp, | |
140 const IP_ADAPTER_ADDRESSES* adapters) { | 133 const IP_ADAPTER_ADDRESSES* adapters) { |
141 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; | 134 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; |
142 adapter = adapter->Next) { | 135 adapter = adapter->Next) { |
143 // Ignore the loopback device. | 136 // Ignore the loopback device. |
144 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 137 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
145 continue; | 138 continue; |
146 } | 139 } |
147 | 140 |
148 if (adapter->OperStatus != IfOperStatusUp) { | 141 if (adapter->OperStatus != IfOperStatusUp) { |
149 continue; | 142 continue; |
150 } | 143 } |
151 | 144 |
152 // Ignore any HOST side vmware adapters with a description like: | 145 // Ignore any HOST side vmware adapters with a description like: |
153 // VMware Virtual Ethernet Adapter for VMnet1 | 146 // VMware Virtual Ethernet Adapter for VMnet1 |
154 // but don't ignore any GUEST side adapters with a description like: | 147 // but don't ignore any GUEST side adapters with a description like: |
155 // VMware Accelerated AMD PCNet Adapter #2 | 148 // VMware Accelerated AMD PCNet Adapter #2 |
156 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && | 149 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && |
157 strstr(adapter->AdapterName, "VMnet") != NULL) { | 150 strstr(adapter->AdapterName, "VMnet") != NULL) { |
158 continue; | 151 continue; |
159 } | 152 } |
160 | 153 |
161 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; | 154 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; |
162 address; address = address->Next) { | 155 address; address = address->Next) { |
163 int family = address->Address.lpSockaddr->sa_family; | 156 int family = address->Address.lpSockaddr->sa_family; |
164 if (family == AF_INET || family == AF_INET6) { | 157 if (family == AF_INET || family == AF_INET6) { |
165 IPEndPoint endpoint; | 158 IPEndPoint endpoint; |
166 if (endpoint.FromSockAddr(address->Address.lpSockaddr, | 159 if (endpoint.FromSockAddr(address->Address.lpSockaddr, |
167 address->Address.iSockaddrLength)) { | 160 address->Address.iSockaddrLength)) { |
168 // XP has no OnLinkPrefixLength field. | 161 size_t prefix_length = address->OnLinkPrefixLength; |
169 size_t prefix_length = is_xp ? 0 : address->OnLinkPrefixLength; | |
170 if (is_xp) { | |
171 // Prior to Windows Vista the FirstPrefix pointed to the list with | |
172 // single prefix for each IP address assigned to the adapter. | |
173 // Order of FirstPrefix does not match order of FirstUnicastAddress, | |
174 // so we need to find corresponding prefix. | |
175 for (IP_ADAPTER_PREFIX* prefix = adapter->FirstPrefix; prefix; | |
176 prefix = prefix->Next) { | |
177 int prefix_family = prefix->Address.lpSockaddr->sa_family; | |
178 IPEndPoint network_endpoint; | |
179 if (prefix_family == family && | |
180 network_endpoint.FromSockAddr( | |
181 prefix->Address.lpSockaddr, | |
182 prefix->Address.iSockaddrLength) && | |
183 IPAddressMatchesPrefix(endpoint.address(), | |
184 network_endpoint.address(), | |
185 prefix->PrefixLength)) { | |
186 prefix_length = | |
187 std::max<size_t>(prefix_length, prefix->PrefixLength); | |
188 } | |
189 } | |
190 } | |
191 | 162 |
192 // If the duplicate address detection (DAD) state is not changed to | 163 // If the duplicate address detection (DAD) state is not changed to |
193 // Preferred, skip this address. | 164 // Preferred, skip this address. |
194 if (address->DadState != IpDadStatePreferred) { | 165 if (address->DadState != IpDadStatePreferred) { |
195 continue; | 166 continue; |
196 } | 167 } |
197 | 168 |
198 uint32_t index = | 169 uint32_t index = |
199 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; | 170 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; |
200 | 171 |
(...skipping 19 matching lines...) Expand all Loading... | |
220 } | 191 } |
221 } | 192 } |
222 } | 193 } |
223 } | 194 } |
224 return true; | 195 return true; |
225 } | 196 } |
226 | 197 |
227 } // namespace internal | 198 } // namespace internal |
228 | 199 |
229 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 200 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
230 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; | |
231 ULONG len = 0; | 201 ULONG len = 0; |
232 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; | 202 ULONG flags = 0; |
233 // GetAdaptersAddresses() may require IO operations. | 203 // GetAdaptersAddresses() may require IO operations. |
234 base::ThreadRestrictions::AssertIOAllowed(); | 204 base::ThreadRestrictions::AssertIOAllowed(); |
235 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | 205 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); |
236 if (result != ERROR_BUFFER_OVERFLOW) { | 206 if (result != ERROR_BUFFER_OVERFLOW) { |
237 // There are 0 networks. | 207 // There are 0 networks. |
238 return true; | 208 return true; |
239 } | 209 } |
240 std::unique_ptr<char[]> buf(new char[len]); | 210 std::unique_ptr<char[]> buf(new char[len]); |
241 IP_ADAPTER_ADDRESSES* adapters = | 211 IP_ADAPTER_ADDRESSES* adapters = |
242 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); | 212 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); |
243 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | 213 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); |
244 if (result != NO_ERROR) { | 214 if (result != NO_ERROR) { |
245 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | 215 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; |
246 return false; | 216 return false; |
247 } | 217 } |
248 | 218 |
249 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); | 219 return internal::GetNetworkListImpl(networks, policy, adapters); |
250 } | 220 } |
251 | 221 |
252 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 222 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
253 auto conn_info = GetConnectionAttributes(); | 223 auto conn_info = GetConnectionAttributes(); |
254 | 224 |
255 if (!conn_info.get()) | 225 if (!conn_info.get()) |
256 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 226 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
257 | 227 |
258 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { | 228 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { |
259 case dot11_phy_type_fhss: | 229 case dot11_phy_type_fhss: |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 | 306 |
337 if (!conn_info.get()) | 307 if (!conn_info.get()) |
338 return ""; | 308 return ""; |
339 | 309 |
340 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; | 310 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; |
341 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), | 311 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), |
342 dot11_ssid.uSSIDLength); | 312 dot11_ssid.uSSIDLength); |
343 } | 313 } |
344 | 314 |
345 } // namespace net | 315 } // namespace net |
OLD | NEW |