Exemple #1
0
 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")
Exemple #2
0
 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")
Exemple #3
0
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")