user26753351
user26753351

Reputation: 1

Why is the piece size I'm receiving under 9 byte's long when I send a request to a peer through my torrent client?

The request piece I'm sending to a peer is the following:

b'\x00\x00\x00\r\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'

and the response I'm getting is:

b'\x00\x00\x00\x00\x00\x00\x00\x00R'

the unofficial BitTorrent spec specifies that a piece message should follow this:

piece: <len=0009+X><id=7>

The piece message is variable length, where X is the length of the block. The payload contains the following information:

index: integer specifying the zero-based piece index begin: integer specifying the zero-based byte offset within the piece block: block of data, which is a subset of the piece specified by index.

I'm handling piece messages as such:

       elif message_id == 7:
            if len(payload) >= 9:
                piece_index, begin = struct.unpack('!II', payload[:8])
                block = payload[8:]

                if piece_index not in self.received_pieces:
                    self.received_pieces[piece_index] = {}

                self.received_pieces[piece_index][begin] = block

Please do tell me if there is any more info you'd need

I've tried being finnicky with the way that the messages were being handled, but I'm outright just not receiving data so I couldn't really do much.

Upvotes: 0

Views: 71

Answers (1)

the8472
the8472

Reputation: 43125

As a commenter notes, TCP is a byte stream. So you have to keep reading data from the socket until you can decode a complete message. What's currently in the read buffer is not guaranteed to be a complete message, exactly one message or anything like that. It's just a stream of bytes and you have to do the segmenting.

Bittorrent streams are also not a synchronized RPC mechanism, so you're not guaranteed that the next thing you receive is a reply to your last message. Instead you have to keep track of what you requested, the peer might be sending something else in the meantime and only answer it a few messages later.

For debugging I recommend using wireshark, it can decode bittorrent as long as the TCP connection gets recorded from the beginning.

Upvotes: -1

Related Questions