Search code examples
pythonsocketsbittorrent

Bittorrent protocol TCP communication example


I'm trying to implement a bittorent client and I got stuck at the part were I succesfully connected to a peer but I don't know how to communicate with the peer.

I managed to decode the torrent metadata file, I succesfully got all the info from there, I connected to a peer with TCP, I send the handshake message, I receive the handshake message back from the peer, but after that I don't receive any message from the peer (I was expecting the have messages). I tried sending an unchoke message to the peer and I started receiving some data but I don't know how to understand that data.

This is what I have until now:

s.connect((ip, port))
print "Connected"

message = "%s%s%s%s%s" % (chr(19), "BitTorrent protocol", 8 * chr(0),
                         handshake_params["info_hash"], 
                         handshake_params["peer_id"]

s.send(message)
handshake_data = s.recv(4096)

# unchoke
m = struct.pack(">IB", 1, 1)
s.send(m)
data = s.recv(4096)

print handshake_data
print struct.unpack("B" * len(data), data)

And this is the output:

BitTorrent protocolp    p�I0a��9"x`��-UT3450-��kP+�BG   ���������
(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 231, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, 255, 251, 255, 255, 255, 255, 255, 255, 223, 255, 255, 255, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 127, 255, 221, 255, 255, 255, 255, 255, 191, 191, 255, 255, 127, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 255, 247, 255, 255, 255, 255, 255, 255, 255, 255, 255, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 255, 255, 255, 251, 255, 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 252, 0, 0, 0, 5, 4, 0, 0, 4, 191, 0, 0, 0, 5, 4, 0, 0, 1, 123, 0, 0, 0, 5, 4, 0, 0, 2, 122, 0, 0, 0, 5, 4, 0, 0, 2, 126, 0, 0, 0, 5, 4, 0, 0, 2, 177, 0, 0, 0, 5, 4, 0, 0, 2, 104, 0, 0, 0, 5, 4, 0, 0, 1, 37, 0, 0, 0, 5, 4, 0, 0, 0, 174, 0, 0, 0, 5, 4, 0, 0, 4, 157, 0, 0, 0, 5, 4, 0, 0, 0, 4, 0, 0, 0, 5, 4, 0, 0, 3, 172, 0, 0, 0, 5, 4, 0, 0, 2, 241, 0, 0, 0, 5, 4, 0, 0, 1, 90, 0, 0, 0, 5, 4, 0, 0, 3, 251, 0, 0, 0, 5, 4, 0, 0, 2, 200, 0, 0, 0, 5, 4, 0, 0, 0, 179, 0, 0, 0, 5, 4, 0, 0, 0, 180, 0, 0, 0, 5, 4, 0, 0, 3, 113, 0, 0, 0, 5, 4, 0, 0, 4, 181, 0, 0, 0, 5, 4, 0, 0, 1, 16, 0, 0, 0, 5, 4, 0, 0, 2, 169, 0, 0, 0, 5, 4, 0, 0, 4, 81, 0, 0, 0, 5, 4, 0, 0, 2, 57, 0, 0, 0, 5, 4, 0, 0, 1, 219)

The handshake data looks ok. What I can't figure out is why I get so many 255 bytes there. I was trying to decode the message using the length_prefix, message_id, payload format, but since I get so many 255 bytes at the beginning it results in the message having a huge length and I don't have a message that big.

Is there any kind of noise I should try and filter out? If you scroll right a lot you will see that at some point the bytes start being decent but I don't know what to do with the beginning of the message.


Solution

  • s.recv(4096)

    You're just reading an block of data of some unspecified length, presumably whatever is in the TCP buffer.

    Bittorrent is message-based. TCP is a stream of bytes, which means it does not nicely chop the data sent by the remote peer into messages, you will have to do that yourself.