From 52c6f2f31c7a7766fbeed987e0419af18da559e9 Mon Sep 17 00:00:00 2001 From: bharnden <32446120+bharnden@users.noreply.github.com> Date: Fri, 22 Nov 2019 14:52:46 -0800 Subject: [PATCH] fixed issue with services identifying ip4/ip6 addresses --- daemon/core/services/bird.py | 7 ++++--- daemon/core/services/frr.py | 13 ++++++++----- daemon/core/services/nrl.py | 7 ++++--- daemon/core/services/quagga.py | 18 ++++++++++-------- daemon/core/services/sdn.py | 6 ++++-- daemon/core/services/utility.py | 13 +++++++++---- daemon/core/services/xorp.py | 18 ++++++++++-------- 7 files changed, 49 insertions(+), 33 deletions(-) diff --git a/daemon/core/services/bird.py b/daemon/core/services/bird.py index 80774201..a0f4c640 100644 --- a/daemon/core/services/bird.py +++ b/daemon/core/services/bird.py @@ -1,7 +1,7 @@ """ bird.py: defines routing services provided by the BIRD Internet Routing Daemon. """ - +from core.nodes import ipaddress from core.services.coreservices import CoreService @@ -38,8 +38,9 @@ class Bird(CoreService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") >= 0: - return a.split("/")[0] + a = a.split("/")[0] + if ipaddress.is_ipv4_address(a): + return a # raise ValueError, "no IPv4 address found for router ID" return "0.0.0.0" diff --git a/daemon/core/services/frr.py b/daemon/core/services/frr.py index b4332009..95ffb0b5 100644 --- a/daemon/core/services/frr.py +++ b/daemon/core/services/frr.py @@ -112,9 +112,10 @@ class FRRZebra(CoreService): """ helper for mapping IP addresses to zebra config statements """ - if x.find(".") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv4_address(addr): return "ip address %s" % x - elif x.find(":") >= 0: + elif ipaddress.is_ipv6_address(addr): return "ipv6 address %s" % x else: raise ValueError("invalid address: %s", x) @@ -328,8 +329,9 @@ class FrrService(CoreService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") >= 0: - return a.split("/")[0] + a = a.split("/")[0] + if ipaddress.is_ipv4_address(a): + return a # raise ValueError, "no IPv4 address found for router ID" return "0.0.0.0" @@ -411,7 +413,8 @@ class FRROspfv2(FrrService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") < 0: + addr = a.split("/")[0] + if not ipaddress.is_ipv4_address(addr): continue net = ipaddress.Ipv4Prefix(a) cfg += " network %s area 0\n" % net diff --git a/daemon/core/services/nrl.py b/daemon/core/services/nrl.py index a610f1cc..0a6b1f92 100644 --- a/daemon/core/services/nrl.py +++ b/daemon/core/services/nrl.py @@ -4,6 +4,7 @@ nrl.py: defines services provided by NRL protolib tools hosted here: """ from core import utils +from core.nodes import ipaddress from core.nodes.ipaddress import Ipv4Prefix from core.services.coreservices import CoreService @@ -36,9 +37,9 @@ class NrlService(CoreService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") >= 0: - addr = a.split("/")[0] - pre = Ipv4Prefix("%s/%s" % (addr, prefixlen)) + a = a.split("/")[0] + if ipaddress.is_ipv4_address(a): + pre = Ipv4Prefix("%s/%s" % (a, prefixlen)) return str(pre) # raise ValueError, "no IPv4 address found" return "0.0.0.0/%s" % prefixlen diff --git a/daemon/core/services/quagga.py b/daemon/core/services/quagga.py index 5b5cf0ba..267bbcdd 100644 --- a/daemon/core/services/quagga.py +++ b/daemon/core/services/quagga.py @@ -109,9 +109,10 @@ class Zebra(CoreService): """ helper for mapping IP addresses to zebra config statements """ - if x.find(".") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv4_address(addr): return "ip address %s" % x - elif x.find(":") >= 0: + elif ipaddress.is_ipv6_address(addr): return "ipv6 address %s" % x else: raise ValueError("invalid address: %s", x) @@ -255,8 +256,9 @@ class QuaggaService(CoreService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") >= 0: - return a.split("/")[0] + a = a.split("/")[0] + if ipaddress.is_ipv4_address(a): + return a # raise ValueError, "no IPv4 address found for router ID" return "0.0.0.0" @@ -338,10 +340,10 @@ class Ospfv2(QuaggaService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") < 0: - continue - net = ipaddress.Ipv4Prefix(a) - cfg += " network %s area 0\n" % net + addr = a.split("/")[0] + if ipaddress.is_ipv4_address(addr): + net = ipaddress.Ipv4Prefix(a) + cfg += " network %s area 0\n" % net cfg += "!\n" return cfg diff --git a/daemon/core/services/sdn.py b/daemon/core/services/sdn.py index c837de53..d924abd7 100644 --- a/daemon/core/services/sdn.py +++ b/daemon/core/services/sdn.py @@ -4,6 +4,7 @@ sdn.py defines services to start Open vSwitch and the Ryu SDN Controller. import re +from core.nodes import ipaddress from core.services.coreservices import CoreService @@ -56,11 +57,12 @@ class OvsService(SdnService): # remove ip address of eths because quagga/zebra will assign same IPs to rtr interfaces # or assign them manually to rtr interfaces if zebra is not running for ifcaddr in ifc.addrlist: - if ifcaddr.find(".") >= 0: + addr = ifcaddr.split("/")[0] + if ipaddress.is_ipv4_address(addr): cfg += "ip addr del %s dev %s\n" % (ifcaddr, ifc.name) if has_zebra == 0: cfg += "ip addr add %s dev rtr%s\n" % (ifcaddr, ifnum) - elif ifcaddr.find(":") >= 0: + elif ipaddress.is_ipv6_address(addr): cfg += "ip -6 addr del %s dev %s\n" % (ifcaddr, ifc.name) if has_zebra == 0: cfg += "ip -6 addr add %s dev rtr%s\n" % (ifcaddr, ifnum) diff --git a/daemon/core/services/utility.py b/daemon/core/services/utility.py index e408b182..16dc6906 100644 --- a/daemon/core/services/utility.py +++ b/daemon/core/services/utility.py @@ -6,6 +6,7 @@ import os from core import constants, utils from core.errors import CoreCommandError +from core.nodes import ipaddress from core.nodes.ipaddress import Ipv4Prefix, Ipv6Prefix from core.services.coreservices import CoreService @@ -87,7 +88,8 @@ class DefaultRouteService(UtilService): @staticmethod def addrstr(x): - if x.find(":") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv6_address(addr): net = Ipv6Prefix(x) else: net = Ipv4Prefix(x) @@ -147,7 +149,8 @@ class StaticRouteService(UtilService): @staticmethod def routestr(x): - if x.find(":") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv6_address(addr): net = Ipv6Prefix(x) dst = "3ffe:4::/64" else: @@ -280,7 +283,8 @@ ddns-update-style none; Generate a subnet declaration block given an IPv4 prefix string for inclusion in the dhcpd3 config file. """ - if x.find(":") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv6_address(addr): return "" else: addr = x.split("/")[0] @@ -702,7 +706,8 @@ interface %s Generate a subnet declaration block given an IPv6 prefix string for inclusion in the RADVD config file. """ - if x.find(":") >= 0: + addr = x.split("/")[0] + if ipaddress.is_ipv6_address(addr): net = Ipv6Prefix(x) return str(net) else: diff --git a/daemon/core/services/xorp.py b/daemon/core/services/xorp.py index 1cd62620..3c1de852 100644 --- a/daemon/core/services/xorp.py +++ b/daemon/core/services/xorp.py @@ -4,6 +4,7 @@ xorp.py: defines routing services provided by the XORP routing suite. import logging +from core.nodes import ipaddress from core.services.coreservices import CoreService @@ -150,8 +151,9 @@ class XorpService(CoreService): if hasattr(ifc, "control") and ifc.control is True: continue for a in ifc.addrlist: - if a.find(".") >= 0: - return a.split("/")[0] + a = a.split("/")[0] + if ipaddress.is_ipv4_address(a): + return a # raise ValueError, "no IPv4 address found for router ID" return "0.0.0.0" @@ -187,9 +189,9 @@ class XorpOspfv2(XorpService): cfg += "\t interface %s {\n" % ifc.name cfg += "\t\tvif %s {\n" % ifc.name for a in ifc.addrlist: - if a.find(".") < 0: - continue addr = a.split("/")[0] + if not ipaddress.is_ipv4_address(addr): + continue cfg += "\t\t address %s {\n" % addr cfg += "\t\t }\n" cfg += "\t\t}\n" @@ -280,9 +282,9 @@ class XorpRip(XorpService): cfg += "\tinterface %s {\n" % ifc.name cfg += "\t vif %s {\n" % ifc.name for a in ifc.addrlist: - if a.find(".") < 0: - continue addr = a.split("/")[0] + if not ipaddress.is_ipv4_address(addr): + continue cfg += "\t\taddress %s {\n" % addr cfg += "\t\t disable: false\n" cfg += "\t\t}\n" @@ -462,9 +464,9 @@ class XorpOlsr(XorpService): cfg += "\tinterface %s {\n" % ifc.name cfg += "\t vif %s {\n" % ifc.name for a in ifc.addrlist: - if a.find(".") < 0: - continue addr = a.split("/")[0] + if not ipaddress.is_ipv4_address(addr): + continue cfg += "\t\taddress %s {\n" % addr cfg += "\t\t}\n" cfg += "\t }\n"