| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/base/net_util.h" | |
| 6 | |
| 7 #include <iphlpapi.h> | |
| 8 #include <wlanapi.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/strings/string_piece.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/strings/sys_string_conversions.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/threading/thread_restrictions.h" | |
| 20 #include "base/win/scoped_handle.h" | |
| 21 #include "base/win/windows_version.h" | |
| 22 #include "net/base/escape.h" | |
| 23 #include "net/base/ip_endpoint.h" | |
| 24 #include "net/base/net_errors.h" | |
| 25 #include "net/base/net_util_win.h" | |
| 26 #include "url/gurl.h" | |
| 27 | |
| 28 namespace net { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 // Converts Windows defined types to NetworkInterfaceType. | |
| 33 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { | |
| 34 // Bail out for pre-Vista versions of Windows which are documented to give | |
| 35 // inaccurate results like returning Ethernet for WiFi. | |
| 36 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx | |
| 37 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
| 38 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
| 39 | |
| 40 NetworkChangeNotifier::ConnectionType type = | |
| 41 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
| 42 if (ifType == IF_TYPE_ETHERNET_CSMACD) { | |
| 43 type = NetworkChangeNotifier::CONNECTION_ETHERNET; | |
| 44 } else if (ifType == IF_TYPE_IEEE80211) { | |
| 45 type = NetworkChangeNotifier::CONNECTION_WIFI; | |
| 46 } | |
| 47 // TODO(mallinath) - Cellular? | |
| 48 return type; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 namespace internal { | |
| 54 | |
| 55 base::LazyInstance<WlanApi>::Leaky lazy_wlanapi = | |
| 56 LAZY_INSTANCE_INITIALIZER; | |
| 57 | |
| 58 WlanApi& WlanApi::GetInstance() { | |
| 59 return lazy_wlanapi.Get(); | |
| 60 } | |
| 61 | |
| 62 WlanApi::WlanApi() : initialized(false) { | |
| 63 // Use an absolute path to load the DLL to avoid DLL preloading attacks. | |
| 64 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll"; | |
| 65 wchar_t path[MAX_PATH] = {0}; | |
| 66 ExpandEnvironmentStrings(kDLL, path, arraysize(path)); | |
| 67 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 68 if (!module) | |
| 69 return; | |
| 70 | |
| 71 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>( | |
| 72 ::GetProcAddress(module, "WlanOpenHandle")); | |
| 73 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>( | |
| 74 ::GetProcAddress(module, "WlanEnumInterfaces")); | |
| 75 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>( | |
| 76 ::GetProcAddress(module, "WlanQueryInterface")); | |
| 77 set_interface_func = reinterpret_cast<WlanSetInterfaceFunc>( | |
| 78 ::GetProcAddress(module, "WlanSetInterface")); | |
| 79 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( | |
| 80 ::GetProcAddress(module, "WlanFreeMemory")); | |
| 81 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | |
| 82 ::GetProcAddress(module, "WlanCloseHandle")); | |
| 83 initialized = open_handle_func && enum_interfaces_func && | |
| 84 query_interface_func && set_interface_func && | |
| 85 free_memory_func && close_handle_func; | |
| 86 } | |
| 87 | |
| 88 bool GetNetworkListImpl(NetworkInterfaceList* networks, | |
| 89 int policy, | |
| 90 bool is_xp, | |
| 91 const IP_ADAPTER_ADDRESSES* adapters) { | |
| 92 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; | |
| 93 adapter = adapter->Next) { | |
| 94 // Ignore the loopback device. | |
| 95 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | |
| 96 continue; | |
| 97 } | |
| 98 | |
| 99 if (adapter->OperStatus != IfOperStatusUp) { | |
| 100 continue; | |
| 101 } | |
| 102 | |
| 103 // Ignore any HOST side vmware adapters with a description like: | |
| 104 // VMware Virtual Ethernet Adapter for VMnet1 | |
| 105 // but don't ignore any GUEST side adapters with a description like: | |
| 106 // VMware Accelerated AMD PCNet Adapter #2 | |
| 107 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && | |
| 108 strstr(adapter->AdapterName, "VMnet") != NULL) { | |
| 109 continue; | |
| 110 } | |
| 111 | |
| 112 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; | |
| 113 address; address = address->Next) { | |
| 114 int family = address->Address.lpSockaddr->sa_family; | |
| 115 if (family == AF_INET || family == AF_INET6) { | |
| 116 IPEndPoint endpoint; | |
| 117 if (endpoint.FromSockAddr(address->Address.lpSockaddr, | |
| 118 address->Address.iSockaddrLength)) { | |
| 119 // XP has no OnLinkPrefixLength field. | |
| 120 size_t prefix_length = is_xp ? 0 : address->OnLinkPrefixLength; | |
| 121 if (is_xp) { | |
| 122 // Prior to Windows Vista the FirstPrefix pointed to the list with | |
| 123 // single prefix for each IP address assigned to the adapter. | |
| 124 // Order of FirstPrefix does not match order of FirstUnicastAddress, | |
| 125 // so we need to find corresponding prefix. | |
| 126 for (IP_ADAPTER_PREFIX* prefix = adapter->FirstPrefix; prefix; | |
| 127 prefix = prefix->Next) { | |
| 128 int prefix_family = prefix->Address.lpSockaddr->sa_family; | |
| 129 IPEndPoint network_endpoint; | |
| 130 if (prefix_family == family && | |
| 131 network_endpoint.FromSockAddr(prefix->Address.lpSockaddr, | |
| 132 prefix->Address.iSockaddrLength) && | |
| 133 IPNumberMatchesPrefix(endpoint.address(), | |
| 134 network_endpoint.address(), | |
| 135 prefix->PrefixLength)) { | |
| 136 prefix_length = | |
| 137 std::max<size_t>(prefix_length, prefix->PrefixLength); | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 // If the duplicate address detection (DAD) state is not changed to | |
| 143 // Preferred, skip this address. | |
| 144 if (address->DadState != IpDadStatePreferred) { | |
| 145 continue; | |
| 146 } | |
| 147 | |
| 148 uint32 index = | |
| 149 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; | |
| 150 | |
| 151 // From http://technet.microsoft.com/en-us/ff568768(v=vs.60).aspx, the | |
| 152 // way to identify a temporary IPv6 Address is to check if | |
| 153 // PrefixOrigin is equal to IpPrefixOriginRouterAdvertisement and | |
| 154 // SuffixOrigin equal to IpSuffixOriginRandom. | |
| 155 int ip_address_attributes = IP_ADDRESS_ATTRIBUTE_NONE; | |
| 156 if (family == AF_INET6) { | |
| 157 if (address->PrefixOrigin == IpPrefixOriginRouterAdvertisement && | |
| 158 address->SuffixOrigin == IpSuffixOriginRandom) { | |
| 159 ip_address_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY; | |
| 160 } | |
| 161 if (address->PreferredLifetime == 0) { | |
| 162 ip_address_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED; | |
| 163 } | |
| 164 } | |
| 165 networks->push_back(NetworkInterface( | |
| 166 adapter->AdapterName, | |
| 167 base::SysWideToNativeMB(adapter->FriendlyName), index, | |
| 168 GetNetworkInterfaceType(adapter->IfType), endpoint.address(), | |
| 169 prefix_length, ip_address_attributes)); | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 } // namespace internal | |
| 178 | |
| 179 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | |
| 180 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; | |
| 181 ULONG len = 0; | |
| 182 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; | |
| 183 // GetAdaptersAddresses() may require IO operations. | |
| 184 base::ThreadRestrictions::AssertIOAllowed(); | |
| 185 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | |
| 186 if (result != ERROR_BUFFER_OVERFLOW) { | |
| 187 // There are 0 networks. | |
| 188 return true; | |
| 189 } | |
| 190 scoped_ptr<char[]> buf(new char[len]); | |
| 191 IP_ADAPTER_ADDRESSES* adapters = | |
| 192 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); | |
| 193 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | |
| 194 if (result != NO_ERROR) { | |
| 195 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | |
| 196 return false; | |
| 197 } | |
| 198 | |
| 199 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); | |
| 200 } | |
| 201 | |
| 202 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | |
| 203 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); | |
| 204 if (!wlanapi.initialized) | |
| 205 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 206 | |
| 207 internal::WlanHandle client; | |
| 208 DWORD cur_version = 0; | |
| 209 const DWORD kMaxClientVersion = 2; | |
| 210 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); | |
| 211 if (result != ERROR_SUCCESS) | |
| 212 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 213 | |
| 214 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; | |
| 215 result = wlanapi.enum_interfaces_func(client.Get(), NULL, | |
| 216 &interface_list_ptr); | |
| 217 if (result != ERROR_SUCCESS) | |
| 218 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 219 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( | |
| 220 interface_list_ptr); | |
| 221 | |
| 222 // Assume at most one connected wifi interface. | |
| 223 WLAN_INTERFACE_INFO* info = NULL; | |
| 224 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { | |
| 225 if (interface_list->InterfaceInfo[i].isState == | |
| 226 wlan_interface_state_connected) { | |
| 227 info = &interface_list->InterfaceInfo[i]; | |
| 228 break; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 if (info == NULL) | |
| 233 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 234 | |
| 235 WLAN_CONNECTION_ATTRIBUTES* conn_info_ptr; | |
| 236 DWORD conn_info_size = 0; | |
| 237 WLAN_OPCODE_VALUE_TYPE op_code; | |
| 238 result = wlanapi.query_interface_func( | |
| 239 client.Get(), &info->InterfaceGuid, wlan_intf_opcode_current_connection, | |
| 240 NULL, &conn_info_size, reinterpret_cast<VOID**>(&conn_info_ptr), | |
| 241 &op_code); | |
| 242 if (result != ERROR_SUCCESS) | |
| 243 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | |
| 244 scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter> conn_info( | |
| 245 conn_info_ptr); | |
| 246 | |
| 247 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { | |
| 248 case dot11_phy_type_fhss: | |
| 249 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; | |
| 250 case dot11_phy_type_dsss: | |
| 251 return WIFI_PHY_LAYER_PROTOCOL_B; | |
| 252 case dot11_phy_type_irbaseband: | |
| 253 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; | |
| 254 case dot11_phy_type_ofdm: | |
| 255 return WIFI_PHY_LAYER_PROTOCOL_A; | |
| 256 case dot11_phy_type_hrdsss: | |
| 257 return WIFI_PHY_LAYER_PROTOCOL_B; | |
| 258 case dot11_phy_type_erp: | |
| 259 return WIFI_PHY_LAYER_PROTOCOL_G; | |
| 260 case dot11_phy_type_ht: | |
| 261 return WIFI_PHY_LAYER_PROTOCOL_N; | |
| 262 default: | |
| 263 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 // Note: There is no need to explicitly set the options back | |
| 268 // as the OS will automatically set them back when the WlanHandle | |
| 269 // is closed. | |
| 270 class WifiOptionSetter : public ScopedWifiOptions { | |
| 271 public: | |
| 272 WifiOptionSetter(int options) { | |
| 273 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); | |
| 274 if (!wlanapi.initialized) | |
| 275 return; | |
| 276 | |
| 277 DWORD cur_version = 0; | |
| 278 const DWORD kMaxClientVersion = 2; | |
| 279 DWORD result = wlanapi.OpenHandle( | |
| 280 kMaxClientVersion, &cur_version, &client_); | |
| 281 if (result != ERROR_SUCCESS) | |
| 282 return; | |
| 283 | |
| 284 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; | |
| 285 result = wlanapi.enum_interfaces_func(client_.Get(), NULL, | |
| 286 &interface_list_ptr); | |
| 287 if (result != ERROR_SUCCESS) | |
| 288 return; | |
| 289 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> | |
| 290 interface_list(interface_list_ptr); | |
| 291 | |
| 292 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { | |
| 293 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i]; | |
| 294 if (options & WIFI_OPTIONS_DISABLE_SCAN) { | |
| 295 BOOL data = false; | |
| 296 wlanapi.set_interface_func(client_.Get(), | |
| 297 &info->InterfaceGuid, | |
| 298 wlan_intf_opcode_background_scan_enabled, | |
| 299 sizeof(data), | |
| 300 &data, | |
| 301 NULL); | |
| 302 } | |
| 303 if (options & WIFI_OPTIONS_MEDIA_STREAMING_MODE) { | |
| 304 BOOL data = true; | |
| 305 wlanapi.set_interface_func(client_.Get(), | |
| 306 &info->InterfaceGuid, | |
| 307 wlan_intf_opcode_media_streaming_mode, | |
| 308 sizeof(data), | |
| 309 &data, | |
| 310 NULL); | |
| 311 } | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 private: | |
| 316 internal::WlanHandle client_; | |
| 317 }; | |
| 318 | |
| 319 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { | |
| 320 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); | |
| 321 } | |
| 322 | |
| 323 std::string GetWifiSSID() { | |
| 324 NOTIMPLEMENTED(); | |
| 325 return ""; | |
| 326 } | |
| 327 | |
| 328 } // namespace net | |
| OLD | NEW |