Exemplo n.º 1
0
 def to_bytes(self):
     result = bytearray(self.total_length)
     offset = 0
     offset = Network.write_int(result, self.length, offset)
     offset = Network.write_int_as_byte(result, self.message_type, offset)
     Network.write_int(result, self.piece_index, offset)
     return result
Exemplo n.º 2
0
    def to_bytes(self):
        result = bytearray(self.total_length)
        offset = 0
        offset = Network.write_int(result, self.length, offset)
        offset = Network.write_int_as_byte(result, self.message_type, offset)
        offset = Network.write_int_as_byte(result, self.type, offset)
        Network.write_bytes(result, self.bencoded_payload, offset)

        return result
Exemplo n.º 3
0
    def from_bytes(cls, data):
        if not check_bytes_length(data, 13):
            return None

        offset = 1
        offset, piece_index = Network.read_integer(data, offset)
        offset, data_offset = Network.read_integer(data, offset)
        offset, data_length = Network.read_integer(data, offset)
        return cls(piece_index, data_offset, data_length)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def to_bytes(self):
        result = bytearray(68)
        offset = 0
        offset = Network.write_int_as_byte(result, self.length, offset)
        result[1:20] = self.protocol
        offset += 19
        offset = Network.write_bytes(result, self.reserved, offset)
        Network.write_bytes(result, self.info_hash, offset)
        result[48:68] = self.peer_id

        return result
Exemplo n.º 6
0
    def to_bytes(self):
        result = dict()
        result[b'msg_type'] = self.metadata_message_type
        result[b'piece'] = self.piece_index
        data = Bencode.bencode(result)
        self.length = 2 + len(data)
        result = bytearray(self.total_length)
        offset = 0

        offset = Network.write_int(result, self.length, offset)
        offset = Network.write_int_as_byte(result, self.message_type, offset)
        offset = Network.write_int_as_byte(result, self.type, offset)
        Network.write_bytes(result, data, offset)
        return result
Exemplo n.º 7
0
    def from_bytes(data):
        length = len(data)
        if length == 0:
            return KeepAliveMessage()

        offset, type = Network.read_byte_as_int(data, 0)
        if type == PeerMessageType.Piece: return PieceMessage.from_bytes(data)
        if type == PeerMessageType.Choke: return ChokeMessage.from_bytes(data)
        if type == PeerMessageType.Unchoke:
            return UnchokeMessage.from_bytes(data)
        if type == PeerMessageType.Interested:
            return InterestedMessage.from_bytes(data)
        if type == PeerMessageType.Uninterested:
            return UninterestedMessage.from_bytes(data)
        if type == PeerMessageType.Have: return HaveMessage.from_bytes(data)
        if type == PeerMessageType.Bitfield:
            return BitfieldMessage.from_bytes(data)
        if type == PeerMessageType.Request:
            return RequestMessage.from_bytes(data)
        if type == PeerMessageType.Port: return PortMessage.from_bytes(data)
        if type == PeerMessageType.Cancel:
            return CancelMessage.from_bytes(data)
        if type == PeerMessageType.HaveAll:
            return HaveAllMessage.from_bytes(data)
        if type == PeerMessageType.HaveNone:
            return HaveNoneMessage.from_bytes(data)
        if type == PeerMessageType.AllowedFast:
            return AllowedFastMessage.from_bytes(data)
        if type == PeerMessageType.SuggestPiece:
            return SuggestPieceMessage.from_bytes(data)
        if type == PeerMessageType.RejectRequest:
            return RejectRequestMessage.from_bytes(data)

        if type == PeerMessageType.ExtensionMessage:
            offset, extension_message_type = Network.read_byte_as_int(
                data, offset)
            if extension_message_type == 0:
                return ExtensionHandshakeMessage.from_bytes(data)
            if extension_message_type == ExtensionProtocolMessageType.PeerExchange:
                return PeerExchangeMessage.from_bytes(data)
            if extension_message_type == ExtensionProtocolMessageType.Metadata:
                return MetadataMessage.from_bytes(data)

        Logger().write(LogVerbosity.Info,
                       "Unknown message! type = " + str(type))
Exemplo n.º 8
0
    def from_bytes(cls, data):
        if not check_bytes_length(data, 68):
            return None

        offset = 0
        offset, length = Network.read_byte_as_int(data, offset)
        offset, protocol = Network.read_bytes(data, 19, offset)
        offset, reserved = Network.read_bytes(data, 8, offset)
        offset, info_hash = Network.read_bytes(data, 20, offset)
        offset, peer_id = Network.read_bytes(data, 20, offset)

        result = cls(info_hash)
        result.length = length
        result.protocol = protocol
        result.reserved = reserved
        result.peer_id = peer_id

        return result
Exemplo n.º 9
0
    def to_bytes(self):
        result = dict()
        added_result = bytearray()
        flag_result = bytearray()
        index = 0

        for uri in self.added:
            added_result[index:index + 6] = uri_to_bytes(uri)
            flag_result.append(0x10)
            index += 6

        result[b'added'] = bytes(added_result)
        result[b'added.f'] = bytes(flag_result)
        bencoded = Bencode.bencode(result)

        self.length = len(bencoded) + 2
        result_bytes = bytearray(self.total_length)
        offset = 0
        offset = Network.write_int(result_bytes, self.length, offset)
        offset = Network.write_int_as_byte(result_bytes, self.message_type,
                                           offset)
        offset = Network.write_int_as_byte(result_bytes, self.type, offset)
        Network.write_bytes(result_bytes, bencoded, offset)
        return result_bytes
Exemplo n.º 10
0
    def from_bytes(cls, data):
        added = []
        dropped = []

        offset, payload_data = Network.read_bytes(data, len(data) - 2, 2)
        payload = Bencode.bdecode(payload_data)
        if b'added' in payload:
            addedbytes = payload[b'added']
            total_peers = int(len(addedbytes) / 6)
            offset = 0
            for index in range(total_peers):
                added.append(uri_from_bytes(addedbytes[offset:offset + 6]))
                offset += 6

        return cls(added, dropped)
Exemplo n.º 11
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)
Exemplo n.º 12
0
def uri_to_bytes(uri):
    uri_port = uri.split(':')
    result = socket.inet_aton(uri_port[0]) + bytearray(2)
    Network.write_ushort(result, int(uri_port[1]), 4)
    return result
Exemplo n.º 13
0
 def to_bytes(self):
     result = bytearray(4)
     Network.write_int(result, 0, 0)
     return result
Exemplo n.º 14
0
    def from_bytes(cls, data):
        if not check_bytes_length(data, 5):
            return None

        offset, piece_index = Network.read_integer(data, 1)
        return cls(piece_index)
Exemplo n.º 15
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)
Exemplo n.º 16
0
def ip_port_to_bytes(ip, port):
    result = bytearray(socket.inet_aton(ip)) + bytearray(2)
    Network.write_ushort(result, port, 4)
    return result
Exemplo n.º 17
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)
Exemplo n.º 18
0
    def from_bytes(cls, data):
        if not check_bytes_length(data, 3):
            return None

        offset, port = Network.read_ushort(data, 1)
        return cls(port)