Ejemplo n.º 1
0
    def from_bytes(data):
        if not check_minimal_bytes_length(data, 8):
            return None

        msg = TrackerResponseMessage()

        offset = 0
        msg.peers = list()
        msg.error = None

        try:
            offset, msg.message_type = read_integer(data, offset)
            offset, msg.transaction_id = read_integer(data, offset)
            if msg.message_type == 3:
                msg.error = str(data[offset:len(data)])
                Logger().write(LogVerbosity.Important,
                               "Tracker error message: " + msg.error)
            else:
                offset, msg.interval = read_integer(data, offset)
                offset, msg.leechers = read_integer(data, offset)
                offset, msg.seeders = read_integer(data, offset)

                bytes_left = len(data) - offset
                total_peers = int(bytes_left / 6)
                for index in range(total_peers):
                    msg.peers.append(uri_from_bytes(data[offset:offset + 6]))
                    offset += 6
        except Exception as e:
            Logger().write(
                LogVerbosity.Important, "Error in tacker message: " + str(e) +
                ". Message: " + str(data))
            return None

        return msg
Ejemplo n.º 2
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 10):
            return None

        offset = 1
        offset, piece_index = Network.read_integer(data, offset)
        offset, data_offset = Network.read_integer(data, offset)
        offset, data_bytes = Network.read_bytes(data,
                                                len(data) - offset, offset)
        return cls(piece_index, data_offset, data_bytes)
Ejemplo n.º 3
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 2):
            return None

        offset, payload_data = Network.read_bytes(data, len(data) - 2, 2)
        payload = Bencode.bdecode(payload_data)

        metadata_message_type = payload[b'msg_type']
        piece_index = payload[b'piece']
        total_size = 0
        metadata = None
        if metadata_message_type == MetadataMessageType.Data:
            if b'total_size' not in payload:
                return None
            total_size = payload[b'total_size']
            metadata = data[2 + len(Bencode.bencode(payload)):len(data)]

        return cls(None, metadata_message_type, piece_index, total_size,
                   metadata)
Ejemplo n.º 4
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 3):
            return None

        offset, payload = Network.read_bytes(data, len(data) - 2, 2)
        return cls(payload)
Ejemplo n.º 5
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 2):
            return None

        offset, bitfield = Network.read_bytes(data, len(data) - 1, 1)
        return cls(bitfield)