Ray_Write
Ray_Write

Reputation: 131

Bittorrent Extension Protocol

I am trying to write my own bittorrent client in java, it is a make work project to refresh my java skills. I have been stuck at a certain point in development and although there are forums out there for bittorrent development they don't appear to be very active.

So far I can do the following:

  1. Get a list of peers from Tracker sites
  2. Connect to a peer
  3. Send the 'Handshake' message
  4. Receive the 'Handshake' message back
  5. Send the 'Interested' message.
  6. Receive the 'Unchoke' message back.

The problem I am having is if I try sending extended data (http://www.bittorrent.org/beps/bep_0010.html) to the peer prior to sending the 'Interested' message. The protocol states you must send extended data immediately after receiving the handshake. If I send the extended data to the peer the peer immediately closes the connection, ie, in Wireshark I see a FIN,ACK. I have used Wireshark and uTorrent to see what extended data is being sent and I have tried to copy that data, but to no avail. I have tried all different combinations of extended data parameters but no matter what I do I cannot get past this point.

Here is an example of extended data message that works, copied from Wireshark.

d1:ei0e4:ipv44:czH12:complete_agoi-1e1:md11:upload_onlyi3e11:lt_donthavei7e12:ut_holepunchi4e11:ut_metadatai2e6:ut_pexi1e10:ut_commenti6ee1:pi43276e4:reqqi255e1:v13:Torrent 3.52:ypi48680e6:yourip4:jFe

And here is extended data from my client that doesn't work. btw, I have checked to make sure the ip variables are correct (they are sent as 4 bytes).

d1:ei0e4:ipv44:czH12:complete_agoi-1e1:md11:upload_onlyi3e11:lt_donthavei7e12:ut_holepunchi4e11:ut_metadatai2e6:ut_pexi1e10:ut_commenti6ee1:pi51361e4:reqqi255e1:v15:MyTorrent v0.152:ypi40898e6:yourip4:RO e

Also, once I receive the 'Unchoke' message I am able to successfully retrieve pieces.

Any information would be appreciated.

Upvotes: 2

Views: 761

Answers (2)

Encombe
Encombe

Reputation: 2090

The captured TCP stream from the link Ray_Write posted:

00000000  13 42 69 74 54 6f 72 72  65 6e 74 20 70 72 6f 74 .BitTorr ent prot
00000010  6f 63 6f 6c 00 00 00 00  00 10 00 00 14 82 9a a1 ocol.... ........
00000020  b5 7d 5a d7 3c b5 c9 d6  60 83 c8 99 8f f6 aa cb .}Z.<... `.......
00000030  2d 4d 59 31 32 33 34 2d  31 32 33 34 35 36 37 38 -MY1234- 12345678
00000040  39 30 31 32                                      9012
    00000000  13 42 69 74 54 6f 72 72  65 6e 74 20 70 72 6f 74 .BitTorr ent prot
    00000010  6f 63 6f 6c 00 00 00 00  00 10 00 05 14 82 9a a1 ocol.... ........
    00000020  b5 7d 5a d7 3c b5 c9 d6  60 83 c8 99 8f f6 aa cb .}Z.<... `.......
    00000030  2d 55 54 33 33 30 42 2d  06 77 26 8b 01 b2 4d 0e -UT330B- .w&...M.
    00000040  69 ab d2 3b 00 00 00 eb  14 00 64 31 3a 65 69 30 i..;.... ..d1:ei0
    00000050  65 34 3a 69 70 76 34 34  3a b2 21 7b 3d 31 32 3a e4:ipv44 :.!{=12:
    00000060  63                                               c
00000044  64 31 3a 65 69 30 65 34  3a 69 70 76 34 34 3a 79 d1:ei0e4 :ipv44:y
00000054  98 b9 fa 31 32 3a 63 6f  6d 70 6c 65 74 65 5f 61 ...12:co mplete_a
00000064  67 6f 69 2d 31 65 31 3a  6d 64 31 31 3a 75 70 6c goi-1e1: md11:upl
00000074  6f 61 64 5f 6f 6e 6c 79  69 33 65 31 31 3a 6c 74 oad_only i3e11:lt
00000084  5f 64 6f 6e 74 68 61 76  65 69 37 65 31 32 3a 75 _donthav ei7e12:u
00000094  74 5f 68 6f 6c 65 70 75  6e 63 68 69 34 65 31 31 t_holepu nchi4e11
000000A4  3a 75 74 5f 6d 65 74 61  64 61 74 61 69 32 65 36 :ut_meta datai2e6
000000B4  3a 75 74 5f 70 65 78 69  31 65 31 30 3a 75 74 5f :ut_pexi 1e10:ut_
000000C4  63 6f 6d 6d 65 6e 74 69  36 65 65 31 3a 70 69 35 commenti 6ee1:pi5
000000D4  31 39 30 32 65 34 3a 72  65 71 71 69 32 35 35 65 1902e4:r eqqi255e
000000E4  31 3a 76 31 33 3a 76 31  35 3a 4d 79 54 6f 72 72 1:v13:v1 5:MyTorr
000000F4  65 6e 74 20 76 30 2e 31  35 32 3a 79 70 69 32 30 ent v0.1 52:ypi20
00000104  37 30 34 65 36 3a 79 6f  75 72 69 70 34 3a b2 21 704e6:yo urip4:.!
00000114  7b 3d 65                                         {=e
    00000061  6f 6d 70 6c 65 74 65 5f  61 67 6f 69 31 32 65 31 omplete_ agoi12e1
    00000071  3a 6d 64 31 31 3a 75 70  6c 6f 61 64 5f 6f 6e 6c :md11:up load_onl
    00000081  79 69 33 65 31 32 3a 75  74 5f 68 6f 6c 65 70 75 yi3e12:u t_holepu
    00000091  6e 63 68 69 34 65 31 31  3a 75 74 5f 6d 65 74 61 nchi4e11 :ut_meta
    000000A1  64 61 74 61 69 32 65 36  3a 75 74 5f 70 65 78 69 datai2e6 :ut_pexi
    000000B1  31 65 31 32 3a 75 74 5f  72 65 63 6f 6d 6d 65 6e 1e12:ut_ recommen
    000000C1  64 69 35 65 31 30 3a 75  74 5f 63 6f 6d 6d 65 6e di5e10:u t_commen
    000000D1  74 69 36 65 31 31 3a 6c  74 5f 64 6f 6e 74 68 61 ti6e11:l t_dontha
    000000E1  76 65 69 37 65 65 31 33  3a 6d 65 74 61 64 61 74 vei7ee13 :metadat
    000000F1  61 5f 73 69 7a 65 69 32  36 37 38 65 31 3a 70 69 a_sizei2 678e1:pi
    00000101  32 30 37 30 34 65 34 3a  72 65 71 71 69 32 35 35 20704e4: reqqi255
    00000111  65 31 3a 76 31 32 3a 75  54 6f 72 72 65 6e 74 20 e1:v12:u Torrent 
    00000121  33 2e 33 36 3a 79 6f 75  72 69 70 34 3a 79 98 b9 3.36:you rip4:y..
    00000131  fa 65 00 00 00 11 05 ff  ff fa ee f7 af ff ef bf .e...... ........
    00000141  de ee f7 ec 5e 4f a0 00  00 00 05 04 00 00 00 29 ....^O.. .......)
    00000151  00 00 00 05 04 00 00 00  53 00 00 00 05 04 00 00 ........ S.......
    00000161  00 6a 00 00 00 05 04 00  00 00 17 00 00 00 05 04 .j...... ........
    00000171  00 00 00 2b 00 00 00 05  04 00 00 00 1f 00 00 00 ...+.... ........
    00000181  05 04 00 00 00 73 00 00  00 05 04 00 00 00 24 00 .....s.. ......$.
    00000191  00 00 05 04 00 00 00 41  00 00 00 05 04 00 00 00 .......A ........
    000001A1  1b 00 00 00 05 04 00 00  00 4f 00 00 00 05 04 00 ........ .O......
    000001B1  00 00 66 00 00 00 05 04  00 00 00 5c 00 00 00 05 ..f..... ...\....
    000001C1  04 00 00 00 57 00 00 00  05 04 00 00 00 79 00 00 ....W... .....y..
    000001D1  00 05 04 00 00 00 4a 00  00 00 05 04 00 00 00 3b ......J. .......;
    000001E1  00 00 00 05 04 00 00 00  72 00 00 00 05 04 00 00 ........ r.......
    000001F1  00 68 00 00 00 05 04 00  00 00 15 00 00 00 05 04 .h...... ........
    00000201  00 00 00 63 00 00 00 05  04 00 00 00 6f 00 00 00 ...c.... ....o...
    00000211  05 04 00 00 00 70 00 00  00 05 04 00 00 00 67    .....p.. ......g
00000117  00 00 00 01 02  

The main problem is on this line:

...
00000044  64 31 3a 65 69 30 65 34  3a 69 70 76 34 34 3a 79 d1:ei0e4 :ipv44:y
...

The 4 bytes length prefix, 1 byte bittorrent message ID and 1 byte extended message ID is missing from the Extension Protocol handshake. It should have been:

...
00000044  00 00 00 d5 14 00 64 31  3a 65 69 30 65 34 3a 69 ......d1:ei0e4 :i
...

Further there is also an issue here:

...
000000E4  31 3a 76 31 33 3a 76 31  35 3a 4d 79 54 6f 72 72 1:v13:v1 5:MyTorr
000000F4  65 6e 74 20 76 30 2e 31  35 32 3a 79 70 69 32 30 ent v0.1 52:ypi20
...

The value for the 'v'-key has two string length indexes.
It is: 1:v13:v15:MyTorrent v0.15
It should be: 1:v15:MyTorrent v0.15

A minor issue is also that in both clients Extension Protocol handshake, the root and the 'm' dictionary is unsorted. They should be sorted, but most clients accept unsorted dicts because this issue is so common.

I also suspect that the µTorrent client is fake, because a real client uses a 'µ' in the version string, not a 'u'.

Upvotes: 3

the8472
the8472

Reputation: 43052

Based on your wireshark capture you are only sending the extended handshake payload but not the necessary extension protocol or bittorrent message headers.

Upvotes: 0

Related Questions