Beispiel #1
0
    def get_ip_networks(self):
        ip_networks = dict()
        xml = self._XMLDesc(0)
        if util.get_xml_path(xml, "/network/ip") is None:
            return ip_networks
        tree = etree.fromstring(xml)
        ips = tree.findall('.ip')
        for ip in ips:
            address_str = ip.get('address')
            netmask_str = ip.get('netmask')
            prefix = ip.get('prefix')
            family = ip.get('family', 'ipv4')
            base = 32 if family == 'ipv4' else 128
            if prefix:
                prefix = int(prefix)
                binstr = ((prefix * "1") + ((base - prefix) * "0"))
                netmask_str = str(IP(int(binstr, base=2)))

            if netmask_str:
                netmask = IP(netmask_str)
                gateway = IP(address_str)
                network = IP(gateway.int() & netmask.int())
                netmask_str = netmask_str if family == 'ipv4' else str(prefix)
                ret = IP(str(network) + "/" + netmask_str)
            else:
                ret = IP(str(address_str))
            ip_networks[family] = ret
        return ip_networks
Beispiel #2
0
    def modify_fixed_address(self, name, address, mac):
        util.validate_macaddr(mac)
        if name:
            new_xml = '<host mac="{}" name="{}" ip="{}"/>'.format(
                mac, name, IP(address))
        else:
            new_xml = '<host mac="{}" ip="{}"/>'.format(mac, IP(address))
        new_host_xml = ElementTree.fromstring(new_xml)

        tree = ElementTree.fromstring(self._XMLDesc(0))
        hosts = tree.findall("./ip/dhcp/host")

        host = None
        for h in hosts:
            if h.get('mac') == mac:
                host = h
                break
        if host is None:
            self.update(
                VIR_NETWORK_UPDATE_COMMAND_ADD_LAST,
                VIR_NETWORK_SECTION_IP_DHCP_HOST, -1, new_xml,
                VIR_NETWORK_UPDATE_AFFECT_LIVE
                | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
        else:
            # change the host
            if host.get('name') == new_host_xml.get('name') and host.get(
                    'ip') == new_host_xml.get('ip'):
                return False
            else:
                self.update(
                    VIR_NETWORK_UPDATE_COMMAND_MODIFY,
                    VIR_NETWORK_SECTION_IP_DHCP_HOST, -1, new_xml,
                    VIR_NETWORK_UPDATE_AFFECT_LIVE
                    | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
Beispiel #3
0
    def get_ipv4_dhcp_range(self):
        xml = self._XMLDesc(0)
        dhcpstart = util.get_xml_path(xml, "/network/ip/dhcp/range[1]/@start")
        dhcpend = util.get_xml_path(xml, "/network/ip/dhcp/range[1]/@end")
        if not dhcpstart or not dhcpend:
            return None

        return [IP(dhcpstart), IP(dhcpend)]
Beispiel #4
0
def network_size(net, dhcp=None):
    """
    Func return gateway, mask and dhcp pool.
    """
    mask = IP(net).strNetmask()
    addr = IP(net)
    gateway = addr[1].strNormal()
    dhcp_pool = [addr[2].strNormal(), addr[addr.len() - 2].strNormal()]
    if dhcp:
        return gateway, mask, dhcp_pool
    else:
        return gateway, mask, None
Beispiel #5
0
def network_size(net, dhcp=None):
    """
    Func return gateway, mask and dhcp pool.
    """
    mask = IP(net).strNetmask()
    addr = IP(net)
    gateway = addr[1].strNormal()
    dhcp_pool = [addr[2].strNormal(), addr[addr.len() - 2].strNormal()]
    if dhcp:
        return gateway, mask, dhcp_pool
    else:
        return gateway, mask, None
Beispiel #6
0
    def get_dhcp_range(self, family='ipv4'):
        xml = self._XMLDesc(0)
        if family == 'ipv4':
            dhcpstart = util.get_xml_path(xml, "/network/ip[not(@family='ipv6')]/dhcp/range[1]/@start")
            dhcpend = util.get_xml_path(xml, "/network/ip[not(@family='ipv6')]/dhcp/range[1]/@end")
        if family == 'ipv6':
            dhcpstart = util.get_xml_path(xml, "/network/ip[@family='ipv6']/dhcp/range[1]/@start")
            dhcpend = util.get_xml_path(xml, "/network/ip[@family='ipv6']/dhcp/range[1]/@end")

        if not dhcpstart or not dhcpend:
            return None

        return [IP(dhcpstart), IP(dhcpend)]
Beispiel #7
0
def network_size(subnet, dhcp=None):
    """
    Func return gateway, mask and dhcp pool.
    """
    mask = IP(subnet).strNetmask()
    addr = IP(subnet)
    gateway = addr[1].strCompressed()
    if addr.version() == 4:
        dhcp_pool = [addr[2].strCompressed(), addr[addr.len() - 2].strCompressed()]
    if addr.version() == 6:
        mask = mask.lstrip('/') if '/' in mask else mask
        dhcp_pool = [IP(addr[0].strCompressed() + hex(256)), IP(addr[0].strCompressed() + hex(512 - 1))]
    if dhcp:
        return gateway, mask, dhcp_pool
    else:
        return gateway, mask, None
Beispiel #8
0
    def get_ipv4_network(self):
        xml = self._XMLDesc(0)
        if util.get_xml_path(xml, "/network/ip") is None:
            return None
        addrStr = util.get_xml_path(xml, "/network/ip/@address")
        netmaskStr = util.get_xml_path(xml, "/network/ip/@netmask")
        prefix = util.get_xml_path(xml, "/network/ip/@prefix")

        if prefix:
            prefix = int(prefix)
            binstr = ((prefix * "1") + ((32 - prefix) * "0"))
            netmaskStr = str(IP(int(binstr, base=2)))

        if netmaskStr:
            netmask = IP(netmaskStr)
            gateway = IP(addrStr)
            network = IP(gateway.int() & netmask.int())
            ret = IP(str(network) + "/" + netmaskStr)
        else:
            ret = IP(str(addrStr))

        return ret
Beispiel #9
0
    def get_ipv4_network(self):
        xml = self._XMLDesc(0)
        if util.get_xml_path(xml, "/network/ip") is None:
            return None
        addrStr = util.get_xml_path(xml, "/network/ip/@address")
        netmaskStr = util.get_xml_path(xml, "/network/ip/@netmask")
        prefix = util.get_xml_path(xml, "/network/ip/@prefix")

        if prefix:
            prefix = int(prefix)
            binstr = ((prefix * "1") + ((32 - prefix) * "0"))
            netmaskStr = str(IP(int(binstr, base=2)))

        if netmaskStr:
            netmask = IP(netmaskStr)
            gateway = IP(addrStr)
            network = IP(gateway.int() & netmask.int())
            ret = IP(str(network) + "/" + netmaskStr)
        else:
            ret = IP(str(addrStr))

        return ret
Beispiel #10
0
    def modify_fixed_address(self, name, address, mac_duid, family='ipv4'):
        tree = etree.fromstring(self._XMLDesc(0))
        if family == 'ipv4':
            new_xml = '<host mac="{}" {} ip="{}"/>'.format(
                mac_duid, 'name="' + name + '"' if name else '', IP(address))
            hosts = tree.xpath("./ip[not(@family='ipv6')]/dhcp/host")
            compare_var = 'mac'
            parent_index = self.parent_count - 2
        if family == 'ipv6':
            new_xml = '<host id="{}" {} ip="{}"/>'.format(
                mac_duid, 'name="' + name + '"' if name else '', IP(address))
            hosts = tree.xpath("./ip[@family='ipv6']/dhcp/host")
            compare_var = 'id'
            parent_index = self.parent_count - 1
        new_host_xml = etree.fromstring(new_xml)

        host = None
        for h in hosts:
            if h.get(compare_var) == mac_duid:
                host = h
                break
        if host is None:
            self.update(
                VIR_NETWORK_UPDATE_COMMAND_ADD_LAST,
                VIR_NETWORK_SECTION_IP_DHCP_HOST, new_xml, parent_index,
                VIR_NETWORK_UPDATE_AFFECT_LIVE
                | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
        else:
            # change the host
            if host.get('name') == new_host_xml.get('name') and host.get(
                    'ip') == new_host_xml.get('ip'):
                return False
            else:
                self.update(
                    VIR_NETWORK_UPDATE_COMMAND_MODIFY,
                    VIR_NETWORK_SECTION_IP_DHCP_HOST, new_xml, parent_index,
                    VIR_NETWORK_UPDATE_AFFECT_LIVE
                    | VIR_NETWORK_UPDATE_AFFECT_CONFIG)