示例#1
0
文件: link.py 项目: gklinich/exabgp
    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,
        )
示例#2
0
    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)
示例#3
0
	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)