daemon: refactored CoreInterface.mac from a string to a netaddr.EUI object, providing more functionality

This commit is contained in:
Blake Harnden 2020-06-19 15:21:45 -07:00
parent cfda9509a2
commit 1829a8e2f8
8 changed files with 25 additions and 23 deletions

View file

@ -750,7 +750,6 @@ class CoreNode(CoreNodeBase):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
mac = utils.validate_mac(mac)
iface = self.get_iface(iface_id)
iface.set_mac(mac)
if self.up:
@ -1059,7 +1058,7 @@ class CoreNetworkBase(NodeBase):
unidirectional = 1
iface2_data = InterfaceData(
id=linked_node.get_iface_id(iface), name=iface.name, mac=iface.mac
id=linked_node.get_iface_id(iface), name=iface.name, mac=str(iface.mac)
)
ip4 = iface.get_ip4()
if ip4:

View file

@ -56,7 +56,7 @@ class CoreInterface:
self._params: Dict[str, float] = {}
self.ip4s: List[netaddr.IPNetwork] = []
self.ip6s: List[netaddr.IPNetwork] = []
self.mac: Optional[str] = None
self.mac: Optional[netaddr.EUI] = None
# placeholder position hook
self.poshook: Callable[[CoreInterface], None] = lambda x: None
# used with EMANE
@ -149,8 +149,8 @@ class CoreInterface:
self.ip4s.append(ip)
else:
self.ip6s.append(ip)
except netaddr.AddrFormatError:
raise CoreError(f"adding invalid address {ip}")
except netaddr.AddrFormatError as e:
raise CoreError(f"adding invalid address {ip}: {e}")
def remove_ip(self, ip: str) -> None:
"""
@ -167,8 +167,8 @@ class CoreInterface:
self.ip4s.remove(ip)
else:
self.ip6s.remove(ip)
except (netaddr.AddrFormatError, ValueError):
raise CoreError(f"deleting invalid address {ip}")
except (netaddr.AddrFormatError, ValueError) as e:
raise CoreError(f"deleting invalid address {ip}: {e}")
def get_ip4(self) -> Optional[netaddr.IPNetwork]:
"""
@ -194,16 +194,21 @@ class CoreInterface:
"""
return self.ip4s + self.ip6s
def set_mac(self, mac: str) -> None:
def set_mac(self, mac: Optional[str]) -> None:
"""
Set mac address.
:param mac: mac address to set
:param mac: mac address to set, None for random mac
:return: nothing
:raises CoreError: when there is an invalid mac address
"""
if mac is not None:
mac = utils.validate_mac(mac)
self.mac = mac
if mac is None:
self.mac = mac
else:
try:
self.mac = netaddr.EUI(mac, dialect=netaddr.mac_unix_expanded)
except netaddr.AddrFormatError as e:
raise CoreError(f"invalid mac address({mac}): {e}")
def getparam(self, key: str) -> float:
"""

View file

@ -878,7 +878,7 @@ class PtpNet(CoreNetwork):
unidirectional = 1
iface1_data = InterfaceData(
id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=iface1.mac
id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=str(iface1.mac)
)
ip4 = iface1.get_ip4()
if ip4:
@ -890,7 +890,7 @@ class PtpNet(CoreNetwork):
iface1_data.ip6_mask = ip6.prefixlen
iface2_data = InterfaceData(
id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=iface2.mac
id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=str(iface2.mac)
)
ip4 = iface2.get_ip4()
if ip4:

View file

@ -7,7 +7,6 @@ import os
import threading
from typing import IO, TYPE_CHECKING, List, Optional, Tuple
from core import utils
from core.constants import MOUNT_BIN, UMOUNT_BIN
from core.emulator.data import InterfaceData, LinkOptions
from core.emulator.distributed import DistributedServer
@ -74,7 +73,6 @@ class PhysicalNode(CoreNodeBase):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
mac = utils.validate_mac(mac)
iface = self.get_iface(iface_id)
iface.set_mac(mac)
if self.up: