daemon: refactored CoreInterface.addrlist storing strings into CoreInterface.ip4s and ip6s, stored as netaddr.IPNetwork objects
This commit is contained in:
parent
ca2b1c9e4c
commit
d88f3a2535
20 changed files with 209 additions and 262 deletions
|
@ -1,8 +1,6 @@
|
|||
import abc
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import constants
|
||||
from core.config import Configuration
|
||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||
|
@ -49,10 +47,9 @@ def get_router_id(node: CoreNodeBase) -> str:
|
|||
Helper to return the first IPv4 address of a node as its router ID.
|
||||
"""
|
||||
for iface in node.get_ifaces(control=False):
|
||||
for a in iface.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
return str(ip4.ip)
|
||||
return "0.0.0.0"
|
||||
|
||||
|
||||
|
@ -102,12 +99,10 @@ class FRRZebra(ConfigService):
|
|||
for iface in self.node.get_ifaces():
|
||||
ip4s = []
|
||||
ip6s = []
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
ip4s.append(x)
|
||||
else:
|
||||
ip6s.append(x)
|
||||
for ip4 in iface.ip4s:
|
||||
ip4s.append(str(ip4.ip))
|
||||
for ip6 in iface.ip6s:
|
||||
ip6s.append(str(ip6.ip))
|
||||
is_control = getattr(iface, "control", False)
|
||||
ifaces.append((iface, ip4s, ip6s, is_control))
|
||||
|
||||
|
@ -163,10 +158,8 @@ class FRROspfv2(FrrService, ConfigService):
|
|||
router_id = get_router_id(self.node)
|
||||
addresses = []
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
for a in iface.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
addresses.append(a)
|
||||
for ip4 in iface.ip4s:
|
||||
addresses.append(str(ip4.ip))
|
||||
data = dict(router_id=router_id, addresses=addresses)
|
||||
text = """
|
||||
router ospf
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
from typing import Any, Dict, List
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import utils
|
||||
from core.config import Configuration
|
||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||
|
@ -75,13 +73,10 @@ class NrlSmf(ConfigService):
|
|||
ip4_prefix = None
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
ifnames.append(iface.name)
|
||||
if ip4_prefix:
|
||||
continue
|
||||
for a in iface.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(a):
|
||||
ip4_prefix = f"{a}/{24}"
|
||||
break
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
ip4_prefix = f"{ip4.ip}/{24}"
|
||||
break
|
||||
return dict(
|
||||
has_arouted=has_arouted,
|
||||
has_nhdp=has_nhdp,
|
||||
|
@ -191,11 +186,8 @@ class Arouted(ConfigService):
|
|||
def data(self) -> Dict[str, Any]:
|
||||
ip4_prefix = None
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
if ip4_prefix:
|
||||
continue
|
||||
for a in iface.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(a):
|
||||
ip4_prefix = f"{a}/{24}"
|
||||
break
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
ip4_prefix = f"{ip4.ip}/{24}"
|
||||
break
|
||||
return dict(ip4_prefix=ip4_prefix)
|
||||
|
|
|
@ -2,8 +2,6 @@ import abc
|
|||
import logging
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import constants
|
||||
from core.config import Configuration
|
||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||
|
@ -50,10 +48,9 @@ def get_router_id(node: CoreNodeBase) -> str:
|
|||
Helper to return the first IPv4 address of a node as its router ID.
|
||||
"""
|
||||
for iface in node.get_ifaces(control=False):
|
||||
for a in iface.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
return str(ip4.ip)
|
||||
return "0.0.0.0"
|
||||
|
||||
|
||||
|
@ -103,12 +100,10 @@ class Zebra(ConfigService):
|
|||
for iface in self.node.get_ifaces():
|
||||
ip4s = []
|
||||
ip6s = []
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
ip4s.append(x)
|
||||
else:
|
||||
ip6s.append(x)
|
||||
for ip4 in iface.ip4s:
|
||||
ip4s.append(str(ip4.ip))
|
||||
for ip6 in iface.ip6s:
|
||||
ip6s.append(str(ip6.ip))
|
||||
is_control = getattr(iface, "control", False)
|
||||
ifaces.append((iface, ip4s, ip6s, is_control))
|
||||
|
||||
|
@ -170,10 +165,8 @@ class Ospfv2(QuaggaService, ConfigService):
|
|||
router_id = get_router_id(self.node)
|
||||
addresses = []
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
for a in iface.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
addresses.append(a)
|
||||
for ip4 in iface.ip4s:
|
||||
addresses.append(str(ip4.ip))
|
||||
data = dict(router_id=router_id, addresses=addresses)
|
||||
text = """
|
||||
router ospf
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
from typing import Any, Dict, List
|
||||
|
||||
import netaddr
|
||||
|
||||
from core.config import Configuration
|
||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||
from core.emulator.enumerations import ConfigDataTypes
|
||||
|
@ -79,10 +77,10 @@ class VpnServer(ConfigService):
|
|||
def data(self) -> Dict[str, Any]:
|
||||
address = None
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
address = addr
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
address = str(ip4.ip)
|
||||
break
|
||||
return dict(address=address)
|
||||
|
||||
|
||||
|
|
|
@ -29,8 +29,8 @@ class DefaultRouteService(ConfigService):
|
|||
ifaces = self.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))
|
||||
|
@ -76,15 +76,14 @@ class StaticRouteService(ConfigService):
|
|||
def data(self) -> Dict[str, Any]:
|
||||
routes = []
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv6(addr):
|
||||
for ip in iface.all_ips():
|
||||
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]:
|
||||
routes.append((dst, net[1]))
|
||||
if ip[-2] != ip[1]:
|
||||
routes.append((dst, ip[1]))
|
||||
return dict(routes=routes)
|
||||
|
||||
|
||||
|
@ -149,15 +148,12 @@ class DhcpService(ConfigService):
|
|||
def data(self) -> Dict[str, Any]:
|
||||
subnets = []
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv4(addr):
|
||||
net = netaddr.IPNetwork(x)
|
||||
# divide the address space in half
|
||||
index = (net.size - 2) / 2
|
||||
rangelow = net[index]
|
||||
rangehigh = net[-2]
|
||||
subnets.append((net.ip, net.netmask, rangelow, rangehigh, addr))
|
||||
for ip4 in iface.ip4s:
|
||||
# divide the address space in half
|
||||
index = (ip4.size - 2) / 2
|
||||
rangelow = ip4[index]
|
||||
rangehigh = ip4[-2]
|
||||
subnets.append((ip4.ip, ip4.netmask, rangelow, rangehigh, str(ip4.ip)))
|
||||
return dict(subnets=subnets)
|
||||
|
||||
|
||||
|
@ -238,10 +234,8 @@ class RadvdService(ConfigService):
|
|||
ifaces = []
|
||||
for iface in self.node.get_ifaces(control=False):
|
||||
prefixes = []
|
||||
for x in iface.addrlist:
|
||||
addr = x.split("/")[0]
|
||||
if netaddr.valid_ipv6(addr):
|
||||
prefixes.append(x)
|
||||
for ip6 in iface.ip6s:
|
||||
prefixes.append(str(ip6))
|
||||
if not prefixes:
|
||||
continue
|
||||
ifaces.append((iface.name, prefixes))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue