Commitb69af54dRecorded18 Oct 2026Repositorysigil-quic

Support authenticated path migration and addressed UDP output

Message

Track bounded path validation and per-path amplification credit, issue and retire spare connection IDs, and expose owned peer addresses and path events. Route protected output to its recorded destination and bound blocked-send retention so an old port cannot prevent validation of a new path.

The loopback endpoint survives authenticated client source-port rebinding. Configurable interface and IPv6 binds remain a separate shared-socket follow-up.

Validated on Phantom: 777 tests on bytecode and native, the sole expected header-protection sabotage failure, 47 P0 vectors red then green, 31 unchanged focused entries, and focused path/CID/output fault controls.

Changed
 README.md                              |  73 +++++++++++++-----
 scripts/native-tls-tests.mjs           |  44 ++++++++++-
 src/sigil/quic/connection.sgl          | 632 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
 src/sigil/quic/connection/ids.sgl      | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/quic/connection/network.sgl  | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/quic/connection/peer/ids.sgl | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/quic/endpoint.sgl            | 130 +++++++++++++++++++++++++-------
 src/sigil/quic/path/manager.sgl        | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/quic/path/validation.sgl     | 179 ++++++++++++++++++++++++++++++++++++++++++++
 test/test-addressed-output.sgl         |  84 +++++++++++++++++++++
 test/test-connection-network.sgl       | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-endpoint-network.sgl         | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-local-ids.sgl                | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-network-confirmation.sgl     |  65 ++++++++++++++++
 test/test-network-receive.sgl          | 606 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-path-manager.sgl             | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-path-validation.sgl          | 189 ++++++++++++++++++++++++++++++++++++++++++++++
 test/test-peer-ids.sgl                 | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-random-ids.sgl               |  49 ++++++++++++
 19 files changed, 3927 insertions(+), 159 deletions(-)
