Fix network addresses and indexing

Multiple fixes to make network addresses consistent
This commit is contained in:
Théophile Bastian 2020-03-11 11:29:13 +01:00
parent 5b8f16163d
commit 82dcc54c71
2 changed files with 29 additions and 14 deletions

View File

@ -19,8 +19,8 @@ class Network(util.LibvirtObject):
self.name = settings.PREFIX + "_link_" + name
self.bridge_id = settings.NETWORK_ID * 0xFF + self.id
self.bridge_mac = util.MACAddress(self.id, None)
self.ipv4 = util.Addrv4(self.id, None)
self.ipv6 = util.Addrv6(self.id, None)
self.ipv4 = util.Addrv4(self.id, None, host_address=True)
self.ipv6 = util.Addrv6(self.id, None, host_address=True)
self.lxc_network = None
def create(self):

View File

@ -7,7 +7,7 @@ import uuid
class NumberedClass:
""" A class that counts its current instance number """
next_id = 0
next_id = 1 # Count from 1: IP addresses and id 0 are not a good mix.
@classmethod
def get_id(cls):
@ -49,12 +49,12 @@ class LibvirtObject(NumberedClass):
class MACAddress:
""" A MAC address for a NIC or bridge """
def __init__(self, link_id, dev_id):
def __init__(self, link_id, dev_id=None):
""" 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
if dev_id is None:
dev_id = 0xFE
self.link_id = link_id
self.dev_id = dev_id
@ -71,12 +71,20 @@ class MACAddress:
class Addrv4:
""" An address in IPv4 """
def __init__(self, link_id, dev_id):
def __init__(self, link_id, dev_id=None, host_address=False):
""" link_id: id of the current link
dev_id: id of the current NIC. Use None to get the base address
host_address: if True and dev_id is None, returns the host machine address
on this network instead of the base address
"""
if not dev_id:
dev_id = 0
self.host_address = host_address
if dev_id is None:
if host_address:
dev_id = 0xFE
else:
dev_id = 0
self.link_id = link_id
self.dev_id = dev_id
@ -85,7 +93,7 @@ class Addrv4:
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
base_range=settings.IPV4_RANGE, link_id=self.link_id, dev_id=self.dev_id,
)
def __repr__(self):
@ -95,12 +103,19 @@ class Addrv4:
class Addrv6:
""" An address in IPv6 """
def __init__(self, link_id, dev_id):
def __init__(self, link_id, dev_id=None, host_address=False):
""" link_id: id of the current link
dev_id: id of the current NIC. Use None to get the base address
host_address: if True and dev_id is None, returns the host machine address
on this network instead of the base address
"""
if not dev_id:
dev_id = 0
self.host_address = host_address
if dev_id is None:
if host_address:
dev_id = 0xFE
else:
dev_id = 0
self.link_id = link_id
self.dev_id = dev_id
@ -108,5 +123,5 @@ class Addrv6:
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
base_range=settings.IPV6_RANGE, link_id=self.link_id, dev_id=self.dev_id,
)