| 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" | |
| 19 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
| 20 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 23 | 22 |
| 24 namespace net { | 23 namespace net { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 // Converts Windows defined types to NetworkInterfaceType. | 27 // Converts Windows defined types to NetworkInterfaceType. |
| 29 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { | 28 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 = | 29 NetworkChangeNotifier::ConnectionType type = |
| 37 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 30 NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 38 if (ifType == IF_TYPE_ETHERNET_CSMACD) { | 31 if (ifType == IF_TYPE_ETHERNET_CSMACD) { |
| 39 type = NetworkChangeNotifier::CONNECTION_ETHERNET; | 32 type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 40 } else if (ifType == IF_TYPE_IEEE80211) { | 33 } else if (ifType == IF_TYPE_IEEE80211) { |
| 41 type = NetworkChangeNotifier::CONNECTION_WIFI; | 34 type = NetworkChangeNotifier::CONNECTION_WIFI; |
| 42 } | 35 } |
| 43 // TODO(mallinath) - Cellular? | 36 // TODO(mallinath) - Cellular? |
| 44 return type; | 37 return type; |
| 45 } | 38 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ::GetProcAddress(module, "WlanFreeMemory")); | 122 ::GetProcAddress(module, "WlanFreeMemory")); |
| 130 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | 123 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( |
| 131 ::GetProcAddress(module, "WlanCloseHandle")); | 124 ::GetProcAddress(module, "WlanCloseHandle")); |
| 132 initialized = open_handle_func && enum_interfaces_func && | 125 initialized = open_handle_func && enum_interfaces_func && |
| 133 query_interface_func && set_interface_func && | 126 query_interface_func && set_interface_func && |
| 134 free_memory_func && close_handle_func; | 127 free_memory_func && close_handle_func; |
| 135 } | 128 } |
| 136 | 129 |
| 137 bool GetNetworkListImpl(NetworkInterfaceList* networks, | 130 bool GetNetworkListImpl(NetworkInterfaceList* networks, |
| 138 int policy, | 131 int policy, |
| 139 bool is_xp, | |
| 140 const IP_ADAPTER_ADDRESSES* adapters) { | 132 const IP_ADAPTER_ADDRESSES* adapters) { |
| 141 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; | 133 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; |
| 142 adapter = adapter->Next) { | 134 adapter = adapter->Next) { |
| 143 // Ignore the loopback device. | 135 // Ignore the loopback device. |
| 144 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 136 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
| 145 continue; | 137 continue; |
| 146 } | 138 } |
| 147 | 139 |
| 148 if (adapter->OperStatus != IfOperStatusUp) { | 140 if (adapter->OperStatus != IfOperStatusUp) { |
| 149 continue; | 141 continue; |
| 150 } | 142 } |
| 151 | 143 |
| 152 // Ignore any HOST side vmware adapters with a description like: | 144 // Ignore any HOST side vmware adapters with a description like: |
| 153 // VMware Virtual Ethernet Adapter for VMnet1 | 145 // VMware Virtual Ethernet Adapter for VMnet1 |
| 154 // but don't ignore any GUEST side adapters with a description like: | 146 // but don't ignore any GUEST side adapters with a description like: |
| 155 // VMware Accelerated AMD PCNet Adapter #2 | 147 // VMware Accelerated AMD PCNet Adapter #2 |
| 156 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && | 148 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && |
| 157 strstr(adapter->AdapterName, "VMnet") != NULL) { | 149 strstr(adapter->AdapterName, "VMnet") != NULL) { |
| 158 continue; | 150 continue; |
| 159 } | 151 } |
| 160 | 152 |
| 161 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; | 153 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; |
| 162 address; address = address->Next) { | 154 address; address = address->Next) { |
| 163 int family = address->Address.lpSockaddr->sa_family; | 155 int family = address->Address.lpSockaddr->sa_family; |
| 164 if (family == AF_INET || family == AF_INET6) { | 156 if (family == AF_INET || family == AF_INET6) { |
| 165 IPEndPoint endpoint; | 157 IPEndPoint endpoint; |
| 166 if (endpoint.FromSockAddr(address->Address.lpSockaddr, | 158 if (endpoint.FromSockAddr(address->Address.lpSockaddr, |
| 167 address->Address.iSockaddrLength)) { | 159 address->Address.iSockaddrLength)) { |
| 168 // XP has no OnLinkPrefixLength field. | 160 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 | 161 |
| 192 // If the duplicate address detection (DAD) state is not changed to | 162 // If the duplicate address detection (DAD) state is not changed to |
| 193 // Preferred, skip this address. | 163 // Preferred, skip this address. |
| 194 if (address->DadState != IpDadStatePreferred) { | 164 if (address->DadState != IpDadStatePreferred) { |
| 195 continue; | 165 continue; |
| 196 } | 166 } |
| 197 | 167 |
| 198 uint32_t index = | 168 uint32_t index = |
| 199 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; | 169 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; |
| 200 | 170 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 220 } | 190 } |
| 221 } | 191 } |
| 222 } | 192 } |
| 223 } | 193 } |
| 224 return true; | 194 return true; |
| 225 } | 195 } |
| 226 | 196 |
| 227 } // namespace internal | 197 } // namespace internal |
| 228 | 198 |
| 229 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 199 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 230 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; | |
| 231 ULONG len = 0; | 200 ULONG len = 0; |
| 232 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; | 201 ULONG flags = 0; |
| 233 // GetAdaptersAddresses() may require IO operations. | 202 // GetAdaptersAddresses() may require IO operations. |
| 234 base::ThreadRestrictions::AssertIOAllowed(); | 203 base::ThreadRestrictions::AssertIOAllowed(); |
| 235 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | 204 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); |
| 236 if (result != ERROR_BUFFER_OVERFLOW) { | 205 if (result != ERROR_BUFFER_OVERFLOW) { |
| 237 // There are 0 networks. | 206 // There are 0 networks. |
| 238 return true; | 207 return true; |
| 239 } | 208 } |
| 240 std::unique_ptr<char[]> buf(new char[len]); | 209 std::unique_ptr<char[]> buf(new char[len]); |
| 241 IP_ADAPTER_ADDRESSES* adapters = | 210 IP_ADAPTER_ADDRESSES* adapters = |
| 242 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); | 211 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); |
| 243 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | 212 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); |
| 244 if (result != NO_ERROR) { | 213 if (result != NO_ERROR) { |
| 245 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | 214 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; |
| 246 return false; | 215 return false; |
| 247 } | 216 } |
| 248 | 217 |
| 249 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); | 218 return internal::GetNetworkListImpl(networks, policy, adapters); |
| 250 } | 219 } |
| 251 | 220 |
| 252 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 221 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
| 253 auto conn_info = GetConnectionAttributes(); | 222 auto conn_info = GetConnectionAttributes(); |
| 254 | 223 |
| 255 if (!conn_info.get()) | 224 if (!conn_info.get()) |
| 256 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 225 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
| 257 | 226 |
| 258 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { | 227 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { |
| 259 case dot11_phy_type_fhss: | 228 case dot11_phy_type_fhss: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 | 305 |
| 337 if (!conn_info.get()) | 306 if (!conn_info.get()) |
| 338 return ""; | 307 return ""; |
| 339 | 308 |
| 340 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; | 309 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; |
| 341 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), | 310 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), |
| 342 dot11_ssid.uSSIDLength); | 311 dot11_ssid.uSSIDLength); |
| 343 } | 312 } |
| 344 | 313 |
| 345 } // namespace net | 314 } // namespace net |
| OLD | NEW |