WlanLibrary.cpp (3908B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "WlanLibrary.h" 6 7 #include "mozilla/Logging.h" 8 9 // Moz headers (alphabetical) 10 namespace mozilla { 11 extern LazyLogModule gWifiScannerLog; 12 } 13 #define WIFISCAN_LOG(args) \ 14 MOZ_LOG(mozilla::gWifiScannerLog, mozilla::LogLevel::Debug, args) 15 16 WinWLANLibrary* WinWLANLibrary::Load() { 17 WinWLANLibrary* ret = new WinWLANLibrary(); 18 if (!ret) { 19 return nullptr; 20 } 21 22 if (!ret->Initialize()) { 23 delete ret; 24 return nullptr; 25 } 26 27 return ret; 28 } 29 30 HANDLE 31 WinWLANLibrary::GetWLANHandle() const { return mWlanHandle; } 32 33 decltype(::WlanEnumInterfaces)* WinWLANLibrary::GetWlanEnumInterfacesPtr() 34 const { 35 return mWlanEnumInterfacesPtr; 36 } 37 38 decltype(::WlanGetNetworkBssList)* WinWLANLibrary::GetWlanGetNetworkBssListPtr() 39 const { 40 return mWlanGetNetworkBssListPtr; 41 } 42 43 decltype(::WlanFreeMemory)* WinWLANLibrary::GetWlanFreeMemoryPtr() const { 44 return mWlanFreeMemoryPtr; 45 } 46 47 decltype(::WlanCloseHandle)* WinWLANLibrary::GetWlanCloseHandlePtr() const { 48 return mWlanCloseHandlePtr; 49 } 50 51 decltype(::WlanOpenHandle)* WinWLANLibrary::GetWlanOpenHandlePtr() const { 52 return mWlanOpenHandlePtr; 53 } 54 55 decltype(::WlanRegisterNotification)* 56 WinWLANLibrary::GetWlanRegisterNotificationPtr() const { 57 return mWlanRegisterNotificationPtr; 58 } 59 60 decltype(::WlanScan)* WinWLANLibrary::GetWlanScanPtr() const { 61 return mWlanScanPtr; 62 } 63 64 bool WinWLANLibrary::Initialize() { 65 mWlanLibrary = LoadLibraryW(L"Wlanapi.dll"); 66 if (!mWlanLibrary) { 67 WIFISCAN_LOG( 68 ("WinWLANLibrary::Initialize failed - couldn't open wlanapi.dll")); 69 return false; 70 } 71 72 mWlanOpenHandlePtr = (decltype(::WlanOpenHandle)*)GetProcAddress( 73 mWlanLibrary, "WlanOpenHandle"); 74 mWlanEnumInterfacesPtr = (decltype(::WlanEnumInterfaces)*)GetProcAddress( 75 mWlanLibrary, "WlanEnumInterfaces"); 76 mWlanRegisterNotificationPtr = 77 (decltype(::WlanRegisterNotification)*)GetProcAddress( 78 mWlanLibrary, "WlanRegisterNotification"); 79 mWlanScanPtr = 80 (decltype(::WlanScan)*)GetProcAddress(mWlanLibrary, "WlanScan"); 81 82 mWlanFreeMemoryPtr = (decltype(::WlanFreeMemory)*)GetProcAddress( 83 mWlanLibrary, "WlanFreeMemory"); 84 mWlanCloseHandlePtr = (decltype(::WlanCloseHandle)*)GetProcAddress( 85 mWlanLibrary, "WlanCloseHandle"); 86 mWlanGetNetworkBssListPtr = 87 (decltype(::WlanGetNetworkBssList)*)GetProcAddress( 88 mWlanLibrary, "WlanGetNetworkBssList"); 89 90 if (!mWlanOpenHandlePtr || !mWlanEnumInterfacesPtr || 91 !mWlanRegisterNotificationPtr || !mWlanGetNetworkBssListPtr || 92 !mWlanScanPtr || !mWlanFreeMemoryPtr || !mWlanCloseHandlePtr) { 93 WIFISCAN_LOG( 94 ("WinWLANLibrary::Initialize failed to find functions in wlanapi.dll")); 95 return false; 96 } 97 98 // Get the handle to the WLAN API. 99 DWORD negotiated_version; 100 // We could be executing on either Windows XP or Windows Vista, so use the 101 // lower version of the client WLAN API. It seems that the negotiated version 102 // is the Vista version irrespective of what we pass! 103 static const int kXpWlanClientVersion = 1; 104 if (ERROR_SUCCESS != (*mWlanOpenHandlePtr)(kXpWlanClientVersion, nullptr, 105 &negotiated_version, 106 &mWlanHandle)) { 107 WIFISCAN_LOG(("WinWLANLibrary::Initialize: WlanOpenHandle failed")); 108 return false; 109 } 110 111 WIFISCAN_LOG(("WinWLANLibrary::Initialize succeeded - version is %lu", 112 negotiated_version)); 113 return true; 114 } 115 116 WinWLANLibrary::~WinWLANLibrary() { 117 if (mWlanLibrary) { 118 if (mWlanHandle) { 119 (*mWlanCloseHandlePtr)(mWlanLibrary, mWlanHandle); 120 mWlanHandle = nullptr; 121 } 122 ::FreeLibrary(mWlanLibrary); 123 mWlanLibrary = nullptr; 124 } 125 } 126 127 #undef WIFISCAN_LOG