def from_bytes(cls, frame_bytes: bytes): if not frame_is_enclosed_by_hdlc_flags(frame_bytes): raise hdlc_exceptions.MissingHdlcFlags() frame_format = BaseHdlcFrame.extract_format_field_from_bytes(frame_bytes) if not frame_has_correct_length(frame_format.length, frame_bytes): raise hdlc_exceptions.HdlcParsingError( f"Frame data is not of length specified in frame format field. " f"Should be {frame_format.length} but is {len(frame_bytes)}" ) destination_address = address.HdlcAddress.destination_from_bytes( frame_bytes, "server" ) source_address = address.HdlcAddress.source_from_bytes(frame_bytes, "client") fcs = frame_bytes[-3:-1] frame = cls(destination_address, source_address) if fcs != frame.fcs: raise hdlc_exceptions.HdlcParsingError("FCS is not correct") return frame
def from_bytes(cls, frame_bytes: bytes): if not frame_is_enclosed_by_hdlc_flags(frame_bytes): raise hdlc_exceptions.MissingHdlcFlags() frame_format = BaseHdlcFrame.extract_format_field_from_bytes(frame_bytes) if not frame_has_correct_length(frame_format.length, frame_bytes): raise hdlc_exceptions.HdlcParsingError( f"Frame data is not of length specified in frame format field. " f"Should be {frame_format.length} but is {len(frame_bytes)}" ) destination_address = address.HdlcAddress.destination_from_bytes( frame_bytes, "client" ) source_address = address.HdlcAddress.source_from_bytes(frame_bytes, "server") control_byte_position = ( 1 + 2 + destination_address.length + source_address.length ) control_byte = frame_bytes[control_byte_position : control_byte_position + 1] control = fields.ReceiveReadyControlField.from_bytes(control_byte) fcs = frame_bytes[-3:-1] frame = cls( destination_address=destination_address, source_address=source_address, receive_sequence_number=control.receive_sequence_number, final=control.is_final, ) if fcs != frame.fcs: raise hdlc_exceptions.HdlcParsingError("FCS is not correct") return frame
def from_bytes(cls, frame_bytes: bytes): if not frame_is_enclosed_by_hdlc_flags(frame_bytes): raise hdlc_exceptions.MissingHdlcFlags() frame_format = BaseHdlcFrame.extract_format_field_from_bytes(frame_bytes) if not frame_has_correct_length(frame_format.length, frame_bytes): raise hdlc_exceptions.HdlcParsingError( f"Frame data is not of length specified in frame format field. " f"Should be {frame_format.length} but is {len(frame_bytes)}" ) destination_address = address.HdlcAddress.destination_from_bytes( frame_bytes, "client" ) source_address = address.HdlcAddress.source_from_bytes(frame_bytes, "server") hcs_position = 1 + 2 + destination_address.length + source_address.length + 1 hcs = frame_bytes[hcs_position : hcs_position + 2] fcs = frame_bytes[-3:-1] information = frame_bytes[hcs_position + 2 : -3] frame = cls(destination_address, source_address, information) if hcs != frame.hcs: raise hdlc_exceptions.HdlcParsingError( f"HCS is not correct. " f"Calculated: {frame.hcs!r}, in data: {hcs!r}" ) if fcs != frame.fcs: raise hdlc_exceptions.HdlcParsingError("FCS is not correct") return frame
def from_bytes(cls, frame_bytes: bytes): if not frame_is_enclosed_by_hdlc_flags(frame_bytes): raise hdlc_exceptions.MissingHdlcFlags() frame_format = BaseHdlcFrame.extract_format_field_from_bytes(frame_bytes) if not frame_has_correct_length(frame_format.length, frame_bytes): raise hdlc_exceptions.HdlcParsingError( f"Frame data is not of length specified in frame format field. " f"Should be {frame_format.length} but is {len(frame_bytes)}" ) destination_address = address.HdlcAddress.destination_from_bytes( frame_bytes, "client" ) source_address = address.HdlcAddress.source_from_bytes(frame_bytes, "server") information_control_byte_position = ( 1 + 2 + destination_address.length + source_address.length ) information_control_byte = frame_bytes[ information_control_byte_position : information_control_byte_position + 1 ] information_control = fields.InformationControlField.from_bytes( information_control_byte ) hcs_position = 1 + 2 + destination_address.length + source_address.length + 1 hcs = frame_bytes[hcs_position : hcs_position + 2] fcs = frame_bytes[-3:-1] information = frame_bytes[hcs_position + 2 : -3] frame = cls( destination_address, source_address, information, send_sequence_number=information_control.send_sequence_number, receive_sequence_number=information_control.receive_sequence_number, segmented=frame_format.segmented, final=information_control.final, ) if hcs != frame.hcs: raise hdlc_exceptions.HdlcParsingError( f"HCS is not correct Calculated: {frame.hcs!r}, in data: {hcs!r}" ) if fcs != frame.fcs: raise hdlc_exceptions.HdlcParsingError( f"FCS is not correct, Calculated: {frame.fcs!r}, in data: {fcs!r}" ) return frame