summaryrefslogtreecommitdiffstats
path: root/netwerk/wifi/win_wlanLibrary.cpp
blob: 8e5ad00b0c15447a227d7ee761c80f8a0c606cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "win_wlanLibrary.h"

// Moz headers (alphabetical)

WinWLANLibrary* WinWLANLibrary::Load() {
  WinWLANLibrary* ret = new WinWLANLibrary();
  if (!ret) {
    return nullptr;
  }

  if (!ret->Initialize()) {
    delete ret;
    return nullptr;
  }

  return ret;
}

WinWLANLibrary::WinWLANLibrary()
    : mWlanLibrary(nullptr),
      mWlanHandle(nullptr),
      mWlanEnumInterfacesPtr(nullptr),
      mWlanGetNetworkBssListPtr(nullptr),
      mWlanFreeMemoryPtr(nullptr),
      mWlanCloseHandlePtr(nullptr),
      mWlanOpenHandlePtr(nullptr),
      mWlanRegisterNotificationPtr(nullptr),
      mWlanScanPtr(nullptr) {}

HANDLE
WinWLANLibrary::GetWLANHandle() const { return mWlanHandle; }

decltype(::WlanEnumInterfaces)* WinWLANLibrary::GetWlanEnumInterfacesPtr()
    const {
  return mWlanEnumInterfacesPtr;
}

decltype(::WlanGetNetworkBssList)* WinWLANLibrary::GetWlanGetNetworkBssListPtr()
    const {
  return mWlanGetNetworkBssListPtr;
}

decltype(::WlanFreeMemory)* WinWLANLibrary::GetWlanFreeMemoryPtr() const {
  return mWlanFreeMemoryPtr;
}

decltype(::WlanCloseHandle)* WinWLANLibrary::GetWlanCloseHandlePtr() const {
  return mWlanCloseHandlePtr;
}

decltype(::WlanOpenHandle)* WinWLANLibrary::GetWlanOpenHandlePtr() const {
  return mWlanOpenHandlePtr;
}

decltype(::WlanRegisterNotification)*
WinWLANLibrary::GetWlanRegisterNotificationPtr() const {
  return mWlanRegisterNotificationPtr;
}

decltype(::WlanScan)* WinWLANLibrary::GetWlanScanPtr() const {
  return mWlanScanPtr;
}

bool WinWLANLibrary::Initialize() {
  mWlanLibrary = LoadLibraryW(L"Wlanapi.dll");
  if (!mWlanLibrary) {
    return false;
  }

  mWlanOpenHandlePtr = (decltype(::WlanOpenHandle)*)GetProcAddress(
      mWlanLibrary, "WlanOpenHandle");
  mWlanEnumInterfacesPtr = (decltype(::WlanEnumInterfaces)*)GetProcAddress(
      mWlanLibrary, "WlanEnumInterfaces");
  mWlanRegisterNotificationPtr =
      (decltype(::WlanRegisterNotification)*)GetProcAddress(
          mWlanLibrary, "WlanRegisterNotification");
  mWlanScanPtr =
      (decltype(::WlanScan)*)GetProcAddress(mWlanLibrary, "WlanScan");

  mWlanFreeMemoryPtr = (decltype(::WlanFreeMemory)*)GetProcAddress(
      mWlanLibrary, "WlanFreeMemory");
  mWlanCloseHandlePtr = (decltype(::WlanCloseHandle)*)GetProcAddress(
      mWlanLibrary, "WlanCloseHandle");
  mWlanGetNetworkBssListPtr =
      (decltype(::WlanGetNetworkBssList)*)GetProcAddress(
          mWlanLibrary, "WlanGetNetworkBssList");

  if (!mWlanOpenHandlePtr || !mWlanEnumInterfacesPtr ||
      !mWlanRegisterNotificationPtr || !mWlanGetNetworkBssListPtr ||
      !mWlanScanPtr || !mWlanFreeMemoryPtr || !mWlanCloseHandlePtr) {
    return false;
  }

  // Get the handle to the WLAN API.
  DWORD negotiated_version;
  // We could be executing on either Windows XP or Windows Vista, so use the
  // lower version of the client WLAN API. It seems that the negotiated version
  // is the Vista version irrespective of what we pass!
  static const int kXpWlanClientVersion = 1;
  if (ERROR_SUCCESS != (*mWlanOpenHandlePtr)(kXpWlanClientVersion, nullptr,
                                             &negotiated_version,
                                             &mWlanHandle)) {
    return false;
  }

  return true;
}

WinWLANLibrary::~WinWLANLibrary() {
  if (mWlanLibrary) {
    if (mWlanHandle) {
      (*mWlanCloseHandlePtr)(mWlanLibrary, mWlanHandle);
      mWlanHandle = nullptr;
    }
    ::FreeLibrary(mWlanLibrary);
    mWlanLibrary = nullptr;
  }
}