mem_count is now mem_size everywhere
[ganeti-local] / lib / netutils.py
index 11d5150..ac87d4d 100644 (file)
@@ -58,7 +58,7 @@ _IP_FAMILY_RE = re.compile(r"(?P<family>inet6?)\s+(?P<ip>%s)/" % _IP_RE_TEXT,
 
 # Dict used to convert from a string representing an IP family to an IP
 # version
-_NAME_TO_IP_VER =  {
+_NAME_TO_IP_VER = {
   "inet": constants.IP4_VERSION,
   "inet6": constants.IP6_VERSION,
   }
@@ -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.
 
@@ -417,9 +431,9 @@ class IPAddress(object):
     assert 0 <= prefix <= cls.iplen
     target_int = cls._GetIPIntFromString(subnet[0])
     # Convert prefix netmask to integer value of netmask
-    netmask_int = (2**cls.iplen)-1 ^ ((2**cls.iplen)-1 >> prefix)
+    netmask_int = (2 ** cls.iplen) - 1 ^ ((2 ** cls.iplen) - 1 >> prefix)
     # Calculate hostmask
-    hostmask_int = netmask_int ^ (2**cls.iplen)-1
+    hostmask_int = netmask_int ^ (2 ** cls.iplen) - 1
     # Calculate network address by and'ing netmask
     network_int = target_int & netmask_int
     # Calculate broadcast address by or'ing hostmask
@@ -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(":")