some updates to how i handle address allocation for he_ipv6

This commit is contained in:
2020-05-13 13:31:16 -04:00
parent 71df39ca11
commit fc8eda8198
5 changed files with 253 additions and 27 deletions

View File

@@ -8,6 +8,20 @@ import netaddr
import requests
from lxml import etree
from pyroute2 import IPRoute
##
from . import radvd
def xml2bool(xml_str):
if xml_str is None:
return(None)
xml_str = xml_str.lower()[0]
if xml_str == ('t', '1'):
return(True)
elif xml_str == ('f', '0'):
return(False)
else:
raise ValueError('Not a boolean value')
class IP(object):
@@ -50,17 +64,60 @@ class IP6(IP):
def __init__(self, ip, prefix, *args, **kwargs):
super().__init__(ip, prefix, *args, **kwargs)
self._ext_init()
self.alloc_block = netaddr.SubnetSplitter(self.net_net)
class Assignment(object):
def __init__(self, assign_xml, radvd = False, dns = False):
self.xml = assign_xml
self.do_radvd = radvd
self.radvd_dns = dns
self.iface = None
self.alloc = None # This must be set externally to a mapped Allocation instance
self.alloc_name = None
self.prefix = None
self.iface_ip = None
self.alloc_block = None
self.parse()
def _alloc(self):
self.alloc_name = self.xml.attrib['alloc'].strip()
return(None)
def _iface(self):
self.iface = self.xml.attrib['iface'].strip()
return(None)
def _prefix(self):
self.prefix = int(self.xml.attrib.get('prefix', 64).strip())
return(None)
def parse(self):
self._iface()
self._alloc()
self._prefix()
return(None)
def parse_alloc(self):
self.iface_ip = IP6(str(next(self.alloc.ip.net.hosts())), 128)
self.alloc_block = self.alloc.ip.alloc_block
return(None)
class Allocation(object):
def __init__(self, alloc_xml):
self.xml = alloc_xml
self.id = None
self.prefix = None
self.ip = None
self.iface = None
self.iface_idx = None
self.parse()
def _id(self):
self.id = self.xml.attrib['id'].strip()
return(None)
def _iface(self):
_iface_txt = self.xml.attrib['iface']
self.iface = _iface_txt.strip()
@@ -77,6 +134,7 @@ class Allocation(object):
return(None)
def parse(self):
self._id()
self._iface()
self._ip()
return(None)
@@ -90,13 +148,26 @@ class Tunnel(object):
self.server = None
self.creds = None # This should be handled externally and map to a Cred obj
self.creds_id = None
self.allocations = []
self.allocations = {}
self.assignments = []
self.parse()
def _allocations(self):
_allocs_xml = self.xml.find('allocs')
_allocs_xml = self.xml.find('allocations')
for _allocation_xml in _allocs_xml.findall('alloc'):
self.allocations.append(Allocation(_allocation_xml))
alloc = Allocation(_allocation_xml)
self.allocations[alloc.id] = alloc
return(None)
def _assignments(self):
_assigns_xml = self.xml.find('assignments')
radvd = xml2bool(_assigns_xml.attrib.get('radvd', 'false'))
radvd_dns = xml2bool(_assigns_xml.attrib.get('radvdDns', 'false'))
for _assign_xml in _assigns_xml.findall('assign'):
assign = Assignment(_assign_xml, radvd = radvd, dns = radvd_dns)
assign.alloc = self.allocations[assign.alloc_name]
assign.parse_alloc()
self.assignments.append(assign)
return(None)
def _client(self):
@@ -126,6 +197,7 @@ class Tunnel(object):
self._client()
self._server()
self._allocations()
self._assignments()
return(None)