From 05a59fb5dba42f3771110a520393fb5181afd65a Mon Sep 17 00:00:00 2001 From: imdano <> Date: Thu, 19 Jun 2008 22:09:39 +0000 Subject: [PATCH] Experimental/Testing: - Improved behavior in the networking backend. The wired/wireless wnettools instances now refer to each other, and get passed on to connection threads as well, which simplifies passing settings for external program usage. Also removed some unecessary creating of duplicate wnettools instances which ended up causing some issues. - Fixed bug where dhclient was being used as the dhcp client even if it was selected in the options menu. - Fixed a typo in the connection commands used for ralink cards. - Fixed the wrong cli option for releasing a dhcpcd lease. - Monitor.py no longer calls for an auto-rescan if the daemon is currently connecting to a network. - Cleaned up some comments and simplified the logic in a few methods/functions. --- daemon.py | 10 ++++++---- gui.py | 2 +- misc.py | 11 ++++------- monitor.py | 2 +- networking.py | 29 +++++++++++++++++------------ wnettools.py | 31 ++++++++++++++++++++++++------- 6 files changed, 53 insertions(+), 32 deletions(-) diff --git a/daemon.py b/daemon.py index 69bc77a..24bf71a 100644 --- a/daemon.py +++ b/daemon.py @@ -154,10 +154,11 @@ class ConnectionWizard(dbus.service.Object): # Make a variable that will hold the wired network profile self.WiredNetwork = {} - - # Load our wired/wireless interfaces - self.wifi.LoadInterfaces() - self.wired.LoadInterfaces() + + # Kind of hackish way to load the secondary wnettools interface + # for both wired and wireless connection managers. + self.wifi.liface = self.wired.liface + self.wired.wiface = self.wifi.wiface # Scan since we just got started if auto_connect: @@ -590,6 +591,7 @@ class ConnectionWizard(dbus.service.Object): @dbus.service.method('org.wicd.daemon') def SetDHCPClient(self, client): + print "Setting dhcp client to %i" % (int(client)) self.dhcp_client = int(client) self.wifi.dhcp_client = int(client) self.wired.dhcp_client = int(client) diff --git a/gui.py b/gui.py index 48c881c..004290c 100644 --- a/gui.py +++ b/gui.py @@ -382,7 +382,7 @@ class appGui: dhclientradio = self.wTree.get_widget("dhclient_radio") pumpradio = self.wTree.get_widget("pump_radio") dhcpcdradio = self.wTree.get_widget("dhcpcd_radio") - dhcp_list = [dhcpautoradio, dhclientradio, pumpradio, dhcpcdradio] + dhcp_list = [dhcpautoradio, dhclientradio, dhcpcdradio, pumpradio] dhcp_method = daemon.GetDHCPClient() dhcp_list[dhcp_method].set_active(True) diff --git a/misc.py b/misc.py index fb7edb2..dd6c639 100644 --- a/misc.py +++ b/misc.py @@ -283,14 +283,11 @@ def get_gettext(): def to_unicode(x): """ Attempts to convert a string to utf-8. """ try: # This may never fail, but let's be safe - default_encoding = locale.getpreferredencoding() + encoding = locale.getpreferredencoding() except: - default_encoding = None - - if default_encoding: - ret = x.decode(default_encoding, 'replace').encode('utf-8') - else: # Just guess UTF-8 - ret = x.decode('utf-8', 'replace').encode('utf-8') + # Just guess utf-8 if it fails. + encoding = 'utf-8' + ret = x.decode(encoding, 'replace').encode('utf-8') return ret def RenameProcess(new_name): diff --git a/monitor.py b/monitor.py index f2c17dd..12fc2c8 100755 --- a/monitor.py +++ b/monitor.py @@ -260,7 +260,7 @@ class ConnectionStatus(): def rescan_networks(self): """ Calls a wireless scan. """ try: - if daemon.GetSuspend(): + if daemon.GetSuspend() or daemon.CheckIfConnecting(): return True wireless.Scan() daemon.SendScanSignal() diff --git a/networking.py b/networking.py index 67b71b6..b7ed330 100644 --- a/networking.py +++ b/networking.py @@ -103,8 +103,10 @@ class Controller(object): self._dhcp_client = value if self.wiface: self.wiface.DHCP_CLIENT = value + self.wiface.CheckDHCP() if self.liface: self.liface.DHCP_CLIENT = value + self.liface.CheckDHCP() def get_dhcp_client(self): return self._dhcp_client @@ -140,7 +142,7 @@ class ConnectThread(threading.Thread): lock = thread.allocate_lock() def __init__(self, network, wireless, wired, before_script, after_script, - disconnect_script, gdns1, gdns2, gdns3, debug): + disconnect_script, gdns1, gdns2, gdns3, wiface, liface, debug): """ Initialise the required object variables and the thread. Keyword arguments: @@ -172,6 +174,9 @@ class ConnectThread(threading.Thread): self.global_dns_1 = gdns1 self.global_dns_2 = gdns2 self.global_dns_3 = gdns3 + + self.wiface = wiface + self.liface = liface self.connecting_message = None self.debug = debug @@ -391,7 +396,8 @@ class Wireless(Controller): self.wireless_interface, self.wired_interface, self.wpa_driver, self.before_script, self.after_script, self.disconnect_script, self.global_dns_1, - self.global_dns_2, self.global_dns_3, debug) + self.global_dns_2, self.global_dns_3, self.wiface, self.liface, + debug) self.connecting_thread.setDaemon(True) self.connecting_thread.start() return True @@ -570,7 +576,7 @@ class WirelessConnectThread(ConnectThread): def __init__(self, network, wireless, wired, wpa_driver, before_script, after_script, disconnect_script, gdns1, - gdns2, gdns3, debug=False): + gdns2, gdns3, wiface, liface, debug=False): """ Initialise the thread with network information. Keyword arguments: @@ -588,7 +594,7 @@ class WirelessConnectThread(ConnectThread): """ ConnectThread.__init__(self, network, wireless, wired, before_script, after_script, disconnect_script, gdns1, - gdns2, gdns3, debug) + gdns2, gdns3, wiface, liface, debug) self.wpa_driver = wpa_driver @@ -606,9 +612,8 @@ class WirelessConnectThread(ConnectThread): 5. Get/set IP address and DNS servers. """ - wiface = wnettools.WirelessInterface(self.wireless_interface, - self.debug, self.wpa_driver) - liface = wnettools.WiredInterface(self.wired_interface, self.debug) + wiface = self.wiface + liface = self.liface self.is_connecting = True # Run pre-connection script. @@ -747,7 +752,7 @@ class Wired(Controller): self.wireless_interface, self.wired_interface, self.before_script, self.after_script, self.disconnect_script, self.global_dns_1, - self.global_dns_2, self.global_dns_3, debug) + self.global_dns_2, self.global_dns_3, wiface, liface, debug) self.connecting_thread.setDaemon(True) self.connecting_thread.start() return True @@ -809,7 +814,7 @@ class WiredConnectThread(ConnectThread): """ def __init__(self, network, wireless, wired, before_script, after_script, disconnect_script, gdns1, - gdns2, gdns3, debug=False): + gdns2, gdns3, wiface, liface, debug=False): """ Initialise the thread with network information. Keyword arguments: @@ -826,7 +831,7 @@ class WiredConnectThread(ConnectThread): """ ConnectThread.__init__(self, network, wireless, wired, before_script, after_script, disconnect_script, gdns1, - gdns2, gdns3, debug) + gdns2, gdns3, wiface, liface, debug) def run(self): """ The main function of the connection thread. @@ -842,8 +847,8 @@ class WiredConnectThread(ConnectThread): 5. Run post-connection script. """ - wiface = wnettools.WirelessInterface(self.wireless_interface, self.debug) - liface = wnettools.WiredInterface(self.wired_interface, self.debug) + wiface = self.wiface + liface = self.liface self.is_connecting = True diff --git a/wnettools.py b/wnettools.py index e5f2586..c8cee13 100644 --- a/wnettools.py +++ b/wnettools.py @@ -134,7 +134,7 @@ def GetWirelessInterfaces(): def _fast_get_wifi_interfaces(): """ Tries to get a wireless interface using /sys/class/net. """ - dev_dir = '/sys/class/net/' + dev_dir = '/sys/class/net/' ifnames = [] ifnames = [iface for iface in os.listdir(dev_dir) if 'wireless' \ @@ -239,15 +239,31 @@ class Interface(object): def CheckDHCP(self): """ Check for a valid DHCP client. - Checks for the existence of a support DHCP client. If one is + Checks for the existence of a supported DHCP client. If one is found, the appropriate values for DHCP_CMD, DHCP_RELEASE, and DHCP_CLIENT are set. If a supported client is not found, a warning is printed. """ + def get_client_name(cl): + """ Converts the integer value for a dhcp client to a string. """ + if cl in [misc.DHCLIENT, "dhclient"]: + client = "dhclient" + elif cl in [misc.DHCPCD, "dhcpcd"]: + client = "dhcpcd" + else: + client = "pump" + return client + + print 'checking dhcp...' + if self.DHCP_CLIENT: - dhcp_client = self.DHCP_CLIENT - else: + dhcp_client = get_client_name(self.DHCP_CLIENT) + dhcp_path = self._find_client_path(dhcp_client) + if not dhcp_path: + print "WARNING: Could not find selected dhcp client. Wicd " + \ + " will try to find another supported client." + if not self.DHCP_CLIENT or not dhcp_path: dhcp_client = None dhcp_path = None dhcpclients = ["dhclient", "dhcpcd", "pump"] @@ -271,7 +287,7 @@ class Interface(object): elif dhcp_client in [misc.DHCPCD, "dhcpcd"]: dhcp_client = misc.DHCPCD dhcp_cmd = dhcp_path - dhcp_release = dhcp_cmd + " -r" + dhcp_release = dhcp_cmd + " -k" else: dhcp_client = None dhcp_cmd = None @@ -1126,7 +1142,7 @@ class WirelessInterface(Interface): cmd_list = [] cmd_list.append('NetworkType=' + info[6]) cmd_list.append('AuthMode=' + auth_mode) - cmd_list.append('EncryptType=' + info[4]) + cmd_list.append('EncrypType=' + info[4]) cmd_list.append('SSID=' + info[2]) cmd_list.append(key_name + '=' + network.get('key')) if info[5] == 'SHARED' and info[4] == 'WEP': @@ -1207,7 +1223,8 @@ class WirelessInterface(Interface): if self.verbose: print "SIOCGIWRANGE failed: " + str(e) return None - fmt = "iiihb6ii4B4Bi32i2i2i2i2i3h8h2b2bhi8i2b3h2i2ihB17x" + 32*"ihbb" + # This defines the iwfreq struct, used to get singal strength. + fmt = "iiihb6ii4B4Bi32i2i2i2i2i3h8h2b2bhi8i2b3h2i2ihB17x" + 32 * "ihbb" size = struct.calcsize(fmt) data = buff.tostring() data = data[0:size]