Commits

buge committed d463d24

Restructured project.

Comments (0)

Files changed (8)

 syntax: glob
 
+oui.py
 *.pyc
 
 # Code coverage output

addrinfo.py

-# Copyright (C) 2011  Philipp Bunge
-#
-# 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
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-
-import operator
-
-try:
-    from oui import OUIS
-except ImportError:
-    OUIS = {}
-
-
-__all__ = [
-    'Address',
-    'EUIAddress', 'EUI48Address', 'EUI64Address',
-    'IPAddress', 'IPv4Address', 'IPv6Address'
-]
-
-
-class Address(object):
-
-    @classmethod
-    def value_of(cls, value):
-        return None
-#       tokens = []
-#       ipv6 = False
-
-#       while i < len(value):
-#           if value[i:i+2] == "::":
-#               tokens += [("GLUE", 0), ("", 16)]
-#               ipv6 = True
-#               i += 2
-#           elif value[i] == ":":
-#               tokens[-1] = (tokens[-1][0], 16)
-#               tokens += [("", 16)]
-#               ipv6 = True
-#               i += 1
-#           elif value[i] == ".":
-#               tokens[-1] = (tokens[-1][0], 0)
-#               tokens += [("", 0)]
-#               i += 1
-#           else:
-#               tokens[-1] = (tokens[-1][0] + value[i], tokens[-1][1])
-#               i += 1
-
-#       address = 0
-#       for token in tokens:
-#           if token[1] == 16:
-#               address <<= 16
-#           else:
-#               address <<= 8
-#           if token[0]:
-#               address |= int(*token)
-
-#       if ipv6 or (address >> 128) != 0:
-#           return IPv6Address(address)
-#       return IPv4Address(address)
-
-
-    def __init__(self, address):
-        a = int(address)
-        bitsize = self.__class__.BIT_SIZE
-        if a < 0 or (a >> bitsize) != 0:
-            raise ValueError("address must be between 0 and 2**%d-1" % bitsize)
-        self.__address = a
-
-    def get_address(self):
-        return self.__address
-
-    def __has_prefix(self, prefix, length):
-        bitsize = self.__class__.BIT_SIZE
-        return prefix == self.get_address() >> (bitsize - length)
-
-    def __repr__(self):
-        return "%s(%r)" % (self.__class__.__name__, self.get_address())
-
-    def __str__(self):
-        raise NotImplementedError(
-            "subclasses must override __str__ to return a canonical " +
-            "representation of themselves.")
-
-    def __lshift__(self, other):
-        return self.__address_op(operator.lshift, other)
-
-    def __rshift__(self, other):
-        return self.__address_op(operator.rshift, other)
-
-    def __and__(self, other):
-        return self.__address_op(operator.and_, other)
-
-    def __xor__(self, other):
-        return self.__address_op(operator.xor, other)
-
-    def __or__(self, other):
-        return self.__address_op(operator.or_, other)
-
-    def __rand__(self, other):
-        return other & self.get_address()
-
-    def __rxor__(self, other):
-        return other ^ self.get_address()
-
-    def __ror__(self, other):
-        return other | self.get_address()
-
-    def __invert__(self):
-        c = 2 ** self.__class__.BIT_SIZE - 1
-        return self.__address_op(operator.xor, c)
-
-    def __address_op(self, f, other):
-        address = other
-        if isinstance(address, Address):
-            address = address.get_address()
-
-        max_address = 2 ** self.__class__.BIT_SIZE - 1
-        new_address = f(self.get_address(), address) & max_address
-
-        return self.__class__(new_address)
-
-    def __complex__(self):
-        return complex(self.get_address())
-
-    def __int__(self):
-        return int(self.get_address())
-
-    def __long__(self):
-        return long(self.get_address())
-
-    def __float__(self):
-        return long(self.get_address())
-
-    def __oct__(self):
-        # Deprecated with Python 3.0, use __index__ instead
-        return oct(self.get_address())
-
-    def __hex__(self):
-        # Deprecated with Python 3.0, use __index__ instead
-        return hex(self.get_address())
-
-    def __index__(self):
-        return int(self.get_address())
-
-
-class AddressGroup(object):
-
-    pass
-
-
-class _AddressFormatter(object):
-
-    def __init__(self, address_size, group_size, group_format, separator):
-        self.__address_size = address_size
-        self.__group_size = group_size
-        self.__group_format = group_format
-        self.__separator = separator
-
-    def get_address_size(self):
-        return self.__address_size
-
-    def get_group_size(self):
-        return self.__group_size
-
-    def get_group_format(self):
-        return self.__group_format
-
-    def get_separator(self):
-        return self.__separator
-
-    def format(self, address):
-        a = address
-        if isinstance(a, Address):
-            a = a.get_address()
-
-        address_size = self.get_address_size()
-        group_size = self.get_group_size()
-        group_format = self.get_group_format()
-        mask = 2 ** group_size - 1
-
-        fields = []
-        for i in xrange(0, address_size / group_size):
-            fields.insert(0, group_format % (a & mask))
-            a >>= group_size
-        return self.get_separator().join(fields)
-
-
-class EUIAddress(Address):
-
-    @classmethod
-    def get_hardware_address(cls):
-        from uuid import getnode
-        return EUI48Address(getnode())
-
-    def get_oui_number(self):
-        return self.get_address() >> (self.__class__.BIT_SIZE - 24)
-
-    def get_oui_name(self):
-        return OUIS.get(self.get_oui_number())
-
-    def as_eui48(self):
-        return NotImplementedError("abstract method")
-
-    def as_eui64(self):
-        return NotImplementedError("abstract method")
-
-    def get_ipv6_link_local_address(self):
-        return IPv6Address.get_link_local_address(self)
-
-
-class EUI48Address(EUIAddress):
-
-    BIT_SIZE = 48
-
-    def as_eui48(self):
-        return self
-
-    def as_eui64(self):
-        # Conversion from MAC-48 is deprecated
-        # (see IEEE "Guidelines for use of a 64-bit Extended Unique Identifier")
-        return self.as_eui64_from_eui48()
- 
-    def as_eui64_from_eui48(self):
-        a = self.get_address()
-        eui64_a = (a & 0xffffff << 24) << 16 | 0xfffe << 24 | (a & 0xffffff)
-        return EUI64Address(eui64_a)
-
-    def as_eui64_from_mac48(self):
-        a = self.get_address()
-        eui64_a = (a & 0xffffff << 24) << 16 | 0xffff << 24 | (a & 0xffffff)
-        return EUI64Address(eui64_a)
-
-    def __str__(self):
-        return _AddressFormatter(
-            address_size = EUI48Address.BIT_SIZE,
-            group_size = 8,
-            group_format = "%02X",
-            separator = '-'
-        ).format(self)
-
-    def __eq__(self, other):
-        if not isinstance(other, EUI48Address):
-            return False
-        return self.get_address() == other.get_address()
-
-    def __hash__(self):
-        return hash(self.get_address())
-
-
-class EUI64Address(EUIAddress):
-
-    BIT_SIZE = 64
-
-    def as_eui48(self):
-        a = self.get_address()
-        if a >> 24 & 0xfffe != 0xfffe:
-            raise ValueError(
-                "%s is not an EUI-48 encapsulated EUI-64 value" % self)
-        eui48_a = a >> 16 & 0xffffff << 24 | a & 0xffffff
-        return EUI48Address(eui48_a)
-
-    def as_eui64(self):
-        return self
- 
-    def __str__(self):
-        return _AddressFormatter(
-            address_size = EUI64Address.BIT_SIZE,
-            group_size = 8,
-            group_format = "%02X",
-            separator = '-'
-        ).format(self)
-
-    def __eq__(self, other):
-        if not isinstance(other, EUI64Address):
-            return False
-        return self.get_address() == other.get_address()
-
-    def __hash__(self):
-        return hash(self.get_address())
-
-
-class IPAddress(Address):
-
-    pass
-
-
-class IPv4Address(IPAddress):
-
-    BIT_SIZE = 32
-
-    def __str__(self):
-        return _AddressFormatter(
-            address_size = IPv4Address.BIT_SIZE,
-            group_size = 8,
-            group_format = "%d",
-            separator = '.'
-        ).format(self)
-
-    def __eq__(self, other):
-        if not isinstance(other, IPv4Address):
-            return False
-        return self.get_address() == other.get_address()
-
-    def __hash__(self):
-        return hash(self.get_address())
-
-IPv4Address.UNSPECIFIED_ADDRESS = IPv4Address(0)
-IPv4Address.LOCALHOST_ADDRESS = IPv4Address(2130706433)
-
-
-class IPv6Address(IPAddress):
-
-    BIT_SIZE = 128
-
-    # Descriptions as by RFC 4291
-    TYPE_UNSPECIFIED = 'Unspecified'
-    TYPE_LOOPBACK = 'Loopback'
-    TYPE_MULTICAST = 'Multicast'
-    TYPE_LINK_LOCAL_UNICAST = 'Link-Local unicast'
-    TYPE_GLOBAL_UNICAST = 'Global Unicast'
-
-    @classmethod
-    def get_link_local_address(cls, interface_address=None):
-        eui = interface_address
-        if eui is None:
-            eui = EUIAddress.get_hardware_address()
-        elif not isinstance(eui, EUIAddress):
-            eui = EUI64Address(interface_address)
-        eui = eui.as_eui64()
-        return IPv6Address(0xfe80 << 112 | eui ^ 1 << 57)
-
-    def get_address_type(self):
-        """Returns the address type according to :rfc:`4291`.
-
-        The returned string corresponds to the `TYPE_*` constants defined in
-        this class.
-
-        """
-        if self.get_address() == 0:
-            return IPv6Address.TYPE_UNSPECIFIED
-        if self.get_address() == 1:
-            return IPv6Address.TYPE_LOOPBACK
-        if self.__has_prefix(255, 8):
-            return IPv6Address.TYPE_MULTICAST
-        if self.__has_prefix(1018, 10):
-            return IPv6Address.TYPE_LINK_LOCAL_UNICAST
-        return IPv6Address.TYPE_GLOBAL_UNICAST
-
-    def is_type_unspecified(self):
-        """Returns whether the address is of type "Unspecified",
-        as defined by :rfc:`4291`.
-
-        """
-        return self.get_address_type() == IPv6Address.TYPE_UNSPECIFIED
-
-    def is_type_loopback(self):
-        """Returns whether the address is of type "Loopback",
-        as defined by :rfc:`4291`.
-
-        """
-        return self.get_address_type() == IPv6Address.TYPE_LOOPBACK
-
-    def is_type_multicast(self):
-        """Returns whether the address is of type "Multicast",
-        as defined by :rfc:`4291`.
-
-        """
-        return self.get_address_type() == IPv6Address.TYPE_MULTICAST
-
-    def is_type_linklocal_unicast(self):
-        """Returns whether the address is of type "Link-Local Unicast",
-        as defined by :rfc:`4291`.
-
-        """
-        return self.get_address_type() == IPv6Address.TYPE_LINK_LOCAL_UNICAST
-
-    def is_type_global_unicast(self):
-        """Returns whether the address is of type "Global Unicast",
-        as defined by :rfc:`4291`.
-
-        """
-        return self.get_address_type() == IPv6Address.TYPE_GLOBAL_UNICAST
-
-    def __str__(self):
-        # FIXME implement canonical form correctly
-        return _AddressFormatter(
-            address_size = IPv6Address.BIT_SIZE,
-            group_size = 16,
-            group_format = "%x",
-            separator = ':'
-        ).format(self)
-
-    def __eq__(self, other):
-        if not isinstance(other, IPv6Address):
-            return False
-        return self.get_address() == other.get_address()
-
-    def __hash__(self):
-        return hash(self.get_address())
-
-IPv6Address.UNSPECIFIED_ADDRESS = IPv6Address(0)
-IPv6Address.LOOPBACK_ADDRESS = IPv6Address(1)
-
-
-class IPNetwork(AddressGroup):
-
-    def __init__(self, address, mask):
-        if not isinstance(address, IPAddress):
-            raise TypeError("FIXME") # FIXME
-        bitsize = address.__class__.BIT_SIZE
-        m = int(mask)
-        if m > bitsize:
-            raise ValueError("FIXME") # FIXME
-        a = address & (2 ** m - 1) << (bitsize - m)
-        self.__address = a
-        self.__mask = m
-
-    def get_address(self):
-        return self.__address
-
-    def get_mask(self):
-        return self.__mask
-
-

