From 458b0620b43116c943549a0f060c7e8830c2d77a Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 20 Jan 2014 17:29:17 -0500 Subject: [PATCH] Log the bad packet in a prettier way. Also, don't show the authlib debug data in the log file. --- .../mods/fml/common/network/ByteBufUtils.java | 70 +++++++++++++++++++ .../network/internal/FMLRuntimeCodec.java | 10 +-- src/main/resources/log4j2.xml | 5 ++ 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/main/java/cpw/mods/fml/common/network/ByteBufUtils.java b/src/main/java/cpw/mods/fml/common/network/ByteBufUtils.java index 0716cc56db..d8904b9d97 100644 --- a/src/main/java/cpw/mods/fml/common/network/ByteBufUtils.java +++ b/src/main/java/cpw/mods/fml/common/network/ByteBufUtils.java @@ -179,4 +179,74 @@ public class ByteBufUtils { throw Throwables.propagate(e); } } + + public static String getContentDump(ByteBuf buffer) + { + int currentLength = buffer.readableBytes(); + StringBuffer returnString = new StringBuffer((currentLength * 3) + // The + // hex + (currentLength) + // The ascii + (currentLength / 4) + // The tabs/\n's + 30); // The text + + // returnString.append("Buffer contents:\n"); + int i, j; // Loop variables + for (i = 0; i < currentLength; i++) + { + if ((i != 0) && (i % 16 == 0)) + { + // If it's a multiple of 16 and i isn't null, show the ascii + returnString.append('\t'); + for (j = i - 16; j < i; j++) + { + if (buffer.getByte(j) < 0x20 || buffer.getByte(j) > 0x7F) + returnString.append('.'); + else + returnString.append((char) buffer.getByte(j)); + } + // Add a linefeed after the string + returnString.append("\n"); + } + + returnString.append(Integer.toString((buffer.getByte(i) & 0xF0) >> 4, 16) + Integer.toString((buffer.getByte(i) & 0x0F) >> 0, 16)); + returnString.append(' '); + } + + // Add padding spaces if it's not a multiple of 16 + if (i != 0 && i % 16 != 0) + { + for (j = 0; j < ((16 - (i % 16)) * 3); j++) + { + returnString.append(' '); + } + } + // Add the tab for alignment + returnString.append('\t'); + + // Add final chararacters at right, after padding + + // If it was at the end of a line, print out the full line + if (i > 0 && (i % 16) == 0) + { + j = i - 16; + } else + { + j = (i - (i % 16)); + } + + for (; i >= 0 && j < i; j++) + { + if (buffer.getByte(j) < 0x20 || buffer.getByte(j) > 0x7F) + returnString.append('.'); + else + returnString.append((char) buffer.getByte(j)); + } + + // Finally, tidy it all up with a newline + returnString.append('\n'); + returnString.append("Length: " + currentLength); + + return returnString.toString(); + + } } diff --git a/src/main/java/cpw/mods/fml/common/network/internal/FMLRuntimeCodec.java b/src/main/java/cpw/mods/fml/common/network/internal/FMLRuntimeCodec.java index d1f6ba99cc..5cdf7a33bb 100644 --- a/src/main/java/cpw/mods/fml/common/network/internal/FMLRuntimeCodec.java +++ b/src/main/java/cpw/mods/fml/common/network/internal/FMLRuntimeCodec.java @@ -1,9 +1,10 @@ package cpw.mods.fml.common.network.internal; +import com.google.common.base.Splitter; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import com.google.common.primitives.Bytes; import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec; import cpw.mods.fml.common.network.FMLNetworkException; @@ -33,9 +34,10 @@ public class FMLRuntimeCodec extends FMLIndexedMessageToMessageCodec if (msg.payload().getByte(0) == 0 && msg.payload().readableBytes() > 2) { FMLLog.severe("The connection appears to have sent an invalid FML packet of type 0, this is likely because it think's it's talking to 1.6.4 FML"); - byte[] badData = new byte[msg.payload().readableBytes()]; - msg.payload().getBytes(0, badData); - FMLLog.info("Bad data : %s",Bytes.asList(badData)); + FMLLog.info("Bad data :"); + for (String l : Splitter.on('\n').split(ByteBufUtils.getContentDump(msg.payload()))) { + FMLLog.info("\t%s",l); + } throw new FMLNetworkException("Invalid FML packet"); } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 5ce95d0927..49d1c53676 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -27,6 +27,11 @@ + + + + +