def test_node_descriptor(self,): data = b'\x02\x00\x00\x04\x00\x00\xff\xfd\x02\x01\x00\x04\x00\x00\x00\x00\x02\x03\x00\x04\nq?\xf0' igp_type = 3 descriptor, remain = NodeDescriptor.unpack(data, igp_type) self.assertEqual(descriptor.json(), '"autonomous-system": 65533') descriptor, remain = NodeDescriptor.unpack(remain, igp_type) self.assertEqual(descriptor.json(), '"bgp-ls-identifier": "0"') descriptor, remain = NodeDescriptor.unpack(remain, igp_type) self.assertEqual(descriptor.json(), '"router-id": "10.113.63.240"')
def test_node_descriptor(self, ): data = b'\x02\x00\x00\x04\x00\x00\xff\xfd\x02\x01\x00\x04\x00\x00\x00\x00\x02\x03\x00\x04\nq?\xf0' igp_type = 3 descriptor, remain = NodeDescriptor.unpack(data, igp_type) self.assertEqual(descriptor.json(), '"autonomous-system": 65533') descriptor, remain = NodeDescriptor.unpack(remain, igp_type) self.assertEqual(descriptor.json(), '"bgp-ls-identifier": "0"') descriptor, remain = NodeDescriptor.unpack(remain, igp_type) self.assertEqual(descriptor.json(), '"router-id": "10.113.63.240"')
def unpack (cls, data, rd): proto_id = unpack('!B',data[0:1])[0] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q',data[1:9])[0] # unpack list of node descriptors node_type, node_length = unpack('!HH',data[9:13]) if node_type != 256: raise Exception( 'Unknown type: {}. Only Local Node descriptors are allowed in' 'Node type msg'.format(node_type) ) values = data[13: 13 + node_length] node_ids = [] while values: # Unpack Node Descriptor Sub-TLVs node_id, left = NodeDescriptor.unpack(values, proto_id) node_ids.append(node_id) if left == data: raise RuntimeError("sub-calls should consume data") values = left return cls( domain=domain,proto_id=proto_id, node_ids=node_ids,route_d=rd,packed=data )
def unpack_nlri (cls, data, rd): proto_id = unpack('!B',data[0:1])[0] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q',data[1:9])[0] # unpack list of node descriptors node_type, node_length = unpack('!HH',data[9:13]) if node_type != 256: raise Exception( 'Unknown type: {}. Only Local Node descriptors are allowed in' 'Node type msg'.format(node_type) ) values = data[13: 13 + node_length] node_ids = [] while values: # Unpack Node Descriptor Sub-TLVs node_id, left = NodeDescriptor.unpack(values, proto_id) node_ids.append(node_id) if left == values: raise RuntimeError("sub-calls should consume data") values = left return cls( domain=domain,proto_id=proto_id, node_ids=node_ids,route_d=rd,packed=data )
def unpack_nlri(cls, data, rd): ospf_type = None proto_id = unpack('!B', data[0:1])[0] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q', data[1:9])[0] tlvs = data[9:] while tlvs: tlv_type, tlv_length = unpack('!HH', tlvs[:4]) if tlv_type == 256: values = tlvs[4:4 + tlv_length] local_node = [] while values: # Unpack Local Node Descriptor Sub-TLVs # We pass proto_id as TLV interpretation # follows IGP type node, left = NodeDescriptor.unpack(values, proto_id) local_node.append(node) if left == values: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue if tlv_type == 264: values = tlvs[4:4 + tlv_length] ospf_type = OspfRoute.unpack(values) tlvs = tlvs[4 + tlv_length:] if tlv_type == 265: values = tlvs[4:4 + tlv_length] prefix = IpReach.unpack(values, 4) tlvs = tlvs[4 + tlv_length:] return cls( domain=domain, proto_id=proto_id, packed=data, local_node=local_node, ospf_type=ospf_type, prefix=prefix, route_d=rd, )
def unpack_nlri (cls, data, rd): ospf_type = None proto_id = unpack('!B',data[0:1])[0] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q',data[1:9])[0] tlvs = data[9:] while tlvs: tlv_type, tlv_length = unpack('!HH', tlvs[:4]) if tlv_type == 256: values = tlvs[4: 4 + tlv_length] local_node = [] while values: # Unpack Local Node Descriptor Sub-TLVs # We pass proto_id as TLV interpretation # follows IGP type node, left = NodeDescriptor.unpack(values, proto_id) local_node.append(node) if left == tlvs: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue if tlv_type == 264: values = tlvs[4: 4 + tlv_length] ospf_type = OspfRoute.unpack(values) tlvs = tlvs[4 + tlv_length:] if tlv_type == 265: values = tlvs[4: 4 + tlv_length] prefix = IpReach.unpack(values) tlvs = tlvs[4 + tlv_length:] return cls( domain=domain,proto_id=proto_id,packed=data, local_node=local_node,ospf_type=ospf_type, prefix=prefix,route_d=rd )
def unpack(cls, data, rd): proto_id = unpack('!B', data[0:1])[0] iface_addrs = [] neigh_addrs = [] link_identifiers = [] remote_node = [] local_node = [] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q', data[1:9])[0] tlvs = data[9:] while tlvs: tlv_type, tlv_length = unpack('!HH', tlvs[:4]) if tlv_type == 256: values = tlvs[4:4 + tlv_length] local_node = [] while values: # Unpack Local Node Descriptor Sub-TLVs # We pass proto_id as TLV interpretation # follows IGP type node, left = NodeDescriptor.unpack(values, proto_id) local_node.append(node) if left == tlvs: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue elif tlv_type == 257: # Remote Node Descriptor values = tlvs[4:4 + tlv_length] remote_node = [] while values: node, left = NodeDescriptor.unpack(values, proto_id) remote_node.append(node) if left == tlvs: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue elif tlv_type == 258: # Link Local/Remote identifiers value = tlvs[4:4 + 8] link_identifiers = LinkIdentifier.unpack(value) tlvs = tlvs[4 + 8:] continue elif tlv_type in [259, 261]: # IPv{4,6} Interface Address value = tlvs[4:4 + tlv_length] iface_addrs.append(IfaceAddr.unpack(value)) tlvs = tlvs[4 + tlv_length:] continue elif tlv_type in [260, 262]: # IPv{4,6} Neighbor Address value = tlvs[4:4 + tlv_length] neigh_addrs.append(NeighAddr.unpack(value)) tlvs = tlvs[4 + tlv_length:] continue return cls(domain=domain, proto_id=proto_id, local_node=local_node, remote_node=remote_node, neigh_addrs=neigh_addrs, iface_addrs=iface_addrs, link_ids=link_identifiers, route_d=rd, packed=data)
def unpack_nlri(cls, data, rd): proto_id = unpack('!B', data[0:1])[0] # FIXME: all these list should probably be defined in the objects iface_addrs = [] neigh_addrs = [] link_identifiers = [] topology_ids = [] remote_node = [] local_node = [] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q', data[1:9])[0] tlvs = data[9:] while tlvs: tlv_type, tlv_length = unpack('!HH', tlvs[:4]) value = tlvs[4 : 4 + tlv_length] tlvs = tlvs[4 + tlv_length :] if tlv_type == 256: local_node = [] while value: # Unpack Local Node Descriptor Sub-TLVs # We pass proto_id as TLV interpretation # follows IGP type node, left = NodeDescriptor.unpack(value, proto_id) local_node.append(node) if left == value: raise RuntimeError("sub-calls should consume data") value = left continue if tlv_type == 257: # Remote Node Descriptor remote_node = [] while value: node, left = NodeDescriptor.unpack(value, proto_id) remote_node.append(node) if left == value: raise RuntimeError("sub-calls should consume data") value = left continue if tlv_type == 258: # Link Local/Remote identifiers link_identifiers = LinkIdentifier.unpack(value) continue if tlv_type in [259, 261]: # IPv{4,6} Interface Address iface_addrs.append(IfaceAddr.unpack(value)) continue if tlv_type in [260, 262]: # IPv{4,6} Neighbor Address neigh_addrs.append(NeighAddr.unpack(value)) continue if tlv_type == 263: topology_ids.append(MTID.unpack(value)) continue raise RuntimeError('Not implemented') return cls( domain=domain, proto_id=proto_id, local_node=local_node, remote_node=remote_node, neigh_addrs=neigh_addrs, iface_addrs=iface_addrs, link_ids=link_identifiers, topology_ids=topology_ids, route_d=rd, packed=data, )
def unpack (cls, data, rd): proto_id = unpack('!B',data[0])[0] iface_addrs = [] neigh_addrs = [] link_identifiers = [] remote_node = [] local_node = [] if proto_id not in PROTO_CODES.keys(): raise Exception('Protocol-ID {} is not valid'.format(proto_id)) domain = unpack('!Q',data[1:9])[0] tlvs = data[9:] while tlvs: tlv_type, tlv_length = unpack('!HH', tlvs[:4]) if tlv_type == 256: values = tlvs[4: 4 + tlv_length] local_node = [] while values: # Unpack Local Node Descriptor Sub-TLVs # We pass proto_id as TLV interpretation # follows IGP type node, left = NodeDescriptor.unpack(values, proto_id) local_node.append(node) if left == tlvs: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue elif tlv_type == 257: # Remote Node Descriptor values = tlvs[4: 4 + tlv_length] remote_node = [] while values: node, left = NodeDescriptor.unpack(values, proto_id) remote_node.append(node) if left == tlvs: raise RuntimeError("sub-calls should consume data") values = left tlvs = tlvs[4 + tlv_length:] continue elif tlv_type == 258: # Link Local/Remote identifiers value = tlvs[4: 4 + 8] link_identifiers = LinkIdentifier.unpack(value) tlvs = tlvs[4 + 8:] continue elif tlv_type in [259,261]: # IPv{4,6} Interface Address value = tlvs[4: 4 + tlv_length] iface_addrs.append(IfaceAddr.unpack(value)) tlvs = tlvs[4 + tlv_length:] continue elif tlv_type in [260,262]: # IPv{4,6} Neighbor Address value = tlvs[4: 4 + tlv_length] neigh_addrs.append(NeighAddr.unpack(value)) tlvs = tlvs[4 + tlv_length:] continue return cls(domain=domain,proto_id=proto_id, local_node=local_node,remote_node=remote_node, neigh_addrs=neigh_addrs,iface_addrs=iface_addrs, link_ids=link_identifiers,route_d=rd,packed=data)