From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Tue, 27 Nov 2018 21:18:06 -0500 Subject: [PATCH] Handle Large Packets disconnecting client If a players inventory is too big to send in a single packet, split the inventory set into multiple packets instead. diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java index 0aacf2e0155a9a7c9dfe9b368a251aee25ed24fc..0b3f1585618e29685ffcba704f03ff5e581f0851 100644 --- a/src/main/java/net/minecraft/network/Connection.java +++ b/src/main/java/net/minecraft/network/Connection.java @@ -120,6 +120,15 @@ public class Connection extends SimpleChannelInboundHandler> { } public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) { + // Paper start + if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException) { + if (((PacketEncoder.PacketTooLargeException) throwable.getCause()).getPacket().packetTooLarge(this)) { + return; + } else { + throwable = throwable.getCause(); + } + } + // Paper end if (throwable instanceof SkipPacketException) { Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause()); } else { diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java index dfeff0da932b0d66b70f3a5dd281c926c048f2f0..9cdcd4d92f73d1b9866cdef33d2463e843462516 100644 --- a/src/main/java/net/minecraft/network/PacketEncoder.java +++ b/src/main/java/net/minecraft/network/PacketEncoder.java @@ -57,7 +57,31 @@ public class PacketEncoder extends MessageToByteEncoder> { throw var10; } } + + // Paper start + int packetLength = friendlyByteBuf.readableBytes(); + if (packetLength > MAX_PACKET_SIZE) { + throw new PacketTooLargeException(packet, packetLength); + } + // Paper end } } } + + // Paper start + private static int MAX_PACKET_SIZE = 2097152; + + public static class PacketTooLargeException extends RuntimeException { + private final Packet packet; + + PacketTooLargeException(Packet packet, int packetLength) { + super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE); + this.packet = packet; + } + + public Packet getPacket() { + return packet; + } + } + // Paper end } diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java index 10c1f2d8a92f848c3f2be9d1d06fd254978e6dcc..74bfe0d3942259c45702b099efdc4e101a4e3022 100644 --- a/src/main/java/net/minecraft/network/protocol/Packet.java +++ b/src/main/java/net/minecraft/network/protocol/Packet.java @@ -8,6 +8,12 @@ public interface Packet { void handle(T listener); + // Paper start + default boolean packetTooLarge(net.minecraft.network.Connection manager) { + return false; + } + // Paper end + default boolean isSkippable() { return false; } diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java index 0e076173033278587df2b5dfbd01cc9005651eb5..dbd8b9b09b82c1b75e8be9dc7416d9f0863c8c87 100644 --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java @@ -31,6 +31,16 @@ public class ClientboundContainerSetContentPacket implements Packet 2097152) { + if (i > 2097152) { // Paper - diff on change - if this changes, update PacketEncoder throw new RuntimeException("Chunk Packet trying to allocate too much memory on read."); } else { this.buffer = new byte[i];