the8472 / mldht

Bittorrent Mainline DHT implementation in java
Mozilla Public License 2.0
147 stars 45 forks source link

failed to decode message #25

Closed sophieperrineau closed 5 years ago

sophieperrineau commented 5 years ago

Hello, I'm trying to use the project, and i'm concerned about the errors that occurs on many exchanges. Could you help me to understand why i've got these errors ? Are the message corrupted or is there an issue in th code ?

failed to decode message d1:e00 00 00 00 00 00 00 00 00 00 00 00 00 00 03A4¤CBËd (length:23) from: /201.92.184.213:19908 torrentDHT.log

I changed the logger in order to log the message in an other format :

public static String stripToAscii(ByteBuffer buf) {
                int length = buf.remaining();               
        StringBuilder out = new StringBuilder();
        for(int i=0;i<length ;i++) {
            char b = (char)(buf.get(buf.position() + i) & 0xff);
            if(b < ' ' || b > '~')
                appendByte(buf.get(buf.position() + i), out);
            out.append(b);
        }
        return new String(out);
}
    protected static void appendByte(byte toHex, StringBuilder builder) {
        int nibble = (toHex & 0xF0) >> 4;
        builder.append((char)(nibble < 0x0A ? '0'+nibble : 'A'+nibble-10 ));
        nibble = toHex & 0x0F;
        builder.append((char)(nibble < 0x0A ? '0'+nibble : 'A'+nibble-10 ));
    }

Sophie

the8472 commented 5 years ago

Note that this message is only emitted on INFO level, not WARN or ERROR.

It doesn't look like a valid bencoded message to me, so the error is correct. The last line of the log looks like an overlong message that got truncated, maybe because the sender didn't check whether it fits into their send buffer or something like that.

It's a public network with many implementations, malicious actors and unreliable networks, so all kinds of funky things can happen, this isn't unexpected as long as it doesn't happen to the majority of messages.