#
#
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 2011, 2012 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
if source is not None:
try:
sock.bind((source, 0))
- except socket.error, (errcode, _):
- if errcode == errno.EADDRNOTAVAIL:
+ except socket.error, err:
+ if err[0] == errno.EADDRNOTAVAIL:
success = False
sock.settimeout(timeout)
success = True
except socket.timeout:
success = False
- except socket.error, (errcode, _):
- success = (not live_port_needed) and (errcode == errno.ECONNREFUSED)
+ except socket.error, err:
+ success = (not live_port_needed) and (err[0] == errno.ECONNREFUSED)
return success
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.
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.
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(":")