OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Windows Vista uses the Native Wifi (WLAN) API for accessing WiFi cards. See | 5 // Windows Vista uses the Native Wifi (WLAN) API for accessing WiFi cards. See |
6 // http://msdn.microsoft.com/en-us/library/ms705945(VS.85).aspx. Windows XP | 6 // http://msdn.microsoft.com/en-us/library/ms705945(VS.85).aspx. Windows XP |
7 // Service Pack 3 (and Windows XP Service Pack 2, if upgraded with a hot fix) | 7 // Service Pack 3 (and Windows XP Service Pack 2, if upgraded with a hot fix) |
8 // also support a limited version of the WLAN API. See | 8 // also support a limited version of the WLAN API. See |
9 // http://msdn.microsoft.com/en-us/library/bb204766.aspx. The WLAN API uses | 9 // http://msdn.microsoft.com/en-us/library/bb204766.aspx. The WLAN API uses |
10 // wlanapi.h, which is not part of the SDK used by Gears, so is replicated | 10 // wlanapi.h, which is not part of the SDK used by Gears, so is replicated |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 #include "base/memory/free_deleter.h" | 30 #include "base/memory/free_deleter.h" |
31 #include "base/metrics/histogram.h" | 31 #include "base/metrics/histogram.h" |
32 #include "base/strings/utf_string_conversions.h" | 32 #include "base/strings/utf_string_conversions.h" |
33 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
34 #include "device/geolocation/wifi_data_provider_common.h" | 34 #include "device/geolocation/wifi_data_provider_common.h" |
35 #include "device/geolocation/wifi_data_provider_common_win.h" | 35 #include "device/geolocation/wifi_data_provider_common_win.h" |
36 #include "device/geolocation/wifi_data_provider_manager.h" | 36 #include "device/geolocation/wifi_data_provider_manager.h" |
37 | 37 |
38 // Taken from ndis.h for WinCE. | 38 // Taken from ndis.h for WinCE. |
39 #define NDIS_STATUS_INVALID_LENGTH ((NDIS_STATUS)0xC0010014L) | 39 #define NDIS_STATUS_INVALID_LENGTH ((NDIS_STATUS)0xC0010014L) |
40 #define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L) | 40 #define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L) |
41 | 41 |
42 namespace device { | 42 namespace device { |
43 namespace { | 43 namespace { |
44 // The limits on the size of the buffer used for the OID query. | 44 // The limits on the size of the buffer used for the OID query. |
45 const int kInitialBufferSize = 2 << 12; // Good for about 50 APs. | 45 const int kInitialBufferSize = 2 << 12; // Good for about 50 APs. |
46 const int kMaximumBufferSize = 2 << 20; // 2MB | 46 const int kMaximumBufferSize = 2 << 20; // 2MB |
47 | 47 |
48 // Length for generic string buffers passed to Windows APIs. | 48 // Length for generic string buffers passed to Windows APIs. |
49 const int kStringLength = 512; | 49 const int kStringLength = 512; |
50 | 50 |
51 // The time periods, in milliseconds, between successive polls of the wifi data. | 51 // The time periods, in milliseconds, between successive polls of the wifi data. |
52 const int kDefaultPollingInterval = 10000; // 10s | 52 const int kDefaultPollingInterval = 10000; // 10s |
53 const int kNoChangePollingInterval = 120000; // 2 mins | 53 const int kNoChangePollingInterval = 120000; // 2 mins |
54 const int kTwoNoChangePollingInterval = 600000; // 10 mins | 54 const int kTwoNoChangePollingInterval = 600000; // 10 mins |
55 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 55 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s |
56 | 56 |
57 // WlanOpenHandle | 57 // WlanOpenHandle |
58 typedef DWORD (WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, | 58 typedef DWORD(WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, |
59 PVOID pReserved, | 59 PVOID pReserved, |
60 PDWORD pdwNegotiatedVersion, | 60 PDWORD pdwNegotiatedVersion, |
61 PHANDLE phClientHandle); | 61 PHANDLE phClientHandle); |
62 | 62 |
63 // WlanEnumInterfaces | 63 // WlanEnumInterfaces |
64 typedef DWORD (WINAPI* WlanEnumInterfacesFunction)( | 64 typedef DWORD(WINAPI* WlanEnumInterfacesFunction)( |
65 HANDLE hClientHandle, | 65 HANDLE hClientHandle, |
66 PVOID pReserved, | 66 PVOID pReserved, |
67 PWLAN_INTERFACE_INFO_LIST* ppInterfaceList); | 67 PWLAN_INTERFACE_INFO_LIST* ppInterfaceList); |
68 | 68 |
69 // WlanGetNetworkBssList | 69 // WlanGetNetworkBssList |
70 typedef DWORD (WINAPI* WlanGetNetworkBssListFunction)( | 70 typedef DWORD(WINAPI* WlanGetNetworkBssListFunction)( |
71 HANDLE hClientHandle, | 71 HANDLE hClientHandle, |
72 const GUID* pInterfaceGuid, | 72 const GUID* pInterfaceGuid, |
73 const PDOT11_SSID pDot11Ssid, | 73 const PDOT11_SSID pDot11Ssid, |
74 DOT11_BSS_TYPE dot11BssType, | 74 DOT11_BSS_TYPE dot11BssType, |
75 BOOL bSecurityEnabled, | 75 BOOL bSecurityEnabled, |
76 PVOID pReserved, | 76 PVOID pReserved, |
77 PWLAN_BSS_LIST* ppWlanBssList | 77 PWLAN_BSS_LIST* ppWlanBssList); |
78 ); | |
79 | 78 |
80 // WlanFreeMemory | 79 // WlanFreeMemory |
81 typedef VOID (WINAPI* WlanFreeMemoryFunction)(PVOID pMemory); | 80 typedef VOID(WINAPI* WlanFreeMemoryFunction)(PVOID pMemory); |
82 | 81 |
83 // WlanCloseHandle | 82 // WlanCloseHandle |
84 typedef DWORD (WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle, | 83 typedef DWORD(WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle, |
85 PVOID pReserved); | 84 PVOID pReserved); |
86 | |
87 | 85 |
88 // Local classes and functions | 86 // Local classes and functions |
89 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { | 87 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { |
90 public: | 88 public: |
91 ~WindowsWlanApi() override; | 89 ~WindowsWlanApi() override; |
92 // Factory function. Will return NULL if this API is unavailable. | 90 // Factory function. Will return NULL if this API is unavailable. |
93 static WindowsWlanApi* Create(); | 91 static WindowsWlanApi* Create(); |
94 | 92 |
95 // WlanApiInterface | 93 // WlanApiInterface |
96 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; | 94 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 std::unique_ptr<BYTE, base::FreeDeleter>* buffer); | 156 std::unique_ptr<BYTE, base::FreeDeleter>* buffer); |
159 // Gets the system directory and appends a trailing slash if not already | 157 // Gets the system directory and appends a trailing slash if not already |
160 // present. | 158 // present. |
161 bool GetSystemDirectory(base::string16* path); | 159 bool GetSystemDirectory(base::string16* path); |
162 } // namespace | 160 } // namespace |
163 | 161 |
164 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { | 162 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { |
165 return new WifiDataProviderWin(); | 163 return new WifiDataProviderWin(); |
166 } | 164 } |
167 | 165 |
168 WifiDataProviderWin::WifiDataProviderWin() { | 166 WifiDataProviderWin::WifiDataProviderWin() {} |
169 } | |
170 | 167 |
171 WifiDataProviderWin::~WifiDataProviderWin() { | 168 WifiDataProviderWin::~WifiDataProviderWin() {} |
172 } | |
173 | 169 |
174 WifiDataProviderCommon::WlanApiInterface* WifiDataProviderWin::NewWlanApi() { | 170 WifiDataProviderCommon::WlanApiInterface* WifiDataProviderWin::NewWlanApi() { |
175 // Use the WLAN interface if we're on Vista and if it's available. Otherwise, | 171 // Use the WLAN interface if we're on Vista and if it's available. Otherwise, |
176 // use NDIS. | 172 // use NDIS. |
177 WlanApiInterface* api = WindowsWlanApi::Create(); | 173 WlanApiInterface* api = WindowsWlanApi::Create(); |
178 if (api) { | 174 if (api) { |
179 return api; | 175 return api; |
180 } | 176 } |
181 return WindowsNdisApi::Create(); | 177 return WindowsNdisApi::Create(); |
182 } | 178 } |
183 | 179 |
184 WifiPollingPolicy* WifiDataProviderWin::NewPollingPolicy() { | 180 WifiPollingPolicy* WifiDataProviderWin::NewPollingPolicy() { |
185 return new GenericWifiPollingPolicy<kDefaultPollingInterval, | 181 return new GenericWifiPollingPolicy< |
186 kNoChangePollingInterval, | 182 kDefaultPollingInterval, kNoChangePollingInterval, |
187 kTwoNoChangePollingInterval, | 183 kTwoNoChangePollingInterval, kNoWifiPollingIntervalMilliseconds>; |
188 kNoWifiPollingIntervalMilliseconds>; | |
189 } | 184 } |
190 | 185 |
191 // Local classes and functions | 186 // Local classes and functions |
192 namespace { | 187 namespace { |
193 | 188 |
194 // WindowsWlanApi | 189 // WindowsWlanApi |
195 WindowsWlanApi::WindowsWlanApi(HINSTANCE library) | 190 WindowsWlanApi::WindowsWlanApi(HINSTANCE library) : library_(library) { |
196 : library_(library) { | |
197 GetWLANFunctions(library_); | 191 GetWLANFunctions(library_); |
198 } | 192 } |
199 | 193 |
200 WindowsWlanApi::~WindowsWlanApi() { | 194 WindowsWlanApi::~WindowsWlanApi() { |
201 FreeLibrary(library_); | 195 FreeLibrary(library_); |
202 } | 196 } |
203 | 197 |
204 WindowsWlanApi* WindowsWlanApi::Create() { | 198 WindowsWlanApi* WindowsWlanApi::Create() { |
205 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 199 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
206 return NULL; | 200 return NULL; |
207 // We use an absolute path to load the DLL to avoid DLL preloading attacks. | 201 // We use an absolute path to load the DLL to avoid DLL preloading attacks. |
208 base::string16 system_directory; | 202 base::string16 system_directory; |
209 if (!GetSystemDirectory(&system_directory)) { | 203 if (!GetSystemDirectory(&system_directory)) { |
210 return NULL; | 204 return NULL; |
211 } | 205 } |
212 DCHECK(!system_directory.empty()); | 206 DCHECK(!system_directory.empty()); |
213 base::string16 dll_path = system_directory + L"wlanapi.dll"; | 207 base::string16 dll_path = system_directory + L"wlanapi.dll"; |
214 HINSTANCE library = LoadLibraryEx(dll_path.c_str(), | 208 HINSTANCE library = |
215 NULL, | 209 LoadLibraryEx(dll_path.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
216 LOAD_WITH_ALTERED_SEARCH_PATH); | |
217 if (!library) { | 210 if (!library) { |
218 return NULL; | 211 return NULL; |
219 } | 212 } |
220 return new WindowsWlanApi(library); | 213 return new WindowsWlanApi(library); |
221 } | 214 } |
222 | 215 |
223 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) { | 216 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) { |
224 DCHECK(wlan_library); | 217 DCHECK(wlan_library); |
225 WlanOpenHandle_function_ = reinterpret_cast<WlanOpenHandleFunction>( | 218 WlanOpenHandle_function_ = reinterpret_cast<WlanOpenHandleFunction>( |
226 GetProcAddress(wlan_library, "WlanOpenHandle")); | 219 GetProcAddress(wlan_library, "WlanOpenHandle")); |
227 WlanEnumInterfaces_function_ = reinterpret_cast<WlanEnumInterfacesFunction>( | 220 WlanEnumInterfaces_function_ = reinterpret_cast<WlanEnumInterfacesFunction>( |
228 GetProcAddress(wlan_library, "WlanEnumInterfaces")); | 221 GetProcAddress(wlan_library, "WlanEnumInterfaces")); |
229 WlanGetNetworkBssList_function_ = | 222 WlanGetNetworkBssList_function_ = |
230 reinterpret_cast<WlanGetNetworkBssListFunction>( | 223 reinterpret_cast<WlanGetNetworkBssListFunction>( |
231 GetProcAddress(wlan_library, "WlanGetNetworkBssList")); | 224 GetProcAddress(wlan_library, "WlanGetNetworkBssList")); |
232 WlanFreeMemory_function_ = reinterpret_cast<WlanFreeMemoryFunction>( | 225 WlanFreeMemory_function_ = reinterpret_cast<WlanFreeMemoryFunction>( |
233 GetProcAddress(wlan_library, "WlanFreeMemory")); | 226 GetProcAddress(wlan_library, "WlanFreeMemory")); |
234 WlanCloseHandle_function_ = reinterpret_cast<WlanCloseHandleFunction>( | 227 WlanCloseHandle_function_ = reinterpret_cast<WlanCloseHandleFunction>( |
235 GetProcAddress(wlan_library, "WlanCloseHandle")); | 228 GetProcAddress(wlan_library, "WlanCloseHandle")); |
236 DCHECK(WlanOpenHandle_function_ && | 229 DCHECK(WlanOpenHandle_function_ && WlanEnumInterfaces_function_ && |
237 WlanEnumInterfaces_function_ && | 230 WlanGetNetworkBssList_function_ && WlanFreeMemory_function_ && |
238 WlanGetNetworkBssList_function_ && | |
239 WlanFreeMemory_function_ && | |
240 WlanCloseHandle_function_); | 231 WlanCloseHandle_function_); |
241 } | 232 } |
242 | 233 |
243 void WindowsWlanApi::LogWlanInterfaceCount(int count) { | 234 void WindowsWlanApi::LogWlanInterfaceCount(int count) { |
244 UMA_HISTOGRAM_CUSTOM_COUNTS( | 235 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Wifi.InterfaceCount", count, 1, 5, 6); |
245 "Net.Wifi.InterfaceCount", | |
246 count, | |
247 1, | |
248 5, | |
249 6); | |
250 } | 236 } |
251 | 237 |
252 bool WindowsWlanApi::GetAccessPointData( | 238 bool WindowsWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { |
253 WifiData::AccessPointDataSet* data) { | |
254 DCHECK(data); | 239 DCHECK(data); |
255 | 240 |
256 // Get the handle to the WLAN API. | 241 // Get the handle to the WLAN API. |
257 DWORD negotiated_version; | 242 DWORD negotiated_version; |
258 HANDLE wlan_handle = NULL; | 243 HANDLE wlan_handle = NULL; |
259 // We could be executing on either Windows XP or Windows Vista, so use the | 244 // We could be executing on either Windows XP or Windows Vista, so use the |
260 // lower version of the client WLAN API. It seems that the negotiated version | 245 // lower version of the client WLAN API. It seems that the negotiated version |
261 // is the Vista version irrespective of what we pass! | 246 // is the Vista version irrespective of what we pass! |
262 static const int kXpWlanClientVersion = 1; | 247 static const int kXpWlanClientVersion = 1; |
263 if ((*WlanOpenHandle_function_)(kXpWlanClientVersion, | 248 if ((*WlanOpenHandle_function_)(kXpWlanClientVersion, NULL, |
264 NULL, | |
265 &negotiated_version, | 249 &negotiated_version, |
266 &wlan_handle) != ERROR_SUCCESS) { | 250 &wlan_handle) != ERROR_SUCCESS) { |
267 LogWlanInterfaceCount(0); | 251 LogWlanInterfaceCount(0); |
268 return false; | 252 return false; |
269 } | 253 } |
270 DCHECK(wlan_handle); | 254 DCHECK(wlan_handle); |
271 | 255 |
272 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list. | 256 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list. |
273 WLAN_INTERFACE_INFO_LIST* interface_list = NULL; | 257 WLAN_INTERFACE_INFO_LIST* interface_list = NULL; |
274 if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) != | 258 if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) != |
(...skipping 12 matching lines...) Expand all Loading... |
287 // when it's in this state. http://crbug.com/39300 | 271 // when it's in this state. http://crbug.com/39300 |
288 if (interface_list->InterfaceInfo[i].isState == | 272 if (interface_list->InterfaceInfo[i].isState == |
289 wlan_interface_state_associating) { | 273 wlan_interface_state_associating) { |
290 LOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" | 274 LOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" |
291 << interface_list->InterfaceInfo[i].strInterfaceDescription | 275 << interface_list->InterfaceInfo[i].strInterfaceDescription |
292 << ") in 'associating' state. Repeated occurrences " | 276 << ") in 'associating' state. Repeated occurrences " |
293 "indicates a non-responding adapter."; | 277 "indicates a non-responding adapter."; |
294 continue; | 278 continue; |
295 } | 279 } |
296 GetInterfaceDataWLAN(wlan_handle, | 280 GetInterfaceDataWLAN(wlan_handle, |
297 interface_list->InterfaceInfo[i].InterfaceGuid, | 281 interface_list->InterfaceInfo[i].InterfaceGuid, data); |
298 data); | |
299 } | 282 } |
300 | 283 |
301 // Free interface_list. | 284 // Free interface_list. |
302 (*WlanFreeMemory_function_)(interface_list); | 285 (*WlanFreeMemory_function_)(interface_list); |
303 | 286 |
304 // Close the handle. | 287 // Close the handle. |
305 if ((*WlanCloseHandle_function_)(wlan_handle, NULL) != ERROR_SUCCESS) { | 288 if ((*WlanCloseHandle_function_)(wlan_handle, NULL) != ERROR_SUCCESS) { |
306 return false; | 289 return false; |
307 } | 290 } |
308 | 291 |
309 return true; | 292 return true; |
310 } | 293 } |
311 | 294 |
312 // Appends the data for a single interface to the data vector. Returns the | 295 // Appends the data for a single interface to the data vector. Returns the |
313 // number of access points found, or -1 on error. | 296 // number of access points found, or -1 on error. |
314 int WindowsWlanApi::GetInterfaceDataWLAN( | 297 int WindowsWlanApi::GetInterfaceDataWLAN(const HANDLE wlan_handle, |
315 const HANDLE wlan_handle, | 298 const GUID& interface_id, |
316 const GUID& interface_id, | 299 WifiData::AccessPointDataSet* data) { |
317 WifiData::AccessPointDataSet* data) { | |
318 DCHECK(data); | 300 DCHECK(data); |
319 | 301 |
320 const base::TimeTicks start_time = base::TimeTicks::Now(); | 302 const base::TimeTicks start_time = base::TimeTicks::Now(); |
321 | 303 |
322 // WlanGetNetworkBssList allocates bss_list. | 304 // WlanGetNetworkBssList allocates bss_list. |
323 WLAN_BSS_LIST* bss_list = NULL; | 305 WLAN_BSS_LIST* bss_list = NULL; |
324 if ((*WlanGetNetworkBssList_function_)(wlan_handle, | 306 if ((*WlanGetNetworkBssList_function_)(wlan_handle, &interface_id, |
325 &interface_id, | 307 NULL, // Use all SSIDs. |
326 NULL, // Use all SSIDs. | |
327 dot11_BSS_type_any, | 308 dot11_BSS_type_any, |
328 false, // bSecurityEnabled - unused | 309 false, // bSecurityEnabled - unused |
329 NULL, // reserved | 310 NULL, // reserved |
330 &bss_list) != ERROR_SUCCESS) { | 311 &bss_list) != ERROR_SUCCESS) { |
331 return -1; | 312 return -1; |
332 } | 313 } |
333 // According to http://www.attnetclient.com/kb/questions.php?questionid=75 | 314 // According to http://www.attnetclient.com/kb/questions.php?questionid=75 |
334 // WlanGetNetworkBssList can sometimes return success, but leave the bss | 315 // WlanGetNetworkBssList can sometimes return success, but leave the bss |
335 // list as NULL. | 316 // list as NULL. |
336 if (!bss_list) | 317 if (!bss_list) |
337 return -1; | 318 return -1; |
338 | 319 |
339 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; | 320 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
340 | 321 |
341 UMA_HISTOGRAM_CUSTOM_TIMES( | 322 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", duration, |
342 "Net.Wifi.ScanLatency", | 323 base::TimeDelta::FromMilliseconds(1), |
343 duration, | 324 base::TimeDelta::FromMinutes(1), 100); |
344 base::TimeDelta::FromMilliseconds(1), | |
345 base::TimeDelta::FromMinutes(1), | |
346 100); | |
347 | |
348 | 325 |
349 int found = 0; | 326 int found = 0; |
350 for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++i) { | 327 for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++i) { |
351 AccessPointData access_point_data; | 328 AccessPointData access_point_data; |
352 if (GetNetworkData(bss_list->wlanBssEntries[i], &access_point_data)) { | 329 if (GetNetworkData(bss_list->wlanBssEntries[i], &access_point_data)) { |
353 ++found; | 330 ++found; |
354 data->insert(access_point_data); | 331 data->insert(access_point_data); |
355 } | 332 } |
356 } | 333 } |
357 | 334 |
358 (*WlanFreeMemory_function_)(bss_list); | 335 (*WlanFreeMemory_function_)(bss_list); |
359 | 336 |
360 return found; | 337 return found; |
361 } | 338 } |
362 | 339 |
363 // WindowsNdisApi | 340 // WindowsNdisApi |
364 WindowsNdisApi::WindowsNdisApi( | 341 WindowsNdisApi::WindowsNdisApi( |
365 std::vector<base::string16>* interface_service_names) | 342 std::vector<base::string16>* interface_service_names) |
366 : oid_buffer_size_(kInitialBufferSize) { | 343 : oid_buffer_size_(kInitialBufferSize) { |
367 DCHECK(!interface_service_names->empty()); | 344 DCHECK(!interface_service_names->empty()); |
368 interface_service_names_.swap(*interface_service_names); | 345 interface_service_names_.swap(*interface_service_names); |
369 } | 346 } |
370 | 347 |
371 WindowsNdisApi::~WindowsNdisApi() { | 348 WindowsNdisApi::~WindowsNdisApi() {} |
372 } | |
373 | 349 |
374 WindowsNdisApi* WindowsNdisApi::Create() { | 350 WindowsNdisApi* WindowsNdisApi::Create() { |
375 std::vector<base::string16> interface_service_names; | 351 std::vector<base::string16> interface_service_names; |
376 if (GetInterfacesNDIS(&interface_service_names)) { | 352 if (GetInterfacesNDIS(&interface_service_names)) { |
377 return new WindowsNdisApi(&interface_service_names); | 353 return new WindowsNdisApi(&interface_service_names); |
378 } | 354 } |
379 return NULL; | 355 return NULL; |
380 } | 356 } |
381 | 357 |
382 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { | 358 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { |
(...skipping 28 matching lines...) Expand all Loading... |
411 | 387 |
412 // Return true if at least one interface succeeded, or at the very least none | 388 // Return true if at least one interface succeeded, or at the very least none |
413 // failed. | 389 // failed. |
414 return interfaces_succeeded > 0 || interfaces_failed == 0; | 390 return interfaces_succeeded > 0 || interfaces_failed == 0; |
415 } | 391 } |
416 | 392 |
417 bool WindowsNdisApi::GetInterfacesNDIS( | 393 bool WindowsNdisApi::GetInterfacesNDIS( |
418 std::vector<base::string16>* interface_service_names_out) { | 394 std::vector<base::string16>* interface_service_names_out) { |
419 HKEY network_cards_key = NULL; | 395 HKEY network_cards_key = NULL; |
420 if (RegOpenKeyEx( | 396 if (RegOpenKeyEx( |
421 HKEY_LOCAL_MACHINE, | 397 HKEY_LOCAL_MACHINE, |
422 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards", | 398 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards", 0, |
423 0, | 399 KEY_READ, &network_cards_key) != ERROR_SUCCESS) { |
424 KEY_READ, | |
425 &network_cards_key) != ERROR_SUCCESS) { | |
426 return false; | 400 return false; |
427 } | 401 } |
428 DCHECK(network_cards_key); | 402 DCHECK(network_cards_key); |
429 | 403 |
430 for (int i = 0; ; ++i) { | 404 for (int i = 0;; ++i) { |
431 TCHAR name[kStringLength]; | 405 TCHAR name[kStringLength]; |
432 DWORD name_size = kStringLength; | 406 DWORD name_size = kStringLength; |
433 FILETIME time; | 407 FILETIME time; |
434 if (RegEnumKeyEx(network_cards_key, | 408 if (RegEnumKeyEx(network_cards_key, i, name, &name_size, NULL, NULL, NULL, |
435 i, | |
436 name, | |
437 &name_size, | |
438 NULL, | |
439 NULL, | |
440 NULL, | |
441 &time) != ERROR_SUCCESS) { | 409 &time) != ERROR_SUCCESS) { |
442 break; | 410 break; |
443 } | 411 } |
444 HKEY hardware_key = NULL; | 412 HKEY hardware_key = NULL; |
445 if (RegOpenKeyEx(network_cards_key, name, 0, KEY_READ, &hardware_key) != | 413 if (RegOpenKeyEx(network_cards_key, name, 0, KEY_READ, &hardware_key) != |
446 ERROR_SUCCESS) { | 414 ERROR_SUCCESS) { |
447 break; | 415 break; |
448 } | 416 } |
449 DCHECK(hardware_key); | 417 DCHECK(hardware_key); |
450 | 418 |
451 TCHAR service_name[kStringLength]; | 419 TCHAR service_name[kStringLength]; |
452 DWORD service_name_size = kStringLength; | 420 DWORD service_name_size = kStringLength; |
453 DWORD type = 0; | 421 DWORD type = 0; |
454 if (RegQueryValueEx(hardware_key, | 422 if (RegQueryValueEx(hardware_key, L"ServiceName", NULL, &type, |
455 L"ServiceName", | |
456 NULL, | |
457 &type, | |
458 reinterpret_cast<LPBYTE>(service_name), | 423 reinterpret_cast<LPBYTE>(service_name), |
459 &service_name_size) == ERROR_SUCCESS) { | 424 &service_name_size) == ERROR_SUCCESS) { |
460 interface_service_names_out->push_back(service_name); | 425 interface_service_names_out->push_back(service_name); |
461 } | 426 } |
462 RegCloseKey(hardware_key); | 427 RegCloseKey(hardware_key); |
463 } | 428 } |
464 | 429 |
465 RegCloseKey(network_cards_key); | 430 RegCloseKey(network_cards_key); |
466 return true; | 431 return true; |
467 } | 432 } |
468 | 433 |
469 | |
470 bool WindowsNdisApi::GetInterfaceDataNDIS(HANDLE adapter_handle, | 434 bool WindowsNdisApi::GetInterfaceDataNDIS(HANDLE adapter_handle, |
471 WifiData::AccessPointDataSet* data) { | 435 WifiData::AccessPointDataSet* data) { |
472 DCHECK(data); | 436 DCHECK(data); |
473 | 437 |
474 std::unique_ptr<BYTE, base::FreeDeleter> buffer( | 438 std::unique_ptr<BYTE, base::FreeDeleter> buffer( |
475 static_cast<BYTE*>(malloc(oid_buffer_size_))); | 439 static_cast<BYTE*>(malloc(oid_buffer_size_))); |
476 if (buffer == NULL) { | 440 if (buffer == NULL) { |
477 return false; | 441 return false; |
478 } | 442 } |
479 | 443 |
480 DWORD bytes_out; | 444 DWORD bytes_out; |
481 int result; | 445 int result; |
482 | 446 |
483 while (true) { | 447 while (true) { |
484 bytes_out = 0; | 448 bytes_out = 0; |
485 result = PerformQuery(adapter_handle, buffer.get(), | 449 result = PerformQuery(adapter_handle, buffer.get(), oid_buffer_size_, |
486 oid_buffer_size_, &bytes_out); | 450 &bytes_out); |
487 if (result == ERROR_GEN_FAILURE || // Returned by some Intel cards. | 451 if (result == ERROR_GEN_FAILURE || // Returned by some Intel cards. |
488 result == ERROR_INSUFFICIENT_BUFFER || | 452 result == ERROR_INSUFFICIENT_BUFFER || result == ERROR_MORE_DATA || |
489 result == ERROR_MORE_DATA || | |
490 result == NDIS_STATUS_INVALID_LENGTH || | 453 result == NDIS_STATUS_INVALID_LENGTH || |
491 result == NDIS_STATUS_BUFFER_TOO_SHORT) { | 454 result == NDIS_STATUS_BUFFER_TOO_SHORT) { |
492 // The buffer we supplied is too small, so increase it. bytes_out should | 455 // The buffer we supplied is too small, so increase it. bytes_out should |
493 // provide the required buffer size, but this is not always the case. | 456 // provide the required buffer size, but this is not always the case. |
494 if (bytes_out > static_cast<DWORD>(oid_buffer_size_)) { | 457 if (bytes_out > static_cast<DWORD>(oid_buffer_size_)) { |
495 oid_buffer_size_ = bytes_out; | 458 oid_buffer_size_ = bytes_out; |
496 } else { | 459 } else { |
497 oid_buffer_size_ *= 2; | 460 oid_buffer_size_ *= 2; |
498 } | 461 } |
499 if (!ResizeBuffer(oid_buffer_size_, &buffer)) { | 462 if (!ResizeBuffer(oid_buffer_size_, &buffer)) { |
(...skipping 29 matching lines...) Expand all Loading... |
529 // TODO(steveblock): Is it possible to get the following? | 492 // TODO(steveblock): Is it possible to get the following? |
530 // access_point_data->signal_to_noise | 493 // access_point_data->signal_to_noise |
531 // access_point_data->age | 494 // access_point_data->age |
532 // access_point_data->channel | 495 // access_point_data->channel |
533 return true; | 496 return true; |
534 } | 497 } |
535 | 498 |
536 bool UndefineDosDevice(const base::string16& device_name) { | 499 bool UndefineDosDevice(const base::string16& device_name) { |
537 // We remove only the mapping we use, that is \Device\<device_name>. | 500 // We remove only the mapping we use, that is \Device\<device_name>. |
538 base::string16 target_path = L"\\Device\\" + device_name; | 501 base::string16 target_path = L"\\Device\\" + device_name; |
539 return DefineDosDevice( | 502 return DefineDosDevice(DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | |
540 DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, | 503 DDD_EXACT_MATCH_ON_REMOVE, |
541 device_name.c_str(), | 504 device_name.c_str(), target_path.c_str()) == TRUE; |
542 target_path.c_str()) == TRUE; | |
543 } | 505 } |
544 | 506 |
545 bool DefineDosDeviceIfNotExists(const base::string16& device_name) { | 507 bool DefineDosDeviceIfNotExists(const base::string16& device_name) { |
546 // We create a DOS device name for the device at \Device\<device_name>. | 508 // We create a DOS device name for the device at \Device\<device_name>. |
547 base::string16 target_path = L"\\Device\\" + device_name; | 509 base::string16 target_path = L"\\Device\\" + device_name; |
548 | 510 |
549 TCHAR target[kStringLength]; | 511 TCHAR target[kStringLength]; |
550 if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | 512 if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && |
551 target_path.compare(target) == 0) { | 513 target_path.compare(target) == 0) { |
552 // Device already exists. | 514 // Device already exists. |
553 return true; | 515 return true; |
554 } | 516 } |
555 | 517 |
556 if (GetLastError() != ERROR_FILE_NOT_FOUND) { | 518 if (GetLastError() != ERROR_FILE_NOT_FOUND) { |
557 return false; | 519 return false; |
558 } | 520 } |
559 | 521 |
560 if (!DefineDosDevice(DDD_RAW_TARGET_PATH, | 522 if (!DefineDosDevice(DDD_RAW_TARGET_PATH, device_name.c_str(), |
561 device_name.c_str(), | |
562 target_path.c_str())) { | 523 target_path.c_str())) { |
563 return false; | 524 return false; |
564 } | 525 } |
565 | 526 |
566 // Check that the device is really there. | 527 // Check that the device is really there. |
567 return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | 528 return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && |
568 target_path.compare(target) == 0; | 529 target_path.compare(target) == 0; |
569 } | 530 } |
570 | 531 |
571 HANDLE GetFileHandle(const base::string16& device_name) { | 532 HANDLE GetFileHandle(const base::string16& device_name) { |
572 // We access a device with DOS path \Device\<device_name> at | 533 // We access a device with DOS path \Device\<device_name> at |
573 // \\.\<device_name>. | 534 // \\.\<device_name>. |
574 base::string16 formatted_device_name = L"\\\\.\\" + device_name; | 535 base::string16 formatted_device_name = L"\\\\.\\" + device_name; |
575 | 536 |
576 return CreateFile(formatted_device_name.c_str(), | 537 return CreateFile(formatted_device_name.c_str(), GENERIC_READ, |
577 GENERIC_READ, | |
578 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode | 538 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode |
579 0, // security attributes | 539 0, // security attributes |
580 OPEN_EXISTING, | 540 OPEN_EXISTING, |
581 0, // flags and attributes | 541 0, // flags and attributes |
582 INVALID_HANDLE_VALUE); | 542 INVALID_HANDLE_VALUE); |
583 } | 543 } |
584 | 544 |
585 int PerformQuery(HANDLE adapter_handle, | 545 int PerformQuery(HANDLE adapter_handle, |
586 BYTE* buffer, | 546 BYTE* buffer, |
587 DWORD buffer_size, | 547 DWORD buffer_size, |
588 DWORD* bytes_out) { | 548 DWORD* bytes_out) { |
589 DWORD oid = OID_802_11_BSSID_LIST; | 549 DWORD oid = OID_802_11_BSSID_LIST; |
590 if (!DeviceIoControl(adapter_handle, | 550 if (!DeviceIoControl(adapter_handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, |
591 IOCTL_NDIS_QUERY_GLOBAL_STATS, | 551 sizeof(oid), buffer, buffer_size, bytes_out, NULL)) { |
592 &oid, | |
593 sizeof(oid), | |
594 buffer, | |
595 buffer_size, | |
596 bytes_out, | |
597 NULL)) { | |
598 return GetLastError(); | 552 return GetLastError(); |
599 } | 553 } |
600 return ERROR_SUCCESS; | 554 return ERROR_SUCCESS; |
601 } | 555 } |
602 | 556 |
603 bool ResizeBuffer(int requested_size, | 557 bool ResizeBuffer(int requested_size, |
604 std::unique_ptr<BYTE, base::FreeDeleter>* buffer) { | 558 std::unique_ptr<BYTE, base::FreeDeleter>* buffer) { |
605 DCHECK_GT(requested_size, 0); | 559 DCHECK_GT(requested_size, 0); |
606 DCHECK(buffer); | 560 DCHECK(buffer); |
607 if (requested_size > kMaximumBufferSize) { | 561 if (requested_size > kMaximumBufferSize) { |
608 buffer->reset(); | 562 buffer->reset(); |
609 return false; | 563 return false; |
610 } | 564 } |
611 | 565 |
612 buffer->reset(reinterpret_cast<BYTE*>( | 566 buffer->reset( |
613 realloc(buffer->release(), requested_size))); | 567 reinterpret_cast<BYTE*>(realloc(buffer->release(), requested_size))); |
614 return buffer != NULL; | 568 return buffer != NULL; |
615 } | 569 } |
616 | 570 |
617 bool GetSystemDirectory(base::string16* path) { | 571 bool GetSystemDirectory(base::string16* path) { |
618 DCHECK(path); | 572 DCHECK(path); |
619 // Return value includes terminating NULL. | 573 // Return value includes terminating NULL. |
620 int buffer_size = ::GetSystemDirectory(NULL, 0); | 574 int buffer_size = ::GetSystemDirectory(NULL, 0); |
621 if (buffer_size == 0) { | 575 if (buffer_size == 0) { |
622 return false; | 576 return false; |
623 } | 577 } |
(...skipping 10 matching lines...) Expand all Loading... |
634 | 588 |
635 if (*path->rbegin() != L'\\') { | 589 if (*path->rbegin() != L'\\') { |
636 path->append(L"\\"); | 590 path->append(L"\\"); |
637 } | 591 } |
638 DCHECK_EQ(L'\\', *path->rbegin()); | 592 DCHECK_EQ(L'\\', *path->rbegin()); |
639 return true; | 593 return true; |
640 } | 594 } |
641 } // namespace | 595 } // namespace |
642 | 596 |
643 } // namespace device | 597 } // namespace device |
OLD | NEW |