initial changes towards removing ipaddress and using netaddr
This commit is contained in:
parent
93ce2a3755
commit
f431254e15
10 changed files with 47 additions and 67 deletions
|
@ -9,6 +9,8 @@ import socket
|
|||
import threading
|
||||
from socket import AF_INET, AF_INET6
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import utils
|
||||
from core.constants import MOUNT_BIN, VNODED_BIN
|
||||
from core.emulator.data import LinkData, NodeData
|
||||
|
@ -1015,7 +1017,7 @@ class CoreNetworkBase(NodeBase):
|
|||
for address in netif.addrlist:
|
||||
ip, _sep, mask = address.partition("/")
|
||||
mask = int(mask)
|
||||
if ipaddress.is_ipv4_address(ip):
|
||||
if netaddr.valid_ipv4(ip):
|
||||
family = AF_INET
|
||||
ipl = socket.inet_pton(family, ip)
|
||||
interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
||||
|
|
|
@ -8,6 +8,8 @@ import threading
|
|||
import time
|
||||
from socket import AF_INET, AF_INET6
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import utils
|
||||
from core.constants import EBTABLES_BIN, TC_BIN
|
||||
from core.emulator.data import LinkData
|
||||
|
@ -908,7 +910,7 @@ class PtpNet(CoreNetwork):
|
|||
for address in if1.addrlist:
|
||||
ip, _sep, mask = address.partition("/")
|
||||
mask = int(mask)
|
||||
if ipaddress.is_ipv4_address(ip):
|
||||
if netaddr.valid_ipv4(ip):
|
||||
family = AF_INET
|
||||
ipl = socket.inet_pton(family, ip)
|
||||
interface1_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
||||
|
@ -926,7 +928,7 @@ class PtpNet(CoreNetwork):
|
|||
for address in if2.addrlist:
|
||||
ip, _sep, mask = address.partition("/")
|
||||
mask = int(mask)
|
||||
if ipaddress.is_ipv4_address(ip):
|
||||
if netaddr.valid_ipv4(ip):
|
||||
family = AF_INET
|
||||
ipl = socket.inet_pton(family, ip)
|
||||
interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
"""
|
||||
bird.py: defines routing services provided by the BIRD Internet Routing Daemon.
|
||||
"""
|
||||
from core.nodes import ipaddress
|
||||
import netaddr
|
||||
|
||||
from core.services.coreservices import CoreService
|
||||
|
||||
|
||||
|
@ -39,7 +40,7 @@ class Bird(CoreService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(a):
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
# raise ValueError, "no IPv4 address found for router ID"
|
||||
return "0.0.0.0"
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
frr.py: defines routing services provided by FRRouting.
|
||||
Assumes installation of FRR via https://deb.frrouting.org/
|
||||
"""
|
||||
import netaddr
|
||||
|
||||
from core import constants
|
||||
from core.emulator.enumerations import LinkTypes
|
||||
from core.nodes import ipaddress
|
||||
from core.nodes.network import PtpNet
|
||||
from core.nodes.physical import Rj45Node
|
||||
from core.services.coreservices import CoreService
|
||||
|
@ -85,7 +85,7 @@ class FRRZebra(CoreService):
|
|||
|
||||
if want_ipv4:
|
||||
ipv4list = filter(
|
||||
lambda x: ipaddress.is_ipv4_address(x.split("/")[0]), ifc.addrlist
|
||||
lambda x: netaddr.valid_ipv4(x.split("/")[0]), ifc.addrlist
|
||||
)
|
||||
cfg += " "
|
||||
cfg += "\n ".join(map(cls.addrstr, ipv4list))
|
||||
|
@ -93,7 +93,7 @@ class FRRZebra(CoreService):
|
|||
cfg += cfgv4
|
||||
if want_ipv6:
|
||||
ipv6list = filter(
|
||||
lambda x: ipaddress.is_ipv6_address(x.split("/")[0]), ifc.addrlist
|
||||
lambda x: netaddr.valid_ipv6(x.split("/")[0]), ifc.addrlist
|
||||
)
|
||||
cfg += " "
|
||||
cfg += "\n ".join(map(cls.addrstr, ipv6list))
|
||||
|
@ -113,9 +113,9 @@ class FRRZebra(CoreService):
|
|||
helper for mapping IP addresses to zebra config statements
|
||||
"""
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(addr):
|
||||
if netaddr.valid_ipv4(addr):
|
||||
return "ip address %s" % x
|
||||
elif ipaddress.is_ipv6_address(addr):
|
||||
elif netaddr.valid_ipv6(addr):
|
||||
return "ipv6 address %s" % x
|
||||
else:
|
||||
raise ValueError("invalid address: %s", x)
|
||||
|
@ -330,7 +330,7 @@ class FrrService(CoreService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(a):
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
# raise ValueError, "no IPv4 address found for router ID"
|
||||
return "0.0.0.0"
|
||||
|
@ -414,29 +414,15 @@ class FRROspfv2(FrrService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if not ipaddress.is_ipv4_address(addr):
|
||||
if not netaddr.valid_ipv4(addr):
|
||||
continue
|
||||
net = ipaddress.Ipv4Prefix(a)
|
||||
cfg += " network %s area 0\n" % net
|
||||
cfg += " network %s area 0\n" % a
|
||||
cfg += "!\n"
|
||||
return cfg
|
||||
|
||||
@classmethod
|
||||
def generatefrrifcconfig(cls, node, ifc):
|
||||
return cls.mtucheck(ifc)
|
||||
# cfg = cls.mtucheck(ifc)
|
||||
# external RJ45 connections will use default OSPF timers
|
||||
# if cls.rj45check(ifc):
|
||||
# return cfg
|
||||
# cfg += cls.ptpcheck(ifc)
|
||||
|
||||
# return cfg + """\
|
||||
|
||||
|
||||
# ip ospf hello-interval 2
|
||||
# ip ospf dead-interval 6
|
||||
# ip ospf retransmit-interval 5
|
||||
# """
|
||||
|
||||
|
||||
class FRROspfv3(FrrService):
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
nrl.py: defines services provided by NRL protolib tools hosted here:
|
||||
http://www.nrl.navy.mil/itd/ncs/products
|
||||
"""
|
||||
import netaddr
|
||||
|
||||
from core import utils
|
||||
from core.nodes import ipaddress
|
||||
from core.nodes.ipaddress import Ipv4Prefix
|
||||
from core.services.coreservices import CoreService
|
||||
|
||||
|
@ -38,7 +38,7 @@ class NrlService(CoreService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(a):
|
||||
if netaddr.valid_ipv4(a):
|
||||
pre = Ipv4Prefix("%s/%s" % (a, prefixlen))
|
||||
return str(pre)
|
||||
# raise ValueError, "no IPv4 address found"
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
"""
|
||||
quagga.py: defines routing services provided by Quagga.
|
||||
"""
|
||||
import netaddr
|
||||
|
||||
from core import constants
|
||||
from core.emane.nodes import EmaneNet
|
||||
from core.emulator.enumerations import LinkTypes
|
||||
from core.nodes import ipaddress
|
||||
from core.nodes.network import PtpNet, WlanNode
|
||||
from core.nodes.physical import Rj45Node
|
||||
from core.services.coreservices import CoreService
|
||||
|
@ -82,7 +82,7 @@ class Zebra(CoreService):
|
|||
|
||||
if want_ipv4:
|
||||
ipv4list = filter(
|
||||
lambda x: ipaddress.is_ipv4_address(x.split("/")[0]), ifc.addrlist
|
||||
lambda x: netaddr.valid_ipv4(x.split("/")[0]), ifc.addrlist
|
||||
)
|
||||
cfg += " "
|
||||
cfg += "\n ".join(map(cls.addrstr, ipv4list))
|
||||
|
@ -90,7 +90,7 @@ class Zebra(CoreService):
|
|||
cfg += cfgv4
|
||||
if want_ipv6:
|
||||
ipv6list = filter(
|
||||
lambda x: ipaddress.is_ipv6_address(x.split("/")[0]), ifc.addrlist
|
||||
lambda x: netaddr.valid_ipv6(x.split("/")[0]), ifc.addrlist
|
||||
)
|
||||
cfg += " "
|
||||
cfg += "\n ".join(map(cls.addrstr, ipv6list))
|
||||
|
@ -110,9 +110,9 @@ class Zebra(CoreService):
|
|||
helper for mapping IP addresses to zebra config statements
|
||||
"""
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(addr):
|
||||
if netaddr.valid_ipv4(addr):
|
||||
return "ip address %s" % x
|
||||
elif ipaddress.is_ipv6_address(addr):
|
||||
elif netaddr.valid_ipv6(addr):
|
||||
return "ipv6 address %s" % x
|
||||
else:
|
||||
raise ValueError("invalid address: %s", x)
|
||||
|
@ -257,7 +257,7 @@ class QuaggaService(CoreService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(a):
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
# raise ValueError, "no IPv4 address found for router ID"
|
||||
return "0.0.0.0"
|
||||
|
@ -341,28 +341,14 @@ class Ospfv2(QuaggaService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(addr):
|
||||
net = ipaddress.Ipv4Prefix(a)
|
||||
cfg += " network %s area 0\n" % net
|
||||
if netaddr.valid_ipv4(addr):
|
||||
cfg += " network %s area 0\n" % a
|
||||
cfg += "!\n"
|
||||
return cfg
|
||||
|
||||
@classmethod
|
||||
def generatequaggaifcconfig(cls, node, ifc):
|
||||
return cls.mtucheck(ifc)
|
||||
# cfg = cls.mtucheck(ifc)
|
||||
# external RJ45 connections will use default OSPF timers
|
||||
# if cls.rj45check(ifc):
|
||||
# return cfg
|
||||
# cfg += cls.ptpcheck(ifc)
|
||||
|
||||
# return cfg + """\
|
||||
|
||||
|
||||
# ip ospf hello-interval 2
|
||||
# ip ospf dead-interval 6
|
||||
# ip ospf retransmit-interval 5
|
||||
# """
|
||||
|
||||
|
||||
class Ospfv3(QuaggaService):
|
||||
|
|
|
@ -4,7 +4,8 @@ sdn.py defines services to start Open vSwitch and the Ryu SDN Controller.
|
|||
|
||||
import re
|
||||
|
||||
from core.nodes import ipaddress
|
||||
import netaddr
|
||||
|
||||
from core.services.coreservices import CoreService
|
||||
|
||||
|
||||
|
@ -58,11 +59,11 @@ class OvsService(SdnService):
|
|||
# or assign them manually to rtr interfaces if zebra is not running
|
||||
for ifcaddr in ifc.addrlist:
|
||||
addr = ifcaddr.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(addr):
|
||||
if netaddr.valid_ipv4(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 ipaddress.is_ipv6_address(addr):
|
||||
elif netaddr.valid_ipv6(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)
|
||||
|
|
|
@ -4,9 +4,10 @@ utility.py: defines miscellaneous utility services.
|
|||
|
||||
import os
|
||||
|
||||
import netaddr
|
||||
|
||||
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, ServiceMode
|
||||
|
||||
|
@ -89,7 +90,7 @@ class DefaultRouteService(UtilService):
|
|||
@staticmethod
|
||||
def addrstr(x):
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv6_address(addr):
|
||||
if netaddr.valid_ipv6(addr):
|
||||
net = Ipv6Prefix(x)
|
||||
else:
|
||||
net = Ipv4Prefix(x)
|
||||
|
@ -150,7 +151,7 @@ class StaticRouteService(UtilService):
|
|||
@staticmethod
|
||||
def routestr(x):
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv6_address(addr):
|
||||
if netaddr.valid_ipv6(addr):
|
||||
net = Ipv6Prefix(x)
|
||||
dst = "3ffe:4::/64"
|
||||
else:
|
||||
|
@ -285,7 +286,7 @@ ddns-update-style none;
|
|||
for inclusion in the dhcpd3 config file.
|
||||
"""
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv6_address(addr):
|
||||
if netaddr.valid_ipv6(addr):
|
||||
return ""
|
||||
else:
|
||||
addr = x.split("/")[0]
|
||||
|
@ -708,7 +709,7 @@ interface %s
|
|||
for inclusion in the RADVD config file.
|
||||
"""
|
||||
addr = x.split("/")[0]
|
||||
if ipaddress.is_ipv6_address(addr):
|
||||
if netaddr.valid_ipv6(addr):
|
||||
net = Ipv6Prefix(x)
|
||||
return str(net)
|
||||
else:
|
||||
|
|
|
@ -4,7 +4,8 @@ xorp.py: defines routing services provided by the XORP routing suite.
|
|||
|
||||
import logging
|
||||
|
||||
from core.nodes import ipaddress
|
||||
import netaddr
|
||||
|
||||
from core.services.coreservices import CoreService
|
||||
|
||||
|
||||
|
@ -152,7 +153,7 @@ class XorpService(CoreService):
|
|||
continue
|
||||
for a in ifc.addrlist:
|
||||
a = a.split("/")[0]
|
||||
if ipaddress.is_ipv4_address(a):
|
||||
if netaddr.valid_ipv4(a):
|
||||
return a
|
||||
# raise ValueError, "no IPv4 address found for router ID"
|
||||
return "0.0.0.0"
|
||||
|
@ -190,7 +191,7 @@ class XorpOspfv2(XorpService):
|
|||
cfg += "\t\tvif %s {\n" % ifc.name
|
||||
for a in ifc.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if not ipaddress.is_ipv4_address(addr):
|
||||
if not netaddr.valid_ipv4(addr):
|
||||
continue
|
||||
cfg += "\t\t address %s {\n" % addr
|
||||
cfg += "\t\t }\n"
|
||||
|
@ -283,7 +284,7 @@ class XorpRip(XorpService):
|
|||
cfg += "\t vif %s {\n" % ifc.name
|
||||
for a in ifc.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if not ipaddress.is_ipv4_address(addr):
|
||||
if not netaddr.valid_ipv4(addr):
|
||||
continue
|
||||
cfg += "\t\taddress %s {\n" % addr
|
||||
cfg += "\t\t disable: false\n"
|
||||
|
@ -465,7 +466,7 @@ class XorpOlsr(XorpService):
|
|||
cfg += "\t vif %s {\n" % ifc.name
|
||||
for a in ifc.addrlist:
|
||||
addr = a.split("/")[0]
|
||||
if not ipaddress.is_ipv4_address(addr):
|
||||
if not netaddr.valid_ipv4(addr):
|
||||
continue
|
||||
cfg += "\t\taddress %s {\n" % addr
|
||||
cfg += "\t\t}\n"
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import os
|
||||
import socket
|
||||
|
||||
import netaddr
|
||||
from lxml import etree
|
||||
|
||||
from core import utils
|
||||
from core.constants import IP_BIN
|
||||
from core.emane.nodes import EmaneNet
|
||||
from core.nodes import ipaddress
|
||||
from core.nodes.base import CoreNodeBase
|
||||
|
||||
|
||||
|
@ -56,9 +56,9 @@ def add_emane_interface(host_element, netif, platform_name="p1", transport_name=
|
|||
|
||||
def get_address_type(address):
|
||||
addr, _slash, _prefixlen = address.partition("/")
|
||||
if ipaddress.is_ipv4_address(addr):
|
||||
if netaddr.valid_ipv4(addr):
|
||||
address_type = "IPv4"
|
||||
elif ipaddress.is_ipv6_address(addr):
|
||||
elif netaddr.valid_ipv6(addr):
|
||||
address_type = "IPv6"
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
|
Loading…
Add table
Reference in a new issue