Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: net/base/net_util_win.cc

Issue 566243005: Cast: Allow extension to control wifi options on windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: less base Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/base/net_util_unittest.cc ('K') | « net/base/net_util_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <iphlpapi.h> 7 #include <iphlpapi.h>
8 #include <wlanapi.h> 8 #include <wlanapi.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 18 matching lines...) Expand all
29 namespace { 29 namespace {
30 30
31 struct WlanApi { 31 struct WlanApi {
32 typedef DWORD (WINAPI *WlanOpenHandleFunc)( 32 typedef DWORD (WINAPI *WlanOpenHandleFunc)(
33 DWORD, VOID*, DWORD*, HANDLE*); 33 DWORD, VOID*, DWORD*, HANDLE*);
34 typedef DWORD (WINAPI *WlanEnumInterfacesFunc)( 34 typedef DWORD (WINAPI *WlanEnumInterfacesFunc)(
35 HANDLE, VOID*, WLAN_INTERFACE_INFO_LIST**); 35 HANDLE, VOID*, WLAN_INTERFACE_INFO_LIST**);
36 typedef DWORD (WINAPI *WlanQueryInterfaceFunc)( 36 typedef DWORD (WINAPI *WlanQueryInterfaceFunc)(
37 HANDLE, const GUID*, WLAN_INTF_OPCODE, VOID*, DWORD*, VOID**, 37 HANDLE, const GUID*, WLAN_INTF_OPCODE, VOID*, DWORD*, VOID**,
38 WLAN_OPCODE_VALUE_TYPE*); 38 WLAN_OPCODE_VALUE_TYPE*);
39 typedef DWORD (WINAPI *WlanSetInterfaceFunc)(
40 HANDLE, const GUID*, WLAN_INTF_OPCODE, DWORD, const VOID*, VOID*);
39 typedef VOID (WINAPI *WlanFreeMemoryFunc)(VOID*); 41 typedef VOID (WINAPI *WlanFreeMemoryFunc)(VOID*);
40 typedef DWORD (WINAPI *WlanCloseHandleFunc)(HANDLE, VOID*); 42 typedef DWORD (WINAPI *WlanCloseHandleFunc)(HANDLE, VOID*);
41 43
42 WlanApi() : initialized(false) { 44 WlanApi() : initialized(false) {
43 // Use an absolute path to load the DLL to avoid DLL preloading attacks. 45 // Use an absolute path to load the DLL to avoid DLL preloading attacks.
44 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll"; 46 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll";
45 wchar_t path[MAX_PATH] = {0}; 47 wchar_t path[MAX_PATH] = {0};
46 ExpandEnvironmentStrings(kDLL, path, arraysize(path)); 48 ExpandEnvironmentStrings(kDLL, path, arraysize(path));
47 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 49 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
48 if (!module) 50 if (!module)
49 return; 51 return;
50 52
51 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>( 53 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>(
52 ::GetProcAddress(module, "WlanOpenHandle")); 54 ::GetProcAddress(module, "WlanOpenHandle"));
53 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>( 55 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>(
54 ::GetProcAddress(module, "WlanEnumInterfaces")); 56 ::GetProcAddress(module, "WlanEnumInterfaces"));
55 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>( 57 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>(
56 ::GetProcAddress(module, "WlanQueryInterface")); 58 ::GetProcAddress(module, "WlanQueryInterface"));
59 set_interface_func = reinterpret_cast<WlanSetInterfaceFunc>(
60 ::GetProcAddress(module, "WlanSetInterface"));
57 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( 61 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>(
58 ::GetProcAddress(module, "WlanFreeMemory")); 62 ::GetProcAddress(module, "WlanFreeMemory"));
59 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( 63 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>(
60 ::GetProcAddress(module, "WlanCloseHandle")); 64 ::GetProcAddress(module, "WlanCloseHandle"));
61 initialized = open_handle_func && enum_interfaces_func && 65 initialized = open_handle_func && enum_interfaces_func &&
62 query_interface_func && free_memory_func && 66 query_interface_func && free_memory_func &&
63 close_handle_func; 67 close_handle_func;
64 } 68 }
65 69
66 template <typename T> 70 template <typename T>
67 DWORD OpenHandle(DWORD client_version, DWORD* cur_version, T* handle) const { 71 DWORD OpenHandle(DWORD client_version, DWORD* cur_version, T* handle) const {
68 HANDLE temp_handle; 72 HANDLE temp_handle;
69 DWORD result = open_handle_func(client_version, NULL, cur_version, 73 DWORD result = open_handle_func(client_version, NULL, cur_version,
70 &temp_handle); 74 &temp_handle);
71 if (result != ERROR_SUCCESS) 75 if (result != ERROR_SUCCESS)
72 return result; 76 return result;
73 handle->Set(temp_handle); 77 handle->Set(temp_handle);
74 return ERROR_SUCCESS; 78 return ERROR_SUCCESS;
75 } 79 }
76 80
77 HMODULE module; 81 HMODULE module;
78 WlanOpenHandleFunc open_handle_func; 82 WlanOpenHandleFunc open_handle_func;
79 WlanEnumInterfacesFunc enum_interfaces_func; 83 WlanEnumInterfacesFunc enum_interfaces_func;
80 WlanQueryInterfaceFunc query_interface_func; 84 WlanQueryInterfaceFunc query_interface_func;
85 WlanSetInterfaceFunc set_interface_func;
81 WlanFreeMemoryFunc free_memory_func; 86 WlanFreeMemoryFunc free_memory_func;
82 WlanCloseHandleFunc close_handle_func; 87 WlanCloseHandleFunc close_handle_func;
83 bool initialized; 88 bool initialized;
84 }; 89 };
85 90
86 // Converts Windows defined types to NetworkInterfaceType. 91 // Converts Windows defined types to NetworkInterfaceType.
87 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { 92 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) {
88 // Bail out for pre-Vista versions of Windows which are documented to give 93 // Bail out for pre-Vista versions of Windows which are documented to give
89 // inaccurate results like returning Ethernet for WiFi. 94 // inaccurate results like returning Ethernet for WiFi.
90 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx 95 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx
91 if (base::win::GetVersion() < base::win::VERSION_VISTA) 96 if (base::win::GetVersion() < base::win::VERSION_VISTA)
92 return NetworkChangeNotifier::CONNECTION_UNKNOWN; 97 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
93 98
94 NetworkChangeNotifier::ConnectionType type = 99 NetworkChangeNotifier::ConnectionType type =
95 NetworkChangeNotifier::CONNECTION_UNKNOWN; 100 NetworkChangeNotifier::CONNECTION_UNKNOWN;
96 if (ifType == IF_TYPE_ETHERNET_CSMACD) { 101 if (ifType == IF_TYPE_ETHERNET_CSMACD) {
97 type = NetworkChangeNotifier::CONNECTION_ETHERNET; 102 type = NetworkChangeNotifier::CONNECTION_ETHERNET;
98 } else if (ifType == IF_TYPE_IEEE80211) { 103 } else if (ifType == IF_TYPE_IEEE80211) {
99 type = NetworkChangeNotifier::CONNECTION_WIFI; 104 type = NetworkChangeNotifier::CONNECTION_WIFI;
100 } 105 }
101 // TODO(mallinath) - Cellular? 106 // TODO(mallinath) - Cellular?
102 return type; 107 return type;
103 } 108 }
104 109
110 base::LazyInstance<WlanApi>::Leaky lazy_wlanapi =
111 LAZY_INSTANCE_INITIALIZER;
112
113 struct WlanApiHandleTraits {
114 typedef HANDLE Handle;
115
116 static bool CloseHandle(HANDLE handle) {
117 return lazy_wlanapi.Get().close_handle_func(handle, NULL) ==
118 ERROR_SUCCESS;
pauljensen 2014/09/18 14:16:50 Please put this on the previous line.
hubbe 2014/09/18 18:21:05 Done.
119 }
120 static bool IsHandleValid(HANDLE handle) {
121 return base::win::HandleTraits::IsHandleValid(handle);
122 }
123 static HANDLE NullHandle() {
124 return base::win::HandleTraits::NullHandle();
125 }
126 };
127
pauljensen 2014/09/18 14:16:50 Remove the extra line.
hubbe 2014/09/18 18:21:05 Done.
128
129 typedef base::win::GenericScopedHandle<
130 WlanApiHandleTraits,
131 base::win::DummyVerifierTraits> WlanHandle;
132
133 struct WlanApiDeleter {
134 inline void operator()(void* ptr) const {
135 lazy_wlanapi.Get().free_memory_func(ptr);
136 }
137 };
138
105 } // namespace 139 } // namespace
106 140
107 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { 141 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
108 // GetAdaptersAddresses() may require IO operations. 142 // GetAdaptersAddresses() may require IO operations.
109 base::ThreadRestrictions::AssertIOAllowed(); 143 base::ThreadRestrictions::AssertIOAllowed();
110 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; 144 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA;
111 ULONG len = 0; 145 ULONG len = 0;
112 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; 146 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0;
113 // First get number of networks. 147 // First get number of networks.
114 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); 148 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 } 243 }
210 } 244 }
211 245
212 if (ipv6_address.get()) { 246 if (ipv6_address.get()) {
213 networks->push_back(*(ipv6_address.get())); 247 networks->push_back(*(ipv6_address.get()));
214 } 248 }
215 return true; 249 return true;
216 } 250 }
217 251
218 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { 252 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
219 static base::LazyInstance<WlanApi>::Leaky lazy_wlanapi =
220 LAZY_INSTANCE_INITIALIZER;
221
222 struct WlanApiHandleTraits {
223 typedef HANDLE Handle;
224
225 static bool CloseHandle(HANDLE handle) {
226 return lazy_wlanapi.Get().close_handle_func(handle, NULL) ==
227 ERROR_SUCCESS;
228 }
229 static bool IsHandleValid(HANDLE handle) {
230 return base::win::HandleTraits::IsHandleValid(handle);
231 }
232 static HANDLE NullHandle() {
233 return base::win::HandleTraits::NullHandle();
234 }
235 };
236
237 typedef base::win::GenericScopedHandle<
238 WlanApiHandleTraits,
239 base::win::DummyVerifierTraits> WlanHandle;
240
241 struct WlanApiDeleter {
242 inline void operator()(void* ptr) const {
243 lazy_wlanapi.Get().free_memory_func(ptr);
244 }
245 };
246
247 const WlanApi& wlanapi = lazy_wlanapi.Get(); 253 const WlanApi& wlanapi = lazy_wlanapi.Get();
248 if (!wlanapi.initialized) 254 if (!wlanapi.initialized)
249 return WIFI_PHY_LAYER_PROTOCOL_NONE; 255 return WIFI_PHY_LAYER_PROTOCOL_NONE;
250 256
251 WlanHandle client; 257 WlanHandle client;
252 DWORD cur_version = 0; 258 DWORD cur_version = 0;
253 const DWORD kMaxClientVersion = 2; 259 const DWORD kMaxClientVersion = 2;
254 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); 260 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client);
255 if (result != ERROR_SUCCESS) 261 if (result != ERROR_SUCCESS)
256 return WIFI_PHY_LAYER_PROTOCOL_NONE; 262 return WIFI_PHY_LAYER_PROTOCOL_NONE;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 return WIFI_PHY_LAYER_PROTOCOL_B; 305 return WIFI_PHY_LAYER_PROTOCOL_B;
300 case dot11_phy_type_erp: 306 case dot11_phy_type_erp:
301 return WIFI_PHY_LAYER_PROTOCOL_G; 307 return WIFI_PHY_LAYER_PROTOCOL_G;
302 case dot11_phy_type_ht: 308 case dot11_phy_type_ht:
303 return WIFI_PHY_LAYER_PROTOCOL_N; 309 return WIFI_PHY_LAYER_PROTOCOL_N;
304 default: 310 default:
305 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; 311 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
306 } 312 }
307 } 313 }
308 314
315 // Note: There is no need to explicitly set the options back
316 // as the OS will automatically set them back when the WlanHandle
317 // is closed.
318 class WifiOptionSetter : public ScopedWifiOptions {
319 public:
320 WifiOptionSetter(int options) {
321 const WlanApi& wlanapi = lazy_wlanapi.Get();
322 if (!wlanapi.initialized)
323 return;
324
325 DWORD cur_version = 0;
326 const DWORD kMaxClientVersion = 2;
327 DWORD result = wlanapi.OpenHandle(
328 kMaxClientVersion, &cur_version, &client_);
329 if (result != ERROR_SUCCESS)
330 return;
331
332 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL;
333 result = wlanapi.enum_interfaces_func(client_, NULL, &interface_list_ptr);
334 if (result != ERROR_SUCCESS)
335 return;
336 scoped_ptr<WLAN_INTERFACE_INFO_LIST, WlanApiDeleter> interface_list(
337 interface_list_ptr);
338
339 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) {
340 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i];
341 if (options & WIFI_OPTIONS_DISABLE_SCAN) {
342 BOOL data = false;
343 wlanapi.set_interface_func(client_,
344 &info->InterfaceGuid,
345 wlan_intf_opcode_background_scan_enabled,
346 sizeof(data),
347 &data,
348 NULL);
349 }
350 if (options & WIFI_OPTIONS_MEDIA_STREAMING_MODE) {
351 BOOL data = true;
352 wlanapi.set_interface_func(client_,
353 &info->InterfaceGuid,
354 wlan_intf_opcode_media_streaming_mode,
355 sizeof(data),
356 &data,
357 NULL);
358 }
359 }
360 }
361
362 private:
363 WlanHandle client_;
364 };
365
366 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) {
367 return scoped_ptr<ScopedWifiOptions>(
368 new WifiOptionSetter(options));
pauljensen 2014/09/18 14:16:50 Please put this on the previous line.
hubbe 2014/09/18 18:21:05 Done.
369 }
370
309 } // namespace net 371 } // namespace net
OLDNEW
« net/base/net_util_unittest.cc ('K') | « net/base/net_util_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698