2020-03-09 12:14:26 +01:00
|
|
|
""" Various utils """
|
|
|
|
|
2020-03-09 12:58:57 +01:00
|
|
|
from . import settings
|
2020-03-09 12:14:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NumberedClass:
|
|
|
|
""" A class that counts its current instance number """
|
|
|
|
|
|
|
|
next_id = 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_id(cls):
|
|
|
|
out = cls.next_id
|
|
|
|
cls.next_id += 1
|
|
|
|
return out
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.id = self.get_id()
|
|
|
|
|
|
|
|
|
|
|
|
class MACAddress:
|
|
|
|
""" A MAC address for a NIC or bridge """
|
|
|
|
|
|
|
|
def __init__(self, link_id, dev_id):
|
|
|
|
""" link_id: id of the current link
|
|
|
|
dev_id: id of the current NIC. If the device is a bridge, use None
|
|
|
|
"""
|
|
|
|
if not dev_id:
|
|
|
|
dev_id = 0xFF
|
|
|
|
|
|
|
|
self.link_id = link_id
|
|
|
|
self.dev_id = dev_id
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "52:54:00:{nwid:02x}:{link_id:02x}:{dev_id:02x}".format(
|
|
|
|
nwid=settings.NETWORK_ID, link_id=self.link_id, dev_id=self.dev_id
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Addrv4:
|
|
|
|
""" An address in IPv4 """
|
|
|
|
|
|
|
|
def __init__(self, link_id, dev_id):
|
|
|
|
""" link_id: id of the current link
|
|
|
|
dev_id: id of the current NIC. Use None to get the base address
|
|
|
|
"""
|
|
|
|
if not dev_id:
|
|
|
|
dev_id = 0
|
|
|
|
|
|
|
|
self.link_id = link_id
|
|
|
|
self.dev_id = dev_id
|
|
|
|
self.netmask = "255.255.255.0"
|
|
|
|
self.prefix = 24
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{base_range}.{link_id}.{dev_id}".format(
|
|
|
|
base_range=settings.IPV4_RANGE, link_id=self.link_id, dev_id=self.dev_id
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Addrv6:
|
|
|
|
""" An address in IPv6 """
|
|
|
|
|
|
|
|
def __init__(self, link_id, dev_id):
|
|
|
|
""" link_id: id of the current link
|
|
|
|
dev_id: id of the current NIC. Use None to get the base address
|
|
|
|
"""
|
|
|
|
if not dev_id:
|
|
|
|
dev_id = 0
|
|
|
|
|
|
|
|
self.link_id = link_id
|
|
|
|
self.dev_id = dev_id
|
|
|
|
self.prefix = 64
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{base_range}:{link_id:04x}::{dev_id:04x}".format(
|
|
|
|
base_range=settings.IPV6_RANGE, link_id=self.link_id, dev_id=self.dev_id
|
|
|
|
)
|