X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/e687ec0110d984ef53afc2bfc2a69c2708b98ceb..ddc1de7c566450adda92e30a814e792d5beb22a7:/lib/netutils.py diff --git a/lib/netutils.py b/lib/netutils.py index 7a1b6d0..ac87d4d 100644 --- a/lib/netutils.py +++ b/lib/netutils.py @@ -362,6 +362,20 @@ class IPAddress(object): return False @classmethod + def ValidateNetmask(cls, netmask): + """Validate a netmask suffix in CIDR notation. + + @type netmask: int + @param netmask: netmask suffix to validate + @rtype: bool + @return: True if valid, False otherwise + + """ + assert (isinstance(netmask, (int, long))) + + return 0 < netmask <= cls.iplen + + @classmethod def Own(cls, address): """Check if the current host has the the given IP address. @@ -487,6 +501,36 @@ class IPAddress(object): raise errors.ProgrammerError("%s is not a valid IP version" % version) + @staticmethod + def GetClassFromIpVersion(version): + """Return the IPAddress subclass for the given IP version. + + @type version: int + @param version: IP version, one of L{constants.IP4_VERSION} or + L{constants.IP6_VERSION} + @return: a subclass of L{netutils.IPAddress} + @raise errors.ProgrammerError: for unknowo IP versions + + """ + if version == constants.IP4_VERSION: + return IP4Address + elif version == constants.IP6_VERSION: + return IP6Address + + raise errors.ProgrammerError("%s is not a valid IP version" % version) + + @staticmethod + def GetClassFromIpFamily(family): + """Return the IPAddress subclass for the given IP family. + + @param family: IP family (one of C{socket.AF_INET} or C{socket.AF_INET6} + @return: a subclass of L{netutils.IPAddress} + @raise errors.ProgrammerError: for unknowo IP versions + + """ + return IPAddress.GetClassFromIpVersion( + IPAddress.GetVersionFromAddressFamily(family)) + @classmethod def IsLoopback(cls, address): """Determine whether it is a loopback address. @@ -583,7 +627,7 @@ class IP6Address(IPAddress): twoparts = address.split("::") sep = len(twoparts[0].split(":")) + len(twoparts[1].split(":")) parts = twoparts[0].split(":") - [parts.append("0") for _ in range(8 - sep)] + parts.extend(["0"] * (8 - sep)) parts += twoparts[1].split(":") else: parts = address.split(":")