fix(protocol): decode the existing session packet from a real capture (#121)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-08 23:52:10 +02:00
1 parent a1453b8db2
commit 299886ae19
4 files changed
+108 -22

No files matched your search

@@ -1,7 +1,10 @@
package de.fiereu.openmmo.net.login.packets 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.CodecScope
import de.fiereu.bytecodec.MalformedPacketException
import de.fiereu.bytecodec.PacketCodec import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE import de.fiereu.bytecodec.S32LE
@@ -9,44 +12,90 @@ import de.fiereu.bytecodec.S64LE
import de.fiereu.bytecodec.U8 import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.Utf16LeNullTerminated import de.fiereu.bytecodec.Utf16LeNullTerminated
import de.fiereu.bytecodec.bytesPrefixed 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( data class ExistingSessionPacket(
val sessionId: Long, val sessionId: Long,
val sessionKey: ByteArray, val sessionKey: ByteArray,
val gameServerId: UByte,
val serverName: String, 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<GameServerNode>,
) { ) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean =
if (this === other) return true other is ExistingSessionPacket &&
if (other !is ExistingSessionPacket) return false sessionId == other.sessionId &&
return sessionId == other.sessionId && sessionKey.contentEquals(other.sessionKey) &&
sessionKey.contentEquals(other.sessionKey) && gameServerId == other.gameServerId &&
serverName == other.serverName serverName == other.serverName &&
} localAddress == other.localAddress &&
localHostname == other.localHostname &&
port == other.port &&
unknown.contentEquals(other.unknown) &&
nodes == other.nodes
override fun hashCode(): Int { override fun hashCode(): Int {
var result = sessionId.hashCode() var h = sessionId.hashCode()
result = 31 * result + sessionKey.contentHashCode() h = h * 31 + sessionKey.contentHashCode()
result = 31 * result + serverName.hashCode() h = h * 31 + gameServerId.hashCode()
return result 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 SessionKeyCodec = bytesPrefixed(U8)
private val LocalAddressCodec = bytesPrefixed(U8)
private val UnknownCodec = fixedBytes(5)
object ExistingSessionPacketCodec : PacketCodec<ExistingSessionPacket>() { object ExistingSessionPacketCodec : PacketCodec<ExistingSessionPacket>() {
override fun CodecScope<ExistingSessionPacket>.body(): ExistingSessionPacket { override fun CodecScope<ExistingSessionPacket>.body(): ExistingSessionPacket {
val sessionId = field(S64LE) { it.sessionId } val sessionId = field(S64LE) { it.sessionId }
val sessionKey = field(SessionKeyCodec) { it.sessionKey } val sessionKey = field(SessionKeyCodec) { it.sessionKey }
reserved(byte = 0) val gameServerId = field(U8) { it.gameServerId.toInt() }.toUByte()
val serverName = field(Utf16LeNullTerminated) { it.serverName } val serverName = field(Utf16LeNullTerminated) { it.serverName }
reserved(byte = 0) val localBytes = field(LocalAddressCodec) { it.localAddress.toBigEndianArray() }
field(Utf16LeNullTerminated) { "" } val localAddress = IPAddress.of(localBytes)
field(S32LE) { 0 } val localHostname = field(Utf16LeNullTerminated) { it.localHostname }
field(S16LE) { 0 } val port = field(S32LE) { it.port.toInt() }.toUShort()
field(S16LE) { 0 } val unknown = field(UnknownCodec) { it.unknown }
field(Bool) { false }
field(U8) { 0 } val nodeCount = field(U8) { it.nodes.size }
return ExistingSessionPacket(sessionId, sessionKey, serverName) val nodes = ArrayList<GameServerNode>(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,
)
} }
} }
@@ -18,6 +18,7 @@ data class GameServerNode(
val iPv6Address: IPv6Address, val iPv6Address: IPv6Address,
val port: UShort = 7777u, val port: UShort = 7777u,
val weight: UByte, val weight: UByte,
val id: UByte = 0u,
) )
data class GameServerData( data class GameServerData(
@@ -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() }
}
})