Diff
README.mdmodified
@@ -278,12 +278,13 @@ Packet factories clear their private Initial extraction/side intermediates and
278
discard partial key material if a later derivation throws. Borrowed inputs and
279
complete returned vectors keep their caller's ownership.
280
−281
Authenticated PATH_CHALLENGE frames receive matching PATH_RESPONSE frames on
−282
the endpoint's pinned path, independently of an application handler. Responses
−283
precede application backlog and are not retransmitted on PTO. Path probes and
−284
responses expand to 1200 bytes when amplification credit permits; ordinary
−285
congestion and AEAD limits still apply. This does not add migration or an
−286
active path-validation state machine.
+281
Authenticated PATH_CHALLENGE frames receive matching PATH_RESPONSE frames,
+282
independently of an application handler. Address-bound connections route each
+283
response to the challenge's source. Responses precede application backlog,
+284
expire while blocked and are not retransmitted on PTO. Independent candidate
+285
and previous-path validation uses fresh, paced probes and separate three-times
+286
received-byte budgets. Promotion requires reachability and a 1200-byte MTU
+287
check. The UDP endpoint below supplies this address-bound adapter on loopback.
288
289
Zero-length CID policy is enforced after packet authentication and before any
290
application handler: NEW_CONNECTION_ID is forbidden when sending a zero-length
@@ -292,8 +293,11 @@ zero-length local CID. Both close with PROTOCOL_VIOLATION and the offending
293
frame type. RETIRE_CONNECTION_ID above the largest issued sequence also closes
294
with PROTOCOL_VIOLATION. A server's preferred-address CID has sequence 1; other
295
caller-issued NEW_CONNECTION_ID sequences count only after packet emission,
−295
including PTO retransmission, rather than queue admission. Full nonzero CID
−296
inventory, routing, rotation and retirement remain under audit.
+296
including PTO retransmission, rather than queue admission. Address-bound
+297
connections own spare-ID issuance and path assignment: reservations become
+298
routable only after emission, retired aliases stop routing immediately, and
+299
RETIRE_CONNECTION_ID remains pending until its carrying packet is acknowledged.
+300
These pools enforce the negotiated active limit and bounded lifetime inventory.
301
302
`(sigil quic connection space)` provides bounded PN/ACK bookkeeping for
303
each encryption level. `make-packet-space level` accepts `range-limit:`,
@@ -330,7 +334,28 @@ certificate policy and CIDs; the connection supplies its CID transport
334
parameters. Start clients with `quic-start! connection now`, feed datagrams
335
with `quic-receive! connection bytes now`, call `quic-tick! connection now`
336
and immediately send the ordered list from `quic-take-datagrams! connection now`.
−333
Times are monotonic exact milliseconds. The endpoint owns one fixed peer address.
+337
Times are monotonic exact milliseconds. The UDP endpoint binds the original
+338
peer address before starting or receiving the handshake.
+339
+340
A transport adapter can instead call `quic-bind-peer-address! connection bytes`
+341
once before starting or receiving the handshake. The adapter supplies canonical
+342
IP-and-port bytes, bounded to 1–64 bytes, and must pass the actual source through
+343
`quic-receive! connection bytes now address: source`. It drains protected output
+344
with `quic-take-addressed-datagrams! connection now`. Each output has copied
+345
bytes, destination address and destination CID through `quic-output-bytes`,
+346
`quic-output-address` and `quic-output-id`. Check `quic-output-valid? connection
+347
output` immediately before sending retained output: path promotion, CID
+348
retirement or closure can invalidate a previously queued datagram. Raw datagram
+349
output is unavailable after binding an address.
+350
+351
`quic-peer-address` returns the current validated destination. Drain up to sixteen
+352
retained diagnostics with `quic-take-path-events!`; each is a vector containing
+353
`path-changed`, `path-validated` or `path-failed`, owned address bytes and a reason
+354
symbol. A changed-path event does not authorize sending application data there.
+355
Only authenticated, replay-checked, handler-ready packets earn path credit;
+356
clients discard unknown server sources. Server migration uses validated paths
+357
and separate peer CIDs, requeues reliable flight under fresh packet numbers and
+358
resets congestion accounting. DATAGRAM payloads are never retransmitted.
359
360
After validating a Retry token, the caller can pass `retry-source-id:` to
361
`make-quic-server`. The original positional destination ID remains the first
@@ -478,9 +503,11 @@ before the full suite passes. Stream tests include protected bidirectional
503
transfer beyond the receive-window capacity and reset/cancellation under loss.
504
505
`(sigil quic endpoint)` is a private IPv4 loopback UDP adapter. It binds only
−481
`127.0.0.1`, uses a dedicated nonblocking socket, pins one peer port, and
−482
limits receive work and retained protected datagrams. It provides no public
−483
listener, migration, or multi-connection service.
+506
`127.0.0.1` and uses a dedicated nonblocking socket for one connection. It binds
+507
the original peer before the handshake, sends each protected datagram to its
+508
recorded destination and validates confirmed-server peer port changes. Clients
+509
keep their original server-source restriction. Configurable interface binds,
+510
IPv6 and multiple connections remain outside this adapter.
511
512
`open-quic-endpoint port: 0` chooses an ephemeral port, available through
513
`quic-endpoint-port`. Attach a fresh client connection/stream with
@@ -495,8 +522,15 @@ a decoded, nonempty CRYPTO frame at offset zero. Keys are discarded after
522
this check. Initial integrity is not address validation: Retry remains
523
necessary before public operation. Failed candidates before confirmation
524
release the slot and discard their output while preserving the original
−498
factory and configured port restriction; a confirmed connection retains the
−499
endpoint's single-connection lifetime.
+525
factory and configured port restriction. After confirmation, a server passes
+526
candidate source ports to authenticated path validation; the admission pin
+527
does not prevent NAT rebinding. The endpoint retains one connection throughout.
+528
+529
`quic-endpoint-peer-address` returns owned canonical peer bytes or `#f` before
+530
admission. `quic-endpoint-take-path-events!` drains the connection's bounded
+531
path events. Application data moves only after validation; a `path-changed`
+532
event alone does not change its destination. Retained ciphertext is checked
+533
again before each send, so CID retirement and path promotion can invalidate it.
534
535
For private Retry testing, call `quic-endpoint-accept-retry! endpoint factory`
536
instead. Its factory receives three copied IDs: original destination, client
@@ -521,9 +555,14 @@ This path remains restricted to loopback pending admission tests and review.
555
556
Call `quic-endpoint-step! endpoint wait-ms: milliseconds` repeatedly alongside
557
application reads/writes. Each call waits at most 50 milliseconds and drives
−524
receive, timers and protected output. Temporary send blockage retains the
−525
exact datagram bytes for at most one second; failures close the socket and
−526
connection. `quic-endpoint-connection`, `quic-endpoint-closed?` and
+558
receive, timers and protected output. Each flush attempts at most 32 retained
+559
plus 32 fresh protected datagrams, once per record, and retains at most 32
+560
failed sends. One blocked address cannot stop validation controls for another.
+561
Each retained record keeps its original one-second deadline; expiry and
+562
overflow drop ciphertext as UDP loss. Reliable frames remain owned by the
+563
connection's retransmission machinery, while DATAGRAM data is unreliable.
+564
Malformed output and partial datagram sends close the socket and connection.
+565
`quic-endpoint-connection`, `quic-endpoint-closed?` and
566
`quic-endpoint-failure` expose progress. `quic-endpoint-close!` is idempotent,
567
clears connection/stream secrets and storage, attempts final close output,
568
and closes the dedicated socket. Cleanup also runs if initiating connection
scripts/native-tls-tests.mjsmodified
@@ -28,6 +28,11 @@ const files = [
28
'test-server-retry.sgl',
29
'test-connection-space.sgl',
30
'test-connection.sgl',
+31
'test-connection-network.sgl',
+32
'test-addressed-output.sgl',
+33
'test-network-confirmation.sgl',
+34
'test-network-receive.sgl',
+35
'test-random-ids.sgl',
36
'test-datagram.sgl',
37
'test-aead-limits.sgl',
38
'test-application-close.sgl',
@@ -42,8 +47,12 @@ const files = [
47
'test-sni-fallback.sgl',
48
'test-tls-identity.sgl',
49
'test-path-response.sgl',
+50
'test-path-validation.sgl',
+51
'test-path-manager.sgl',
52
'test-cid-zero.sgl',
53
'test-cid-sequence.sgl',
+54
'test-local-ids.sgl',
+55
'test-peer-ids.sgl',
56
'test-handler-readiness.sgl',
57
'test-early-packets.sgl',
58
'test-receive-bounds.sgl',
@@ -54,6 +63,7 @@ const files = [
63
'test-endpoint.sgl',
64
'test-endpoint-admission.sgl',
65
'test-endpoint-retry.sgl',
+66
'test-endpoint-network.sgl',
67
];
68
const cleanupFaults = process.argv.includes('--cleanup-faults');
69
const secretCleanup = process.argv.includes('--secret-cleanup');
@@ -80,7 +90,14 @@ const serverRetry = process.argv.includes('--server-retry');
90
const endpointRetry = process.argv.includes('--endpoint-retry');
91
const retryCleanup = process.argv.includes('--retry-cleanup');
92
const datagram = process.argv.includes('--datagram');
−83
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry, endpointRetry, retryCleanup, datagram].filter(Boolean).length > 1) {
+93
const pathValidation = process.argv.includes('--path-validation');
+94
const localIds = process.argv.includes('--local-ids');
+95
const peerIds = process.argv.includes('--peer-ids');
+96
const pathManager = process.argv.includes('--path-manager');
+97
const connectionNetwork = process.argv.includes('--connection-network');
+98
const networkIntegration = process.argv.includes('--network-integration');
+99
const endpointNetwork = process.argv.includes('--endpoint-network');
+100
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry, endpointRetry, retryCleanup, datagram, pathValidation, localIds, peerIds, pathManager, connectionNetwork, networkIntegration, endpointNetwork].filter(Boolean).length > 1) {
101
throw new Error('Select one focused test suite');
102
}
103
if (cleanupFaults) {
@@ -158,6 +175,27 @@ if (retryCleanup) {
175
if (datagram) {
176
files.splice(0, files.length, 'test-datagram-codec.sgl', 'test-datagram.sgl');
177
}
+178
if (pathValidation) {
+179
files.splice(0, files.length, 'test-path-validation.sgl');
+180
}
+181
if (localIds) {
+182
files.splice(0, files.length, 'test-local-ids.sgl');
+183
}
+184
if (peerIds) {
+185
files.splice(0, files.length, 'test-peer-ids.sgl');
+186
}
+187
if (pathManager) {
+188
files.splice(0, files.length, 'test-path-manager.sgl');
+189
}
+190
if (connectionNetwork) {
+191
files.splice(0, files.length, 'test-connection-network.sgl');
+192
}
+193
if (networkIntegration) {
+194
files.splice(0, files.length, 'test-addressed-output.sgl', 'test-network-confirmation.sgl', 'test-network-receive.sgl', 'test-random-ids.sgl');
+195
}
+196
if (endpointNetwork) {
+197
files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl');
+198
}
199
const bodies = files.map(file => {
200
const text = readFileSync(join(root, 'test', file), 'utf8');
201
const boundary = text.indexOf('\n\n');
@@ -181,9 +219,9 @@ writeFileSync(join(source, 'native.sgl'), `(define-library (sigil quic tests nat
219
(sigil quic packet)${files.includes('test-retry-token.sgl') || retryCleanup ? '\n (sigil quic retry token)' : ''}
220
(sigil quic error)
221
(sigil quic connection space)
−184
(sigil quic connection)
+222
(sigil quic connection)${(files.includes('test-local-ids.sgl') || files.includes('test-random-ids.sgl')) ? '\n (sigil quic connection ids)' : ''}${files.includes('test-peer-ids.sgl') ? '\n (sigil quic connection peer ids)' : ''}${(files.includes('test-connection-network.sgl') || files.includes('test-network-confirmation.sgl')) ? '\n (sigil quic connection network)' : ''}
223
(sigil quic stream)
−186
(sigil quic endpoint)
+224
(sigil quic endpoint)${files.includes('test-path-validation.sgl') ? '\n (sigil quic path validation)' : ''}${files.includes('test-path-manager.sgl') ? '\n (sigil quic path manager)' : ''}
225
(sigil quic stream state)
226
(sigil quic transport parameters)
227
(sigil quic tls)
src/sigil/quic/connection.sglmodified
@@ -1,5 +1,5 @@
−1
;;; Q1 QUIC connection machinery. No sockets: the endpoint supplies one
−2
;;; fixed peer address and sends returned datagrams immediately, in order.
+1
;;; QUIC connection machinery without sockets. Addressed output retains its
+2
;;; sealed destination; the endpoint sends that datagram without retargeting.
3
(define-library (sigil quic connection)
4
(import (sigil crypto)
5
(sigil io)
@@ -12,10 +12,13 @@
12
(sigil quic reassembly)
13
(sigil quic tls)
14
(sigil quic connection space)
+15
(sigil quic connection network)
16
(sigil quic transport parameters))
17
(export make-quic-client make-quic-server quic-connection? quic-role
18
quic-local-id quic-peer-id quic-complete? quic-confirmed? quic-closed? quic-idle-expired?
19
quic-start! quic-take-datagrams! quic-receive! quic-tick! quic-failure
+20
quic-bind-peer-address! quic-peer-address quic-take-addressed-datagrams! quic-take-path-events!
+21
quic-output? quic-output-bytes quic-output-address quic-output-id quic-output-valid?
22
quic-set-frame-handler! quic-send-frame! quic-send-capacity
23
quic-send-datagram! quic-read-datagram!
24
quic-datagram-queue-limit quic-datagram-byte-limit
@@ -26,6 +29,33 @@
29
(define quic-datagram-queue-limit 32)
30
(define quic-datagram-byte-limit 65536)
31
+32
;; Metadata is owned alongside protected bytes. Public reads return copies;
+33
;; close-packet replay copies the same destination instead of a later peer.
+34
(define-struct protected-output
+35
(bytes)
+36
(address)
+37
(destination-id)
+38
(close?)
+39
(path-control? default: #f))
+40
+41
(define (quic-output? output) (protected-output? output))
+42
(define (quic-output-bytes output) (bytevector-copy (protected-output-bytes output)))
+43
(define (quic-output-address output) (bytevector-copy (protected-output-address output)))
+44
(define (quic-output-id output) (bytevector-copy (protected-output-destination-id output)))
+45
+46
(define (output-size output)
+47
(bytevector-length (if (protected-output? output) (protected-output-bytes output) output)))
+48
+49
(define (copy-output output)
+50
(if (protected-output? output)
+51
(protected-output bytes: (quic-output-bytes output) address: (quic-output-address output)
+52
destination-id: (quic-output-id output) close?: (protected-output-close? output)
+53
path-control?: (protected-output-path-control? output))
+54
(bytevector-copy output)))
+55
+56
(define (wipe-output! output)
+57
(wipe! (if (protected-output? output) (protected-output-bytes output) output)))
+58
59
(define-struct connection-level
60
(level)
61
(space)
@@ -57,6 +87,11 @@
87
(retry-hello default: #f mutable: #t)
88
(peer-initial-id default: #f mutable: #t)
89
(peer-id mutable: #t)
+90
(peer-address default: #f mutable: #t)
+91
(network default: #f mutable: #t)
+92
(send-path default: #f mutable: #t)
+93
(path-responses default: '() mutable: #t)
+94
(closing-routes default: '() mutable: #t)
95
(levels)
96
(state default: 'new mutable: #t)
97
(confirmed? default: #f mutable: #t)
@@ -100,6 +135,120 @@
135
(define (quic-negotiated-alpn connection) (tls-negotiated-alpn (quic-connection-tls connection)))
136
(define (quic-local-id connection) (bytevector-copy (quic-connection-local-id connection)))
137
(define (quic-peer-id connection) (bytevector-copy (quic-connection-peer-id connection)))
+138
(define (quic-peer-address connection)
+139
(let ((network (quic-connection-network connection)))
+140
(if network
+141
(network-address network)
+142
(and (quic-connection-peer-address connection)
+143
(bytevector-copy (quic-connection-peer-address connection))))))
+144
+145
(define (quic-take-path-events! connection)
+146
(if (quic-connection-network connection)
+147
(network-take-events! (quic-connection-network connection))
+148
'()))
+149
+150
;; Bind the original address once, before the handshake starts. Subsequent
+151
;; changes belong to authenticated path validation, never to this setter.
+152
(define (quic-bind-peer-address! connection address)
+153
(unless (and (bytevector? address) (<= 1 (bytevector-length address) 64)
+154
(eq? (quic-connection-state connection) 'new)
+155
(not (quic-connection-peer-address connection)))
+156
(error "QUIC connection: original peer address must be bound once before starting"))
+157
(set-quic-connection-peer-address! connection (bytevector-copy address)))
+158
+159
;; The endpoint drops queued output whose peer CID has since retired.
+160
;; Closing output retains only the frozen final destination, while ordinary
+161
;; application output becomes invalid as soon as closure starts.
+162
(define (quic-output-valid? connection output)
+163
(and (protected-output? output)
+164
(or (not (quic-closed? connection))
+165
(and (quic-closing? connection) (protected-output-close? output)))
+166
(let ((network (quic-connection-network connection)))
+167
(if network
+168
(let ((destination (network-destination network (protected-output-address output))))
+169
(and destination
+170
(or (protected-output-path-control? output)
+171
(bytes=? (network-address network) (protected-output-address output)))
+172
(bytes=? (vector-ref destination 1) (protected-output-destination-id output))))
+173
(and (bytes=? (quic-peer-address connection) (protected-output-address output))
+174
(bytes=? (quic-peer-id connection) (protected-output-destination-id output)))))))
+175
+176
;; TLS may deliver authenticated CID frames before HANDSHAKE_DONE reaches
+177
;; a client. Install passive inventory at TLS completion; migration inputs
+178
;; and path probes remain gated on confirmation by the receive/send hooks.
+179
(define (ensure-network! connection)
+180
(when (and (quic-connection-peer-address connection)
+181
(not (quic-connection-network connection))
+182
(not (quic-closed? connection))
+183
(tls-complete? (quic-connection-tls connection)))
+184
(set-quic-connection-network! connection
+185
(make-connection-network (quic-local-id connection) (quic-peer-id connection)
+186
(quic-role connection) (quic-connection-peer-address connection)
+187
(quic-connection-now connection)
+188
(quic-local-parameters connection) (quic-peer-parameters connection)
+189
pto: (min 60000 (connection-pto connection))
+190
highest: (packet-space-largest (connection-level-space (level-state connection 'application)))
+191
confirmed?: (quic-confirmed? connection)))
+192
(set-quic-connection-send-path! connection
+193
(bytevector-copy (quic-connection-peer-address connection))))
+194
(when (and (quic-connection-network connection) (quic-confirmed? connection))
+195
(network-confirm! (quic-connection-network connection) (quic-connection-now connection)
+196
(packet-space-largest (connection-level-space (level-state connection 'application))))))
+197
+198
(define (synchronize-peer-id! connection)
+199
(let ((network (quic-connection-network connection)))
+200
(when network
+201
(unless (bytes=? (quic-connection-send-path connection) (network-address network))
+202
(reset-path-congestion! connection)
+203
(set-quic-connection-send-path! connection (network-address network)))
+204
(let ((destination (network-destination network (network-address network))))
+205
(when destination
+206
(set-quic-connection-peer-id! connection (vector-ref destination 1)))))))
+207
+208
;; Retire all old-path congestion inventory, retaining reliable frame
+209
;; ownership for new-PN transmission. Old ACKs remain syntactically valid
+210
;; but cannot grow this path's fresh window or reset its loss timer.
+211
(define (reset-path-congestion! connection)
+212
(let* ((state (level-state connection 'application))
+213
(space (connection-level-space state))
+214
(pending (packet-space-pending space))
+215
(sent (connection-level-sent state)))
+216
(for-each (lambda (packet) (packet-space-retire! space (sent-packet-info-number packet))) pending)
+217
(set-connection-level-sent! state '())
+218
(set-connection-level-retries! state 0)
+219
(set-quic-connection-window! connection 12000)
+220
(set-quic-connection-threshold! connection #f)
+221
(for-each
+222
(lambda (packet)
+223
(let ((entry (assv (sent-packet-info-number packet) sent)))
+224
(when entry (queue-frame! connection 'application (cdr entry)))))
+225
(reverse pending))))
+226
+227
(define (local-route? connection cid)
+228
(let ((network (quic-connection-network connection)))
+229
(cond
+230
(network (and (network-route network cid) #t))
+231
((pair? (quic-connection-closing-routes connection))
+232
(and (any (lambda (old) (bytes=? old cid)) (quic-connection-closing-routes connection)) #t))
+233
(else (bytes=? cid (quic-connection-local-id connection))))))
+234
+235
;; Local issued CIDs have unambiguous prefixes. Select a short-header CID
+236
;; length by actual owned route, rather than always assuming initial length.
+237
;; Long headers carry their own lengths and need no speculative iteration.
+238
(define (decode-connection-header connection datagram offset)
+239
(let ((network (quic-connection-network connection)))
+240
(if (or (>= (bytevector-u8-ref datagram offset) 128)
+241
(and (not network) (null? (quic-connection-closing-routes connection))))
+242
(decode-packet-header datagram offset (bytevector-length (quic-connection-local-id connection)))
+243
(let loop ((lengths
+244
(if network
+245
(network-route-lengths network)
+246
(map bytevector-length (quic-connection-closing-routes connection)))))
+247
(and (pair? lengths)
+248
(let ((header (guard (exn (else #f)) (decode-packet-header datagram offset (car lengths)))))
+249
(if (and header (local-route? connection (quic-header-destination-id header)))
+250
header
+251
(loop (cdr lengths)))))))))
252
(define (quic-confirmed? connection) (quic-connection-confirmed? connection))
253
(define (quic-closed? connection)
254
(and (memq (quic-connection-state connection) '(closing closed draining)) #t))
@@ -304,7 +453,10 @@
453
(define (check-frame-packet-budget! connection level wire)
454
(unless (and (<= (bytevector-length wire) 1200)
455
(let ((header
−307
(encode-packet-header level (quic-connection-peer-id connection)
+456
(encode-packet-header level
+457
(if (and (eq? level 'application) (quic-connection-peer-address connection))
+458
(make-bytevector 20 0)
+459
(quic-connection-peer-id connection))
460
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
461
(initial-token connection level) 0 4 (+ (bytevector-length wire) 16)
462
length-width: (and (not (eq? level 'application)) 2))))
@@ -385,6 +537,111 @@
537
(- (connection-level-queue-bytes state) (bytevector-length (encode-frame frame)))))
538
frame))
539
+540
(define (control-pending? state type sequence)
+541
(define (same? frame)
+542
(and (= type (quic-frame-type frame)) (= sequence (frame-ref frame 'sequence))))
+543
(or (any same? (connection-level-queue-front state))
+544
(any same? (connection-level-queue-back state))
+545
(any (lambda (entry) (same? (cdr entry))) (connection-level-sent state))))
+546
+547
;; NEW/RETIRE controls fit within64 bytes at every bounded sequence. Check
+548
;; queue capacity before reserving an ID, so a full queue cannot strand a
+549
;; non-routable reservation. The inventory counts queued reservations.
+550
(define (control-room? state)
+551
(and (< (connection-level-queue-count state) 4096)
+552
(<= (+ (connection-level-queue-bytes state) 64) 2097152)))
+553
+554
(define (sync-network-controls! connection)
+555
(ensure-network! connection)
+556
(let ((network (quic-connection-network connection))
+557
(state (level-state connection 'application)))
+558
(when (and network (quic-confirmed? connection) (not (quic-closed? connection)))
+559
(for-each
+560
(lambda (frame)
+561
(when (and (control-room? state)
+562
(not (control-pending? state 25 (frame-ref frame 'sequence))))
+563
(queue-frame! connection 'application frame)))
+564
(network-retirements network))
+565
(let loop ((count 0))
+566
(when (and (< count 8) (control-room? state))
+567
(let ((frame (network-issue-spare! network)))
+568
(when frame
+569
(queue-frame! connection 'application frame)
+570
;; queue-frame! owns its encoded token copy and the pool
+571
;; retains a separate copy for advertisement preflight.
+572
(wipe! (frame-ref frame 'token))
+573
(loop (+ count 1)))))))))
+574
+575
;; A queued/lost advertisement must never resurrect a retired local ID.
+576
;; An already acknowledged retirement need not be emitted again either.
+577
(define (stale-network-control? connection frame)
+578
(let ((network (quic-connection-network connection)))
+579
(and network
+580
(case (quic-frame-type frame)
+581
((24) (not (network-check-id! network frame)))
+582
((25)
+583
(not (any (lambda (pending)
+584
(= (frame-ref pending 'sequence) (frame-ref frame 'sequence)))
+585
(network-retirements network))))
+586
(else #f)))))
+587
+588
;; Response metadata stays attached to the authenticated challenge source.
+589
;; Limit pending responses independently of the ordinary reliable queue.
+590
(define (queue-path-response! connection source data)
+591
(let ((pending (quic-connection-path-responses connection)))
+592
(when (and (< (length pending) 16)
+593
(not (any (lambda (entry)
+594
(and (bytes=? source (vector-ref entry 0))
+595
(bytes=? data (vector-ref entry 1)))) pending)))
+596
(set-quic-connection-path-responses! connection
+597
(append pending
+598
(list (vector (bytevector-copy source) (bytevector-copy data)
+599
(+ (quic-connection-now connection) (* 3 (connection-pto connection))))))))))
+600
+601
;; Independent path timers replace transport retransmission of controls.
+602
;; Only a fresh challenge can retry validation. A response is sent once,
+603
;; padded to1200 when its path can afford it, otherwise at minimal size.
+604
(define (take-path-output! connection now maximum)
+605
(let ((out '()) (kept '()) (count 0)
+606
(network (and (quic-confirmed? connection) (quic-connection-network connection))))
+607
(when network (network-tick! network now))
+608
(for-each
+609
(lambda (entry)
+610
(cond
+611
((>= now (vector-ref entry 2)) (wipe! (vector-ref entry 1)))
+612
((>= count maximum) (set! kept (cons entry kept)))
+613
(else
+614
(let* ((frame (make-quic-frame 27 (list (cons 'data (vector-ref entry 1)))))
+615
(packet
+616
(or (emit-packet! connection 'application frame now
+617
address: (vector-ref entry 0) pad?: #t path-control?: #t)
+618
(emit-packet! connection 'application frame now
+619
address: (vector-ref entry 0) path-control?: #t))))
+620
(if packet
+621
(begin
+622
(set! out (cons packet out))
+623
(set! count (+ count 1))
+624
(wipe! (vector-ref entry 1)))
+625
(set! kept (cons entry kept)))))))
+626
(quic-connection-path-responses connection))
+627
(set-quic-connection-path-responses! connection (reverse kept))
+628
(when network
+629
(for-each
+630
(lambda (address)
+631
(when (and address (< count maximum))
+632
(let ((action (network-challenge! network address now)))
+633
(when action
+634
(let* ((frame (make-quic-frame 26 (list (cons 'data (vector-ref action 3)))))
+635
(packet (emit-packet! connection 'application frame now
+636
address: (vector-ref action 0) pad?: #t
+637
path-control?: #t reserved?: #t)))
+638
(wipe! (vector-ref action 3))
+639
(when packet
+640
(set! out (cons packet out))
+641
(set! count (+ count 1))))))))
+642
(list (network-candidate network) (network-previous-address network))))
+643
(reverse out)))
+644
645
(define (wipe! bytes)
646
(when (bytevector? bytes)
647
(do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))
@@ -467,9 +724,9 @@
724
(apply + (map (lambda (level) (packet-space-pending-bytes (connection-level-space (level-state connection level))))
725
'(initial handshake application))))
726
−470
(define (packet-plaintext connection level number frame)
+727
(define (packet-plaintext connection level number frame destination-id pad?)
728
(let* ((body (encode-frames (list frame) level (quic-role connection)))
−472
(header (encode-packet-header level (quic-connection-peer-id connection)
+729
(header (encode-packet-header level destination-id
730
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
731
(initial-token connection level)
732
number 4 (+ (bytevector-length body) 16)
@@ -478,9 +735,10 @@
735
(when (> size 1200) (raise-quic-error 1 #f "QUIC connection frame exceeds packet budget"))
736
(bytes-append body
737
(make-bytevector
−481
(if (or (and (eq? level 'initial)
+738
(if (or pad? (and (eq? level 'initial)
739
(or (eq? (quic-role connection) 'client) (frame-ack-eliciting? frame)))
740
(and (memv (quic-frame-type frame) '(26 27))
+741
(not (quic-connection-peer-address connection))
742
(or (quic-connection-validated? connection)
743
(<= (+ (quic-connection-sent-bytes connection) 1200)
744
(* 3 (quic-connection-received-bytes connection))))))
@@ -488,60 +746,82 @@
746
0)
747
0))))
748
−491
(define (emit-packet! connection level frame now)
−492
(let* ((state (level-state connection level)) (space (connection-level-space state))
−493
(number (packet-space-next space)) (payload (packet-plaintext connection level number frame))
−494
(header (encode-packet-header level (quic-connection-peer-id connection)
−495
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
−496
(initial-token connection level)
−497
number 4 (+ (bytevector-length payload) 16)
−498
length-width: (and (not (eq? level 'application)) 2)))
−499
(size (+ (bytevector-length header) (bytevector-length payload) 16))
−500
(eliciting? (frame-ack-eliciting? frame)))
−501
(and (packet-space-can-send? space size eliciting?)
−502
(or (not eliciting?) (<= (+ (in-flight connection) size) (quic-connection-window connection)))
−503
(or (quic-connection-validated? connection)
−504
(<= (+ (quic-connection-sent-bytes connection) size) (* 3 (quic-connection-received-bytes connection))))
−505
(begin (check-send-key-budget! connection state frame) #t)
−506
(guard (exn
−507
(else
−508
;; Encryption may already have used this PN even if a
−509
;; later admission/allocation failed. Erase this write
−510
;; key before outer closure can attempt a close packet.
−511
(let ((keys (connection-level-write-keys state)))
−512
(set-connection-level-write-keys! state #f)
−513
(when keys (discard-packet-keys! keys)))
−514
(raise exn)))
−515
(let ((packet (seal-quic-packet (connection-level-write-keys state) level
−516
(quic-connection-peer-id connection)
−517
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
−518
number payload token: (initial-token connection level)
−519
length-width: (and (not (eq? level 'application)) 2))))
−520
(set-connection-level-sealed! state (+ 1 (connection-level-sealed state)))
−521
(packet-space-record-sent! space number size now eliciting?)
−522
(when eliciting?
−523
;; Keep congestion/ACK metadata, but never DATAGRAM payload
−524
;; for retransmission. Its PTO elicits an ACK using PING.
−525
(let ((retained (if (datagram-frame? frame) (make-quic-frame 1 '()) frame)))
−526
(set-connection-level-sent! state
−527
(cons (cons number retained) (connection-level-sent state)))))
−528
(sent-activity! connection now eliciting?)
−529
(when (and eliciting? (memq level '(initial handshake)))
−530
(set-quic-connection-probe-start! connection now))
−531
(set-quic-connection-sent-bytes! connection (+ (quic-connection-sent-bytes connection) size))
−532
;; Queue admission alone does not advertise an ID. Record
−533
;; caller-issued NEW IDs only after this packet is committed.
−534
(when (= (quic-frame-type frame) 24)
−535
(set-quic-connection-issued-sequence! connection
−536
(max (quic-connection-issued-sequence connection)
−537
(frame-ref frame 'sequence))))
−538
(when (and (eq? (quic-role connection) 'client) (eq? level 'handshake))
−539
(discard-level! connection 'initial)
−540
(when (and (= (quic-frame-type frame) 6) (tls-complete? (quic-connection-tls connection))
−541
(= (+ (frame-ref frame 'offset) (bytevector-length (frame-ref frame 'data)))
−542
(connection-level-send-offset state)))
−543
(set-quic-connection-finished-sent?! connection #t)))
−544
packet)))))
+749
(define (emit-packet! connection level frame now
+750
(keys: (address #f) (pad? #f) (path-control? #f) (reserved? #f)))
+751
(let* ((network (and (eq? level 'application) (quic-confirmed? connection)
+752
(quic-connection-network connection)))
+753
(target-address (or address (quic-peer-address connection)))
+754
(destination (and network (network-destination network target-address)))
+755
(destination-id (if network (and destination (vector-ref destination 1))
+756
(quic-peer-id connection))))
+757
(and destination-id
+758
(let* ((state (level-state connection level)) (space (connection-level-space state))
+759
(number (packet-space-next space))
+760
(payload (packet-plaintext connection level number frame destination-id pad?))
+761
(header (encode-packet-header level destination-id
+762
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
+763
(initial-token connection level)
+764
number 4 (+ (bytevector-length payload) 16)
+765
length-width: (and (not (eq? level 'application)) 2)))
+766
(size (+ (bytevector-length header) (bytevector-length payload) 16))
+767
(eliciting? (frame-ack-eliciting? frame))
+768
(tracked? (and eliciting? (not path-control?))))
+769
(and (packet-space-can-send? space size tracked?)
+770
(or (not tracked?) (<= (+ (in-flight connection) size) (quic-connection-window connection)))
+771
(or (quic-connection-validated? connection)
+772
(<= (+ (quic-connection-sent-bytes connection) size) (* 3 (quic-connection-received-bytes connection))))
+773
(not (stale-network-control? connection frame))
+774
(begin (check-send-key-budget! connection state frame) #t)
+775
(or (not network)
+776
(if reserved?
+777
(and path-control? (= size 1200))
+778
(network-reserve-send! network target-address size now)))
+779
(guard (exn
+780
(else
+781
;; Encryption may already have used this PN even if a
+782
;; later admission/allocation failed. Erase this write
+783
;; key before outer closure can attempt a close packet.
+784
(let ((keys (connection-level-write-keys state)))
+785
(set-connection-level-write-keys! state #f)
+786
(when keys (discard-packet-keys! keys)))
+787
(raise exn)))
+788
(let ((packet (seal-quic-packet (connection-level-write-keys state) level
+789
destination-id
+790
(if (eq? level 'application) #u8() (quic-connection-local-id connection))
+791
number payload token: (initial-token connection level)
+792
length-width: (and (not (eq? level 'application)) 2))))
+793
(set-connection-level-sealed! state (+ 1 (connection-level-sealed state)))
+794
(packet-space-record-sent! space number size now tracked?)
+795
(when tracked?
+796
;; Keep congestion/ACK metadata, but never DATAGRAM payload
+797
;; for retransmission. Its PTO elicits an ACK using PING.
+798
(let ((retained (if (datagram-frame? frame) (make-quic-frame 1 '()) frame)))
+799
(set-connection-level-sent! state
+800
(cons (cons number retained) (connection-level-sent state)))))
+801
(unless path-control? (sent-activity! connection now eliciting?))
+802
(when (and eliciting? (memq level '(initial handshake)))
+803
(set-quic-connection-probe-start! connection now))
+804
(set-quic-connection-sent-bytes! connection (+ (quic-connection-sent-bytes connection) size))
+805
;; Queue admission alone does not advertise an ID. Record
+806
;; caller-issued NEW IDs only after this packet is committed.
+807
(when (= (quic-frame-type frame) 24)
+808
(when (quic-connection-network connection)
+809
(network-commit-id! (quic-connection-network connection) frame))
+810
(set-quic-connection-issued-sequence! connection
+811
(max (quic-connection-issued-sequence connection)
+812
(frame-ref frame 'sequence))))
+813
(when (and (eq? (quic-role connection) 'client) (eq? level 'handshake))
+814
(discard-level! connection 'initial)
+815
(when (and (= (quic-frame-type frame) 6) (tls-complete? (quic-connection-tls connection))
+816
(= (+ (frame-ref frame 'offset) (bytevector-length (frame-ref frame 'data)))
+817
(connection-level-send-offset state)))
+818
(set-quic-connection-finished-sent?! connection #t)))
+819
(if (quic-connection-peer-address connection)
+820
(protected-output bytes: packet address: (bytevector-copy target-address)
+821
destination-id: (bytevector-copy destination-id)
+822
path-control?: path-control?
+823
close?: (and (memv (quic-frame-type frame) '(28 29)) #t))
+824
packet))))))))
825
826
(define (quic-start! connection now)
827
(update-time! connection now)
@@ -568,10 +848,11 @@
848
(cons 'reason #u8()))))))
849
850
(define (end-closing! connection)
−571
(for-each wipe! (quic-connection-close-packets connection))
+851
(for-each wipe-output! (quic-connection-close-packets connection))
852
(set-quic-connection-close-packets! connection '())
853
(set-quic-connection-close-next! connection #f)
854
(set-quic-connection-close-until! connection #f)
+855
(set-quic-connection-closing-routes! connection '())
856
(set-quic-connection-ready! connection '())
857
(set-quic-connection-state! connection 'closed))
858
@@ -604,12 +885,25 @@
885
(cons packet (quic-connection-ready connection))))))))
886
(if (quic-confirmed? connection) '(application) '(initial handshake application))))
887
(for-each (lambda (level) (discard-level! connection level)) '(initial handshake application))
+888
(for-each (lambda (entry) (wipe! (vector-ref entry 1))) (quic-connection-path-responses connection))
+889
(set-quic-connection-path-responses! connection '())
+890
(when (quic-connection-network connection)
+891
(let ((network (quic-connection-network connection)))
+892
(set-quic-connection-peer-address! connection (network-address network))
+893
(set-quic-connection-closing-routes! connection
+894
(let loop ((sequence 0) (routes '()))
+895
(if (= sequence 256)
+896
(reverse routes)

Showing the first 500 of 870 diff lines for this file. This diff is INCOMPLETE; read the file or clone the repository for the rest.

src/sigil/quic/connection/ids.sgladded
@@ -0,0 +1,221 @@
+1
;;; Bounded locally issued connection IDs. The connection owns wire emission,
+2
;;; authenticated retirement, packet routing and replacement scheduling.
+3
(define-library (sigil quic connection ids)
+4
(import (sigil crypto)
+5
(sigil struct)
+6
(sigil quic bytes)
+7
(sigil quic error)
+8
(sigil quic frame)
+9
(sigil quic wire))
+10
(export make-local-id-pool local-id-pool? local-id-pool-count
+11
local-id-pool-reserve! local-id-pool-reserve-random!
+12
local-id-pool-check! local-id-pool-commit! local-id-pool-retire!
+13
local-id-pool-route local-id-pool-id local-id-pool-lengths
+14
local-id-pool-close!)
+15
(begin
+16
(define-struct local-id-pool
+17
(entries)
+18
(limit)
+19
(next mutable: #t)
+20
(count mutable: #t)
+21
(closed? default: #f mutable: #t))
+22
+23
;; Entry: sequence, copied CID, copied reset token, emitted?, retired?.
+24
;; Retired entries remain as bounded tombstones; sequence slots never reuse.
+25
(define (entry sequence cid token)
+26
(vector sequence (bytevector-copy cid) (and token (bytevector-copy token)) #f #f))
+27
+28
(define (uint? number maximum)
+29
(and (integer? number) (exact? number) (<= 0 number maximum)))
+30
+31
(define (id? cid minimum)
+32
(and (bytevector? cid) (<= minimum (bytevector-length cid) 20)))
+33
+34
(define (prefix? first second)
+35
(and (<= (bytevector-length first) (bytevector-length second))
+36
(let loop ((index 0))
+37
(or (= index (bytevector-length first))
+38
(and (= (bytevector-u8-ref first index) (bytevector-u8-ref second index))
+39
(loop (+ index 1)))))))
+40
+41
;; A short header carries no CID length. Reject overlapping local prefixes
+42
;; so the connection can select a unique length before packet decryption.
+43
(define (ambiguous-id? pool cid)
+44
(let loop ((index 0))
+45
(and (< index (local-id-pool-next pool))
+46
(let ((old (vector-ref (local-id-pool-entries pool) index)))
+47
(or (prefix? cid (vector-ref old 1))
+48
(prefix? (vector-ref old 1) cid)
+49
(loop (+ index 1)))))))
+50
+51
(define (make-local-id-pool initial
+52
(keys: (limit 2) (maximum 256) (preferred #f)))
+53
(unless (and (id? initial 0) (uint? limit 8) (>= limit 2)
+54
(uint? maximum 256) (>= maximum limit)
+55
(or (not preferred)
+56
(and (pair? preferred) (id? (car preferred) 1)
+57
(bytevector? (cdr preferred)) (= 16 (bytevector-length (cdr preferred)))
+58
(> (bytevector-length initial) 0))))
+59
(error "QUIC local IDs: invalid initial ID, limits or preferred address ID"))
+60
(when (and preferred
+61
(or (prefix? initial (car preferred)) (prefix? (car preferred) initial)))
+62
(error "QUIC local IDs: ambiguous preferred address ID"))
+63
(let* ((entries (make-vector maximum #f))
+64
(initial-entry (entry 0 initial #f))
+65
(pool (local-id-pool entries: entries limit: limit
+66
next: (if preferred 2 1) count: (if preferred 2 1))))
+67
;; Construct this inventory only after the handshake's source-ID and
+68
;; optional preferred-address metadata have actually been transmitted.
+69
(vector-set! initial-entry 3 #t)
+70
(vector-set! entries 0 initial-entry)
+71
(when preferred
+72
(let ((preferred-entry (entry 1 (car preferred) (cdr preferred))))
+73
(vector-set! preferred-entry 3 #t)
+74
(vector-set! entries 1 preferred-entry)))
+75
pool))
+76
+77
(define (require-open! pool)
+78
(when (local-id-pool-closed? pool) (error "QUIC local IDs: pool is closed")))
+79
+80
(define (existing pool sequence)
+81
(and (uint? sequence (- (local-id-pool-next pool) 1))
+82
(vector-ref (local-id-pool-entries pool) sequence)))
+83
+84
(define (active? value)
+85
(and value (vector-ref value 3) (not (vector-ref value 4))))
+86
+87
(define (capacity? pool)
+88
(and (> (bytevector-length (vector-ref (vector-ref (local-id-pool-entries pool) 0) 1)) 0)
+89
(< (local-id-pool-count pool) (local-id-pool-limit pool))
+90
(< (local-id-pool-next pool) (vector-length (local-id-pool-entries pool)))))
+91
+92
;; Generate bounded, independent random CID/reset-token material. Retry
+93
;; prefix collisions before allocating sequence or inventory capacity.
+94
;; Zero-length, full and lifetime-exhausted inventories need no entropy.
+95
(define (local-id-pool-reserve-random! pool)
+96
(require-open! pool)
+97
(and (capacity? pool)
+98
(let loop ((attempt 0))
+99
(when (= attempt 8)
+100
(raise-quic-error 1 #f "QUIC local CID entropy repeatedly collided"))
+101
(let ((cid (random-bytes 8)))
+102
(if (ambiguous-id? pool cid)
+103
(loop (+ attempt 1))
+104
(let ((token (random-bytes 16)))
+105
(dynamic-wind
+106
(lambda () #t)
+107
(lambda () (local-id-pool-reserve! pool cid token))
+108
(lambda ()
+109
(do ((index 0 (+ index 1))) ((= index 16))
+110
(bytevector-u8-set! token index 0))))))))))
+111
+112
;; Reserve before queueing NEW_CONNECTION_ID. A reservation counts toward
+113
;; the peer limit but is not routable/retirable until actual packet commit.
+114
;; Lifetime exhaustion stops issuance without forgetting any live ID.
+115
(define (local-id-pool-reserve! pool cid token)
+116
(require-open! pool)
+117
(unless (and (id? cid 1) (bytevector? token) (= (bytevector-length token) 16))
+118
(error "QUIC local IDs: invalid new ID or reset token"))
+119
(let ((initial (vector-ref (local-id-pool-entries pool) 0))
+120
(sequence (local-id-pool-next pool)))
+121
(and (> (bytevector-length (vector-ref initial 1)) 0)
+122
(< (local-id-pool-count pool) (local-id-pool-limit pool))
+123
(< sequence (vector-length (local-id-pool-entries pool)))
+124
(begin
+125
(when (ambiguous-id? pool cid)
+126
(error "QUIC local IDs: repeated or ambiguous new ID"))
+127
(let* ((owned (entry sequence cid token))
+128
(frame (make-quic-frame 24
+129
(list (cons 'sequence sequence) (cons 'retire-prior-to 0)
+130
(cons 'connection-id (bytevector-copy (vector-ref owned 1)))
+131
(cons 'token (bytevector-copy (vector-ref owned 2)))))))
+132
(vector-set! (local-id-pool-entries pool) sequence owned)
+133
(set-local-id-pool-next! pool (+ sequence 1))
+134
(set-local-id-pool-count! pool (+ 1 (local-id-pool-count pool)))
+135
frame)))))
+136
+137
;; Validate the exact queued advertisement before consuming a packet number.
+138
;; A stale advertisement for a retired ID is a drop, never a resurrection.
+139
(define (local-id-pool-check! pool frame)
+140
(require-open! pool)
+141
(unless (and (quic-frame? frame) (= 24 (quic-frame-type frame)))
+142
(error "QUIC local IDs: expected a reserved NEW_CONNECTION_ID"))
+143
(let ((value (existing pool (frame-ref frame 'sequence))))
+144
(unless value (error "QUIC local IDs: unknown advertisement sequence"))
+145
(and (not (vector-ref value 4))
+146
(begin
+147
(unless (and (= 0 (frame-ref frame 'retire-prior-to))
+148
(bytes=? (frame-ref frame 'connection-id) (vector-ref value 1))
+149
(bytes-timing-safe=? (frame-ref frame 'token) (vector-ref value 2)))
+150
(error "QUIC local IDs: advertisement differs from its reservation"))
+151
#t))))
+152
+153
;; Call CHECK! before sealing, then commit that same owned frame only after
+154
;; its protected packet has been recorded. Retransmission is idempotent.
+155
(define (local-id-pool-commit! pool frame)
+156
(and (local-id-pool-check! pool frame)
+157
(let ((value (existing pool (frame-ref frame 'sequence))))
+158
(vector-set! value 3 #t)
+159
#t)))
+160
+161
(define (local-id-pool-route pool cid)
+162
(unless (id? cid 0) (error "QUIC local IDs: invalid routing ID"))
+163
(and (not (local-id-pool-closed? pool))
+164
(let loop ((index 0))
+165
(and (< index (local-id-pool-next pool))
+166
(let ((value (vector-ref (local-id-pool-entries pool) index)))
+167
(if (and (active? value) (bytes=? cid (vector-ref value 1)))
+168
index
+169
(loop (+ index 1))))))))
+170
+171
(define (local-id-pool-id pool sequence)
+172
(and (not (local-id-pool-closed? pool))
+173
(let ((value (existing pool sequence)))
+174
(and (active? value) (bytevector-copy (vector-ref value 1))))))
+175
+176
(define (local-id-pool-lengths pool)
+177
(if (local-id-pool-closed? pool)
+178
'()
+179
(let loop ((index 0) (lengths '()))
+180
(if (= index (local-id-pool-next pool))
+181
lengths
+182
(let* ((value (vector-ref (local-id-pool-entries pool) index))
+183
(size (bytevector-length (vector-ref value 1))))
+184
(loop (+ index 1)
+185
(if (and (active? value) (not (memv size lengths)))
+186
(cons size lengths)
+187
lengths)))))))
+188
+189
(define (wipe-token! value)
+190
(let ((token (vector-ref value 2)))
+191
(when token
+192
(do ((index 0 (+ index 1))) ((= index (bytevector-length token)))
+193
(bytevector-u8-set! token index 0))
+194
(vector-set! value 2 #f))))
+195
+196
;; Only the connection may pass authenticated retirement metadata here.
+197
;; PACKET-ID is the DCID of that exact packet, not its latest active CID.
+198
(define (local-id-pool-retire! pool sequence packet-id)
+199
(require-open! pool)
+200
(unless (and (uint? sequence quic-varint-maximum) (id? packet-id 0))
+201
(error "QUIC local IDs: invalid retirement metadata"))
+202
(let ((value (existing pool sequence)))
+203
(when (or (= (bytevector-length (vector-ref (vector-ref (local-id-pool-entries pool) 0) 1)) 0)
+204
(not value) (not (vector-ref value 3)))
+205
(raise-quic-error 10 25 "QUIC retirement refers to a zero-length or never-issued ID"))
+206
(when (bytes=? packet-id (vector-ref value 1))
+207
(raise-quic-error 10 25 "QUIC retirement refers to this packet's destination ID"))
+208
(and (not (vector-ref value 4))
+209
(begin
+210
(vector-set! value 4 #t)
+211
(wipe-token! value)
+212
(set-local-id-pool-count! pool (- (local-id-pool-count pool) 1))
+213
#t))))
+214
+215
(define (local-id-pool-close! pool)
+216
(unless (local-id-pool-closed? pool)
+217
(do ((index 0 (+ index 1))) ((= index (local-id-pool-next pool)))
+218
(wipe-token! (vector-ref (local-id-pool-entries pool) index))
+219
(vector-set! (local-id-pool-entries pool) index #f))
+220
(set-local-id-pool-count! pool 0)
+221
(set-local-id-pool-closed?! pool #t)))))
src/sigil/quic/connection/network.sgladded
@@ -0,0 +1,266 @@
+1
;;; Confirmed connection network ownership, without sockets or packet crypto.
+2
;;; The connection authenticates/replay-checks complete packets before input,
+3
;;; seals addressed output, and reliably queues/acknowledges retirement frames.
+4
(define-library (sigil quic connection network)
+5
(import (sigil struct)
+6
(sigil quic bytes)
+7
(sigil quic frame)
+8
(sigil quic wire)
+9
(sigil quic transport parameters)
+10
(sigil quic connection ids)
+11
(sigil quic connection peer ids)
+12
(sigil quic path manager))
+13
(export make-connection-network connection-network? network-confirm!
+14
network-address network-candidate network-previous-address network-destination
+15
network-local-count network-local-limit network-issue-id! network-issue-spare!
+16
network-check-id! network-commit-id! network-retire-local-id!
+17
network-route network-route-lengths network-local-id network-peer-id!
+18
network-retirements network-retirement-ack!
+19
network-observe! network-tick! network-reserve-send!
+20
network-challenge! network-response! network-take-events! network-close!)
+21
(begin
+22
(define-struct connection-network
+23
(local default: #f mutable: #t)
+24
(peer default: #f mutable: #t)
+25
(paths default: #f mutable: #t)
+26
(role)
+27
(pto)
+28
(original-address mutable: #t)
+29
(limit)
+30
(assignments default: '() mutable: #t)
+31
(closed? default: #f mutable: #t))
+32
+33
(define (require-open! network)
+34
(when (connection-network-closed? network) (error "QUIC network: state is closed")))
+35
+36
(define (require-confirmed! network)
+37
(require-open! network)
+38
(unless (connection-network-paths network)
+39
(error "QUIC network: path operations require handshake confirmation")))
+40
+41
(define (preferred parameters)
+42
(let ((entry (assoc 13 parameters)))
+43
(and entry
+44
(let* ((value (cdr entry)) (length (bytevector-u8-ref value 24)))
+45
(cons (bytevector-copy value 25 (+ 25 length))
+46
(bytevector-copy value (+ 25 length) (+ 41 length)))))))
+47
+48
;; The supplied transport parameters and source IDs must already be bound
+49
;; to the confirmed TLS handshake. Encoding checks shape, not authenticity.
+50
(define (make-connection-network local-id peer-id role address now local-parameters peer-parameters
+51
(keys: (pto 1000) (highest -1) (confirmed? #t)))
+52
(unless (and (memq role '(client server)) (boolean? confirmed?)
+53
(bytevector? address) (<= 1 (bytevector-length address) 64)
+54
(integer? now) (exact? now) (>= now 0)
+55
(integer? pto) (exact? pto) (<= 1000 pto 60000)
+56
(integer? highest) (exact? highest) (<= -1 highest quic-varint-maximum))
+57
(error "QUIC network: invalid role, confirmation, address or timing metadata"))
+58
(encode-transport-parameters local-parameters role)
+59
(encode-transport-parameters peer-parameters (if (eq? role 'client) 'server 'client))
+60
(unless (and (bytes=? local-id (cdr (assoc 15 local-parameters)))
+61
(bytes=? peer-id (cdr (assoc 15 peer-parameters))))
+62
(error "QUIC network: source IDs differ from authenticated transport parameters"))
+63
(let ((limit (transport-parameter-integer local-parameters 14 2))
+64
(reset (assoc 2 peer-parameters))
+65
(network (connection-network role: role pto: pto original-address: (bytevector-copy address)
+66
limit: (min 8 (transport-parameter-integer peer-parameters 14 2)))))
+67
(when (> limit 8)
+68
(error "QUIC network: local active CID advertisement exceeds supported inventory"))
+69
(guard (exn (else (network-close! network) (raise exn)))
+70
(set-connection-network-local! network
+71
(make-local-id-pool local-id limit: (connection-network-limit network)
+72
preferred: (preferred local-parameters)))
+73
(set-connection-network-peer! network
+74
(make-peer-id-pool peer-id limit: limit
+75
token: (and reset (cdr reset)) preferred: (preferred peer-parameters)))
+76
(when confirmed?
+77
(set-connection-network-paths! network
+78
(make-path-manager address role now pto: pto highest: highest)))
+79
;; Sequence zero is already assigned to the handshake's path.
+80
(set-connection-network-assignments! network (list (cons (bytevector-copy address) 0)))
+81
network)))
+82
+83
(define (network-address network)
+84
(and (not (connection-network-closed? network))
+85
(if (connection-network-paths network)
+86
(path-manager-address (connection-network-paths network))
+87
(bytevector-copy (connection-network-original-address network)))))
+88
+89
;; CID metadata can arrive before HANDSHAKE_DONE. Passive inventory cannot
+90
;; observe new paths, reserve path output or produce a validation challenge.
+91
;; At confirmation seed from the connection's then-current received PN.
+92
(define (network-confirm! network now highest)
+93
(require-open! network)
+94
(unless (connection-network-paths network)
+95
(set-connection-network-paths! network
+96
(make-path-manager (connection-network-original-address network) (connection-network-role network)
+97
now pto: (connection-network-pto network) highest: highest)))
+98
#t)
+99
+100
(define (network-candidate network)
+101
(and (connection-network-paths network) (path-manager-candidate (connection-network-paths network))))
+102
+103
(define (network-previous-address network)
+104
(and (connection-network-paths network)
+105
(path-manager-previous-address (connection-network-paths network))))
+106
+107
(define (contains-address? addresses address)
+108
(any (lambda (key) (bytes=? key address)) addresses))
+109
+110
(define (managed-addresses network)
+111
(let loop ((rest (list (network-address network) (network-candidate network)
+112
(network-previous-address network)))
+113
(out '()))
+114
(cond
+115
((null? rest) (reverse out))
+116
((or (not (car rest)) (contains-address? out (car rest))) (loop (cdr rest) out))
+117
(else (loop (cdr rest) (cons (car rest) out))))))
+118
+119
(define (assignment network address)
+120
(find (lambda (entry) (bytes=? address (car entry))) (connection-network-assignments network)))
+121
+122
;; Retain CID assignments for the active, candidate and old-probe paths.
+123
;; Retire an abandoned assignment rather than reuse it on another address.
+124
;; Active-path allocation comes first when a peer RetirePriorTo removed
+125
;; its previous CID. Exhaustion pauses output without killing a live path.
+126
(define (synchronize! network)
+127
(let* ((peer (connection-network-peer network))
+128
(addresses (managed-addresses network))
+129
(kept '()))
+130
(for-each
+131
(lambda (entry)
+132
(when (peer-id-pool-id peer (cdr entry))
+133
(if (contains-address? addresses (car entry))
+134
(set! kept (cons entry kept))
+135
(peer-id-pool-retire! peer (cdr entry)))))
+136
(connection-network-assignments network))
+137
(set-connection-network-assignments! network (reverse kept))
+138
(for-each
+139
(lambda (address)
+140
(unless (assignment network address)
+141
(let ((sequence (peer-id-pool-acquire! peer)))
+142
(when sequence
+143
(set-connection-network-assignments! network
+144
(append (connection-network-assignments network)
+145
(list (cons (bytevector-copy address) sequence))))))))
+146
addresses)))
+147
+148
;; Return owned (sequence, destination CID) or #f when no unused ID exists.
+149
;; The connection snapshots this together with ADDRESS into sealed output;
+150
;; an endpoint must never send old queued bytes to a later current peer.
+151
(define (network-destination network address)
+152
(let ((entry (assignment network address)))
+153
(and entry
+154
(let ((cid (peer-id-pool-id (connection-network-peer network) (cdr entry))))
+155
(and cid (vector (cdr entry) cid))))))
+156
+157
(define (network-local-count network)
+158
(if (connection-network-local network) (local-id-pool-count (connection-network-local network)) 0))
+159
+160
(define (network-local-limit network) (connection-network-limit network))
+161
+162
;; The connection supplies unpredictable CID/token bytes, queues a returned
+163
;; advertisement once, checks it before sealing, and commits after recording
+164
;; the protected packet. Queueing alone never makes the CID routable.
+165
(define (network-issue-id! network cid token)
+166
(require-open! network)
+167
(local-id-pool-reserve! (connection-network-local network) cid token))
+168
+169
(define (network-issue-spare! network)
+170
(require-open! network)
+171
(local-id-pool-reserve-random! (connection-network-local network)))
+172
+173
(define (network-check-id! network frame)
+174
(require-open! network)
+175
(local-id-pool-check! (connection-network-local network) frame))
+176
+177
(define (network-commit-id! network frame)
+178
(require-open! network)
+179
(local-id-pool-commit! (connection-network-local network) frame))
+180
+181
(define (network-retire-local-id! network sequence packet-destination-id)
+182
(require-open! network)
+183
(local-id-pool-retire! (connection-network-local network) sequence packet-destination-id))
+184
+185
(define (network-route network cid)
+186
(and (connection-network-local network) (local-id-pool-route (connection-network-local network) cid)))
+187
+188
(define (network-route-lengths network)
+189
(if (connection-network-local network) (local-id-pool-lengths (connection-network-local network)) '()))
+190
+191
;; Owned active CID snapshot for bounded close-state routing. Never exposes
+192
;; reset tokens or private inventory entries to the packet connection.
+193
(define (network-local-id network sequence)
+194
(and (connection-network-local network)
+195
(local-id-pool-id (connection-network-local network) sequence)))
+196
+197
(define (network-peer-id! network frame)
+198
(require-open! network)
+199
(let ((active? (peer-id-pool-receive! (connection-network-peer network) frame)))
+200
(synchronize! network)
+201
active?))
+202
+203
;; The connection deduplicates these reliable frames against its queued
+204
;; and sent controls. Repeated calls do not forget unacknowledged IDs.
+205
(define (network-retirements network)
+206
(if (connection-network-peer network) (peer-id-pool-retirements (connection-network-peer network)) '()))
+207
+208
(define (network-retirement-ack! network sequence)
+209
(require-open! network)
+210
(peer-id-pool-ack! (connection-network-peer network) sequence))
+211
+212
(define (network-observe! network address size number frames now)
+213
(require-confirmed! network)
+214
(unless (and (list? frames) (not (any (lambda (frame) (not (quic-frame? frame))) frames)))
+215
(error "QUIC network: expected completely decoded authenticated frames"))
+216
(let ((accepted?
+217
(path-manager-observe! (connection-network-paths network) address size number
+218
(not (any (lambda (frame) (not (memv (quic-frame-type frame) '(0 24 26 27)))) frames))
+219
now)))
+220
(synchronize! network)
+221
accepted?))
+222
+223
(define (network-tick! network now)
+224
(require-confirmed! network)
+225
(path-manager-tick! (connection-network-paths network) now)
+226
(synchronize! network))
+227
+228
(define (network-reserve-send! network address size now)
+229
(require-confirmed! network)
+230
(and (network-destination network address)
+231
(path-manager-send! (connection-network-paths network) address size now)))
+232
+233
;; Return owned (address, peer sequence, peer CID, challenge token). The
+234
;; reservation is already charged for exactly1200 bytes; do not charge it
+235
;; again in network-reserve-send!. No CID means no probe or credit spend.
+236
(define (network-challenge! network address now)
+237
(require-confirmed! network)
+238
(let ((destination (network-destination network address)))
+239
(and destination
+240
(let ((data (path-manager-challenge! (connection-network-paths network) address now)))
+241
(and data (vector (bytevector-copy address) (vector-ref destination 0)
+242
(vector-ref destination 1) data))))))
+243
+244
(define (network-response! network data now)
+245
(require-confirmed! network)
+246
(let ((matched? (path-manager-response! (connection-network-paths network) data now)))
+247
(synchronize! network)
+248
matched?))
+249
+250
(define (network-take-events! network)
+251
(if (connection-network-paths network)
+252
(path-manager-take-events! (connection-network-paths network))
+253
'()))
+254
+255
(define (network-close! network)
+256
(unless (connection-network-closed? network)
+257
(when (connection-network-local network) (local-id-pool-close! (connection-network-local network)))
+258
(when (connection-network-peer network) (peer-id-pool-close! (connection-network-peer network)))
+259
(when (connection-network-paths network) (path-manager-close! (connection-network-paths network)))
+260
(set-connection-network-local! network #f)
+261
(set-connection-network-peer! network #f)
+262
(set-connection-network-paths! network #f)
+263
(set-connection-network-original-address! network #f)
+264
(set-connection-network-assignments! network '())
+265
(set-connection-network-closed?! network #t))
+266
#t)))
src/sigil/quic/connection/peer/ids.sgladded
@@ -0,0 +1,215 @@
+1
;;; Bounded ownership of authenticated peer connection IDs. Callers assign
+2
;;; acquired IDs to paths and reliably send/acknowledge pending retirements.
+3
(define-library (sigil quic connection peer ids)
+4
(import (sigil struct)
+5
(sigil quic bytes)
+6
(sigil quic error)
+7
(sigil quic frame)
+8
(sigil quic wire))
+9
(export make-peer-id-pool peer-id-pool? peer-id-pool-count
+10
peer-id-pool-receive! peer-id-pool-acquire! peer-id-pool-id
+11
peer-id-pool-sequences peer-id-pool-retire! peer-id-pool-retirements
+12
peer-id-pool-ack! peer-id-pool-close!)
+13
(begin
+14
(define-struct peer-id-pool
+15
(entries mutable: #t)
+16
(limit)
+17
(zero?)
+18
(peer-floor default: 0 mutable: #t)
+19
(retired-floor default: 0 mutable: #t)
+20
(closed? default: #f mutable: #t))
+21
+22
;; Entry: sequence, owned CID, owned reset token/#f, acquired?, retiring?.
+23
;; At most limit active plus twice limit awaiting RETIRE acknowledgment.
+24
(define (new-entry sequence cid token acquired?)
+25
(vector sequence (bytevector-copy cid) (and token (bytevector-copy token)) acquired? #f))
+26
+27
(define (uint? value)
+28
(and (integer? value) (exact? value) (<= 0 value quic-varint-maximum)))
+29
+30
(define (id? cid minimum)
+31
(and (bytevector? cid) (<= minimum (bytevector-length cid) 20)))
+32
+33
(define (token? token)
+34
(and (bytevector? token) (= 16 (bytevector-length token))))
+35
+36
(define (make-peer-id-pool initial
+37
(keys: (limit 2) (token #f) (preferred #f)))
+38
(unless (and (id? initial 0) (uint? limit) (<= 2 limit 8)
+39
(or (not token) (and (> (bytevector-length initial) 0) (token? token)))
+40
(or (not preferred)
+41
(and (> (bytevector-length initial) 0) (pair? preferred)
+42
(id? (car preferred) 1) (token? (cdr preferred))
+43
(not (bytes=? initial (car preferred))))))
+44
(error "QUIC peer IDs: invalid initial ID, limits or authenticated metadata"))
+45
;; Construct only after source-ID / preferred-address / token metadata
+46
;; has been authenticated. The original path already uses sequence zero.
+47
(peer-id-pool entries:
+48
(cons (new-entry 0 initial token #t)
+49
(if preferred (list (new-entry 1 (car preferred) (cdr preferred) #f)) '()))
+50
limit: limit zero?: (= (bytevector-length initial) 0)))
+51
+52
(define (require-open! pool)
+53
(when (peer-id-pool-closed? pool) (error "QUIC peer IDs: pool is closed")))
+54
+55
(define (find-entry entries sequence)
+56
(and (pair? entries)
+57
(if (= sequence (vector-ref (car entries) 0))
+58
(car entries)
+59
(find-entry (cdr entries) sequence))))
+60
+61
(define (count-state entries retiring?)
+62
(let loop ((rest entries) (count 0))
+63
(if (null? rest)
+64
count
+65
(loop (cdr rest)
+66
(+ count (if (eq? (vector-ref (car rest) 4) retiring?) 1 0))))))
+67
+68
(define (peer-id-pool-count pool)
+69
(count-state (peer-id-pool-entries pool) #f))
+70
+71
(define (capacity! pool entries)
+72
(when (or (> (count-state entries #f) (peer-id-pool-limit pool))
+73
(> (count-state entries #t) (* 2 (peer-id-pool-limit pool))))
+74
(raise-quic-error 9 24 "QUIC peer connection ID inventory limit exceeded")))
+75
+76
(define (peer-id-pool-receive! pool frame)
+77
(require-open! pool)
+78
(unless (and (quic-frame? frame) (= 24 (quic-frame-type frame)))
+79
(error "QUIC peer IDs: expected NEW_CONNECTION_ID"))
+80
(when (peer-id-pool-zero? pool)
+81
(raise-quic-error 10 24 "QUIC peer IDs: NEW_CONNECTION_ID after zero-length source ID"))
+82
(let ((sequence (frame-ref frame 'sequence))
+83
(retire (frame-ref frame 'retire-prior-to))
+84
(cid (frame-ref frame 'connection-id))
+85
(token (frame-ref frame 'token)))
+86
(unless (and (uint? sequence) (uint? retire) (<= retire sequence)
+87
(id? cid 1) (token? token))
+88
(raise-quic-error 7 24 "QUIC peer IDs: invalid NEW_CONNECTION_ID fields"))
+89
(let* ((entries (peer-id-pool-entries pool))
+90
(known (find-entry entries sequence))
+91
(floor (max retire (peer-id-pool-peer-floor pool))))
+92
(when (and known
+93
(or (not (bytes=? cid (vector-ref known 1)))
+94
(and (vector-ref known 2)
+95
(not (bytes-timing-safe=? token (vector-ref known 2))))))
+96
(raise-quic-error 10 24 "QUIC peer IDs: conflicting sequence metadata"))
+97
(for-each
+98
(lambda (value)
+99
(when (and (not (= sequence (vector-ref value 0)))
+100
(bytes=? cid (vector-ref value 1)))
+101
(raise-quic-error 10 24 "QUIC peer IDs: connection ID reused with another sequence")))
+102
entries)
+103
;; Plan changes without touching owned entries, so limit errors leave
+104
;; the original path and its inventory intact. RetirePriorTo applies
+105
;; before counting the newly supplied active ID.
+106
(let* ((planned
+107
(map
+108
(lambda (value)
+109
(vector (vector-ref value 0) (vector-ref value 1)
+110
(if (and known (= sequence (vector-ref value 0)) (not (vector-ref value 2)))
+111
(bytevector-copy token)
+112
(vector-ref value 2))
+113
(vector-ref value 3)
+114
(or (vector-ref value 4) (< (vector-ref value 0) floor))))
+115
entries))
+116
(incoming (or (find-entry planned sequence) (new-entry sequence cid token #f))))
+117
(unless known
+118
(vector-set! incoming 4 (< sequence (max floor (peer-id-pool-retired-floor pool))))
+119
(set! planned (append planned (list incoming))))
+120
(capacity! pool planned)
+121
(set-peer-id-pool-entries! pool planned)
+122
(set-peer-id-pool-peer-floor! pool floor)
+123
(for-each
+124
(lambda (value)
+125
(when (vector-ref value 4)
+126
(set-peer-id-pool-retired-floor! pool
+127
(max (peer-id-pool-retired-floor pool) (+ 1 (vector-ref value 0))))))
+128
planned)
+129
(not (vector-ref incoming 4))))))
+130
+131
;; A CID is assigned to at most one path. A failed candidate retires it;
+132
;; releasing it for another address would permit cross-path correlation.
+133
(define (peer-id-pool-acquire! pool)
+134
(require-open! pool)
+135
(let loop ((rest (peer-id-pool-entries pool)))
+136
(and (pair? rest)
+137
(let ((value (car rest)))
+138
(if (or (vector-ref value 3) (vector-ref value 4))
+139
(loop (cdr rest))
+140
(begin
+141
(vector-set! value 3 #t)
+142
(vector-ref value 0)))))))
+143
+144
(define (peer-id-pool-id pool sequence)
+145
(unless (uint? sequence) (error "QUIC peer IDs: invalid sequence"))
+146
(let ((value (find-entry (peer-id-pool-entries pool) sequence)))
+147
(and value (not (vector-ref value 4)) (bytevector-copy (vector-ref value 1)))))
+148
+149
(define (peer-id-pool-sequences pool)
+150
(let loop ((rest (peer-id-pool-entries pool)) (out '()))
+151
(if (null? rest)
+152
(reverse out)
+153
(let ((value (car rest)))
+154
(loop (cdr rest)
+155
(if (vector-ref value 4) out (cons (vector-ref value 0) out)))))))
+156
+157
(define (peer-id-pool-retire! pool sequence)
+158
(require-open! pool)
+159
(unless (uint? sequence) (error "QUIC peer IDs: invalid sequence"))
+160
(let ((value (find-entry (peer-id-pool-entries pool) sequence)))
+161
(unless value (error "QUIC peer IDs: cannot retire an unknown ID"))
+162
(when (peer-id-pool-zero? pool) (error "QUIC peer IDs: cannot retire a zero-length ID"))
+163
(and (not (vector-ref value 4))
+164
(begin
+165
(when (>= (count-state (peer-id-pool-entries pool) #t) (* 2 (peer-id-pool-limit pool)))
+166
(raise-quic-error 9 24 "QUIC peer IDs: too many pending retirements"))
+167
(vector-set! value 4 #t)
+168
(set-peer-id-pool-retired-floor! pool
+169
(max (peer-id-pool-retired-floor pool) (+ sequence 1)))
+170
#t))))
+171
+172
;; Keep all pending retirements until a carrying packet is acknowledged.
+173
;; The connection deduplicates/retransmits these reliable control frames.
+174
(define (peer-id-pool-retirements pool)
+175
(let loop ((rest (peer-id-pool-entries pool)) (out '()))
+176
(if (null? rest)
+177
(reverse out)
+178
(let ((value (car rest)))
+179
(loop (cdr rest)
+180
(if (vector-ref value 4)
+181
(cons (make-quic-frame 25 (list (cons 'sequence (vector-ref value 0)))) out)
+182
out))))))
+183
+184
(define (wipe-token! value)
+185
(let ((token (vector-ref value 2)))
+186
(when token
+187
(do ((index 0 (+ index 1))) ((= index (bytevector-length token)))
+188
(bytevector-u8-set! token index 0)))
+189
(vector-set! value 2 #f)))
+190
+191
;; Only call after validating an ACK for a packet actually carrying RETIRE.
+192
;; A compact local retirement floor prevents forgotten IDs resurrecting.
+193
;; Existing live IDs below that floor are explicit exceptions; previously
+194
;; unseen older IDs are immediately retired instead of assigned to a path.
+195
(define (peer-id-pool-ack! pool sequence)
+196
(require-open! pool)
+197
(unless (uint? sequence) (error "QUIC peer IDs: invalid retirement acknowledgment"))
+198
(let ((value (find-entry (peer-id-pool-entries pool) sequence)))
+199
(and value (vector-ref value 4)
+200
(begin
+201
(wipe-token! value)
+202
(set-peer-id-pool-entries! pool
+203
(let loop ((rest (peer-id-pool-entries pool)) (out '()))
+204
(cond
+205
((null? rest) (reverse out))
+206
((= sequence (vector-ref (car rest) 0)) (loop (cdr rest) out))
+207
(else (loop (cdr rest) (cons (car rest) out))))))
+208
#t))))
+209
+210
(define (peer-id-pool-close! pool)
+211
(unless (peer-id-pool-closed? pool)
+212
(for-each wipe-token! (peer-id-pool-entries pool))
+213
(set-peer-id-pool-entries! pool '())
+214
(set-peer-id-pool-closed?! pool #t))
+215
#t)))
src/sigil/quic/endpoint.sglmodified
@@ -1,5 +1,6 @@
1
;;; Private Q1 UDP adapter. Loopback only until independent security review.
−2
;;; One fixed peer, one connection, bounded work and protected output per step.
+2
;;; One connection, addressed protected output and bounded work per step.
+3
;;; The address codec/bind below remains loopback-only pending shared UDP support.
4
(define-library (sigil quic endpoint)
5
(import (sigil crypto)
6
(sigil struct)
@@ -14,6 +15,7 @@
15
(export open-quic-endpoint quic-endpoint? quic-endpoint-port
16
quic-endpoint-connection quic-endpoint-closed? quic-endpoint-failure
17
quic-endpoint-attach! quic-endpoint-accept! quic-endpoint-accept-retry!
+18
quic-endpoint-peer-address quic-endpoint-take-path-events!
19
quic-endpoint-step!
20
quic-endpoint-close!)
21
(begin
@@ -68,6 +70,7 @@
70
(set-quic-endpoint-peer-port! endpoint peer-port)
71
(set-quic-endpoint-connection! endpoint connection)
72
(guard (exn (else (quic-endpoint-close! endpoint) (raise exn)))
+73
(quic-bind-peer-address! connection (retry-address peer-port))
74
(when (eq? (quic-role connection) 'client) (quic-start! connection (endpoint-now endpoint)))))
75
76
;; The factory receives copied (original-DCID, client-SCID). It constructs
@@ -104,21 +107,62 @@
107
(set-quic-endpoint-retry-manager! endpoint #f)
108
(when manager (close-retry-token-manager! manager))))
109
+110
;; Address bytes are the same canonical loopback-IP/port tuple used by
+111
;; Retry. General numeric IPv4/IPv6 encoding belongs to the shared socket
+112
;; contract; this adapter slice deliberately retains the existing bind.
+113
(define (output-port output)
+114
(let ((address (quic-output-address output)))
+115
(unless (and (= (bytevector-length address) 6)
+116
(= (bytevector-u8-ref address 0) 127)
+117
(= (bytevector-u8-ref address 1) 0)
+118
(= (bytevector-u8-ref address 2) 0)
+119
(= (bytevector-u8-ref address 3) 1))
+120
(error "QUIC endpoint: output has an unsupported address"))
+121
(let ((port (+ (* 256 (bytevector-u8-ref address 4))
+122
(bytevector-u8-ref address 5))))
+123
(unless (port? port 1)
+124
(error "QUIC endpoint: output has an invalid port"))
+125
port)))
+126
+127
(define (quic-endpoint-peer-address endpoint)
+128
(let ((connection (quic-endpoint-connection endpoint)))
+129
(and connection (quic-peer-address connection))))
+130
+131
(define (quic-endpoint-take-path-events! endpoint)
+132
(let ((connection (quic-endpoint-connection endpoint)))
+133
(if connection (quic-take-path-events! connection) '())))
+134
+135
;; Pending entries pair immutable protected output with its own deadline
+136
;; origin. A new batch must not reset an older datagram's blocked age.
137
(define (send-pending! endpoint now)
−108
(let loop ()
−109
(when (pair? (quic-endpoint-pending endpoint))
−110
(let* ((packet (car (quic-endpoint-pending endpoint)))
−111
(sent (udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" (quic-endpoint-peer-port endpoint))))
−112
(cond
−113
((and sent (= sent (bytevector-length packet)))
−114
(set-quic-endpoint-pending! endpoint (cdr (quic-endpoint-pending endpoint)))
−115
(loop))
−116
(sent (error "QUIC endpoint: partial UDP datagram send"))
−117
((not (quic-endpoint-pending-since endpoint))
−118
(set-quic-endpoint-pending-since! endpoint now))
−119
((>= (- now (quic-endpoint-pending-since endpoint)) 1000)
−120
(error "QUIC endpoint: UDP output remained blocked"))))))
−121
(when (null? (quic-endpoint-pending endpoint)) (set-quic-endpoint-pending-since! endpoint #f)))
+138
(let ((connection (quic-endpoint-connection endpoint))
+139
(pending (quic-endpoint-pending endpoint))
+140
(kept '())
+141
(count 0)
+142
(since #f))
+143
(when (> (length pending) 64)
+144
(error "QUIC endpoint: protected send-attempt budget exceeded"))
+145
(for-each
+146
(lambda (entry)
+147
(let ((output (car entry)) (queued-at (cdr entry)))
+148
;; Check before every send attempt, including failed retries.
+149
(when (and (quic-output-valid? connection output)
+150
(< (- now queued-at) 1000))
+151
(let* ((packet (quic-output-bytes output))
+152
(sent (udp-send (quic-endpoint-socket endpoint) packet
+153
"127.0.0.1" (output-port output))))
+154
(cond
+155
((and sent (= sent (bytevector-length packet))) #t)
+156
(sent (error "QUIC endpoint: partial UDP datagram send"))
+157
((< count 32)
+158
(set! kept (cons entry kept))
+159
(set! count (+ count 1))
+160
(set! since (if since (min since queued-at) queued-at))))))))
+161
pending)
+162
;; Overflow is UDP loss: reliable frames retain connection flight/PTO
+163
;; ownership; DATAGRAM data is unreliable. Never reseal or reroute it.
+164
(set-quic-endpoint-pending! endpoint (reverse kept))
+165
(set-quic-endpoint-pending-since! endpoint since)))
166
167
(define (flush! endpoint now)
168
;; An idle close is silent: even previously sealed blocked output dies.
@@ -126,13 +170,22 @@
170
(quic-idle-expired? (quic-endpoint-connection endpoint)))
171
(set-quic-endpoint-pending! endpoint '())
172
(set-quic-endpoint-pending-since! endpoint #f))
−129
(send-pending! endpoint now)
−130
(when (and (null? (quic-endpoint-pending endpoint)) (quic-endpoint-connection endpoint))
−131
(let ((packets (quic-take-datagrams! (quic-endpoint-connection endpoint) now)))
−132
(when (or (> (length packets) 32) (any (lambda (packet) (> (bytevector-length packet) 1200)) packets))
+173
;; Collect a bounded fresh batch even when old-path sends are blocked.
+174
;; At most32 retained plus32 new records are attempted once each; a
+175
;; failed address cannot stop a candidate path's validation controls.
+176
(when (quic-endpoint-connection endpoint)
+177
(let ((packets (quic-take-addressed-datagrams!
+178
(quic-endpoint-connection endpoint) now)))
+179
(when (or (> (length packets) 32)
+180
(any (lambda (output)
+181
(or (not (quic-output? output))
+182
(> (bytevector-length (quic-output-bytes output)) 1200)))
+183
packets))
184
(error "QUIC endpoint: protected output budget exceeded"))
−134
(set-quic-endpoint-pending! endpoint packets)
−135
(send-pending! endpoint now))))
+185
(set-quic-endpoint-pending! endpoint
+186
(append (quic-endpoint-pending endpoint)
+187
(map (lambda (output) (cons output now)) packets)))))
+188
(send-pending! endpoint now))
189
190
;; Initial keys establish packet integrity, not address ownership. Reject
191
;; noise before allocating the connection or claiming its peer-port slot.
@@ -199,6 +252,7 @@
252
(bytes=? (quic-local-id connection) (cdr source-parameter))
253
(bytes=? retry (cdr retry-parameter)))
254
(error "QUIC endpoint: Retry factory connection ID metadata mismatch")))
+255
(quic-bind-peer-address! connection (retry-address peer-port))
256
(set-quic-endpoint-peer-port! endpoint peer-port)
257
(set-quic-endpoint-connection! endpoint connection)
258
(set! transferred? #t))
@@ -229,11 +283,18 @@
283
(let ((connection ((quic-endpoint-acceptor endpoint)
284
(bytevector-copy (quic-header-destination-id header))
285
(bytevector-copy (quic-header-source-id header)))))
−232
(unless (and (quic-connection? connection) (eq? (quic-role connection) 'server)
−233
(not (quic-closed? connection)))
−234
(error "QUIC endpoint: factory must return a fresh server connection"))
−235
(set-quic-endpoint-peer-port! endpoint peer-port)
−236
(set-quic-endpoint-connection! endpoint connection))))))))
+286
(guard (exn
+287
(else
+288
(when (quic-connection? connection)
+289
(quic-abort! connection))
+290
(raise exn)))
+291
(unless (and (quic-connection? connection)
+292
(eq? (quic-role connection) 'server)
+293
(not (quic-closed? connection)))
+294
(error "QUIC endpoint: factory must return a fresh server connection"))
+295
(quic-bind-peer-address! connection (retry-address peer-port))
+296
(set-quic-endpoint-peer-port! endpoint peer-port)
+297
(set-quic-endpoint-connection! endpoint connection)))))))))
298
299
;; Preserve the configured admission port, not the tentative source pin.
300
;; A failure before confirmation releases the slot without closing UDP.
@@ -253,6 +314,16 @@
314
(set-quic-endpoint-connection! endpoint #f)
315
(set-quic-endpoint-peer-port! endpoint (quic-endpoint-accept-port endpoint)))))))
316
+317
;; The configured admission port applies before confirmation. A confirmed
+318
;; server must authenticate candidate paths in the connection; a client
+319
;; keeps its original server-source restriction.
+320
(define (receive-port? endpoint port)
+321
(let ((connection (quic-endpoint-connection endpoint)))
+322
(or (and connection (eq? (quic-role connection) 'server)
+323
(quic-confirmed? connection))
+324
(not (quic-endpoint-peer-port endpoint))
+325
(= port (quic-endpoint-peer-port endpoint)))))
+326
327
(define (receive! endpoint now)
328
;; Out-of-peer noise must not consume the connection's receive budget.
329
;; A separate read ceiling still bounds work under a continuous flood.
@@ -263,8 +334,8 @@
334
(when received
335
(let ((processed? #f))
336
(when (and (equal? (cadr received) "127.0.0.1")
−266
(or (not (quic-endpoint-peer-port endpoint))
−267
(= (caddr received) (quic-endpoint-peer-port endpoint))))
+337
(port? (caddr received) 1)
+338
(receive-port? endpoint (caddr received)))
339
;; Native UDP strings retain byte_length/data; string->utf8
340
;; copies these exact bytes, including NUL and invalid UTF-8.
341
(let ((bytes (string->utf8 (car received))))
@@ -272,7 +343,8 @@
343
(unless (quic-endpoint-connection endpoint) (admit! endpoint bytes (caddr received) now))
344
(when (quic-endpoint-connection endpoint)
345
(set! processed? #t)
−275
(quic-receive! (quic-endpoint-connection endpoint) bytes now)
+346
(quic-receive! (quic-endpoint-connection endpoint) bytes now
+347
address: (retry-address (caddr received)))
348
(reconcile-admission! endpoint now)))))
349
(loop (+ reads 1) (+ admitted (if processed? 1 0)))))))))
350
src/sigil/quic/path/manager.sgladded
@@ -0,0 +1,214 @@
+1
;;; Socket-free selection of a confirmed connection's peer path. The caller
+2
;;; supplies authenticated, replay-checked packet observations, credits each
+3
;;; UDP datagram once, and assigns a distinct peer CID before probing a path.
+4
(define-library (sigil quic path manager)
+5
(import (sigil struct)
+6
(sigil quic bytes)
+7
(sigil quic wire)
+8
(sigil quic path validation))
+9
(export make-path-manager path-manager? path-manager-address path-manager-candidate
+10
path-manager-previous-address
+11
path-manager-observe! path-manager-send! path-manager-challenge!
+12
path-manager-response! path-manager-tick! path-manager-take-events!
+13
path-manager-close!)
+14
(begin
+15
(define-struct path-manager
+16
(role)
+17
(pto)
+18
(now mutable: #t)
+19
(active mutable: #t)
+20
(candidate-state default: #f mutable: #t)
+21
(eligible? default: #f mutable: #t)
+22
(previous default: #f mutable: #t)
+23
(highest default: -1 mutable: #t)
+24
(events default: '() mutable: #t)
+25
(closed? default: #f mutable: #t))
+26
+27
(define (address? address)
+28
(and (bytevector? address) (<= 1 (bytevector-length address) 64)))
+29
+30
(define (make-path-manager address role now (keys: (pto 1000) (highest -1)))
+31
(unless (and (memq role '(client server)) (integer? highest) (exact? highest)
+32
(<= -1 highest quic-varint-maximum))
+33
(error "QUIC paths: invalid role or packet-number baseline"))
+34
;; The caller has confirmed the handshake and authenticated the original
+35
;; address, including the original path's minimum 1200-byte datagram.
+36
(path-manager role: role pto: pto now: now highest: highest
+37
active: (make-path-validation address now pto: pto validated?: #t mtu?: #t)))
+38
+39
(define (require-open! manager)
+40
(when (path-manager-closed? manager) (error "QUIC paths: manager is closed")))
+41
+42
(define (same? path address)
+43
(and path (bytes=? address (path-validation-address path))))
+44
+45
(define (path-manager-address manager)
+46
(and (path-manager-active manager) (path-validation-address (path-manager-active manager))))
+47
+48
(define (path-manager-candidate manager)
+49
(and (path-manager-candidate-state manager)
+50
(path-validation-address (path-manager-candidate-state manager))))
+51
+52
(define (path-manager-previous-address manager)
+53
(and (path-manager-previous manager)
+54
(path-validation-address (path-manager-previous manager))))
+55
+56
;; Events own their address bytes. Keep the newest sixteen observations;
+57
;; an application that does not drain diagnostics cannot grow path state.
+58
(define (event! manager kind path reason)
+59
(let ((events (path-manager-events manager)))
+60
(set-path-manager-events! manager
+61
(append (if (= (length events) 16) (cdr events) events)
+62
(list (vector kind (path-validation-address path) reason))))))
+63
+64
(define (cancel-candidate! manager reason)
+65
(let ((path (path-manager-candidate-state manager)))
+66
(when path
+67
(event! manager 'path-failed path reason)
+68
(path-validation-close! path)
+69
(set-path-manager-candidate-state! manager #f)
+70
(set-path-manager-eligible?! manager #f))))
+71
+72
(define (close-previous! manager)
+73
(when (path-manager-previous manager)
+74
(path-validation-close! (path-manager-previous manager))
+75
(set-path-manager-previous! manager #f)))
+76
+77
(define (probe-previous! manager)
+78
(close-previous! manager)
+79
;; Fresh reachability exchange on the old path mitigates forwarding.
+80
;; It retains that path's previously established amplification exemption.
+81
(set-path-manager-previous! manager
+82
(make-path-validation (path-manager-address manager) (path-manager-now manager)
+83
pto: (path-manager-pto manager) validated?: #t)))
+84
+85
(define (path-manager-tick! manager now)
+86
(require-open! manager)
+87
(unless (and (integer? now) (exact? now) (>= now (path-manager-now manager)))
+88
(error "QUIC paths: time must be monotonic exact milliseconds"))
+89
(set-path-manager-now! manager now)
+90
(path-validation-tick! (path-manager-active manager) now)
+91
(when (and (path-manager-candidate-state manager)
+92
(path-validation-tick! (path-manager-candidate-state manager) now))
+93
(cancel-candidate! manager 'validation-timeout))
+94
(when (and (path-manager-previous manager)
+95
(path-validation-tick! (path-manager-previous manager) now))
+96
(event! manager 'path-failed (path-manager-previous manager) 'previous-validation-timeout)
+97
(close-previous! manager))
+98
#t)
+99
+100
(define (promote! manager)
+101
(let ((path (path-manager-candidate-state manager)))
+102
(and path (path-manager-eligible? manager) (path-validation-mtu-valid? path)
+103
(begin
+104
(path-validation-close! (path-manager-active manager))
+105
(set-path-manager-active! manager path)
+106
(set-path-manager-candidate-state! manager #f)
+107
(set-path-manager-eligible?! manager #f)
+108
(event! manager 'path-validated path 'active)
+109
#t))))
+110
+111
(define (path-manager-observe! manager address size number probing? now)
+112
(unless (and (address? address) (integer? size) (exact? size) (<= 0 size 65527)
+113
(integer? number) (exact? number) (<= 0 number quic-varint-maximum)
+114
(boolean? probing?))
+115
(error "QUIC paths: invalid authenticated observation"))
+116
(path-manager-tick! manager now)
+117
(let* ((active (path-manager-active manager))
+118
(candidate (path-manager-candidate-state manager))
+119
(newest? (and (not probing?) (> number (path-manager-highest manager)))))
+120
(cond
+121
((same? active address)
+122
(path-validation-credit! active size now)
+123
(when newest?
+124
(set-path-manager-highest! manager number)
+125
(when candidate
+126
(when (path-manager-eligible? manager) (close-previous! manager))
+127
(cancel-candidate! manager 'returned-to-active)))
+128
#t)
+129
;; Preferred-server-address migration is not initiated by this
+130
;; manager. A client must discard unknown server source addresses.
+131
((eq? (path-manager-role manager) 'client) #f)
+132
((same? candidate address)
+133
(when newest?
+134
(set-path-manager-highest! manager number)
+135
(unless (path-manager-eligible? manager)
+136
;; Probe-only observations cannot consume the later migration's
+137
;; validation timer. Discard their credit conservatively.
+138
(path-validation-close! candidate)
+139
(set! candidate (make-path-validation address now pto: (path-manager-pto manager)))
+140
(set-path-manager-candidate-state! manager candidate)
+141
(set-path-manager-eligible?! manager #t)
+142
(probe-previous! manager)))
+143
(path-validation-credit! candidate size now))
+144
((or newest? (and probing? (not candidate)))
+145
(cancel-candidate! manager 'superseded)
+146
(let ((path (make-path-validation address now pto: (path-manager-pto manager))))
+147
(set-path-manager-candidate-state! manager path)
+148
(set-path-manager-eligible?! manager newest?)
+149
(when newest?
+150
(set-path-manager-highest! manager number)
+151
(probe-previous! manager))
+152
(event! manager 'path-changed path (if newest? 'non-probing 'probing))
+153
(path-validation-credit! path size now)))
+154
(else #f))))
+155
+156
;; Reserve every protected output before sealing. Non-probing application
+157
;; output always uses path-manager-address; candidate sends are controls.
+158
(define (path-manager-send! manager address size now)
+159
(unless (and (address? address) (integer? size) (exact? size) (<= 1 size 1200))
+160
(error "QUIC paths: invalid output address or datagram size"))
+161
(path-manager-tick! manager now)
+162
(let ((path
+163
(find (lambda (value) (same? value address))
+164
(list (path-manager-active manager) (path-manager-candidate-state manager)
+165
(path-manager-previous manager)))))
+166
(and path (path-validation-send! path size now))))
+167
+168
;; The caller first assigns the destination CID for ADDRESS. A returned
+169
;; token reserves exactly 1200 wire bytes; packet loss gets a fresh token
+170
;; after PTO, never transport retransmission of an old challenge frame.
+171
(define (path-manager-challenge! manager address now)
+172
(unless (address? address) (error "QUIC paths: invalid challenge address"))
+173
(path-manager-tick! manager now)
+174
(let ((candidate (path-manager-candidate-state manager))
+175
(previous (path-manager-previous manager)))
+176
(cond
+177
((and (path-manager-eligible? manager) (same? candidate address))
+178
(path-validation-challenge! candidate 1200 now))
+179
((same? previous address) (path-validation-challenge! previous 1200 now))
+180
(else #f))))
+181
+182
;; Match across attempts rather than the response packet's arrival path.
+183
(define (path-manager-response! manager data now)
+184
(unless (and (bytevector? data) (= 8 (bytevector-length data)))
+185
(error "QUIC paths: invalid response data"))
+186
(path-manager-tick! manager now)
+187
(let ((candidate (path-manager-candidate-state manager))
+188
(previous (path-manager-previous manager))
+189
(matched? #f))
+190
(when (and candidate (path-validation-response! candidate data now))
+191
(set! matched? #t)
+192
(promote! manager))
+193
(when (and previous (path-validation-response! previous data now))
+194
(set! matched? #t)
+195
(event! manager 'path-validated previous 'previous)
+196
(close-previous! manager))
+197
matched?))
+198
+199
(define (path-manager-take-events! manager)
+200
(let ((events (path-manager-events manager)))
+201
(set-path-manager-events! manager '())
+202
events))
+203
+204
(define (path-manager-close! manager)
+205
(unless (path-manager-closed? manager)
+206
(when (path-manager-candidate-state manager)
+207
(path-validation-close! (path-manager-candidate-state manager)))
+208
(path-validation-close! (path-manager-active manager))
+209
(close-previous! manager)
+210
(set-path-manager-active! manager #f)
+211
(set-path-manager-candidate-state! manager #f)
+212
(set-path-manager-events! manager '())
+213
(set-path-manager-closed?! manager #t))
+214
#t)))
src/sigil/quic/path/validation.sgladded
@@ -0,0 +1,179 @@
+1
;;; One bounded RFC 9000 validation attempt, independent of sockets and routing.
+2
;;; The connection supplies authenticated observations and reserves every output
+3
;;; datagram here before sealing it. Address keys are canonical caller-owned bytes.
+4
(define-library (sigil quic path validation)
+5
(import (sigil struct)
+6
(sigil crypto)
+7
(sigil quic bytes))
+8
(export make-path-validation path-validation? path-validation-address
+9
path-validation-status path-validation-address-valid? path-validation-mtu-valid?
+10
path-validation-credit! path-validation-send! path-validation-challenge!
+11
path-validation-response! path-validation-tick! path-validation-close!)
+12
(begin
+13
(define-struct path-validation
+14
(key)
+15
(pto)
+16
(now mutable: #t)
+17
(deadline mutable: #t)
+18
(next-probe mutable: #t)
+19
(address-valid? default: #f mutable: #t)
+20
(mtu? default: #f mutable: #t)
+21
(received default: 0 mutable: #t)
+22
(sent default: 0 mutable: #t)
+23
(phase-probes default: 0 mutable: #t)
+24
(challenges default: '() mutable: #t)
+25
(failed? default: #f mutable: #t)
+26
(closed? default: #f mutable: #t))
+27
+28
(define (time? now)
+29
(and (integer? now) (exact? now) (>= now 0)))
+30
+31
(define (make-path-validation address now
+32
(keys: (pto 1000) (validated? #f) (mtu? #f)))
+33
(unless (and (bytevector? address) (<= 1 (bytevector-length address) 64)
+34
(time? now) (integer? pto) (exact? pto) (<= 1000 pto 60000)
+35
(boolean? validated?) (boolean? mtu?) (or validated? (not mtu?)))
+36
(error "QUIC path: invalid address, time, PTO or validation state"))
+37
(path-validation key: (bytevector-copy address) now: now pto: pto
+38
deadline: (+ now (* 3 pto)) next-probe: now
+39
address-valid?: validated? mtu?: mtu?))
+40
+41
(define (path-validation-address path)
+42
(bytevector-copy (path-validation-key path)))
+43
+44
(define (path-validation-status path)
+45
(cond
+46
((path-validation-closed? path) 'closed)
+47
((path-validation-failed? path) 'failed)
+48
((and (path-validation-address-valid? path) (path-validation-mtu? path)) 'validated)
+49
(else 'validating)))
+50
+51
(define (path-validation-mtu-valid? path)
+52
(eq? (path-validation-status path) 'validated))
+53
+54
(define (require-open! path)
+55
(when (path-validation-closed? path) (error "QUIC path: validation is closed")))
+56
+57
;; These owned bytevectors are challenge payloads, not native key handles.
+58
;; Clear them before releasing the attempt's references.
+59
(define (wipe! bytes)
+60
(do ((index 0 (+ index 1))) ((= index (bytevector-length bytes)))
+61
(bytevector-u8-set! bytes index 0)))
+62
+63
(define (wipe-challenges! path)
+64
(for-each (lambda (entry) (wipe! (vector-ref entry 0))) (path-validation-challenges path))
+65
(set-path-validation-challenges! path '()))
+66
+67
;; Expiry is independent of incoming ACKs, new data and unknown responses.
+68
(define (path-validation-tick! path now)
+69
(require-open! path)
+70
(unless (and (time? now) (>= now (path-validation-now path)))
+71
(error "QUIC path: time must be monotonic exact milliseconds"))
+72
(set-path-validation-now! path now)
+73
(when (and (eq? (path-validation-status path) 'validating)
+74
(>= now (path-validation-deadline path)))
+75
(set-path-validation-failed?! path #t)
+76
(set-path-validation-address-valid?! path #f)
+77
(set-path-validation-mtu?! path #f)
+78
(wipe-challenges! path))
+79
(path-validation-failed? path))
+80
+81
;; The credit caller must have authenticated this path's packet. One UDP
+82
;; datagram is credited once, including coalesced packets. Saturation is
+83
;; conservative: it can only reduce available unvalidated-path credit.
+84
(define (path-validation-credit! path size now)
+85
(unless (and (integer? size) (exact? size) (<= 0 size 65527))
+86
(error "QUIC path: invalid received datagram size"))
+87
(path-validation-tick! path now)
+88
(and (not (path-validation-failed? path))
+89
(begin
+90
(set-path-validation-received! path
+91
(min 1073741824 (+ (path-validation-received path) size)))
+92
#t)))
+93
+94
(define (wire-size! size)
+95
(unless (and (integer? size) (exact? size) (<= 1 size 1200))
+96
(error "QUIC path: invalid protected datagram size")))
+97
+98
(define (send-allowed? path size)
+99
(and (not (path-validation-failed? path))
+100
(or (path-validation-address-valid? path)
+101
(<= (+ (path-validation-sent path) size) (* 3 (path-validation-received path))))))
+102
+103
;; This is a conservative reservation. A subsequently blocked/dropped UDP
+104
;; send does not refund credit; previously sealed output cannot spend twice.
+105
(define (path-validation-send! path size now)
+106
(wire-size! size)
+107
(path-validation-tick! path now)
+108
(and (send-allowed? path size)
+109
(begin
+110
(set-path-validation-sent! path (+ (path-validation-sent path) size))
+111
#t)))
+112
+113
(define (fresh-challenge path)
+114
(let loop ((attempt 0))
+115
(when (= attempt 4) (error "QUIC path: repeated challenge entropy"))
+116
(let ((data (random-bytes 8)))
+117
(unless (and (bytevector? data) (= (bytevector-length data) 8))
+118
(error "QUIC path: challenge entropy unavailable"))
+119
(if (any (lambda (entry) (bytes=? data (vector-ref entry 0)))
+120
(path-validation-challenges path))
+121
(begin (wipe! data) (loop (+ attempt 1)))
+122
data))))
+123
+124
;; Return one owned challenge or #f. The caller uses exactly SIZE wire
+125
;; bytes on this path. At most three probes per address/MTU phase, six
+126
;; remembered challenges total; retries use fresh unpredictable payloads.
+127
(define (path-validation-challenge! path size now)
+128
(wire-size! size)
+129
(path-validation-tick! path now)
+130
(and (eq? (path-validation-status path) 'validating)
+131
(< (path-validation-phase-probes path) 3)
+132
(>= now (path-validation-next-probe path))
+133
(or (not (path-validation-address-valid? path)) (= size 1200))
+134
(send-allowed? path size)
+135
(let ((data (fresh-challenge path)))
+136
(set-path-validation-sent! path (+ (path-validation-sent path) size))
+137
(set-path-validation-phase-probes! path (+ 1 (path-validation-phase-probes path)))
+138
(set-path-validation-next-probe! path (+ now (path-validation-pto path)))
+139
(set-path-validation-challenges! path
+140
(cons (vector data (= size 1200) #f) (path-validation-challenges path)))
+141
(bytevector-copy data))))
+142
+143
;; An authenticated matching response on ANY incoming path validates the
+144
;; path on which this challenge was sent (RFC 9000 8.2.3). The caller finds
+145
;; the outstanding attempt by payload, not by the response's source address.
+146
(define (path-validation-response! path data now)
+147
(unless (and (bytevector? data) (= (bytevector-length data) 8))
+148
(error "QUIC path: invalid response payload"))
+149
(path-validation-tick! path now)
+150
(and (eq? (path-validation-status path) 'validating)
+151
(let ((entry (find (lambda (candidate)
+152
(and (not (vector-ref candidate 2))
+153
(bytes-timing-safe=? data (vector-ref candidate 0))))
+154
(path-validation-challenges path))))
+155
(and entry
+156
(begin
+157
(vector-set! entry 2 #t)
+158
(cond
+159
((vector-ref entry 1)
+160
(set-path-validation-address-valid?! path #t)
+161
(set-path-validation-mtu?! path #t)
+162
(wipe-challenges! path)
+163
'validated)
+164
((not (path-validation-address-valid? path))
+165
(set-path-validation-address-valid?! path #t)
+166
;; A short probe proves reachability, not the 1200-byte
+167
;; MTU. Start one bounded, separately paced MTU phase.
+168
(set-path-validation-phase-probes! path 0)
+169
(set-path-validation-next-probe! path now)
+170
(set-path-validation-deadline! path (+ now (* 3 (path-validation-pto path))))
+171
'address-validated)
+172
(else #f)))))))
+173
+174
(define (path-validation-close! path)
+175
(unless (path-validation-closed? path)
+176
(wipe-challenges! path)
+177
(set-path-validation-address-valid?! path #f)
+178
(set-path-validation-mtu?! path #f)
+179
(set-path-validation-closed?! path #t)))))
test/test-addressed-output.sgladded
@@ -0,0 +1,84 @@
+1
(import (sigil test)
+2
(sigil crypto)
+3
(sigil quic bytes)
+4
(sigil quic connection)
+5
(sigil quic tls))
+6
+7
(define (ao-with-client procedure)
+8
(let ((key (ecdsa-p256-generate-keypair)) (client #f))
+9
(dynamic-wind
+10
(lambda () #t)
+11
(lambda ()
+12
(set! client
+13
(make-quic-client (list #u8(113 49)) 'pinned-key '()
+14
(make-tls-pin-verifier (cdr key)) local-id: #u8(1)
+15
destination-id: #u8(10 11 12 13 14 15 16 17)))
+16
(procedure client))
+17
(lambda ()
+18
(when client (quic-abort! client))
+19
(do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))
+20
(bytevector-u8-set! (car key) index 0))))))
+21
+22
(define (ao-first! client)
+23
(quic-bind-peer-address! client #u8(42))
+24
(quic-start! client 0)
+25
(let ((packets (quic-take-addressed-datagrams! client 0)))
+26
(unless (= 1 (length packets)) (error "Expected one addressed Initial in this fixture"))
+27
(car packets)))
+28
+29
(test-group "Protected datagram destination ownership"
+30
(test "addressed-output: original address is bound once and owned before handshake start"
+31
(ao-with-client
+32
(lambda (client)
+33
(let ((address (make-bytevector 1 42)))
+34
(quic-bind-peer-address! client address)
+35
(bytevector-u8-set! address 0 43)
+36
(let ((copy (quic-peer-address client)))
+37
(bytevector-u8-set! copy 0 44))
+38
(assert-true (bytes=? #u8(42) (quic-peer-address client)))
+39
(assert-error (quic-bind-peer-address! client #u8(99)))
+40
(quic-start! client 0)
+41
(assert-error (quic-bind-peer-address! client #u8(99)))))))
+42
(test "addressed-output: raw and addressed output contracts cannot silently drop destination metadata"
+43
(ao-with-client
+44
(lambda (client)
+45
(assert-error (quic-take-addressed-datagrams! client 0))
+46
(quic-bind-peer-address! client #u8(42))
+47
(assert-error (quic-take-datagrams! client 0))
+48
(quic-start! client 0)
+49
(let ((packets (quic-take-addressed-datagrams! client 0)))
+50
(assert-true
+51
(and (= 1 (length packets)) (quic-output? (car packets))
+52
(quic-output-valid? client (car packets))
+53
(= 1200 (bytevector-length (quic-output-bytes (car packets))))
+54
(bytes=? #u8(42) (quic-output-address (car packets)))
+55
(bytes=? #u8(10 11 12 13 14 15 16 17) (quic-output-id (car packets)))))))))
+56
(test "addressed-output: returned bytes address and CID cannot mutate sealed output ownership"
+57
(ao-with-client
+58
(lambda (client)
+59
(let* ((output (ao-first! client)) (bytes (quic-output-bytes output))
+60
(original (bytevector-copy bytes)) (address (quic-output-address output))
+61
(cid (quic-output-id output)))
+62
(bytevector-u8-set! bytes 0 0)
+63
(bytevector-u8-set! address 0 0)
+64
(bytevector-u8-set! cid 0 0)
+65
(assert-true
+66
(and (quic-output-valid? client output) (bytes=? original (quic-output-bytes output))
+67
(bytes=? #u8(42) (quic-output-address output))
+68
(bytes=? #u8(10 11 12 13 14 15 16 17) (quic-output-id output))))))))
+69
(test "addressed-output: closing invalidates queued ordinary traffic and freezes close destination"
+70
(ao-with-client
+71
(lambda (client)
+72
(let ((old (ao-first! client)))
+73
(quic-close! client)
+74
(let ((packets (quic-take-addressed-datagrams! client 0)))
+75
(assert-true
+76
(and (not (quic-output-valid? client old)) (= 1 (length packets))
+77
(quic-closing? client) (quic-output-valid? client (car packets))
+78
(bytes=? #u8(42) (quic-output-address (car packets)))
+79
(bytes=? (quic-output-id old) (quic-output-id (car packets)))))
+80
(quic-abort! client)
+81
(assert-true
+82
(and (not (quic-output-valid? client old))
+83
(not (quic-output-valid? client (car packets)))
+84
(null? (quic-take-addressed-datagrams! client 1))))))))))
test/test-connection-network.sgladded
@@ -0,0 +1,233 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic wire)
+4
(sigil quic frame)
+5
(sigil quic error)
+6
(sigil quic connection network))
+7
+8
;; These are explicitly trusted, post-handshake fixtures for the socket-free
+9
;; owner. TLS authentication and packet metadata hooks are tested separately.
+10
(define (net-local-parameters)
+11
(list (cons 0 #u8(10 11 12 13 14 15 16 17)) (cons 15 #u8(1))))
+12
+13
(define (net-peer-parameters)
+14
(list (cons 15 #u8(2))))
+15
+16
(define (net-with-owner procedure)
+17
(let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
+18
(net-local-parameters) (net-peer-parameters))))
+19
(dynamic-wind
+20
(lambda () #t)
+21
(lambda () (procedure network))
+22
(lambda () (network-close! network)))))
+23
+24
(define (net-new sequence retire)
+25
(make-quic-frame 24
+26
(list (cons 'sequence sequence) (cons 'retire-prior-to retire)
+27
(cons 'connection-id (bytevector (+ sequence 2)))
+28
(cons 'token (make-bytevector 16 0)))))
+29
+30
(define (net-observe! network address number now)
+31
(network-observe! network address 1200 number (list (make-quic-frame 1 '())) now))
+32
+33
(define (net-retiring network)
+34
(map (lambda (frame) (frame-ref frame 'sequence)) (network-retirements network)))
+35
+36
(define (net-destination-is? network address sequence cid)
+37
(let ((destination (network-destination network address)))
+38
(and destination (= sequence (vector-ref destination 0))
+39
(bytes=? cid (vector-ref destination 1)))))
+40
+41
(define (net-retirement-error? procedure)
+42
(guard (exn
+43
(else (and (quic-error? exn) (= 10 (quic-error-code exn))
+44
(= 25 (quic-error-frame-type exn)))))
+45
(procedure)
+46
#f))
+47
+48
(define (net-binding-error? procedure)
+49
(guard (exn
+50
(else (and (quic-error? exn) (= 10 (quic-error-code exn))
+51
(= 24 (quic-error-frame-type exn)))))
+52
(procedure)
+53
#f))
+54
+55
(test-group "Confirmed connection CID and path ownership"
+56
(test "connection-network: initial path owns peer sequence zero and copied destination metadata"
+57
(net-with-owner
+58
(lambda (network)
+59
(let ((destination (network-destination network #u8(10))))
+60
(assert-true
+61
(and destination (= 0 (vector-ref destination 0))
+62
(bytes=? #u8(2) (vector-ref destination 1))
+63
(= 0 (network-route network #u8(1)))
+64
(not (network-destination network #u8(11)))))
+65
(bytevector-u8-set! (vector-ref destination 1) 0 9)
+66
(assert-true (bytes=? #u8(2) (vector-ref (network-destination network #u8(10)) 1)))))))
+67
(test "connection-network: local advertisements route only after commit and retire against actual packet DCID"
+68
(net-with-owner
+69
(lambda (network)
+70
(let ((frame (network-issue-id! network #u8(4) (make-bytevector 16 0))))
+71
(assert-true
+72
(and frame (not (network-route network #u8(4)))
+73
(net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(1))))
+74
(network-check-id! network frame) (network-commit-id! network frame)
+75
(= 1 (network-route network #u8(4)))
+76
(net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(4))))
+77
(network-retire-local-id! network 0 #u8(4))
+78
(not (network-route network #u8(1)))
+79
(network-retire-local-id! network 1 #u8(1))
+80
(not (network-check-id! network frame))
+81
(not (network-commit-id! network frame))
+82
(not (network-route network #u8(4)))))))))
+83
(test "connection-network: missing peer spare pauses probes without charging a candidate"
+84
(net-with-owner
+85
(lambda (network)
+86
(net-observe! network #u8(11) 1 0)
+87
(assert-true
+88
(and (not (network-challenge! network #u8(11) 0))
+89
(not (network-reserve-send! network #u8(11) 1 0))
+90
(bytes=? #u8(10) (network-address network))
+91
(network-peer-id! network (net-new 1 0))
+92
(let ((probe (network-challenge! network #u8(11) 0)))
+93
(and probe (= 1 (vector-ref probe 1))
+94
(bytes=? #u8(3) (vector-ref probe 2))
+95
(bytes=? #u8(10) (network-address network)))))))))
+96
(test "connection-network: validated migration retains the old CID until its independent probe finishes"
+97
(net-with-owner
+98
(lambda (network)
+99
(network-peer-id! network (net-new 1 0))
+100
(net-observe! network #u8(11) 1 0)
+101
(let ((old (network-challenge! network #u8(10) 0))
+102
(next (network-challenge! network #u8(11) 0)))
+103
(assert-true
+104
(and old next (network-response! network (vector-ref next 3) 1)
+105
(bytes=? #u8(11) (network-address network))
+106
(net-destination-is? network #u8(11) 1 #u8(3))
+107
(net-destination-is? network #u8(10) 0 #u8(2))
+108
(null? (net-retiring network))
+109
(network-response! network (vector-ref old 3) 1)
+110
(not (network-destination network #u8(10)))
+111
(equal? '(0) (net-retiring network))
+112
(equal? '(0) (net-retiring network))
+113
(network-retirement-ack! network 0)
+114
(null? (net-retiring network))))))))
+115
(test "connection-network: candidate timeout retires its CID and preserves the working path"
+116
(net-with-owner
+117
(lambda (network)
+118
(network-peer-id! network (net-new 1 0))
+119
(net-observe! network #u8(11) 1 0)
+120
(network-tick! network 3000)
+121
(assert-true
+122
(and (not (network-candidate network))
+123
(not (network-destination network #u8(11)))
+124
(equal? '(1) (net-retiring network))
+125
(net-destination-is? network #u8(10) 0 #u8(2))
+126
(network-peer-id! network (net-new 2 0))
+127
(net-observe! network #u8(12) 2 3001)
+128
(let ((probe (network-challenge! network #u8(12) 3001)))
+129
(and probe (= 2 (vector-ref probe 1))
+130
(bytes=? #u8(4) (vector-ref probe 2)))))))))
+131
(test "connection-network: superseded candidate CID is retired rather than reassigned"
+132
(net-with-owner
+133
(lambda (network)
+134
(network-peer-id! network (net-new 1 0))
+135
(net-observe! network #u8(11) 1 0)
+136
(net-observe! network #u8(12) 2 1)
+137
(assert-true
+138
(and (not (network-destination network #u8(11)))
+139
(not (network-destination network #u8(12)))
+140
(equal? '(1) (net-retiring network))
+141
(network-peer-id! network (net-new 2 0))
+142
(net-destination-is? network #u8(12) 2 #u8(4))
+143
(net-destination-is? network #u8(10) 0 #u8(2)))))))
+144
(test "connection-network: peer RetirePriorTo replaces the current CID before further output"
+145
(net-with-owner
+146
(lambda (network)
+147
(assert-true
+148
(and (network-peer-id! network (net-new 1 1))
+149
(net-destination-is? network #u8(10) 1 #u8(3))
+150
(equal? '(0) (net-retiring network))
+151
(bytes=? #u8(10) (network-address network))
+152
(null? (network-take-events! network)))))))
+153
(test "connection-network: only non-probing decoded frames authorize candidate validation"
+154
(net-with-owner
+155
(lambda (network)
+156
(network-peer-id! network (net-new 1 0))
+157
(network-observe! network #u8(11) 1200 10 (list (net-new 1 0)) 0)
+158
(assert-true
+159
(and (not (network-challenge! network #u8(11) 0))
+160
(bytes=? #u8(10) (network-address network))
+161
(net-observe! network #u8(11) 1 1)
+162
(network-challenge! network #u8(11) 1))))))
+163
(test "connection-network: constructor rejects mismatched IDs and unsupported local advertisements"
+164
(assert-error
+165
(make-connection-network #u8(1) #u8(9) 'server #u8(10) 0
+166
(net-local-parameters) (net-peer-parameters)))
+167
(assert-error
+168
(make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
+169
(cons (cons 14 (encode-varint 9)) (net-local-parameters)) (net-peer-parameters)))
+170
(let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
+171
(net-local-parameters) (cons (cons 14 (encode-varint 100)) (net-peer-parameters)))))
+172
(dynamic-wind
+173
(lambda () #t)
+174
(lambda () (assert-equal 8 (network-local-limit network)))
+175
(lambda () (network-close! network)))))
+176
(test "connection-network: per-path reservations cannot borrow original path credit"
+177
(net-with-owner
+178
(lambda (network)
+179
(network-peer-id! network (net-new 1 0))
+180
(network-observe! network #u8(11) 10 1 (list (make-quic-frame 1 '())) 0)
+181
(assert-true
+182
(and (network-reserve-send! network #u8(10) 1200 0)
+183
(network-reserve-send! network #u8(11) 30 0)
+184
(not (network-reserve-send! network #u8(11) 1 0))
+185
(not (network-challenge! network #u8(11) 0))
+186
(bytes=? #u8(10) (network-address network)))))))
+187
(test "connection-network: preferred metadata installs owned sequence one on both directions"
+188
(let* ((preferred (make-bytevector 42 0)) (server #f) (client #f))
+189
(bytevector-u8-set! preferred 24 1)
+190
(bytevector-u8-set! preferred 25 99)
+191
(dynamic-wind
+192
(lambda () #t)
+193
(lambda ()
+194
(set! server
+195
(make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
+196
(cons (cons 13 preferred) (net-local-parameters)) (net-peer-parameters)))
+197
(set! client
+198
(make-connection-network #u8(2) #u8(1) 'client #u8(10) 0
+199
(net-peer-parameters) (cons (cons 13 preferred) (net-local-parameters))))
+200
(bytevector-u8-set! preferred 25 88)
+201
(assert-true
+202
(and (= 1 (network-route server #u8(99))) (= 2 (network-local-count server))
+203
(net-binding-error?
+204
(lambda ()
+205
(network-peer-id! client
+206
(make-quic-frame 24
+207
(list (cons 'sequence 1) (cons 'retire-prior-to 0)
+208
(cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 1)))))))
+209
(network-peer-id! client
+210
(make-quic-frame 24
+211
(list (cons 'sequence 1) (cons 'retire-prior-to 0)
+212
(cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 0)))))
+213
(net-destination-is? client #u8(10) 0 #u8(1)))))
+214
(lambda ()
+215
(when server (network-close! server))
+216
(when client (network-close! client))))))
+217
(test "connection-network: close clears path CID and control ownership and rejects mutations"
+218
(net-with-owner
+219
(lambda (network)
+220
(network-peer-id! network (net-new 1 0))
+221
(net-observe! network #u8(11) 1 0)
+222
(network-close! network)
+223
(network-close! network)
+224
(assert-false (network-address network))
+225
(assert-false (network-candidate network))
+226
(assert-false (network-destination network #u8(10)))
+227
(assert-false (network-route network #u8(1)))
+228
(assert-equal '() (network-retirements network))
+229
(assert-equal '() (network-route-lengths network))
+230
(assert-equal 0 (network-local-count network))
+231
(assert-error (network-peer-id! network (net-new 2 0)))
+232
(assert-error (net-observe! network #u8(12) 2 1))
+233
(assert-error (network-issue-id! network #u8(4) (make-bytevector 16 0)))))))
test/test-endpoint-network.sgladded
@@ -0,0 +1,205 @@
+1
(import (sigil test)
+2
(sigil io)
+3
(sigil socket)
+4
(sigil time)
+5
(sigil crypto)
+6
(sigil quic wire)
+7
(sigil quic bytes)
+8
(sigil quic frame)
+9
(sigil quic packet)
+10
(sigil quic connection)
+11
(sigil quic stream)
+12
(sigil quic endpoint))
+13
+14
(define en-original #u8(1 2 3 4 5 6 7 8))
+15
(define en-client-id #u8(11 12 13 14 15 16 17 18))
+16
+17
;; Public UDP fixture: client, endpoint, primary/alternate sockets, client
+18
;; stream, returned server/stream, factory calls/IDs, clock origin, signing key.
+19
;; All sockets and candidate connections remain owned even on assertion errors.
+20
(define (en-with-pair procedure
+21
(keys: (retry? #t) (pin? #f) (defer? #f) (lifetime 10000) (bad-factory? #f)))
+22
(let ((key (ecdsa-p256-generate-keypair)) (client #f) (endpoint #f)
+23
(primary #f) (alternate #f) (pair #f))
+24
(dynamic-wind
+25
(lambda () #t)
+26
(lambda ()
+27
(set! client
+28
(make-quic-client (list #u8(113 49)) "example.test"
+29
(append (stream-transport-parameters 'client)
+30
(list (cons 32 (encode-varint 1200))))
+31
(lambda (certificates name)
+32
(and (equal? certificates (list #u8(48 1 0))) (cdr key)))
+33
destination-id: en-original local-id: en-client-id defer-key-share?: defer?))
+34
(set! endpoint (open-quic-endpoint))
+35
(set! primary (udp-socket))
+36
(set! alternate (udp-socket))
+37
(for-each
+38
(lambda (socket)
+39
(unless (and socket (udp-bind socket 0 "127.0.0.1")
+40
(socket-set-non-blocking! socket #t))
+41
(error "endpoint Retry fixture: socket setup failed")))
+42
(list primary alternate))
+43
(set! pair (vector client endpoint primary alternate (make-quic-stream client)
+44
#f #f 0 #f (current-jiffy) key #f 0))
+45
(let ((factory
+46
(lambda (original source retry)
+47
(vector-set! pair 7 (+ 1 (vector-ref pair 7)))
+48
(vector-set! pair 8 (list (bytevector-copy original) (bytevector-copy source)
+49
(and retry (bytevector-copy retry))))
+50
(let ((server
+51
(make-quic-server (list #u8(113 49))
+52
(append (stream-transport-parameters 'server)
+53
(list (cons 32 (encode-varint 1200))))
+54
(list #u8(48 1 0)) (car key)
+55
original source retry-source-id: (and (not bad-factory?) retry))))
+56
(vector-set! pair 5 server)
+57
(vector-set! pair 6 (make-quic-stream server))
+58
server))))
+59
(if retry?
+60
(quic-endpoint-accept-retry! endpoint factory
+61
peer-port: (and pin? (cadr (socket-local-address primary))) token-lifetime: lifetime)
+62
(quic-endpoint-accept! endpoint
+63
(lambda (original source) (factory original source #f)))))
+64
(quic-bind-peer-address! client (en-address (quic-endpoint-port endpoint)))
+65
(quic-start! client 0)
+66
(procedure pair))
+67
(lambda ()
+68
(when primary (socket-close primary))
+69
(when alternate (socket-close alternate))
+70
(when endpoint (quic-endpoint-close! endpoint))
+71
(when client (quic-abort! client))
+72
(when (and pair (vector-ref pair 5)) (quic-abort! (vector-ref pair 5)))
+73
(do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))
+74
(bytevector-u8-set! (car key) index 0))))))
+75
+76
;; Fields 11/12: active alternate socket?, received alternate datagram count.
+77
(define (en-address port)
+78
(bytevector 127 0 0 1 (quotient port 256) (modulo port 256)))
+79
+80
(define (en-now pair)
+81
(quotient (* 1000 (- (current-jiffy) (vector-ref pair 9)))
+82
(jiffies-per-second)))
+83
+84
(define (en-step! pair)
+85
(let ((client (vector-ref pair 0))
+86
(endpoint (vector-ref pair 1))
+87
(socket (vector-ref pair (if (vector-ref pair 11) 3 2))))
+88
(quic-tick! client (en-now pair))
+89
(for-each
+90
(lambda (output)
+91
(when (quic-output-valid? client output)
+92
(let* ((bytes (quic-output-bytes output))
+93
(sent (udp-send socket bytes "127.0.0.1"
+94
(quic-endpoint-port endpoint))))
+95
(unless (and sent (= sent (bytevector-length bytes)))
+96
(error "endpoint network fixture: UDP send failed")))))
+97
(quic-take-addressed-datagrams! client (en-now pair)))
+98
(quic-endpoint-step! endpoint wait-ms: 1)
+99
;; The old port is unreachable to this simulated NAT-rebound client;
+100
;; its socket remains owned only for deterministic cleanup.
+101
(let drain ((count 0))
+102
(when (< count 64)
+103
(let ((received (udp-receive socket 65528)))
+104
(when received
+105
(unless (and (equal? (cadr received) "127.0.0.1")
+106
(= (caddr received) (quic-endpoint-port endpoint)))
+107
(error "endpoint network fixture: unexpected UDP source"))
+108
(when (vector-ref pair 11)
+109
(vector-set! pair 12 (+ 1 (vector-ref pair 12))))
+110
(quic-receive! client (string->utf8 (car received)) (en-now pair)
+111
address: (en-address (quic-endpoint-port endpoint)))
+112
(drain (+ count 1))))))))
+113
+114
(define (en-await! pair predicate)
+115
(let loop ((round 0))
+116
(cond
+117
((predicate) #t)
+118
((= round 2000) #f)
+119
(else
+120
(en-step! pair)
+121
(loop (+ round 1))))))
+122
+123
(define (en-ready! pair)
+124
(unless (en-await! pair
+125
(lambda ()
+126
(and (quic-confirmed? (vector-ref pair 0))
+127
(vector-ref pair 5)
+128
(quic-confirmed? (vector-ref pair 5)))))
+129
(error "endpoint network fixture: handshake precondition failed"))
+130
;; Deliver and ACK connection-owned spare IDs before rebinding.
+131
(do ((round 0 (+ round 1))) ((= round 12))
+132
(en-step! pair)))
+133
+134
(test-group "Addressed loopback UDP endpoint"
+135
(test "endpoint-network: confirmed endpoint exposes an owned peer address and drains path events"
+136
(en-with-pair
+137
(lambda (pair)
+138
(en-ready! pair)
+139
(let* ((endpoint (vector-ref pair 1))
+140
(expected (en-address (cadr (socket-local-address (vector-ref pair 2)))))
+141
(address (quic-endpoint-peer-address endpoint)))
+142
(assert-true (bytes=? expected address))
+143
(bytevector-u8-set! address 0 0)
+144
(assert-true (bytes=? expected (quic-endpoint-peer-address endpoint)))
+145
(assert-true (null? (quic-endpoint-take-path-events! endpoint)))))))
+146
(test "endpoint-network: actual client port rebind preserves stream and DATAGRAM traffic"
+147
(en-with-pair
+148
(lambda (pair)
+149
(en-ready! pair)
+150
(let* ((endpoint (vector-ref pair 1))
+151
(client (vector-ref pair 0))
+152
(server (vector-ref pair 5))
+153
(old (quic-endpoint-peer-address endpoint))
+154
(next (en-address (cadr (socket-local-address (vector-ref pair 3)))))
+155
(request #u8(0 255 1))
+156
(response #u8(2 0 254))
+157
(request-read #u8())
+158
(response-read #u8())
+159
(request-datagram #f)
+160
(response-datagram #f))
+161
(assert-false (bytes=? old next))
+162
(vector-set! pair 11 #t)
+163
;; PING selects the candidate; a separate bounded padding packet
+164
;; supplies actual UDP credit for its1200-byte validation probe.
+165
(quic-send-frame! client (make-quic-frame 1 '()))
+166
(quic-send-frame! client
+167
(make-quic-frame 0 (list (cons 'length 400))))
+168
(assert-true
+169
(and
+170
(en-await! pair
+171
(lambda () (bytes=? next (quic-endpoint-peer-address endpoint))))
+172
(= 3 (quic-stream-write! (vector-ref pair 4) request))
+173
(quic-send-datagram! client #u8(7 0 255))
+174
(en-await! pair
+175
(lambda ()
+176
(set! request-read
+177
(bytes-append request-read (quic-stream-read! (vector-ref pair 6))))
+178
(unless request-datagram
+179
(set! request-datagram (quic-read-datagram! server)))
+180
(and (bytes=? request request-read) request-datagram)))
+181
(= 3 (quic-stream-write! (vector-ref pair 6) response))
+182
(quic-send-datagram! server #u8(8 255 0))
+183
(en-await! pair
+184
(lambda ()
+185
(set! response-read
+186
(bytes-append response-read (quic-stream-read! (vector-ref pair 4))))
+187
(unless response-datagram
+188
(set! response-datagram (quic-read-datagram! client)))
+189
(and (bytes=? response response-read) response-datagram)))
+190
(let ((events (quic-endpoint-take-path-events! endpoint)))
+191
(and (bytes=? #u8(7 0 255) request-datagram)
+192
(bytes=? #u8(8 255 0) response-datagram)
+193
(> (vector-ref pair 12) 0)
+194
(= 1 (vector-ref pair 7))
+195
(not (quic-closed? client))
+196
(not (quic-closed? server))
+197
(any (lambda (event)
+198
(and (eq? (vector-ref event 0) 'path-changed)
+199
(bytes=? next (vector-ref event 1))))
+200
events)
+201
(any (lambda (event)
+202
(and (eq? (vector-ref event 0) 'path-validated)
+203
(bytes=? next (vector-ref event 1))))
+204
events)
+205
(null? (quic-endpoint-take-path-events! endpoint)))))))))))
test/test-local-ids.sgladded
@@ -0,0 +1,211 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic wire)
+4
(sigil quic frame)
+5
(sigil quic error)
+6
(sigil quic connection ids))
+7
+8
(define (lic-with-pool procedure)
+9
(let ((pool (make-local-id-pool #u8(1))))
+10
(dynamic-wind
+11
(lambda () #t)
+12
(lambda () (procedure pool))
+13
(lambda () (local-id-pool-close! pool)))))
+14
+15
(define (lic-protocol-error? procedure)
+16
(guard (exn
+17
(else (and (quic-error? exn) (= 10 (quic-error-code exn))
+18
(= 25 (quic-error-frame-type exn)))))
+19
(procedure)
+20
#f))
+21
+22
(define (lic-spare! pool)
+23
(local-id-pool-reserve! pool #u8(2 3) (make-bytevector 16 0)))
+24
+25
(define (lic-frame sequence cid token)
+26
(make-quic-frame 24
+27
(list (cons 'sequence sequence) (cons 'retire-prior-to 0)
+28
(cons 'connection-id cid) (cons 'token token))))
+29
+30
(define (lic-commit-spare! pool)
+31
(local-id-pool-commit! pool (lic-frame 1 #u8(2 3) (make-bytevector 16 0))))
+32
+33
(test-group "Locally issued CID ownership and retirement"
+34
(test "local-ids: invalid limits and preferred metadata refuse construction"
+35
(for-each
+36
(lambda (procedure) (assert-error (procedure)))
+37
(list
+38
(lambda () (make-local-id-pool (make-bytevector 21 0)))
+39
(lambda () (make-local-id-pool #u8(1) limit: 1))
+40
(lambda () (make-local-id-pool #u8(1) limit: 9))
+41
(lambda () (make-local-id-pool #u8(1) maximum: 1))
+42
(lambda () (make-local-id-pool #u8(1) maximum: 257))
+43
(lambda () (make-local-id-pool #u8(1) preferred: (cons #u8(2) #u8(0))))
+44
(lambda () (make-local-id-pool #u8() preferred: (cons #u8(2) (make-bytevector 16 0))))
+45
(lambda () (make-local-id-pool #u8(1) preferred: (cons #u8(1 2) (make-bytevector 16 0)))))))
+46
(test "local-ids: initial sequence zero routes and unknown IDs do not"
+47
(lic-with-pool
+48
(lambda (pool)
+49
(assert-equal 0 (local-id-pool-route pool #u8(1)))
+50
(assert-false (local-id-pool-route pool #u8(2)))
+51
(assert-equal '(1) (local-id-pool-lengths pool))
+52
(assert-equal 1 (local-id-pool-count pool)))))
+53
(test "local-ids: a reservation consumes capacity but cannot route or retire"
+54
(lic-with-pool
+55
(lambda (pool)
+56
(let ((frame (lic-spare! pool)))
+57
(assert-true
+58
(and (= 24 (quic-frame-type frame)) (= 1 (frame-ref frame 'sequence))
+59
(= 0 (frame-ref frame 'retire-prior-to))
+60
(not (local-id-pool-route pool #u8(2 3)))
+61
(lic-protocol-error? (lambda () (local-id-pool-retire! pool 1 #u8(1))))
+62
(= 2 (local-id-pool-count pool))
+63
(not (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 1)))))))))
+64
(test "local-ids: repeated packet commits advertise one routable sequence"
+65
(lic-with-pool
+66
(lambda (pool)
+67
(lic-spare! pool)
+68
(assert-true
+69
(and (lic-commit-spare! pool) (lic-commit-spare! pool)
+70
(= 1 (local-id-pool-route pool #u8(2 3)))
+71
(= 2 (local-id-pool-count pool))
+72
(memv 1 (local-id-pool-lengths pool))
+73
(memv 2 (local-id-pool-lengths pool)))))))
+74
(test "local-ids: caller and queued-frame mutations do not change routing ownership"
+75
(lic-with-pool
+76
(lambda (pool)
+77
(let* ((cid (make-bytevector 2 2)) (token (make-bytevector 16 0))
+78
(frame (local-id-pool-reserve! pool cid token)))
+79
(bytevector-u8-set! cid 0 3)
+80
(bytevector-u8-set! token 0 5)
+81
(assert-equal 0 (bytevector-u8-ref (frame-ref frame 'token) 0))
+82
(bytevector-u8-set! (frame-ref frame 'connection-id) 0 4)
+83
(assert-error (local-id-pool-check! pool frame))
+84
(local-id-pool-commit! pool (lic-frame 1 #u8(2 2) (make-bytevector 16 0)))
+85
(assert-equal 1 (local-id-pool-route pool #u8(2 2)))
+86
(assert-false (local-id-pool-route pool cid))
+87
(let ((copy (local-id-pool-id pool 1)))
+88
(bytevector-u8-set! copy 0 6)
+89
(assert-true (bytes=? #u8(2 2) (local-id-pool-id pool 1))))))))
+90
(test "local-ids: retirement above an emitted sequence is a typed violation"
+91
(lic-with-pool
+92
(lambda (pool)
+93
(lic-spare! pool)
+94
(lic-commit-spare! pool)
+95
(assert-true
+96
(and (lic-protocol-error? (lambda () (local-id-pool-retire! pool 2 #u8(1))))
+97
(lic-protocol-error? (lambda () (local-id-pool-retire! pool quic-varint-maximum #u8(1))))
+98
(= 2 (local-id-pool-count pool)))))))
+99
(test "local-ids: a retirement cannot name its own packet destination ID"
+100
(lic-with-pool
+101
(lambda (pool)
+102
(lic-spare! pool)
+103
(lic-commit-spare! pool)
+104
(assert-true
+105
(and (lic-protocol-error? (lambda () (local-id-pool-retire! pool 1 #u8(2 3))))
+106
(= 1 (local-id-pool-route pool #u8(2 3)))
+107
(= 2 (local-id-pool-count pool)))))))
+108
(test "local-ids: duplicate retirement is idempotent and replacement sequence increases"
+109
(lic-with-pool
+110
(lambda (pool)
+111
(lic-spare! pool)
+112
(lic-commit-spare! pool)
+113
(assert-true
+114
(and (local-id-pool-retire! pool 0 #u8(2 3))
+115
(not (local-id-pool-retire! pool 0 #u8(2 3)))
+116
(not (local-id-pool-route pool #u8(1)))
+117
(= 1 (local-id-pool-count pool))
+118
(let ((frame (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 1))))
+119
(and (= 2 (frame-ref frame 'sequence)) (local-id-pool-commit! pool frame)
+120
(= 2 (local-id-pool-route pool #u8(4)))
+121
(= 1 (local-id-pool-route pool #u8(2 3))))))))))
+122
(test "local-ids: repeated and overlapping CID prefixes fail before reservation"
+123
(lic-with-pool
+124
(lambda (pool)
+125
(assert-error (local-id-pool-reserve! pool #u8(1) (make-bytevector 16 0)))
+126
(assert-error (local-id-pool-reserve! pool #u8(1 2) (make-bytevector 16 0)))
+127
(assert-equal 1 (local-id-pool-count pool))
+128
(assert-equal 1 (frame-ref (lic-spare! pool) 'sequence)))))
+129
(test "local-ids: lifetime exhaustion preserves every remaining live ID"
+130
(let ((pool (make-local-id-pool #u8(1) maximum: 2)))
+131
(dynamic-wind
+132
(lambda () #t)
+133
(lambda ()
+134
(lic-spare! pool)
+135
(lic-commit-spare! pool)
+136
(local-id-pool-retire! pool 0 #u8(2 3))
+137
(assert-false (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 0)))
+138
(assert-equal 1 (local-id-pool-route pool #u8(2 3)))
+139
(assert-equal 1 (local-id-pool-count pool)))
+140
(lambda () (local-id-pool-close! pool)))))
+141
(test "local-ids: zero-length source IDs never issue spares or allow retirement"
+142
(let ((pool (make-local-id-pool #u8())))
+143
(dynamic-wind
+144
(lambda () #t)
+145
(lambda ()
+146
(assert-equal 0 (local-id-pool-route pool #u8()))
+147
(assert-false (lic-spare! pool))
+148
(assert-true (lic-protocol-error? (lambda () (local-id-pool-retire! pool 0 #u8())))))
+149
(lambda () (local-id-pool-close! pool)))))
+150
(test "local-ids: authenticated preferred-address CID occupies sequence one"
+151
(let ((pool (make-local-id-pool #u8(1) preferred: (cons #u8(2) (make-bytevector 16 0)))))
+152
(dynamic-wind
+153
(lambda () #t)
+154
(lambda ()
+155
(assert-equal 1 (local-id-pool-route pool #u8(2)))
+156
(assert-equal 2 (local-id-pool-count pool))
+157
(local-id-pool-retire! pool 1 #u8(1))
+158
(assert-equal 2
+159
(frame-ref (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 0)) 'sequence)))
+160
(lambda () (local-id-pool-close! pool)))))
+161
(test "local-ids: initial and returned IDs are owned copies"
+162
(let* ((initial (make-bytevector 1 1)) (pool (make-local-id-pool initial)))
+163
(dynamic-wind
+164
(lambda () #t)
+165
(lambda ()
+166
(bytevector-u8-set! initial 0 3)
+167
(let ((copy (local-id-pool-id pool 0)))
+168
(bytevector-u8-set! copy 0 4)
+169
(assert-true (bytes=? #u8(1) (local-id-pool-id pool 0)))))
+170
(lambda () (local-id-pool-close! pool)))))
+171
(test "local-ids: close clears routes and refuses further ownership mutations"
+172
(lic-with-pool
+173
(lambda (pool)
+174
(lic-spare! pool)
+175
(local-id-pool-close! pool)
+176
(local-id-pool-close! pool)
+177
(assert-false (local-id-pool-route pool #u8(1)))
+178
(assert-false (local-id-pool-id pool 0))
+179
(assert-equal '() (local-id-pool-lengths pool))
+180
(assert-equal 0 (local-id-pool-count pool))
+181
(assert-error (lic-spare! pool))
+182
(assert-error (lic-commit-spare! pool))
+183
(assert-error (local-id-pool-retire! pool 0 #u8(2 3))))))
+184
(test "local-ids: a retired advertisement cannot revive a route"
+185
(lic-with-pool
+186
(lambda (pool)
+187
(let ((frame (lic-spare! pool)))
+188
(local-id-pool-commit! pool frame)
+189
(local-id-pool-retire! pool 1 #u8(1))
+190
(assert-true
+191
(and (not (local-id-pool-check! pool frame))
+192
(not (local-id-pool-commit! pool frame))
+193
(not (local-id-pool-route pool #u8(2 3)))
+194
(= 1 (local-id-pool-count pool))))))))
+195
(test "local-ids: wire metadata must match the reserved sequence CID and token"
+196
(lic-with-pool
+197
(lambda (pool)
+198
(let ((frame (lic-spare! pool)))
+199
(assert-error (local-id-pool-check! pool (make-quic-frame 1 '())))
+200
(assert-error (local-id-pool-check! pool (lic-frame 2 #u8(2 3) (make-bytevector 16 0))))
+201
(assert-error (local-id-pool-check! pool (lic-frame 1 #u8(4) (make-bytevector 16 0))))
+202
(assert-error (local-id-pool-check! pool (lic-frame 1 #u8(2 3) (make-bytevector 16 1))))
+203
(assert-error
+204
(local-id-pool-check! pool
+205
(make-quic-frame 24
+206
(list (cons 'sequence 1) (cons 'retire-prior-to 1)
+207
(cons 'connection-id #u8(2 3)) (cons 'token (make-bytevector 16 0))))))
+208
(assert-true
+209
(and (not (local-id-pool-route pool #u8(2 3)))
+210
(local-id-pool-check! pool frame) (local-id-pool-commit! pool frame)
+211
(= 1 (local-id-pool-route pool #u8(2 3))))))))))
test/test-network-confirmation.sgladded
@@ -0,0 +1,65 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic frame)
+4
(sigil quic connection network))
+5
+6
(define (nc-with-passive procedure)
+7
(let ((network
+8
(make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
+9
(list (cons 0 #u8(10 11 12 13 14 15 16 17)) (cons 15 #u8(1)))
+10
(list (cons 15 #u8(2))) confirmed?: #f)))
+11
(dynamic-wind
+12
(lambda () #t)
+13
(lambda () (procedure network))
+14
(lambda () (network-close! network)))))
+15
+16
(define (nc-advertisement)
+17
(make-quic-frame 24
+18
(list (cons 'sequence 1) (cons 'retire-prior-to 0)
+19
(cons 'connection-id #u8(3)) (cons 'token (make-bytevector 16 0)))))
+20
+21
(test-group "Passive CID ownership before handshake confirmation"
+22
(test "network-confirmation: early CID metadata is retained while every path operation is refused"
+23
(nc-with-passive
+24
(lambda (network)
+25
(assert-true (network-peer-id! network (nc-advertisement)))
+26
(for-each
+27
(lambda (procedure) (assert-error (procedure)))
+28
(list
+29
(lambda () (network-observe! network #u8(11) 1200 1 (list (make-quic-frame 1 '())) 0))
+30
(lambda () (network-challenge! network #u8(11) 0))
+31
(lambda () (network-reserve-send! network #u8(10) 1200 0))
+32
(lambda () (network-response! network (make-bytevector 8 0) 0))
+33
(lambda () (network-tick! network 0))))
+34
(assert-true
+35
(and (bytes=? #u8(10) (network-address network))
+36
(not (network-candidate network)) (not (network-destination network #u8(11)))
+37
(null? (network-take-events! network)))))))
+38
(test "network-confirmation: confirmation seeds ordering once and preserves early CID ownership"
+39
(nc-with-passive
+40
(lambda (network)
+41
(network-peer-id! network (nc-advertisement))
+42
(network-confirm! network 100 10)
+43
(assert-true
+44
(and (not (network-observe! network #u8(11) 1200 9 (list (make-quic-frame 1 '())) 100))
+45
(network-observe! network #u8(11) 1200 11 (list (make-quic-frame 1 '())) 100)
+46
(let ((probe (network-challenge! network #u8(11) 100)))
+47
(and probe (= 1 (vector-ref probe 1))
+48
(bytes=? #u8(3) (vector-ref probe 2))
+49
(network-response! network (vector-ref probe 3) 101)
+50
(network-confirm! network 102 0)
+51
(bytes=? #u8(11) (network-address network))
+52
(not (network-observe! network #u8(12) 1200 10 (list (make-quic-frame 1 '())) 102)))))))))
+53
(test "network-confirmation: close-route snapshots are owned and exclude uncommitted or retired IDs"
+54
(nc-with-passive
+55
(lambda (network)
+56
(let ((frame (network-issue-id! network #u8(4) (make-bytevector 16 0))))
+57
(assert-false (network-local-id network 1))
+58
(network-commit-id! network frame)
+59
(let ((copy (network-local-id network 1)))
+60
(bytevector-u8-set! copy 0 5))
+61
(assert-true
+62
(and (bytes=? #u8(4) (network-local-id network 1))
+63
(network-retire-local-id! network 1 #u8(1))
+64
(not (network-local-id network 1))
+65
(bytes=? #u8(1) (network-local-id network 0)))))))))
test/test-network-receive.sgladded
@@ -0,0 +1,606 @@
+1
(import (sigil test)
+2
(sigil crypto)
+3
(sigil quic bytes)
+4
(sigil quic wire)
+5
(sigil quic frame)
+6
(sigil quic packet)
+7
(sigil quic reassembly)
+8
(sigil quic connection)
+9
(sigil quic stream)
+10
(sigil quic tls))
+11
+12
;; A public TLS peer owns its keys. The target connection exposes no secrets.
+13
;; Fields: connection/TLS, read/write keys, peer PNs/target largest PNs,
+14
;; CRYPTO reassemblers/offsets, observed output/handler frames, time, peer role,
+15
;; HANDSHAKE_DONE flag, target CID and peer CID.
+16
(define nr-original #u8(1 2 3 4 5 6 7 8))
+17
(define nr-client-id #u8(11 12 13 14 15 16 17 18))
+18
(define nr-server-id #u8(21 22 23 24 25 26 27 28))
+19
(define nr-data #u8(0 1 2 255))
+20
(define nr-path-data #u8(0 1 0 3 4 0 6 255))
+21
+22
(define (nr-index level)
+23
(case level ((initial) 0) ((handshake) 1) ((application) 2)))
+24
+25
(define (nr-wipe! bytes)
+26
(when (bytevector? bytes)
+27
(do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))
+28
(bytevector-u8-set! bytes i 0))))
+29
+30
(define (nr-build! peer level frames)
+31
(let* ((index (nr-index level)) (numbers (vector-ref peer 4))
+32
(number (vector-ref numbers index)) (role (vector-ref peer 11))
+33
(destination (if (and (eq? role 'client) (eq? level 'initial)
+34
(= -1 (vector-ref (vector-ref peer 5) 0)))
+35
nr-original (vector-ref peer 13)))
+36
(source (if (eq? level 'application) #u8() (vector-ref peer 14)))
+37
(body (encode-frames frames level role))
+38
(header (encode-packet-header level destination source #u8() number 4
+39
(+ 16 (bytevector-length body))
+40
length-width: (and (not (eq? level 'application)) 2)))
+41
(payload (if (eq? level 'initial)
+42
(bytes-append body
+43
(make-bytevector (- 1200 16 (bytevector-length header) (bytevector-length body)) 0))
+44
body)))
+45
(vector-set! numbers index (+ number 1))
+46
(cons number
+47
(seal-quic-packet (vector-ref (vector-ref peer 3) index) level destination source
+48
number payload length-width: (and (not (eq? level 'application)) 2)))))
+49
+50
(define (nr-receive! peer packet now)
+51
(vector-set! peer 10 now)
+52
(quic-receive! (vector-ref peer 0) packet now address: #u8(42)))
+53
+54
(define (nr-send! peer level frames)
+55
(let ((packet (nr-build! peer level frames)))
+56
(nr-receive! peer (cdr packet) (vector-ref peer 10))
+57
packet))
+58
+59
(define (nr-events! peer)
+60
(for-each
+61
(lambda (event)
+62
(let* ((level (tls-event-level event)) (index (nr-index level))
+63
(data (tls-event-data event)))
+64
(case (tls-event-kind event)
+65
((secrets)
+66
(when (car data)
+67
(vector-set! (vector-ref peer 2) index (traffic-keys (car data)))
+68
(nr-wipe! (car data)))
+69
(when (cdr data)
+70
(vector-set! (vector-ref peer 3) index (traffic-keys (cdr data)))
+71
(nr-wipe! (cdr data))))
+72
((crypto)
+73
(let* ((offsets (vector-ref peer 7)) (offset (vector-ref offsets index)))
+74
(nr-send! peer level
+75
(list (make-quic-frame 6 (list (cons 'offset offset) (cons 'data data)))))
+76
(vector-set! offsets index (+ offset (bytevector-length data))))))))
+77
(tls-take-events! (vector-ref peer 1)))
+78
(when (and (eq? (vector-ref peer 11) 'server)
+79
(tls-complete? (vector-ref peer 1)) (not (vector-ref peer 12)))
+80
(vector-set! peer 12 #t)
+81
(nr-send! peer 'application (list (make-quic-frame 30 '())))))
+82
+83
(define (nr-read! peer packet ack?)
+84
(let* ((header (decode-packet-header packet 0 8)) (level (quic-header-kind header))
+85
(index (nr-index level)) (largest (vector-ref peer 5))
+86
(opened (open-quic-packet packet 0 8 (vector-ref (vector-ref peer 2) index)
+87
(vector-ref largest index))))
+88
(unless opened (error "Addressed receive fixture target packet did not authenticate"))
+89
(let* ((number (opened-quic-packet-number opened))
+90
(frames (decode-frames (opened-quic-packet-payload opened) level
+91
(quic-role (vector-ref peer 0)))))
+92
(vector-set! largest index (max number (vector-ref largest index)))
+93
(when (eq? level 'application)
+94
(vector-set! peer 8 (cons (cons number frames) (vector-ref peer 8))))
+95
(for-each
+96
(lambda (frame)
+97
(when (= (quic-frame-type frame) 6)
+98
(let ((receiver (vector-ref (vector-ref peer 6) index)))
+99
(reassembler-insert! receiver (frame-ref frame 'offset) (frame-ref frame 'data))
+100
(let ((bytes (reassembler-read! receiver)))
+101
(when (> (bytevector-length bytes) 0)
+102
(tls-input! (vector-ref peer 1) level bytes)
+103
;; Install client-side Handshake keys before opening the
+104
;; next packet in the server's already returned flight.
+105
(nr-events! peer)))))) frames)
+106
(when (and ack? (any frame-ack-eliciting? frames))
+107
(nr-send! peer level
+108
(list (make-quic-frame 2
+109
(list (cons 'ranges (list (cons number number))) (cons 'delay 0)))))))))
+110
+111
(define (nr-output! peer now (keys: (ack? #t)))
+112
(vector-set! peer 10 now)
+113
(let ((packets
+114
(map quic-output-bytes (quic-take-addressed-datagrams! (vector-ref peer 0) now))))
+115
(for-each (lambda (packet) (nr-read! peer packet ack?)) packets)
+116
packets))
+117
+118
(define (nr-live? peer)
+119
(let ((connection (vector-ref peer 0)))
+120
(and (quic-confirmed? connection) (not (quic-closed? connection))
+121
(not (quic-failure connection)))))
+122
+123
(define (nr-with-peer role procedure (keys: (confirm? #t)))
+124
(let ((key (ecdsa-p256-generate-keypair)) (connection #f) (engine #f) (peer #f))
+125
(dynamic-wind
+126
(lambda () #t)
+127
(lambda ()
+128
(let* ((client? (eq? role 'client)) (peer-role (if client? 'server 'client))
+129
(target-id (if client? nr-client-id nr-server-id))
+130
(peer-id (if client? nr-server-id nr-client-id))
+131
(parameters
+132
(append (stream-transport-parameters peer-role receive-window: 64)
+133
(list (cons 1 (encode-varint 30000)) (cons 15 peer-id))
+134
(if client? (list (cons 0 nr-original)) '()))))
+135
(set! connection
+136
(if client?
+137
(make-quic-client (list #u8(113 49)) 'pinned-key
+138
(stream-transport-parameters role receive-window: 64)
+139
(make-tls-pin-verifier (cdr key)) local-id: target-id destination-id: nr-original)
+140
(make-quic-server (list #u8(113 49))
+141
(stream-transport-parameters role receive-window: 64)
+142
(list #u8(48 1 0)) (car key) nr-original peer-id local-id: target-id)))
+143
(set! engine
+144
(if client?
+145
(make-tls-server (list #u8(113 49)) parameters (list #u8(48 1 0)) (car key))
+146
(make-tls-client (list #u8(113 49)) 'pinned-key parameters
+147
(make-tls-pin-verifier (cdr key)))))
+148
(set! peer
+149
(vector connection engine
+150
(vector (initial-keys nr-original role) #f #f)
+151
(vector (initial-keys nr-original peer-role) #f #f)
+152
(vector 0 0 0) (vector -1 -1 -1)
+153
(vector (make-byte-reassembler 65536) (make-byte-reassembler 65536)
+154
(make-byte-reassembler 65536))
+155
(vector 0 0 0) '() '() 0 peer-role (not confirm?) target-id peer-id))
+156
(quic-bind-peer-address! connection #u8(42))
+157
(tls-bind-peer! engine nr-original target-id #f)
+158
(if client?
+159
(quic-start! connection 0)
+160
(begin (tls-start! engine) (nr-events! peer)))
+161
(do ((round 0 (+ round 1))) ((= round 12))
+162
(nr-output! peer 0)
+163
(nr-events! peer))
+164
(unless (and (quic-complete? connection) (not (quic-closed? connection))
+165
(eq? confirm? (quic-confirmed? connection)) (tls-complete? engine)
+166
(vector-ref (vector-ref peer 2) 2) (vector-ref (vector-ref peer 3) 2))
+167
(error "Addressed receive fixture protected handshake did not complete"))
+168
(procedure peer)))
+169
(lambda ()
+170
(when connection (quic-abort! connection))
+171
(when engine (tls-abort! engine))
+172
(when peer
+173
(for-each
+174
(lambda (keys) (when keys (discard-packet-keys! keys)))
+175
(append (vector->list (vector-ref peer 2)) (vector->list (vector-ref peer 3))))
+176
(for-each reassembler-close! (vector->list (vector-ref peer 6))))
+177
(nr-wipe! (car key))))))
+178
+179
(define (nr-from! peer packet address)
+180
(quic-receive! (vector-ref peer 0) packet (vector-ref peer 10) address: address))
+181
+182
(define (nr-ping! peer)
+183
(nr-build! peer 'application (list (make-quic-frame 1 '()))))
+184
+185
(define (nr-events? connection kind address)
+186
(let ((events (quic-take-path-events! connection)))
+187
(and (= 1 (length events))
+188
(eq? kind (vector-ref (car events) 0))
+189
(bytes=? address (vector-ref (car events) 1)))))
+190
+191
(define (nr-observed peer type)
+192
(filter (lambda (frame) (= type (quic-frame-type frame)))
+193
(apply append (map cdr (vector-ref peer 8)))))
+194
+195
(define (nr-advertised peer sequence)
+196
(find (lambda (frame) (= sequence (frame-ref frame 'sequence))) (nr-observed peer 24)))
+197
+198
(define (nr-stream! peer)
+199
(cdr (nr-build! peer 'application
+200
(list (make-quic-frame 15
+201
(list (cons 'stream-id 0) (cons 'offset 0) (cons 'data #u8(1))))))))
+202
+203
(define (nr-give-spare! peer)
+204
(nr-send! peer 'application
+205
(list (make-quic-frame 24
+206
(list (cons 'sequence 1) (cons 'retire-prior-to 0)
+207
(cons 'connection-id #u8(51 52 53 54 55 56 57 58))
+208
(cons 'token (make-bytevector 16 5)))))))
+209
+210
(define (nr-addressed! peer now)
+211
(vector-set! peer 10 now)
+212
(let ((outputs (quic-take-addressed-datagrams! (vector-ref peer 0) now)))
+213
(for-each (lambda (output) (nr-read! peer (quic-output-bytes output) #f)) outputs)
+214
outputs))
+215
+216
(define (nr-output-frames peer output)
+217
(let ((opened (open-quic-packet (quic-output-bytes output) 0 8
+218
(vector-ref (vector-ref peer 2) 2) -1)))
+219
(unless opened (error "Addressed fixture: application output did not authenticate"))
+220
(decode-frames (opened-quic-packet-payload opened) 'application (quic-role (vector-ref peer 0)))))
+221
+222
(define (nr-control-output peer outputs address type)
+223
(find (lambda (output)
+224
(and (bytes=? address (quic-output-address output))
+225
(any (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output))))
+226
outputs))
+227
+228
(define (nr-control-token peer output type)
+229
(and output
+230
(let ((frame (find (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output))))
+231
(and frame (frame-ref frame 'data)))))
+232
+233
(define (nr-count-control peer outputs address type)
+234
(length
+235
(filter (lambda (output)
+236
(and (bytes=? address (quic-output-address output))
+237
(any (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output))))
+238
outputs)))
+239
+240
(define (nr-probe-pair? peer outputs)
+241
(and (= 1 (nr-count-control peer outputs #u8(42) 26))
+242
(= 1 (nr-count-control peer outputs #u8(43) 26))))
+243
+244
(define (nr-path-input! peer type data address)
+245
(nr-from! peer (cdr (nr-build! peer 'application
+246
(list (make-quic-frame type (list (cons 'data data)))))) address))
+247
+248
(define (nr-padded-ping! peer size)
+249
(let ((packet
+250
(cdr (nr-build! peer 'application
+251
(list (make-quic-frame 1 '())
+252
(make-quic-frame 0 (list (cons 'length (- size 30)))))))))
+253
(unless (= size (bytevector-length packet)) (error "Addressed fixture: wrong padded wire size"))
+254
packet))
+255
+256
(define (nr-stream-outputs peer outputs address)
+257
(filter
+258
(lambda (output)
+259
(and (bytes=? address (quic-output-address output))
+260
(any (lambda (frame) (<= 8 (quic-frame-type frame) 15)) (nr-output-frames peer output))))
+261
outputs))
+262
+263
(define (nr-output-number peer output)
+264
(opened-quic-packet-number
+265
(open-quic-packet (quic-output-bytes output) 0 8 (vector-ref (vector-ref peer 2) 2) -1)))
+266
+267
(test-group "Authenticated path credit and pending-response bounds"
+268
(test "network-path-credit: replay cannot fund another challenge after exact wire credit is spent"
+269
(nr-with-peer 'server
+270
(lambda (peer)
+271
(nr-give-spare! peer)
+272
(let ((packet (nr-padded-ping! peer 400)))
+273
(nr-from! peer packet #u8(43))
+274
(assert-true (nr-probe-pair? peer (nr-addressed! peer 0)))
+275
(nr-from! peer packet #u8(43))
+276
(quic-tick! (vector-ref peer 0) 1000)
+277
(assert-true
+278
(and (nr-live? peer)
+279
(not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 26))))))))
+280
(test "network-path-credit: forged tags and unowned STREAM input cannot refill a candidate budget"
+281
(for-each
+282
(lambda (kind)
+283
(nr-with-peer 'server
+284
(lambda (peer)
+285
(nr-give-spare! peer)
+286
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+287
(assert-true (nr-probe-pair? peer (nr-addressed! peer 0)))
+288
(let ((packet
+289
(if (eq? kind 'forged)
+290
(flip-byte (nr-padded-ping! peer 400) 399)
+291
(cdr (nr-build! peer 'application
+292
(list (make-quic-frame 15
+293
(list (cons 'stream-id 0) (cons 'offset 0)
+294
(cons 'data (make-bytevector 366 1))))))))))
+295
(unless (= 400 (bytevector-length packet)) (error "Credit fixture: wrong wire size"))
+296
(nr-from! peer packet #u8(43)))
+297
(quic-tick! (vector-ref peer 0) 1000)
+298
(assert-true
+299
(and (nr-live? peer)
+300
(not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 26)))))))
+301
'(forged unowned)))
+302
(test "network-path-credit: response padding refusal does not consume a packet number"
+303
(nr-with-peer 'server
+304
(lambda (peer)
+305
(let ((before (vector-ref (vector-ref peer 5) 2)))
+306
(nr-give-spare! peer)
+307
(nr-path-input! peer 26 #u8(0 1 2 3 4 5 6 7) #u8(43))
+308
(let ((response (nr-control-output peer (nr-addressed! peer 0) #u8(43) 27)))
+309
(assert-true
+310
(and (nr-live? peer) response
+311
(= 38 (bytevector-length (quic-output-bytes response)))
+312
(= (+ before 1) (nr-output-number peer response)))))))))
+313
(test "network-path-credit: pending responses stop at16 and expired requests cannot bind to a later attempt"
+314
(nr-with-peer 'server
+315
(lambda (peer)
+316
(nr-give-spare! peer)
+317
(do ((index 0 (+ index 1))) ((= index 17))
+318
(nr-path-input! peer 26 (make-bytevector 8 index) #u8(43)))
+319
(let* ((outputs (nr-addressed! peer 0))
+320
(responses (filter (lambda (output) (bytes=? #u8(43) (quic-output-address output))) outputs)))
+321
(assert-true
+322
(and (nr-live? peer) (= 16 (nr-count-control peer outputs #u8(43) 27))
+323
(= 1770 (apply + (map (lambda (output) (bytevector-length (quic-output-bytes output))) responses)))
+324
(<= 1770 (* 3 17 38)))))))
+325
(nr-with-peer 'server
+326
(lambda (peer)
+327
;; No spare CID: retain requests without sending them on another path.
+328
(do ((index 0 (+ index 1))) ((= index 17))
+329
(nr-path-input! peer 26 (make-bytevector 8 index) #u8(43)))
+330
(assert-false (nr-control-output peer (nr-addressed! peer 0) #u8(43) 27))
+331
(quic-tick! (vector-ref peer 0) 3000)
+332
(vector-set! peer 10 3000)
+333
(nr-give-spare! peer)
+334
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+335
(let ((outputs (nr-addressed! peer 3000)))
+336
(assert-true
+337
(and (nr-live? peer) (nr-control-output peer outputs #u8(43) 26)
+338
(not (nr-control-output peer outputs #u8(43) 27)))))))))
+339
+340
(test-group "Addressed PATH controls and migration accounting"
+341
(test "network-path: promotion invalidates old ordinary output but preserves addressed validation controls"
+342
(nr-with-peer 'server
+343
(lambda (peer)
+344
(let ((connection (vector-ref peer 0)))
+345
(nr-give-spare! peer)
+346
(quic-send-frame! connection (make-quic-frame 1 '()))
+347
(let ((old (find (lambda (output)
+348
(any (lambda (frame) (= 1 (quic-frame-type frame))) (nr-output-frames peer output)))
+349
(nr-addressed! peer 0))))
+350
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+351
(let* ((outputs (nr-addressed! peer 0)) (probe (nr-control-output peer outputs #u8(43) 26))
+352
(previous (nr-control-output peer outputs #u8(42) 26)))
+353
(assert-true
+354
(and old probe previous (quic-output-valid? connection old)
+355
(begin
+356
(nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(42))
+357
(and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address connection))
+358
(not (quic-output-valid? connection old))
+359
(quic-output-valid? connection probe)
+360
(quic-output-valid? connection previous)))))))))))
+361
(test "network-path: promotion requeues reliable flight under fresh PN and ignores old ACK window credit"
+362
(nr-with-peer 'server
+363
(lambda (peer)
+364
(let ((connection (vector-ref peer 0)))
+365
(nr-give-spare! peer)
+366
(do ((index 0 (+ index 1))) ((= index 16))
+367
(quic-send-frame! connection
+368
(make-quic-frame 14
+369
(list (cons 'stream-id 1) (cons 'offset (* index 1100))
+370
(cons 'data (make-bytevector 1100 index))))))
+371
(let* ((old (nr-stream-outputs peer (nr-addressed! peer 0) #u8(42)))
+372
(old-numbers (map (lambda (output) (nr-output-number peer output)) old)))
+373
(vector-set! peer 10 999)
+374
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+375
(let ((probe (nr-control-output peer (nr-addressed! peer 999) #u8(43) 26)))
+376
(assert-true
+377
(and (pair? old) probe
+378
(begin
+379
(nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(42))
+380
(let ((fresh (nr-stream-outputs peer (nr-addressed! peer 999) #u8(43))))
+381
(and (nr-live? peer) (= 10 (length fresh))
+382
(every (lambda (output)
+383
(> (nr-output-number peer output) (apply max old-numbers))) fresh)
+384
(begin
+385
;; The old flight is due; the new flight is1ms old.
+386
(quic-tick! connection 1000)
+387
(vector-set! peer 10 1000)
+388
(nr-from! peer
+389
(cdr (nr-build! peer 'application
+390
(list (make-quic-frame 2
+391
(list (cons 'ranges
+392
(list (cons (apply min old-numbers)
+393
(apply max old-numbers))))
+394
(cons 'delay 0)))))) #u8(43))
+395
(and (nr-live? peer)
+396
(null? (nr-stream-outputs peer (nr-addressed! peer 1000) #u8(43))))))))))))))))
+397
(test "network-path: response uses challenge source and falls back to its exact threefold budget"
+398
(nr-with-peer 'server
+399
(lambda (peer)
+400
(nr-give-spare! peer)
+401
(nr-path-input! peer 26 #u8(1 2 3 4 5 6 7 8) #u8(43))
+402
(let* ((connection (vector-ref peer 0)) (outputs (nr-addressed! peer 0))
+403
(response (nr-control-output peer outputs #u8(43) 27)))
+404
(assert-true
+405
(and (nr-live? peer) response
+406
(= 38 (bytevector-length (quic-output-bytes response)))
+407
(bytes=? #u8(1 2 3 4 5 6 7 8) (nr-control-token peer response 27))
+408
(quic-output-valid? connection response)
+409
(not (nr-control-output peer outputs #u8(42) 27))
+410
(not (nr-control-output peer outputs #u8(43) 26))
+411
(bytes=? #u8(42) (quic-peer-address connection)))))
+412
(quic-tick! (vector-ref peer 0) 1000)
+413
(assert-true (not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 27))))))
+414
(test "network-path: a400-byte authenticated datagram funds one1200-byte challenge without double charging"
+415
(nr-with-peer 'server
+416
(lambda (peer)
+417
(nr-give-spare! peer)
+418
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+419
(let* ((connection (vector-ref peer 0)) (outputs (nr-addressed! peer 0))
+420
(candidate (nr-control-output peer outputs #u8(43) 26))
+421
(previous (nr-control-output peer outputs #u8(42) 26)))
+422
(assert-true
+423
(and candidate previous
+424
(= 1200 (bytevector-length (quic-output-bytes candidate)))
+425
(= 1200 (bytevector-length (quic-output-bytes previous)))
+426
(not (bytes=? (quic-output-id candidate) (quic-output-id previous)))
+427
(bytes=? #u8(42) (quic-peer-address connection))
+428
(begin
+429
(nr-path-input! peer 27 (nr-control-token peer candidate 26) #u8(42))
+430
(and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address connection))
+431
(bytes=? #u8(51 52 53 54 55 56 57 58) (quic-peer-id connection))))))))))
+432
(test "network-path: a matching response arriving on a third path validates its original challenge"
+433
(nr-with-peer 'server
+434
(lambda (peer)
+435
(nr-give-spare! peer)
+436
(nr-from! peer (nr-padded-ping! peer 400) #u8(43))
+437
(let* ((outputs (nr-addressed! peer 0)) (probe (nr-control-output peer outputs #u8(43) 26)))
+438
(assert-true
+439
(and probe
+440
(begin
+441
(nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(44))
+442
(and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address (vector-ref peer 0)))))))))))
+443
(test "network-path: lost probes use fresh PTO-paced payloads and expire without transport retransmission"
+444
(nr-with-peer 'server
+445
(lambda (peer)
+446
(nr-give-spare! peer)
+447
(nr-from! peer (nr-padded-ping! peer 1200) #u8(43))
+448
(let* ((first-outputs (nr-addressed! peer 0))
+449
(first (nr-control-output peer first-outputs #u8(43) 26))
+450
(early (nr-control-output peer (nr-addressed! peer 0) #u8(43) 26)))
+451
(quic-tick! (vector-ref peer 0) 1000)
+452
(let* ((second-outputs (nr-addressed! peer 1000))
+453
(second (nr-control-output peer second-outputs #u8(43) 26)))
+454
(quic-tick! (vector-ref peer 0) 2000)
+455
(let* ((third-outputs (nr-addressed! peer 2000))
+456
(third (nr-control-output peer third-outputs #u8(43) 26)))
+457
(assert-true
+458
(and first (not early) second third
+459
(nr-probe-pair? peer first-outputs)
+460
(nr-probe-pair? peer second-outputs)
+461
(nr-probe-pair? peer third-outputs)
+462
(not (bytes=? (nr-control-token peer first 26) (nr-control-token peer second 26)))
+463
(not (bytes=? (nr-control-token peer second 26) (nr-control-token peer third 26)))))
+464
(quic-tick! (vector-ref peer 0) 3000)
+465
(assert-true
+466
(and (nr-live? peer) (bytes=? #u8(42) (quic-peer-address (vector-ref peer 0)))
+467
(not (nr-control-output peer (nr-addressed! peer 3000) #u8(43) 26))
+468
(any (lambda (event) (eq? 'path-failed (vector-ref event 0)))
+469
(quic-take-path-events! (vector-ref peer 0))))))))))))
+470
+471
(test-group "Connection-owned spare-ID output and retirement"
+472
(test "network-cids: one spare is advertised within the peer limit and owns CID control output"
+473
(nr-with-peer 'server
+474
(lambda (peer)
+475
(let* ((connection (vector-ref peer 0)) (spare (nr-advertised peer 1)))
+476
(assert-true
+477
(and (nr-live? peer) spare (= 1 (length (nr-observed peer 24)))
+478
(= 8 (bytevector-length (frame-ref spare 'connection-id)))
+479
(= 16 (bytevector-length (frame-ref spare 'token)))
+480
(not (bytes=? nr-server-id (frame-ref spare 'connection-id)))))
+481
(assert-error (quic-send-frame! connection (make-quic-frame 25 (list (cons 'sequence 0)))))
+482
(when spare (assert-error (quic-send-frame! connection spare)))
+483
(nr-output! peer 0)
+484
(assert-true (and (nr-live? peer) (= 1 (length (nr-observed peer 24)))))))))
+485
(test "network-cids: emitted spare routes and retired aliases cannot dispatch application input"
+486
(nr-with-peer 'server
+487
(lambda (peer)
+488
(let* ((connection (vector-ref peer 0)) (spare (nr-advertised peer 1)) (seen 0))
+489
(quic-set-frame-handler! connection
+490
(lambda (frame) (set! seen (+ seen 1))))
+491
(assert-true
+492
(and spare
+493
(begin
+494
(vector-set! peer 13 (frame-ref spare 'connection-id))
+495
(nr-from! peer (nr-stream! peer) #u8(42))
+496
(= seen 1))
+497
(begin
+498
(vector-set! peer 13 nr-server-id)
+499
(nr-send! peer 'application (list (make-quic-frame 25 (list (cons 'sequence 1)))))

Showing the first 500 of 607 diff lines for this file. This diff is INCOMPLETE; read the file or clone the repository for the rest.

test/test-path-manager.sgladded
@@ -0,0 +1,206 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic path manager))
+4
+5
(define (pm-with-manager procedure)
+6
(let ((manager (make-path-manager #u8(1) 'server 0)))
+7
(dynamic-wind
+8
(lambda () #t)
+9
(lambda () (procedure manager))
+10
(lambda () (path-manager-close! manager)))))
+11
+12
(define (pm-candidate! manager)
+13
(path-manager-observe! manager #u8(2) 1200 1 #f 0))
+14
+15
(define (pm-kind? event kind address reason)
+16
(and (eq? kind (vector-ref event 0)) (bytes=? address (vector-ref event 1))
+17
(eq? reason (vector-ref event 2))))
+18
+19
(test-group "Authenticated peer path selection"
+20
(test "path-manager: construction rejects invalid role baseline and path metadata"
+21
(for-each
+22
(lambda (procedure) (assert-error (procedure)))
+23
(list
+24
(lambda () (make-path-manager #u8(1) 'other 0))
+25
(lambda () (make-path-manager #u8() 'server 0))
+26
(lambda () (make-path-manager #u8(1) 'server -1))
+27
(lambda () (make-path-manager #u8(1) 'server 0 highest: -2))
+28
(lambda () (make-path-manager #u8(1) 'server 0 pto: 1)))))
+29
(test "path-manager: addresses and drained events cannot mutate owned path keys"
+30
(let* ((address (make-bytevector 1 1)) (manager (make-path-manager address 'server 0)))
+31
(dynamic-wind
+32
(lambda () #t)
+33
(lambda ()
+34
(bytevector-u8-set! address 0 9)
+35
(let ((copy (path-manager-address manager)))
+36
(bytevector-u8-set! copy 0 8))
+37
(pm-candidate! manager)
+38
(let* ((copy (path-manager-candidate manager)) (events (path-manager-take-events! manager)))
+39
(bytevector-u8-set! copy 0 7)
+40
(bytevector-u8-set! (vector-ref (car events) 1) 0 6)
+41
(assert-true
+42
(and (bytes=? #u8(1) (path-manager-address manager))
+43
(bytes=? #u8(2) (path-manager-candidate manager))
+44
(bytes=? #u8(1) (path-manager-previous-address manager))))))
+45
(lambda () (path-manager-close! manager)))))
+46
(test "path-manager: unvalidated candidate never moves application sends or shares credit"
+47
(pm-with-manager
+48
(lambda (manager)
+49
(path-manager-observe! manager #u8(2) 10 1 #f 0)
+50
(assert-true
+51
(and (bytes=? #u8(1) (path-manager-address manager))
+52
(path-manager-send! manager #u8(1) 1200 0)
+53
(path-manager-send! manager #u8(2) 30 0)
+54
(not (path-manager-send! manager #u8(2) 1 0))
+55
(not (path-manager-send! manager #u8(3) 1 0))
+56
(not (path-manager-challenge! manager #u8(2) 0)))))))
+57
(test "path-manager: only a matching padded challenge promotes the candidate"
+58
(pm-with-manager
+59
(lambda (manager)
+60
(pm-candidate! manager)
+61
(let ((token (path-manager-challenge! manager #u8(2) 0)))
+62
(assert-true
+63
(and token (bytes=? #u8(1) (path-manager-address manager))
+64
(path-manager-response! manager token 1)
+65
(bytes=? #u8(2) (path-manager-address manager))
+66
(not (path-manager-candidate manager))
+67
(not (path-manager-response! manager token 1))
+68
(let ((events (path-manager-take-events! manager)))
+69
(and (= 2 (length events))
+70
(pm-kind? (car events) 'path-changed #u8(2) 'non-probing)
+71
(pm-kind? (cadr events) 'path-validated #u8(2) 'active)))))))))
+72
(test "path-manager: unknown responses cannot promote or extend candidate lifetime"
+73
(pm-with-manager
+74
(lambda (manager)
+75
(pm-candidate! manager)
+76
(assert-true
+77
(and (not (path-manager-response! manager (make-bytevector 8 0) 2999))
+78
(begin (path-manager-tick! manager 3000) #t)
+79
(not (path-manager-candidate manager))
+80
(bytes=? #u8(1) (path-manager-address manager)))))))
+81
(test "path-manager: a late correct response cannot revive an expired candidate"
+82
(pm-with-manager
+83
(lambda (manager)
+84
(pm-candidate! manager)
+85
(let ((token (path-manager-challenge! manager #u8(2) 0)))
+86
(assert-true
+87
(and token (not (path-manager-response! manager token 3000))
+88
(not (path-manager-candidate manager))
+89
(bytes=? #u8(1) (path-manager-address manager))))))))
+90
(test "path-manager: probing packets can receive bounded replies but cannot initiate a switch"
+91
(pm-with-manager
+92
(lambda (manager)
+93
(path-manager-observe! manager #u8(2) 400 10 #t 0)
+94
(assert-true
+95
(and (not (path-manager-challenge! manager #u8(2) 0))
+96
(not (path-manager-previous-address manager))
+97
(path-manager-send! manager #u8(2) 1200 0)
+98
(not (path-manager-send! manager #u8(2) 1 0))
+99
(bytes=? #u8(1) (path-manager-address manager))
+100
(path-manager-observe! manager #u8(2) 1200 1 #f 2000)
+101
(path-manager-challenge! manager #u8(2) 2000)
+102
(bytes=? #u8(1) (path-manager-previous-address manager)))))))
+103
(test "path-manager: reordered non-probing packets cannot replace a newer observation"
+104
(let ((manager (make-path-manager #u8(1) 'server 0 highest: 100)))
+105
(dynamic-wind
+106
(lambda () #t)
+107
(lambda ()
+108
(assert-true
+109
(and (not (path-manager-observe! manager #u8(2) 1200 99 #f 0))
+110
(not (path-manager-candidate manager))
+111
(path-manager-observe! manager #u8(2) 1200 101 #f 0)
+112
(not (path-manager-observe! manager #u8(3) 1200 100 #f 0))
+113
(bytes=? #u8(2) (path-manager-candidate manager)))))
+114
(lambda () (path-manager-close! manager)))))
+115
(test "path-manager: a newer non-probing packet on the active path cancels spurious migration"
+116
(pm-with-manager
+117
(lambda (manager)
+118
(pm-candidate! manager)
+119
(let ((token (path-manager-challenge! manager #u8(2) 0)))
+120
(path-manager-observe! manager #u8(1) 100 2 #f 1)
+121
(assert-true
+122
(and token (not (path-manager-candidate manager))
+123
(not (path-manager-previous-address manager))
+124
(not (path-manager-response! manager token 1))
+125
(bytes=? #u8(1) (path-manager-address manager))))))))
+126
(test "path-manager: clients discard packets from unknown server addresses"
+127
(let ((manager (make-path-manager #u8(1) 'client 0)))
+128
(dynamic-wind
+129
(lambda () #t)
+130
(lambda ()
+131
(assert-true
+132
(and (not (pm-candidate! manager))
+133
(not (path-manager-observe! manager #u8(2) 1200 2 #t 0))
+134
(not (path-manager-candidate manager))
+135
(null? (path-manager-take-events! manager))
+136
(bytes=? #u8(1) (path-manager-address manager)))))
+137
(lambda () (path-manager-close! manager)))))
+138
(test "path-manager: the previously active path gets an independent fresh validation exchange"
+139
(pm-with-manager
+140
(lambda (manager)
+141
(pm-candidate! manager)
+142
(let ((old-token (path-manager-challenge! manager #u8(1) 0))
+143
(new-token (path-manager-challenge! manager #u8(2) 0)))
+144
(assert-true
+145
(and old-token new-token
+146
(path-manager-response! manager old-token 1)
+147
(bytes=? #u8(1) (path-manager-address manager))
+148
(bytes=? #u8(2) (path-manager-candidate manager))
+149
(not (path-manager-previous-address manager))
+150
(path-manager-response! manager new-token 1)
+151
(bytes=? #u8(2) (path-manager-address manager))))))))
+152
(test "path-manager: failed candidate and previous probes leave the working path usable"
+153
(pm-with-manager
+154
(lambda (manager)
+155
(pm-candidate! manager)
+156
(path-manager-tick! manager 3000)
+157
(assert-true
+158
(and (not (path-manager-candidate manager))
+159
(not (path-manager-previous-address manager))
+160
(bytes=? #u8(1) (path-manager-address manager))
+161
(path-manager-send! manager #u8(1) 1200 3000)
+162
(= 3 (length (path-manager-take-events! manager))))))))
+163
(test "path-manager: higher numbered candidate supersedes one bounded pending attempt"
+164
(pm-with-manager
+165
(lambda (manager)
+166
(pm-candidate! manager)
+167
(path-manager-observe! manager #u8(3) 1200 2 #f 1)
+168
(assert-true
+169
(and (bytes=? #u8(3) (path-manager-candidate manager))
+170
(not (path-manager-challenge! manager #u8(2) 1))
+171
(bytes=? #u8(1) (path-manager-address manager))
+172
(= 3 (length (path-manager-take-events! manager))))))))
+173
(test "path-manager: diagnostic backlog is bounded and drains with owned addresses"
+174
(pm-with-manager
+175
(lambda (manager)
+176
(do ((number 1 (+ number 1))) ((= number 40))
+177
(path-manager-observe! manager (bytevector (+ number 1)) 1200 number #f 0))
+178
(let ((events (path-manager-take-events! manager)))
+179
(assert-true
+180
(and (= 16 (length events)) (null? (path-manager-take-events! manager))
+181
(bytes=? #u8(40) (path-manager-candidate manager))
+182
(bytes=? #u8(1) (path-manager-address manager))))))))
+183
(test "path-manager: invalid metadata and backwards time do not replace a valid candidate"
+184
(pm-with-manager
+185
(lambda (manager)
+186
(pm-candidate! manager)
+187
(path-manager-tick! manager 1)
+188
(assert-error (path-manager-observe! manager #u8(3) 1200 2 #f 0))
+189
(assert-error (path-manager-observe! manager #u8() 1200 2 #f 1))
+190
(assert-error (path-manager-observe! manager #u8(3) 1200 -1 #f 1))
+191
(assert-error (path-manager-send! manager #u8(3) 1201 1))
+192
(assert-error (path-manager-response! manager #u8(0) 1))
+193
(assert-true (bytes=? #u8(2) (path-manager-candidate manager))))))
+194
(test "path-manager: close is idempotent and refuses further observations or sends"
+195
(pm-with-manager
+196
(lambda (manager)
+197
(pm-candidate! manager)
+198
(path-manager-close! manager)
+199
(path-manager-close! manager)
+200
(assert-false (path-manager-address manager))
+201
(assert-false (path-manager-candidate manager))
+202
(assert-false (path-manager-previous-address manager))
+203
(assert-equal '() (path-manager-take-events! manager))
+204
(assert-error (pm-candidate! manager))
+205
(assert-error (path-manager-send! manager #u8(1) 1 0))
+206
(assert-error (path-manager-challenge! manager #u8(1) 0))))))
test/test-path-validation.sgladded
@@ -0,0 +1,189 @@
+1
(import (sigil test)
+2
(sigil math)
+3
(sigil quic bytes)
+4
(sigil quic path validation))
+5
+6
(define (pv-with-path procedure)
+7
(let ((path (make-path-validation #u8(4 127 0 0 1 1 187) 0)))
+8
(dynamic-wind
+9
(lambda () #t)
+10
(lambda () (procedure path))
+11
(lambda () (path-validation-close! path)))))
+12
+13
(test-group "Per-path validation and amplification reservations"
+14
(test "path-validation: invalid construction refuses before allocating a path"
+15
(for-each
+16
(lambda (thunk) (assert-error (thunk)))
+17
(list
+18
(lambda () (make-path-validation #u8() 0))
+19
(lambda () (make-path-validation (make-bytevector 65 0) 0))
+20
(lambda () (make-path-validation #u8(1) -1))
+21
(lambda () (make-path-validation #u8(1) 0 pto: 999))
+22
(lambda () (make-path-validation #u8(1) 0 pto: 60001))
+23
(lambda () (make-path-validation #u8(1) 0 mtu?: #t))
+24
(lambda () (make-path-validation #u8(1) 0 validated?: 1)))))
+25
(test "path-validation: address ownership isolates caller and returned bytes"
+26
(let* ((address (make-bytevector 7 1))
+27
(path (make-path-validation address 0)))
+28
(dynamic-wind
+29
(lambda () #t)
+30
(lambda ()
+31
(bytevector-u8-set! address 0 2)
+32
(let ((copy (path-validation-address path)))
+33
(bytevector-u8-set! copy 1 3)
+34
(assert-equal #u8(1 1 1 1 1 1 1) (path-validation-address path))))
+35
(lambda () (path-validation-close! path)))))
+36
(test "path-validation: zero credit refuses and exact threefold output is allowed once"
+37
(pv-with-path
+38
(lambda (path)
+39
(assert-true
+40
(and (not (path-validation-send! path 1 0))
+41
(not (path-validation-challenge! path 1200 0))
+42
(path-validation-credit! path 100 0)
+43
(path-validation-send! path 300 0)
+44
(not (path-validation-send! path 1 0)))))))
+45
(test "path-validation: credit for another path cannot authorize a probe"
+46
(pv-with-path
+47
(lambda (first)
+48
(pv-with-path
+49
(lambda (second)
+50
(path-validation-credit! first 400 0)
+51
(path-validation-credit! second 20 0)
+52
(assert-true
+53
(and (bytevector? (path-validation-challenge! first 1200 0))
+54
(not (path-validation-challenge! second 1200 0))
+55
(path-validation-send! second 60 0)
+56
(not (path-validation-send! second 1 0)))))))))
+57
(test "path-validation: matching padded challenge validates address and MTU"
+58
(pv-with-path
+59
(lambda (path)
+60
(path-validation-credit! path 400 0)
+61
(let ((challenge (path-validation-challenge! path 1200 0)))
+62
(assert-equal 'validated (path-validation-response! path challenge 1))
+63
(assert-true (path-validation-address-valid? path))
+64
(assert-true (path-validation-mtu-valid? path))
+65
(assert-true (path-validation-send! path 1200 1))
+66
(assert-false (path-validation-response! path challenge 1))))))
+67
(test "path-validation: delayed response can match an earlier outstanding probe"
+68
(pv-with-path
+69
(lambda (path)
+70
(path-validation-credit! path 1200 0)
+71
(let* ((first (path-validation-challenge! path 1200 0))
+72
(second (path-validation-challenge! path 1200 1000)))
+73
(assert-false (bytes=? first second))
+74
;; No source-address argument: the connection finds the challenged
+75
;; path by token even when the response arrives on its old path.
+76
(assert-equal 'validated (path-validation-response! path first 1500))))))
+77
(test "path-validation: short probe requires a separate padded MTU validation"
+78
(pv-with-path
+79
(lambda (path)
+80
(path-validation-credit! path 100 0)
+81
(let ((small (path-validation-challenge! path 100 0)))
+82
(assert-true
+83
(and (eq? 'address-validated (path-validation-response! path small 1))
+84
(path-validation-address-valid? path)
+85
(not (path-validation-mtu-valid? path))
+86
(eq? 'validating (path-validation-status path))
+87
(not (path-validation-challenge! path 100 1))
+88
(let ((large (path-validation-challenge! path 1200 1)))
+89
(and (bytevector? large)
+90
(eq? 'validated (path-validation-response! path large 2))))))))))
+91
(test "path-validation: probes are paced fresh and bounded before deadline"
+92
(pv-with-path
+93
(lambda (path)
+94
(path-validation-credit! path 1200 0)
+95
(let ((first (path-validation-challenge! path 1200 0)))
+96
(assert-true
+97
(and (not (path-validation-challenge! path 1200 999))
+98
(let* ((second (path-validation-challenge! path 1200 1000))
+99
(third (path-validation-challenge! path 1200 2000)))
+100
(and (bytevector? second) (bytevector? third)
+101
(not (bytes=? first second)) (not (bytes=? first third))
+102
(not (bytes=? second third))
+103
(not (path-validation-challenge! path 1200 2999))
+104
(path-validation-tick! path 3000)
+105
(eq? 'failed (path-validation-status path))))))))))
+106
(test "path-validation: unknown and malformed responses do not validate"
+107
(pv-with-path
+108
(lambda (path)
+109
(assert-false (path-validation-response! path #u8(0 1 2 3 4 5 6 7) 0))
+110
(assert-error (path-validation-response! path #u8(1) 0))
+111
(assert-false (path-validation-address-valid? path))
+112
(assert-false (path-validation-mtu-valid? path)))))
+113
(test "path-validation: data and unknown responses cannot extend expiry"
+114
(pv-with-path
+115
(lambda (path)
+116
(path-validation-credit! path 400 0)
+117
(let ((challenge (path-validation-challenge! path 1200 0)))
+118
(path-validation-credit! path 1200 2998)
+119
(assert-true
+120
(and (not (path-validation-response! path (flip-byte challenge 0) 2999))
+121
(path-validation-tick! path 3000)
+122
(not (path-validation-send! path 1 3000))
+123
(not (path-validation-credit! path 1200 3000))))))))
+124
(test "path-validation: response at the expired deadline cannot revive a path"
+125
(pv-with-path
+126
(lambda (path)
+127
(path-validation-credit! path 400 0)
+128
(let ((challenge (path-validation-challenge! path 1200 0)))
+129
(assert-true
+130
(and (not (path-validation-response! path challenge 3000))
+131
(eq? 'failed (path-validation-status path))
+132
(not (path-validation-address-valid? path))))))))
+133
(test "path-validation: address reachability alone cannot survive a failed MTU phase"
+134
(pv-with-path
+135
(lambda (path)
+136
(path-validation-credit! path 100 0)
+137
(let ((small (path-validation-challenge! path 100 0)))
+138
(assert-true
+139
(and (eq? 'address-validated (path-validation-response! path small 1))
+140
(bytevector? (path-validation-challenge! path 1200 1))
+141
(not (path-validation-response! path small 3000))
+142
(path-validation-tick! path 3001)
+143
(not (path-validation-address-valid? path))
+144
(not (path-validation-mtu-valid? path))))))))
+145
(test "path-validation: mutating a returned challenge does not mutate the matcher"
+146
(pv-with-path
+147
(lambda (path)
+148
(path-validation-credit! path 400 0)
+149
(let* ((challenge (path-validation-challenge! path 1200 0))
+150
(original (bytevector-copy challenge)))
+151
(bytevector-u8-set! challenge 0 (bitwise-xor 1 (bytevector-u8-ref challenge 0)))
+152
(assert-true
+153
(and (not (path-validation-response! path challenge 1))
+154
(eq? 'validated (path-validation-response! path original 1))))))))
+155
(test "path-validation: invalid size and backwards time cannot spend credit"
+156
(pv-with-path
+157
(lambda (path)
+158
(path-validation-credit! path 100 10)
+159
(assert-error (path-validation-send! path 1 9))
+160
(assert-error (path-validation-send! path 0 10))
+161
(assert-error (path-validation-send! path 1201 10))
+162
(assert-error (path-validation-credit! path 65528 10))
+163
(assert-true
+164
(and (path-validation-send! path 300 10)
+165
(not (path-validation-send! path 1 10)))))))
+166
(test "path-validation: an already validated original path needs no amplification credit"
+167
(let ((path (make-path-validation #u8(1) 0 validated?: #t mtu?: #t)))
+168
(dynamic-wind
+169
(lambda () #t)
+170
(lambda ()
+171
(assert-equal 'validated (path-validation-status path))
+172
(assert-true (path-validation-send! path 1200 0))
+173
(assert-false (path-validation-tick! path 1000000))
+174
(assert-true (path-validation-mtu-valid? path)))
+175
(lambda () (path-validation-close! path)))))
+176
(test "path-validation: idempotent close retires validation and refuses further work"
+177
(pv-with-path
+178
(lambda (path)
+179
(path-validation-credit! path 400 0)
+180
(let ((challenge (path-validation-challenge! path 1200 0)))
+181
(path-validation-close! path)
+182
(path-validation-close! path)
+183
(assert-equal 'closed (path-validation-status path))
+184
(assert-false (path-validation-address-valid? path))
+185
(assert-false (path-validation-mtu-valid? path))
+186
(assert-error (path-validation-credit! path 400 0))
+187
(assert-error (path-validation-send! path 1 0))
+188
(assert-error (path-validation-challenge! path 1200 0))
+189
(assert-error (path-validation-response! path challenge 0)))))))
test/test-peer-ids.sgladded
@@ -0,0 +1,264 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic wire)
+4
(sigil quic frame)
+5
(sigil quic error)
+6
(sigil quic connection peer ids))
+7
+8
(define (pic-with-pool procedure)
+9
(let ((pool (make-peer-id-pool #u8(1))))
+10
(dynamic-wind
+11
(lambda () #t)
+12
(lambda () (procedure pool))
+13
(lambda () (peer-id-pool-close! pool)))))
+14
+15
(define (pic-frame sequence retire cid token)
+16
(make-quic-frame 24
+17
(list (cons 'sequence sequence) (cons 'retire-prior-to retire)
+18
(cons 'connection-id cid) (cons 'token token))))
+19
+20
(define (pic-new! pool sequence retire)
+21
(peer-id-pool-receive! pool
+22
(pic-frame sequence retire (bytevector (+ sequence 1)) (make-bytevector 16 0))))
+23
+24
(define (pic-error? code procedure)
+25
(guard (exn
+26
(else (and (quic-error? exn) (= code (quic-error-code exn))
+27
(= 24 (quic-error-frame-type exn)))))
+28
(procedure)
+29
#f))
+30
+31
(define (pic-retiring pool)
+32
(map (lambda (frame) (frame-ref frame 'sequence)) (peer-id-pool-retirements pool)))
+33
+34
(test-group "Peer CID capacity, retirement and path ownership"
+35
(test "peer-ids: invalid construction refuses ambiguous authenticated metadata"
+36
(for-each
+37
(lambda (procedure) (assert-error (procedure)))
+38
(list
+39
(lambda () (make-peer-id-pool (make-bytevector 21 0)))
+40
(lambda () (make-peer-id-pool #u8(1) limit: 1))
+41
(lambda () (make-peer-id-pool #u8(1) limit: 9))
+42
(lambda () (make-peer-id-pool #u8(1) token: #u8(0)))
+43
(lambda () (make-peer-id-pool #u8() token: (make-bytevector 16 0)))
+44
(lambda () (make-peer-id-pool #u8(1) preferred: (cons #u8(1) (make-bytevector 16 0))))
+45
(lambda () (make-peer-id-pool #u8() preferred: (cons #u8(2) (make-bytevector 16 0)))))))
+46
(test "peer-ids: initial CID is owned and already assigned to its original path"
+47
(let* ((cid (make-bytevector 1 1)) (pool (make-peer-id-pool cid)))
+48
(dynamic-wind
+49
(lambda () #t)
+50
(lambda ()
+51
(bytevector-u8-set! cid 0 3)
+52
(let ((copy (peer-id-pool-id pool 0)))
+53
(bytevector-u8-set! copy 0 4)
+54
(assert-true
+55
(and (bytes=? #u8(1) (peer-id-pool-id pool 0))
+56
(not (peer-id-pool-acquire! pool))
+57
(equal? '(0) (peer-id-pool-sequences pool))))))
+58
(lambda () (peer-id-pool-close! pool)))))
+59
(test "peer-ids: incoming CID and token are owned and assigned at most once"
+60
(pic-with-pool
+61
(lambda (pool)
+62
(let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0))
+63
(frame (pic-frame 1 0 cid token)))
+64
(peer-id-pool-receive! pool frame)
+65
(bytevector-u8-set! cid 0 3)
+66
(bytevector-u8-set! token 0 5)
+67
(assert-true
+68
(and (bytes=? #u8(2) (peer-id-pool-id pool 1))
+69
(pic-new! pool 1 0) (= 1 (peer-id-pool-acquire! pool))
+70
(not (peer-id-pool-acquire! pool)) (= 2 (peer-id-pool-count pool))))))))
+71
(test "peer-ids: duplicate advertisement neither consumes capacity nor releases ownership"
+72
(pic-with-pool
+73
(lambda (pool)
+74
(pic-new! pool 1 0)
+75
(peer-id-pool-acquire! pool)
+76
(assert-true
+77
(and (pic-new! pool 1 0) (= 2 (peer-id-pool-count pool))
+78
(not (peer-id-pool-acquire! pool)) (null? (pic-retiring pool)))))))
+79
(test "peer-ids: conflicting sequence CID or token and reused CID are typed violations"
+80
(pic-with-pool
+81
(lambda (pool)
+82
(pic-new! pool 1 0)
+83
(assert-true
+84
(and (pic-error? 10
+85
(lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(3) (make-bytevector 16 0)))))
+86
(pic-error? 10
+87
(lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(2) (make-bytevector 16 1)))))
+88
(pic-error? 10
+89
(lambda () (peer-id-pool-receive! pool (pic-frame 2 0 #u8(2) (make-bytevector 16 0)))))
+90
(bytes=? #u8(2) (peer-id-pool-id pool 1)) (= 2 (peer-id-pool-count pool)))))))
+91
(test "peer-ids: active limit failure leaves the original inventory unchanged"
+92
(pic-with-pool
+93
(lambda (pool)
+94
(pic-new! pool 1 0)
+95
(assert-true
+96
(and (pic-error? 9 (lambda () (pic-new! pool 2 0)))
+97
(equal? '(0 1) (peer-id-pool-sequences pool))
+98
(not (peer-id-pool-id pool 2)) (null? (pic-retiring pool)))))))
+99
(test "peer-ids: RetirePriorTo frees active capacity before admitting its replacement"
+100
(pic-with-pool
+101
(lambda (pool)
+102
(pic-new! pool 1 0)
+103
(assert-true
+104
(and (pic-new! pool 2 1) (= 2 (peer-id-pool-count pool))
+105
(not (peer-id-pool-id pool 0))
+106
(equal? '(1 2) (peer-id-pool-sequences pool))
+107
(equal? '(0) (pic-retiring pool)))))))
+108
(test "peer-ids: reordered smaller RetirePriorTo cannot undo a peer retirement floor"
+109
(pic-with-pool
+110
(lambda (pool)
+111
(pic-new! pool 2 2)
+112
(assert-true
+113
(and (not (pic-new! pool 1 0)) (= 1 (peer-id-pool-count pool))
+114
(not (peer-id-pool-id pool 1))
+115
(equal? '(0 1) (pic-retiring pool))
+116
(= 2 (peer-id-pool-acquire! pool)))))))
+117
(test "peer-ids: acknowledged retirement cannot resurrect while an older path stays live"
+118
(pic-with-pool
+119
(lambda (pool)
+120
(pic-new! pool 2 0)
+121
(peer-id-pool-retire! pool 2)
+122
(assert-true
+123
(and (peer-id-pool-ack! pool 2) (not (peer-id-pool-ack! pool 2))
+124
(bytes=? #u8(1) (peer-id-pool-id pool 0))
+125
(not (pic-new! pool 2 0)) (not (peer-id-pool-id pool 2))
+126
(not (peer-id-pool-acquire! pool))
+127
(equal? '(2) (pic-retiring pool)))))))
+128
(test "peer-ids: unseen older sequence gaps are retired without displacing a live exception"
+129
(pic-with-pool
+130
(lambda (pool)
+131
(pic-new! pool 3 0)
+132
(peer-id-pool-retire! pool 3)
+133
(peer-id-pool-ack! pool 3)
+134
(assert-true
+135
(and (not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1))
+136
(= 1 (peer-id-pool-count pool))
+137
(bytes=? #u8(1) (peer-id-pool-id pool 0))
+138
(equal? '(1) (pic-retiring pool)))))))
+139
(test "peer-ids: twice-limit pending retirement budget is bounded and errors are atomic"
+140
(pic-with-pool
+141
(lambda (pool)
+142
(for-each
+143
(lambda (sequence)
+144
(pic-new! pool sequence 0)
+145
(peer-id-pool-retire! pool sequence))
+146
'(2 3 4 5))
+147
(pic-new! pool 6 0)
+148
(assert-true
+149
(and (pic-error? 9 (lambda () (pic-new! pool 1 0)))
+150
(pic-error? 9 (lambda () (peer-id-pool-retire! pool 6)))
+151
(pic-error? 9 (lambda () (pic-new! pool 7 7)))
+152
(= 2 (peer-id-pool-count pool))
+153
(equal? '(0 6) (peer-id-pool-sequences pool))
+154
(equal? '(2 3 4 5) (pic-retiring pool))
+155
(peer-id-pool-ack! pool 2)
+156
(peer-id-pool-retire! pool 6)
+157
(equal? '(3 4 5 6) (pic-retiring pool)))))))
+158
(test "peer-ids: retiring IDs remain pending and cannot be assigned again"
+159
(pic-with-pool
+160
(lambda (pool)
+161
(pic-new! pool 1 0)
+162
(peer-id-pool-acquire! pool)
+163
(assert-true
+164
(and (peer-id-pool-retire! pool 1) (not (peer-id-pool-retire! pool 1))
+165
(not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1))
+166
(not (peer-id-pool-acquire! pool))
+167
(equal? '(1) (pic-retiring pool))
+168
(equal? '(1) (pic-retiring pool)))))))
+169
(test "peer-ids: zero-length peer source disables spare issue and retirement"
+170
(let ((pool (make-peer-id-pool #u8())))
+171
(dynamic-wind
+172
(lambda () #t)
+173
(lambda ()
+174
(assert-true
+175
(and (bytes=? #u8() (peer-id-pool-id pool 0))
+176
(pic-error? 10 (lambda () (pic-new! pool 1 0)))
+177
(not (peer-id-pool-acquire! pool))))
+178
(assert-error (peer-id-pool-retire! pool 0)))
+179
(lambda () (peer-id-pool-close! pool)))))
+180
(test "peer-ids: authenticated preferred CID is owned sequence one and consumes capacity"
+181
(let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0))
+182
(pool (make-peer-id-pool #u8(1) preferred: (cons cid token))))
+183
(dynamic-wind
+184
(lambda () #t)
+185
(lambda ()
+186
(bytevector-u8-set! cid 0 4)
+187
(bytevector-u8-set! token 0 5)
+188
(assert-true
+189
(and (= 2 (peer-id-pool-count pool)) (pic-new! pool 1 0)
+190
(= 1 (peer-id-pool-acquire! pool))
+191
(pic-error? 9 (lambda () (pic-new! pool 2 0))))))
+192
(lambda () (peer-id-pool-close! pool)))))
+193
(test "peer-ids: acknowledgment never forgets a live or unknown CID"
+194
(pic-with-pool
+195
(lambda (pool)
+196
(assert-true
+197
(and (not (peer-id-pool-ack! pool 0)) (not (peer-id-pool-ack! pool 1))
+198
(bytes=? #u8(1) (peer-id-pool-id pool 0)) (= 1 (peer-id-pool-count pool)))))))
+199
(test "peer-ids: maximum wire sequence retires without overflow or resurrection"
+200
(pic-with-pool
+201
(lambda (pool)
+202
(let ((frame (pic-frame quic-varint-maximum 0 #u8(2) (make-bytevector 16 0))))
+203
(peer-id-pool-receive! pool frame)
+204
(peer-id-pool-retire! pool quic-varint-maximum)
+205
(peer-id-pool-ack! pool quic-varint-maximum)
+206
(assert-true
+207
(and (not (peer-id-pool-receive! pool frame))
+208
(not (peer-id-pool-id pool quic-varint-maximum))
+209
(bytes=? #u8(1) (peer-id-pool-id pool 0))
+210
(equal? (list quic-varint-maximum) (pic-retiring pool))))))))
+211
(test "peer-ids: malformed NEW fields are typed frame errors without effects"
+212
(pic-with-pool
+213
(lambda (pool)
+214
(for-each
+215
(lambda (frame)
+216
(assert-true (pic-error? 7 (lambda () (peer-id-pool-receive! pool frame)))))
+217
(list
+218
(pic-frame 1 2 #u8(2) (make-bytevector 16 0))
+219
(pic-frame -1 0 #u8(2) (make-bytevector 16 0))
+220
(pic-frame 1 0 #u8() (make-bytevector 16 0))
+221
(pic-frame 1 0 #u8(2) #u8(0))))
+222
(assert-equal '(0) (peer-id-pool-sequences pool))
+223
(assert-equal '() (pic-retiring pool)))))
+224
(test "peer-ids: initial sequence learns a token once and then enforces its binding"
+225
(pic-with-pool
+226
(lambda (pool)
+227
(let* ((token (make-bytevector 16 0))
+228
(frame (pic-frame 0 0 #u8(1) token)))
+229
(peer-id-pool-receive! pool frame)
+230
(bytevector-u8-set! token 0 1)
+231
(assert-true
+232
(and (pic-error? 10 (lambda () (peer-id-pool-receive! pool frame)))
+233
(peer-id-pool-receive! pool (pic-frame 0 0 #u8(1) (make-bytevector 16 0)))
+234
(= 1 (peer-id-pool-count pool)) (not (peer-id-pool-acquire! pool))))))))
+235
(test "peer-ids: a duplicate sequence can advance RetirePriorTo without releasing its path"
+236
(let ((pool (make-peer-id-pool #u8(1) limit: 3)))
+237
(dynamic-wind
+238
(lambda () #t)
+239
(lambda ()
+240
(pic-new! pool 1 0)
+241
(pic-new! pool 2 0)
+242
(peer-id-pool-acquire! pool)
+243
(peer-id-pool-acquire! pool)
+244
(assert-true
+245
(and (pic-new! pool 2 2) (= 1 (peer-id-pool-count pool))
+246
(equal? '(0 1) (pic-retiring pool))
+247
(not (peer-id-pool-acquire! pool)) (not (pic-new! pool 1 0))
+248
(bytes=? #u8(3) (peer-id-pool-id pool 2)))))
+249
(lambda () (peer-id-pool-close! pool)))))
+250
(test "peer-ids: close clears inventories and refuses subsequent mutations"
+251
(pic-with-pool
+252
(lambda (pool)
+253
(pic-new! pool 1 0)
+254
(peer-id-pool-retire! pool 1)
+255
(peer-id-pool-close! pool)
+256
(peer-id-pool-close! pool)
+257
(assert-equal 0 (peer-id-pool-count pool))
+258
(assert-equal '() (peer-id-pool-sequences pool))
+259
(assert-equal '() (pic-retiring pool))
+260
(assert-false (peer-id-pool-id pool 0))
+261
(assert-error (pic-new! pool 1 0))
+262
(assert-error (peer-id-pool-acquire! pool))
+263
(assert-error (peer-id-pool-retire! pool 1))
+264
(assert-error (peer-id-pool-ack! pool 1))))))
test/test-random-ids.sgladded
@@ -0,0 +1,49 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic frame)
+4
(sigil quic connection ids))
+5
+6
(define (ri-with-pool initial procedure (keys: (maximum 256)))
+7
(let ((pool (make-local-id-pool initial maximum: maximum)))
+8
(dynamic-wind
+9
(lambda () #t)
+10
(lambda () (procedure pool))
+11
(lambda () (local-id-pool-close! pool)))))
+12
+13
(test-group "Bounded random spare-CID reservations"
+14
(test "random-ids: fresh reservation stays unroutable until its exact advertisement commits"
+15
(ri-with-pool #u8(1 2 3 4 5 6 7 8)
+16
(lambda (pool)
+17
(let ((frame (local-id-pool-reserve-random! pool)))
+18
(assert-true
+19
(and frame (= 1 (frame-ref frame 'sequence))
+20
(= 8 (bytevector-length (frame-ref frame 'connection-id)))
+21
(= 16 (bytevector-length (frame-ref frame 'token)))
+22
(not (bytes=? #u8(1 2 3 4 5 6 7 8) (frame-ref frame 'connection-id)))
+23
(not (local-id-pool-route pool (frame-ref frame 'connection-id)))
+24
(local-id-pool-check! pool frame)
+25
(local-id-pool-commit! pool frame)
+26
(= 1 (local-id-pool-route pool (frame-ref frame 'connection-id)))))))))
+27
(test "random-ids: queued reservations consume peer capacity without making routes"
+28
(ri-with-pool #u8(1)
+29
(lambda (pool)
+30
(let ((frame (local-id-pool-reserve-random! pool)))
+31
(assert-true
+32
(and frame (= 2 (local-id-pool-count pool))
+33
(not (local-id-pool-route pool (frame-ref frame 'connection-id)))
+34
(not (local-id-pool-reserve-random! pool))))))))
+35
(test "random-ids: zero-length routing cannot acquire spare IDs"
+36
(ri-with-pool #u8()
+37
(lambda (pool)
+38
(assert-true
+39
(and (not (local-id-pool-reserve-random! pool)) (= 1 (local-id-pool-count pool)))))))
+40
(test "random-ids: lifetime exhaustion preserves live routes and refuses sequence reuse"
+41
(ri-with-pool #u8(1)
+42
(lambda (pool)
+43
(let ((frame (local-id-pool-reserve-random! pool)))
+44
(assert-true
+45
(and frame (local-id-pool-commit! pool frame)
+46
(local-id-pool-retire! pool 1 #u8(1))
+47
(= 1 (local-id-pool-count pool))
+48
(not (local-id-pool-reserve-random! pool))
+49
(= 0 (local-id-pool-route pool #u8(1))))))) maximum: 2)))