コード例 #1
0
def setAddress(name, mode, address, mask, gateway):
    profile = Profile(name)
    profile.info["net_mode"] = mode
    profile.info["net_address"] = address
    profile.info["net_mask"] = mask
    profile.info["net_gateway"] = gateway
    profile.save()
コード例 #2
0
def getAuthParameters(name):
    profile = Profile(name)
    d = {}
    for key in profile.info:
        if key.startswith("auth_"):
            d[key[5:]] = profile.info[key]
    return d
コード例 #3
0
ファイル: link.py プロジェクト: pars-linux/corporate2
def kernelEvent(data):
    type, dir = data.split("@", 1)
    if not "/net/" in dir:
        return
    device = dir.split("/net/")[-1]
    new = True

    if type == "add":
        # Get device information
        ifc = netutils.IF(device)
        device_id = ifc.deviceUID()
        if not ifc.isWireless():
            return
        # Notify clients
        notify("Network.Link", "deviceChanged",
               ("add", "wifi", device_id, netutils.deviceName(device_id)))
        # Bring related connection up
        for pn in listProfiles():
            profile = Profile(pn)
            if profile.info.get("device", None) == device_id:
                new = False
                if profile.info.get("state", "down").startswith("unplugged"):
                    setState(pn, "up")
                    break
        # Notify clients if device has no connections
        if new:
            notify("Network.Link", "deviceChanged",
                   ("new", "wifi", device_id, netutils.deviceName(device_id)))
    elif type == "remove":
        # Bring related connection down
        for pn in listProfiles():
            profile = Profile(pn)
            if profile.info.get("device",
                                "").split(":")[-1].split("_")[-1] == device:
                if profile.info.get("state", "down").startswith("up"):
                    # Stop ifplug daemon
                    plugService(device, "down", wireless=True)
                    setState(pn, "unplugged")
                    break
        # Notify clients
        notify("Network.Link", "deviceChanged",
               ("removed", "wifi", device, ""))
コード例 #4
0
def connectionInfo(name):
    profile = Profile(name)
    device = profile.info.get("device", "")
    return {
        "name": name,
        "device_id": device,
        "device_name": netutils.deviceName(device),
        "net_mode": profile.info.get("net_mode", "auto"),
        "net_address": profile.info.get("net_address", ""),
        "net_mask": profile.info.get("net_mask", ""),
        "net_gateway": profile.info.get("net_gateway", ""),
        "name_mode": profile.info.get("name_mode", "default"),
        "name_server": profile.info.get("name_server", ""),
        "state": profile.info.get("state", "down"),
    }
コード例 #5
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")
コード例 #6
0
def getState(name):
    profile = Profile(name)
    return profile.info.get("state", "down")
コード例 #7
0
def getAuthMethod(name):
    profile = Profile(name)
    return profile.info.get("auth", "")
コード例 #8
0
def setAuthParameters(name, key, value):
    profile = Profile(name)
    profile.info["auth_%s" % key] = value
    profile.save()
コード例 #9
0
def setAuthMethod(name, method):
    profile = Profile(name)
    profile.info["auth"] = method
    profile.save()
コード例 #10
0
def setNameService(name, namemode, nameserver):
    profile = Profile(name)
    profile.info["name_mode"] = namemode
    profile.info["name_server"] = nameserver
    profile.save()
コード例 #11
0
def setRemote(name, remote):
    profile = Profile(name)
    profile.info["remote"] = remote
    profile.save()
コード例 #12
0
def deleteConnection(name):
    profile = Profile(name)
    profile.delete()
    notify("Network.Link", "connectionChanged", ("deleted", name))
コード例 #13
0
def setDeviceMode(name, mode):
    profile = Profile(name)
    profile.info["device_mode"] = mode
    profile.save()
コード例 #14
0
def setDevice(name, device):
    profile = Profile(name)
    profile.info["device"] = device
    profile.save()
コード例 #15
0
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")