Debian Codegraph @

plyer/platforms/win/libs/wifi_defs.py

raw symbols SCIP
1 '''
2 Reference
3 Methods, Structures and Documentation adapted from.
4 https://msdn.microsoft.com/en-us/library/windows/desktop \
5 /ms705945%28v=vs.85%29.aspx
6 '''
7
8 from ctypes import (
9 POINTER,
10 FormatError,
11 Structure,
12 addressof,
13 byref,
14 c_bool,
15 c_char,
16 c_ubyte,
17 c_uint,
18 c_ulong,
19 c_ushort,
20 c_void_p,
21 c_wchar,
22 pointer,
23 windll,
24 )
25 from ctypes.wintypes import DWORD, HANDLE, LPCWSTR, ULONG
26 from sys import exit as sys_exit
27
28
29 def customresize(array, new_size):
30 return (
31 array._type_ * new_size
32 ).from_address(
33 addressof(array)
34 )
35
36
37 wlanapi = windll.LoadLibrary('wlanapi.dll')
38
39
40 class GUID(Structure):
41 _fields_ = [
42 ('Data1', c_ulong),
43 ('Data2', c_ushort),
44 ('Data3', c_ushort),
45 ('Data4', c_ubyte * 8),
46 ]
47
48
49 # The WLAN_INTERFACE_STATE enumerated type indicates the state of an interface.
50 WLAN_INTERFACE_STATE = c_uint
51 (wlan_interface_state_not_ready,
52 wlan_interface_state_connected,
53 wlan_interface_state_ad_hoc_network_formed,
54 wlan_interface_state_disconnecting,
55 wlan_interface_state_disconnected,
56 wlan_interface_state_associating,
57 wlan_interface_state_discovering,
58 wlan_interface_state_authenticating) = map(WLAN_INTERFACE_STATE,
59 range(0, 8))
60
61
62 class WLAN_INTERFACE_INFO(Structure):
63 '''
64 The WLAN_INTERFACE_STATE enumerated type indicates the state of an
65 interface.
66 '''
67 _fields_ = [
68 ("InterfaceGuid", GUID),
69 ("strInterfaceDescription", c_wchar * 256),
70 ("isState", WLAN_INTERFACE_STATE)
71 ]
72
73
74 class WLAN_INTERFACE_INFO_LIST(Structure):
75 '''
76 The WLAN_INTERFACE_INFO_LIST structure contains an array of NIC interface
77 information.
78 '''
79 _fields_ = [
80 ("NumberOfItems", DWORD),
81 ("Index", DWORD),
82 ("InterfaceInfo", WLAN_INTERFACE_INFO * 1)
83 ]
84
85
86 DOT11_MAC_ADDRESS = c_ubyte * 6
87 WLAN_MAX_PHY_TYPE_NUMBER = 0x8
88 DOT11_SSID_MAX_LENGTH = 32
89 WLAN_REASON_CODE = DWORD
90
91 DOT11_BSS_TYPE = c_uint
92 (dot11_BSS_type_infrastructure,
93 dot11_BSS_type_independent,
94 dot11_BSS_type_any) = map(DOT11_BSS_TYPE, range(1, 4))
95
96 # The DOT11_PHY_TYPE enumeration defines an 802.11 PHY and media type.
97 DOT11_PHY_TYPE = c_uint
98 dot11_phy_type_unknown = 0
99 dot11_phy_type_any = 0
100 dot11_phy_type_fhss = 1
101 dot11_phy_type_dsss = 2
102 dot11_phy_type_irbaseband = 3
103 dot11_phy_type_ofdm = 4
104 dot11_phy_type_hrdsss = 5
105 dot11_phy_type_erp = 6
106 dot11_phy_type_ht = 7
107 dot11_phy_type_IHV_start = 0x80000000
108 dot11_phy_type_IHV_end = 0xffffffff
109
110 # The DOT11_AUTH_ALGORITHM enumerated type defines a wireless
111 # LAN authentication algorithm.
112 DOT11_AUTH_ALGORITHM = c_uint
113 DOT11_AUTH_ALGO_80211_OPEN = 1
114 DOT11_AUTH_ALGO_80211_SHARED_KEY = 2
115 DOT11_AUTH_ALGO_WPA = 3
116 DOT11_AUTH_ALGO_WPA_PSK = 4
117 DOT11_AUTH_ALGO_WPA_NONE = 5
118 DOT11_AUTH_ALGO_RSNA = 6
119 DOT11_AUTH_ALGO_RSNA_PSK = 7
120 DOT11_AUTH_ALGO_IHV_START = 0x80000000
121 DOT11_AUTH_ALGO_IHV_END = 0xffffffff
122
123 # The DOT11_CIPHER_ALGORITHM enumerated type defines a cipher
124 # algorithm for data encryption and decryption.
125 DOT11_CIPHER_ALGORITHM = c_uint
126 DOT11_CIPHER_ALGO_NONE = 0x00
127 DOT11_CIPHER_ALGO_WEP40 = 0x01
128 DOT11_CIPHER_ALGO_TKIP = 0x02
129 DOT11_CIPHER_ALGO_CCMP = 0x04
130 DOT11_CIPHER_ALGO_WEP104 = 0x05
131 DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100
132 DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100
133 DOT11_CIPHER_ALGO_WEP = 0x101
134 DOT11_CIPHER_ALGO_IHV_START = 0x80000000
135 DOT11_CIPHER_ALGO_IHV_END = 0xffffffff
136
137
138 class DOT11_SSID(Structure):
139 '''
140 A DOT11_SSID structure contains the SSID of an interface
141 '''
142 _fields_ = [
143 ("SSIDLength", c_ulong),
144 ("SSID", c_char * DOT11_SSID_MAX_LENGTH)
145 ]
146
147
148 # Enumerated type to define the code of connection.
149 WLAN_CONNECTION_MODE = c_uint
150 (wlan_connection_mode_profile,
151 wlan_connection_mode_temporary_profile,
152 wlan_connection_mode_discovery_secure,
153 wlan_connection_mode_discovery_unsecure,
154 wlan_connection_mode_auto,
155 wlan_connection_mode_invalid) = map(WLAN_CONNECTION_MODE, range(0, 6))
156
157
158 class NDIS_OBJECT_HEADER(Structure):
159 '''
160 This Structure packages the object type, version, and size information
161 that is required in many NDIS (Netword Driver interface Specification)
162 Structures.
163 '''
164 _fields_ = [
165 ("Type", c_char),
166 ("Revision", c_char),
167 ("Size", c_ushort)]
168
169
170 class DOT11_BSSID_LIST(Structure):
171 '''
172 The DOT11_BSSID_LIST structure contains a list of basic service set (BSS)
173 identifiers.
174 '''
175 _fields_ = [
176 ("Header", NDIS_OBJECT_HEADER),
177 ("uNumOfEntries", ULONG),
178 ("uTotalNumOfEntries", ULONG),
179 ("BSSIDs", DOT11_MAC_ADDRESS * 1)
180 ]
181
182
183 class WLAN_CONNECTION_PARAMETERS(Structure):
184 '''
185 The WLAN_CONNECTION_PARAMETERS structure specifies the parameters used when
186 using the WlanConnect function.
187 '''
188 _fields_ = [
189 ("wlanConnectionMode", WLAN_CONNECTION_MODE),
190 ("strProfile", LPCWSTR),
191 ("pDot11Ssid", POINTER(DOT11_SSID)),
192 ("pDesiredBssidList", POINTER(DOT11_BSSID_LIST)),
193 ("dot11BssType", DOT11_BSS_TYPE),
194 ("dwFlags", DWORD)]
195
196
197 # The `WlanConnect` attempts to connect to a specific network.
198 WlanConnect = wlanapi.WlanConnect
199 WlanConnect.argtypes = (HANDLE,
200 POINTER(GUID),
201 POINTER(WLAN_CONNECTION_PARAMETERS),
202 c_void_p)
203 WlanConnect.restype = DWORD
204
205 # The `WlanDisconnect` method disconnects an interface from its
206 # current network.
207 WlanDisconnect = wlanapi.WlanDisconnect
208 WlanDisconnect.argtypes = (HANDLE,
209 POINTER(GUID),
210 c_void_p)
211 WlanDisconnect.restype = DWORD
212
213 # Opens a connection to the server.
214 WlanOpenHandle = wlanapi.WlanOpenHandle
215 WlanOpenHandle.argtypes = (DWORD, c_void_p, POINTER(DWORD), POINTER(HANDLE))
216 WlanOpenHandle.restype = DWORD
217
218 # The WlanCloseHandle method closes the connection to the server.
219 WlanCloseHandle = wlanapi.WlanCloseHandle
220 WlanCloseHandle.argtypes = (HANDLE, c_void_p)
221 WlanCloseHandle.restype = DWORD
222
223
224 class WLAN_AVAILABLE_NETWORK(Structure):
225 '''
226 The WLAN_INTERFACE_INFO structure contains information about a wireless
227 LAN interface.
228 '''
229 _fields_ = [
230 ("ProfileName", c_wchar * 256),
231 ("dot11Ssid", DOT11_SSID),
232 ("dot11BssType", DOT11_BSS_TYPE),
233 ("NumberOfBssids", c_ulong),
234 ("NetworkConnectable", c_bool),
235 ("wlanNotConnectableReason", WLAN_REASON_CODE),
236 ("NumberOfPhyTypes", c_ulong),
237 ("dot11PhyTypes", DOT11_PHY_TYPE * WLAN_MAX_PHY_TYPE_NUMBER),
238 ("MorePhyTypes", c_bool),
239 ("wlanSignalQuality", c_ulong),
240 ("SecurityEnabled", c_bool),
241 ("dot11DefaultAuthAlgorithm", DOT11_AUTH_ALGORITHM),
242 ("dot11DefaultCipherAlgorithm", DOT11_CIPHER_ALGORITHM),
243 ("Flags", DWORD),
244 ("Reserved", DWORD)]
245
246
247 class WLAN_AVAILABLE_NETWORK_LIST(Structure):
248 '''
249 The WLAN_INTERFACE_INFO_LIST structure contains an array of NIC
250 interface information.
251 '''
252 _fields_ = [
253 ("NumberOfItems", DWORD),
254 ("Index", DWORD),
255 ("Network", WLAN_AVAILABLE_NETWORK * 1)]
256
257
258 # The WlanEnumInterfaces function enumerates all of the wireless LAN interfaces
259 # currently enabled on the local computer.
260 WlanEnumInterfaces = wlanapi.WlanEnumInterfaces
261 WlanEnumInterfaces.argtypes = (HANDLE,
262 c_void_p,
263 POINTER(POINTER(WLAN_INTERFACE_INFO_LIST)))
264 WlanEnumInterfaces.restype = DWORD
265
266 # The WlanGetAvailableNetworkList function retrieves the list of available
267 # networks on a wireless LAN interface.
268 WlanGetAvailableNetworkList = wlanapi.WlanGetAvailableNetworkList
269 WlanGetAvailableNetworkList.argtypes = (HANDLE,
270 POINTER(GUID),
271 DWORD,
272 c_void_p,
273 POINTER(POINTER(
274 WLAN_AVAILABLE_NETWORK_LIST)))
275 WlanGetAvailableNetworkList.restype = DWORD
276
277 # The WlanFreeMemory function frees memory. Any memory returned from Native
278 # Wifi functions must be freed.
279 WlanFreeMemory = wlanapi.WlanFreeMemory
280 WlanFreeMemory.argtypes = [c_void_p]
281
282 wireless_interfaces = None
283 available = None
284 _dict = {}
285
286 # Private methods.
287
288
289 def _connect(network, parameters):
290 '''
291 Attempts to connect to a specific network.
292 '''
293 global _dict
294 wireless_interface = _dict[network]
295
296 wcp = WLAN_CONNECTION_PARAMETERS()
297 connection_mode = parameters['connection_mode']
298 wcp.wlanConnectionMode = WLAN_CONNECTION_MODE(connection_mode)
299
300 if connection_mode == 0 or connection_mode == 1:
301 wcp.strProfile = LPCWSTR(parameters["profile"])
302 else:
303 wcp.strProfile = None
304
305 dot11Ssid = DOT11_SSID()
306 try:
307 dot11Ssid.SSID = parameters["ssid"]
308 dot11Ssid.SSIDLength = len(parameters["ssid"])
309 except KeyError:
310 dot11Ssid.SSID = network
311 dot11Ssid.SSIDLength = len(network)
312 wcp.pDot11Ssid = pointer(dot11Ssid)
313
314 dot11bssid = DOT11_BSSID_LIST()
315 bssid = parameters["bssidList"]
316 dot11bssid.Header = bssid['Header']
317 dot11bssid.uNumOfEntries = bssid['uNumOfEntries']
318 dot11bssid.uTotalNumOfEntries = bssid['uTotalNumOfEntries']
319 dot11bssid.BSSIDs = bssid['BSSIDs']
320
321 wcp.pDesiredBssidList = pointer(dot11bssid)
322
323 bssType = parameters["bssType"]
324 wcp.dot11BssType = DOT11_BSS_TYPE(bssType)
325
326 wcp.dwFlags = DWORD(parameters["flags"])
327
328 NegotiatedVersion = DWORD()
329 ClientHandle = HANDLE()
330
331 wlan = WlanOpenHandle(1,
332 None,
333 byref(NegotiatedVersion),
334 byref(ClientHandle))
335 if wlan:
336 sys_exit(FormatError(wlan))
337 pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
338 wlan = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
339 if wlan:
340 sys_exit(FormatError(wlan))
341
342 try:
343 wlan = WlanConnect(ClientHandle,
344 wireless_interface,
345 wcp,
346 None)
347 if wlan:
348 sys_exit(FormatError(wlan))
349 WlanCloseHandle(ClientHandle, None)
350 finally:
351 WlanFreeMemory(pInterfaceList)
352
353
354 def _disconnect():
355 '''
356 To disconnect an interface form the current network.
357 '''
358 NegotiatedVersion = DWORD()
359 ClientHandle = HANDLE()
360
361 wlan = WlanOpenHandle(
362 1,
363 None,
364 byref(NegotiatedVersion),
365 byref(ClientHandle)
366 )
367 if wlan:
368 sys_exit(FormatError(wlan))
369
370 pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
371
372 wlan = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
373 if wlan:
374 sys_exit(FormatError(wlan))
375
376 result = None
377 try:
378 ifaces = customresize(
379 pInterfaceList.contents.InterfaceInfo,
380 pInterfaceList.contents.NumberOfItems
381 )
382
383 # find each available network for each interface
384 for iface in ifaces:
385 wlan = WlanDisconnect(
386 ClientHandle,
387 byref(iface.InterfaceGuid),
388 None
389 )
390 if wlan:
391 sys_exit(FormatError(wlan))
392 WlanCloseHandle(ClientHandle, None)
393
394 finally:
395 WlanFreeMemory(pInterfaceList)
396 result = get_available_wifi()
397
398 return result
399
400
401 def _start_scanning():
402 '''
403 Private method for scanning and returns the available devices.
404 '''
405 global available
406 global wireless_interfaces
407 NegotiatedVersion = DWORD()
408 ClientHandle = HANDLE()
409
410 wlan = WlanOpenHandle(
411 1,
412 None,
413 byref(NegotiatedVersion),
414 byref(ClientHandle)
415 )
416
417 if wlan:
418 sys_exit(FormatError(wlan))
419
420 # find all wireless network interfaces
421 pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
422 wlan = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
423 if wlan:
424 sys_exit(FormatError(wlan))
425
426 result = None
427 try:
428 ifaces = customresize(
429 pInterfaceList.contents.InterfaceInfo,
430 pInterfaceList.contents.NumberOfItems
431 )
432
433 # find each available network for each interface
434 wireless_interfaces = ifaces
435 for iface in ifaces:
436 pAvailableNetworkList = pointer(WLAN_AVAILABLE_NETWORK_LIST())
437 wlan = WlanGetAvailableNetworkList(
438 ClientHandle,
439 byref(iface.InterfaceGuid),
440 0,
441 None,
442 byref(pAvailableNetworkList)
443 )
444
445 if wlan:
446 sys_exit(FormatError(wlan))
447
448 try:
449 avail_net_list = pAvailableNetworkList.contents
450 networks = customresize(
451 avail_net_list.Network,
452 avail_net_list.NumberOfItems
453 )
454
455 # Assigning the value of networks to the global variable
456 # `available`, so it could be used in other methods.
457 available = networks
458 _make_dict()
459 wlan = WlanDisconnect(
460 ClientHandle,
461 byref(iface.InterfaceGuid),
462 None
463 )
464
465 if wlan:
466 sys_exit(FormatError(wlan))
467 WlanCloseHandle(ClientHandle, None)
468
469 finally:
470 WlanFreeMemory(pAvailableNetworkList)
471
472 finally:
473 WlanFreeMemory(pInterfaceList)
474 result = get_available_wifi()
475
476 return result
477
478
479 def _get_network_info(name):
480 '''
481 returns the list of the network selected in a dict.
482 '''
483 global available
484 global _dict
485
486 net = _dict[name]
487 dot11BssType = net.dot11BssType
488 dot11DefaultAuthAlgorithm = net.dot11DefaultAuthAlgorithm
489 dot11DefaultCipherAlgorithm = net.dot11DefaultCipherAlgorithm
490 dot11PhyTypes = net.dot11PhyTypes[0]
491 dot11Ssid = net.dot11Ssid
492 wlanNotConnectableReason = net.wlanNotConnectableReason
493 wlanSignalQuality = net.wlanSignalQuality
494 return {"dot11BssType": dot11BssType,
495 "dot11DefaultAuthAlgorithm": dot11DefaultAuthAlgorithm,
496 "dot11DefaultCipherAlgorithm": dot11DefaultCipherAlgorithm,
497 "dot11PhyTypes": dot11PhyTypes,
498 "SSID": dot11Ssid.SSID,
499 "SSIDLength": dot11Ssid.SSIDLength,
500 "wlanNotConnectableReason": wlanNotConnectableReason,
501 "wlanSignalQuality": wlanSignalQuality}
502
503
504 def _make_dict():
505 '''
506 Prepares a dict so it could store network information.
507 '''
508 global available
509 global _dict
510 _dict = {}
511 for network in available:
512 _dict[network.dot11Ssid.SSID.decode('utf-8')] = network
513
514
515 def _get_available_wifi():
516 '''
517 returns the available wifi networks.
518 '''
519 global _dict
520 return _dict
521
522
523 def _is_enabled():
524 '''
525 Reason for returning true is explained in widi facade.
526 /plyer/facades/wifi.py
527 '''
528 return True
529
530 # public methods.
531
532
533 def is_enabled():
534 '''
535 calls private method `_is_enabled` and returns the result.
536 '''
537 return _is_enabled()
538
539
540 def connect(network, parameters):
541 '''
542 Connect to a network with following parameters.
543 '''
544 _connect(network=network, parameters=parameters)
545
546
547 def disconnect():
548 '''
549 Disconnect from a network.
550 '''
551 _disconnect()
552
553
554 def start_scanning():
555 '''
556 Start scanning for available wifi networks available.
557 '''
558 return _start_scanning()
559
560
561 def get_network_info(name):
562 '''
563 return the wifi network info.
564 '''
565 return _get_network_info(name=name)
566
567
568 def get_available_wifi():
569 '''
570 return the available wifi networks available
571 '''
572 return _get_available_wifi()