def test_bad_input_address(self): ''' Ask to encode something that is not an address ''' address = 'Something that is not an address' with self.assertRaisesRegexp(ValueError, '[Uu]nsupported'): afi.get_bitstream_for_afi_address(address)
def _to_data_bytes(self): data = BitArray('uint:16=%d, uint:16=%d' % (self.map_server_port, self.etr_port)) data += get_bitstream_for_afi_address(self.global_etr_rloc) data += get_bitstream_for_afi_address(self.map_server_rloc) data += get_bitstream_for_afi_address(self.private_etr_rloc) for rtr_rloc in self.rtr_rlocs: data += get_bitstream_for_afi_address(rtr_rloc) return data
def to_bytes(self): ''' Create bytes from properties ''' # Verify that properties make sense self.sanitize() # Start with the type bitstream = BitArray('uint:4=%d' % self.message_type) # Add the flags bitstream += BitArray('bool=%d' % self.is_reply) # Add reserved bits bitstream += self._reserved1 # Add the nonce bitstream += BitArray(bytes=self.nonce) # Add the key-id and authentication data bitstream += BitArray('uint:16=%d, uint:16=%d, hex=%s' % (self.key_id, len(self.authentication_data), self.authentication_data.encode('hex'))) # Add the TTL bitstream += BitArray('uint:32=%d' % self.ttl) # Add reserved bits bitstream += self._reserved2 # Add the EID prefix mask length bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen) # Add the EID prefix bitstream += get_bitstream_for_afi_address(self.eid_prefix) # Add the reply bitstream += get_bitstream_for_afi_address(self.reply) return bitstream.bytes
def _to_data_bytes(self): data = BitArray('bool=%d, uint:15=%d, uint:8=%d, ' 'uint:8=%d' % (self.north, self.latitude_degrees, self.latitude_minutes, self.latitude_seconds)) data += BitArray('bool=%d, uint:15=%d, uint:8=%d, ' 'uint:8=%d' % (self.east, self.longitude_degrees, self.longitude_minutes, self.longitude_seconds)) data += BitArray('int:32=%d' % self.altitude) data += get_bitstream_for_afi_address(self.address) return data
def test_afi_1_with_prefixlen(self): ''' Test decoding of AFI 1 (IPv4) prefixes ''' afi_address_hex = '0001c0000200' bitstream = ConstBitStream(hex=afi_address_hex) address = afi.read_afi_address_from_bitstream(bitstream, 24) self.assertEqual(address, IPv4Network(u'192.0.2.0/24')) self.assertEqual(bitstream.pos, bitstream.len, 'unprocessed bits remaining in bitstream') new_bitstream = afi.get_bitstream_for_afi_address(address) self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
def test_afi_0_without_prefixlen(self): ''' Test en/decoding of empty AFI addresses ''' afi_address_hex = '0000' bitstream = ConstBitStream(hex=afi_address_hex) address = afi.read_afi_address_from_bitstream(bitstream) self.assertIsNone(address, 'wrong address') self.assertEqual(bitstream.pos, bitstream.len, 'unprocessed bits remaining in bitstream') new_bitstream = afi.get_bitstream_for_afi_address(address) self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
def test_afi_2_with_prefixlen(self): ''' Test decoding of AFI 2 (IPv6) prefixes ''' afi_address_hex = '000220010db80102abcd0000000000000000' bitstream = ConstBitStream(hex=afi_address_hex) address = afi.read_afi_address_from_bitstream(bitstream, 64) self.assertEqual(address, IPv6Network(u'2001:db8:102:abcd::/64')) self.assertEqual(bitstream.pos, bitstream.len, 'unprocessed bits remaining in bitstream') new_bitstream = afi.get_bitstream_for_afi_address(address) self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
def test_afi_0_with_prefixlen_0(self): ''' Although a prefix length of 0 is allowed (since that is how many implementations transmit it on the wire) ''' afi_address_hex = '0000' bitstream = ConstBitStream(hex=afi_address_hex) address = afi.read_afi_address_from_bitstream(bitstream, 0) self.assertIsNone(address, 'wrong address') self.assertEqual(bitstream.pos, bitstream.len, 'unprocessed bits remaining in bitstream') new_bitstream = afi.get_bitstream_for_afi_address(address) self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
def to_bitstream(self): """ Create bitstream from properties """ # Verify that properties make sense self.sanitize() # Start with the TTL bitstream = BitArray("uint:32=%d" % self.ttl) # Add the locator count bitstream += BitArray("uint:8=%d" % len(self.locator_records)) # Add the EID prefix mask length bitstream += BitArray("uint:8=%d" % self.eid_prefix.prefixlen) # Add the NMR action bitstream += BitArray("uint:3=%d" % self.action) # Add the authoritative flag bitstream += BitArray("bool=%d, bool=%d" % (self.authoritative, self.incomplete)) # Add reserved bits bitstream += self._reserved1 # Add sigcount bitstream += BitArray("uint:4=%d" % len(self.signatures)) # Add the map version bitstream += BitArray("uint:12=%d" % self.map_version) # Add the EID prefix bitstream += get_bitstream_for_afi_address(self.eid_prefix) # Add the locator records for locator_record in self.locator_records: bitstream += locator_record.to_bitstream() # TODO: Can't handle signatures yet! [LISP-Security] if self.signatures: raise NotImplementedError("Cannot handle signatures yet") return bitstream
def to_bitstream(self): ''' Create bitstream from properties ''' # Verify that properties make sense self.sanitize() # Start with the TTL bitstream = BitArray('uint:32=%d' % self.ttl) # Add the locator count bitstream += BitArray('uint:8=%d' % len(self.locator_records)) # Add the EID prefix mask length bitstream += BitArray('uint:8=%d' % self.eid_prefix.prefixlen) # Add the NMR action bitstream += BitArray('uint:3=%d' % self.action) # Add the authoritative flag bitstream += BitArray('bool=%d' % self.authoritative) # Add reserved bits bitstream += self._reserved1 # Add the map version bitstream += BitArray('uint:12=%d' % self.map_version) # Add the EID prefix bitstream += get_bitstream_for_afi_address(self.eid_prefix) # Add the locator records for locator_record in self.locator_records: bitstream += locator_record.to_bitstream() return bitstream
def _to_data_bytes(self): data = BitArray('uint:32=%d' % self.asn) data += get_bitstream_for_afi_address(self.address) return data
def to_bytes(self): r''' Create bytes from properties >>> 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' >>> hex[40:] '0020000220010db8000000000000000000000000' ''' # Verify that properties make sense self.sanitize() # Start with the type bitstream = BitArray('uint:4=%d' % self.message_type) # Add the flags bitstream += BitArray('bool=%d, bool=%d, bool=%d, bool=%d, ' 'bool=%d, bool=%d' % (self.authoritative, self.map_reply is not None, self.probe, self.smr, self.pitr, self.smr_invoked)) # Add padding bitstream += self._reserved1 # Add IRC bitstream += BitArray('uint:5=%d' % (len(self.itr_rlocs) - 1)) # Add record count bitstream += BitArray('uint:8=%d' % len(self.eid_prefixes)) # Add the nonce bitstream += BitArray(bytes=self.nonce) # Add the source EID bitstream += get_bitstream_for_afi_address(self.source_eid) # Add the ITR RLOCs for itr_rloc in self.itr_rlocs: bitstream += get_bitstream_for_afi_address(itr_rloc) # Add the EIDs for eid_prefix in self.eid_prefixes: # Make sure it is a network eid_prefix = ip_network(eid_prefix) # Add padding and prefix length bitstream += BitArray('uint:8=0, ' 'uint:8=%d' % eid_prefix.prefixlen) # Add the address bitstream += get_bitstream_for_afi_address(eid_prefix) # Add the map-reply record if present if self.map_reply is not None: bitstream += self.map_reply.to_bitstream() return bitstream.bytes
def _to_data_bytes(self): data = BitArray() for address in self.addresses: data += get_bitstream_for_afi_address(address) return data
def _to_data_bytes(self): self.sanitize() data = BitArray('uint:32=%d' % self.instance_id) data += get_bitstream_for_afi_address(self.address) return data