def up(self): ifc = self.ifc wifi = Wireless(ifc) notify("Net.Link", "stateChanged", (self.name, "connecting", "")) ifc.up() if self.remote: wifi.setSSID(self.remote) err = wifi.setEncryption(mode=self.authmode, username=self.user, password=self.password, channel=self.channel, ssid=self.remote, auth=self.auth, inner=self.auth_inner, anon=self.user_anon) if err: notify("Net.Link", "stateChanged", (self.name, "inaccessible", err)) fail("auth failed") if self.mode == "manual": ifc.setAddress(self.address, self.mask) ifc.up() if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", self.address)) self.saveMAC() else: ifc.up() ret = ifc.startAuto() if ret == 0 and ifc.isUp(): if self.address: ifc.setAddress(self.address, self.mask) if not self.gateway: gateways = ifc.autoGateways() if gateways: self.gateway = gateways[0] if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() addr = ifc.getAddress()[0] d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", addr)) self.saveMAC() else: notify("Net.Link", "stateChanged", (self.name, "inaccessible", _(dhcp_fail_msg))) fail("DHCP failed")
def up(self): ifc = self.ifc if self.mode == "manual": ifc.setAddress(self.address, self.mask) ifc.up() if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", self.address)) self.callScript("up") else: notify("Net.Link", "stateChanged", (self.name, "connecting", "")) ret = ifc.startAuto() if ret == 0 and ifc.isUp(): if self.address: ifc.setAddress(self.address, self.mask) if not self.gateway: gateways = ifc.autoGateways() if gateways: self.gateway = gateways[0] if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() addr = ifc.getAddress() if addr: d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", unicode(addr[0]))) self.callScript("up") else: notify("Net.Link", "stateChanged", (self.name, "inaccessible", _(dhcp_fail_msg))) fail("DHCP failed") else: notify("Net.Link", "stateChanged", (self.name, "inaccessible", _(dhcp_fail_msg))) fail("DHCP failed") ifc.setMTU(self.mtu)
def setState(name, state): profile = Profile(name) ifname = profile.info["device"].split(":")[-1].split("_")[-1] iface = netutils.findInterface(profile.info["device"]) if state == "unplugged" or not iface: # Reset Network Stack unregisterNameServers(ifname) if state == "down": # Save state to profile database profile.info["state"] = "down" profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "down", "")) else: # Save state to profile database profile.info["state"] = "unplugged" profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "unplugged", "")) # Run profile script (/etc/network/netlink.d/profilename.down) callScript(name, "down") return # Here we go... device_mode = profile.info.get("device_mode", "managed") if device_mode == "managed": if state == "up": # Stop other profiles on same device stopSameDevice(name) # Notify clients notify("Network.Link", "stateChanged", (name, "connecting", "")) # Save state to profile database profile.info["state"] = "connecting" profile.save(no_notify=True) # Wifi settings wifi = Wireless(iface) wifi.setSSID(profile.info["remote"]) # Set encryption try: wifi.setEncryption(getAuthMethod(name), getAuthParameters(name)) except Exception, e: # Stop ifplug deamon plugService(ifname, "down") # Save state to profile database profile.info["state"] = "inaccessible %s" % unicode(e) profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "inaccessible", unicode(e))) fail(unicode(e)) if profile.info.get("net_mode", "auto") == "auto": # Start DHCP client ret = iface.startAuto() if ret == 0 and iface.isUp(): if "net_address" in profile.info or "net_gateway" in profile.info: net_address = profile.info.get("net_address", None) net_mask = profile.info.get("net_mask", "255.255.255.0") net_gateway = profile.info.get("net_gateway", None) if net_address: iface.setAddress(net_address, net_mask) if not net_gateway: gateways = iface.autoGateways() if len(gateways): net_gateway = gateways[0] if net_gateway: route = netutils.Route() route.setDefault(net_gateway) elif iface.getAddress(): net_address = iface.getAddress()[0] else: # Bring device down iface.down() # Save state to profile database profile.info["state"] = "inaccessible %s" % _(MSG_DHCP_FAILED) profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "inaccessible", _(MSG_DHCP_FAILED))) return # Set nameservers registerNameServers(profile, iface) # Save state to profile database profile.info["state"] = "up " + net_address profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "up", net_address)) # Run profile script (/etc/network/netlink.d/profilename.up) callScript(name, "up") # Start ifplug daemon plugService(ifname, "up", wireless=True) else: # Bring device down iface.down() # Save state to profile database profile.info["state"] = "inaccessible %s" % _(MSG_DHCP_FAILED) profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "inaccessible", _(MSG_DHCP_FAILED))) else: try: net_address = profile.info["net_address"] net_mask = profile.info["net_mask"] except KeyError: return # Set address iface.setAddress(net_address, net_mask) # Bring up interface if iface.up() == None: fail(_(MSG_NO_DRIVER)) # Set default gateway net_gateway = profile.info.get("net_gateway", "") if net_gateway: route = netutils.Route() route.setDefault(net_gateway) # Set nameservers registerNameServers(profile, iface) # Save state to profile database profile.info["state"] = "up " + net_address profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "up", net_address)) # Run profile script (/etc/network/netlink.d/profilename.up) callScript(name, "up") # Start ifplug deamon plugService(ifname, "up") elif state == "down": if profile.info.get("net_mode", "auto") == "auto": iface.stopAuto() # Set encryption to none wifi = Wireless(iface) wifi.setEncryption(None, None) # Reset Network Stack unregisterNameServers(ifname) # Bring device down iface.down() # Save state to profile database profile.info["state"] = "down" profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, "down", "")) # Run profile script (/etc/network/netlink.d/profilename.down) callScript(name, "down")
def up(self): if self.device_mode == "Managed": ifc = self.ifc ifc.down() wifi = Wireless(ifc) wifi.setMode("Managed") notify("Net.Link", "stateChanged", (self.name, "connecting", "")) ifc.up() if self.remote: wifi.setSSID(self.remote) err = wifi.setEncryption(mode=self.authmode, username=self.user, password=self.password, channel=self.channel, ssid=self.remote, \ auth=self.auth, inner=self.auth_inner, anon=self.user_anon, ca_cert=self.auth_ca_cert, client_cert=self.auth_client_cert,\ private_key=self.auth_private_key, private_key_passwd=self.auth_private_key_pass) if err: notify("Net.Link", "stateChanged", (self.name, "inaccessible", err)) fail("auth failed") if self.mode == "manual": ifc.setAddress(self.address, self.mask) ifc.up() if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", self.address)) self.saveMAC() else: ifc.up() ret = ifc.startAuto() if ret == 0 and ifc.isUp(): if self.address: ifc.setAddress(self.address, self.mask) if not self.gateway: gateways = ifc.autoGateways() if gateways: self.gateway = gateways[0] if self.gateway: route = netutils.Route() route.setDefault(self.gateway) self.dns() addr = ifc.getAddress()[0] d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) notify("Net.Link", "stateChanged", (self.name, "up", addr)) self.saveMAC() else: notify("Net.Link", "stateChanged", (self.name, "inaccessible", _(dhcp_fail_msg))) fail("DHCP failed") elif self.device_mode == "Ad-Hoc": ifc = self.ifc wifi = Wireless(ifc) ifc.down() wifi.setMode("Ad-Hoc") notify("Net.Link", "stateChanged", (self.name, "connecting", "")) if self.mode == "manual": ifc.setAddress(self.address, self.mask) else: subprocess.Popen([ "/usr/sbin/avahi-autoipd", "-D", "--force-bind", ifc.name ], stdout=subprocess.PIPE).wait() from time import sleep sleep( 8 ) # FIXME: Give sometime to the auto-ip deamon for getting IP. Yeah time.sleep is evil and ugly but works for now. Will be fixed for non-Desperate housecoders :) subprocess.Popen( ["/usr/sbin/iwconfig", ifc.name, "channel", "01"], stdout=subprocess.PIPE ).wait( ) # FIXME: channel auto mode sometimes does not work. So, a 2 digit number (01,02,03 ... 10) must be given subprocess.Popen( ["/usr/sbin/iwconfig", ifc.name, "key", self.password], stdout=subprocess.PIPE).wait() subprocess.Popen( ["/usr/sbin/iwconfig", ifc.name, "essid", self.remote], stdout=subprocess.PIPE).wait() ifc.up() d = DB.getDB(self.name) d["state"] = "up" DB.setDB(self.name, d) addr = ifc.getAddress()[0] notify("Net.Link", "stateChanged", (self.name, "up", addr))
def setState(name, state): profile = Profile(name) ifname = profile.info["device"].split(":")[-1].split("_")[-1] iface = netutils.findInterface(profile.info["device"]) def saveState(_state, _msg=""): # Save state to profile database if _msg: profile.info["state"] = _state + " " + _msg else: profile.info["state"] = _state profile.save(no_notify=True) # Notify clients notify("Network.Link", "stateChanged", (name, _state, _msg)) # Run profile script if _state in ["down", "inaccessible", "unplugged"]: callScript(name, "down") elif _state in ["up"]: callScript(name, "up") if iface and state == "up": if iface.up() == None: fail(_(MSG_NO_DRIVER)) # Check cable state saveState("connecting") plug = False for i in xrange(10): if plugCheck(ifname): plug = True break time.sleep(0.5) if not plug: saveState("unplugged") return if state == "unplugged" or not iface: # Reset Network Stack unregisterNameServers(ifname) if state == "down": saveState("down") else: saveState("unplugged") return # Here we go... if state == "up": # Reset interface address iface.setAddress("0.0.0.0", "0.0.0.0") # Stop other profiles on same device stopSameDevice(name) # Save state saveState("connecting") # Do whatever you need to do... if profile.info.get("net_mode", "auto") == "auto": # Start DHCP client ret = iface.startAuto() if ret == 0 and iface.isUp(): if "net_address" in profile.info or "net_gateway" in profile.info: net_address = profile.info.get("net_address", None) net_mask = profile.info.get("net_mask", "255.255.255.0") net_gateway = profile.info.get("net_gateway", None) if net_address: iface.setAddress(net_address, net_mask) if not net_gateway: gateways = iface.autoGateways() if len(gateways): net_gateway = gateways[0] if net_gateway: route = netutils.Route() route.setDefault(net_gateway) elif iface.getAddress(): net_address = iface.getAddress()[0] else: # Bring device down iface.down() # Save state saveState("inaccessible", _(MSG_DHCP_FAILED)) return # Set nameservers registerNameServers(profile, iface) # Save state saveState("up", net_address) # Start ifplug deamon plugService(ifname, "up") else: # Bring device down iface.down() # Save state saveState("inaccessible", _(MSG_DHCP_FAILED)) else: try: net_address = profile.info["net_address"] net_mask = profile.info["net_mask"] except KeyError: return # Set address iface.setAddress(net_address, net_mask) if iface.up() == None: fail(_(MSG_NO_DRIVER)) # Set default gateway net_gateway = profile.info.get("net_gateway", "") if net_gateway: route = netutils.Route() route.setDefault(net_gateway) # Set nameservers registerNameServers(profile, iface) # Save state saveState("up", net_address) # Start ifplug deamon plugService(ifname, "up") elif state == "down": if profile.info.get("net_mode", "auto") == "auto": iface.stopAuto() # Bring device down iface.down() # Reset Network Stack unregisterNameServers(ifname) # Save state saveState("down")