Skip to content

Commit 75febc0

Browse files
committed
🐛 Fix Package Null padding
Thanks @ExusAltimus Partial-Fix: #56 Discovered-in: #57
1 parent 8791b01 commit 75febc0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/CoreRCON/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ internal class Constants
1818
/// </summary>
1919
internal const int PACKET_HEADER_SIZE = 12;
2020

21+
/// <summary>
22+
/// The size of the padding of an RCON packet.
23+
/// The packet is padded with two null bytes.
24+
/// One to terminate the string and one to terminate the packet.
25+
/// </summary>
26+
internal const int PACKET_PADDING_SIZE = 2;
27+
2128
/// <summary>
2229
/// Special response value when you send a Response.Response to the server.
2330
/// Used to finde the end of a multi packet response.

src/CoreRCON/PacketFormats/RCONPacket.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ internal static RCONPacket FromBytes(byte[] buffer)
6868
/// <returns>Byte array with each field.</returns>
6969
internal byte[] ToBytes()
7070
{
71-
int bodyLength = Encoding.UTF8.GetByteCount(Body) + 1;
72-
int totalLength = Constants.PACKET_HEADER_SIZE + bodyLength;
71+
int bodyLength = Encoding.UTF8.GetByteCount(Body);
72+
int totalLength = Constants.PACKET_HEADER_SIZE + bodyLength + Constants.PACKET_PADDING_SIZE;
7373
byte[] packetBytes = new byte[totalLength];
7474
Span<byte> packetSpan = packetBytes;
7575

@@ -88,7 +88,7 @@ internal byte[] ToBytes()
8888

8989
// Write body
9090
Encoding.UTF8.GetBytes(Body, 0, Body.Length, packetBytes, Constants.PACKET_HEADER_SIZE);
91-
packetSpan[bodyLength - 1] = 0; // Null terminator for the body
91+
packetSpan[bodyLength] = 0; // Null terminator for the body
9292
packetBytes[totalLength - 1] = 0; // Null terminator for the package
9393

9494
return packetBytes;

0 commit comments

Comments
 (0)