Permalink
Browse files

Migrate from IPy to ipaddress (which is part of the standard library …

…since Py3.3)
  • Loading branch information...
1 parent 9b7e73a commit dbf76f379b1ca67c222089d5870df8098929a2bd @steffann committed Mar 10, 2013
@@ -4,10 +4,10 @@
@author: sander
'''
from bitstring import ConstBitStream, BitStream, Bits
+from ipaddress import IPv4Address
from pylisp.packet.ip import protocol_registry
from pylisp.packet.ip.protocol import Protocol
from pylisp.utils import checksum
-from pylisp.utils.IPy_clone import IP
import math
import numbers
@@ -129,13 +129,11 @@ def sanitize(self):
raise ValueError('Invalid TTL')
# Check the source and destination addresses
- if not isinstance(self.source, IP) \
- or self.source.version() != 4:
+ if not isinstance(self.source, IPv4Address):
raise ValueError('Source address must be IPv4')
- if not isinstance(self.destination, IP) \
- or self.destination.version() != 4:
- raise ValueError('Source address must be IPv4')
+ if not isinstance(self.destination, IPv4Address):
+ raise ValueError('Destination address must be IPv4')
@classmethod
def from_bytes(cls, bitstream):
@@ -206,8 +204,8 @@ def from_bytes(cls, bitstream):
raise ValueError('Header checksum does not match')
# Read the source and destination addresses
- packet.source = IP(bitstream.read('uint:32'))
- packet.destination = IP(bitstream.read('uint:32'))
+ packet.source = IPv4Address(bitstream.read('uint:32'))
+ packet.destination = IPv4Address(bitstream.read('uint:32'))
# Read the options
option_len = (ihl - 5) * 4
@@ -274,8 +272,8 @@ def to_bytes(self):
# Write the source and destination addresses
bitstream += BitStream('uint:32=%d, '
- 'uint:32=%d' % (self.source.ip,
- self.destination.ip))
+ 'uint:32=%d' % (int(self.source),
+ int(self.destination)))
# Add the options
bitstream += BitStream(bytes=self.options)
@@ -4,9 +4,9 @@
@author: sander
'''
from bitstring import ConstBitStream, BitStream, Bits
+from ipaddress import IPv6Address
from pylisp.packet.ip import protocol_registry
from pylisp.packet.ip.protocol import Protocol
-from pylisp.utils.IPy_clone import IP
import numbers
@@ -94,13 +94,11 @@ def sanitize(self):
raise ValueError('Invalid hop limit')
# Check the source and destination addresses
- if not isinstance(self.source, IP) \
- or self.source.version() != 6:
+ if not isinstance(self.source, IPv6Address):
raise ValueError('Source address must be IPv6')
- if not isinstance(self.destination, IP) \
- or self.destination.version() != 6:
- raise ValueError('Source address must be IPv6')
+ if not isinstance(self.destination, IPv6Address):
+ raise ValueError('Destination address must be IPv6')
@classmethod
def from_bytes(cls, bitstream):
@@ -137,8 +135,8 @@ def from_bytes(cls, bitstream):
packet.hop_limit = bitstream.read('uint:8')
# Read the source and destination addresses
- packet.source = IP(bitstream.read('uint:128'))
- packet.destination = IP(bitstream.read('uint:128'))
+ packet.source = IPv6Address(bitstream.read('uint:128'))
+ packet.destination = IPv6Address(bitstream.read('uint:128'))
# And the rest is payload
packet.payload = bitstream.read('bytes:%d' % payload_length)
@@ -185,8 +183,8 @@ def to_bytes(self):
# Write the source and destination addresses
bitstream += BitStream('uint:128=%d, '
- 'uint:128=%d' % (self.source.ip,
- self.destination.ip))
+ 'uint:128=%d' % (int(self.source),
+ int(self.destination)))
return bitstream.bytes + payload_bytes
@@ -49,17 +49,17 @@ def generate_pseudo_header(self, source, destination):
header = BitStream('uint:32=%d, '
'uint:32=%d, '
'uint:16=17, '
- 'uint:16=%d' % (source.ip,
- destination.ip,
+ 'uint:16=%d' % (int(source),
+ int(destination),
udp_length))
elif source.version() == 6 and destination.version() == 6:
# Generate an IPv6 pseudo-header
header = BitStream('uint:128=%d, '
'uint:128=%d, '
'uint:32=%d, '
- 'uint:32=17' % (source.ip,
- destination.ip,
+ 'uint:32=17' % (int(source),
+ int(destination),
udp_length))
else:
raise ValueError('Source and destination must belong to the same '
@@ -4,7 +4,7 @@
@author: sander
'''
from bitstring import ConstBitStream, BitArray, Bits
-from pylisp.utils.IPy_clone import IP
+from ipaddress import IPv4Address, IPv6Address, IPv4Network, IPv6Network
from pylisp.utils.afi import read_afi_address_from_bitstream, \
get_bitstream_for_afi_address
from pylisp.utils.lcaf.base import LCAFAddress
@@ -127,25 +127,31 @@ def sanitize(self):
# SHOULD be a multicast address if it is being mapped from a
# multicast destination EID.
- if isinstance(self.locator, IP):
+ if isinstance(self.locator, (IPv4Address, IPv6Address)):
addresses = [self.locator]
elif isinstance(self.locator, LCAFAddress):
addresses = self.locator.get_addresses()
else:
raise ValueError('Locator must be an (LCAF) IPv4 or IPv6 address')
for address in addresses:
- if not isinstance(address, IP):
- raise ValueError('Locator must be an IPv4 or IPv6 address')
+ if isinstance(self.locator, IPv4Address):
+ if address == IPv4Address(u'255.255.255.255'):
+ raise ValueError('Locator must not be the broadcast '
+ 'address')
+
+ if address in IPv4Network(u'224.0.0.0/24'):
+ raise ValueError('Locator must not be a link-local '
+ 'multicast address')
- if address == IP('255.255.255.255'):
- raise ValueError('Locator must not be broadcast address')
+ elif isinstance(self.locator, IPv6Address):
+ if address in IPv6Network('ff02::/16') \
+ or address in IPv6Network('ff12::/16'):
+ raise ValueError('Locator must not be a link-local '
+ 'multicast address')
- if address in IP('224.0.0.0/24') \
- or address in IP('ff02::/16') \
- or address in IP('ff12::/16'):
- raise ValueError('Locator must not be link-local multicast ' +
- 'address')
+ else:
+ raise ValueError('Locator must be an IPv4 or IPv6 address')
@classmethod
def from_bytes(cls, bitstream):
@@ -4,8 +4,8 @@
@author: sander
'''
from bitstring import ConstBitStream, BitArray, Bits
+from ipaddress import IPv4Network, IPv6Network
from pylisp.packet.lisp.control import LocatorRecord
-from pylisp.utils.IPy_clone import IP
from pylisp.utils.afi import read_afi_address_from_bitstream, \
get_bitstream_for_afi_address
from pylisp.utils.lcaf.instance_address import LCAFInstanceAddress
@@ -249,7 +249,7 @@ def sanitize(self):
# EID-prefix: 4 octets if an IPv4 address-family, 16 octets if an IPv6
# address-family.
if not isinstance(self.eid_prefix, LCAFInstanceAddress):
- if not isinstance(self.eid_prefix, IP):
+ if not isinstance(self.eid_prefix, (IPv4Network, IPv6Network)):
raise ValueError("Unexpected EID prefix %r", self.eid_prefix)
# Wrap in LCAF address with Instance ID
@@ -347,7 +347,7 @@ def to_bitstream(self):
bitstream += BitArray('uint:8=%d' % len(self.locator_records))
# Add the EID prefix mask length
- bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen())
+ bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen)
# Add the NMR action
bitstream += BitArray('uint:3=%d' % self.action)
@@ -4,8 +4,8 @@
@author: sander
'''
from bitstring import ConstBitStream, BitArray, Bits
+from ipaddress import IPv4Network, IPv6Network
from pylisp.packet.lisp.control import LocatorRecord
-from pylisp.utils.IPy_clone import IP
from pylisp.utils.afi import read_afi_address_from_bitstream, \
get_bitstream_for_afi_address
from pylisp.utils.represent import represent
@@ -121,7 +121,7 @@ def sanitize(self):
# EID-prefix: 4 octets if an IPv4 address-family, 16 octets if an IPv6
# address-family.
- if not isinstance(self.eid_prefix, IP):
+ if not isinstance(self.eid_prefix, (IPv4Network, IPv6Network)):
raise ValueError('EID prefix must be IPv4 or IPv6')
# Check locator records
@@ -200,7 +200,7 @@ def to_bitstream(self):
bitstream += BitArray('uint:8=%d' % len(self.locator_records))
# Add the EID prefix mask length
- bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen())
+ bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen)
# Add the NMR action
bitstream += BitArray('uint:3=%d' % self.action)
@@ -4,9 +4,9 @@
@author: sander
'''
from bitstring import ConstBitStream, BitArray, Bits
+from ipaddress import IPv4Address, IPv6Address, IPv4Network, IPv6Network
from pylisp.packet.lisp.control import type_registry, ControlMessage, \
MapReplyRecord
-from pylisp.utils.IPy_clone import IP
from pylisp.utils.afi import read_afi_address_from_bitstream, \
get_bitstream_for_afi_address
from pylisp.utils.lcaf.base import LCAFAddress
@@ -134,17 +134,15 @@ def sanitize(self):
if len(addresses) != 1:
raise ValueError('Source EID must be one address')
- if not isinstance(addresses[0], IP) \
- or addresses[0].len() != 1:
+ if not isinstance(addresses[0], (IPv4Address, IPv6Address)):
raise ValueError('Invalid source EID: %r' % self.source_eid)
# ITR-RLOC Address: Used to give the ETR the option of selecting the
# destination address from any address family for the Map-Reply
# message. This address MUST be a routable RLOC address of the
# sender of the Map-Request message.
for itr_rloc in self.itr_rlocs:
- if not isinstance(itr_rloc, IP) \
- or itr_rloc.len() != 1:
+ if not isinstance(itr_rloc, (IPv4Address, IPv6Address)):
raise ValueError('Invalid ITR RLOC')
# EID-prefix: 4 octets if an IPv4 address-family, 16 octets if an IPv6
@@ -165,7 +163,7 @@ def sanitize(self):
addresses = [eid_prefix]
for address in addresses:
- if not isinstance(address, IP):
+ if not isinstance(address, (IPv4Network, IPv6Network)):
raise ValueError('Invalid EID prefix: %r', address)
# Map-Reply Record: When the M bit is set, this field is the size of a
@@ -203,11 +201,11 @@ def from_bytes(cls, bitstream):
>>> message.nonce
'\xae\x92\xb5WO\x84\x9c\xd0'
>>> message.source_eid
- IP('172.16.31.3')
+ IPv4Address(u'172.16.31.3')
>>> message.itr_rlocs
- [IP('92.254.28.189')]
+ [IPv4Address(u'92.254.28.189')]
>>> message.eid_prefixes
- [IP('172.16.31.1')]
+ [IPv4Network(u'172.16.31.1/32')]
>>> message.map_reply
'''
packet = cls()
@@ -283,8 +281,8 @@ def to_bytes(self):
r'''
Create bytes from properties
- >>> message = MapRequestMessage(itr_rlocs=[IP('192.0.2.1')],
- ... eid_prefixes=[IP('2001:db8::/32')])
+ >>> message = MapRequestMessage(itr_rlocs=[IPv4Address(u'192.0.2.1')],
+ ... eid_prefixes=[IPv6Network(u'2001:db8::/32')])
>>> hex = message.to_bytes().encode('hex')
>>> hex[:40]
'10000001000000000000000000000001c0000201'
@@ -329,7 +327,7 @@ def to_bytes(self):
for eid_prefix in self.eid_prefixes:
# Add padding and prefix length
bitstream += BitArray('uint:8=0, '
- 'uint:8=%d' % eid_prefix.prefixlen())
+ 'uint:8=%d' % eid_prefix.prefixlen)
# Add the address
bitstream += get_bitstream_for_afi_address(eid_prefix)
Oops, something went wrong.

0 comments on commit dbf76f3

Please sign in to comment.