From 299886ae1910f608320bac6ffb89e5ccc572b388 Mon Sep 17 00:00:00 2001 From: Fiereu Date: Sat, 8 Aug 2026 23:52:10 +0200 Subject: [PATCH] fix(protocol): decode the existing session packet from a real capture (#121) --- .../login/packets/ExistingSessionPacket.kt | 93 +++++++++++++----- .../login/packets/GameServerNodesPacket.kt | 1 + .../net/login/ExistingSessionPacketTest.kt | 36 +++++++ .../login/s2c/26/existing_session_32710.bin | Bin 0 -> 201 bytes 4 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 protocols.login/src/test/kotlin/de/fiereu/openmmo/net/login/ExistingSessionPacketTest.kt create mode 100644 protocols.login/src/test/resources/fixtures/login/s2c/26/existing_session_32710.bin diff --git a/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/ExistingSessionPacket.kt b/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/ExistingSessionPacket.kt index 25597b5..25d91e2 100644 --- a/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/ExistingSessionPacket.kt +++ b/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/ExistingSessionPacket.kt @@ -1,7 +1,10 @@ package de.fiereu.openmmo.net.login.packets -import de.fiereu.bytecodec.Bool +import com.github.maltalex.ineter.base.IPAddress +import com.github.maltalex.ineter.base.IPv4Address +import com.github.maltalex.ineter.base.IPv6Address import de.fiereu.bytecodec.CodecScope +import de.fiereu.bytecodec.MalformedPacketException import de.fiereu.bytecodec.PacketCodec import de.fiereu.bytecodec.S16LE import de.fiereu.bytecodec.S32LE @@ -9,44 +12,90 @@ import de.fiereu.bytecodec.S64LE import de.fiereu.bytecodec.U8 import de.fiereu.bytecodec.Utf16LeNullTerminated import de.fiereu.bytecodec.bytesPrefixed -import de.fiereu.bytecodec.reserved +import de.fiereu.bytecodec.fixedBytes +/** + * Tells a client that the account it just logged in with is still playing, and carries everything + * needed to reach that session: its id and key, and the server hosting it. + */ data class ExistingSessionPacket( val sessionId: Long, val sessionKey: ByteArray, + val gameServerId: UByte, val serverName: String, + val localAddress: IPAddress, + val localHostname: String, + val port: UShort, + /** Five bytes between the port and the node count whose meaning is not established yet. */ + val unknown: ByteArray, + val nodes: List, ) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is ExistingSessionPacket) return false - return sessionId == other.sessionId && - sessionKey.contentEquals(other.sessionKey) && - serverName == other.serverName - } + override fun equals(other: Any?): Boolean = + other is ExistingSessionPacket && + sessionId == other.sessionId && + sessionKey.contentEquals(other.sessionKey) && + gameServerId == other.gameServerId && + serverName == other.serverName && + localAddress == other.localAddress && + localHostname == other.localHostname && + port == other.port && + unknown.contentEquals(other.unknown) && + nodes == other.nodes override fun hashCode(): Int { - var result = sessionId.hashCode() - result = 31 * result + sessionKey.contentHashCode() - result = 31 * result + serverName.hashCode() - return result + var h = sessionId.hashCode() + h = h * 31 + sessionKey.contentHashCode() + h = h * 31 + gameServerId.hashCode() + h = h * 31 + serverName.hashCode() + h = h * 31 + localAddress.hashCode() + h = h * 31 + localHostname.hashCode() + h = h * 31 + port.hashCode() + h = h * 31 + unknown.contentHashCode() + h = h * 31 + nodes.hashCode() + return h } } private val SessionKeyCodec = bytesPrefixed(U8) +private val LocalAddressCodec = bytesPrefixed(U8) +private val UnknownCodec = fixedBytes(5) object ExistingSessionPacketCodec : PacketCodec() { override fun CodecScope.body(): ExistingSessionPacket { val sessionId = field(S64LE) { it.sessionId } val sessionKey = field(SessionKeyCodec) { it.sessionKey } - reserved(byte = 0) + val gameServerId = field(U8) { it.gameServerId.toInt() }.toUByte() val serverName = field(Utf16LeNullTerminated) { it.serverName } - reserved(byte = 0) - field(Utf16LeNullTerminated) { "" } - field(S32LE) { 0 } - field(S16LE) { 0 } - field(S16LE) { 0 } - field(Bool) { false } - field(U8) { 0 } - return ExistingSessionPacket(sessionId, sessionKey, serverName) + val localBytes = field(LocalAddressCodec) { it.localAddress.toBigEndianArray() } + val localAddress = IPAddress.of(localBytes) + val localHostname = field(Utf16LeNullTerminated) { it.localHostname } + val port = field(S32LE) { it.port.toInt() }.toUShort() + val unknown = field(UnknownCodec) { it.unknown } + + val nodeCount = field(U8) { it.nodes.size } + val nodes = ArrayList(nodeCount) + repeat(nodeCount) { i -> + val id = field(U8) { it.nodes[i].id.toInt() }.toUByte() + val ipv4 = + field(TaggedIpCodec) { it.nodes[i].iPv4Address } as? IPv4Address + ?: throw MalformedPacketException("Expected IPv4 address for game server node") + val ipv6 = + field(TaggedIpCodec) { it.nodes[i].iPv6Address } as? IPv6Address + ?: throw MalformedPacketException("Expected IPv6 address for game server node") + val nodePort = field(S16LE) { it.nodes[i].port.toShort() }.toUShort() + val weight = field(U8) { it.nodes[i].weight.toInt() }.toUByte() + nodes += GameServerNode(ipv4, ipv6, nodePort, weight, id) + } + return ExistingSessionPacket( + sessionId, + sessionKey, + gameServerId, + serverName, + localAddress, + localHostname, + port, + unknown, + nodes, + ) } } diff --git a/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/GameServerNodesPacket.kt b/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/GameServerNodesPacket.kt index b8aac58..dbf7a62 100644 --- a/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/GameServerNodesPacket.kt +++ b/protocols.login/src/main/kotlin/de/fiereu/openmmo/net/login/packets/GameServerNodesPacket.kt @@ -18,6 +18,7 @@ data class GameServerNode( val iPv6Address: IPv6Address, val port: UShort = 7777u, val weight: UByte, + val id: UByte = 0u, ) data class GameServerData( diff --git a/protocols.login/src/test/kotlin/de/fiereu/openmmo/net/login/ExistingSessionPacketTest.kt b/protocols.login/src/test/kotlin/de/fiereu/openmmo/net/login/ExistingSessionPacketTest.kt new file mode 100644 index 0000000..c8c1d09 --- /dev/null +++ b/protocols.login/src/test/kotlin/de/fiereu/openmmo/net/login/ExistingSessionPacketTest.kt @@ -0,0 +1,36 @@ +package de.fiereu.openmmo.net.login + +import de.fiereu.openmmo.common.test.assertBytesRoundtrip +import de.fiereu.openmmo.common.test.decodeBytes +import de.fiereu.openmmo.common.test.fixture +import de.fiereu.openmmo.net.login.packets.ExistingSessionPacketCodec +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +class ExistingSessionPacketTest : + FunSpec({ + val capture = "login/s2c/26/existing_session_32710.bin" + + test("round-trips the captured payload byte for byte") { + ExistingSessionPacketCodec.assertBytesRoundtrip(fixture(capture)) + } + + test("decodes the session and the server hosting it") { + val packet = ExistingSessionPacketCodec.decodeBytes(fixture(capture)) + + packet.sessionKey.size shouldBe 16 + packet.gameServerId shouldBe 1u.toUByte() + packet.serverName shouldBe "Live" + packet.localAddress.toString() shouldBe "127.0.0.1" + packet.localHostname shouldBe "localhost" + packet.port shouldBe 7777u.toUShort() + } + + test("decodes every node, whose ids are assigned rather than sequential") { + val packet = ExistingSessionPacketCodec.decodeBytes(fixture(capture)) + + packet.nodes.size shouldBe 5 + packet.nodes.map { it.id.toInt() } shouldBe listOf(1, 2, 5, 9, 15) + packet.nodes.forEach { it.port shouldBe 7777u.toUShort() } + } + }) diff --git a/protocols.login/src/test/resources/fixtures/login/s2c/26/existing_session_32710.bin b/protocols.login/src/test/resources/fixtures/login/s2c/26/existing_session_32710.bin new file mode 100644 index 0000000000000000000000000000000000000000..d4423334f913dd7965a9e964429e36b84900d730 GIT binary patch literal 201 zcmZRW&~Zd2j8j73ulwXj29a7@KKS_Y|DWpfgVBc}lc9_um4ShQrJjL-F^3_aA(