Send DHT queries to “router.bittorrent.com” response garbled text

前端 未结 2 1411
旧时难觅i
旧时难觅i 2021-01-23 07:28

I read the DHT Protocol in bep_0005 page.

But when I send a ping query or a find_node query, the server response a garbled text (both of router.bittorrent.com:6881

相关标签:
2条回答
  • 2021-01-23 08:11

    the server response a garbled text

    No, the response is bencoded and contains raw binary data.
    It CAN NOT be treated as text.

    In BEP5, to make the raw binary node_id in the examples printable, it has cleverly been chosen to consist of only alphanumeric characters.
    See:
    Bittorrent KRPC - Why are node ID's half the size of an info_hash and use every character a-z?

    The ip key is a extension explained in: BEP42 - DHT Security extension

    The received response is fully valid.

    TODO: Working Java code
    
    0 讨论(0)
  • 2021-01-23 08:14

    Map<String, Object> dict = bencode.decode(result, Type.DICTIONARY);

    This gives you the decoded root dictionary of the message as Map. Within that you should find the r dictionary as another map and with in that map the id value. What type the id has will depend on the bedecoding library you are using.

    If it is ByteBuffer or byte[] then you should have 20 bytes that you can hexencode (to 40 characters) if you need it to be human-readable. The DHT protocol deals in raw hashes, not hex values.

    If it is a String then you will have to convert the string back into byte[] before hex-encoding it. That is only possible when the bdecoder used ISO 8859-1 to decode because that charset is roundtrip-safe while utf-8 is not for arbitrary byte sequences.

    0 讨论(0)
提交回复
热议问题