fix(protocol): decode the existing session packet from a real capture (#121)
This commit is contained in:
1 parent
a1453b8db2
commit
299886ae19
4 files changed
+107
-21
No files matched your search
+70
-21
@@ -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<GameServerNode>,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is ExistingSessionPacket) return false
|
||||
return sessionId == other.sessionId &&
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is ExistingSessionPacket &&
|
||||
sessionId == other.sessionId &&
|
||||
sessionKey.contentEquals(other.sessionKey) &&
|
||||
serverName == other.serverName
|
||||
}
|
||||
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<ExistingSessionPacket>() {
|
||||
override fun CodecScope<ExistingSessionPacket>.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<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,
|
||||
)
|
||||
}
|
||||
}
|
||||
+1
@@ -18,6 +18,7 @@ data class GameServerNode(
|
||||
val iPv6Address: IPv6Address,
|
||||
val port: UShort = 7777u,
|
||||
val weight: UByte,
|
||||
val id: UByte = 0u,
|
||||
)
|
||||
|
||||
data class GameServerData(
|
||||
|
||||
+36
@@ -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() }
|
||||
}
|
||||
})
|
||||
Binary file not shown.
Reference in new issue
Block a user