feat(quic): Phase L — wire QuicWebTransportFactory into nestsClient

Replace the Kwik stub in :nestsClient with a pure-Kotlin QUIC + WebTransport
adapter built on top of :quic.

- QuicWebTransportSessionState (in :quic) bundles the QuicConnection +
  QuicConnectionDriver + the CONNECT bidi stream id and exposes
  open-bidi-stream / open-uni-stream / send-datagram / poll-incoming-* /
  close primitives. Stream-type prefix bytes (0x41 / 0x54 + quarter session id)
  are pushed onto each new stream automatically. close() emits a
  WT_CLOSE_SESSION capsule before tearing down the QUIC connection.
- QuicWebTransportFactory (in :nestsClient/jvmAndroid, replacing
  KwikWebTransportFactory) drives the full open sequence:
    1. UDP connect + QuicConnection.start
    2. wait until handshake completes (Status.CONNECTED)
    3. open H3 control uni-stream with stream-type 0x00 + the
       SETTINGS frame (ENABLE_CONNECT_PROTOCOL=1, H3_DATAGRAM=1,
       ENABLE_WEBTRANSPORT=1)
    4. open the Extended CONNECT bidi: HEADERS frame carrying
       :method=CONNECT, :protocol=webtransport, :scheme=https,
       :authority, :path, optional Authorization: Bearer
    5. wrap the connection + driver + connect stream id in a
       WebTransportSession adapter that the existing nestsClient MoQ +
       audio pipeline already targets.
- The KwikWebTransportFactory stub + its test are removed; nothing else in
  :amethyst, :commons, or the audio pipeline changes — the moment connect()
  returns a session, the rest of PR #2494's stack runs end-to-end.
- spotless / ktlint compliance: file rename to match the QuicWebTransportSession
  class name, comment-style cleanup in TlsConstants and LongHeaderPacket.

Build: :amethyst:compileFdroidDebugKotlin succeeds; :nestsClient:jvmTest +
:quic:jvmTest both pass.

