daemon: refactored CoreInterface.addrlist storing strings into CoreInterface.ip4s and ip6s, stored as netaddr.IPNetwork objects

This commit is contained in:
Blake Harnden 2020-06-19 08:50:36 -07:00
parent ca2b1c9e4c
commit d88f3a2535
20 changed files with 209 additions and 262 deletions

View file

@ -74,8 +74,8 @@ class DefaultRouteService(UtilService):
ifaces = node.get_ifaces()
if ifaces:
iface = ifaces[0]
for x in iface.addrlist:
net = netaddr.IPNetwork(x).cidr
for ip in iface.all_ips():
net = ip.cidr
if net.size > 1:
router = net[1]
routes.append(str(router))
@ -118,23 +118,22 @@ class StaticRouteService(UtilService):
cfg += "# NOTE: this service must be customized to be of any use\n"
cfg += "# Below are samples that you can uncomment and edit.\n#\n"
for iface in node.get_ifaces(control=False):
cfg += "\n".join(map(cls.routestr, iface.addrlist))
cfg += "\n".join(map(cls.routestr, iface.all_ips()))
cfg += "\n"
return cfg
@staticmethod
def routestr(x: str) -> str:
addr = x.split("/")[0]
if netaddr.valid_ipv6(addr):
def routestr(ip: netaddr.IPNetwork) -> str:
address = str(ip.ip)
if netaddr.valid_ipv6(address):
dst = "3ffe:4::/64"
else:
dst = "10.9.8.0/24"
net = netaddr.IPNetwork(x)
if net[-2] == net[1]:
if ip[-2] == ip[1]:
return ""
else:
rtcmd = "#/sbin/ip route add %s via" % dst
return "%s %s" % (rtcmd, net[1])
return "%s %s" % (rtcmd, ip[1])
class SshService(UtilService):
@ -242,25 +241,24 @@ max-lease-time 7200;
ddns-update-style none;
"""
for iface in node.get_ifaces(control=False):
cfg += "\n".join(map(cls.subnetentry, iface.addrlist))
cfg += "\n".join(map(cls.subnetentry, iface.all_ips()))
cfg += "\n"
return cfg
@staticmethod
def subnetentry(x: str) -> str:
def subnetentry(ip: netaddr.IPNetwork) -> str:
"""
Generate a subnet declaration block given an IPv4 prefix string
for inclusion in the dhcpd3 config file.
"""
addr = x.split("/")[0]
if netaddr.valid_ipv6(addr):
address = str(ip.ip)
if netaddr.valid_ipv6(address):
return ""
else:
net = netaddr.IPNetwork(x)
# divide the address space in half
index = (net.size - 2) / 2
rangelow = net[index]
rangehigh = net[-2]
index = (ip.size - 2) / 2
rangelow = ip[index]
rangehigh = ip[-2]
return """
subnet %s netmask %s {
pool {
@ -270,11 +268,11 @@ subnet %s netmask %s {
}
}
""" % (
net.ip,
net.netmask,
ip.ip,
ip.netmask,
rangelow,
rangehigh,
addr,
address,
)
@ -557,7 +555,10 @@ export LANG
% node.name
)
for iface in node.get_ifaces(control=False):
body += "<li>%s - %s</li>\n" % (iface.name, iface.addrlist)
body += "<li>%s - %s</li>\n" % (
iface.name,
[str(x) for x in iface.all_ips()],
)
return "<html><body>%s</body></html>" % body
@ -625,7 +626,7 @@ class RadvdService(UtilService):
"""
cfg = "# auto-generated by RADVD service (utility.py)\n"
for iface in node.get_ifaces(control=False):
prefixes = list(map(cls.subnetentry, iface.addrlist))
prefixes = list(map(cls.subnetentry, iface.all_ips()))
if len(prefixes) < 1:
continue
cfg += (
@ -658,14 +659,14 @@ interface %s
return cfg
@staticmethod
def subnetentry(x: str) -> str:
def subnetentry(ip: netaddr.IPNetwork) -> str:
"""
Generate a subnet declaration block given an IPv6 prefix string
for inclusion in the RADVD config file.
"""
addr = x.split("/")[0]
if netaddr.valid_ipv6(addr):
return x
address = str(ip.ip)
if netaddr.valid_ipv6(address):
return str(ip)
else:
return ""