addrinfo/addresses.py

+# Copyright (C) 2011  Philipp Bunge
+#
+# 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import operator
+
+try:
+    from oui import OUIS
+except ImportError:
+    OUIS = {}
+
+
+__all__ = [
+    'Address',
+    'EUIAddress', 'EUI48Address', 'EUI64Address',
+    'IPAddress', 'IPv4Address', 'IPv6Address'
+]
+
+
+class Address(object):
+
+    @classmethod
+    def value_of(cls, value):
+        return None
+#       tokens = []
+#       ipv6 = False
+
+#       while i < len(value):
+#           if value[i:i+2] == "::":
+#               tokens += [("GLUE", 0), ("", 16)]
+#               ipv6 = True
+#               i += 2
+#           elif value[i] == ":":
+#               tokens[-1] = (tokens[-1][0], 16)
+#               tokens += [("", 16)]
+#               ipv6 = True
+#               i += 1
+#           elif value[i] == ".":
+#               tokens[-1] = (tokens[-1][0], 0)
+#               tokens += [("", 0)]
+#               i += 1
+#           else:
+#               tokens[-1] = (tokens[-1][0] + value[i], tokens[-1][1])
+#               i += 1
+
+#       address = 0
+#       for token in tokens:
+#           if token[1] == 16:
+#               address <<= 16
+#           else:
+#               address <<= 8
+#           if token[0]:
+#               address |= int(*token)
+
+#       if ipv6 or (address >> 128) != 0:
+#           return IPv6Address(address)
+#       return IPv4Address(address)
+
+
+    def __init__(self, address):
+        a = int(address)
+        bitsize = self.__class__.BIT_SIZE
+        if a < 0 or (a >> bitsize) != 0:
+            raise ValueError("address must be between 0 and 2**%d-1" % bitsize)
+        self.__address = a
+
+    def get_address(self):
+        return self.__address
+
+    def __has_prefix(self, prefix, length):
+        bitsize = self.__class__.BIT_SIZE
+        return prefix == self.get_address() >> (bitsize - length)
+
+    def __repr__(self):
+        return "%s(%r)" % (self.__class__.__name__, self.get_address())
+
+    def __str__(self):
+        raise NotImplementedError(
+            "subclasses must override __str__ to return a canonical " +
+            "representation of themselves.")
+
+    def __lshift__(self, other):
+        return self.__address_op(operator.lshift, other)
+
+    def __rshift__(self, other):
+        return self.__address_op(operator.rshift, other)
+
+    def __and__(self, other):
+        return self.__address_op(operator.and_, other)
+
+    def __xor__(self, other):
+        return self.__address_op(operator.xor, other)
+
+    def __or__(self, other):
+        return self.__address_op(operator.or_, other)
+
+    def __rand__(self, other):
+        return other & self.get_address()
+
+    def __rxor__(self, other):
+        return other ^ self.get_address()
+
+    def __ror__(self, other):
+        return other | self.get_address()
+
+    def __invert__(self):
+        c = 2 ** self.__class__.BIT_SIZE - 1
+        return self.__address_op(operator.xor, c)
+
+    def __address_op(self, f, other):
+        address = other
+        if isinstance(address, Address):
+            address = address.get_address()
+
+        max_address = 2 ** self.__class__.BIT_SIZE - 1
+        new_address = f(self.get_address(), address) & max_address
+
+        return self.__class__(new_address)
+
+    def __complex__(self):
+        return complex(self.get_address())
+
+    def __int__(self):
+        return int(self.get_address())
+
+    def __long__(self):
+        return long(self.get_address())
+
+    def __float__(self):
+        return long(self.get_address())
+
+    def __oct__(self):
+        # Deprecated with Python 3.0, use __index__ instead
+        return oct(self.get_address())
+
+    def __hex__(self):
+        # Deprecated with Python 3.0, use __index__ instead
+        return hex(self.get_address())
+
+    def __index__(self):
+        return int(self.get_address())
+
+
+class AddressGroup(object):
+
+    pass
+
+
+class _AddressFormatter(object):
+
+    def __init__(self, address_size, group_size, group_format, separator):
+        self.__address_size = address_size
+        self.__group_size = group_size
+        self.__group_format = group_format
+        self.__separator = separator
+
+    def get_address_size(self):
+        return self.__address_size
+
+    def get_group_size(self):
+        return self.__group_size
+
+    def get_group_format(self):
+        return self.__group_format
+
+    def get_separator(self):
+        return self.__separator
+
+    def format(self, address):
+        a = address
+        if isinstance(a, Address):
+            a = a.get_address()
+
+        address_size = self.get_address_size()
+        group_size = self.get_group_size()
+        group_format = self.get_group_format()
+        mask = 2 ** group_size - 1
+
+        fields = []
+        for i in xrange(0, address_size / group_size):
+            fields.insert(0, group_format % (a & mask))
+            a >>= group_size
+        return self.get_separator().join(fields)
+
+
+class EUIAddress(Address):
+
+    @classmethod
+    def get_hardware_address(cls):
+        from uuid import getnode
+        return EUI48Address(getnode())
+
+    def get_oui_number(self):
+        return self.get_address() >> (self.__class__.BIT_SIZE - 24)
+
+    def get_oui_name(self):
+        return OUIS.get(self.get_oui_number())
+
+    def as_eui48(self):
+        return NotImplementedError("abstract method")
+
+    def as_eui64(self):
+        return NotImplementedError("abstract method")
+
+    def get_ipv6_link_local_address(self):
+        return IPv6Address.get_link_local_address(self)
+
+
+class EUI48Address(EUIAddress):
+
+    BIT_SIZE = 48
+
+    def as_eui48(self):
+        return self
+
+    def as_eui64(self):
+        # Conversion from MAC-48 is deprecated
+        # (see IEEE "Guidelines for use of a 64-bit Extended Unique Identifier")
+        return self.as_eui64_from_eui48()
+ 
+    def as_eui64_from_eui48(self):
+        a = self.get_address()
+        eui64_a = (a & 0xffffff << 24) << 16 | 0xfffe << 24 | (a & 0xffffff)
+        return EUI64Address(eui64_a)
+
+    def as_eui64_from_mac48(self):
+        a = self.get_address()
+        eui64_a = (a & 0xffffff << 24) << 16 | 0xffff << 24 | (a & 0xffffff)
+        return EUI64Address(eui64_a)
+
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = EUI48Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%02X",
+            separator = '-'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, EUI48Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+
+class EUI64Address(EUIAddress):
+
+    BIT_SIZE = 64
+
+    def as_eui48(self):
+        a = self.get_address()
+        if a >> 24 & 0xfffe != 0xfffe:
+            raise ValueError(
+                "%s is not an EUI-48 encapsulated EUI-64 value" % self)
+        eui48_a = a >> 16 & 0xffffff << 24 | a & 0xffffff
+        return EUI48Address(eui48_a)
+
+    def as_eui64(self):
+        return self
+ 
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = EUI64Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%02X",
+            separator = '-'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, EUI64Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+
+class IPAddress(Address):
+
+    pass
+
+
+class IPv4Address(IPAddress):
+
+    BIT_SIZE = 32
+
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = IPv4Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%d",
+            separator = '.'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, IPv4Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+IPv4Address.UNSPECIFIED_ADDRESS = IPv4Address(0)
+IPv4Address.LOCALHOST_ADDRESS = IPv4Address(2130706433)
+
+
+class IPv6Address(IPAddress):
+
+    BIT_SIZE = 128
+
+    # Descriptions as by RFC 4291
+    TYPE_UNSPECIFIED = 'Unspecified'
+    TYPE_LOOPBACK = 'Loopback'
+    TYPE_MULTICAST = 'Multicast'
+    TYPE_LINK_LOCAL_UNICAST = 'Link-Local unicast'
+    TYPE_GLOBAL_UNICAST = 'Global Unicast'
+
+    @classmethod
+    def get_link_local_address(cls, interface_address=None):
+        eui = interface_address
+        if eui is None:
+            eui = EUIAddress.get_hardware_address()
+        elif not isinstance(eui, EUIAddress):
+            eui = EUI64Address(interface_address)
+        eui = eui.as_eui64()
+        return IPv6Address(0xfe80 << 112 | eui ^ 1 << 57)
+
+    def get_address_type(self):
+        """Returns the address type according to :rfc:`4291`.
+
+        The returned string corresponds to the `TYPE_*` constants defined in
+        this class.
+
+        """
+        if self.get_address() == 0:
+            return IPv6Address.TYPE_UNSPECIFIED
+        if self.get_address() == 1:
+            return IPv6Address.TYPE_LOOPBACK
+        if self.__has_prefix(255, 8):
+            return IPv6Address.TYPE_MULTICAST
+        if self.__has_prefix(1018, 10):
+            return IPv6Address.TYPE_LINK_LOCAL_UNICAST
+        return IPv6Address.TYPE_GLOBAL_UNICAST
+
+    def is_type_unspecified(self):
+        """Returns whether the address is of type "Unspecified",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_UNSPECIFIED
+
+    def is_type_loopback(self):
+        """Returns whether the address is of type "Loopback",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_LOOPBACK
+
+    def is_type_multicast(self):
+        """Returns whether the address is of type "Multicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_MULTICAST
+
+    def is_type_linklocal_unicast(self):
+        """Returns whether the address is of type "Link-Local Unicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_LINK_LOCAL_UNICAST
+
+    def is_type_global_unicast(self):
+        """Returns whether the address is of type "Global Unicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_GLOBAL_UNICAST
+
+    def __str__(self):
+        # FIXME implement canonical form correctly
+        return _AddressFormatter(
+            address_size = IPv6Address.BIT_SIZE,
+            group_size = 16,
+            group_format = "%x",
+            separator = ':'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, IPv6Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+IPv6Address.UNSPECIFIED_ADDRESS = IPv6Address(0)
+IPv6Address.LOOPBACK_ADDRESS = IPv6Address(1)
+
+
+class IPNetwork(AddressGroup):
+
+    def __init__(self, address, mask):
+        if not isinstance(address, IPAddress):
+            raise TypeError("FIXME") # FIXME
+        bitsize = address.__class__.BIT_SIZE
+        m = int(mask)
+        if m > bitsize:
+            raise ValueError("FIXME") # FIXME
+        a = address & (2 ** m - 1) << (bitsize - m)
+        self.__address = a
+        self.__mask = m
+
+    def get_address(self):
+        return self.__address
+
+    def get_mask(self):
+        return self.__mask
+
+
+# Copyright (C) 2011  Philipp Bunge
+#
+# 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS    =
+SPHINXBUILD   = sphinx-build
+PAPER         =
+BUILDDIR      = _build
+
+# Internal variables.
+PAPEROPT_a4     = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS   = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+
+.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest
+
+help:
+	@echo "Please use \`make <target>' where <target> is one of"
+	@echo "  html       to make standalone HTML files"
+	@echo "  dirhtml    to make HTML files named index.html in directories"
+	@echo "  singlehtml to make a single large HTML file"
+	@echo "  pickle     to make pickle files"
+	@echo "  json       to make JSON files"
+	@echo "  htmlhelp   to make HTML files and a HTML help project"
+	@echo "  qthelp     to make HTML files and a qthelp project"
+	@echo "  devhelp    to make HTML files and a Devhelp project"
+	@echo "  epub       to make an epub"
+	@echo "  latex      to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+	@echo "  latexpdf   to make LaTeX files and run them through pdflatex"
+	@echo "  text       to make text files"
+	@echo "  man        to make manual pages"
+	@echo "  changes    to make an overview of all changed/added/deprecated items"
+	@echo "  linkcheck  to check all external links for integrity"
+	@echo "  doctest    to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+	-rm -rf $(BUILDDIR)/*
+
+html:
+	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
+	@echo
+	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
+
+dirhtml:
+	$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
+	@echo
+	@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
+
+singlehtml:
+	$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
+	@echo
+	@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
+
+pickle:
+	$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
+	@echo
+	@echo "Build finished; now you can process the pickle files."
+
+json:
+	$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
+	@echo
+	@echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+	$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
+	@echo
+	@echo "Build finished; now you can run HTML Help Workshop with the" \
+	      ".hhp project file in $(BUILDDIR)/htmlhelp."
+
+qthelp:
+	$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
+	@echo
+	@echo "Build finished; now you can run "qcollectiongenerator" with the" \
+	      ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
+	@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/addrinfo.qhcp"
+	@echo "To view the help file:"
+	@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/addrinfo.qhc"
+
+devhelp:
+	$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
+	@echo
+	@echo "Build finished."
+	@echo "To view the help file:"
+	@echo "# mkdir -p $$HOME/.local/share/devhelp/addrinfo"
+	@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/addrinfo"
+	@echo "# devhelp"
+
+epub:
+	$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
+	@echo
+	@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
+
+latex:
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+	@echo
+	@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
+	@echo "Run \`make' in that directory to run these through (pdf)latex" \
+	      "(use \`make latexpdf' here to do that automatically)."
+
+latexpdf:
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+	@echo "Running LaTeX files through pdflatex..."
+	make -C $(BUILDDIR)/latex all-pdf
+	@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+text:
+	$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
+	@echo
+	@echo "Build finished. The text files are in $(BUILDDIR)/text."
+
+man:
+	$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
+	@echo
+	@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
+
+changes:
+	$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
+	@echo
+	@echo "The overview file is in $(BUILDDIR)/changes."
+
+linkcheck:
+	$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
+	@echo
+	@echo "Link check complete; look for any errors in the above output " \
+	      "or in $(BUILDDIR)/linkcheck/output.txt."
+
+doctest:
+	$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
+	@echo "Testing of doctests in the sources finished, look at the " \
+	      "results in $(BUILDDIR)/doctest/output.txt."
+# -*- coding: utf-8 -*-
+#
+# addrinfo documentation build configuration file, created by
+# sphinx-quickstart on Wed Feb 16 15:17:12 2011.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#sys.path.insert(0, os.path.abspath('.'))
+
+# -- General configuration -----------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage']
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'addrinfo'
+copyright = u'2011, Philipp Bunge'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '0.1'
+# The full version, including alpha/beta/rc tags.
+release = '0.1'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+exclude_patterns = ['_build']
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages.  See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further.  For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents.  If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar.  Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['_static']
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it.  The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'addrinfodoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+# The paper size ('letter' or 'a4').
+#latex_paper_size = 'letter'
+
+# The font size ('10pt', '11pt' or '12pt').
+#latex_font_size = '10pt'
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+  ('index', 'addrinfo.tex', u'addrinfo Documentation',
+   u'Philipp Bunge', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Additional stuff for the LaTeX preamble.
+#latex_preamble = ''
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output --------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+    ('index', 'addrinfo', u'addrinfo Documentation',
+     [u'Philipp Bunge'], 1)
+]
+.. addrinfo documentation master file, created by
+   sphinx-quickstart on Wed Feb 16 15:17:12 2011.
+   You can adapt this file completely to your liking, but it should at least
+   contain the root `toctree` directive.
+
+Welcome to addrinfo's documentation!
+====================================
+
+Contents:
+
+.. toctree::
+   :maxdepth: 2
+
+Indices and tables
+==================
+
+* :ref:`genindex`
+* :ref:`modindex`
+* :ref:`search`
+
+@ECHO OFF
+
+REM Command file for Sphinx documentation
+
+if "%SPHINXBUILD%" == "" (
+	set SPHINXBUILD=sphinx-build
+)
+set BUILDDIR=_build
+set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% .
+if NOT "%PAPER%" == "" (
+	set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
+)
+
+if "%1" == "" goto help
+
+if "%1" == "help" (
+	:help
+	echo.Please use `make ^<target^>` where ^<target^> is one of
+	echo.  html       to make standalone HTML files
+	echo.  dirhtml    to make HTML files named index.html in directories
+	echo.  singlehtml to make a single large HTML file
+	echo.  pickle     to make pickle files
+	echo.  json       to make JSON files
+	echo.  htmlhelp   to make HTML files and a HTML help project
+	echo.  qthelp     to make HTML files and a qthelp project
+	echo.  devhelp    to make HTML files and a Devhelp project
+	echo.  epub       to make an epub
+	echo.  latex      to make LaTeX files, you can set PAPER=a4 or PAPER=letter
+	echo.  text       to make text files
+	echo.  man        to make manual pages
+	echo.  changes    to make an overview over all changed/added/deprecated items
+	echo.  linkcheck  to check all external links for integrity
+	echo.  doctest    to run all doctests embedded in the documentation if enabled
+	goto end
+)
+
+if "%1" == "clean" (
+	for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
+	del /q /s %BUILDDIR%\*
+	goto end
+)
+
+if "%1" == "html" (
+	%SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/html.
+	goto end
+)
+
+if "%1" == "dirhtml" (
+	%SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
+	goto end
+)
+
+if "%1" == "singlehtml" (
+	%SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
+	goto end
+)
+
+if "%1" == "pickle" (
+	%SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can process the pickle files.
+	goto end
+)
+
+if "%1" == "json" (
+	%SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can process the JSON files.
+	goto end
+)
+
+if "%1" == "htmlhelp" (
+	%SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can run HTML Help Workshop with the ^
+.hhp project file in %BUILDDIR%/htmlhelp.
+	goto end
+)
+
+if "%1" == "qthelp" (
+	%SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can run "qcollectiongenerator" with the ^
+.qhcp project file in %BUILDDIR%/qthelp, like this:
+	echo.^> qcollectiongenerator %BUILDDIR%\qthelp\addrinfo.qhcp
+	echo.To view the help file:
+	echo.^> assistant -collectionFile %BUILDDIR%\qthelp\addrinfo.ghc
+	goto end
+)
+
+if "%1" == "devhelp" (
+	%SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished.
+	goto end
+)
+
+if "%1" == "epub" (
+	%SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The epub file is in %BUILDDIR%/epub.
+	goto end
+)
+
+if "%1" == "latex" (
+	%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
+	goto end
+)
+
+if "%1" == "text" (
+	%SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The text files are in %BUILDDIR%/text.
+	goto end
+)
+
+if "%1" == "man" (
+	%SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The manual pages are in %BUILDDIR%/man.
+	goto end
+)
+
+if "%1" == "changes" (
+	%SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.The overview file is in %BUILDDIR%/changes.
+	goto end
+)
+
+if "%1" == "linkcheck" (
+	%SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Link check complete; look for any errors in the above output ^
+or in %BUILDDIR%/linkcheck/output.txt.
+	goto end
+)
+
+if "%1" == "doctest" (
+	%SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Testing of doctests in the sources finished, look at the ^
+results in %BUILDDIR%/doctest/output.txt.
+	goto end
+)
+
+:end