[docs]classFrameGeneratorScapy(FrameGenerator):"""Scapy wrapper frame generator. Attrs: packet: Scapy packet header to which a payload would be attached. packet_type: Type of the first layer of the packet header. """packet:Packetpacket_type:Type[Packet]def__init__(self,packet:Packet)->None:""" Args: packet: Packet to which a payload will be attached. """self.packet=packetself.packet_num_bits=len(packet)*8self.packet_type=packet.layers()[0]
[docs]defpack_frame(self,source:BitsSource,num_bits:int)->np.ndarray:"""Generate a frame of num_bits bits from the given bitsource. Note that the payload size is num_bits minus number of bits in the packet header. Note that payload can be of size 0, in which case no data would be sent (except for the packet header). Args: source: Payload source. num_bits: Number of bits in the whole resulting frame. Raises: ValueError: If num_bits is not enough to fit the packet. """payload_num_bits=num_bits-self.packet_num_bitsifpayload_num_bits<0:raiseValueError(f"Packet header is bigger then the requested amount of bits ({len(self.packet)*8} > {num_bits}).")packet_new=self.packet_type()packet_new.add_payload(np.packbits(source.generate_bits(payload_num_bits)).tobytes())returnnp.unpackbits(np.frombuffer(raw(packet_new),np.uint8))
[docs]defunpack_frame(self,frame:np.ndarray)->np.ndarray:ifframe.size<self.packet_num_bits:raiseValueError(f"The frame contains less bits then the header ({frame.size} < {self.packet_num_bits}).")returnframe[self.packet_num_bits:]