Error in order of extension handshake messages and wrong byte order in length field of extension handshake request message

It seems that the extension handshake response is sent before the extension handshake request. The flow described here says send the request first and then receive the response. So, in my opinion, the tester code should wait for the extension handshake request before the response. You can clearly see void doExtendedHandshake(const TCPHandler &tcp_handler) that in Commands.cc (of my submission).

Also, the length field in the extension handshake message seems not to be in network order. Please see std::string Message::serialize(bool convert_length_order) in PieceMessages.cc (of my submission).

Command

$  ./your_bittorrent.sh magnet_handshake "magnet:?xt=urn:btih:ad42ce8109f54c99613ce38f9b4d87e70f24a165&dn=magnet1.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce"

Hi @silversword05, thanks for highlighting the issue! We’ll look into it and keep you updated.

@silversword05

  • Length field in the extension handshake message

The tester is using little endian, so message.length = htonl(message.payload.size() + 1) is already converting it to network order:

So applying htonl again will convert it back to little endian:

The tester is using little-endian, so message.length = htonl(message.payload.size() + 1) is already converting it to network order:

Should this be the case? The tester should expect the length in network order right, i.e. big endian.

Sorry never mind, I think I understood my mistake. Thanks for pointing out.

1 Like
  • Order of the extension handshake request/response,

The order shouldn’t matter. Either peer can send the handshake first. Here’s a related explanation that might help clarify.


The issue seems to be caused by clearReadBuffer:

I’m not sure why it’s there, but if I comment it out, and rearrange doExtendedHandshake, your code still works:

Let me know if you’d like further assistance!

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!