From c23708b44443794b0ccdc10347202f6fae410f2a Mon Sep 17 00:00:00 2001 From: drf Date: Sun, 1 Mar 2009 16:01:21 +0100 Subject: [PATCH 1/7] Starting implementation of current bitrate retrieval --- wicd/backends/be-ioctl.py | 14 ++++++++++++++ wicd/networking.py | 11 +++++++++++ wicd/wicd-daemon.py | 5 +++++ wicd/wnettools.py | 17 +++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/wicd/backends/be-ioctl.py b/wicd/backends/be-ioctl.py index b4681a0..c799874 100644 --- a/wicd/backends/be-ioctl.py +++ b/wicd/backends/be-ioctl.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ ioctl Network interface control tools for wicd. @@ -479,6 +480,19 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): raw_addr = struct.unpack("xxBBBBBB", result[:8]) return "%02X:%02X:%02X:%02X:%02X:%02X" % raw_addr + def GetCurrentBitrate(self, iwconfig=None): + """ Get the current bitrate for the interface. """ + if not self.iface: return "" + data = (self.iface + '\0' * 32)[:32] + try: + result = fcntl.ioctl(self.sock.fileno(), SIOCGIWAP, data)[16:] + except IOError, e: + if self.verbose: + print "SIOCGIWAP failed: " + str(e) + return "" + raw_addr = struct.unpack("xxBBBBBB", result[:8]) + return "%02X:%02X:%02X:%02X:%02X:%02X" % raw_addr + def GetSignalStrength(self, iwconfig=None): """ Get the signal strength of the current network. diff --git a/wicd/networking.py b/wicd/networking.py index c308790..5f97455 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ networking - Provides wrappers for common network operations @@ -624,6 +625,16 @@ class Wireless(Controller): """ return self.wiface.GetBSSID() + def GetCurrentBitrate(self): + """ Get the current bitrate of the interface. + + Returns: + The bitrate of the active access point as a string, or + None the bitrate can't be found. + + """ + return self.wiface.GetCurrentBitrate() + def GetIwconfig(self): """ Get the out of iwconfig. """ return self.wiface.GetIwconfig() diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index c4015ce..ecbf23f 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ wicd - wireless connection daemon implementation. @@ -995,6 +996,10 @@ class WirelessDaemon(dbus.service.Object): def GetApBssid(self): return self.wifi.GetBSSID() + @dbus.service.method('org.wicd.daemon.wireless') + def GetCurrentBitrate(self): + return self.wifi.GetCurrentBitrate() + @dbus.service.method('org.wicd.daemon.wireless') def CreateAdHocNetwork(self, essid, channel, ip, enctype, key, encused, ics): diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 774bb6d..c1ee8de 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ Network interface control tools for wicd. @@ -59,6 +60,7 @@ wpa2_pattern = re.compile('(WPA2)', __re_mode) #iwconfig-only regular expressions. ip_pattern = re.compile(r'inet [Aa]d?dr[^.]*:([^.]*\.[^.]*\.[^.]*\.[0-9]*)',re.S) bssid_pattern = re.compile('.*Access Point: (([0-9A-Z]{2}:){5}[0-9A-Z]{2})', __re_mode) +bitrate_pattern = re.compile('.*Bit Rate=(.*?)s', __re_mode) # Regular expressions for wpa_cli output auth_pattern = re.compile('.*wpa_state=(.*?)\n', __re_mode) @@ -1056,6 +1058,9 @@ class BaseWirelessInterface(BaseInterface): freq = misc.RunRegex(freq_pattern, cell) ap['channel'] = self._FreqToChannel(freq) + # Bit Rate + ap['bitrate'] = misc.RunRegex(bitrate_pattern, cell) + # BSSID ap['bssid'] = misc.RunRegex(ap_mac_pattern, cell) @@ -1180,6 +1185,18 @@ class BaseWirelessInterface(BaseInterface): bssid = misc.RunRegex(bssid_pattern, output) return bssid + def GetCurrentBitrate(self, iwconfig=None): + """ Get the MAC address for the interface. """ + if not iwconfig: + cmd = 'iwconfig ' + self.iface + if self.verbose: print cmd + output = misc.Run(cmd) + else: + output = iwconfig + + bitrate = misc.RunRegex(bitrate_pattern, output) + return bitrate + 's' + def _get_link_quality(self, output): """ Parse out the link quality from iwlist scan or iwconfig output. """ try: From db80f31e03a22256e856f6eb643be3de531cbd64 Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Sun, 1 Mar 2009 16:46:58 +0100 Subject: [PATCH 2/7] Adding per-channel bitrate information as a string --- wicd/wnettools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wicd/wnettools.py b/wicd/wnettools.py index c1ee8de..49ae1af 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -50,6 +50,7 @@ channel_pattern = re.compile('.*Channel:? ?(\d\d?)', __re_mode) strength_pattern = re.compile('.*Quality:?=? ?(\d+)\s*/?\s*(\d*)', __re_mode) altstrength_pattern = re.compile('.*Signal level:?=? ?(\d+)\s*/?\s*(\d*)', __re_mode) signaldbm_pattern = re.compile('.*Signal level:?=? ?(-\d\d*)', __re_mode) +bitrates_pattern = re.compile('.*Bit Rates:(.*?)E', __re_mode) mode_pattern = re.compile('.*Mode:(.*?)\n', __re_mode) freq_pattern = re.compile('.*Frequency:(.*?)\n', __re_mode) wep_pattern = re.compile('.*Encryption key:(.*?)\n', __re_mode) @@ -1059,7 +1060,7 @@ class BaseWirelessInterface(BaseInterface): ap['channel'] = self._FreqToChannel(freq) # Bit Rate - ap['bitrate'] = misc.RunRegex(bitrate_pattern, cell) + ap['bitrates'] = misc.RunRegex(bitrates_pattern, cell).replace('\n', '; ').replace(' ', '')[:-2] # BSSID ap['bssid'] = misc.RunRegex(ap_mac_pattern, cell) From 2898adec48009abad58847d3c2b4d783a7672a3b Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Sun, 1 Mar 2009 19:22:30 +0100 Subject: [PATCH 3/7] Adding the possibility of a custom iwconfig --- wicd/networking.py | 4 ++-- wicd/wicd-daemon.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/wicd/networking.py b/wicd/networking.py index 5f97455..c6b81ba 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -625,7 +625,7 @@ class Wireless(Controller): """ return self.wiface.GetBSSID() - def GetCurrentBitrate(self): + def GetCurrentBitrate(self, iwconfig): """ Get the current bitrate of the interface. Returns: @@ -633,7 +633,7 @@ class Wireless(Controller): None the bitrate can't be found. """ - return self.wiface.GetCurrentBitrate() + return self.wiface.GetCurrentBitrate(iwconfig) def GetIwconfig(self): """ Get the out of iwconfig. """ diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index ecbf23f..7d75a27 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -997,8 +997,8 @@ class WirelessDaemon(dbus.service.Object): return self.wifi.GetBSSID() @dbus.service.method('org.wicd.daemon.wireless') - def GetCurrentBitrate(self): - return self.wifi.GetCurrentBitrate() + def GetCurrentBitrate(self, iwconfig): + return self.wifi.GetCurrentBitrate(iwconfig) @dbus.service.method('org.wicd.daemon.wireless') def CreateAdHocNetwork(self, essid, channel, ip, enctype, key, encused, @@ -1130,6 +1130,12 @@ class WirelessDaemon(dbus.service.Object): ip = self.wifi.GetIP(ifconfig) return ip + @dbus.service.method('org.wicd.daemon.wireless') + def GetOperationalMode(self, ifconfig=""): + """ Returns the IP associated with the wireless interface. """ + ip = self.wifi.GetOperationalMode(ifconfig) + return ip + @dbus.service.method('org.wicd.daemon.wireless') def CheckWirelessConnectingMessage(self): """ Returns the wireless interface's status message. """ From 9213c7c333049e2b5a362fb1a3b9186d1426af46 Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Sun, 1 Mar 2009 19:31:34 +0100 Subject: [PATCH 4/7] Adding GetOperationalMode() to determine op mode of an interface --- wicd/networking.py | 10 ++++++++++ wicd/wicd-daemon.py | 5 +++++ wicd/wnettools.py | 13 +++++++++++++ 3 files changed, 28 insertions(+) diff --git a/wicd/networking.py b/wicd/networking.py index c6b81ba..609e50a 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -635,6 +635,16 @@ class Wireless(Controller): """ return self.wiface.GetCurrentBitrate(iwconfig) + def GetOperationalMode(self, iwconfig): + """ Get the current operational mode of the interface. + + Returns: + The operational mode of the interface as a string, or + None if the operational mode can't be found. + + """ + return self.wiface.GetOperationalMode(iwconfig) + def GetIwconfig(self): """ Get the out of iwconfig. """ return self.wiface.GetIwconfig() diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index 7d75a27..33958c3 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -1000,6 +1000,11 @@ class WirelessDaemon(dbus.service.Object): def GetCurrentBitrate(self, iwconfig): return self.wifi.GetCurrentBitrate(iwconfig) + @dbus.service.method('org.wicd.daemon.wireless') + def GetOperationalMode(self, iwconfig): + """ Returns the operational mode for the iwconfig parameter """ + return misc.to_unicode(self.wifi.GetOperationalMode()) + @dbus.service.method('org.wicd.daemon.wireless') def CreateAdHocNetwork(self, essid, channel, ip, enctype, key, encused, ics): diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 49ae1af..8a7f3f0 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -62,6 +62,7 @@ wpa2_pattern = re.compile('(WPA2)', __re_mode) ip_pattern = re.compile(r'inet [Aa]d?dr[^.]*:([^.]*\.[^.]*\.[^.]*\.[0-9]*)',re.S) bssid_pattern = re.compile('.*Access Point: (([0-9A-Z]{2}:){5}[0-9A-Z]{2})', __re_mode) bitrate_pattern = re.compile('.*Bit Rate=(.*?)s', __re_mode) +opmode_pattern = re.compile('.*Mode:(.*?) ', __re_mode) # Regular expressions for wpa_cli output auth_pattern = re.compile('.*wpa_state=(.*?)\n', __re_mode) @@ -1198,6 +1199,18 @@ class BaseWirelessInterface(BaseInterface): bitrate = misc.RunRegex(bitrate_pattern, output) return bitrate + 's' + def GetOperationalMode(self, iwconfig=None): + """ Get the MAC address for the interface. """ + if not iwconfig: + cmd = 'iwconfig ' + self.iface + if self.verbose: print cmd + output = misc.Run(cmd) + else: + output = iwconfig + + opmode = misc.RunRegex(opmode_pattern, output) + return opmode + def _get_link_quality(self, output): """ Parse out the link quality from iwlist scan or iwconfig output. """ try: From 23cb084036998eb538e2a6ec1e79bd557a0b3361 Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Sun, 1 Mar 2009 20:19:16 +0100 Subject: [PATCH 5/7] Adding available auth methods --- wicd/networking.py | 10 ++++++++++ wicd/wicd-daemon.py | 7 ++++++- wicd/wnettools.py | 13 +++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/wicd/networking.py b/wicd/networking.py index 609e50a..ef4208e 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -645,6 +645,16 @@ class Wireless(Controller): """ return self.wiface.GetOperationalMode(iwconfig) + def GetAvailableAuthMethods(self, iwlistauth): + """ Get the available authentication methods for the interface. + + Returns: + The available authentication methods of the interface as a string, or + None if the auth methods can't be found. + + """ + return self.wiface.GetAvailableAuthMethods(iwlistauth) + def GetIwconfig(self): """ Get the out of iwconfig. """ return self.wiface.GetIwconfig() diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index 33958c3..0f4704a 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -1003,7 +1003,12 @@ class WirelessDaemon(dbus.service.Object): @dbus.service.method('org.wicd.daemon.wireless') def GetOperationalMode(self, iwconfig): """ Returns the operational mode for the iwconfig parameter """ - return misc.to_unicode(self.wifi.GetOperationalMode()) + return misc.to_unicode(self.wifi.GetOperationalMode(iwconfig)) + + @dbus.service.method('org.wicd.daemon.wireless') + def GetAvailableAuthMethods(self, iwlistauth): + """ Returns the operational mode for the iwlistauth parameter """ + return misc.to_unicode(self.wifi.GetAvailableAuthMethods(iwlistauth)) @dbus.service.method('org.wicd.daemon.wireless') def CreateAdHocNetwork(self, essid, channel, ip, enctype, key, encused, diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 8a7f3f0..3bf22d0 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -63,6 +63,7 @@ ip_pattern = re.compile(r'inet [Aa]d?dr[^.]*:([^.]*\.[^.]*\.[^.]*\.[0-9]*)',re.S bssid_pattern = re.compile('.*Access Point: (([0-9A-Z]{2}:){5}[0-9A-Z]{2})', __re_mode) bitrate_pattern = re.compile('.*Bit Rate=(.*?)s', __re_mode) opmode_pattern = re.compile('.*Mode:(.*?) ', __re_mode) +authmethods_pattern = re.compile('.*Authentication capabilities :\n(.*?)Current', __re_mode) # Regular expressions for wpa_cli output auth_pattern = re.compile('.*wpa_state=(.*?)\n', __re_mode) @@ -1211,6 +1212,18 @@ class BaseWirelessInterface(BaseInterface): opmode = misc.RunRegex(opmode_pattern, output) return opmode + def GetAvailableAuthMethods(self, iwlistauth=None): + """ Get the MAC address for the interface. """ + if not iwlistauth: + cmd = 'iwlist ' + self.iface + ' auth' + if self.verbose: print cmd + output = misc.Run(cmd) + else: + output = iwlistauth + + authm = misc.RunRegex(authmethods_pattern, output).replace(' ', '').replace('\t', '').replace('\n', '; ')[:-2] + return authm + def _get_link_quality(self, output): """ Parse out the link quality from iwlist scan or iwconfig output. """ try: From 1e65babbd91a128171b8b012ccfd017dab3b717f Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Tue, 3 Mar 2009 11:40:34 +0100 Subject: [PATCH 6/7] Removing placeholders, and fixing issues pointed out by Dan --- wicd/backends/be-ioctl.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/wicd/backends/be-ioctl.py b/wicd/backends/be-ioctl.py index bce0986..3d03c64 100644 --- a/wicd/backends/be-ioctl.py +++ b/wicd/backends/be-ioctl.py @@ -485,27 +485,26 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): """ Get the current bitrate for the interface. """ if not self.iface: return "" data = (self.iface + '\0' * 32)[:32] - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) fmt = "ihbb" size = struct.calcsize(fmt) try: - data = fcntl.ioctl(s, 0x8B21, ifreq)[16:]  # 0x8B21 is SIOCGIWRATE in /usr/include/linux/wireless.h + result = fcntl.ioctl(self.sock.fileno(), SIOCGIWRATE, data)[16:] except IOError, e: if self.verbose: - print "SIOCGIWAP failed: " + str(e) + print "SIOCGIWRATE failed: " + str(e) return "" - f, e, x, x = struct.unpack(fmt, data[:size]) + f, e, x, x = struct.unpack(fmt, result[:size]) return (f / 1000000) + ' Mb/s' - def GetOperationalMode(self, iwconfig=None): + #def GetOperationalMode(self, iwconfig=None): """ Get the MAC address for the interface. """ # TODO: implement me - return '' + # return '' - def GetAvailableAuthMethods(self, iwlistauth=None): + #def GetAvailableAuthMethods(self, iwlistauth=None): """ Get the MAC address for the interface. """ # TODO: Implement me - return '' + # return '' def GetSignalStrength(self, iwconfig=None): """ Get the signal strength of the current network. From 1efa36014f5b2aa13d086d6db37806343d669ea6 Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Sat, 7 Mar 2009 12:16:45 +0100 Subject: [PATCH 7/7] Forgot to add a definition --- wicd/backends/be-ioctl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wicd/backends/be-ioctl.py b/wicd/backends/be-ioctl.py index 3d03c64..1d2ad9e 100644 --- a/wicd/backends/be-ioctl.py +++ b/wicd/backends/be-ioctl.py @@ -67,6 +67,7 @@ SIOCGIWESSID = 0x8B1B SIOCGIWRANGE = 0x8B0B SIOCGIWAP = 0x8B15 SIOCGIWSTATS = 0x8B0F +SIOCGIWRATE = 0x8B21 # Got these from /usr/include/sockios.h SIOCGIFADDR = 0x8915