~ubuntu-branches/ubuntu/utopic/python-ipaddr/utopic

« back to all changes in this revision

Viewing changes to ipaddr.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-01-25 07:08:30 UTC
  • mfrom: (1.2.7 upstream) (1.4.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110125070830-5x1pwesudizdkkol
Tags: 2.1.7-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
"""
24
24
 
25
 
__version__ = '2.1.5'
 
25
__version__ = '2.1.7'
26
26
 
27
27
import struct
28
28
 
119
119
                     address)
120
120
 
121
121
 
 
122
def v4_int_to_packed(address):
 
123
    """The binary representation of this address.
 
124
 
 
125
    Args:
 
126
        address: An integer representation of an IPv4 IP address.
 
127
 
 
128
    Returns:
 
129
        The binary representation of this address.
 
130
 
 
131
    Raises:
 
132
        ValueError: If the integer is too large to be an IPv4 IP
 
133
          address.
 
134
    """
 
135
    if address > _BaseV4._ALL_ONES:
 
136
        raise ValueError('Address too large for IPv4')
 
137
    return struct.pack('!I', address)
 
138
 
 
139
 
 
140
def v6_int_to_packed(address):
 
141
    """The binary representation of this address.
 
142
 
 
143
    Args:
 
144
        address: An integer representation of an IPv4 IP address.
 
145
 
 
146
    Returns:
 
147
        The binary representation of this address.
 
148
    """
 
149
    return struct.pack('!QQ', address >> 64, address & (2**64 - 1))
 
150
 
 
151
 
122
152
def _find_address_range(addresses):
123
153
    """Find a sequence of addresses.
124
154
 
476
506
        return  '%s' % self._string_from_ip_int(self._ip)
477
507
 
478
508
    def __hash__(self):
479
 
        return hash(hex(self._ip))
 
509
        return hash(hex(long(self._ip)))
480
510
 
481
511
    def _get_address_key(self):
482
512
        return (self._version, self)
915
945
                         version=self._version)
916
946
 
917
947
    def subnet(self, prefixlen_diff=1, new_prefix=None):
918
 
        """Return a list of subnets, rather than an interator."""
 
948
        """Return a list of subnets, rather than an iterator."""
919
949
        return list(self.iter_subnets(prefixlen_diff, new_prefix))
920
950
 
921
951
    def supernet(self, prefixlen_diff=1, new_prefix=None):
1065
1095
    @property
1066
1096
    def packed(self):
1067
1097
        """The binary representation of this address."""
1068
 
        return struct.pack('!I', self._ip)
 
1098
        return v4_int_to_packed(self._ip)
1069
1099
 
1070
1100
    @property
1071
1101
    def version(self):
1394
1424
 
1395
1425
        ip_int = 0
1396
1426
 
1397
 
        fields = self._explode_shorthand_ip_string(ip_str).split(':')
1398
 
 
1399
1427
        # Do we have an IPv4 mapped (::ffff:a.b.c.d) or compact (::a.b.c.d)
1400
1428
        # ip_str?
 
1429
        fields = ip_str.split(':')
1401
1430
        if fields[-1].count('.') == 3:
1402
1431
            ipv4_string = fields.pop()
1403
1432
            ipv4_int = IPv4Network(ipv4_string)._ip
1406
1435
                octets.append(hex(ipv4_int & 0xFFFF).lstrip('0x').rstrip('L'))
1407
1436
                ipv4_int >>= 16
1408
1437
            fields.extend(reversed(octets))
 
1438
            ip_str = ':'.join(fields)
1409
1439
 
 
1440
        fields = self._explode_shorthand_ip_string(ip_str).split(':')
1410
1441
        for field in fields:
1411
1442
            try:
1412
1443
                ip_int = (ip_int << 16) + int(field or '0', 16)
1603
1634
    @property
1604
1635
    def packed(self):
1605
1636
        """The binary representation of this address."""
1606
 
        return struct.pack('!QQ', self._ip >> 64, self._ip & (2**64 - 1))
 
1637
        return v6_int_to_packed(self._ip)
1607
1638
 
1608
1639
    @property
1609
1640
    def version(self):
1718
1749
        except AddressValueError:
1719
1750
            return None
1720
1751
 
 
1752
    @property
 
1753
    def teredo(self):
 
1754
        """Tuple of embedded teredo IPs.
 
1755
 
 
1756
        Returns:
 
1757
            Tuple of the (server, client) IPs or None if the address
 
1758
            doesn't appear to be a teredo address (doesn't start with
 
1759
            2001)
 
1760
 
 
1761
        """
 
1762
        bits = self._explode_shorthand_ip_string().split(':')
 
1763
        if not bits[0] == '2001':
 
1764
            return None
 
1765
        return (IPv4Address(int(''.join(bits[2:4]), 16)),
 
1766
                IPv4Address(int(''.join(bits[6:]), 16) ^ 0xFFFFFFFF))
 
1767
 
 
1768
    @property
 
1769
    def sixtofour(self):
 
1770
        """Return the IPv4 6to4 embedded address.
 
1771
 
 
1772
        Returns:
 
1773
            The IPv4 6to4-embedded address if present or None if the
 
1774
            address doesn't appear to contain a 6to4 embedded address.
 
1775
 
 
1776
        """
 
1777
        bits = self._explode_shorthand_ip_string().split(':')
 
1778
        if not bits[0] == '2002':
 
1779
            return None
 
1780
        return IPv4Address(int(''.join(bits[1:3]), 16))
 
1781
 
1721
1782
 
1722
1783
class IPv6Address(_BaseV6, _BaseIP):
1723
1784
 
1868
1929
                raise ValueError('%s has host bits set' %
1869
1930
                                 self.ip)
1870
1931
 
1871
 
 
1872
1932
    def _is_valid_netmask(self, prefixlen):
1873
1933
        """Verify that the netmask/prefixlen is valid.
1874
1934