From 6f042b779ca016eb2af3caf974dafc5ae4e0748b Mon Sep 17 00:00:00 2001 From: NicolePP Date: Sat, 2 Feb 2019 11:17:41 +0800 Subject: [PATCH 1/3] some update --- pywifi/GUID.py | 101 ++++++++++++++++++++++++++++++++++++++++ pywifi/_wifiutil_win.py | 54 +++++++++++++++++---- pywifi/iface.py | 9 +++- pywifi/profile.py | 1 + pywifi/wifi.py | 9 ++++ 5 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 pywifi/GUID.py diff --git a/pywifi/GUID.py b/pywifi/GUID.py new file mode 100644 index 0000000..198526a --- /dev/null +++ b/pywifi/GUID.py @@ -0,0 +1,101 @@ +from ctypes import * +import sys + +if sys.version_info >= (2, 6): + def binary(obj): + return bytes(obj) +else: + def binary(obj): + return buffer(obj) + +BYTE = c_byte +WORD = c_ushort +DWORD = c_ulong + +_ole32 = oledll.ole32 + +_StringFromCLSID = _ole32.StringFromCLSID +_CoTaskMemFree = windll.ole32.CoTaskMemFree +_ProgIDFromCLSID = _ole32.ProgIDFromCLSID +_CLSIDFromString = _ole32.CLSIDFromString +_CLSIDFromProgID = _ole32.CLSIDFromProgID +_CoCreateGuid = _ole32.CoCreateGuid + +# Note: Comparing GUID instances by comparing their buffers +# is slightly faster than using ole32.IsEqualGUID. + +class GUID(Structure): + _fields_ = [("Data1", DWORD), + ("Data2", WORD), + ("Data3", WORD), + ("Data4", BYTE * 8)] + + def __init__(self, name=None): + if name is not None: + _CLSIDFromString(unicode(name), byref(self)) + + def __repr__(self): + return u'GUID("%s")' % unicode(self) + + def __unicode__(self): + p = c_wchar_p() + _StringFromCLSID(byref(self), byref(p)) + result = p.value + _CoTaskMemFree(p) + return result + __str__ = __unicode__ + + def __cmp__(self, other): + if isinstance(other, GUID): + return cmp(binary(self), binary(other)) + return -1 + + def __nonzero__(self): + return self != GUID_null + + def __eq__(self, other): + return isinstance(other, GUID) and \ + binary(self) == binary(other) + + def __hash__(self): + # We make GUID instances hashable, although they are mutable. + return hash(binary(self)) + + def copy(self): + return GUID(unicode(self)) + + def from_progid(cls, progid): + """Get guid from progid, ... + """ + if hasattr(progid, "_reg_clsid_"): + progid = progid._reg_clsid_ + if isinstance(progid, cls): + return progid + elif isinstance(progid, basestring): + if progid.startswith("{"): + return cls(progid) + inst = cls() + _CLSIDFromProgID(unicode(progid), byref(inst)) + return inst + else: + raise TypeError("Cannot construct guid from %r" % progid) + from_progid = classmethod(from_progid) + + def as_progid(self): + "Convert a GUID into a progid" + progid = c_wchar_p() + _ProgIDFromCLSID(byref(self), byref(progid)) + result = progid.value + _CoTaskMemFree(progid) + return result + + def create_new(cls): + "Create a brand new guid" + guid = cls() + _CoCreateGuid(byref(guid)) + return guid + create_new = classmethod(create_new) + +GUID_null = GUID() + +__all__ = ["GUID"] diff --git a/pywifi/_wifiutil_win.py b/pywifi/_wifiutil_win.py index e81e64b..056c7a5 100755 --- a/pywifi/_wifiutil_win.py +++ b/pywifi/_wifiutil_win.py @@ -6,11 +6,14 @@ import re import platform import time +import binascii import logging from ctypes import * from ctypes.wintypes import * from comtypes import GUID +import chardet + from .const import * from .profile import Profile @@ -333,11 +336,14 @@ def add_network_profile(self, obj, params): params.process_akm() profile_data = {} - profile_data['ssid'] = params.ssid + profile_data['ssid'] = binascii.b2a_hex(params.ssid) if AKM_TYPE_NONE in params.akm: profile_data['auth'] = auth_value_to_str_dict[params.auth] profile_data['encrypt'] = "none" + elif AKM_TYPE_UNKNOWN in params.akm: + profile_data['auth'] = auth_value_to_str_dict[params.auth] + profile_data['encrypt'] = cipher_value_to_str_dict[params.cipher] else: profile_data['auth'] = akm_value_to_str_dict[params.akm[-1]] profile_data['encrypt'] = cipher_value_to_str_dict[params.cipher] @@ -345,14 +351,17 @@ def add_network_profile(self, obj, params): profile_data['key'] = params.key profile_data['protected'] = 'false' - profile_data['profile_name'] = params.ssid + profile_name = params.ssid + if profile_name and (not isinstance(profile_name, unicode)): + profile_name = profile_name.decode(chardet.detect(profile_name)['encoding']) + profile_data['profile_name'] = profile_name xml = """ - {profile_name} + - {ssid} + {ssid} ESS @@ -367,11 +376,19 @@ def add_network_profile(self, obj, params): """ if AKM_TYPE_NONE not in params.akm: - xml += """ - passPhrase - {protected} - {key} + xml += """""" + if params.cipher == CIPHER_TYPE_WEP or len(params.key) == 64: + xml += """networkKey""" + else: + xml += """passPhrase""" + + xml += """{protected} + """ + + if params.cipher == CIPHER_TYPE_WEP: + profile_data['key_index'] = params.keyindex - 1 + xml += """ {key_index}""" xml += """ @@ -394,7 +411,7 @@ def add_network_profile(self, obj, params): buf = create_unicode_buffer(64) self._wlan_reason_code_to_str(reason_code, buf_size, buf) - return params + return (status, buf) def network_profile_name_list(self, obj): """Get AP profile names.""" @@ -459,6 +476,25 @@ def remove_all_network_profiles(self, obj): str_buf = create_unicode_buffer(profile_name) ret = self._wlan_delete_profile(self._handle, obj['guid'], str_buf) self._logger.debug("delete result %d", ret) + + def remove_network_profile(self, obj, profile_name): + """Remove an AP profile.""" + + profile_name_list = self.network_profile_name_list(obj) + + for p_name in profile_name_list: + if profile_name == p_name: + self._logger.debug("delete profile: %s", profile_name) + str_buf = create_unicode_buffer(profile_name) + ret = self._wlan_delete_profile(self._handle, obj['guid'], str_buf) + if ret != ERROR_SUCCESS: + self._logger.debug("Status %d: Delete profile failed", ret) + return False + else: + return True + else: + self._logger.debug('The profile %s to be delete is not viable' % profile_name.decode('gb2312')) + return False def status(self, obj): """Get the wifi interface status.""" diff --git a/pywifi/iface.py b/pywifi/iface.py index b4ed703..2da9564 100755 --- a/pywifi/iface.py +++ b/pywifi/iface.py @@ -67,15 +67,20 @@ def add_network_profile(self, params): return self._wifi_ctrl.add_network_profile(self._raw_obj, params) - def remove_network_profile(self, **kwargs): + def remove_network_profile(self, profile_name): """Remove the specified AP settings.""" - self._wifi_ctrl.remove_network_profile(self._raw_obj, kwargs) + self._wifi_ctrl.remove_network_profile(self._raw_obj, profile_name) def remove_all_network_profiles(self): """Remove all the AP settings.""" self._wifi_ctrl.remove_all_network_profiles(self._raw_obj) + + def network_profile_name_list(self): + """Get all the AP profile names.""" + + return self._wifi_ctrl.network_profile_name_list(self._raw_obj) def network_profiles(self): """Get all the AP profiles.""" diff --git a/pywifi/profile.py b/pywifi/profile.py index e30c30c..7bcd4d5 100755 --- a/pywifi/profile.py +++ b/pywifi/profile.py @@ -16,6 +16,7 @@ def __init__(self): self.ssid = None self.bssid = None self.key = None + self.keyindex = None def process_akm(self): diff --git a/pywifi/wifi.py b/pywifi/wifi.py index 270163a..90e48ba 100755 --- a/pywifi/wifi.py +++ b/pywifi/wifi.py @@ -48,3 +48,12 @@ def interfaces(self): self._logger.error("Can't get wifi interface") return self._ifaces + + def interface(self, guid): + wifi_ctrl = wifiutil.WifiUtil() + for interface in wifi_ctrl.interfaces(): + if str(interface['guid']) == str(guid): + return Interface(interface) + else: + self._logger.error("Can't get wifi interface with GUID: %s" % guid) + raise RuntimeError("Can't get wifi interface with GUID: %s" % guid) From d74495c8003a47e16aa4d4cab80705eee10c87d5 Mon Sep 17 00:00:00 2001 From: NicolePP Date: Sat, 2 Feb 2019 11:18:16 +0800 Subject: [PATCH 2/3] some update --- pywifi/GUID.py | 101 ------------------------------------------------- 1 file changed, 101 deletions(-) delete mode 100644 pywifi/GUID.py diff --git a/pywifi/GUID.py b/pywifi/GUID.py deleted file mode 100644 index 198526a..0000000 --- a/pywifi/GUID.py +++ /dev/null @@ -1,101 +0,0 @@ -from ctypes import * -import sys - -if sys.version_info >= (2, 6): - def binary(obj): - return bytes(obj) -else: - def binary(obj): - return buffer(obj) - -BYTE = c_byte -WORD = c_ushort -DWORD = c_ulong - -_ole32 = oledll.ole32 - -_StringFromCLSID = _ole32.StringFromCLSID -_CoTaskMemFree = windll.ole32.CoTaskMemFree -_ProgIDFromCLSID = _ole32.ProgIDFromCLSID -_CLSIDFromString = _ole32.CLSIDFromString -_CLSIDFromProgID = _ole32.CLSIDFromProgID -_CoCreateGuid = _ole32.CoCreateGuid - -# Note: Comparing GUID instances by comparing their buffers -# is slightly faster than using ole32.IsEqualGUID. - -class GUID(Structure): - _fields_ = [("Data1", DWORD), - ("Data2", WORD), - ("Data3", WORD), - ("Data4", BYTE * 8)] - - def __init__(self, name=None): - if name is not None: - _CLSIDFromString(unicode(name), byref(self)) - - def __repr__(self): - return u'GUID("%s")' % unicode(self) - - def __unicode__(self): - p = c_wchar_p() - _StringFromCLSID(byref(self), byref(p)) - result = p.value - _CoTaskMemFree(p) - return result - __str__ = __unicode__ - - def __cmp__(self, other): - if isinstance(other, GUID): - return cmp(binary(self), binary(other)) - return -1 - - def __nonzero__(self): - return self != GUID_null - - def __eq__(self, other): - return isinstance(other, GUID) and \ - binary(self) == binary(other) - - def __hash__(self): - # We make GUID instances hashable, although they are mutable. - return hash(binary(self)) - - def copy(self): - return GUID(unicode(self)) - - def from_progid(cls, progid): - """Get guid from progid, ... - """ - if hasattr(progid, "_reg_clsid_"): - progid = progid._reg_clsid_ - if isinstance(progid, cls): - return progid - elif isinstance(progid, basestring): - if progid.startswith("{"): - return cls(progid) - inst = cls() - _CLSIDFromProgID(unicode(progid), byref(inst)) - return inst - else: - raise TypeError("Cannot construct guid from %r" % progid) - from_progid = classmethod(from_progid) - - def as_progid(self): - "Convert a GUID into a progid" - progid = c_wchar_p() - _ProgIDFromCLSID(byref(self), byref(progid)) - result = progid.value - _CoTaskMemFree(progid) - return result - - def create_new(cls): - "Create a brand new guid" - guid = cls() - _CoCreateGuid(byref(guid)) - return guid - create_new = classmethod(create_new) - -GUID_null = GUID() - -__all__ = ["GUID"] From c5e7176ad6365ca3fea0ab4e6ae58bde3ab894f2 Mon Sep 17 00:00:00 2001 From: NicolePP Date: Sun, 3 Feb 2019 10:10:06 +0800 Subject: [PATCH 3/3] some update --- pywifi/_wifiutil_win.py | 25 +++++++++++++++++++------ pywifi/const.py | 4 ++++ pywifi/profile.py | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pywifi/_wifiutil_win.py b/pywifi/_wifiutil_win.py index a9fbc30..5514df7 100755 --- a/pywifi/_wifiutil_win.py +++ b/pywifi/_wifiutil_win.py @@ -316,7 +316,7 @@ def connect(self, obj, params): connect_params = WLAN_CONNECTION_PARAMETERS() connect_params.wlanConnectionMode = 0 # Profile - connect_params.dot11BssType = 1 # infra + connect_params.dot11BssType = params.bsstype # infra profile_name = create_unicode_buffer(params.ssid) connect_params.strProfile = profile_name.value @@ -349,9 +349,12 @@ def add_network_profile(self, obj, params): profile_data['encrypt'] = cipher_value_to_str_dict[params.cipher] profile_data['key'] = params.key - profile_data['protected'] = 'false' - profile_name = params.ssid + + if params.bsstype == BSS_TYPE_ADHOC and 1 == CLIENT_VERSION: + profile_name = '%s-adhoc' % params.ssid + else: + profile_name = params.ssid if profile_name and (not isinstance(profile_name, unicode)): profile_name = profile_name.decode(chardet.detect(profile_name)['encoding']) profile_data['profile_name'] = profile_name @@ -363,9 +366,19 @@ def add_network_profile(self, obj, params): {ssid} - - ESS - manual + """ + if params.bsstype == BSS_TYPE_ADHOC: + xml += """ false""" + else: + xml += """ true""" + + xml += """ """ + if params.bsstype == BSS_TYPE_ADHOC: + xml += """IBSS""" + else: + xml += """ESS""" + + xml += """manual diff --git a/pywifi/const.py b/pywifi/const.py index 66b73dc..e554502 100755 --- a/pywifi/const.py +++ b/pywifi/const.py @@ -31,3 +31,7 @@ KEY_TYPE_NETWORKKEY = 0 KEY_TYPE_PASSPHRASE = 1 + +# Define Bss Type +BSS_TYPE_INFRA = 1 +BSS_TYPE_ADHOC = 2 diff --git a/pywifi/profile.py b/pywifi/profile.py index e409a74..a21aded 100755 --- a/pywifi/profile.py +++ b/pywifi/profile.py @@ -18,6 +18,7 @@ def __init__(self): self.bssid = None self.key = None self.keyindex = None + self.bsstype = BSS_TYPE_INFRA # 1 - Infra, 2 - Adhoc def process_akm(self):