def get_cell_cmd(cell_bytes: bytes, link_version: int): """ Get the command of a cell which is represented in bytes :param cell_bytes: the bytes of the cell :param link_version: the link version of the cell :return: the cells command """ cmd = struct.unpack(PACK_FMT[public_constants.CMD_LEN], cell_bytes[public_constants.CIRCID_LEN( link_version):public_constants.CIRCID_LEN(link_version) + public_constants.CMD_LEN])[0] return cmd
def get_relay_cmd(cell_bytes: bytes, link_version: int): """ Get the command of the relay cell which is in bytes :param cell_bytes: the cell bytes :param link_version: the link version of the cell :return: the relay command """ cmd = struct.unpack(PACK_FMT[public_constants.CMD_LEN], cell_bytes[public_constants.CIRCID_LEN( link_version) + public_constants.CMD_LEN:public_constants.CIRCID_LEN( link_version) + public_constants.CMD_LEN * 2])[0] return cmd
def set_fmt_string(self): """ Sets the string format of the cell based on the link version. :return: None """ if public_constants.CIRCID_LEN(self.link_version) == 2: self.fmt_string = '!HB' else: self.fmt_string = '!LB'
def buffer_ok(self, buffer: bytes): """ Checks if the buffer starts with the correct circuit ID :param buffer: buffer of bytes from the read :return:True if recognized, False otherwise """ return int.from_bytes( buffer[:public_constants.CIRCID_LEN(self.link_version)], 'big') == self.circID
def set_fmt_string(self): if public_constants.CIRCID_LEN(self.link_version) == 2: self.fmt_string = '!HBH' else: self.fmt_string = '!LBH'