# The software in this package is distributed under the GNU General # Public License version 2 (with a special exception described below). # # A copy of GNU General Public License (GPL) is included in this distribution, # in the file COPYING.GPL. # # As a special exception, if other files instantiate templates or use macros # or inline functions from this file, or you compile this file and link it # with other works to produce a work based on this file, this file # does not by itself cause the resulting work to be covered # by the GNU General Public License. # # However the source code for this file must still be made available # in accordance with section (3) of the GNU General Public License. # # This exception does not invalidate any other reasons why a work based # on this file might be covered by the GNU General Public License. # # Copyright (c) 2016-2018 Intra2net AG """ V4_addr_range class. Copyright: Intra2net AG """ default_addr_lo = 42 default_addr_range = 42 uint32_max = 0xffffffff class V4_addr_range: def __init__(self, lo=None, addr_range=None): self.val_lo = default_addr_lo # : int, initial offset for allocation self.val_range = default_addr_range # : int, allocation block size self.val_hi = 0 # : int, lo + range self.var_alloc = {} # : mutable int, bool allocated values if lo is not None: if isinstance(lo, int) is False: raise TypeError("Expected value of integer type, got \"%s\" : %s." % (lo, type(lo))) self.val_lo = lo if addr_range is not None: if isinstance(addr_range, int) is False: raise TypeError("Expected value of integer type, got \"%s\" : %s." % (lo, type(lo))) self.val_range = addr_range self.fix_range() for val in range(self.val_lo, self.val_hi): # we use ints as keys since the types are checked elsewhere self.var_alloc[val] = False def __len__(self): length = 0 for v in self.var_alloc.values(): length += 1 if v is True else 0 return length def __eq__(self, other): if isinstance(other, int): return other == len(self) if isinstance(other, self.__class__): return len(self) == len(other) raise TypeError("Equality comparison of %s with type %s is undefined." % (self.__class__.__name__, type(other))) def __getitem__(self, k): try: return self.var_alloc[k] except KeyError: return False def fix_range(self): if self.val_range <= 0: raise TypeError("IP address ranges need to be natural numbers > 0.") hi = self.val_lo + self.val_range if hi <= self.val_lo or hi > uint32_max: raise ValueError("Invalid IP address range: %d+%d." % (self.val_lo, self.val_range)) self.val_hi = hi def lo(self): return self.val_lo def range(self): return self.val_range def hi(self): return self.val_hi def get(self): """ .get -- Return lowest unallocated number in range and insert as a Python integer. """ curlen = len(self) if curlen == self.val_range: raise IndexError("Address range (%d) exhausted." % self.val_range) for val in range(self.val_lo, self.val_hi): if self.var_alloc[val] is False: self.var_alloc[val] = True return val def rm(self, val): """ Remove allocated number from range. """ if isinstance(val, int) is False: raise TypeError("Expected int or short, got %s." % type(val)) val = val vali = val if val < self.val_lo or self.val_hi < val: raise IndexError("Address %d out of bounds ([%d, %d])." % (vali, self.val_lo, self.val_hi)) if self.var_alloc[vali] is False: raise ValueError("Address %d was never allocated." % vali) self.var_alloc[vali] = True def __str__(self): return "v4 address range: [%d, %d] = %d+%d, at %d (%.2f%%)" \ % (self.val_lo, self.val_hi, self.val_lo, self.val_range, len(self.var_alloc), (100.0 * (float(len(self.var_alloc)) / float(self.val_range)))) def __repr__(self): return "(%d, %d, %s)" \ % (self.val_lo, self.val_range, self.var_alloc.__repr__())