https://claude.ai/code/session_01EC1tfXfap8k8GyKvrxkxZx
This commit is contained in:
Claude
2026-04-25 21:03:54 +00:00
parent cceb5bfe96
commit 46742636c7
24 changed files with 692 additions and 353 deletions
@@ -32,9 +32,10 @@ class PacketProtection(
)
/** All four encryption levels we ever see in a QUIC client connection. */
enum class EncryptionLevel(val space: PacketNumberSpace) {
enum class EncryptionLevel(
val space: PacketNumberSpace,
) {
INITIAL(PacketNumberSpace.INITIAL),
HANDSHAKE(PacketNumberSpace.HANDSHAKE),
APPLICATION(PacketNumberSpace.APPLICATION),
;
}
@@ -26,7 +26,9 @@ import com.vitorpamplona.quic.stream.SendBuffer
/** Per-encryption-level state owned by [QuicConnection]. */
class LevelState {
val pnSpace = PacketNumberSpaceState()
val ackTracker = com.vitorpamplona.quic.recovery.AckTracker()
val ackTracker =
com.vitorpamplona.quic.recovery
.AckTracker()
val cryptoSend = SendBuffer()
val cryptoReceive = ReceiveBuffer()
var sendProtection: PacketProtection? = null
@@ -42,7 +42,7 @@ fun packetProtectionFromSecret(
): PacketProtection {
val (aead, keyLen, ivLen, hpLen, hp) =
when (cipherSuite) {
TlsConstants.CIPHER_TLS_AES_128_GCM_SHA256 ->
TlsConstants.CIPHER_TLS_AES_128_GCM_SHA256 -> {
ProtectionParams(
aead = Aes128Gcm,
keyLen = 16,
@@ -50,15 +50,23 @@ fun packetProtectionFromSecret(
hpLen = 16,
hp = AesEcbHeaderProtection(PlatformAesOneBlock),
)
TlsConstants.CIPHER_TLS_CHACHA20_POLY1305_SHA256 ->
}
TlsConstants.CIPHER_TLS_CHACHA20_POLY1305_SHA256 -> {
ProtectionParams(
aead = com.vitorpamplona.quic.crypto.ChaCha20Poly1305Aead,
keyLen = 32,
ivLen = 12,
hpLen = 32,
hp = com.vitorpamplona.quic.crypto.ChaCha20HeaderProtection(com.vitorpamplona.quic.crypto.PlatformChaCha20Block),
hp =
com.vitorpamplona.quic.crypto
.ChaCha20HeaderProtection(com.vitorpamplona.quic.crypto.PlatformChaCha20Block),
)
else -> error("unsupported cipher suite 0x${cipherSuite.toString(16)}")
}
else -> {
error("unsupported cipher suite 0x${cipherSuite.toString(16)}")
}
}
val keys = deriveQuicKeys(secret, keyLen, ivLen, hpLen)
return PacketProtection(aead, keys.key, keys.iv, hp, keys.hp)
@@ -20,25 +20,10 @@
*/
package com.vitorpamplona.quic.connection
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.crypto.Aes128Gcm
import com.vitorpamplona.quic.crypto.AesEcbHeaderProtection
import com.vitorpamplona.quic.crypto.InitialSecrets
import com.vitorpamplona.quic.crypto.PlatformAesOneBlock
import com.vitorpamplona.quic.frame.ConnectionCloseFrame
import com.vitorpamplona.quic.frame.CryptoFrame
import com.vitorpamplona.quic.frame.DatagramFrame
import com.vitorpamplona.quic.frame.Frame
import com.vitorpamplona.quic.frame.PingFrame
import com.vitorpamplona.quic.frame.StreamFrame
import com.vitorpamplona.quic.frame.decodeFrames
import com.vitorpamplona.quic.frame.encodeFrames
import com.vitorpamplona.quic.packet.LongHeaderPacket
import com.vitorpamplona.quic.packet.LongHeaderPlaintextPacket
import com.vitorpamplona.quic.packet.LongHeaderType
import com.vitorpamplona.quic.packet.QuicVersion
import com.vitorpamplona.quic.packet.ShortHeaderPacket
import com.vitorpamplona.quic.packet.ShortHeaderPlaintextPacket
import com.vitorpamplona.quic.stream.QuicStream
import com.vitorpamplona.quic.stream.StreamId
import com.vitorpamplona.quic.tls.TlsClient
@@ -71,7 +56,11 @@ class QuicConnection(
val serverName: String,
val config: QuicConnectionConfig,
val tlsCertificateValidator: com.vitorpamplona.quic.tls.CertificateValidator? = null,
val nowMillis: () -> Long = { kotlin.time.Clock.System.now().toEpochMilliseconds() },
val nowMillis: () -> Long = {
kotlin.time.Clock.System
.now()
.toEpochMilliseconds()
},
val alpnList: List<ByteArray> = listOf(TlsConstants.ALPN_H3),
) {
val sourceConnectionId: ConnectionId = ConnectionId.random(8)
@@ -187,10 +176,13 @@ class QuicConnection(
for ((id, stream) in streams) {
stream.sendCredit =
when (StreamId.kindOf(id)) {
StreamId.Kind.CLIENT_BIDI, StreamId.Kind.SERVER_BIDI ->
StreamId.Kind.CLIENT_BIDI, StreamId.Kind.SERVER_BIDI -> {
if (StreamId.isClientInitiated(id)) tp.initialMaxStreamDataBidiRemote ?: 0L else tp.initialMaxStreamDataBidiLocal ?: 0L
StreamId.Kind.CLIENT_UNI, StreamId.Kind.SERVER_UNI ->
}
StreamId.Kind.CLIENT_UNI, StreamId.Kind.SERVER_UNI -> {
tp.initialMaxStreamDataUni ?: 0L
}
}
}
}
@@ -143,6 +143,7 @@ private fun dispatchFrames(
// We don't currently retransmit, so we just absorb ACKs; once
// Phase F adds retransmission this updates the inflight tracker.
}
is CryptoFrame -> {
ackEliciting = true
state.cryptoReceive.insert(frame.offset, frame.data)
@@ -160,6 +161,7 @@ private fun dispatchFrames(
drainTlsOutbound(conn)
}
}
is StreamFrame -> {
ackEliciting = true
val stream = conn.getOrCreatePeerStream(frame.streamId)
@@ -172,33 +174,42 @@ private fun dispatchFrames(
stream.closeIncoming()
}
}
is DatagramFrame -> {
ackEliciting = true
conn.incomingDatagramsBuffer().addLast(frame.data)
}
is MaxDataFrame -> {
// Updates connection-level send credit; left to the orchestrator.
}
is MaxStreamDataFrame -> {
conn.streamById(frame.streamId)?.let {
if (frame.maxStreamData > it.sendCredit) it.sendCredit = frame.maxStreamData
}
}
is MaxStreamsFrame -> {
// Tracking left for a later phase.
}
is NewConnectionIdFrame -> {
// We don't support migration; ignore.
}
is ConnectionCloseFrame -> {
conn.status = QuicConnection.Status.CLOSED
}
is HandshakeDoneFrame -> {
conn.status = QuicConnection.Status.CONNECTED
}
is PingFrame -> {
ackEliciting = true
}
else -> {
// PADDING + DATA_BLOCKED + STREAM_DATA_BLOCKED + STREAMS_BLOCKED
// are non-eliciting / cleared during decode.
@@ -24,7 +24,6 @@ import com.vitorpamplona.quic.frame.ConnectionCloseFrame
import com.vitorpamplona.quic.frame.CryptoFrame
import com.vitorpamplona.quic.frame.DatagramFrame
import com.vitorpamplona.quic.frame.Frame
import com.vitorpamplona.quic.frame.PingFrame
import com.vitorpamplona.quic.frame.StreamFrame
import com.vitorpamplona.quic.frame.encodeFrames
import com.vitorpamplona.quic.packet.LongHeaderPacket
@@ -39,11 +39,26 @@ package com.vitorpamplona.quic.crypto
object InitialSecrets {
val V1_INITIAL_SALT: ByteArray =
byteArrayOf(
0x38.toByte(), 0x76.toByte(), 0x2c.toByte(), 0xf7.toByte(),
0xf5.toByte(), 0x59.toByte(), 0x34.toByte(), 0xb3.toByte(),
0x4d.toByte(), 0x17.toByte(), 0x9a.toByte(), 0xe6.toByte(),
0xa4.toByte(), 0xc8.toByte(), 0x0c.toByte(), 0xad.toByte(),
0xcc.toByte(), 0xbb.toByte(), 0x7f.toByte(), 0x0a.toByte(),
0x38.toByte(),
0x76.toByte(),
0x2c.toByte(),
0xf7.toByte(),
0xf5.toByte(),
0x59.toByte(),
0x34.toByte(),
0xb3.toByte(),
0x4d.toByte(),
0x17.toByte(),
0x9a.toByte(),
0xe6.toByte(),
0xa4.toByte(),
0xc8.toByte(),
0x0c.toByte(),
0xad.toByte(),
0xcc.toByte(),
0xbb.toByte(),
0x7f.toByte(),
0x0a.toByte(),
)
/**
@@ -246,7 +246,10 @@ fun decodeFrames(data: ByteArray): List<Frame> {
// We don't expect any frame type to exceed 0x40 in our minimal subset.
val type = typeByte.toLong()
when {
type == FrameType.PING -> out += PingFrame
type == FrameType.PING -> {
out += PingFrame
}
type == FrameType.ACK -> {
val largest = r.readVarint()
val delay = r.readVarint()
@@ -260,6 +263,7 @@ fun decodeFrames(data: ByteArray): List<Frame> {
}
out += AckFrame(largest, delay, firstRange, ranges)
}
type == FrameType.ACK_ECN -> {
val largest = r.readVarint()
val delay = r.readVarint()
@@ -272,15 +276,19 @@ fun decodeFrames(data: ByteArray): List<Frame> {
ranges += AckRange(gap, len)
}
// skip ECN counts
r.readVarint(); r.readVarint(); r.readVarint()
r.readVarint()
r.readVarint()
r.readVarint()
out += AckFrame(largest, delay, firstRange, ranges)
}
type == FrameType.CRYPTO -> {
val offset = r.readVarint()
val len = r.readVarint().toInt()
val data2 = r.readBytes(len)
out += CryptoFrame(offset, data2)
}
type in FrameType.STREAM_BASE..(FrameType.STREAM_BASE or 0x07) -> {
val flags = (type - FrameType.STREAM_BASE)
val hasOff = (flags and FrameType.STREAM_OFF_BIT) != 0L
@@ -298,15 +306,37 @@ fun decodeFrames(data: ByteArray): List<Frame> {
}
out += StreamFrame(streamId, offset, payload, fin, hasLen)
}
type == FrameType.MAX_DATA -> out += MaxDataFrame(r.readVarint())
type == FrameType.MAX_STREAM_DATA -> out += MaxStreamDataFrame(r.readVarint(), r.readVarint())
type == FrameType.MAX_STREAMS_BIDI -> out += MaxStreamsFrame(true, r.readVarint())
type == FrameType.MAX_STREAMS_UNI -> out += MaxStreamsFrame(false, r.readVarint())
type == FrameType.DATA_BLOCKED -> r.readVarint() // ignored
type == FrameType.STREAM_DATA_BLOCKED -> {
r.readVarint(); r.readVarint()
type == FrameType.MAX_DATA -> {
out += MaxDataFrame(r.readVarint())
}
type == FrameType.STREAMS_BLOCKED_BIDI || type == FrameType.STREAMS_BLOCKED_UNI -> r.readVarint()
type == FrameType.MAX_STREAM_DATA -> {
out += MaxStreamDataFrame(r.readVarint(), r.readVarint())
}
type == FrameType.MAX_STREAMS_BIDI -> {
out += MaxStreamsFrame(true, r.readVarint())
}
type == FrameType.MAX_STREAMS_UNI -> {
out += MaxStreamsFrame(false, r.readVarint())
}
type == FrameType.DATA_BLOCKED -> {
r.readVarint()
}
// ignored
type == FrameType.STREAM_DATA_BLOCKED -> {
r.readVarint()
r.readVarint()
}
type == FrameType.STREAMS_BLOCKED_BIDI || type == FrameType.STREAMS_BLOCKED_UNI -> {
r.readVarint()
}
type == FrameType.NEW_CONNECTION_ID -> {
val seq = r.readVarint()
val retire = r.readVarint()
@@ -315,9 +345,19 @@ fun decodeFrames(data: ByteArray): List<Frame> {
val token = r.readBytes(16)
out += NewConnectionIdFrame(seq, retire, cid, token)
}
type == FrameType.RETIRE_CONNECTION_ID -> r.readVarint()
type == FrameType.PATH_CHALLENGE -> r.readBytes(8)
type == FrameType.PATH_RESPONSE -> r.readBytes(8)
type == FrameType.RETIRE_CONNECTION_ID -> {
r.readVarint()
}
type == FrameType.PATH_CHALLENGE -> {
r.readBytes(8)
}
type == FrameType.PATH_RESPONSE -> {
r.readBytes(8)
}
type == FrameType.CONNECTION_CLOSE_TRANSPORT -> {
val err = r.readVarint()
val frameType2 = r.readVarint()
@@ -325,22 +365,31 @@ fun decodeFrames(data: ByteArray): List<Frame> {
val reason = r.readBytes(reasonLen).decodeToString()
out += ConnectionCloseFrame(err, frameType2, reason)
}
type == FrameType.CONNECTION_CLOSE_APP -> {
val err = r.readVarint()
val reasonLen = r.readVarint().toInt()
val reason = r.readBytes(reasonLen).decodeToString()
out += ConnectionCloseFrame(err, null, reason)
}
type == FrameType.HANDSHAKE_DONE -> out += HandshakeDoneFrame()
type == FrameType.HANDSHAKE_DONE -> {
out += HandshakeDoneFrame()
}
type == FrameType.DATAGRAM -> {
val payload = r.readBytes(r.remaining)
out += DatagramFrame(payload, explicitLength = false)
}
type == FrameType.DATAGRAM_LEN -> {
val ln = r.readVarint().toInt()
out += DatagramFrame(r.readBytes(ln), explicitLength = true)
}
else -> throw QuicCodecException("unknown frame type 0x${type.toString(16)}")
else -> {
throw QuicCodecException("unknown frame type 0x${type.toString(16)}")
}
}
}
return out
@@ -40,6 +40,7 @@ object Http3StreamType {
const val PUSH: Long = 0x01
const val QPACK_ENCODER: Long = 0x02
const val QPACK_DECODER: Long = 0x03
/** WebTransport unidirectional stream type. */
const val WEBTRANSPORT_UNI_STREAM: Long = 0x54
}
@@ -23,7 +23,6 @@ package com.vitorpamplona.quic.packet
import com.vitorpamplona.quic.QuicCodecException
import com.vitorpamplona.quic.QuicReader
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.Varint
import com.vitorpamplona.quic.connection.ConnectionId
import com.vitorpamplona.quic.crypto.Aead
import com.vitorpamplona.quic.crypto.HeaderProtection
@@ -74,10 +73,11 @@ object LongHeaderPacket {
hpKey: ByteArray,
largestAckedInSpace: Long,
): ByteArray {
val pnLen = com.vitorpamplona.quic.connection.PacketNumberSpaceState.encodeLength(
plain.packetNumber,
largestAckedInSpace,
)
val pnLen =
com.vitorpamplona.quic.connection.PacketNumberSpaceState.encodeLength(
plain.packetNumber,
largestAckedInSpace,
)
require(pnLen in 1..4)
// Build the unprotected header
@@ -161,9 +161,6 @@ object LongHeaderPacket {
}
val length = r.readVarint().toInt()
val pnOffset = r.position
if (pnOffset + length > offset + bytes.size - packetStart + bytes.size /* room check */) {
// length sanity
}
if (pnOffset + length > bytes.size) return null
// Sample for HP starts at pnOffset + 4.
val sampleStart = pnOffset + 4
@@ -191,11 +188,12 @@ object LongHeaderPacket {
for (i in 0 until pnLen) {
truncatedPn = (truncatedPn shl 8) or (packet[localPnOffset + i].toInt() and 0xFF).toLong()
}
val fullPn = com.vitorpamplona.quic.connection.PacketNumberSpaceState.decodePacketNumber(
largestReceived = largestReceivedInSpace,
truncatedPn = truncatedPn,
pnLen = pnLen,
)
val fullPn =
com.vitorpamplona.quic.connection.PacketNumberSpaceState.decodePacketNumber(
largestReceived = largestReceivedInSpace,
truncatedPn = truncatedPn,
pnLen = pnLen,
)
val aadEnd = localPnOffset + pnLen
val aad = packet.copyOfRange(0, aadEnd)
@@ -34,8 +34,7 @@ enum class LongHeaderType(
;
companion object {
fun fromTypeBits(bits: Int): LongHeaderType =
entries.firstOrNull { it.code == bits } ?: error("unknown long-header type bits: $bits")
fun fromTypeBits(bits: Int): LongHeaderType = entries.firstOrNull { it.code == bits } ?: error("unknown long-header type bits: $bits")
}
}
@@ -127,7 +127,9 @@ object ShortHeaderPacket {
return ParseResult(
packet =
ShortHeaderPlaintextPacket(
dcid = com.vitorpamplona.quic.connection.ConnectionId(bytes.copyOfRange(offset + 1, offset + 1 + dcidLen)),
dcid =
com.vitorpamplona.quic.connection
.ConnectionId(bytes.copyOfRange(offset + 1, offset + 1 + dcidLen)),
packetNumber = fullPn,
payload = plaintext,
keyPhase = (packet[0].toInt() and 0x04) != 0,
@@ -63,6 +63,7 @@ class QpackDecoder {
val entry = QpackStaticTable.entries[r.value.toInt()]
out += entry
}
(first and 0x40) != 0 -> {
// Literal Field Line With Name Reference: 0|1|N|T|index(4)
val isStatic = (first and 0x10) != 0
@@ -74,6 +75,7 @@ class QpackDecoder {
pos += valueLen
out += name to value
}
(first and 0x20) != 0 -> {
// Literal Field Line With Literal Name: 0|0|1|N|H|len(3)
val nameH = (first and 0x08) != 0
@@ -87,6 +89,7 @@ class QpackDecoder {
pos += valueLen
out += name to value
}
else -> {
// Indexed Field Line With Post-Base Index — uses dynamic table; reject.
throw QuicCodecException("QPACK post-base indexed field line unsupported")
@@ -33,70 +33,262 @@ object QpackHuffman {
/** RFC 7541 Appendix B — (code, length) for each of the 256 symbols + EOS at index 256. */
private val table: Array<IntArray> =
arrayOf(
intArrayOf(0x1ff8, 13), intArrayOf(0x7fffd8, 23), intArrayOf(0xfffffe2, 28), intArrayOf(0xfffffe3, 28),
intArrayOf(0xfffffe4, 28), intArrayOf(0xfffffe5, 28), intArrayOf(0xfffffe6, 28), intArrayOf(0xfffffe7, 28),
intArrayOf(0xfffffe8, 28), intArrayOf(0xffffea, 24), intArrayOf(0x3ffffffc, 30), intArrayOf(0xfffffe9, 28),
intArrayOf(0xfffffea, 28), intArrayOf(0x3ffffffd, 30), intArrayOf(0xfffffeb, 28), intArrayOf(0xfffffec, 28),
intArrayOf(0xfffffed, 28), intArrayOf(0xfffffee, 28), intArrayOf(0xfffffef, 28), intArrayOf(0xffffff0, 28),
intArrayOf(0xffffff1, 28), intArrayOf(0xffffff2, 28), intArrayOf(0x3ffffffe, 30), intArrayOf(0xffffff3, 28),
intArrayOf(0xffffff4, 28), intArrayOf(0xffffff5, 28), intArrayOf(0xffffff6, 28), intArrayOf(0xffffff7, 28),
intArrayOf(0xffffff8, 28), intArrayOf(0xffffff9, 28), intArrayOf(0xffffffa, 28), intArrayOf(0xffffffb, 28),
intArrayOf(0x14, 6), intArrayOf(0x3f8, 10), intArrayOf(0x3f9, 10), intArrayOf(0xffa, 12),
intArrayOf(0x1ff9, 13), intArrayOf(0x15, 6), intArrayOf(0xf8, 8), intArrayOf(0x7fa, 11),
intArrayOf(0x3fa, 10), intArrayOf(0x3fb, 10), intArrayOf(0xf9, 8), intArrayOf(0x7fb, 11),
intArrayOf(0xfa, 8), intArrayOf(0x16, 6), intArrayOf(0x17, 6), intArrayOf(0x18, 6),
intArrayOf(0x0, 5), intArrayOf(0x1, 5), intArrayOf(0x2, 5), intArrayOf(0x19, 6),
intArrayOf(0x1a, 6), intArrayOf(0x1b, 6), intArrayOf(0x1c, 6), intArrayOf(0x1d, 6),
intArrayOf(0x1e, 6), intArrayOf(0x1f, 6), intArrayOf(0x5c, 7), intArrayOf(0xfb, 8),
intArrayOf(0x7ffc, 15), intArrayOf(0x20, 6), intArrayOf(0xffb, 12), intArrayOf(0x3fc, 10),
intArrayOf(0x1ffa, 13), intArrayOf(0x21, 6), intArrayOf(0x5d, 7), intArrayOf(0x5e, 7),
intArrayOf(0x5f, 7), intArrayOf(0x60, 7), intArrayOf(0x61, 7), intArrayOf(0x62, 7),
intArrayOf(0x63, 7), intArrayOf(0x64, 7), intArrayOf(0x65, 7), intArrayOf(0x66, 7),
intArrayOf(0x67, 7), intArrayOf(0x68, 7), intArrayOf(0x69, 7), intArrayOf(0x6a, 7),
intArrayOf(0x6b, 7), intArrayOf(0x6c, 7), intArrayOf(0x6d, 7), intArrayOf(0x6e, 7),
intArrayOf(0x6f, 7), intArrayOf(0x70, 7), intArrayOf(0x71, 7), intArrayOf(0x72, 7),
intArrayOf(0xfc, 8), intArrayOf(0x73, 7), intArrayOf(0xfd, 8), intArrayOf(0x1ffb, 13),
intArrayOf(0x7fff0, 19), intArrayOf(0x1ffc, 13), intArrayOf(0x3ffc, 14), intArrayOf(0x22, 6),
intArrayOf(0x7ffd, 15), intArrayOf(0x3, 5), intArrayOf(0x23, 6), intArrayOf(0x4, 5),
intArrayOf(0x24, 6), intArrayOf(0x5, 5), intArrayOf(0x25, 6), intArrayOf(0x26, 6),
intArrayOf(0x27, 6), intArrayOf(0x6, 5), intArrayOf(0x74, 7), intArrayOf(0x75, 7),
intArrayOf(0x28, 6), intArrayOf(0x29, 6), intArrayOf(0x2a, 6), intArrayOf(0x7, 5),
intArrayOf(0x2b, 6), intArrayOf(0x76, 7), intArrayOf(0x2c, 6), intArrayOf(0x8, 5),
intArrayOf(0x9, 5), intArrayOf(0x2d, 6), intArrayOf(0x77, 7), intArrayOf(0x78, 7),
intArrayOf(0x79, 7), intArrayOf(0x7a, 7), intArrayOf(0x7b, 7), intArrayOf(0x7ffe, 15),
intArrayOf(0x7fc, 11), intArrayOf(0x3ffd, 14), intArrayOf(0x1ffd, 13), intArrayOf(0xffffffc, 28),
intArrayOf(0xfffe6, 20), intArrayOf(0x3fffd2, 22), intArrayOf(0xfffe7, 20), intArrayOf(0xfffe8, 20),
intArrayOf(0x3fffd3, 22), intArrayOf(0x3fffd4, 22), intArrayOf(0x3fffd5, 22), intArrayOf(0x7fffd9, 23),
intArrayOf(0x3fffd6, 22), intArrayOf(0x7fffda, 23), intArrayOf(0x7fffdb, 23), intArrayOf(0x7fffdc, 23),
intArrayOf(0x7fffdd, 23), intArrayOf(0x7fffde, 23), intArrayOf(0xffffeb, 24), intArrayOf(0x7fffdf, 23),
intArrayOf(0xffffec, 24), intArrayOf(0xffffed, 24), intArrayOf(0x3fffd7, 22), intArrayOf(0x7fffe0, 23),
intArrayOf(0xffffee, 24), intArrayOf(0x7fffe1, 23), intArrayOf(0x7fffe2, 23), intArrayOf(0x7fffe3, 23),
intArrayOf(0x7fffe4, 23), intArrayOf(0x1fffdc, 21), intArrayOf(0x3fffd8, 22), intArrayOf(0x7fffe5, 23),
intArrayOf(0x3fffd9, 22), intArrayOf(0x7fffe6, 23), intArrayOf(0x7fffe7, 23), intArrayOf(0xffffef, 24),
intArrayOf(0x3fffda, 22), intArrayOf(0x1fffdd, 21), intArrayOf(0xfffe9, 20), intArrayOf(0x3fffdb, 22),
intArrayOf(0x3fffdc, 22), intArrayOf(0x7fffe8, 23), intArrayOf(0x7fffe9, 23), intArrayOf(0x1fffde, 21),
intArrayOf(0x7fffea, 23), intArrayOf(0x3fffdd, 22), intArrayOf(0x3fffde, 22), intArrayOf(0xfffff0, 24),
intArrayOf(0x1fffdf, 21), intArrayOf(0x3fffdf, 22), intArrayOf(0x7fffeb, 23), intArrayOf(0x7fffec, 23),
intArrayOf(0x1fffe0, 21), intArrayOf(0x1fffe1, 21), intArrayOf(0x3fffe0, 22), intArrayOf(0x1fffe2, 21),
intArrayOf(0x7fffed, 23), intArrayOf(0x3fffe1, 22), intArrayOf(0x7fffee, 23), intArrayOf(0x7fffef, 23),
intArrayOf(0xfffea, 20), intArrayOf(0x3fffe2, 22), intArrayOf(0x3fffe3, 22), intArrayOf(0x3fffe4, 22),
intArrayOf(0x7ffff0, 23), intArrayOf(0x3fffe5, 22), intArrayOf(0x3fffe6, 22), intArrayOf(0x7ffff1, 23),
intArrayOf(0x3ffffe0, 26), intArrayOf(0x3ffffe1, 26), intArrayOf(0xfffeb, 20), intArrayOf(0x7fff1, 19),
intArrayOf(0x3fffe7, 22), intArrayOf(0x7ffff2, 23), intArrayOf(0x3fffe8, 22), intArrayOf(0x1ffffec, 25),
intArrayOf(0x3ffffe2, 26), intArrayOf(0x3ffffe3, 26), intArrayOf(0x3ffffe4, 26), intArrayOf(0x7ffffde, 27),
intArrayOf(0x7ffffdf, 27), intArrayOf(0x3ffffe5, 26), intArrayOf(0xfffff1, 24), intArrayOf(0x1ffffed, 25),
intArrayOf(0x7fff2, 19), intArrayOf(0x1fffe3, 21), intArrayOf(0x3ffffe6, 26), intArrayOf(0x7ffffe0, 27),
intArrayOf(0x7ffffe1, 27), intArrayOf(0x3ffffe7, 26), intArrayOf(0x7ffffe2, 27), intArrayOf(0xfffff2, 24),
intArrayOf(0x1fffe4, 21), intArrayOf(0x1fffe5, 21), intArrayOf(0x3ffffe8, 26), intArrayOf(0x3ffffe9, 26),
intArrayOf(0xffffffd, 28), intArrayOf(0x7ffffe3, 27), intArrayOf(0x7ffffe4, 27), intArrayOf(0x7ffffe5, 27),
intArrayOf(0xfffec, 20), intArrayOf(0xfffff3, 24), intArrayOf(0xfffed, 20), intArrayOf(0x1fffe6, 21),
intArrayOf(0x3fffe9, 22), intArrayOf(0x1fffe7, 21), intArrayOf(0x1fffe8, 21), intArrayOf(0x7ffff3, 23),
intArrayOf(0x3fffea, 22), intArrayOf(0x3fffeb, 22), intArrayOf(0x1ffffee, 25), intArrayOf(0x1ffffef, 25),
intArrayOf(0xfffff4, 24), intArrayOf(0xfffff5, 24), intArrayOf(0x3ffffea, 26), intArrayOf(0x7ffff4, 23),
intArrayOf(0x3ffffeb, 26), intArrayOf(0x7ffffe6, 27), intArrayOf(0x3ffffec, 26), intArrayOf(0x3ffffed, 26),
intArrayOf(0x7ffffe7, 27), intArrayOf(0x7ffffe8, 27), intArrayOf(0x7ffffe9, 27), intArrayOf(0x7ffffea, 27),
intArrayOf(0x7ffffeb, 27), intArrayOf(0xffffffe, 28), intArrayOf(0x7ffffec, 27), intArrayOf(0x7ffffed, 27),
intArrayOf(0x7ffffee, 27), intArrayOf(0x7ffffef, 27), intArrayOf(0x7fffff0, 27), intArrayOf(0x3ffffee, 26),
intArrayOf(0x1ff8, 13),
intArrayOf(0x7fffd8, 23),
intArrayOf(0xfffffe2, 28),
intArrayOf(0xfffffe3, 28),
intArrayOf(0xfffffe4, 28),
intArrayOf(0xfffffe5, 28),
intArrayOf(0xfffffe6, 28),
intArrayOf(0xfffffe7, 28),
intArrayOf(0xfffffe8, 28),
intArrayOf(0xffffea, 24),
intArrayOf(0x3ffffffc, 30),
intArrayOf(0xfffffe9, 28),
intArrayOf(0xfffffea, 28),
intArrayOf(0x3ffffffd, 30),
intArrayOf(0xfffffeb, 28),
intArrayOf(0xfffffec, 28),
intArrayOf(0xfffffed, 28),
intArrayOf(0xfffffee, 28),
intArrayOf(0xfffffef, 28),
intArrayOf(0xffffff0, 28),
intArrayOf(0xffffff1, 28),
intArrayOf(0xffffff2, 28),
intArrayOf(0x3ffffffe, 30),
intArrayOf(0xffffff3, 28),
intArrayOf(0xffffff4, 28),
intArrayOf(0xffffff5, 28),
intArrayOf(0xffffff6, 28),
intArrayOf(0xffffff7, 28),
intArrayOf(0xffffff8, 28),
intArrayOf(0xffffff9, 28),
intArrayOf(0xffffffa, 28),
intArrayOf(0xffffffb, 28),
intArrayOf(0x14, 6),
intArrayOf(0x3f8, 10),
intArrayOf(0x3f9, 10),
intArrayOf(0xffa, 12),
intArrayOf(0x1ff9, 13),
intArrayOf(0x15, 6),
intArrayOf(0xf8, 8),
intArrayOf(0x7fa, 11),
intArrayOf(0x3fa, 10),
intArrayOf(0x3fb, 10),
intArrayOf(0xf9, 8),
intArrayOf(0x7fb, 11),
intArrayOf(0xfa, 8),
intArrayOf(0x16, 6),
intArrayOf(0x17, 6),
intArrayOf(0x18, 6),
intArrayOf(0x0, 5),
intArrayOf(0x1, 5),
intArrayOf(0x2, 5),
intArrayOf(0x19, 6),
intArrayOf(0x1a, 6),
intArrayOf(0x1b, 6),
intArrayOf(0x1c, 6),
intArrayOf(0x1d, 6),
intArrayOf(0x1e, 6),
intArrayOf(0x1f, 6),
intArrayOf(0x5c, 7),
intArrayOf(0xfb, 8),
intArrayOf(0x7ffc, 15),
intArrayOf(0x20, 6),
intArrayOf(0xffb, 12),
intArrayOf(0x3fc, 10),
intArrayOf(0x1ffa, 13),
intArrayOf(0x21, 6),
intArrayOf(0x5d, 7),
intArrayOf(0x5e, 7),
intArrayOf(0x5f, 7),
intArrayOf(0x60, 7),
intArrayOf(0x61, 7),
intArrayOf(0x62, 7),
intArrayOf(0x63, 7),
intArrayOf(0x64, 7),
intArrayOf(0x65, 7),
intArrayOf(0x66, 7),
intArrayOf(0x67, 7),
intArrayOf(0x68, 7),
intArrayOf(0x69, 7),
intArrayOf(0x6a, 7),
intArrayOf(0x6b, 7),
intArrayOf(0x6c, 7),
intArrayOf(0x6d, 7),
intArrayOf(0x6e, 7),
intArrayOf(0x6f, 7),
intArrayOf(0x70, 7),
intArrayOf(0x71, 7),
intArrayOf(0x72, 7),
intArrayOf(0xfc, 8),
intArrayOf(0x73, 7),
intArrayOf(0xfd, 8),
intArrayOf(0x1ffb, 13),
intArrayOf(0x7fff0, 19),
intArrayOf(0x1ffc, 13),
intArrayOf(0x3ffc, 14),
intArrayOf(0x22, 6),
intArrayOf(0x7ffd, 15),
intArrayOf(0x3, 5),
intArrayOf(0x23, 6),
intArrayOf(0x4, 5),
intArrayOf(0x24, 6),
intArrayOf(0x5, 5),
intArrayOf(0x25, 6),
intArrayOf(0x26, 6),
intArrayOf(0x27, 6),
intArrayOf(0x6, 5),
intArrayOf(0x74, 7),
intArrayOf(0x75, 7),
intArrayOf(0x28, 6),
intArrayOf(0x29, 6),
intArrayOf(0x2a, 6),
intArrayOf(0x7, 5),
intArrayOf(0x2b, 6),
intArrayOf(0x76, 7),
intArrayOf(0x2c, 6),
intArrayOf(0x8, 5),
intArrayOf(0x9, 5),
intArrayOf(0x2d, 6),
intArrayOf(0x77, 7),
intArrayOf(0x78, 7),
intArrayOf(0x79, 7),
intArrayOf(0x7a, 7),
intArrayOf(0x7b, 7),
intArrayOf(0x7ffe, 15),
intArrayOf(0x7fc, 11),
intArrayOf(0x3ffd, 14),
intArrayOf(0x1ffd, 13),
intArrayOf(0xffffffc, 28),
intArrayOf(0xfffe6, 20),
intArrayOf(0x3fffd2, 22),
intArrayOf(0xfffe7, 20),
intArrayOf(0xfffe8, 20),
intArrayOf(0x3fffd3, 22),
intArrayOf(0x3fffd4, 22),
intArrayOf(0x3fffd5, 22),
intArrayOf(0x7fffd9, 23),
intArrayOf(0x3fffd6, 22),
intArrayOf(0x7fffda, 23),
intArrayOf(0x7fffdb, 23),
intArrayOf(0x7fffdc, 23),
intArrayOf(0x7fffdd, 23),
intArrayOf(0x7fffde, 23),
intArrayOf(0xffffeb, 24),
intArrayOf(0x7fffdf, 23),
intArrayOf(0xffffec, 24),
intArrayOf(0xffffed, 24),
intArrayOf(0x3fffd7, 22),
intArrayOf(0x7fffe0, 23),
intArrayOf(0xffffee, 24),
intArrayOf(0x7fffe1, 23),
intArrayOf(0x7fffe2, 23),
intArrayOf(0x7fffe3, 23),
intArrayOf(0x7fffe4, 23),
intArrayOf(0x1fffdc, 21),
intArrayOf(0x3fffd8, 22),
intArrayOf(0x7fffe5, 23),
intArrayOf(0x3fffd9, 22),
intArrayOf(0x7fffe6, 23),
intArrayOf(0x7fffe7, 23),
intArrayOf(0xffffef, 24),
intArrayOf(0x3fffda, 22),
intArrayOf(0x1fffdd, 21),
intArrayOf(0xfffe9, 20),
intArrayOf(0x3fffdb, 22),
intArrayOf(0x3fffdc, 22),
intArrayOf(0x7fffe8, 23),
intArrayOf(0x7fffe9, 23),
intArrayOf(0x1fffde, 21),
intArrayOf(0x7fffea, 23),
intArrayOf(0x3fffdd, 22),
intArrayOf(0x3fffde, 22),
intArrayOf(0xfffff0, 24),
intArrayOf(0x1fffdf, 21),
intArrayOf(0x3fffdf, 22),
intArrayOf(0x7fffeb, 23),
intArrayOf(0x7fffec, 23),
intArrayOf(0x1fffe0, 21),
intArrayOf(0x1fffe1, 21),
intArrayOf(0x3fffe0, 22),
intArrayOf(0x1fffe2, 21),
intArrayOf(0x7fffed, 23),
intArrayOf(0x3fffe1, 22),
intArrayOf(0x7fffee, 23),
intArrayOf(0x7fffef, 23),
intArrayOf(0xfffea, 20),
intArrayOf(0x3fffe2, 22),
intArrayOf(0x3fffe3, 22),
intArrayOf(0x3fffe4, 22),
intArrayOf(0x7ffff0, 23),
intArrayOf(0x3fffe5, 22),
intArrayOf(0x3fffe6, 22),
intArrayOf(0x7ffff1, 23),
intArrayOf(0x3ffffe0, 26),
intArrayOf(0x3ffffe1, 26),
intArrayOf(0xfffeb, 20),
intArrayOf(0x7fff1, 19),
intArrayOf(0x3fffe7, 22),
intArrayOf(0x7ffff2, 23),
intArrayOf(0x3fffe8, 22),
intArrayOf(0x1ffffec, 25),
intArrayOf(0x3ffffe2, 26),
intArrayOf(0x3ffffe3, 26),
intArrayOf(0x3ffffe4, 26),
intArrayOf(0x7ffffde, 27),
intArrayOf(0x7ffffdf, 27),
intArrayOf(0x3ffffe5, 26),
intArrayOf(0xfffff1, 24),
intArrayOf(0x1ffffed, 25),
intArrayOf(0x7fff2, 19),
intArrayOf(0x1fffe3, 21),
intArrayOf(0x3ffffe6, 26),
intArrayOf(0x7ffffe0, 27),
intArrayOf(0x7ffffe1, 27),
intArrayOf(0x3ffffe7, 26),
intArrayOf(0x7ffffe2, 27),
intArrayOf(0xfffff2, 24),
intArrayOf(0x1fffe4, 21),
intArrayOf(0x1fffe5, 21),
intArrayOf(0x3ffffe8, 26),
intArrayOf(0x3ffffe9, 26),
intArrayOf(0xffffffd, 28),
intArrayOf(0x7ffffe3, 27),
intArrayOf(0x7ffffe4, 27),
intArrayOf(0x7ffffe5, 27),
intArrayOf(0xfffec, 20),
intArrayOf(0xfffff3, 24),
intArrayOf(0xfffed, 20),
intArrayOf(0x1fffe6, 21),
intArrayOf(0x3fffe9, 22),
intArrayOf(0x1fffe7, 21),
intArrayOf(0x1fffe8, 21),
intArrayOf(0x7ffff3, 23),
intArrayOf(0x3fffea, 22),
intArrayOf(0x3fffeb, 22),
intArrayOf(0x1ffffee, 25),
intArrayOf(0x1ffffef, 25),
intArrayOf(0xfffff4, 24),
intArrayOf(0xfffff5, 24),
intArrayOf(0x3ffffea, 26),
intArrayOf(0x7ffff4, 23),
intArrayOf(0x3ffffeb, 26),
intArrayOf(0x7ffffe6, 27),
intArrayOf(0x3ffffec, 26),
intArrayOf(0x3ffffed, 26),
intArrayOf(0x7ffffe7, 27),
intArrayOf(0x7ffffe8, 27),
intArrayOf(0x7ffffe9, 27),
intArrayOf(0x7ffffea, 27),
intArrayOf(0x7ffffeb, 27),
intArrayOf(0xffffffe, 28),
intArrayOf(0x7ffffec, 27),
intArrayOf(0x7ffffed, 27),
intArrayOf(0x7ffffee, 27),
intArrayOf(0x7ffffef, 27),
intArrayOf(0x7fffff0, 27),
intArrayOf(0x3ffffee, 26),
intArrayOf(0x3fffffff, 30),
)
@@ -133,12 +133,14 @@ object QpackStaticTable {
/** Build a quick lookup of name → first index for encoder name-reference selection. */
val nameToIndex: Map<String, Int> =
entries.foldIndexed(mutableMapOf()) { idx, acc, (name, _) ->
acc.putIfAbsent(name, idx); acc
acc.putIfAbsent(name, idx)
acc
}
/** Look up name+value → index, or null if no exact match. */
val pairToIndex: Map<Pair<String, String>, Int> =
entries.foldIndexed(mutableMapOf()) { idx, acc, pair ->
acc.putIfAbsent(pair, idx); acc
acc.putIfAbsent(pair, idx)
acc
}
}
@@ -111,7 +111,9 @@ class TlsClient(
serverName = serverName,
x25519PublicKey = keyPair!!.publicKey,
quicTransportParams = transportParameters,
random = fixedRandom ?: com.vitorpamplona.quartz.utils.RandomInstance.bytes(32),
random =
fixedRandom ?: com.vitorpamplona.quartz.utils.RandomInstance
.bytes(32),
)
val chBytes = ch.encode()
@@ -184,6 +186,7 @@ class TlsClient(
)
state = State.WAITING_ENCRYPTED_EXTENSIONS
}
State.WAITING_ENCRYPTED_EXTENSIONS -> {
if (type != TlsConstants.HS_ENCRYPTED_EXTENSIONS) throw QuicCodecException("expected EncryptedExtensions, got type=$type")
if (level != Level.HANDSHAKE) throw QuicCodecException("EncryptedExtensions must arrive at Handshake level")
@@ -193,6 +196,7 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_CERTIFICATE_OR_FINISHED
}
State.WAITING_CERTIFICATE_OR_FINISHED -> {
when (type) {
TlsConstants.HS_CERTIFICATE -> {
@@ -201,14 +205,19 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_CERTIFICATE_VERIFY
}
TlsConstants.HS_FINISHED -> {
// PSK-only handshake skips Certificate/CertificateVerify. We never use PSK,
// but the state machine handles the transition for completeness.
handleServerFinished(msg, bodyReader, len)
}
else -> throw QuicCodecException("unexpected handshake type after EncryptedExtensions: $type")
else -> {
throw QuicCodecException("unexpected handshake type after EncryptedExtensions: $type")
}
}
}
State.WAITING_CERTIFICATE_VERIFY -> {
if (type != TlsConstants.HS_CERTIFICATE_VERIFY) throw QuicCodecException("expected CertificateVerify, got type=$type")
val cv = TlsCertificateVerify.decodeBody(bodyReader)
@@ -217,11 +226,15 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_SERVER_FINISHED
}
State.WAITING_SERVER_FINISHED -> {
if (type != TlsConstants.HS_FINISHED) throw QuicCodecException("expected Finished, got type=$type")
handleServerFinished(msg, bodyReader, len)
}
else -> throw QuicCodecException("unexpected handshake at state=$state type=$type")
else -> {
throw QuicCodecException("unexpected handshake at state=$state type=$type")
}
}
}
@@ -20,8 +20,8 @@
*/
package com.vitorpamplona.quic.tls
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quartz.utils.RandomInstance
import com.vitorpamplona.quic.QuicWriter
/**
* Build a TLS 1.3 ClientHello + handshake header carrying the QUIC-required
@@ -24,7 +24,8 @@ package com.vitorpamplona.quic.tls
* TLS 1.3 protocol constants from RFC 8446 + RFC 9001 (TLS-over-QUIC binding).
*/
object TlsConstants {
// ── Record / handshake message types ──────────────────────────────────────
// Record / handshake message types
/** TLS 1.3 over QUIC uses `legacy_version = 0x0303` ("TLS 1.2") on the wire. */
const val LEGACY_VERSION_TLS_1_2: Int = 0x0303
const val VERSION_TLS_1_3: Int = 0x0304
@@ -55,6 +56,7 @@ object TlsConstants {
const val EXT_SUPPORTED_VERSIONS: Int = 43
const val EXT_PSK_KEY_EXCHANGE_MODES: Int = 45
const val EXT_KEY_SHARE: Int = 51
/** RFC 9001 §8.2 — the QUIC TLS extension carrying transport parameters. */
const val EXT_QUIC_TRANSPORT_PARAMETERS: Int = 0x39
@@ -36,8 +36,9 @@ data class TlsServerHello(
/** The negotiated protocol version. Must be 0x0304 (TLS 1.3) per RFC 8446. */
val negotiatedVersion: Int
get() {
val ext = extensions.firstOrNull { it.type == TlsConstants.EXT_SUPPORTED_VERSIONS }
?: throw QuicCodecException("server hello missing supported_versions extension")
val ext =
extensions.firstOrNull { it.type == TlsConstants.EXT_SUPPORTED_VERSIONS }
?: throw QuicCodecException("server hello missing supported_versions extension")
// server hello carries selected_version (uint16)
val r = QuicReader(ext.data)
return r.readUint16()
@@ -46,8 +47,9 @@ data class TlsServerHello(
/** The peer's X25519 public key, extracted from key_share. */
val serverKeyShareX25519: ByteArray
get() {
val ext = extensions.firstOrNull { it.type == TlsConstants.EXT_KEY_SHARE }
?: throw QuicCodecException("server hello missing key_share extension")
val ext =
extensions.firstOrNull { it.type == TlsConstants.EXT_KEY_SHARE }
?: throw QuicCodecException("server hello missing key_share extension")
val r = QuicReader(ext.data)
val group = r.readUint16()
if (group != TlsConstants.GROUP_X25519) {
@@ -81,12 +83,13 @@ data class TlsEncryptedExtensions(
get() = extensions.firstOrNull { it.type == TlsConstants.EXT_QUIC_TRANSPORT_PARAMETERS }?.data
val alpn: ByteArray?
get() = extensions.firstOrNull { it.type == TlsConstants.EXT_ALPN }?.data?.let {
// ALPN response carries a single protocol_name<1..2^8-1> inside protocols<3..2^16-1>
val r = QuicReader(it)
r.skip(2) // outer length
r.readTlsOpaque1()
}
get() =
extensions.firstOrNull { it.type == TlsConstants.EXT_ALPN }?.data?.let {
// ALPN response carries a single protocol_name<1..2^8-1> inside protocols<3..2^16-1>
val r = QuicReader(it)
r.skip(2) // outer length
r.readTlsOpaque1()
}
companion object {
fun decodeBody(r: QuicReader): TlsEncryptedExtensions = TlsEncryptedExtensions(TlsExtension.decodeList(r))
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quic.webtransport
import com.vitorpamplona.quic.connection.QuicConnection
import com.vitorpamplona.quic.connection.QuicConnectionDriver
import com.vitorpamplona.quic.stream.QuicStream
/**
* Container that bundles a [QuicConnection], its [QuicConnectionDriver], and
* the QUIC stream id of the CONNECT bidi (the WT session id).
*
* Application code (nestsClient's MoQ layer) gets at this via the
* [QuicWebTransportFactory] adapter which exposes the platform-agnostic
* [com.vitorpamplona.nestsclient.transport.WebTransportSession] interface.
*/
class QuicWebTransportSessionState(
val connection: QuicConnection,
val driver: QuicConnectionDriver,
val connectStreamId: Long,
) {
/** True until [close] is called or the underlying QUIC connection terminates. */
val isOpen: Boolean
get() = connection.status == QuicConnection.Status.CONNECTED
/** Open a new client-initiated bidirectional WebTransport stream. */
fun openBidiStream(): QuicStream {
val s = connection.openBidiStream()
// Prefix bytes go onto the new stream first.
s.send.enqueue(encodeWtBidiStreamPrefix(connectStreamId))
return s
}
/** Open a new client-initiated unidirectional WebTransport stream. */
fun openUniStream(): QuicStream {
val s = connection.openUniStream()
s.send.enqueue(encodeWtUniStreamPrefix(connectStreamId))
return s
}
/** Send a WebTransport datagram via QUIC's datagram extension. */
fun sendDatagram(payload: ByteArray) {
val wrapped = WtDatagram.encode(connectStreamId, payload)
connection.queueDatagram(wrapped)
}
fun pollIncomingDatagram(): ByteArray? {
val raw = connection.pollIncomingDatagram() ?: return null
val decoded = WtDatagram.decode(raw) ?: return null
if (decoded.sessionStreamId != connectStreamId) return null
return decoded.payload
}
fun pollIncomingPeerStream(): QuicStream? = connection.pollIncomingPeerStream()
fun close(
errorCode: Int = 0,
reason: String = "",
) {
connection.streamById(connectStreamId)?.let {
it.send.enqueue(encodeCloseSessionCapsule(errorCode, reason))
it.send.finish()
}
driver.close()
}
}
@@ -20,7 +20,6 @@
*/
package com.vitorpamplona.quic.webtransport
import com.vitorpamplona.quic.QuicReader
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.Varint
@@ -65,6 +64,7 @@ object WtDatagram {
object WtStreamType {
/** Client-initiated bidirectional WT stream — followed by quarter session id. */
const val WT_BIDI_STREAM: Long = 0x41
/** Client-initiated unidirectional WT stream — preceded by HTTP/3 stream-type 0x54 + quarter session id. */
const val WT_UNI_STREAM_PREFIX: Long = 0x54
}