diff --git a/wicd/backends/be-external.py b/wicd/backends/be-external.py index d4180fc..5d74b85 100644 --- a/wicd/backends/be-external.py +++ b/wicd/backends/be-external.py @@ -444,6 +444,12 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): print 'wpa_supplicant rescan forced...' cmd = 'wpa_cli -i' + self.iface + ' scan' misc.Run(cmd) + + def StopWPA(self): + """ Terminates wpa using wpa_cli""" + cmd = 'wpa_cli -i %s terminate' % self.iface + if self.verbose: print cmd + misc.Run(cmd) def GetBSSID(self, iwconfig=None): """ Get the MAC address for the interface. """ diff --git a/wicd/backends/be-ioctl.py b/wicd/backends/be-ioctl.py index d0c5fe2..3ba9a48 100644 --- a/wicd/backends/be-ioctl.py +++ b/wicd/backends/be-ioctl.py @@ -273,7 +273,11 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): print "GetNetworks caught an exception: %s" % e return [] - results = self.scan_iface.Scan() + try: + results = self.scan_iface.Scan() + except iwscan.error, e: + print "ERROR: %s" + return [] return filter(None, [self._parse_ap(cell) for cell in results]) def _parse_ap(self, cell): @@ -334,6 +338,18 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): return ap + def _connect_to_wpa_ctrl_iface(self): + """ Connect to the wpa ctrl interface. """ + ctrl_iface = '/var/run/wpa_supplicant' + try: + socket = [os.path.join(ctrl_iface, s) \ + for s in os.listdir(ctrl_iface) if s == self.iface][0] + except OSError, error: + print error + return None + + return wpactrl.WPACtrl(socket) + def ValidateAuthentication(self, auth_time): """ Validate WPA authentication. @@ -359,15 +375,10 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): if self.wpa_driver == RALINK_DRIVER: return True - ctrl_iface = '/var/run/wpa_supplicant' - try: - socket = [os.path.join(ctrl_iface, s) \ - for s in os.listdir(ctrl_iface) if s == self.iface][0] - except OSError: - print error - return True - - wpa = wpactrl.WPACtrl(socket) + wpa = self._connect_to_wpa_ctrl_iface() + if not wpa: + print "Failed to open ctrl interface" + return False MAX_TIME = 35 MAX_DISCONNECTED_TIME = 3 @@ -402,6 +413,13 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): print 'wpa_supplicant authentication may have failed.' return False + + def StopWPA(self): + """ Terminates wpa_supplicant using its ctrl interface. """ + wpa = self._connect_to_wpa_ctrl_iface() + if not wpa: + return + wpa.request("TERMINATE") def _AuthenticateRalinkLegacy(self, network): """ Authenticate with the specified wireless network. diff --git a/wicd/networking.py b/wicd/networking.py index 02a659f..99a1d52 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -151,11 +151,11 @@ class Controller(object): self.iface = None self.backend_manager = BackendManager() + def get_debug(self): return self._debug def set_debug(self, value): self._debug = value if self.iface: self.iface.SetDebugMode(value) - def get_debug(self): return self._debug debug = property(get_debug, set_debug) def set_dhcp_client(self, value): @@ -187,10 +187,6 @@ class Controller(object): else: return True - def StopDHCP(self): - """ Stops all running DHCP clients. """ - return BACKEND.StopDHCP() - def GetIP(self, ifconfig=""): """ Get the IP of the interface. @@ -208,9 +204,14 @@ class Controller(object): misc.ExecuteScript(expand_script_macros(self.disconnect_script, 'disconnection', *args)) iface.ReleaseDHCP() iface.SetAddress('0.0.0.0') + iface.FlushRoutes() iface.Down() iface.Up() + def ReleaseDHCP(self): + """ Release the DHCP lease for this interface. """ + return self.iface.ReleaseDHCP() + def IsUp(self): """ Calls the IsUp method for the wired interface. @@ -435,12 +436,6 @@ class ConnectThread(threading.Thread): print "Releasing DHCP leases..." iface.ReleaseDHCP() - @abortable - def stop_dhcp_clients(self, iface): - """ Stop and running DHCP clients. """ - print 'Stopping DHCP clients' - BACKEND.StopDHCP() - def connect_aborted(self, reason): """ Sets the thread status to aborted. """ if self.abort_reason: @@ -643,8 +638,8 @@ class Wireless(Controller): """ wiface = self.wiface print 'Creating ad-hoc network' - print 'Killing dhclient and wpa_supplicant' - BACKEND.StopDHCP() + print 'Stopping dhcp client and wpa_supplicant' + BACKEND.ReleaseDHCP() wiface.StopWPA() print 'Putting wireless interface down' wiface.Down() @@ -761,7 +756,6 @@ class WirelessConnectThread(ConnectThread): self.put_iface_down(wiface) self.release_dhcp_clients(wiface) self.reset_ip_addresses(wiface) - self.stop_dhcp_clients(wiface) self.stop_wpa(wiface) self.flush_routes(wiface) @@ -971,7 +965,6 @@ class WiredConnectThread(ConnectThread): self.put_iface_down(liface) self.release_dhcp_clients(liface) self.reset_ip_addresses(liface) - self.stop_dhcp_clients(liface) self.flush_routes(liface) # Bring up interface. diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index 15e9ed4..7e7f3a8 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -379,12 +379,12 @@ class WicdDaemon(dbus.service.Object): print 'canceling connection attempt' if self.wifi.connecting_thread: self.wifi.connecting_thread.should_die = True - self.wifi.StopDHCP() + self.wifi.ReleaseDHCP() self.wifi.StopWPA() self.wifi.connecting_thread.connect_result = 'aborted' if self.wired.connecting_thread: self.wired.connecting_thread.should_die = True - self.wired.StopDHCP() + self.wired.ReleaseDHCP() self.wired.connecting_thread.connect_result = 'aborted' @dbus.service.method('org.wicd.daemon') diff --git a/wicd/wnettools.py b/wicd/wnettools.py index dff9973..9dd9d8c 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -47,7 +47,7 @@ blacklist_strict = '!"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ ' blacklist_norm = ";`$!*|><&\\" blank_trans = maketrans("", "") -__all__ = ["SetDNS", "GetDefaultGateway", "GetWiredInterfaces", "StopDHCP", +__all__ = ["SetDNS", "GetDefaultGateway", "GetWiredInterfaces", "GetWirelessInterfaces", "IsValidWpaSuppDriver", "NeedsExternalCalls"] def _sanitize_string(string): @@ -107,11 +107,6 @@ def GetDefaultGateway(): print 'couldn\'t retrieve default gateway from route -n' return gateway -def StopDHCP(): - """ Stop the DHCP client. """ - cmd = 'killall dhclient dhclient3 pump dhcpcd-bin' - misc.Run(cmd) - def GetWirelessInterfaces(): """ Get available wireless interfaces. @@ -455,7 +450,7 @@ class BaseInterface(object): """ if not self.iface: return False - cmd = "%s %s" % (self._get_dhcp_command('connect'), self.iface) + cmd = self._get_dhcp_command('connect') #cmd = self.DHCP_CMD + " " + self.iface if self.verbose: print cmd pipe = misc.Run(cmd, include_stderr=True, return_pipe=True) @@ -471,20 +466,31 @@ class BaseInterface(object): def ReleaseDHCP(self): """ Release the DHCP lease for this interface. """ if not self.iface: return False - cmd = "%s %s" % (self._get_dhcp_command("release"), self.iface) - #cmd = self.DHCP_RELEASE + " " + self.iface + cmd = self._get_dhcp_command("release") if self.verbose: print cmd misc.Run(cmd) - def FlushRoutes(self): - """ Flush all network routes. """ + def DelDefaultRoute(self): + """ Delete only the default route for a device. """ if not self.iface: return False if self.ip_cmd and self.flush_tool in [misc.AUTO, misc.IP]: - #cmd = "ip route flush dev " + self.iface - cmds = ['%s route flush all' % self.ip_cmd] + cmd = '%s route del default dev %s' % (self.ip_cmd, self.iface) elif self.route_cmd and self.flush_tool in [misc.AUTO, misc.ROUTE]: - cmds = ['%s del default' % self.route_cmd] - cmds.append('route del dev %s' % self.iface) + cmd = '%s del default dev %s' % (self.route_cmd, self.iface) + else: + print "No route manipulation command available!" + return + if self.verbose: print cmd + misc.Run(cmd) + + + def FlushRoutes(self): + """ Flush network routes for this device. """ + if not self.iface: return False + if self.ip_cmd and self.flush_tool in [misc.AUTO, misc.IP]: + cmds = ['%s route flush dev %s' % (self.ip_cmd, self.iface)] + elif self.route_cmd and self.flush_tool in [misc.AUTO, misc.ROUTE]: + cmds = ['%s del dev %s' % (self.route_cmd, self.iface)] else: print "No flush command available!" cmds = [] @@ -583,9 +589,7 @@ class BaseWirelessInterface(BaseInterface): def StopWPA(self): """ Stop wireless encryption. """ - cmd = 'killall wpa_supplicant' - if self.verbose: print cmd - misc.Run(cmd) + raise NotImplementedError def GetKillSwitchStatus(self): """ Determines if the wireless killswitch is enabled.