changes to replace MacAddress usage and remove ipaddress module altogether

This commit is contained in:
Blake Harnden 2020-01-08 17:33:49 -08:00
parent 4db7f845a5
commit de493c810a
11 changed files with 43 additions and 83 deletions

View file

@ -9,8 +9,8 @@ from contextlib import contextmanager
import grpc import grpc
import netaddr import netaddr
from core import utils
from core.api.grpc import core_pb2, core_pb2_grpc from core.api.grpc import core_pb2, core_pb2_grpc
from core.nodes.ipaddress import MacAddress
class InterfaceHelper: class InterfaceHelper:
@ -88,7 +88,7 @@ class InterfaceHelper:
# random mac # random mac
if not mac: if not mac:
mac = MacAddress.random() mac = utils.random_mac()
return core_pb2.Interface( return core_pb2.Interface(
id=interface_id, id=interface_id,

View file

@ -6,7 +6,6 @@ from core.api.grpc import core_pb2
from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions
from core.emulator.enumerations import LinkTypes, NodeTypes from core.emulator.enumerations import LinkTypes, NodeTypes
from core.nodes.base import CoreNetworkBase from core.nodes.base import CoreNetworkBase
from core.nodes.ipaddress import MacAddress
WORKERS = 10 WORKERS = 10
@ -57,8 +56,6 @@ def link_interface(interface_proto):
mac = interface_proto.mac mac = interface_proto.mac
if mac == "": if mac == "":
mac = None mac = None
else:
mac = MacAddress.from_string(mac)
interface = InterfaceData( interface = InterfaceData(
_id=interface_proto.id, _id=interface_proto.id,
name=name, name=name,

View file

@ -5,10 +5,13 @@ types and objects used for parsing and building CORE API messages.
CORE API messaging is leveraged for communication with the GUI. CORE API messaging is leveraged for communication with the GUI.
""" """
import binascii
import socket import socket
import struct import struct
from enum import Enum from enum import Enum
import netaddr
from core.api.tlv import structutils from core.api.tlv import structutils
from core.emulator.enumerations import ( from core.emulator.enumerations import (
ConfigTlvs, ConfigTlvs,
@ -24,7 +27,6 @@ from core.emulator.enumerations import (
RegisterTlvs, RegisterTlvs,
SessionTlvs, SessionTlvs,
) )
from core.nodes.ipaddress import MacAddress
class CoreTlvData: class CoreTlvData:
@ -323,7 +325,7 @@ class CoreTlvDataMacAddr(CoreTlvDataObj):
""" """
data_format = "!2x8s" data_format = "!2x8s"
data_type = MacAddress.from_string data_type = str
pad_len = 2 pad_len = 2
@staticmethod @staticmethod
@ -331,23 +333,27 @@ class CoreTlvDataMacAddr(CoreTlvDataObj):
""" """
Retrieve Ipv6 address value from object. Retrieve Ipv6 address value from object.
:param core.nodes.ipaddress.MacAddress obj: mac address to get value from :param str obj: mac address to get value from
:return: :return: packed mac address
:rtype: bytes
""" """
# extend to 64 bits # extend to 64 bits
return b"\0\0" + obj.addr return b"\0\0" + netaddr.EUI(obj).packed
@staticmethod @staticmethod
def new_obj(value): def new_obj(value):
""" """
Retrieve mac address from a string representation. Retrieve mac address from a string representation.
:param str value: value to get Ipv4 address from :param bytes value: value to get Ipv4 address from
:return: Ipv4 address :return: mac address
:rtype: core.nodes.ipaddress.MacAddress :rtype: str
""" """
# only use 48 bits # only use 48 bits
return MacAddress(address=value[2:]) value = binascii.hexlify(value[2:]).decode()
mac = netaddr.EUI(value)
mac.dialect = netaddr.mac_unix
return str(mac)
class CoreTlv: class CoreTlv:

View file

@ -1,8 +1,8 @@
import netaddr import netaddr
from core import utils
from core.emane.nodes import EmaneNet from core.emane.nodes import EmaneNet
from core.emulator.enumerations import LinkTypes from core.emulator.enumerations import LinkTypes
from core.nodes.ipaddress import MacAddress
from core.nodes.physical import PhysicalNode from core.nodes.physical import PhysicalNode
@ -226,7 +226,7 @@ class IpPrefixes:
# random mac # random mac
if not mac: if not mac:
mac = MacAddress.random() mac = utils.random_mac()
return InterfaceData( return InterfaceData(
_id=inteface_id, _id=inteface_id,
@ -250,7 +250,7 @@ class InterfaceData:
:param int _id: interface id :param int _id: interface id
:param str name: name for interface :param str name: name for interface
:param core.nodes.ipaddress.MacAddress mac: mac address :param str mac: mac address
:param str ip4: ipv4 address :param str ip4: ipv4 address
:param int ip4_mask: ipv4 bit mask :param int ip4_mask: ipv4 bit mask
:param str ip6: ipv6 address :param str ip6: ipv6 address

View file

@ -33,7 +33,6 @@ from core.location.event import EventLoop
from core.location.mobility import BasicRangeModel, MobilityManager from core.location.mobility import BasicRangeModel, MobilityManager
from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase
from core.nodes.docker import DockerNode from core.nodes.docker import DockerNode
from core.nodes.ipaddress import MacAddress
from core.nodes.lxd import LxcNode from core.nodes.lxd import LxcNode
from core.nodes.network import ( from core.nodes.network import (
CtrlNet, CtrlNet,
@ -1778,7 +1777,7 @@ class Session:
net=control_net, net=control_net,
ifindex=control_net.CTRLIF_IDX_BASE + net_index, ifindex=control_net.CTRLIF_IDX_BASE + net_index,
ifname=f"ctrl{net_index}", ifname=f"ctrl{net_index}",
hwaddr=MacAddress.random(), hwaddr=utils.random_mac(),
addrlist=addrlist, addrlist=addrlist,
) )
node.netif(interface1).control = True node.netif(interface1).control = True

View file

@ -725,14 +725,14 @@ class CoreNode(CoreNodeBase):
Set hardware addres for an interface. Set hardware addres for an interface.
:param int ifindex: index of interface to set hardware address for :param int ifindex: index of interface to set hardware address for
:param core.nodes.ipaddress.MacAddress addr: hardware address to set :param str addr: hardware address to set
:return: nothing :return: nothing
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
interface = self._netif[ifindex] interface = self._netif[ifindex]
interface.sethwaddr(addr) interface.sethwaddr(addr)
if self.up: if self.up:
self.node_net_client.device_mac(interface.name, str(addr)) self.node_net_client.device_mac(interface.name, addr)
def addaddr(self, ifindex, addr): def addaddr(self, ifindex, addr):
""" """
@ -787,7 +787,7 @@ class CoreNode(CoreNodeBase):
:param core.nodes.base.CoreNetworkBase net: network to associate with :param core.nodes.base.CoreNetworkBase net: network to associate with
:param list addrlist: addresses to add on the interface :param list addrlist: addresses to add on the interface
:param core.nodes.ipaddress.MacAddress hwaddr: hardware address to set for interface :param str hwaddr: hardware address to set for interface
:param int ifindex: index of interface to create :param int ifindex: index of interface to create
:param str ifname: name for interface :param str ifname: name for interface
:return: interface index :return: interface index

View file

@ -130,7 +130,7 @@ class CoreInterface:
""" """
Set hardware address. Set hardware address.
:param core.nodes.ipaddress.MacAddress addr: hardware address to set to. :param str addr: hardware address to set to.
:return: nothing :return: nothing
""" """
self.hwaddr = addr self.hwaddr = addr

View file

@ -1,55 +0,0 @@
"""
Helper objects for dealing with IPv4/v6 addresses.
"""
import random
import struct
class MacAddress:
"""
Provides mac address utilities for use within core.
"""
def __init__(self, address):
"""
Creates a MacAddress instance.
:param bytes address: mac address
"""
self.addr = address
def __str__(self):
"""
Create a string representation of a MacAddress.
:return: string representation
:rtype: str
"""
return ":".join(f"{x:02x}" for x in bytearray(self.addr))
@classmethod
def from_string(cls, s):
"""
Create a mac address object from a string.
:param s: string representation of a mac address
:return: mac address class
:rtype: MacAddress
"""
addr = b"".join(bytes([int(x, 16)]) for x in s.split(":"))
return cls(addr)
@classmethod
def random(cls):
"""
Create a random mac address.
:return: random mac address
:rtype: MacAddress
"""
tmp = random.randint(0, 0xFFFFFF)
# use the Xen OID 00:16:3E
tmp |= 0x00163E << 24
tmpbytes = struct.pack("!Q", tmp)
return cls(tmpbytes[2:])

View file

@ -11,10 +11,13 @@ import json
import logging import logging
import logging.config import logging.config
import os import os
import random
import shlex import shlex
import sys import sys
from subprocess import PIPE, STDOUT, Popen from subprocess import PIPE, STDOUT, Popen
import netaddr
from core.errors import CoreCommandError from core.errors import CoreCommandError
DEVNULL = open(os.devnull, "wb") DEVNULL = open(os.devnull, "wb")
@ -408,3 +411,17 @@ def threadpool(funcs, workers=10):
except Exception as e: except Exception as e:
exceptions.append(e) exceptions.append(e)
return results, exceptions return results, exceptions
def random_mac():
"""
Create a random mac address using Xen OID 00:16:3E.
:return: random mac address
:rtype: str
"""
value = random.randint(0, 0xFFFFFF)
value |= 0x00163E << 24
mac = netaddr.EUI(value)
mac.dialect = netaddr.mac_unix
return str(mac)

View file

@ -8,7 +8,6 @@ from core.emane.nodes import EmaneNet
from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions
from core.emulator.enumerations import NodeTypes from core.emulator.enumerations import NodeTypes
from core.nodes.base import CoreNetworkBase from core.nodes.base import CoreNetworkBase
from core.nodes.ipaddress import MacAddress
from core.nodes.network import CtrlNet from core.nodes.network import CtrlNet
@ -48,8 +47,6 @@ def create_interface_data(interface_element):
interface_id = int(interface_element.get("id")) interface_id = int(interface_element.get("id"))
name = interface_element.get("name") name = interface_element.get("name")
mac = interface_element.get("mac") mac = interface_element.get("mac")
if mac:
mac = MacAddress.from_string(mac)
ip4 = interface_element.get("ip4") ip4 = interface_element.get("ip4")
ip4_mask = get_int(interface_element, "ip4_mask") ip4_mask = get_int(interface_element, "ip4_mask")
ip6 = interface_element.get("ip6") ip6 = interface_element.get("ip6")

View file

@ -5,7 +5,6 @@ from tempfile import NamedTemporaryFile
from lxml import etree from lxml import etree
from core import utils from core import utils
from core.nodes.ipaddress import MacAddress
from core.xml import corexml from core.xml import corexml
_hwaddr_prefix = "02:02" _hwaddr_prefix = "02:02"
@ -208,7 +207,7 @@ def build_node_platform_xml(emane_manager, control_net, node, nem_id, platform_x
node.setnemid(netif, nem_id) node.setnemid(netif, nem_id)
macstr = _hwaddr_prefix + ":00:00:" macstr = _hwaddr_prefix + ":00:00:"
macstr += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}" macstr += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}"
netif.sethwaddr(MacAddress.from_string(macstr)) netif.sethwaddr(macstr)
# increment nem id # increment nem id
nem_id += 1 nem_id += 1