improve default route service to detect connected routers and use the addresses of the first one found

This commit is contained in:
Blake Harnden 2020-04-02 15:12:07 -07:00
parent 72189a5c28
commit 7d392c43ac
3 changed files with 34 additions and 30 deletions

View file

@ -1,13 +1,13 @@
"""
utility.py: defines miscellaneous utility services.
"""
import os
import netaddr
from core import constants, utils
from core.errors import CoreCommandError
from core.nodes.base import CoreNode
from core.services.coreservices import CoreService, ServiceMode
@ -77,27 +77,26 @@ class DefaultRouteService(UtilService):
@classmethod
def generate_config(cls, node, filename):
# only add default routes for linked routing nodes
routes = []
for other_node in node.session.nodes.values():
if not isinstance(other_node, CoreNode):
continue
if other_node.type not in ["router", "mdr"]:
continue
commonnets = node.commonnets(other_node)
if commonnets:
_, _, router_eth = commonnets[0]
for x in router_eth.addrlist:
addr, prefix = x.split("/")
routes.append(addr)
break
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by DefaultRoute service (utility.py)\n"
for ifc in node.netifs():
if hasattr(ifc, "control") and ifc.control is True:
continue
cfg += "\n".join(map(cls.addrstr, ifc.addrlist))
cfg += "\n"
for route in routes:
cfg += f"ip route add default via {route}\n"
return cfg
@staticmethod
def addrstr(x):
net = netaddr.IPNetwork(x)
if net[1] == net[-2]:
return ""
else:
if os.uname()[0] == "Linux":
rtcmd = "ip route add default via"
else:
raise Exception("unknown platform")
return "%s %s" % (rtcmd, net[1])
class DefaultMulticastRouteService(UtilService):
name = "DefaultMulticastRoute"