Search code examples
responsebittorrenttrackertorrent

http trackers (BitTorrent) compact response peers parsing


I'm trying to create a BitTorrent client in dart and I block on getting peers from a compacted http tracker response.

d8:completei284e10:incompletei3e8:intervali1800e12:min intervali1800e5:peers300:����>���v�[�h��[�B�Z��dr+�N�x���ZR�M%�h���T��DN�R��չ]��'X<��Xy05��N�`���.�mk򭰓qA�[�5�d����՝�Wd�=P�ղ���� ��F��N�C�A�h��%�n��%;7������չ-øNB��㒡�@3U��Ú�]��%�p��չh���h��WA �e�[m��%��DN�0p�6&.���Q�}�������ZY�^уb՘���\��A�N�xu�%;'0�չ�:�X��Z6�R.�m�cs_�����e

I read docs to know how compacted peers list is structured (4 bytes for ip & 2 bytes for port) but I don't know how to read this peers string which is 300 chars long (peers300:) that's logic because their are 50 peers (50 * (4+2)), but their are 557 chars in this string (in bold just below).

[100, 56, 58, 99, 111, 109, 112, 108, 101, 116, 101, 105, 50, 56, 52, 101, 49, 48, 58, 105, 110, 99, 111, 109, 112, 108, 101, 116, 101, 105, 51, 101, 56, 58, 105, 110, 116, 101, 114, 118, 97, 108, 105, 49, 56, 48, 48, 101, 49, 50, 58, 109, 105, 110, 32, 105, 110, 116, 101, 114, 118, 97, 108, 105, 49, 56, 48, 48, 101, 53, 58, 112, 101, 101, 114, 115, 51, 48, 48, 58, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 62, 239, 191, 189, 239, 191, 189, 239, 191, 189, 25, 118, 6, 239, 191, 189, 91, 239, 191, 189, 104, 239, 191, 189, 239, 191, 189, 91, 239, 191, 189, 22, 66, 239, 191, 189, 90, 239, 191, 189, 239, 191, 189, 100, 114, 43, 239, 191, 189, 78, 239, 191, 189, 120, 239, 191, 189, 19, 239, 191, 189, 239, 191, 189, 90, 82, 239, 191, 189, 77, 37, 239, 191, 189, 104, 239, 191, 189, 239, 191, 189, 239, 191, 189, 84, 239, 191, 189, 239, 191, 189, 68, 6, 3, 78, 239, 191, 189, 82, 239, 191, 189, 239, 191, 189, 213, 185, 93, 2, 239, 191, 189, 26, 239, 191, 189, 5, 39, 88, 60, 239, 191, 189, 239, 191, 189, 88, 121, 48, 53, 239, 191, 189, 239, 191, 189, 78, 239, 191, 189, 96, 239, 191, 189, 239, 191, 189, 239, 191, 189, 46, 239, 191, 189, 109, 107, 242, 173, 176, 147, 113, 16, 65, 239, 191, 189, 91, 239, 191, 189, 53, 239, 191, 189, 25, 100, 239, 191, 189, 239, 191, 189, 18, 239, 191, 189, 239, 191, 189, 213, 157, 239, 191, 189, 87, 100, 239, 191, 189, 61, 80, 239, 191, 189, 213, 178, 239, 191, 189, 239, 191, 189, 5, 239, 191, 189, 239, 191, 189, 10, 26, 239, 191, 189, 5, 239, 191, 189, 70, 239, 191, 189, 239, 191, 189, 0, 78, 239, 191, 189, 67, 239, 191, 189, 65, 239, 191, 189, 104, 239, 191, 189, 239, 191, 189, 22, 37, 239, 191, 189, 110, 14, 239, 191, 189, 239, 191, 189, 37, 59, 55, 127, 239, 191, 189, 239, 191, 189, 5, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 213, 185, 45, 195, 184, 78, 66, 239, 191, 189, 239, 191, 189, 227, 146, 161, 239, 191, 189, 64, 20, 51, 85, 239, 191, 189, 239, 191, 189, 195, 154, 239, 191, 189, 93, 239, 191, 189, 239, 191, 189, 37, 239, 191, 189, 112, 239, 191, 189, 239, 191, 189, 213, 185, 104, 239, 191, 189, 5, 239, 191, 189, 239, 191, 189, 104, 239, 191, 189, 239, 191, 189, 82, 8, 87, 65, 10, 239, 191, 189, 101, 239, 191, 189, 91, 109, 239, 191, 189, 8, 239, 191, 189, 239, 191, 189, 37, 239, 191, 189, 17, 14, 239, 191, 189, 68, 78, 239, 191, 189, 48, 112, 22, 239, 191, 189, 54, 38, 46, 239, 191, 189, 239, 191, 189, 239, 191, 189, 81, 239, 191, 189, 125, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 19, 239, 191, 189, 90, 89, 239, 191, 189, 94, 209, 131, 98, 213, 152, 239, 191, 189, 239, 191, 189, 239, 191, 189, 24, 92, 239, 191, 189, 239, 191, 189, 29, 65, 239, 191, 189, 78, 239, 191, 189, 120, 117, 239, 191, 189, 19, 37, 59, 39, 48, 239, 191, 189, 213, 185, 21, 239, 191, 189, 58, 239, 191, 189, 88, 239, 191, 189, 239, 191, 189, 90, 54, 239, 191, 189, 82, 46, 239, 191, 189, 109, 239, 191, 189, 99, 115, 95, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 101]

I found 3 chars many times but I don't know what they represents.

[239, 191, 189]

Thanks for your responses.


Solution

  • There cannot be 557 chars because the bencoding clearly indicates that it is only 300 bytes long.

    The most likely cause of such a mismatch is attempting to interpret binary data as a string type. Bencoding conveys binary data where only some parts are interpretable as strings. The compact response is not one of those cases.

    You need a bdecoder that returns binary data (instead of strings) which you can then slice into 6-byte chunks representing ipv4 addresses and port numbers.

    [239, 191, 189]

    This is the UTF8 representation of the unicode Replacement Character � (U+FFFD). This happens when attempting to decode binary data as utf8 with a decoder that, instead of erroring, inserts those characters when it encounters invalid byte sequences and then encoding the resulting unicode code points as UTF 8 again to display them as an array of numbers.