Support authenticated path migration and addressed UDP output
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.
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(-)README.mdmodified
discard partial key material if a later derivation throws. Borrowed inputs andcomplete returned vectors keep their caller's ownership.Authenticated PATH_CHALLENGE frames receive matching PATH_RESPONSE frames onthe endpoint's pinned path, independently of an application handler. Responsesprecede application backlog and are not retransmitted on PTO. Path probes andresponses expand to 1200 bytes when amplification credit permits; ordinarycongestion and AEAD limits still apply. This does not add migration or anactive path-validation state machine.Authenticated PATH_CHALLENGE frames receive matching PATH_RESPONSE frames,independently of an application handler. Address-bound connections route eachresponse to the challenge's source. Responses precede application backlog,expire while blocked and are not retransmitted on PTO. Independent candidateand previous-path validation uses fresh, paced probes and separate three-timesreceived-byte budgets. Promotion requires reachability and a 1200-byte MTUcheck. The UDP endpoint below supplies this address-bound adapter on loopback.Zero-length CID policy is enforced after packet authentication and before anyapplication handler: NEW_CONNECTION_ID is forbidden when sending a zero-lengthframe type. RETIRE_CONNECTION_ID above the largest issued sequence also closeswith PROTOCOL_VIOLATION. A server's preferred-address CID has sequence 1; othercaller-issued NEW_CONNECTION_ID sequences count only after packet emission,including PTO retransmission, rather than queue admission. Full nonzero CIDinventory, routing, rotation and retirement remain under audit.including PTO retransmission, rather than queue admission. Address-boundconnections own spare-ID issuance and path assignment: reservations becomeroutable only after emission, retired aliases stop routing immediately, andRETIRE_CONNECTION_ID remains pending until its carrying packet is acknowledged.These pools enforce the negotiated active limit and bounded lifetime inventory.`(sigil quic connection space)` provides bounded PN/ACK bookkeeping foreach encryption level. `make-packet-space level` accepts `range-limit:`,parameters. Start clients with `quic-start! connection now`, feed datagramswith `quic-receive! connection bytes now`, call `quic-tick! connection now`and immediately send the ordered list from `quic-take-datagrams! connection now`.Times are monotonic exact milliseconds. The endpoint owns one fixed peer address.Times are monotonic exact milliseconds. The UDP endpoint binds the originalpeer address before starting or receiving the handshake.A transport adapter can instead call `quic-bind-peer-address! connection bytes`once before starting or receiving the handshake. The adapter supplies canonicalIP-and-port bytes, bounded to 1–64 bytes, and must pass the actual source through`quic-receive! connection bytes now address: source`. It drains protected outputwith `quic-take-addressed-datagrams! connection now`. Each output has copiedbytes, destination address and destination CID through `quic-output-bytes`,`quic-output-address` and `quic-output-id`. Check `quic-output-valid? connectionoutput` immediately before sending retained output: path promotion, CIDretirement or closure can invalidate a previously queued datagram. Raw datagramoutput is unavailable after binding an address.`quic-peer-address` returns the current validated destination. Drain up to sixteenretained diagnostics with `quic-take-path-events!`; each is a vector containing`path-changed`, `path-validated` or `path-failed`, owned address bytes and a reasonsymbol. A changed-path event does not authorize sending application data there.Only authenticated, replay-checked, handler-ready packets earn path credit;clients discard unknown server sources. Server migration uses validated pathsand separate peer CIDs, requeues reliable flight under fresh packet numbers andresets congestion accounting. DATAGRAM payloads are never retransmitted.After validating a Retry token, the caller can pass `retry-source-id:` to`make-quic-server`. The original positional destination ID remains the firsttransfer beyond the receive-window capacity and reset/cancellation under loss.`(sigil quic endpoint)` is a private IPv4 loopback UDP adapter. It binds only`127.0.0.1`, uses a dedicated nonblocking socket, pins one peer port, andlimits receive work and retained protected datagrams. It provides no publiclistener, migration, or multi-connection service.`127.0.0.1` and uses a dedicated nonblocking socket for one connection. It bindsthe original peer before the handshake, sends each protected datagram to itsrecorded destination and validates confirmed-server peer port changes. Clientskeep their original server-source restriction. Configurable interface binds,IPv6 and multiple connections remain outside this adapter.`open-quic-endpoint port: 0` chooses an ephemeral port, available through`quic-endpoint-port`. Attach a fresh client connection/stream withthis check. Initial integrity is not address validation: Retry remainsnecessary before public operation. Failed candidates before confirmationrelease the slot and discard their output while preserving the originalfactory and configured port restriction; a confirmed connection retains theendpoint's single-connection lifetime.factory and configured port restriction. After confirmation, a server passescandidate source ports to authenticated path validation; the admission pindoes not prevent NAT rebinding. The endpoint retains one connection throughout.`quic-endpoint-peer-address` returns owned canonical peer bytes or `#f` beforeadmission. `quic-endpoint-take-path-events!` drains the connection's boundedpath events. Application data moves only after validation; a `path-changed`event alone does not change its destination. Retained ciphertext is checkedagain before each send, so CID retirement and path promotion can invalidate it.For private Retry testing, call `quic-endpoint-accept-retry! endpoint factory`instead. Its factory receives three copied IDs: original destination, clientCall `quic-endpoint-step! endpoint wait-ms: milliseconds` repeatedly alongsideapplication reads/writes. Each call waits at most 50 milliseconds and drivesreceive, timers and protected output. Temporary send blockage retains theexact datagram bytes for at most one second; failures close the socket andconnection. `quic-endpoint-connection`, `quic-endpoint-closed?` andreceive, timers and protected output. Each flush attempts at most 32 retainedplus 32 fresh protected datagrams, once per record, and retains at most 32failed sends. One blocked address cannot stop validation controls for another.Each retained record keeps its original one-second deadline; expiry andoverflow drop ciphertext as UDP loss. Reliable frames remain owned by theconnection's retransmission machinery, while DATAGRAM data is unreliable.Malformed output and partial datagram sends close the socket and connection.`quic-endpoint-connection`, `quic-endpoint-closed?` and`quic-endpoint-failure` expose progress. `quic-endpoint-close!` is idempotent,clears connection/stream secrets and storage, attempts final close output,and closes the dedicated socket. Cleanup also runs if initiating connectionscripts/native-tls-tests.mjsmodified
'test-server-retry.sgl', 'test-connection-space.sgl', 'test-connection.sgl', 'test-connection-network.sgl', 'test-addressed-output.sgl', 'test-network-confirmation.sgl', 'test-network-receive.sgl', 'test-random-ids.sgl', 'test-datagram.sgl', 'test-aead-limits.sgl', 'test-application-close.sgl', 'test-sni-fallback.sgl', 'test-tls-identity.sgl', 'test-path-response.sgl', 'test-path-validation.sgl', 'test-path-manager.sgl', 'test-cid-zero.sgl', 'test-cid-sequence.sgl', 'test-local-ids.sgl', 'test-peer-ids.sgl', 'test-handler-readiness.sgl', 'test-early-packets.sgl', 'test-receive-bounds.sgl', 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl',];const cleanupFaults = process.argv.includes('--cleanup-faults');const secretCleanup = process.argv.includes('--secret-cleanup');const endpointRetry = process.argv.includes('--endpoint-retry');const retryCleanup = process.argv.includes('--retry-cleanup');const datagram = process.argv.includes('--datagram');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) {const pathValidation = process.argv.includes('--path-validation');const localIds = process.argv.includes('--local-ids');const peerIds = process.argv.includes('--peer-ids');const pathManager = process.argv.includes('--path-manager');const connectionNetwork = process.argv.includes('--connection-network');const networkIntegration = process.argv.includes('--network-integration');const endpointNetwork = process.argv.includes('--endpoint-network');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) { throw new Error('Select one focused test suite');}if (cleanupFaults) {if (datagram) { files.splice(0, files.length, 'test-datagram-codec.sgl', 'test-datagram.sgl');}if (pathValidation) { files.splice(0, files.length, 'test-path-validation.sgl');}if (localIds) { files.splice(0, files.length, 'test-local-ids.sgl');}if (peerIds) { files.splice(0, files.length, 'test-peer-ids.sgl');}if (pathManager) { files.splice(0, files.length, 'test-path-manager.sgl');}if (connectionNetwork) { files.splice(0, files.length, 'test-connection-network.sgl');}if (networkIntegration) { files.splice(0, files.length, 'test-addressed-output.sgl', 'test-network-confirmation.sgl', 'test-network-receive.sgl', 'test-random-ids.sgl');}if (endpointNetwork) { files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl');}const bodies = files.map(file => { const text = readFileSync(join(root, 'test', file), 'utf8'); const boundary = text.indexOf('\n\n'); (sigil quic packet)${files.includes('test-retry-token.sgl') || retryCleanup ? '\n (sigil quic retry token)' : ''} (sigil quic error) (sigil quic connection space) (sigil quic connection) (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)' : ''} (sigil quic stream) (sigil quic endpoint) (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)' : ''} (sigil quic stream state) (sigil quic transport parameters) (sigil quic tls)src/sigil/quic/connection.sglmodified
;;; Q1 QUIC connection machinery. No sockets: the endpoint supplies one;;; fixed peer address and sends returned datagrams immediately, in order.;;; QUIC connection machinery without sockets. Addressed output retains its;;; sealed destination; the endpoint sends that datagram without retargeting.(define-library (sigil quic connection) (import (sigil crypto) (sigil io) (sigil quic reassembly) (sigil quic tls) (sigil quic connection space) (sigil quic connection network) (sigil quic transport parameters)) (export make-quic-client make-quic-server quic-connection? quic-role quic-local-id quic-peer-id quic-complete? quic-confirmed? quic-closed? quic-idle-expired? quic-start! quic-take-datagrams! quic-receive! quic-tick! quic-failure quic-bind-peer-address! quic-peer-address quic-take-addressed-datagrams! quic-take-path-events! quic-output? quic-output-bytes quic-output-address quic-output-id quic-output-valid? quic-set-frame-handler! quic-send-frame! quic-send-capacity quic-send-datagram! quic-read-datagram! quic-datagram-queue-limit quic-datagram-byte-limit (define quic-datagram-queue-limit 32) (define quic-datagram-byte-limit 65536) ;; Metadata is owned alongside protected bytes. Public reads return copies; ;; close-packet replay copies the same destination instead of a later peer. (define-struct protected-output (bytes) (address) (destination-id) (close?) (path-control? default: #f)) (define (quic-output? output) (protected-output? output)) (define (quic-output-bytes output) (bytevector-copy (protected-output-bytes output))) (define (quic-output-address output) (bytevector-copy (protected-output-address output))) (define (quic-output-id output) (bytevector-copy (protected-output-destination-id output))) (define (output-size output) (bytevector-length (if (protected-output? output) (protected-output-bytes output) output))) (define (copy-output output) (if (protected-output? output) (protected-output bytes: (quic-output-bytes output) address: (quic-output-address output) destination-id: (quic-output-id output) close?: (protected-output-close? output) path-control?: (protected-output-path-control? output)) (bytevector-copy output))) (define (wipe-output! output) (wipe! (if (protected-output? output) (protected-output-bytes output) output))) (define-struct connection-level (level) (space) (retry-hello default: #f mutable: #t) (peer-initial-id default: #f mutable: #t) (peer-id mutable: #t) (peer-address default: #f mutable: #t) (network default: #f mutable: #t) (send-path default: #f mutable: #t) (path-responses default: '() mutable: #t) (closing-routes default: '() mutable: #t) (levels) (state default: 'new mutable: #t) (confirmed? default: #f mutable: #t) (define (quic-negotiated-alpn connection) (tls-negotiated-alpn (quic-connection-tls connection))) (define (quic-local-id connection) (bytevector-copy (quic-connection-local-id connection))) (define (quic-peer-id connection) (bytevector-copy (quic-connection-peer-id connection))) (define (quic-peer-address connection) (let ((network (quic-connection-network connection))) (if network (network-address network) (and (quic-connection-peer-address connection) (bytevector-copy (quic-connection-peer-address connection)))))) (define (quic-take-path-events! connection) (if (quic-connection-network connection) (network-take-events! (quic-connection-network connection)) '())) ;; Bind the original address once, before the handshake starts. Subsequent ;; changes belong to authenticated path validation, never to this setter. (define (quic-bind-peer-address! connection address) (unless (and (bytevector? address) (<= 1 (bytevector-length address) 64) (eq? (quic-connection-state connection) 'new) (not (quic-connection-peer-address connection))) (error "QUIC connection: original peer address must be bound once before starting")) (set-quic-connection-peer-address! connection (bytevector-copy address))) ;; The endpoint drops queued output whose peer CID has since retired. ;; Closing output retains only the frozen final destination, while ordinary ;; application output becomes invalid as soon as closure starts. (define (quic-output-valid? connection output) (and (protected-output? output) (or (not (quic-closed? connection)) (and (quic-closing? connection) (protected-output-close? output))) (let ((network (quic-connection-network connection))) (if network (let ((destination (network-destination network (protected-output-address output)))) (and destination (or (protected-output-path-control? output) (bytes=? (network-address network) (protected-output-address output))) (bytes=? (vector-ref destination 1) (protected-output-destination-id output)))) (and (bytes=? (quic-peer-address connection) (protected-output-address output)) (bytes=? (quic-peer-id connection) (protected-output-destination-id output))))))) ;; TLS may deliver authenticated CID frames before HANDSHAKE_DONE reaches ;; a client. Install passive inventory at TLS completion; migration inputs ;; and path probes remain gated on confirmation by the receive/send hooks. (define (ensure-network! connection) (when (and (quic-connection-peer-address connection) (not (quic-connection-network connection)) (not (quic-closed? connection)) (tls-complete? (quic-connection-tls connection))) (set-quic-connection-network! connection (make-connection-network (quic-local-id connection) (quic-peer-id connection) (quic-role connection) (quic-connection-peer-address connection) (quic-connection-now connection) (quic-local-parameters connection) (quic-peer-parameters connection) pto: (min 60000 (connection-pto connection)) highest: (packet-space-largest (connection-level-space (level-state connection 'application))) confirmed?: (quic-confirmed? connection))) (set-quic-connection-send-path! connection (bytevector-copy (quic-connection-peer-address connection)))) (when (and (quic-connection-network connection) (quic-confirmed? connection)) (network-confirm! (quic-connection-network connection) (quic-connection-now connection) (packet-space-largest (connection-level-space (level-state connection 'application)))))) (define (synchronize-peer-id! connection) (let ((network (quic-connection-network connection))) (when network (unless (bytes=? (quic-connection-send-path connection) (network-address network)) (reset-path-congestion! connection) (set-quic-connection-send-path! connection (network-address network))) (let ((destination (network-destination network (network-address network)))) (when destination (set-quic-connection-peer-id! connection (vector-ref destination 1))))))) ;; Retire all old-path congestion inventory, retaining reliable frame ;; ownership for new-PN transmission. Old ACKs remain syntactically valid ;; but cannot grow this path's fresh window or reset its loss timer. (define (reset-path-congestion! connection) (let* ((state (level-state connection 'application)) (space (connection-level-space state)) (pending (packet-space-pending space)) (sent (connection-level-sent state))) (for-each (lambda (packet) (packet-space-retire! space (sent-packet-info-number packet))) pending) (set-connection-level-sent! state '()) (set-connection-level-retries! state 0) (set-quic-connection-window! connection 12000) (set-quic-connection-threshold! connection #f) (for-each (lambda (packet) (let ((entry (assv (sent-packet-info-number packet) sent))) (when entry (queue-frame! connection 'application (cdr entry))))) (reverse pending)))) (define (local-route? connection cid) (let ((network (quic-connection-network connection))) (cond (network (and (network-route network cid) #t)) ((pair? (quic-connection-closing-routes connection)) (and (any (lambda (old) (bytes=? old cid)) (quic-connection-closing-routes connection)) #t)) (else (bytes=? cid (quic-connection-local-id connection)))))) ;; Local issued CIDs have unambiguous prefixes. Select a short-header CID ;; length by actual owned route, rather than always assuming initial length. ;; Long headers carry their own lengths and need no speculative iteration. (define (decode-connection-header connection datagram offset) (let ((network (quic-connection-network connection))) (if (or (>= (bytevector-u8-ref datagram offset) 128) (and (not network) (null? (quic-connection-closing-routes connection)))) (decode-packet-header datagram offset (bytevector-length (quic-connection-local-id connection))) (let loop ((lengths (if network (network-route-lengths network) (map bytevector-length (quic-connection-closing-routes connection))))) (and (pair? lengths) (let ((header (guard (exn (else #f)) (decode-packet-header datagram offset (car lengths))))) (if (and header (local-route? connection (quic-header-destination-id header))) header (loop (cdr lengths))))))))) (define (quic-confirmed? connection) (quic-connection-confirmed? connection)) (define (quic-closed? connection) (and (memq (quic-connection-state connection) '(closing closed draining)) #t)) (define (check-frame-packet-budget! connection level wire) (unless (and (<= (bytevector-length wire) 1200) (let ((header (encode-packet-header level (quic-connection-peer-id connection) (encode-packet-header level (if (and (eq? level 'application) (quic-connection-peer-address connection)) (make-bytevector 20 0) (quic-connection-peer-id connection)) (if (eq? level 'application) #u8() (quic-connection-local-id connection)) (initial-token connection level) 0 4 (+ (bytevector-length wire) 16) length-width: (and (not (eq? level 'application)) 2)))) (- (connection-level-queue-bytes state) (bytevector-length (encode-frame frame))))) frame)) (define (control-pending? state type sequence) (define (same? frame) (and (= type (quic-frame-type frame)) (= sequence (frame-ref frame 'sequence)))) (or (any same? (connection-level-queue-front state)) (any same? (connection-level-queue-back state)) (any (lambda (entry) (same? (cdr entry))) (connection-level-sent state)))) ;; NEW/RETIRE controls fit within64 bytes at every bounded sequence. Check ;; queue capacity before reserving an ID, so a full queue cannot strand a ;; non-routable reservation. The inventory counts queued reservations. (define (control-room? state) (and (< (connection-level-queue-count state) 4096) (<= (+ (connection-level-queue-bytes state) 64) 2097152))) (define (sync-network-controls! connection) (ensure-network! connection) (let ((network (quic-connection-network connection)) (state (level-state connection 'application))) (when (and network (quic-confirmed? connection) (not (quic-closed? connection))) (for-each (lambda (frame) (when (and (control-room? state) (not (control-pending? state 25 (frame-ref frame 'sequence)))) (queue-frame! connection 'application frame))) (network-retirements network)) (let loop ((count 0)) (when (and (< count 8) (control-room? state)) (let ((frame (network-issue-spare! network))) (when frame (queue-frame! connection 'application frame) ;; queue-frame! owns its encoded token copy and the pool ;; retains a separate copy for advertisement preflight. (wipe! (frame-ref frame 'token)) (loop (+ count 1))))))))) ;; A queued/lost advertisement must never resurrect a retired local ID. ;; An already acknowledged retirement need not be emitted again either. (define (stale-network-control? connection frame) (let ((network (quic-connection-network connection))) (and network (case (quic-frame-type frame) ((24) (not (network-check-id! network frame))) ((25) (not (any (lambda (pending) (= (frame-ref pending 'sequence) (frame-ref frame 'sequence))) (network-retirements network)))) (else #f))))) ;; Response metadata stays attached to the authenticated challenge source. ;; Limit pending responses independently of the ordinary reliable queue. (define (queue-path-response! connection source data) (let ((pending (quic-connection-path-responses connection))) (when (and (< (length pending) 16) (not (any (lambda (entry) (and (bytes=? source (vector-ref entry 0)) (bytes=? data (vector-ref entry 1)))) pending))) (set-quic-connection-path-responses! connection (append pending (list (vector (bytevector-copy source) (bytevector-copy data) (+ (quic-connection-now connection) (* 3 (connection-pto connection)))))))))) ;; Independent path timers replace transport retransmission of controls. ;; Only a fresh challenge can retry validation. A response is sent once, ;; padded to1200 when its path can afford it, otherwise at minimal size. (define (take-path-output! connection now maximum) (let ((out '()) (kept '()) (count 0) (network (and (quic-confirmed? connection) (quic-connection-network connection)))) (when network (network-tick! network now)) (for-each (lambda (entry) (cond ((>= now (vector-ref entry 2)) (wipe! (vector-ref entry 1))) ((>= count maximum) (set! kept (cons entry kept))) (else (let* ((frame (make-quic-frame 27 (list (cons 'data (vector-ref entry 1))))) (packet (or (emit-packet! connection 'application frame now address: (vector-ref entry 0) pad?: #t path-control?: #t) (emit-packet! connection 'application frame now address: (vector-ref entry 0) path-control?: #t)))) (if packet (begin (set! out (cons packet out)) (set! count (+ count 1)) (wipe! (vector-ref entry 1))) (set! kept (cons entry kept))))))) (quic-connection-path-responses connection)) (set-quic-connection-path-responses! connection (reverse kept)) (when network (for-each (lambda (address) (when (and address (< count maximum)) (let ((action (network-challenge! network address now))) (when action (let* ((frame (make-quic-frame 26 (list (cons 'data (vector-ref action 3))))) (packet (emit-packet! connection 'application frame now address: (vector-ref action 0) pad?: #t path-control?: #t reserved?: #t))) (wipe! (vector-ref action 3)) (when packet (set! out (cons packet out)) (set! count (+ count 1)))))))) (list (network-candidate network) (network-previous-address network)))) (reverse out))) (define (wipe! bytes) (when (bytevector? bytes) (do ((i 0 (+ i 1))) ((= i (bytevector-length bytes))) (apply + (map (lambda (level) (packet-space-pending-bytes (connection-level-space (level-state connection level)))) '(initial handshake application)))) (define (packet-plaintext connection level number frame) (define (packet-plaintext connection level number frame destination-id pad?) (let* ((body (encode-frames (list frame) level (quic-role connection))) (header (encode-packet-header level (quic-connection-peer-id connection) (header (encode-packet-header level destination-id (if (eq? level 'application) #u8() (quic-connection-local-id connection)) (initial-token connection level) number 4 (+ (bytevector-length body) 16) (when (> size 1200) (raise-quic-error 1 #f "QUIC connection frame exceeds packet budget")) (bytes-append body (make-bytevector (if (or (and (eq? level 'initial) (if (or pad? (and (eq? level 'initial) (or (eq? (quic-role connection) 'client) (frame-ack-eliciting? frame))) (and (memv (quic-frame-type frame) '(26 27)) (not (quic-connection-peer-address connection)) (or (quic-connection-validated? connection) (<= (+ (quic-connection-sent-bytes connection) 1200) (* 3 (quic-connection-received-bytes connection)))))) 0) 0)))) (define (emit-packet! connection level frame now) (let* ((state (level-state connection level)) (space (connection-level-space state)) (number (packet-space-next space)) (payload (packet-plaintext connection level number frame)) (header (encode-packet-header level (quic-connection-peer-id connection) (if (eq? level 'application) #u8() (quic-connection-local-id connection)) (initial-token connection level) number 4 (+ (bytevector-length payload) 16) length-width: (and (not (eq? level 'application)) 2))) (size (+ (bytevector-length header) (bytevector-length payload) 16)) (eliciting? (frame-ack-eliciting? frame))) (and (packet-space-can-send? space size eliciting?) (or (not eliciting?) (<= (+ (in-flight connection) size) (quic-connection-window connection))) (or (quic-connection-validated? connection) (<= (+ (quic-connection-sent-bytes connection) size) (* 3 (quic-connection-received-bytes connection)))) (begin (check-send-key-budget! connection state frame) #t) (guard (exn (else ;; Encryption may already have used this PN even if a ;; later admission/allocation failed. Erase this write ;; key before outer closure can attempt a close packet. (let ((keys (connection-level-write-keys state))) (set-connection-level-write-keys! state #f) (when keys (discard-packet-keys! keys))) (raise exn))) (let ((packet (seal-quic-packet (connection-level-write-keys state) level (quic-connection-peer-id connection) (if (eq? level 'application) #u8() (quic-connection-local-id connection)) number payload token: (initial-token connection level) length-width: (and (not (eq? level 'application)) 2)))) (set-connection-level-sealed! state (+ 1 (connection-level-sealed state))) (packet-space-record-sent! space number size now eliciting?) (when eliciting? ;; Keep congestion/ACK metadata, but never DATAGRAM payload ;; for retransmission. Its PTO elicits an ACK using PING. (let ((retained (if (datagram-frame? frame) (make-quic-frame 1 '()) frame))) (set-connection-level-sent! state (cons (cons number retained) (connection-level-sent state))))) (sent-activity! connection now eliciting?) (when (and eliciting? (memq level '(initial handshake))) (set-quic-connection-probe-start! connection now)) (set-quic-connection-sent-bytes! connection (+ (quic-connection-sent-bytes connection) size)) ;; Queue admission alone does not advertise an ID. Record ;; caller-issued NEW IDs only after this packet is committed. (when (= (quic-frame-type frame) 24) (set-quic-connection-issued-sequence! connection (max (quic-connection-issued-sequence connection) (frame-ref frame 'sequence)))) (when (and (eq? (quic-role connection) 'client) (eq? level 'handshake)) (discard-level! connection 'initial) (when (and (= (quic-frame-type frame) 6) (tls-complete? (quic-connection-tls connection)) (= (+ (frame-ref frame 'offset) (bytevector-length (frame-ref frame 'data))) (connection-level-send-offset state))) (set-quic-connection-finished-sent?! connection #t))) packet))))) (define (emit-packet! connection level frame now (keys: (address #f) (pad? #f) (path-control? #f) (reserved? #f))) (let* ((network (and (eq? level 'application) (quic-confirmed? connection) (quic-connection-network connection))) (target-address (or address (quic-peer-address connection))) (destination (and network (network-destination network target-address))) (destination-id (if network (and destination (vector-ref destination 1)) (quic-peer-id connection)))) (and destination-id (let* ((state (level-state connection level)) (space (connection-level-space state)) (number (packet-space-next space)) (payload (packet-plaintext connection level number frame destination-id pad?)) (header (encode-packet-header level destination-id (if (eq? level 'application) #u8() (quic-connection-local-id connection)) (initial-token connection level) number 4 (+ (bytevector-length payload) 16) length-width: (and (not (eq? level 'application)) 2))) (size (+ (bytevector-length header) (bytevector-length payload) 16)) (eliciting? (frame-ack-eliciting? frame)) (tracked? (and eliciting? (not path-control?)))) (and (packet-space-can-send? space size tracked?) (or (not tracked?) (<= (+ (in-flight connection) size) (quic-connection-window connection))) (or (quic-connection-validated? connection) (<= (+ (quic-connection-sent-bytes connection) size) (* 3 (quic-connection-received-bytes connection)))) (not (stale-network-control? connection frame)) (begin (check-send-key-budget! connection state frame) #t) (or (not network) (if reserved? (and path-control? (= size 1200)) (network-reserve-send! network target-address size now))) (guard (exn (else ;; Encryption may already have used this PN even if a ;; later admission/allocation failed. Erase this write ;; key before outer closure can attempt a close packet. (let ((keys (connection-level-write-keys state))) (set-connection-level-write-keys! state #f) (when keys (discard-packet-keys! keys))) (raise exn))) (let ((packet (seal-quic-packet (connection-level-write-keys state) level destination-id (if (eq? level 'application) #u8() (quic-connection-local-id connection)) number payload token: (initial-token connection level) length-width: (and (not (eq? level 'application)) 2)))) (set-connection-level-sealed! state (+ 1 (connection-level-sealed state))) (packet-space-record-sent! space number size now tracked?) (when tracked? ;; Keep congestion/ACK metadata, but never DATAGRAM payload ;; for retransmission. Its PTO elicits an ACK using PING. (let ((retained (if (datagram-frame? frame) (make-quic-frame 1 '()) frame))) (set-connection-level-sent! state (cons (cons number retained) (connection-level-sent state))))) (unless path-control? (sent-activity! connection now eliciting?)) (when (and eliciting? (memq level '(initial handshake))) (set-quic-connection-probe-start! connection now)) (set-quic-connection-sent-bytes! connection (+ (quic-connection-sent-bytes connection) size)) ;; Queue admission alone does not advertise an ID. Record ;; caller-issued NEW IDs only after this packet is committed. (when (= (quic-frame-type frame) 24) (when (quic-connection-network connection) (network-commit-id! (quic-connection-network connection) frame)) (set-quic-connection-issued-sequence! connection (max (quic-connection-issued-sequence connection) (frame-ref frame 'sequence)))) (when (and (eq? (quic-role connection) 'client) (eq? level 'handshake)) (discard-level! connection 'initial) (when (and (= (quic-frame-type frame) 6) (tls-complete? (quic-connection-tls connection)) (= (+ (frame-ref frame 'offset) (bytevector-length (frame-ref frame 'data))) (connection-level-send-offset state))) (set-quic-connection-finished-sent?! connection #t))) (if (quic-connection-peer-address connection) (protected-output bytes: packet address: (bytevector-copy target-address) destination-id: (bytevector-copy destination-id) path-control?: path-control? close?: (and (memv (quic-frame-type frame) '(28 29)) #t)) packet)))))))) (define (quic-start! connection now) (update-time! connection now) (cons 'reason #u8())))))) (define (end-closing! connection) (for-each wipe! (quic-connection-close-packets connection)) (for-each wipe-output! (quic-connection-close-packets connection)) (set-quic-connection-close-packets! connection '()) (set-quic-connection-close-next! connection #f) (set-quic-connection-close-until! connection #f) (set-quic-connection-closing-routes! connection '()) (set-quic-connection-ready! connection '()) (set-quic-connection-state! connection 'closed)) (cons packet (quic-connection-ready connection)))))))) (if (quic-confirmed? connection) '(application) '(initial handshake application)))) (for-each (lambda (level) (discard-level! connection level)) '(initial handshake application)) (for-each (lambda (entry) (wipe! (vector-ref entry 1))) (quic-connection-path-responses connection)) (set-quic-connection-path-responses! connection '()) (when (quic-connection-network connection) (let ((network (quic-connection-network connection))) (set-quic-connection-peer-address! connection (network-address network)) (set-quic-connection-closing-routes! connection (let loop ((sequence 0) (routes '())) (if (= sequence 256) (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
;;; Bounded locally issued connection IDs. The connection owns wire emission,;;; authenticated retirement, packet routing and replacement scheduling.(define-library (sigil quic connection ids) (import (sigil crypto) (sigil struct) (sigil quic bytes) (sigil quic error) (sigil quic frame) (sigil quic wire)) (export make-local-id-pool local-id-pool? local-id-pool-count local-id-pool-reserve! local-id-pool-reserve-random! local-id-pool-check! local-id-pool-commit! local-id-pool-retire! local-id-pool-route local-id-pool-id local-id-pool-lengths local-id-pool-close!) (begin (define-struct local-id-pool (entries) (limit) (next mutable: #t) (count mutable: #t) (closed? default: #f mutable: #t)) ;; Entry: sequence, copied CID, copied reset token, emitted?, retired?. ;; Retired entries remain as bounded tombstones; sequence slots never reuse. (define (entry sequence cid token) (vector sequence (bytevector-copy cid) (and token (bytevector-copy token)) #f #f)) (define (uint? number maximum) (and (integer? number) (exact? number) (<= 0 number maximum))) (define (id? cid minimum) (and (bytevector? cid) (<= minimum (bytevector-length cid) 20))) (define (prefix? first second) (and (<= (bytevector-length first) (bytevector-length second)) (let loop ((index 0)) (or (= index (bytevector-length first)) (and (= (bytevector-u8-ref first index) (bytevector-u8-ref second index)) (loop (+ index 1))))))) ;; A short header carries no CID length. Reject overlapping local prefixes ;; so the connection can select a unique length before packet decryption. (define (ambiguous-id? pool cid) (let loop ((index 0)) (and (< index (local-id-pool-next pool)) (let ((old (vector-ref (local-id-pool-entries pool) index))) (or (prefix? cid (vector-ref old 1)) (prefix? (vector-ref old 1) cid) (loop (+ index 1))))))) (define (make-local-id-pool initial (keys: (limit 2) (maximum 256) (preferred #f))) (unless (and (id? initial 0) (uint? limit 8) (>= limit 2) (uint? maximum 256) (>= maximum limit) (or (not preferred) (and (pair? preferred) (id? (car preferred) 1) (bytevector? (cdr preferred)) (= 16 (bytevector-length (cdr preferred))) (> (bytevector-length initial) 0)))) (error "QUIC local IDs: invalid initial ID, limits or preferred address ID")) (when (and preferred (or (prefix? initial (car preferred)) (prefix? (car preferred) initial))) (error "QUIC local IDs: ambiguous preferred address ID")) (let* ((entries (make-vector maximum #f)) (initial-entry (entry 0 initial #f)) (pool (local-id-pool entries: entries limit: limit next: (if preferred 2 1) count: (if preferred 2 1)))) ;; Construct this inventory only after the handshake's source-ID and ;; optional preferred-address metadata have actually been transmitted. (vector-set! initial-entry 3 #t) (vector-set! entries 0 initial-entry) (when preferred (let ((preferred-entry (entry 1 (car preferred) (cdr preferred)))) (vector-set! preferred-entry 3 #t) (vector-set! entries 1 preferred-entry))) pool)) (define (require-open! pool) (when (local-id-pool-closed? pool) (error "QUIC local IDs: pool is closed"))) (define (existing pool sequence) (and (uint? sequence (- (local-id-pool-next pool) 1)) (vector-ref (local-id-pool-entries pool) sequence))) (define (active? value) (and value (vector-ref value 3) (not (vector-ref value 4)))) (define (capacity? pool) (and (> (bytevector-length (vector-ref (vector-ref (local-id-pool-entries pool) 0) 1)) 0) (< (local-id-pool-count pool) (local-id-pool-limit pool)) (< (local-id-pool-next pool) (vector-length (local-id-pool-entries pool))))) ;; Generate bounded, independent random CID/reset-token material. Retry ;; prefix collisions before allocating sequence or inventory capacity. ;; Zero-length, full and lifetime-exhausted inventories need no entropy. (define (local-id-pool-reserve-random! pool) (require-open! pool) (and (capacity? pool) (let loop ((attempt 0)) (when (= attempt 8) (raise-quic-error 1 #f "QUIC local CID entropy repeatedly collided")) (let ((cid (random-bytes 8))) (if (ambiguous-id? pool cid) (loop (+ attempt 1)) (let ((token (random-bytes 16))) (dynamic-wind (lambda () #t) (lambda () (local-id-pool-reserve! pool cid token)) (lambda () (do ((index 0 (+ index 1))) ((= index 16)) (bytevector-u8-set! token index 0)))))))))) ;; Reserve before queueing NEW_CONNECTION_ID. A reservation counts toward ;; the peer limit but is not routable/retirable until actual packet commit. ;; Lifetime exhaustion stops issuance without forgetting any live ID. (define (local-id-pool-reserve! pool cid token) (require-open! pool) (unless (and (id? cid 1) (bytevector? token) (= (bytevector-length token) 16)) (error "QUIC local IDs: invalid new ID or reset token")) (let ((initial (vector-ref (local-id-pool-entries pool) 0)) (sequence (local-id-pool-next pool))) (and (> (bytevector-length (vector-ref initial 1)) 0) (< (local-id-pool-count pool) (local-id-pool-limit pool)) (< sequence (vector-length (local-id-pool-entries pool))) (begin (when (ambiguous-id? pool cid) (error "QUIC local IDs: repeated or ambiguous new ID")) (let* ((owned (entry sequence cid token)) (frame (make-quic-frame 24 (list (cons 'sequence sequence) (cons 'retire-prior-to 0) (cons 'connection-id (bytevector-copy (vector-ref owned 1))) (cons 'token (bytevector-copy (vector-ref owned 2))))))) (vector-set! (local-id-pool-entries pool) sequence owned) (set-local-id-pool-next! pool (+ sequence 1)) (set-local-id-pool-count! pool (+ 1 (local-id-pool-count pool))) frame))))) ;; Validate the exact queued advertisement before consuming a packet number. ;; A stale advertisement for a retired ID is a drop, never a resurrection. (define (local-id-pool-check! pool frame) (require-open! pool) (unless (and (quic-frame? frame) (= 24 (quic-frame-type frame))) (error "QUIC local IDs: expected a reserved NEW_CONNECTION_ID")) (let ((value (existing pool (frame-ref frame 'sequence)))) (unless value (error "QUIC local IDs: unknown advertisement sequence")) (and (not (vector-ref value 4)) (begin (unless (and (= 0 (frame-ref frame 'retire-prior-to)) (bytes=? (frame-ref frame 'connection-id) (vector-ref value 1)) (bytes-timing-safe=? (frame-ref frame 'token) (vector-ref value 2))) (error "QUIC local IDs: advertisement differs from its reservation")) #t)))) ;; Call CHECK! before sealing, then commit that same owned frame only after ;; its protected packet has been recorded. Retransmission is idempotent. (define (local-id-pool-commit! pool frame) (and (local-id-pool-check! pool frame) (let ((value (existing pool (frame-ref frame 'sequence)))) (vector-set! value 3 #t) #t))) (define (local-id-pool-route pool cid) (unless (id? cid 0) (error "QUIC local IDs: invalid routing ID")) (and (not (local-id-pool-closed? pool)) (let loop ((index 0)) (and (< index (local-id-pool-next pool)) (let ((value (vector-ref (local-id-pool-entries pool) index))) (if (and (active? value) (bytes=? cid (vector-ref value 1))) index (loop (+ index 1)))))))) (define (local-id-pool-id pool sequence) (and (not (local-id-pool-closed? pool)) (let ((value (existing pool sequence))) (and (active? value) (bytevector-copy (vector-ref value 1)))))) (define (local-id-pool-lengths pool) (if (local-id-pool-closed? pool) '() (let loop ((index 0) (lengths '())) (if (= index (local-id-pool-next pool)) lengths (let* ((value (vector-ref (local-id-pool-entries pool) index)) (size (bytevector-length (vector-ref value 1)))) (loop (+ index 1) (if (and (active? value) (not (memv size lengths))) (cons size lengths) lengths))))))) (define (wipe-token! value) (let ((token (vector-ref value 2))) (when token (do ((index 0 (+ index 1))) ((= index (bytevector-length token))) (bytevector-u8-set! token index 0)) (vector-set! value 2 #f)))) ;; Only the connection may pass authenticated retirement metadata here. ;; PACKET-ID is the DCID of that exact packet, not its latest active CID. (define (local-id-pool-retire! pool sequence packet-id) (require-open! pool) (unless (and (uint? sequence quic-varint-maximum) (id? packet-id 0)) (error "QUIC local IDs: invalid retirement metadata")) (let ((value (existing pool sequence))) (when (or (= (bytevector-length (vector-ref (vector-ref (local-id-pool-entries pool) 0) 1)) 0) (not value) (not (vector-ref value 3))) (raise-quic-error 10 25 "QUIC retirement refers to a zero-length or never-issued ID")) (when (bytes=? packet-id (vector-ref value 1)) (raise-quic-error 10 25 "QUIC retirement refers to this packet's destination ID")) (and (not (vector-ref value 4)) (begin (vector-set! value 4 #t) (wipe-token! value) (set-local-id-pool-count! pool (- (local-id-pool-count pool) 1)) #t)))) (define (local-id-pool-close! pool) (unless (local-id-pool-closed? pool) (do ((index 0 (+ index 1))) ((= index (local-id-pool-next pool))) (wipe-token! (vector-ref (local-id-pool-entries pool) index)) (vector-set! (local-id-pool-entries pool) index #f)) (set-local-id-pool-count! pool 0) (set-local-id-pool-closed?! pool #t)))))src/sigil/quic/connection/network.sgladded
;;; Confirmed connection network ownership, without sockets or packet crypto.;;; The connection authenticates/replay-checks complete packets before input,;;; seals addressed output, and reliably queues/acknowledges retirement frames.(define-library (sigil quic connection network) (import (sigil struct) (sigil quic bytes) (sigil quic frame) (sigil quic wire) (sigil quic transport parameters) (sigil quic connection ids) (sigil quic connection peer ids) (sigil quic path manager)) (export make-connection-network connection-network? network-confirm! network-address network-candidate network-previous-address network-destination network-local-count network-local-limit network-issue-id! network-issue-spare! network-check-id! network-commit-id! network-retire-local-id! network-route network-route-lengths network-local-id network-peer-id! network-retirements network-retirement-ack! network-observe! network-tick! network-reserve-send! network-challenge! network-response! network-take-events! network-close!) (begin (define-struct connection-network (local default: #f mutable: #t) (peer default: #f mutable: #t) (paths default: #f mutable: #t) (role) (pto) (original-address mutable: #t) (limit) (assignments default: '() mutable: #t) (closed? default: #f mutable: #t)) (define (require-open! network) (when (connection-network-closed? network) (error "QUIC network: state is closed"))) (define (require-confirmed! network) (require-open! network) (unless (connection-network-paths network) (error "QUIC network: path operations require handshake confirmation"))) (define (preferred parameters) (let ((entry (assoc 13 parameters))) (and entry (let* ((value (cdr entry)) (length (bytevector-u8-ref value 24))) (cons (bytevector-copy value 25 (+ 25 length)) (bytevector-copy value (+ 25 length) (+ 41 length))))))) ;; The supplied transport parameters and source IDs must already be bound ;; to the confirmed TLS handshake. Encoding checks shape, not authenticity. (define (make-connection-network local-id peer-id role address now local-parameters peer-parameters (keys: (pto 1000) (highest -1) (confirmed? #t))) (unless (and (memq role '(client server)) (boolean? confirmed?) (bytevector? address) (<= 1 (bytevector-length address) 64) (integer? now) (exact? now) (>= now 0) (integer? pto) (exact? pto) (<= 1000 pto 60000) (integer? highest) (exact? highest) (<= -1 highest quic-varint-maximum)) (error "QUIC network: invalid role, confirmation, address or timing metadata")) (encode-transport-parameters local-parameters role) (encode-transport-parameters peer-parameters (if (eq? role 'client) 'server 'client)) (unless (and (bytes=? local-id (cdr (assoc 15 local-parameters))) (bytes=? peer-id (cdr (assoc 15 peer-parameters)))) (error "QUIC network: source IDs differ from authenticated transport parameters")) (let ((limit (transport-parameter-integer local-parameters 14 2)) (reset (assoc 2 peer-parameters)) (network (connection-network role: role pto: pto original-address: (bytevector-copy address) limit: (min 8 (transport-parameter-integer peer-parameters 14 2))))) (when (> limit 8) (error "QUIC network: local active CID advertisement exceeds supported inventory")) (guard (exn (else (network-close! network) (raise exn))) (set-connection-network-local! network (make-local-id-pool local-id limit: (connection-network-limit network) preferred: (preferred local-parameters))) (set-connection-network-peer! network (make-peer-id-pool peer-id limit: limit token: (and reset (cdr reset)) preferred: (preferred peer-parameters))) (when confirmed? (set-connection-network-paths! network (make-path-manager address role now pto: pto highest: highest))) ;; Sequence zero is already assigned to the handshake's path. (set-connection-network-assignments! network (list (cons (bytevector-copy address) 0))) network))) (define (network-address network) (and (not (connection-network-closed? network)) (if (connection-network-paths network) (path-manager-address (connection-network-paths network)) (bytevector-copy (connection-network-original-address network))))) ;; CID metadata can arrive before HANDSHAKE_DONE. Passive inventory cannot ;; observe new paths, reserve path output or produce a validation challenge. ;; At confirmation seed from the connection's then-current received PN. (define (network-confirm! network now highest) (require-open! network) (unless (connection-network-paths network) (set-connection-network-paths! network (make-path-manager (connection-network-original-address network) (connection-network-role network) now pto: (connection-network-pto network) highest: highest))) #t) (define (network-candidate network) (and (connection-network-paths network) (path-manager-candidate (connection-network-paths network)))) (define (network-previous-address network) (and (connection-network-paths network) (path-manager-previous-address (connection-network-paths network)))) (define (contains-address? addresses address) (any (lambda (key) (bytes=? key address)) addresses)) (define (managed-addresses network) (let loop ((rest (list (network-address network) (network-candidate network) (network-previous-address network))) (out '())) (cond ((null? rest) (reverse out)) ((or (not (car rest)) (contains-address? out (car rest))) (loop (cdr rest) out)) (else (loop (cdr rest) (cons (car rest) out)))))) (define (assignment network address) (find (lambda (entry) (bytes=? address (car entry))) (connection-network-assignments network))) ;; Retain CID assignments for the active, candidate and old-probe paths. ;; Retire an abandoned assignment rather than reuse it on another address. ;; Active-path allocation comes first when a peer RetirePriorTo removed ;; its previous CID. Exhaustion pauses output without killing a live path. (define (synchronize! network) (let* ((peer (connection-network-peer network)) (addresses (managed-addresses network)) (kept '())) (for-each (lambda (entry) (when (peer-id-pool-id peer (cdr entry)) (if (contains-address? addresses (car entry)) (set! kept (cons entry kept)) (peer-id-pool-retire! peer (cdr entry))))) (connection-network-assignments network)) (set-connection-network-assignments! network (reverse kept)) (for-each (lambda (address) (unless (assignment network address) (let ((sequence (peer-id-pool-acquire! peer))) (when sequence (set-connection-network-assignments! network (append (connection-network-assignments network) (list (cons (bytevector-copy address) sequence)))))))) addresses))) ;; Return owned (sequence, destination CID) or #f when no unused ID exists. ;; The connection snapshots this together with ADDRESS into sealed output; ;; an endpoint must never send old queued bytes to a later current peer. (define (network-destination network address) (let ((entry (assignment network address))) (and entry (let ((cid (peer-id-pool-id (connection-network-peer network) (cdr entry)))) (and cid (vector (cdr entry) cid)))))) (define (network-local-count network) (if (connection-network-local network) (local-id-pool-count (connection-network-local network)) 0)) (define (network-local-limit network) (connection-network-limit network)) ;; The connection supplies unpredictable CID/token bytes, queues a returned ;; advertisement once, checks it before sealing, and commits after recording ;; the protected packet. Queueing alone never makes the CID routable. (define (network-issue-id! network cid token) (require-open! network) (local-id-pool-reserve! (connection-network-local network) cid token)) (define (network-issue-spare! network) (require-open! network) (local-id-pool-reserve-random! (connection-network-local network))) (define (network-check-id! network frame) (require-open! network) (local-id-pool-check! (connection-network-local network) frame)) (define (network-commit-id! network frame) (require-open! network) (local-id-pool-commit! (connection-network-local network) frame)) (define (network-retire-local-id! network sequence packet-destination-id) (require-open! network) (local-id-pool-retire! (connection-network-local network) sequence packet-destination-id)) (define (network-route network cid) (and (connection-network-local network) (local-id-pool-route (connection-network-local network) cid))) (define (network-route-lengths network) (if (connection-network-local network) (local-id-pool-lengths (connection-network-local network)) '())) ;; Owned active CID snapshot for bounded close-state routing. Never exposes ;; reset tokens or private inventory entries to the packet connection. (define (network-local-id network sequence) (and (connection-network-local network) (local-id-pool-id (connection-network-local network) sequence))) (define (network-peer-id! network frame) (require-open! network) (let ((active? (peer-id-pool-receive! (connection-network-peer network) frame))) (synchronize! network) active?)) ;; The connection deduplicates these reliable frames against its queued ;; and sent controls. Repeated calls do not forget unacknowledged IDs. (define (network-retirements network) (if (connection-network-peer network) (peer-id-pool-retirements (connection-network-peer network)) '())) (define (network-retirement-ack! network sequence) (require-open! network) (peer-id-pool-ack! (connection-network-peer network) sequence)) (define (network-observe! network address size number frames now) (require-confirmed! network) (unless (and (list? frames) (not (any (lambda (frame) (not (quic-frame? frame))) frames))) (error "QUIC network: expected completely decoded authenticated frames")) (let ((accepted? (path-manager-observe! (connection-network-paths network) address size number (not (any (lambda (frame) (not (memv (quic-frame-type frame) '(0 24 26 27)))) frames)) now))) (synchronize! network) accepted?)) (define (network-tick! network now) (require-confirmed! network) (path-manager-tick! (connection-network-paths network) now) (synchronize! network)) (define (network-reserve-send! network address size now) (require-confirmed! network) (and (network-destination network address) (path-manager-send! (connection-network-paths network) address size now))) ;; Return owned (address, peer sequence, peer CID, challenge token). The ;; reservation is already charged for exactly1200 bytes; do not charge it ;; again in network-reserve-send!. No CID means no probe or credit spend. (define (network-challenge! network address now) (require-confirmed! network) (let ((destination (network-destination network address))) (and destination (let ((data (path-manager-challenge! (connection-network-paths network) address now))) (and data (vector (bytevector-copy address) (vector-ref destination 0) (vector-ref destination 1) data)))))) (define (network-response! network data now) (require-confirmed! network) (let ((matched? (path-manager-response! (connection-network-paths network) data now))) (synchronize! network) matched?)) (define (network-take-events! network) (if (connection-network-paths network) (path-manager-take-events! (connection-network-paths network)) '())) (define (network-close! network) (unless (connection-network-closed? network) (when (connection-network-local network) (local-id-pool-close! (connection-network-local network))) (when (connection-network-peer network) (peer-id-pool-close! (connection-network-peer network))) (when (connection-network-paths network) (path-manager-close! (connection-network-paths network))) (set-connection-network-local! network #f) (set-connection-network-peer! network #f) (set-connection-network-paths! network #f) (set-connection-network-original-address! network #f) (set-connection-network-assignments! network '()) (set-connection-network-closed?! network #t)) #t)))src/sigil/quic/connection/peer/ids.sgladded
;;; Bounded ownership of authenticated peer connection IDs. Callers assign;;; acquired IDs to paths and reliably send/acknowledge pending retirements.(define-library (sigil quic connection peer ids) (import (sigil struct) (sigil quic bytes) (sigil quic error) (sigil quic frame) (sigil quic wire)) (export make-peer-id-pool peer-id-pool? peer-id-pool-count peer-id-pool-receive! peer-id-pool-acquire! peer-id-pool-id peer-id-pool-sequences peer-id-pool-retire! peer-id-pool-retirements peer-id-pool-ack! peer-id-pool-close!) (begin (define-struct peer-id-pool (entries mutable: #t) (limit) (zero?) (peer-floor default: 0 mutable: #t) (retired-floor default: 0 mutable: #t) (closed? default: #f mutable: #t)) ;; Entry: sequence, owned CID, owned reset token/#f, acquired?, retiring?. ;; At most limit active plus twice limit awaiting RETIRE acknowledgment. (define (new-entry sequence cid token acquired?) (vector sequence (bytevector-copy cid) (and token (bytevector-copy token)) acquired? #f)) (define (uint? value) (and (integer? value) (exact? value) (<= 0 value quic-varint-maximum))) (define (id? cid minimum) (and (bytevector? cid) (<= minimum (bytevector-length cid) 20))) (define (token? token) (and (bytevector? token) (= 16 (bytevector-length token)))) (define (make-peer-id-pool initial (keys: (limit 2) (token #f) (preferred #f))) (unless (and (id? initial 0) (uint? limit) (<= 2 limit 8) (or (not token) (and (> (bytevector-length initial) 0) (token? token))) (or (not preferred) (and (> (bytevector-length initial) 0) (pair? preferred) (id? (car preferred) 1) (token? (cdr preferred)) (not (bytes=? initial (car preferred)))))) (error "QUIC peer IDs: invalid initial ID, limits or authenticated metadata")) ;; Construct only after source-ID / preferred-address / token metadata ;; has been authenticated. The original path already uses sequence zero. (peer-id-pool entries: (cons (new-entry 0 initial token #t) (if preferred (list (new-entry 1 (car preferred) (cdr preferred) #f)) '())) limit: limit zero?: (= (bytevector-length initial) 0))) (define (require-open! pool) (when (peer-id-pool-closed? pool) (error "QUIC peer IDs: pool is closed"))) (define (find-entry entries sequence) (and (pair? entries) (if (= sequence (vector-ref (car entries) 0)) (car entries) (find-entry (cdr entries) sequence)))) (define (count-state entries retiring?) (let loop ((rest entries) (count 0)) (if (null? rest) count (loop (cdr rest) (+ count (if (eq? (vector-ref (car rest) 4) retiring?) 1 0)))))) (define (peer-id-pool-count pool) (count-state (peer-id-pool-entries pool) #f)) (define (capacity! pool entries) (when (or (> (count-state entries #f) (peer-id-pool-limit pool)) (> (count-state entries #t) (* 2 (peer-id-pool-limit pool)))) (raise-quic-error 9 24 "QUIC peer connection ID inventory limit exceeded"))) (define (peer-id-pool-receive! pool frame) (require-open! pool) (unless (and (quic-frame? frame) (= 24 (quic-frame-type frame))) (error "QUIC peer IDs: expected NEW_CONNECTION_ID")) (when (peer-id-pool-zero? pool) (raise-quic-error 10 24 "QUIC peer IDs: NEW_CONNECTION_ID after zero-length source ID")) (let ((sequence (frame-ref frame 'sequence)) (retire (frame-ref frame 'retire-prior-to)) (cid (frame-ref frame 'connection-id)) (token (frame-ref frame 'token))) (unless (and (uint? sequence) (uint? retire) (<= retire sequence) (id? cid 1) (token? token)) (raise-quic-error 7 24 "QUIC peer IDs: invalid NEW_CONNECTION_ID fields")) (let* ((entries (peer-id-pool-entries pool)) (known (find-entry entries sequence)) (floor (max retire (peer-id-pool-peer-floor pool)))) (when (and known (or (not (bytes=? cid (vector-ref known 1))) (and (vector-ref known 2) (not (bytes-timing-safe=? token (vector-ref known 2)))))) (raise-quic-error 10 24 "QUIC peer IDs: conflicting sequence metadata")) (for-each (lambda (value) (when (and (not (= sequence (vector-ref value 0))) (bytes=? cid (vector-ref value 1))) (raise-quic-error 10 24 "QUIC peer IDs: connection ID reused with another sequence"))) entries) ;; Plan changes without touching owned entries, so limit errors leave ;; the original path and its inventory intact. RetirePriorTo applies ;; before counting the newly supplied active ID. (let* ((planned (map (lambda (value) (vector (vector-ref value 0) (vector-ref value 1) (if (and known (= sequence (vector-ref value 0)) (not (vector-ref value 2))) (bytevector-copy token) (vector-ref value 2)) (vector-ref value 3) (or (vector-ref value 4) (< (vector-ref value 0) floor)))) entries)) (incoming (or (find-entry planned sequence) (new-entry sequence cid token #f)))) (unless known (vector-set! incoming 4 (< sequence (max floor (peer-id-pool-retired-floor pool)))) (set! planned (append planned (list incoming)))) (capacity! pool planned) (set-peer-id-pool-entries! pool planned) (set-peer-id-pool-peer-floor! pool floor) (for-each (lambda (value) (when (vector-ref value 4) (set-peer-id-pool-retired-floor! pool (max (peer-id-pool-retired-floor pool) (+ 1 (vector-ref value 0)))))) planned) (not (vector-ref incoming 4)))))) ;; A CID is assigned to at most one path. A failed candidate retires it; ;; releasing it for another address would permit cross-path correlation. (define (peer-id-pool-acquire! pool) (require-open! pool) (let loop ((rest (peer-id-pool-entries pool))) (and (pair? rest) (let ((value (car rest))) (if (or (vector-ref value 3) (vector-ref value 4)) (loop (cdr rest)) (begin (vector-set! value 3 #t) (vector-ref value 0))))))) (define (peer-id-pool-id pool sequence) (unless (uint? sequence) (error "QUIC peer IDs: invalid sequence")) (let ((value (find-entry (peer-id-pool-entries pool) sequence))) (and value (not (vector-ref value 4)) (bytevector-copy (vector-ref value 1))))) (define (peer-id-pool-sequences pool) (let loop ((rest (peer-id-pool-entries pool)) (out '())) (if (null? rest) (reverse out) (let ((value (car rest))) (loop (cdr rest) (if (vector-ref value 4) out (cons (vector-ref value 0) out))))))) (define (peer-id-pool-retire! pool sequence) (require-open! pool) (unless (uint? sequence) (error "QUIC peer IDs: invalid sequence")) (let ((value (find-entry (peer-id-pool-entries pool) sequence))) (unless value (error "QUIC peer IDs: cannot retire an unknown ID")) (when (peer-id-pool-zero? pool) (error "QUIC peer IDs: cannot retire a zero-length ID")) (and (not (vector-ref value 4)) (begin (when (>= (count-state (peer-id-pool-entries pool) #t) (* 2 (peer-id-pool-limit pool))) (raise-quic-error 9 24 "QUIC peer IDs: too many pending retirements")) (vector-set! value 4 #t) (set-peer-id-pool-retired-floor! pool (max (peer-id-pool-retired-floor pool) (+ sequence 1))) #t)))) ;; Keep all pending retirements until a carrying packet is acknowledged. ;; The connection deduplicates/retransmits these reliable control frames. (define (peer-id-pool-retirements pool) (let loop ((rest (peer-id-pool-entries pool)) (out '())) (if (null? rest) (reverse out) (let ((value (car rest))) (loop (cdr rest) (if (vector-ref value 4) (cons (make-quic-frame 25 (list (cons 'sequence (vector-ref value 0)))) out) out)))))) (define (wipe-token! value) (let ((token (vector-ref value 2))) (when token (do ((index 0 (+ index 1))) ((= index (bytevector-length token))) (bytevector-u8-set! token index 0))) (vector-set! value 2 #f))) ;; Only call after validating an ACK for a packet actually carrying RETIRE. ;; A compact local retirement floor prevents forgotten IDs resurrecting. ;; Existing live IDs below that floor are explicit exceptions; previously ;; unseen older IDs are immediately retired instead of assigned to a path. (define (peer-id-pool-ack! pool sequence) (require-open! pool) (unless (uint? sequence) (error "QUIC peer IDs: invalid retirement acknowledgment")) (let ((value (find-entry (peer-id-pool-entries pool) sequence))) (and value (vector-ref value 4) (begin (wipe-token! value) (set-peer-id-pool-entries! pool (let loop ((rest (peer-id-pool-entries pool)) (out '())) (cond ((null? rest) (reverse out)) ((= sequence (vector-ref (car rest) 0)) (loop (cdr rest) out)) (else (loop (cdr rest) (cons (car rest) out)))))) #t)))) (define (peer-id-pool-close! pool) (unless (peer-id-pool-closed? pool) (for-each wipe-token! (peer-id-pool-entries pool)) (set-peer-id-pool-entries! pool '()) (set-peer-id-pool-closed?! pool #t)) #t)))src/sigil/quic/endpoint.sglmodified
;;; Private Q1 UDP adapter. Loopback only until independent security review.;;; One fixed peer, one connection, bounded work and protected output per step.;;; One connection, addressed protected output and bounded work per step.;;; The address codec/bind below remains loopback-only pending shared UDP support.(define-library (sigil quic endpoint) (import (sigil crypto) (sigil struct) (export open-quic-endpoint quic-endpoint? quic-endpoint-port quic-endpoint-connection quic-endpoint-closed? quic-endpoint-failure quic-endpoint-attach! quic-endpoint-accept! quic-endpoint-accept-retry! quic-endpoint-peer-address quic-endpoint-take-path-events! quic-endpoint-step! quic-endpoint-close!) (begin (set-quic-endpoint-peer-port! endpoint peer-port) (set-quic-endpoint-connection! endpoint connection) (guard (exn (else (quic-endpoint-close! endpoint) (raise exn))) (quic-bind-peer-address! connection (retry-address peer-port)) (when (eq? (quic-role connection) 'client) (quic-start! connection (endpoint-now endpoint))))) ;; The factory receives copied (original-DCID, client-SCID). It constructs (set-quic-endpoint-retry-manager! endpoint #f) (when manager (close-retry-token-manager! manager)))) ;; Address bytes are the same canonical loopback-IP/port tuple used by ;; Retry. General numeric IPv4/IPv6 encoding belongs to the shared socket ;; contract; this adapter slice deliberately retains the existing bind. (define (output-port output) (let ((address (quic-output-address output))) (unless (and (= (bytevector-length address) 6) (= (bytevector-u8-ref address 0) 127) (= (bytevector-u8-ref address 1) 0) (= (bytevector-u8-ref address 2) 0) (= (bytevector-u8-ref address 3) 1)) (error "QUIC endpoint: output has an unsupported address")) (let ((port (+ (* 256 (bytevector-u8-ref address 4)) (bytevector-u8-ref address 5)))) (unless (port? port 1) (error "QUIC endpoint: output has an invalid port")) port))) (define (quic-endpoint-peer-address endpoint) (let ((connection (quic-endpoint-connection endpoint))) (and connection (quic-peer-address connection)))) (define (quic-endpoint-take-path-events! endpoint) (let ((connection (quic-endpoint-connection endpoint))) (if connection (quic-take-path-events! connection) '()))) ;; Pending entries pair immutable protected output with its own deadline ;; origin. A new batch must not reset an older datagram's blocked age. (define (send-pending! endpoint now) (let loop () (when (pair? (quic-endpoint-pending endpoint)) (let* ((packet (car (quic-endpoint-pending endpoint))) (sent (udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" (quic-endpoint-peer-port endpoint)))) (cond ((and sent (= sent (bytevector-length packet))) (set-quic-endpoint-pending! endpoint (cdr (quic-endpoint-pending endpoint))) (loop)) (sent (error "QUIC endpoint: partial UDP datagram send")) ((not (quic-endpoint-pending-since endpoint)) (set-quic-endpoint-pending-since! endpoint now)) ((>= (- now (quic-endpoint-pending-since endpoint)) 1000) (error "QUIC endpoint: UDP output remained blocked")))))) (when (null? (quic-endpoint-pending endpoint)) (set-quic-endpoint-pending-since! endpoint #f))) (let ((connection (quic-endpoint-connection endpoint)) (pending (quic-endpoint-pending endpoint)) (kept '()) (count 0) (since #f)) (when (> (length pending) 64) (error "QUIC endpoint: protected send-attempt budget exceeded")) (for-each (lambda (entry) (let ((output (car entry)) (queued-at (cdr entry))) ;; Check before every send attempt, including failed retries. (when (and (quic-output-valid? connection output) (< (- now queued-at) 1000)) (let* ((packet (quic-output-bytes output)) (sent (udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" (output-port output)))) (cond ((and sent (= sent (bytevector-length packet))) #t) (sent (error "QUIC endpoint: partial UDP datagram send")) ((< count 32) (set! kept (cons entry kept)) (set! count (+ count 1)) (set! since (if since (min since queued-at) queued-at)))))))) pending) ;; Overflow is UDP loss: reliable frames retain connection flight/PTO ;; ownership; DATAGRAM data is unreliable. Never reseal or reroute it. (set-quic-endpoint-pending! endpoint (reverse kept)) (set-quic-endpoint-pending-since! endpoint since))) (define (flush! endpoint now) ;; An idle close is silent: even previously sealed blocked output dies. (quic-idle-expired? (quic-endpoint-connection endpoint))) (set-quic-endpoint-pending! endpoint '()) (set-quic-endpoint-pending-since! endpoint #f)) (send-pending! endpoint now) (when (and (null? (quic-endpoint-pending endpoint)) (quic-endpoint-connection endpoint)) (let ((packets (quic-take-datagrams! (quic-endpoint-connection endpoint) now))) (when (or (> (length packets) 32) (any (lambda (packet) (> (bytevector-length packet) 1200)) packets)) ;; Collect a bounded fresh batch even when old-path sends are blocked. ;; At most32 retained plus32 new records are attempted once each; a ;; failed address cannot stop a candidate path's validation controls. (when (quic-endpoint-connection endpoint) (let ((packets (quic-take-addressed-datagrams! (quic-endpoint-connection endpoint) now))) (when (or (> (length packets) 32) (any (lambda (output) (or (not (quic-output? output)) (> (bytevector-length (quic-output-bytes output)) 1200))) packets)) (error "QUIC endpoint: protected output budget exceeded")) (set-quic-endpoint-pending! endpoint packets) (send-pending! endpoint now)))) (set-quic-endpoint-pending! endpoint (append (quic-endpoint-pending endpoint) (map (lambda (output) (cons output now)) packets))))) (send-pending! endpoint now)) ;; Initial keys establish packet integrity, not address ownership. Reject ;; noise before allocating the connection or claiming its peer-port slot. (bytes=? (quic-local-id connection) (cdr source-parameter)) (bytes=? retry (cdr retry-parameter))) (error "QUIC endpoint: Retry factory connection ID metadata mismatch"))) (quic-bind-peer-address! connection (retry-address peer-port)) (set-quic-endpoint-peer-port! endpoint peer-port) (set-quic-endpoint-connection! endpoint connection) (set! transferred? #t)) (let ((connection ((quic-endpoint-acceptor endpoint) (bytevector-copy (quic-header-destination-id header)) (bytevector-copy (quic-header-source-id header))))) (unless (and (quic-connection? connection) (eq? (quic-role connection) 'server) (not (quic-closed? connection))) (error "QUIC endpoint: factory must return a fresh server connection")) (set-quic-endpoint-peer-port! endpoint peer-port) (set-quic-endpoint-connection! endpoint connection)))))))) (guard (exn (else (when (quic-connection? connection) (quic-abort! connection)) (raise exn))) (unless (and (quic-connection? connection) (eq? (quic-role connection) 'server) (not (quic-closed? connection))) (error "QUIC endpoint: factory must return a fresh server connection")) (quic-bind-peer-address! connection (retry-address peer-port)) (set-quic-endpoint-peer-port! endpoint peer-port) (set-quic-endpoint-connection! endpoint connection))))))))) ;; Preserve the configured admission port, not the tentative source pin. ;; A failure before confirmation releases the slot without closing UDP. (set-quic-endpoint-connection! endpoint #f) (set-quic-endpoint-peer-port! endpoint (quic-endpoint-accept-port endpoint))))))) ;; The configured admission port applies before confirmation. A confirmed ;; server must authenticate candidate paths in the connection; a client ;; keeps its original server-source restriction. (define (receive-port? endpoint port) (let ((connection (quic-endpoint-connection endpoint))) (or (and connection (eq? (quic-role connection) 'server) (quic-confirmed? connection)) (not (quic-endpoint-peer-port endpoint)) (= port (quic-endpoint-peer-port endpoint))))) (define (receive! endpoint now) ;; Out-of-peer noise must not consume the connection's receive budget. ;; A separate read ceiling still bounds work under a continuous flood. (when received (let ((processed? #f)) (when (and (equal? (cadr received) "127.0.0.1") (or (not (quic-endpoint-peer-port endpoint)) (= (caddr received) (quic-endpoint-peer-port endpoint)))) (port? (caddr received) 1) (receive-port? endpoint (caddr received))) ;; Native UDP strings retain byte_length/data; string->utf8 ;; copies these exact bytes, including NUL and invalid UTF-8. (let ((bytes (string->utf8 (car received)))) (unless (quic-endpoint-connection endpoint) (admit! endpoint bytes (caddr received) now)) (when (quic-endpoint-connection endpoint) (set! processed? #t) (quic-receive! (quic-endpoint-connection endpoint) bytes now) (quic-receive! (quic-endpoint-connection endpoint) bytes now address: (retry-address (caddr received))) (reconcile-admission! endpoint now))))) (loop (+ reads 1) (+ admitted (if processed? 1 0)))))))))src/sigil/quic/path/manager.sgladded
;;; Socket-free selection of a confirmed connection's peer path. The caller;;; supplies authenticated, replay-checked packet observations, credits each;;; UDP datagram once, and assigns a distinct peer CID before probing a path.(define-library (sigil quic path manager) (import (sigil struct) (sigil quic bytes) (sigil quic wire) (sigil quic path validation)) (export make-path-manager path-manager? path-manager-address path-manager-candidate path-manager-previous-address path-manager-observe! path-manager-send! path-manager-challenge! path-manager-response! path-manager-tick! path-manager-take-events! path-manager-close!) (begin (define-struct path-manager (role) (pto) (now mutable: #t) (active mutable: #t) (candidate-state default: #f mutable: #t) (eligible? default: #f mutable: #t) (previous default: #f mutable: #t) (highest default: -1 mutable: #t) (events default: '() mutable: #t) (closed? default: #f mutable: #t)) (define (address? address) (and (bytevector? address) (<= 1 (bytevector-length address) 64))) (define (make-path-manager address role now (keys: (pto 1000) (highest -1))) (unless (and (memq role '(client server)) (integer? highest) (exact? highest) (<= -1 highest quic-varint-maximum)) (error "QUIC paths: invalid role or packet-number baseline")) ;; The caller has confirmed the handshake and authenticated the original ;; address, including the original path's minimum 1200-byte datagram. (path-manager role: role pto: pto now: now highest: highest active: (make-path-validation address now pto: pto validated?: #t mtu?: #t))) (define (require-open! manager) (when (path-manager-closed? manager) (error "QUIC paths: manager is closed"))) (define (same? path address) (and path (bytes=? address (path-validation-address path)))) (define (path-manager-address manager) (and (path-manager-active manager) (path-validation-address (path-manager-active manager)))) (define (path-manager-candidate manager) (and (path-manager-candidate-state manager) (path-validation-address (path-manager-candidate-state manager)))) (define (path-manager-previous-address manager) (and (path-manager-previous manager) (path-validation-address (path-manager-previous manager)))) ;; Events own their address bytes. Keep the newest sixteen observations; ;; an application that does not drain diagnostics cannot grow path state. (define (event! manager kind path reason) (let ((events (path-manager-events manager))) (set-path-manager-events! manager (append (if (= (length events) 16) (cdr events) events) (list (vector kind (path-validation-address path) reason)))))) (define (cancel-candidate! manager reason) (let ((path (path-manager-candidate-state manager))) (when path (event! manager 'path-failed path reason) (path-validation-close! path) (set-path-manager-candidate-state! manager #f) (set-path-manager-eligible?! manager #f)))) (define (close-previous! manager) (when (path-manager-previous manager) (path-validation-close! (path-manager-previous manager)) (set-path-manager-previous! manager #f))) (define (probe-previous! manager) (close-previous! manager) ;; Fresh reachability exchange on the old path mitigates forwarding. ;; It retains that path's previously established amplification exemption. (set-path-manager-previous! manager (make-path-validation (path-manager-address manager) (path-manager-now manager) pto: (path-manager-pto manager) validated?: #t))) (define (path-manager-tick! manager now) (require-open! manager) (unless (and (integer? now) (exact? now) (>= now (path-manager-now manager))) (error "QUIC paths: time must be monotonic exact milliseconds")) (set-path-manager-now! manager now) (path-validation-tick! (path-manager-active manager) now) (when (and (path-manager-candidate-state manager) (path-validation-tick! (path-manager-candidate-state manager) now)) (cancel-candidate! manager 'validation-timeout)) (when (and (path-manager-previous manager) (path-validation-tick! (path-manager-previous manager) now)) (event! manager 'path-failed (path-manager-previous manager) 'previous-validation-timeout) (close-previous! manager)) #t) (define (promote! manager) (let ((path (path-manager-candidate-state manager))) (and path (path-manager-eligible? manager) (path-validation-mtu-valid? path) (begin (path-validation-close! (path-manager-active manager)) (set-path-manager-active! manager path) (set-path-manager-candidate-state! manager #f) (set-path-manager-eligible?! manager #f) (event! manager 'path-validated path 'active) #t)))) (define (path-manager-observe! manager address size number probing? now) (unless (and (address? address) (integer? size) (exact? size) (<= 0 size 65527) (integer? number) (exact? number) (<= 0 number quic-varint-maximum) (boolean? probing?)) (error "QUIC paths: invalid authenticated observation")) (path-manager-tick! manager now) (let* ((active (path-manager-active manager)) (candidate (path-manager-candidate-state manager)) (newest? (and (not probing?) (> number (path-manager-highest manager))))) (cond ((same? active address) (path-validation-credit! active size now) (when newest? (set-path-manager-highest! manager number) (when candidate (when (path-manager-eligible? manager) (close-previous! manager)) (cancel-candidate! manager 'returned-to-active))) #t) ;; Preferred-server-address migration is not initiated by this ;; manager. A client must discard unknown server source addresses. ((eq? (path-manager-role manager) 'client) #f) ((same? candidate address) (when newest? (set-path-manager-highest! manager number) (unless (path-manager-eligible? manager) ;; Probe-only observations cannot consume the later migration's ;; validation timer. Discard their credit conservatively. (path-validation-close! candidate) (set! candidate (make-path-validation address now pto: (path-manager-pto manager))) (set-path-manager-candidate-state! manager candidate) (set-path-manager-eligible?! manager #t) (probe-previous! manager))) (path-validation-credit! candidate size now)) ((or newest? (and probing? (not candidate))) (cancel-candidate! manager 'superseded) (let ((path (make-path-validation address now pto: (path-manager-pto manager)))) (set-path-manager-candidate-state! manager path) (set-path-manager-eligible?! manager newest?) (when newest? (set-path-manager-highest! manager number) (probe-previous! manager)) (event! manager 'path-changed path (if newest? 'non-probing 'probing)) (path-validation-credit! path size now))) (else #f)))) ;; Reserve every protected output before sealing. Non-probing application ;; output always uses path-manager-address; candidate sends are controls. (define (path-manager-send! manager address size now) (unless (and (address? address) (integer? size) (exact? size) (<= 1 size 1200)) (error "QUIC paths: invalid output address or datagram size")) (path-manager-tick! manager now) (let ((path (find (lambda (value) (same? value address)) (list (path-manager-active manager) (path-manager-candidate-state manager) (path-manager-previous manager))))) (and path (path-validation-send! path size now)))) ;; The caller first assigns the destination CID for ADDRESS. A returned ;; token reserves exactly 1200 wire bytes; packet loss gets a fresh token ;; after PTO, never transport retransmission of an old challenge frame. (define (path-manager-challenge! manager address now) (unless (address? address) (error "QUIC paths: invalid challenge address")) (path-manager-tick! manager now) (let ((candidate (path-manager-candidate-state manager)) (previous (path-manager-previous manager))) (cond ((and (path-manager-eligible? manager) (same? candidate address)) (path-validation-challenge! candidate 1200 now)) ((same? previous address) (path-validation-challenge! previous 1200 now)) (else #f)))) ;; Match across attempts rather than the response packet's arrival path. (define (path-manager-response! manager data now) (unless (and (bytevector? data) (= 8 (bytevector-length data))) (error "QUIC paths: invalid response data")) (path-manager-tick! manager now) (let ((candidate (path-manager-candidate-state manager)) (previous (path-manager-previous manager)) (matched? #f)) (when (and candidate (path-validation-response! candidate data now)) (set! matched? #t) (promote! manager)) (when (and previous (path-validation-response! previous data now)) (set! matched? #t) (event! manager 'path-validated previous 'previous) (close-previous! manager)) matched?)) (define (path-manager-take-events! manager) (let ((events (path-manager-events manager))) (set-path-manager-events! manager '()) events)) (define (path-manager-close! manager) (unless (path-manager-closed? manager) (when (path-manager-candidate-state manager) (path-validation-close! (path-manager-candidate-state manager))) (path-validation-close! (path-manager-active manager)) (close-previous! manager) (set-path-manager-active! manager #f) (set-path-manager-candidate-state! manager #f) (set-path-manager-events! manager '()) (set-path-manager-closed?! manager #t)) #t)))src/sigil/quic/path/validation.sgladded
;;; One bounded RFC 9000 validation attempt, independent of sockets and routing.;;; The connection supplies authenticated observations and reserves every output;;; datagram here before sealing it. Address keys are canonical caller-owned bytes.(define-library (sigil quic path validation) (import (sigil struct) (sigil crypto) (sigil quic bytes)) (export make-path-validation path-validation? path-validation-address path-validation-status path-validation-address-valid? path-validation-mtu-valid? path-validation-credit! path-validation-send! path-validation-challenge! path-validation-response! path-validation-tick! path-validation-close!) (begin (define-struct path-validation (key) (pto) (now mutable: #t) (deadline mutable: #t) (next-probe mutable: #t) (address-valid? default: #f mutable: #t) (mtu? default: #f mutable: #t) (received default: 0 mutable: #t) (sent default: 0 mutable: #t) (phase-probes default: 0 mutable: #t) (challenges default: '() mutable: #t) (failed? default: #f mutable: #t) (closed? default: #f mutable: #t)) (define (time? now) (and (integer? now) (exact? now) (>= now 0))) (define (make-path-validation address now (keys: (pto 1000) (validated? #f) (mtu? #f))) (unless (and (bytevector? address) (<= 1 (bytevector-length address) 64) (time? now) (integer? pto) (exact? pto) (<= 1000 pto 60000) (boolean? validated?) (boolean? mtu?) (or validated? (not mtu?))) (error "QUIC path: invalid address, time, PTO or validation state")) (path-validation key: (bytevector-copy address) now: now pto: pto deadline: (+ now (* 3 pto)) next-probe: now address-valid?: validated? mtu?: mtu?)) (define (path-validation-address path) (bytevector-copy (path-validation-key path))) (define (path-validation-status path) (cond ((path-validation-closed? path) 'closed) ((path-validation-failed? path) 'failed) ((and (path-validation-address-valid? path) (path-validation-mtu? path)) 'validated) (else 'validating))) (define (path-validation-mtu-valid? path) (eq? (path-validation-status path) 'validated)) (define (require-open! path) (when (path-validation-closed? path) (error "QUIC path: validation is closed"))) ;; These owned bytevectors are challenge payloads, not native key handles. ;; Clear them before releasing the attempt's references. (define (wipe! bytes) (do ((index 0 (+ index 1))) ((= index (bytevector-length bytes))) (bytevector-u8-set! bytes index 0))) (define (wipe-challenges! path) (for-each (lambda (entry) (wipe! (vector-ref entry 0))) (path-validation-challenges path)) (set-path-validation-challenges! path '())) ;; Expiry is independent of incoming ACKs, new data and unknown responses. (define (path-validation-tick! path now) (require-open! path) (unless (and (time? now) (>= now (path-validation-now path))) (error "QUIC path: time must be monotonic exact milliseconds")) (set-path-validation-now! path now) (when (and (eq? (path-validation-status path) 'validating) (>= now (path-validation-deadline path))) (set-path-validation-failed?! path #t) (set-path-validation-address-valid?! path #f) (set-path-validation-mtu?! path #f) (wipe-challenges! path)) (path-validation-failed? path)) ;; The credit caller must have authenticated this path's packet. One UDP ;; datagram is credited once, including coalesced packets. Saturation is ;; conservative: it can only reduce available unvalidated-path credit. (define (path-validation-credit! path size now) (unless (and (integer? size) (exact? size) (<= 0 size 65527)) (error "QUIC path: invalid received datagram size")) (path-validation-tick! path now) (and (not (path-validation-failed? path)) (begin (set-path-validation-received! path (min 1073741824 (+ (path-validation-received path) size))) #t))) (define (wire-size! size) (unless (and (integer? size) (exact? size) (<= 1 size 1200)) (error "QUIC path: invalid protected datagram size"))) (define (send-allowed? path size) (and (not (path-validation-failed? path)) (or (path-validation-address-valid? path) (<= (+ (path-validation-sent path) size) (* 3 (path-validation-received path)))))) ;; This is a conservative reservation. A subsequently blocked/dropped UDP ;; send does not refund credit; previously sealed output cannot spend twice. (define (path-validation-send! path size now) (wire-size! size) (path-validation-tick! path now) (and (send-allowed? path size) (begin (set-path-validation-sent! path (+ (path-validation-sent path) size)) #t))) (define (fresh-challenge path) (let loop ((attempt 0)) (when (= attempt 4) (error "QUIC path: repeated challenge entropy")) (let ((data (random-bytes 8))) (unless (and (bytevector? data) (= (bytevector-length data) 8)) (error "QUIC path: challenge entropy unavailable")) (if (any (lambda (entry) (bytes=? data (vector-ref entry 0))) (path-validation-challenges path)) (begin (wipe! data) (loop (+ attempt 1))) data)))) ;; Return one owned challenge or #f. The caller uses exactly SIZE wire ;; bytes on this path. At most three probes per address/MTU phase, six ;; remembered challenges total; retries use fresh unpredictable payloads. (define (path-validation-challenge! path size now) (wire-size! size) (path-validation-tick! path now) (and (eq? (path-validation-status path) 'validating) (< (path-validation-phase-probes path) 3) (>= now (path-validation-next-probe path)) (or (not (path-validation-address-valid? path)) (= size 1200)) (send-allowed? path size) (let ((data (fresh-challenge path))) (set-path-validation-sent! path (+ (path-validation-sent path) size)) (set-path-validation-phase-probes! path (+ 1 (path-validation-phase-probes path))) (set-path-validation-next-probe! path (+ now (path-validation-pto path))) (set-path-validation-challenges! path (cons (vector data (= size 1200) #f) (path-validation-challenges path))) (bytevector-copy data)))) ;; An authenticated matching response on ANY incoming path validates the ;; path on which this challenge was sent (RFC 9000 8.2.3). The caller finds ;; the outstanding attempt by payload, not by the response's source address. (define (path-validation-response! path data now) (unless (and (bytevector? data) (= (bytevector-length data) 8)) (error "QUIC path: invalid response payload")) (path-validation-tick! path now) (and (eq? (path-validation-status path) 'validating) (let ((entry (find (lambda (candidate) (and (not (vector-ref candidate 2)) (bytes-timing-safe=? data (vector-ref candidate 0)))) (path-validation-challenges path)))) (and entry (begin (vector-set! entry 2 #t) (cond ((vector-ref entry 1) (set-path-validation-address-valid?! path #t) (set-path-validation-mtu?! path #t) (wipe-challenges! path) 'validated) ((not (path-validation-address-valid? path)) (set-path-validation-address-valid?! path #t) ;; A short probe proves reachability, not the 1200-byte ;; MTU. Start one bounded, separately paced MTU phase. (set-path-validation-phase-probes! path 0) (set-path-validation-next-probe! path now) (set-path-validation-deadline! path (+ now (* 3 (path-validation-pto path)))) 'address-validated) (else #f))))))) (define (path-validation-close! path) (unless (path-validation-closed? path) (wipe-challenges! path) (set-path-validation-address-valid?! path #f) (set-path-validation-mtu?! path #f) (set-path-validation-closed?! path #t)))))test/test-addressed-output.sgladded
(import (sigil test) (sigil crypto) (sigil quic bytes) (sigil quic connection) (sigil quic tls))(define (ao-with-client procedure) (let ((key (ecdsa-p256-generate-keypair)) (client #f)) (dynamic-wind (lambda () #t) (lambda () (set! client (make-quic-client (list #u8(113 49)) 'pinned-key '() (make-tls-pin-verifier (cdr key)) local-id: #u8(1) destination-id: #u8(10 11 12 13 14 15 16 17))) (procedure client)) (lambda () (when client (quic-abort! client)) (do ((index 0 (+ index 1))) ((= index (bytevector-length (car key)))) (bytevector-u8-set! (car key) index 0))))))(define (ao-first! client) (quic-bind-peer-address! client #u8(42)) (quic-start! client 0) (let ((packets (quic-take-addressed-datagrams! client 0))) (unless (= 1 (length packets)) (error "Expected one addressed Initial in this fixture")) (car packets)))(test-group "Protected datagram destination ownership" (test "addressed-output: original address is bound once and owned before handshake start" (ao-with-client (lambda (client) (let ((address (make-bytevector 1 42))) (quic-bind-peer-address! client address) (bytevector-u8-set! address 0 43) (let ((copy (quic-peer-address client))) (bytevector-u8-set! copy 0 44)) (assert-true (bytes=? #u8(42) (quic-peer-address client))) (assert-error (quic-bind-peer-address! client #u8(99))) (quic-start! client 0) (assert-error (quic-bind-peer-address! client #u8(99))))))) (test "addressed-output: raw and addressed output contracts cannot silently drop destination metadata" (ao-with-client (lambda (client) (assert-error (quic-take-addressed-datagrams! client 0)) (quic-bind-peer-address! client #u8(42)) (assert-error (quic-take-datagrams! client 0)) (quic-start! client 0) (let ((packets (quic-take-addressed-datagrams! client 0))) (assert-true (and (= 1 (length packets)) (quic-output? (car packets)) (quic-output-valid? client (car packets)) (= 1200 (bytevector-length (quic-output-bytes (car packets)))) (bytes=? #u8(42) (quic-output-address (car packets))) (bytes=? #u8(10 11 12 13 14 15 16 17) (quic-output-id (car packets))))))))) (test "addressed-output: returned bytes address and CID cannot mutate sealed output ownership" (ao-with-client (lambda (client) (let* ((output (ao-first! client)) (bytes (quic-output-bytes output)) (original (bytevector-copy bytes)) (address (quic-output-address output)) (cid (quic-output-id output))) (bytevector-u8-set! bytes 0 0) (bytevector-u8-set! address 0 0) (bytevector-u8-set! cid 0 0) (assert-true (and (quic-output-valid? client output) (bytes=? original (quic-output-bytes output)) (bytes=? #u8(42) (quic-output-address output)) (bytes=? #u8(10 11 12 13 14 15 16 17) (quic-output-id output)))))))) (test "addressed-output: closing invalidates queued ordinary traffic and freezes close destination" (ao-with-client (lambda (client) (let ((old (ao-first! client))) (quic-close! client) (let ((packets (quic-take-addressed-datagrams! client 0))) (assert-true (and (not (quic-output-valid? client old)) (= 1 (length packets)) (quic-closing? client) (quic-output-valid? client (car packets)) (bytes=? #u8(42) (quic-output-address (car packets))) (bytes=? (quic-output-id old) (quic-output-id (car packets))))) (quic-abort! client) (assert-true (and (not (quic-output-valid? client old)) (not (quic-output-valid? client (car packets))) (null? (quic-take-addressed-datagrams! client 1))))))))))test/test-connection-network.sgladded
(import (sigil test) (sigil quic bytes) (sigil quic wire) (sigil quic frame) (sigil quic error) (sigil quic connection network));; These are explicitly trusted, post-handshake fixtures for the socket-free;; owner. TLS authentication and packet metadata hooks are tested separately.(define (net-local-parameters) (list (cons 0 #u8(10 11 12 13 14 15 16 17)) (cons 15 #u8(1))))(define (net-peer-parameters) (list (cons 15 #u8(2))))(define (net-with-owner procedure) (let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0 (net-local-parameters) (net-peer-parameters)))) (dynamic-wind (lambda () #t) (lambda () (procedure network)) (lambda () (network-close! network)))))(define (net-new sequence retire) (make-quic-frame 24 (list (cons 'sequence sequence) (cons 'retire-prior-to retire) (cons 'connection-id (bytevector (+ sequence 2))) (cons 'token (make-bytevector 16 0)))))(define (net-observe! network address number now) (network-observe! network address 1200 number (list (make-quic-frame 1 '())) now))(define (net-retiring network) (map (lambda (frame) (frame-ref frame 'sequence)) (network-retirements network)))(define (net-destination-is? network address sequence cid) (let ((destination (network-destination network address))) (and destination (= sequence (vector-ref destination 0)) (bytes=? cid (vector-ref destination 1)))))(define (net-retirement-error? procedure) (guard (exn (else (and (quic-error? exn) (= 10 (quic-error-code exn)) (= 25 (quic-error-frame-type exn))))) (procedure) #f))(define (net-binding-error? procedure) (guard (exn (else (and (quic-error? exn) (= 10 (quic-error-code exn)) (= 24 (quic-error-frame-type exn))))) (procedure) #f))(test-group "Confirmed connection CID and path ownership" (test "connection-network: initial path owns peer sequence zero and copied destination metadata" (net-with-owner (lambda (network) (let ((destination (network-destination network #u8(10)))) (assert-true (and destination (= 0 (vector-ref destination 0)) (bytes=? #u8(2) (vector-ref destination 1)) (= 0 (network-route network #u8(1))) (not (network-destination network #u8(11))))) (bytevector-u8-set! (vector-ref destination 1) 0 9) (assert-true (bytes=? #u8(2) (vector-ref (network-destination network #u8(10)) 1))))))) (test "connection-network: local advertisements route only after commit and retire against actual packet DCID" (net-with-owner (lambda (network) (let ((frame (network-issue-id! network #u8(4) (make-bytevector 16 0)))) (assert-true (and frame (not (network-route network #u8(4))) (net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(1)))) (network-check-id! network frame) (network-commit-id! network frame) (= 1 (network-route network #u8(4))) (net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(4)))) (network-retire-local-id! network 0 #u8(4)) (not (network-route network #u8(1))) (network-retire-local-id! network 1 #u8(1)) (not (network-check-id! network frame)) (not (network-commit-id! network frame)) (not (network-route network #u8(4))))))))) (test "connection-network: missing peer spare pauses probes without charging a candidate" (net-with-owner (lambda (network) (net-observe! network #u8(11) 1 0) (assert-true (and (not (network-challenge! network #u8(11) 0)) (not (network-reserve-send! network #u8(11) 1 0)) (bytes=? #u8(10) (network-address network)) (network-peer-id! network (net-new 1 0)) (let ((probe (network-challenge! network #u8(11) 0))) (and probe (= 1 (vector-ref probe 1)) (bytes=? #u8(3) (vector-ref probe 2)) (bytes=? #u8(10) (network-address network))))))))) (test "connection-network: validated migration retains the old CID until its independent probe finishes" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (net-observe! network #u8(11) 1 0) (let ((old (network-challenge! network #u8(10) 0)) (next (network-challenge! network #u8(11) 0))) (assert-true (and old next (network-response! network (vector-ref next 3) 1) (bytes=? #u8(11) (network-address network)) (net-destination-is? network #u8(11) 1 #u8(3)) (net-destination-is? network #u8(10) 0 #u8(2)) (null? (net-retiring network)) (network-response! network (vector-ref old 3) 1) (not (network-destination network #u8(10))) (equal? '(0) (net-retiring network)) (equal? '(0) (net-retiring network)) (network-retirement-ack! network 0) (null? (net-retiring network)))))))) (test "connection-network: candidate timeout retires its CID and preserves the working path" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (net-observe! network #u8(11) 1 0) (network-tick! network 3000) (assert-true (and (not (network-candidate network)) (not (network-destination network #u8(11))) (equal? '(1) (net-retiring network)) (net-destination-is? network #u8(10) 0 #u8(2)) (network-peer-id! network (net-new 2 0)) (net-observe! network #u8(12) 2 3001) (let ((probe (network-challenge! network #u8(12) 3001))) (and probe (= 2 (vector-ref probe 1)) (bytes=? #u8(4) (vector-ref probe 2))))))))) (test "connection-network: superseded candidate CID is retired rather than reassigned" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (net-observe! network #u8(11) 1 0) (net-observe! network #u8(12) 2 1) (assert-true (and (not (network-destination network #u8(11))) (not (network-destination network #u8(12))) (equal? '(1) (net-retiring network)) (network-peer-id! network (net-new 2 0)) (net-destination-is? network #u8(12) 2 #u8(4)) (net-destination-is? network #u8(10) 0 #u8(2))))))) (test "connection-network: peer RetirePriorTo replaces the current CID before further output" (net-with-owner (lambda (network) (assert-true (and (network-peer-id! network (net-new 1 1)) (net-destination-is? network #u8(10) 1 #u8(3)) (equal? '(0) (net-retiring network)) (bytes=? #u8(10) (network-address network)) (null? (network-take-events! network))))))) (test "connection-network: only non-probing decoded frames authorize candidate validation" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (network-observe! network #u8(11) 1200 10 (list (net-new 1 0)) 0) (assert-true (and (not (network-challenge! network #u8(11) 0)) (bytes=? #u8(10) (network-address network)) (net-observe! network #u8(11) 1 1) (network-challenge! network #u8(11) 1)))))) (test "connection-network: constructor rejects mismatched IDs and unsupported local advertisements" (assert-error (make-connection-network #u8(1) #u8(9) 'server #u8(10) 0 (net-local-parameters) (net-peer-parameters))) (assert-error (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0 (cons (cons 14 (encode-varint 9)) (net-local-parameters)) (net-peer-parameters))) (let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0 (net-local-parameters) (cons (cons 14 (encode-varint 100)) (net-peer-parameters))))) (dynamic-wind (lambda () #t) (lambda () (assert-equal 8 (network-local-limit network))) (lambda () (network-close! network))))) (test "connection-network: per-path reservations cannot borrow original path credit" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (network-observe! network #u8(11) 10 1 (list (make-quic-frame 1 '())) 0) (assert-true (and (network-reserve-send! network #u8(10) 1200 0) (network-reserve-send! network #u8(11) 30 0) (not (network-reserve-send! network #u8(11) 1 0)) (not (network-challenge! network #u8(11) 0)) (bytes=? #u8(10) (network-address network))))))) (test "connection-network: preferred metadata installs owned sequence one on both directions" (let* ((preferred (make-bytevector 42 0)) (server #f) (client #f)) (bytevector-u8-set! preferred 24 1) (bytevector-u8-set! preferred 25 99) (dynamic-wind (lambda () #t) (lambda () (set! server (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0 (cons (cons 13 preferred) (net-local-parameters)) (net-peer-parameters))) (set! client (make-connection-network #u8(2) #u8(1) 'client #u8(10) 0 (net-peer-parameters) (cons (cons 13 preferred) (net-local-parameters)))) (bytevector-u8-set! preferred 25 88) (assert-true (and (= 1 (network-route server #u8(99))) (= 2 (network-local-count server)) (net-binding-error? (lambda () (network-peer-id! client (make-quic-frame 24 (list (cons 'sequence 1) (cons 'retire-prior-to 0) (cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 1))))))) (network-peer-id! client (make-quic-frame 24 (list (cons 'sequence 1) (cons 'retire-prior-to 0) (cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 0))))) (net-destination-is? client #u8(10) 0 #u8(1))))) (lambda () (when server (network-close! server)) (when client (network-close! client)))))) (test "connection-network: close clears path CID and control ownership and rejects mutations" (net-with-owner (lambda (network) (network-peer-id! network (net-new 1 0)) (net-observe! network #u8(11) 1 0) (network-close! network) (network-close! network) (assert-false (network-address network)) (assert-false (network-candidate network)) (assert-false (network-destination network #u8(10))) (assert-false (network-route network #u8(1))) (assert-equal '() (network-retirements network)) (assert-equal '() (network-route-lengths network)) (assert-equal 0 (network-local-count network)) (assert-error (network-peer-id! network (net-new 2 0))) (assert-error (net-observe! network #u8(12) 2 1)) (assert-error (network-issue-id! network #u8(4) (make-bytevector 16 0)))))))test/test-endpoint-network.sgladded
(import (sigil test) (sigil io) (sigil socket) (sigil time) (sigil crypto) (sigil quic wire) (sigil quic bytes) (sigil quic frame) (sigil quic packet) (sigil quic connection) (sigil quic stream) (sigil quic endpoint))(define en-original #u8(1 2 3 4 5 6 7 8))(define en-client-id #u8(11 12 13 14 15 16 17 18));; Public UDP fixture: client, endpoint, primary/alternate sockets, client;; stream, returned server/stream, factory calls/IDs, clock origin, signing key.;; All sockets and candidate connections remain owned even on assertion errors.(define (en-with-pair procedure (keys: (retry? #t) (pin? #f) (defer? #f) (lifetime 10000) (bad-factory? #f))) (let ((key (ecdsa-p256-generate-keypair)) (client #f) (endpoint #f) (primary #f) (alternate #f) (pair #f)) (dynamic-wind (lambda () #t) (lambda () (set! client (make-quic-client (list #u8(113 49)) "example.test" (append (stream-transport-parameters 'client) (list (cons 32 (encode-varint 1200)))) (lambda (certificates name) (and (equal? certificates (list #u8(48 1 0))) (cdr key))) destination-id: en-original local-id: en-client-id defer-key-share?: defer?)) (set! endpoint (open-quic-endpoint)) (set! primary (udp-socket)) (set! alternate (udp-socket)) (for-each (lambda (socket) (unless (and socket (udp-bind socket 0 "127.0.0.1") (socket-set-non-blocking! socket #t)) (error "endpoint Retry fixture: socket setup failed"))) (list primary alternate)) (set! pair (vector client endpoint primary alternate (make-quic-stream client) #f #f 0 #f (current-jiffy) key #f 0)) (let ((factory (lambda (original source retry) (vector-set! pair 7 (+ 1 (vector-ref pair 7))) (vector-set! pair 8 (list (bytevector-copy original) (bytevector-copy source) (and retry (bytevector-copy retry)))) (let ((server (make-quic-server (list #u8(113 49)) (append (stream-transport-parameters 'server) (list (cons 32 (encode-varint 1200)))) (list #u8(48 1 0)) (car key) original source retry-source-id: (and (not bad-factory?) retry)))) (vector-set! pair 5 server) (vector-set! pair 6 (make-quic-stream server)) server)))) (if retry? (quic-endpoint-accept-retry! endpoint factory peer-port: (and pin? (cadr (socket-local-address primary))) token-lifetime: lifetime) (quic-endpoint-accept! endpoint (lambda (original source) (factory original source #f))))) (quic-bind-peer-address! client (en-address (quic-endpoint-port endpoint))) (quic-start! client 0) (procedure pair)) (lambda () (when primary (socket-close primary)) (when alternate (socket-close alternate)) (when endpoint (quic-endpoint-close! endpoint)) (when client (quic-abort! client)) (when (and pair (vector-ref pair 5)) (quic-abort! (vector-ref pair 5))) (do ((index 0 (+ index 1))) ((= index (bytevector-length (car key)))) (bytevector-u8-set! (car key) index 0))))));; Fields 11/12: active alternate socket?, received alternate datagram count.(define (en-address port) (bytevector 127 0 0 1 (quotient port 256) (modulo port 256)))(define (en-now pair) (quotient (* 1000 (- (current-jiffy) (vector-ref pair 9))) (jiffies-per-second)))(define (en-step! pair) (let ((client (vector-ref pair 0)) (endpoint (vector-ref pair 1)) (socket (vector-ref pair (if (vector-ref pair 11) 3 2)))) (quic-tick! client (en-now pair)) (for-each (lambda (output) (when (quic-output-valid? client output) (let* ((bytes (quic-output-bytes output)) (sent (udp-send socket bytes "127.0.0.1" (quic-endpoint-port endpoint)))) (unless (and sent (= sent (bytevector-length bytes))) (error "endpoint network fixture: UDP send failed"))))) (quic-take-addressed-datagrams! client (en-now pair))) (quic-endpoint-step! endpoint wait-ms: 1) ;; The old port is unreachable to this simulated NAT-rebound client; ;; its socket remains owned only for deterministic cleanup. (let drain ((count 0)) (when (< count 64) (let ((received (udp-receive socket 65528))) (when received (unless (and (equal? (cadr received) "127.0.0.1") (= (caddr received) (quic-endpoint-port endpoint))) (error "endpoint network fixture: unexpected UDP source")) (when (vector-ref pair 11) (vector-set! pair 12 (+ 1 (vector-ref pair 12)))) (quic-receive! client (string->utf8 (car received)) (en-now pair) address: (en-address (quic-endpoint-port endpoint))) (drain (+ count 1))))))))(define (en-await! pair predicate) (let loop ((round 0)) (cond ((predicate) #t) ((= round 2000) #f) (else (en-step! pair) (loop (+ round 1))))))(define (en-ready! pair) (unless (en-await! pair (lambda () (and (quic-confirmed? (vector-ref pair 0)) (vector-ref pair 5) (quic-confirmed? (vector-ref pair 5))))) (error "endpoint network fixture: handshake precondition failed")) ;; Deliver and ACK connection-owned spare IDs before rebinding. (do ((round 0 (+ round 1))) ((= round 12)) (en-step! pair)))(test-group "Addressed loopback UDP endpoint" (test "endpoint-network: confirmed endpoint exposes an owned peer address and drains path events" (en-with-pair (lambda (pair) (en-ready! pair) (let* ((endpoint (vector-ref pair 1)) (expected (en-address (cadr (socket-local-address (vector-ref pair 2))))) (address (quic-endpoint-peer-address endpoint))) (assert-true (bytes=? expected address)) (bytevector-u8-set! address 0 0) (assert-true (bytes=? expected (quic-endpoint-peer-address endpoint))) (assert-true (null? (quic-endpoint-take-path-events! endpoint))))))) (test "endpoint-network: actual client port rebind preserves stream and DATAGRAM traffic" (en-with-pair (lambda (pair) (en-ready! pair) (let* ((endpoint (vector-ref pair 1)) (client (vector-ref pair 0)) (server (vector-ref pair 5)) (old (quic-endpoint-peer-address endpoint)) (next (en-address (cadr (socket-local-address (vector-ref pair 3))))) (request #u8(0 255 1)) (response #u8(2 0 254)) (request-read #u8()) (response-read #u8()) (request-datagram #f) (response-datagram #f)) (assert-false (bytes=? old next)) (vector-set! pair 11 #t) ;; PING selects the candidate; a separate bounded padding packet ;; supplies actual UDP credit for its1200-byte validation probe. (quic-send-frame! client (make-quic-frame 1 '())) (quic-send-frame! client (make-quic-frame 0 (list (cons 'length 400)))) (assert-true (and (en-await! pair (lambda () (bytes=? next (quic-endpoint-peer-address endpoint)))) (= 3 (quic-stream-write! (vector-ref pair 4) request)) (quic-send-datagram! client #u8(7 0 255)) (en-await! pair (lambda () (set! request-read (bytes-append request-read (quic-stream-read! (vector-ref pair 6)))) (unless request-datagram (set! request-datagram (quic-read-datagram! server))) (and (bytes=? request request-read) request-datagram))) (= 3 (quic-stream-write! (vector-ref pair 6) response)) (quic-send-datagram! server #u8(8 255 0)) (en-await! pair (lambda () (set! response-read (bytes-append response-read (quic-stream-read! (vector-ref pair 4)))) (unless response-datagram (set! response-datagram (quic-read-datagram! client))) (and (bytes=? response response-read) response-datagram))) (let ((events (quic-endpoint-take-path-events! endpoint))) (and (bytes=? #u8(7 0 255) request-datagram) (bytes=? #u8(8 255 0) response-datagram) (> (vector-ref pair 12) 0) (= 1 (vector-ref pair 7)) (not (quic-closed? client)) (not (quic-closed? server)) (any (lambda (event) (and (eq? (vector-ref event 0) 'path-changed) (bytes=? next (vector-ref event 1)))) events) (any (lambda (event) (and (eq? (vector-ref event 0) 'path-validated) (bytes=? next (vector-ref event 1)))) events) (null? (quic-endpoint-take-path-events! endpoint)))))))))))test/test-local-ids.sgladded
(import (sigil test) (sigil quic bytes) (sigil quic wire) (sigil quic frame) (sigil quic error) (sigil quic connection ids))(define (lic-with-pool procedure) (let ((pool (make-local-id-pool #u8(1)))) (dynamic-wind (lambda () #t) (lambda () (procedure pool)) (lambda () (local-id-pool-close! pool)))))(define (lic-protocol-error? procedure) (guard (exn (else (and (quic-error? exn) (= 10 (quic-error-code exn)) (= 25 (quic-error-frame-type exn))))) (procedure) #f))(define (lic-spare! pool) (local-id-pool-reserve! pool #u8(2 3) (make-bytevector 16 0)))(define (lic-frame sequence cid token) (make-quic-frame 24 (list (cons 'sequence sequence) (cons 'retire-prior-to 0) (cons 'connection-id cid) (cons 'token token))))(define (lic-commit-spare! pool) (local-id-pool-commit! pool (lic-frame 1 #u8(2 3) (make-bytevector 16 0))))(test-group "Locally issued CID ownership and retirement" (test "local-ids: invalid limits and preferred metadata refuse construction" (for-each (lambda (procedure) (assert-error (procedure))) (list (lambda () (make-local-id-pool (make-bytevector 21 0))) (lambda () (make-local-id-pool #u8(1) limit: 1)) (lambda () (make-local-id-pool #u8(1) limit: 9)) (lambda () (make-local-id-pool #u8(1) maximum: 1)) (lambda () (make-local-id-pool #u8(1) maximum: 257)) (lambda () (make-local-id-pool #u8(1) preferred: (cons #u8(2) #u8(0)))) (lambda () (make-local-id-pool #u8() preferred: (cons #u8(2) (make-bytevector 16 0)))) (lambda () (make-local-id-pool #u8(1) preferred: (cons #u8(1 2) (make-bytevector 16 0))))))) (test "local-ids: initial sequence zero routes and unknown IDs do not" (lic-with-pool (lambda (pool) (assert-equal 0 (local-id-pool-route pool #u8(1))) (assert-false (local-id-pool-route pool #u8(2))) (assert-equal '(1) (local-id-pool-lengths pool)) (assert-equal 1 (local-id-pool-count pool))))) (test "local-ids: a reservation consumes capacity but cannot route or retire" (lic-with-pool (lambda (pool) (let ((frame (lic-spare! pool))) (assert-true (and (= 24 (quic-frame-type frame)) (= 1 (frame-ref frame 'sequence)) (= 0 (frame-ref frame 'retire-prior-to)) (not (local-id-pool-route pool #u8(2 3))) (lic-protocol-error? (lambda () (local-id-pool-retire! pool 1 #u8(1)))) (= 2 (local-id-pool-count pool)) (not (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 1))))))))) (test "local-ids: repeated packet commits advertise one routable sequence" (lic-with-pool (lambda (pool) (lic-spare! pool) (assert-true (and (lic-commit-spare! pool) (lic-commit-spare! pool) (= 1 (local-id-pool-route pool #u8(2 3))) (= 2 (local-id-pool-count pool)) (memv 1 (local-id-pool-lengths pool)) (memv 2 (local-id-pool-lengths pool))))))) (test "local-ids: caller and queued-frame mutations do not change routing ownership" (lic-with-pool (lambda (pool) (let* ((cid (make-bytevector 2 2)) (token (make-bytevector 16 0)) (frame (local-id-pool-reserve! pool cid token))) (bytevector-u8-set! cid 0 3) (bytevector-u8-set! token 0 5) (assert-equal 0 (bytevector-u8-ref (frame-ref frame 'token) 0)) (bytevector-u8-set! (frame-ref frame 'connection-id) 0 4) (assert-error (local-id-pool-check! pool frame)) (local-id-pool-commit! pool (lic-frame 1 #u8(2 2) (make-bytevector 16 0))) (assert-equal 1 (local-id-pool-route pool #u8(2 2))) (assert-false (local-id-pool-route pool cid)) (let ((copy (local-id-pool-id pool 1))) (bytevector-u8-set! copy 0 6) (assert-true (bytes=? #u8(2 2) (local-id-pool-id pool 1)))))))) (test "local-ids: retirement above an emitted sequence is a typed violation" (lic-with-pool (lambda (pool) (lic-spare! pool) (lic-commit-spare! pool) (assert-true (and (lic-protocol-error? (lambda () (local-id-pool-retire! pool 2 #u8(1)))) (lic-protocol-error? (lambda () (local-id-pool-retire! pool quic-varint-maximum #u8(1)))) (= 2 (local-id-pool-count pool))))))) (test "local-ids: a retirement cannot name its own packet destination ID" (lic-with-pool (lambda (pool) (lic-spare! pool) (lic-commit-spare! pool) (assert-true (and (lic-protocol-error? (lambda () (local-id-pool-retire! pool 1 #u8(2 3)))) (= 1 (local-id-pool-route pool #u8(2 3))) (= 2 (local-id-pool-count pool))))))) (test "local-ids: duplicate retirement is idempotent and replacement sequence increases" (lic-with-pool (lambda (pool) (lic-spare! pool) (lic-commit-spare! pool) (assert-true (and (local-id-pool-retire! pool 0 #u8(2 3)) (not (local-id-pool-retire! pool 0 #u8(2 3))) (not (local-id-pool-route pool #u8(1))) (= 1 (local-id-pool-count pool)) (let ((frame (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 1)))) (and (= 2 (frame-ref frame 'sequence)) (local-id-pool-commit! pool frame) (= 2 (local-id-pool-route pool #u8(4))) (= 1 (local-id-pool-route pool #u8(2 3)))))))))) (test "local-ids: repeated and overlapping CID prefixes fail before reservation" (lic-with-pool (lambda (pool) (assert-error (local-id-pool-reserve! pool #u8(1) (make-bytevector 16 0))) (assert-error (local-id-pool-reserve! pool #u8(1 2) (make-bytevector 16 0))) (assert-equal 1 (local-id-pool-count pool)) (assert-equal 1 (frame-ref (lic-spare! pool) 'sequence))))) (test "local-ids: lifetime exhaustion preserves every remaining live ID" (let ((pool (make-local-id-pool #u8(1) maximum: 2))) (dynamic-wind (lambda () #t) (lambda () (lic-spare! pool) (lic-commit-spare! pool) (local-id-pool-retire! pool 0 #u8(2 3)) (assert-false (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 0))) (assert-equal 1 (local-id-pool-route pool #u8(2 3))) (assert-equal 1 (local-id-pool-count pool))) (lambda () (local-id-pool-close! pool))))) (test "local-ids: zero-length source IDs never issue spares or allow retirement" (let ((pool (make-local-id-pool #u8()))) (dynamic-wind (lambda () #t) (lambda () (assert-equal 0 (local-id-pool-route pool #u8())) (assert-false (lic-spare! pool)) (assert-true (lic-protocol-error? (lambda () (local-id-pool-retire! pool 0 #u8()))))) (lambda () (local-id-pool-close! pool))))) (test "local-ids: authenticated preferred-address CID occupies sequence one" (let ((pool (make-local-id-pool #u8(1) preferred: (cons #u8(2) (make-bytevector 16 0))))) (dynamic-wind (lambda () #t) (lambda () (assert-equal 1 (local-id-pool-route pool #u8(2))) (assert-equal 2 (local-id-pool-count pool)) (local-id-pool-retire! pool 1 #u8(1)) (assert-equal 2 (frame-ref (local-id-pool-reserve! pool #u8(4) (make-bytevector 16 0)) 'sequence))) (lambda () (local-id-pool-close! pool))))) (test "local-ids: initial and returned IDs are owned copies" (let* ((initial (make-bytevector 1 1)) (pool (make-local-id-pool initial))) (dynamic-wind (lambda () #t) (lambda () (bytevector-u8-set! initial 0 3) (let ((copy (local-id-pool-id pool 0))) (bytevector-u8-set! copy 0 4) (assert-true (bytes=? #u8(1) (local-id-pool-id pool 0))))) (lambda () (local-id-pool-close! pool))))) (test "local-ids: close clears routes and refuses further ownership mutations" (lic-with-pool (lambda (pool) (lic-spare! pool) (local-id-pool-close! pool) (local-id-pool-close! pool) (assert-false (local-id-pool-route pool #u8(1))) (assert-false (local-id-pool-id pool 0)) (assert-equal '() (local-id-pool-lengths pool)) (assert-equal 0 (local-id-pool-count pool)) (assert-error (lic-spare! pool)) (assert-error (lic-commit-spare! pool)) (assert-error (local-id-pool-retire! pool 0 #u8(2 3)))))) (test "local-ids: a retired advertisement cannot revive a route" (lic-with-pool (lambda (pool) (let ((frame (lic-spare! pool))) (local-id-pool-commit! pool frame) (local-id-pool-retire! pool 1 #u8(1)) (assert-true (and (not (local-id-pool-check! pool frame)) (not (local-id-pool-commit! pool frame)) (not (local-id-pool-route pool #u8(2 3))) (= 1 (local-id-pool-count pool)))))))) (test "local-ids: wire metadata must match the reserved sequence CID and token" (lic-with-pool (lambda (pool) (let ((frame (lic-spare! pool))) (assert-error (local-id-pool-check! pool (make-quic-frame 1 '()))) (assert-error (local-id-pool-check! pool (lic-frame 2 #u8(2 3) (make-bytevector 16 0)))) (assert-error (local-id-pool-check! pool (lic-frame 1 #u8(4) (make-bytevector 16 0)))) (assert-error (local-id-pool-check! pool (lic-frame 1 #u8(2 3) (make-bytevector 16 1)))) (assert-error (local-id-pool-check! pool (make-quic-frame 24 (list (cons 'sequence 1) (cons 'retire-prior-to 1) (cons 'connection-id #u8(2 3)) (cons 'token (make-bytevector 16 0)))))) (assert-true (and (not (local-id-pool-route pool #u8(2 3))) (local-id-pool-check! pool frame) (local-id-pool-commit! pool frame) (= 1 (local-id-pool-route pool #u8(2 3))))))))))test/test-network-confirmation.sgladded
(import (sigil test) (sigil quic bytes) (sigil quic frame) (sigil quic connection network))(define (nc-with-passive procedure) (let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0 (list (cons 0 #u8(10 11 12 13 14 15 16 17)) (cons 15 #u8(1))) (list (cons 15 #u8(2))) confirmed?: #f))) (dynamic-wind (lambda () #t) (lambda () (procedure network)) (lambda () (network-close! network)))))(define (nc-advertisement) (make-quic-frame 24 (list (cons 'sequence 1) (cons 'retire-prior-to 0) (cons 'connection-id #u8(3)) (cons 'token (make-bytevector 16 0)))))(test-group "Passive CID ownership before handshake confirmation" (test "network-confirmation: early CID metadata is retained while every path operation is refused" (nc-with-passive (lambda (network) (assert-true (network-peer-id! network (nc-advertisement))) (for-each (lambda (procedure) (assert-error (procedure))) (list (lambda () (network-observe! network #u8(11) 1200 1 (list (make-quic-frame 1 '())) 0)) (lambda () (network-challenge! network #u8(11) 0)) (lambda () (network-reserve-send! network #u8(10) 1200 0)) (lambda () (network-response! network (make-bytevector 8 0) 0)) (lambda () (network-tick! network 0)))) (assert-true (and (bytes=? #u8(10) (network-address network)) (not (network-candidate network)) (not (network-destination network #u8(11))) (null? (network-take-events! network))))))) (test "network-confirmation: confirmation seeds ordering once and preserves early CID ownership" (nc-with-passive (lambda (network) (network-peer-id! network (nc-advertisement)) (network-confirm! network 100 10) (assert-true (and (not (network-observe! network #u8(11) 1200 9 (list (make-quic-frame 1 '())) 100)) (network-observe! network #u8(11) 1200 11 (list (make-quic-frame 1 '())) 100) (let ((probe (network-challenge! network #u8(11) 100))) (and probe (= 1 (vector-ref probe 1)) (bytes=? #u8(3) (vector-ref probe 2)) (network-response! network (vector-ref probe 3) 101) (network-confirm! network 102 0) (bytes=? #u8(11) (network-address network)) (not (network-observe! network #u8(12) 1200 10 (list (make-quic-frame 1 '())) 102))))))))) (test "network-confirmation: close-route snapshots are owned and exclude uncommitted or retired IDs" (nc-with-passive (lambda (network) (let ((frame (network-issue-id! network #u8(4) (make-bytevector 16 0)))) (assert-false (network-local-id network 1)) (network-commit-id! network frame) (let ((copy (network-local-id network 1))) (bytevector-u8-set! copy 0 5)) (assert-true (and (bytes=? #u8(4) (network-local-id network 1)) (network-retire-local-id! network 1 #u8(1)) (not (network-local-id network 1)) (bytes=? #u8(1) (network-local-id network 0)))))))))test/test-network-receive.sgladded
(import (sigil test) (sigil crypto) (sigil quic bytes) (sigil quic wire) (sigil quic frame) (sigil quic packet) (sigil quic reassembly) (sigil quic connection) (sigil quic stream) (sigil quic tls));; A public TLS peer owns its keys. The target connection exposes no secrets.;; Fields: connection/TLS, read/write keys, peer PNs/target largest PNs,;; CRYPTO reassemblers/offsets, observed output/handler frames, time, peer role,;; HANDSHAKE_DONE flag, target CID and peer CID.(define nr-original #u8(1 2 3 4 5 6 7 8))(define nr-client-id #u8(11 12 13 14 15 16 17 18))(define nr-server-id #u8(21 22 23 24 25 26 27 28))(define nr-data #u8(0 1 2 255))(define nr-path-data #u8(0 1 0 3 4 0 6 255))(define (nr-index level) (case level ((initial) 0) ((handshake) 1) ((application) 2)))(define (nr-wipe! bytes) (when (bytevector? bytes) (do ((i 0 (+ i 1))) ((= i (bytevector-length bytes))) (bytevector-u8-set! bytes i 0))))(define (nr-build! peer level frames) (let* ((index (nr-index level)) (numbers (vector-ref peer 4)) (number (vector-ref numbers index)) (role (vector-ref peer 11)) (destination (if (and (eq? role 'client) (eq? level 'initial) (= -1 (vector-ref (vector-ref peer 5) 0))) nr-original (vector-ref peer 13))) (source (if (eq? level 'application) #u8() (vector-ref peer 14))) (body (encode-frames frames level role)) (header (encode-packet-header level destination source #u8() number 4 (+ 16 (bytevector-length body)) length-width: (and (not (eq? level 'application)) 2))) (payload (if (eq? level 'initial) (bytes-append body (make-bytevector (- 1200 16 (bytevector-length header) (bytevector-length body)) 0)) body))) (vector-set! numbers index (+ number 1)) (cons number (seal-quic-packet (vector-ref (vector-ref peer 3) index) level destination source number payload length-width: (and (not (eq? level 'application)) 2)))))(define (nr-receive! peer packet now) (vector-set! peer 10 now) (quic-receive! (vector-ref peer 0) packet now address: #u8(42)))(define (nr-send! peer level frames) (let ((packet (nr-build! peer level frames))) (nr-receive! peer (cdr packet) (vector-ref peer 10)) packet))(define (nr-events! peer) (for-each (lambda (event) (let* ((level (tls-event-level event)) (index (nr-index level)) (data (tls-event-data event))) (case (tls-event-kind event) ((secrets) (when (car data) (vector-set! (vector-ref peer 2) index (traffic-keys (car data))) (nr-wipe! (car data))) (when (cdr data) (vector-set! (vector-ref peer 3) index (traffic-keys (cdr data))) (nr-wipe! (cdr data)))) ((crypto) (let* ((offsets (vector-ref peer 7)) (offset (vector-ref offsets index))) (nr-send! peer level (list (make-quic-frame 6 (list (cons 'offset offset) (cons 'data data))))) (vector-set! offsets index (+ offset (bytevector-length data)))))))) (tls-take-events! (vector-ref peer 1))) (when (and (eq? (vector-ref peer 11) 'server) (tls-complete? (vector-ref peer 1)) (not (vector-ref peer 12))) (vector-set! peer 12 #t) (nr-send! peer 'application (list (make-quic-frame 30 '())))))(define (nr-read! peer packet ack?) (let* ((header (decode-packet-header packet 0 8)) (level (quic-header-kind header)) (index (nr-index level)) (largest (vector-ref peer 5)) (opened (open-quic-packet packet 0 8 (vector-ref (vector-ref peer 2) index) (vector-ref largest index)))) (unless opened (error "Addressed receive fixture target packet did not authenticate")) (let* ((number (opened-quic-packet-number opened)) (frames (decode-frames (opened-quic-packet-payload opened) level (quic-role (vector-ref peer 0))))) (vector-set! largest index (max number (vector-ref largest index))) (when (eq? level 'application) (vector-set! peer 8 (cons (cons number frames) (vector-ref peer 8)))) (for-each (lambda (frame) (when (= (quic-frame-type frame) 6) (let ((receiver (vector-ref (vector-ref peer 6) index))) (reassembler-insert! receiver (frame-ref frame 'offset) (frame-ref frame 'data)) (let ((bytes (reassembler-read! receiver))) (when (> (bytevector-length bytes) 0) (tls-input! (vector-ref peer 1) level bytes) ;; Install client-side Handshake keys before opening the ;; next packet in the server's already returned flight. (nr-events! peer)))))) frames) (when (and ack? (any frame-ack-eliciting? frames)) (nr-send! peer level (list (make-quic-frame 2 (list (cons 'ranges (list (cons number number))) (cons 'delay 0)))))))))(define (nr-output! peer now (keys: (ack? #t))) (vector-set! peer 10 now) (let ((packets (map quic-output-bytes (quic-take-addressed-datagrams! (vector-ref peer 0) now)))) (for-each (lambda (packet) (nr-read! peer packet ack?)) packets) packets))(define (nr-live? peer) (let ((connection (vector-ref peer 0))) (and (quic-confirmed? connection) (not (quic-closed? connection)) (not (quic-failure connection)))))(define (nr-with-peer role procedure (keys: (confirm? #t))) (let ((key (ecdsa-p256-generate-keypair)) (connection #f) (engine #f) (peer #f)) (dynamic-wind (lambda () #t) (lambda () (let* ((client? (eq? role 'client)) (peer-role (if client? 'server 'client)) (target-id (if client? nr-client-id nr-server-id)) (peer-id (if client? nr-server-id nr-client-id)) (parameters (append (stream-transport-parameters peer-role receive-window: 64) (list (cons 1 (encode-varint 30000)) (cons 15 peer-id)) (if client? (list (cons 0 nr-original)) '())))) (set! connection (if client? (make-quic-client (list #u8(113 49)) 'pinned-key (stream-transport-parameters role receive-window: 64) (make-tls-pin-verifier (cdr key)) local-id: target-id destination-id: nr-original) (make-quic-server (list #u8(113 49)) (stream-transport-parameters role receive-window: 64) (list #u8(48 1 0)) (car key) nr-original peer-id local-id: target-id))) (set! engine (if client? (make-tls-server (list #u8(113 49)) parameters (list #u8(48 1 0)) (car key)) (make-tls-client (list #u8(113 49)) 'pinned-key parameters (make-tls-pin-verifier (cdr key))))) (set! peer (vector connection engine (vector (initial-keys nr-original role) #f #f) (vector (initial-keys nr-original peer-role) #f #f) (vector 0 0 0) (vector -1 -1 -1) (vector (make-byte-reassembler 65536) (make-byte-reassembler 65536) (make-byte-reassembler 65536)) (vector 0 0 0) '() '() 0 peer-role (not confirm?) target-id peer-id)) (quic-bind-peer-address! connection #u8(42)) (tls-bind-peer! engine nr-original target-id #f) (if client? (quic-start! connection 0) (begin (tls-start! engine) (nr-events! peer))) (do ((round 0 (+ round 1))) ((= round 12)) (nr-output! peer 0) (nr-events! peer)) (unless (and (quic-complete? connection) (not (quic-closed? connection)) (eq? confirm? (quic-confirmed? connection)) (tls-complete? engine) (vector-ref (vector-ref peer 2) 2) (vector-ref (vector-ref peer 3) 2)) (error "Addressed receive fixture protected handshake did not complete")) (procedure peer))) (lambda () (when connection (quic-abort! connection)) (when engine (tls-abort! engine)) (when peer (for-each (lambda (keys) (when keys (discard-packet-keys! keys))) (append (vector->list (vector-ref peer 2)) (vector->list (vector-ref peer 3)))) (for-each reassembler-close! (vector->list (vector-ref peer 6)))) (nr-wipe! (car key))))))(define (nr-from! peer packet address) (quic-receive! (vector-ref peer 0) packet (vector-ref peer 10) address: address))(define (nr-ping! peer) (nr-build! peer 'application (list (make-quic-frame 1 '()))))(define (nr-events? connection kind address) (let ((events (quic-take-path-events! connection))) (and (= 1 (length events)) (eq? kind (vector-ref (car events) 0)) (bytes=? address (vector-ref (car events) 1)))))(define (nr-observed peer type) (filter (lambda (frame) (= type (quic-frame-type frame))) (apply append (map cdr (vector-ref peer 8)))))(define (nr-advertised peer sequence) (find (lambda (frame) (= sequence (frame-ref frame 'sequence))) (nr-observed peer 24)))(define (nr-stream! peer) (cdr (nr-build! peer 'application (list (make-quic-frame 15 (list (cons 'stream-id 0) (cons 'offset 0) (cons 'data #u8(1))))))))(define (nr-give-spare! peer) (nr-send! peer 'application (list (make-quic-frame 24 (list (cons 'sequence 1) (cons 'retire-prior-to 0) (cons 'connection-id #u8(51 52 53 54 55 56 57 58)) (cons 'token (make-bytevector 16 5)))))))(define (nr-addressed! peer now) (vector-set! peer 10 now) (let ((outputs (quic-take-addressed-datagrams! (vector-ref peer 0) now))) (for-each (lambda (output) (nr-read! peer (quic-output-bytes output) #f)) outputs) outputs))(define (nr-output-frames peer output) (let ((opened (open-quic-packet (quic-output-bytes output) 0 8 (vector-ref (vector-ref peer 2) 2) -1))) (unless opened (error "Addressed fixture: application output did not authenticate")) (decode-frames (opened-quic-packet-payload opened) 'application (quic-role (vector-ref peer 0)))))(define (nr-control-output peer outputs address type) (find (lambda (output) (and (bytes=? address (quic-output-address output)) (any (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output)))) outputs))(define (nr-control-token peer output type) (and output (let ((frame (find (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output)))) (and frame (frame-ref frame 'data)))))(define (nr-count-control peer outputs address type) (length (filter (lambda (output) (and (bytes=? address (quic-output-address output)) (any (lambda (frame) (= type (quic-frame-type frame))) (nr-output-frames peer output)))) outputs)))(define (nr-probe-pair? peer outputs) (and (= 1 (nr-count-control peer outputs #u8(42) 26)) (= 1 (nr-count-control peer outputs #u8(43) 26))))(define (nr-path-input! peer type data address) (nr-from! peer (cdr (nr-build! peer 'application (list (make-quic-frame type (list (cons 'data data)))))) address))(define (nr-padded-ping! peer size) (let ((packet (cdr (nr-build! peer 'application (list (make-quic-frame 1 '()) (make-quic-frame 0 (list (cons 'length (- size 30))))))))) (unless (= size (bytevector-length packet)) (error "Addressed fixture: wrong padded wire size")) packet))(define (nr-stream-outputs peer outputs address) (filter (lambda (output) (and (bytes=? address (quic-output-address output)) (any (lambda (frame) (<= 8 (quic-frame-type frame) 15)) (nr-output-frames peer output)))) outputs))(define (nr-output-number peer output) (opened-quic-packet-number (open-quic-packet (quic-output-bytes output) 0 8 (vector-ref (vector-ref peer 2) 2) -1)))(test-group "Authenticated path credit and pending-response bounds" (test "network-path-credit: replay cannot fund another challenge after exact wire credit is spent" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (let ((packet (nr-padded-ping! peer 400))) (nr-from! peer packet #u8(43)) (assert-true (nr-probe-pair? peer (nr-addressed! peer 0))) (nr-from! peer packet #u8(43)) (quic-tick! (vector-ref peer 0) 1000) (assert-true (and (nr-live? peer) (not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 26)))))))) (test "network-path-credit: forged tags and unowned STREAM input cannot refill a candidate budget" (for-each (lambda (kind) (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (assert-true (nr-probe-pair? peer (nr-addressed! peer 0))) (let ((packet (if (eq? kind 'forged) (flip-byte (nr-padded-ping! peer 400) 399) (cdr (nr-build! peer 'application (list (make-quic-frame 15 (list (cons 'stream-id 0) (cons 'offset 0) (cons 'data (make-bytevector 366 1)))))))))) (unless (= 400 (bytevector-length packet)) (error "Credit fixture: wrong wire size")) (nr-from! peer packet #u8(43))) (quic-tick! (vector-ref peer 0) 1000) (assert-true (and (nr-live? peer) (not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 26))))))) '(forged unowned))) (test "network-path-credit: response padding refusal does not consume a packet number" (nr-with-peer 'server (lambda (peer) (let ((before (vector-ref (vector-ref peer 5) 2))) (nr-give-spare! peer) (nr-path-input! peer 26 #u8(0 1 2 3 4 5 6 7) #u8(43)) (let ((response (nr-control-output peer (nr-addressed! peer 0) #u8(43) 27))) (assert-true (and (nr-live? peer) response (= 38 (bytevector-length (quic-output-bytes response))) (= (+ before 1) (nr-output-number peer response))))))))) (test "network-path-credit: pending responses stop at16 and expired requests cannot bind to a later attempt" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (do ((index 0 (+ index 1))) ((= index 17)) (nr-path-input! peer 26 (make-bytevector 8 index) #u8(43))) (let* ((outputs (nr-addressed! peer 0)) (responses (filter (lambda (output) (bytes=? #u8(43) (quic-output-address output))) outputs))) (assert-true (and (nr-live? peer) (= 16 (nr-count-control peer outputs #u8(43) 27)) (= 1770 (apply + (map (lambda (output) (bytevector-length (quic-output-bytes output))) responses))) (<= 1770 (* 3 17 38))))))) (nr-with-peer 'server (lambda (peer) ;; No spare CID: retain requests without sending them on another path. (do ((index 0 (+ index 1))) ((= index 17)) (nr-path-input! peer 26 (make-bytevector 8 index) #u8(43))) (assert-false (nr-control-output peer (nr-addressed! peer 0) #u8(43) 27)) (quic-tick! (vector-ref peer 0) 3000) (vector-set! peer 10 3000) (nr-give-spare! peer) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (let ((outputs (nr-addressed! peer 3000))) (assert-true (and (nr-live? peer) (nr-control-output peer outputs #u8(43) 26) (not (nr-control-output peer outputs #u8(43) 27)))))))))(test-group "Addressed PATH controls and migration accounting" (test "network-path: promotion invalidates old ordinary output but preserves addressed validation controls" (nr-with-peer 'server (lambda (peer) (let ((connection (vector-ref peer 0))) (nr-give-spare! peer) (quic-send-frame! connection (make-quic-frame 1 '())) (let ((old (find (lambda (output) (any (lambda (frame) (= 1 (quic-frame-type frame))) (nr-output-frames peer output))) (nr-addressed! peer 0)))) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (let* ((outputs (nr-addressed! peer 0)) (probe (nr-control-output peer outputs #u8(43) 26)) (previous (nr-control-output peer outputs #u8(42) 26))) (assert-true (and old probe previous (quic-output-valid? connection old) (begin (nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(42)) (and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address connection)) (not (quic-output-valid? connection old)) (quic-output-valid? connection probe) (quic-output-valid? connection previous))))))))))) (test "network-path: promotion requeues reliable flight under fresh PN and ignores old ACK window credit" (nr-with-peer 'server (lambda (peer) (let ((connection (vector-ref peer 0))) (nr-give-spare! peer) (do ((index 0 (+ index 1))) ((= index 16)) (quic-send-frame! connection (make-quic-frame 14 (list (cons 'stream-id 1) (cons 'offset (* index 1100)) (cons 'data (make-bytevector 1100 index)))))) (let* ((old (nr-stream-outputs peer (nr-addressed! peer 0) #u8(42))) (old-numbers (map (lambda (output) (nr-output-number peer output)) old))) (vector-set! peer 10 999) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (let ((probe (nr-control-output peer (nr-addressed! peer 999) #u8(43) 26))) (assert-true (and (pair? old) probe (begin (nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(42)) (let ((fresh (nr-stream-outputs peer (nr-addressed! peer 999) #u8(43)))) (and (nr-live? peer) (= 10 (length fresh)) (every (lambda (output) (> (nr-output-number peer output) (apply max old-numbers))) fresh) (begin ;; The old flight is due; the new flight is1ms old. (quic-tick! connection 1000) (vector-set! peer 10 1000) (nr-from! peer (cdr (nr-build! peer 'application (list (make-quic-frame 2 (list (cons 'ranges (list (cons (apply min old-numbers) (apply max old-numbers)))) (cons 'delay 0)))))) #u8(43)) (and (nr-live? peer) (null? (nr-stream-outputs peer (nr-addressed! peer 1000) #u8(43)))))))))))))))) (test "network-path: response uses challenge source and falls back to its exact threefold budget" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (nr-path-input! peer 26 #u8(1 2 3 4 5 6 7 8) #u8(43)) (let* ((connection (vector-ref peer 0)) (outputs (nr-addressed! peer 0)) (response (nr-control-output peer outputs #u8(43) 27))) (assert-true (and (nr-live? peer) response (= 38 (bytevector-length (quic-output-bytes response))) (bytes=? #u8(1 2 3 4 5 6 7 8) (nr-control-token peer response 27)) (quic-output-valid? connection response) (not (nr-control-output peer outputs #u8(42) 27)) (not (nr-control-output peer outputs #u8(43) 26)) (bytes=? #u8(42) (quic-peer-address connection))))) (quic-tick! (vector-ref peer 0) 1000) (assert-true (not (nr-control-output peer (nr-addressed! peer 1000) #u8(43) 27)))))) (test "network-path: a400-byte authenticated datagram funds one1200-byte challenge without double charging" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (let* ((connection (vector-ref peer 0)) (outputs (nr-addressed! peer 0)) (candidate (nr-control-output peer outputs #u8(43) 26)) (previous (nr-control-output peer outputs #u8(42) 26))) (assert-true (and candidate previous (= 1200 (bytevector-length (quic-output-bytes candidate))) (= 1200 (bytevector-length (quic-output-bytes previous))) (not (bytes=? (quic-output-id candidate) (quic-output-id previous))) (bytes=? #u8(42) (quic-peer-address connection)) (begin (nr-path-input! peer 27 (nr-control-token peer candidate 26) #u8(42)) (and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address connection)) (bytes=? #u8(51 52 53 54 55 56 57 58) (quic-peer-id connection)))))))))) (test "network-path: a matching response arriving on a third path validates its original challenge" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (nr-from! peer (nr-padded-ping! peer 400) #u8(43)) (let* ((outputs (nr-addressed! peer 0)) (probe (nr-control-output peer outputs #u8(43) 26))) (assert-true (and probe (begin (nr-path-input! peer 27 (nr-control-token peer probe 26) #u8(44)) (and (nr-live? peer) (bytes=? #u8(43) (quic-peer-address (vector-ref peer 0))))))))))) (test "network-path: lost probes use fresh PTO-paced payloads and expire without transport retransmission" (nr-with-peer 'server (lambda (peer) (nr-give-spare! peer) (nr-from! peer (nr-padded-ping! peer 1200) #u8(43)) (let* ((first-outputs (nr-addressed! peer 0)) (first (nr-control-output peer first-outputs #u8(43) 26)) (early (nr-control-output peer (nr-addressed! peer 0) #u8(43) 26))) (quic-tick! (vector-ref peer 0) 1000) (let* ((second-outputs (nr-addressed! peer 1000)) (second (nr-control-output peer second-outputs #u8(43) 26))) (quic-tick! (vector-ref peer 0) 2000) (let* ((third-outputs (nr-addressed! peer 2000)) (third (nr-control-output peer third-outputs #u8(43) 26))) (assert-true (and first (not early) second third (nr-probe-pair? peer first-outputs) (nr-probe-pair? peer second-outputs) (nr-probe-pair? peer third-outputs) (not (bytes=? (nr-control-token peer first 26) (nr-control-token peer second 26))) (not (bytes=? (nr-control-token peer second 26) (nr-control-token peer third 26))))) (quic-tick! (vector-ref peer 0) 3000) (assert-true (and (nr-live? peer) (bytes=? #u8(42) (quic-peer-address (vector-ref peer 0))) (not (nr-control-output peer (nr-addressed! peer 3000) #u8(43) 26)) (any (lambda (event) (eq? 'path-failed (vector-ref event 0))) (quic-take-path-events! (vector-ref peer 0))))))))))))(test-group "Connection-owned spare-ID output and retirement" (test "network-cids: one spare is advertised within the peer limit and owns CID control output" (nr-with-peer 'server (lambda (peer) (let* ((connection (vector-ref peer 0)) (spare (nr-advertised peer 1))) (assert-true (and (nr-live? peer) spare (= 1 (length (nr-observed peer 24))) (= 8 (bytevector-length (frame-ref spare 'connection-id))) (= 16 (bytevector-length (frame-ref spare 'token))) (not (bytes=? nr-server-id (frame-ref spare 'connection-id))))) (assert-error (quic-send-frame! connection (make-quic-frame 25 (list (cons 'sequence 0))))) (when spare (assert-error (quic-send-frame! connection spare))) (nr-output! peer 0) (assert-true (and (nr-live? peer) (= 1 (length (nr-observed peer 24))))))))) (test "network-cids: emitted spare routes and retired aliases cannot dispatch application input" (nr-with-peer 'server (lambda (peer) (let* ((connection (vector-ref peer 0)) (spare (nr-advertised peer 1)) (seen 0)) (quic-set-frame-handler! connection (lambda (frame) (set! seen (+ seen 1)))) (assert-true (and spare (begin (vector-set! peer 13 (frame-ref spare 'connection-id)) (nr-from! peer (nr-stream! peer) #u8(42)) (= seen 1)) (begin (vector-set! peer 13 nr-server-id) (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
(import (sigil test) (sigil quic bytes) (sigil quic path manager))(define (pm-with-manager procedure) (let ((manager (make-path-manager #u8(1) 'server 0))) (dynamic-wind (lambda () #t) (lambda () (procedure manager)) (lambda () (path-manager-close! manager)))))(define (pm-candidate! manager) (path-manager-observe! manager #u8(2) 1200 1 #f 0))(define (pm-kind? event kind address reason) (and (eq? kind (vector-ref event 0)) (bytes=? address (vector-ref event 1)) (eq? reason (vector-ref event 2))))(test-group "Authenticated peer path selection" (test "path-manager: construction rejects invalid role baseline and path metadata" (for-each (lambda (procedure) (assert-error (procedure))) (list (lambda () (make-path-manager #u8(1) 'other 0)) (lambda () (make-path-manager #u8() 'server 0)) (lambda () (make-path-manager #u8(1) 'server -1)) (lambda () (make-path-manager #u8(1) 'server 0 highest: -2)) (lambda () (make-path-manager #u8(1) 'server 0 pto: 1))))) (test "path-manager: addresses and drained events cannot mutate owned path keys" (let* ((address (make-bytevector 1 1)) (manager (make-path-manager address 'server 0))) (dynamic-wind (lambda () #t) (lambda () (bytevector-u8-set! address 0 9) (let ((copy (path-manager-address manager))) (bytevector-u8-set! copy 0 8)) (pm-candidate! manager) (let* ((copy (path-manager-candidate manager)) (events (path-manager-take-events! manager))) (bytevector-u8-set! copy 0 7) (bytevector-u8-set! (vector-ref (car events) 1) 0 6) (assert-true (and (bytes=? #u8(1) (path-manager-address manager)) (bytes=? #u8(2) (path-manager-candidate manager)) (bytes=? #u8(1) (path-manager-previous-address manager)))))) (lambda () (path-manager-close! manager))))) (test "path-manager: unvalidated candidate never moves application sends or shares credit" (pm-with-manager (lambda (manager) (path-manager-observe! manager #u8(2) 10 1 #f 0) (assert-true (and (bytes=? #u8(1) (path-manager-address manager)) (path-manager-send! manager #u8(1) 1200 0) (path-manager-send! manager #u8(2) 30 0) (not (path-manager-send! manager #u8(2) 1 0)) (not (path-manager-send! manager #u8(3) 1 0)) (not (path-manager-challenge! manager #u8(2) 0))))))) (test "path-manager: only a matching padded challenge promotes the candidate" (pm-with-manager (lambda (manager) (pm-candidate! manager) (let ((token (path-manager-challenge! manager #u8(2) 0))) (assert-true (and token (bytes=? #u8(1) (path-manager-address manager)) (path-manager-response! manager token 1) (bytes=? #u8(2) (path-manager-address manager)) (not (path-manager-candidate manager)) (not (path-manager-response! manager token 1)) (let ((events (path-manager-take-events! manager))) (and (= 2 (length events)) (pm-kind? (car events) 'path-changed #u8(2) 'non-probing) (pm-kind? (cadr events) 'path-validated #u8(2) 'active))))))))) (test "path-manager: unknown responses cannot promote or extend candidate lifetime" (pm-with-manager (lambda (manager) (pm-candidate! manager) (assert-true (and (not (path-manager-response! manager (make-bytevector 8 0) 2999)) (begin (path-manager-tick! manager 3000) #t) (not (path-manager-candidate manager)) (bytes=? #u8(1) (path-manager-address manager))))))) (test "path-manager: a late correct response cannot revive an expired candidate" (pm-with-manager (lambda (manager) (pm-candidate! manager) (let ((token (path-manager-challenge! manager #u8(2) 0))) (assert-true (and token (not (path-manager-response! manager token 3000)) (not (path-manager-candidate manager)) (bytes=? #u8(1) (path-manager-address manager)))))))) (test "path-manager: probing packets can receive bounded replies but cannot initiate a switch" (pm-with-manager (lambda (manager) (path-manager-observe! manager #u8(2) 400 10 #t 0) (assert-true (and (not (path-manager-challenge! manager #u8(2) 0)) (not (path-manager-previous-address manager)) (path-manager-send! manager #u8(2) 1200 0) (not (path-manager-send! manager #u8(2) 1 0)) (bytes=? #u8(1) (path-manager-address manager)) (path-manager-observe! manager #u8(2) 1200 1 #f 2000) (path-manager-challenge! manager #u8(2) 2000) (bytes=? #u8(1) (path-manager-previous-address manager))))))) (test "path-manager: reordered non-probing packets cannot replace a newer observation" (let ((manager (make-path-manager #u8(1) 'server 0 highest: 100))) (dynamic-wind (lambda () #t) (lambda () (assert-true (and (not (path-manager-observe! manager #u8(2) 1200 99 #f 0)) (not (path-manager-candidate manager)) (path-manager-observe! manager #u8(2) 1200 101 #f 0) (not (path-manager-observe! manager #u8(3) 1200 100 #f 0)) (bytes=? #u8(2) (path-manager-candidate manager))))) (lambda () (path-manager-close! manager))))) (test "path-manager: a newer non-probing packet on the active path cancels spurious migration" (pm-with-manager (lambda (manager) (pm-candidate! manager) (let ((token (path-manager-challenge! manager #u8(2) 0))) (path-manager-observe! manager #u8(1) 100 2 #f 1) (assert-true (and token (not (path-manager-candidate manager)) (not (path-manager-previous-address manager)) (not (path-manager-response! manager token 1)) (bytes=? #u8(1) (path-manager-address manager)))))))) (test "path-manager: clients discard packets from unknown server addresses" (let ((manager (make-path-manager #u8(1) 'client 0))) (dynamic-wind (lambda () #t) (lambda () (assert-true (and (not (pm-candidate! manager)) (not (path-manager-observe! manager #u8(2) 1200 2 #t 0)) (not (path-manager-candidate manager)) (null? (path-manager-take-events! manager)) (bytes=? #u8(1) (path-manager-address manager))))) (lambda () (path-manager-close! manager))))) (test "path-manager: the previously active path gets an independent fresh validation exchange" (pm-with-manager (lambda (manager) (pm-candidate! manager) (let ((old-token (path-manager-challenge! manager #u8(1) 0)) (new-token (path-manager-challenge! manager #u8(2) 0))) (assert-true (and old-token new-token (path-manager-response! manager old-token 1) (bytes=? #u8(1) (path-manager-address manager)) (bytes=? #u8(2) (path-manager-candidate manager)) (not (path-manager-previous-address manager)) (path-manager-response! manager new-token 1) (bytes=? #u8(2) (path-manager-address manager)))))))) (test "path-manager: failed candidate and previous probes leave the working path usable" (pm-with-manager (lambda (manager) (pm-candidate! manager) (path-manager-tick! manager 3000) (assert-true (and (not (path-manager-candidate manager)) (not (path-manager-previous-address manager)) (bytes=? #u8(1) (path-manager-address manager)) (path-manager-send! manager #u8(1) 1200 3000) (= 3 (length (path-manager-take-events! manager)))))))) (test "path-manager: higher numbered candidate supersedes one bounded pending attempt" (pm-with-manager (lambda (manager) (pm-candidate! manager) (path-manager-observe! manager #u8(3) 1200 2 #f 1) (assert-true (and (bytes=? #u8(3) (path-manager-candidate manager)) (not (path-manager-challenge! manager #u8(2) 1)) (bytes=? #u8(1) (path-manager-address manager)) (= 3 (length (path-manager-take-events! manager)))))))) (test "path-manager: diagnostic backlog is bounded and drains with owned addresses" (pm-with-manager (lambda (manager) (do ((number 1 (+ number 1))) ((= number 40)) (path-manager-observe! manager (bytevector (+ number 1)) 1200 number #f 0)) (let ((events (path-manager-take-events! manager))) (assert-true (and (= 16 (length events)) (null? (path-manager-take-events! manager)) (bytes=? #u8(40) (path-manager-candidate manager)) (bytes=? #u8(1) (path-manager-address manager)))))))) (test "path-manager: invalid metadata and backwards time do not replace a valid candidate" (pm-with-manager (lambda (manager) (pm-candidate! manager) (path-manager-tick! manager 1) (assert-error (path-manager-observe! manager #u8(3) 1200 2 #f 0)) (assert-error (path-manager-observe! manager #u8() 1200 2 #f 1)) (assert-error (path-manager-observe! manager #u8(3) 1200 -1 #f 1)) (assert-error (path-manager-send! manager #u8(3) 1201 1)) (assert-error (path-manager-response! manager #u8(0) 1)) (assert-true (bytes=? #u8(2) (path-manager-candidate manager)))))) (test "path-manager: close is idempotent and refuses further observations or sends" (pm-with-manager (lambda (manager) (pm-candidate! manager) (path-manager-close! manager) (path-manager-close! manager) (assert-false (path-manager-address manager)) (assert-false (path-manager-candidate manager)) (assert-false (path-manager-previous-address manager)) (assert-equal '() (path-manager-take-events! manager)) (assert-error (pm-candidate! manager)) (assert-error (path-manager-send! manager #u8(1) 1 0)) (assert-error (path-manager-challenge! manager #u8(1) 0))))))test/test-path-validation.sgladded
(import (sigil test) (sigil math) (sigil quic bytes) (sigil quic path validation))(define (pv-with-path procedure) (let ((path (make-path-validation #u8(4 127 0 0 1 1 187) 0))) (dynamic-wind (lambda () #t) (lambda () (procedure path)) (lambda () (path-validation-close! path)))))(test-group "Per-path validation and amplification reservations" (test "path-validation: invalid construction refuses before allocating a path" (for-each (lambda (thunk) (assert-error (thunk))) (list (lambda () (make-path-validation #u8() 0)) (lambda () (make-path-validation (make-bytevector 65 0) 0)) (lambda () (make-path-validation #u8(1) -1)) (lambda () (make-path-validation #u8(1) 0 pto: 999)) (lambda () (make-path-validation #u8(1) 0 pto: 60001)) (lambda () (make-path-validation #u8(1) 0 mtu?: #t)) (lambda () (make-path-validation #u8(1) 0 validated?: 1))))) (test "path-validation: address ownership isolates caller and returned bytes" (let* ((address (make-bytevector 7 1)) (path (make-path-validation address 0))) (dynamic-wind (lambda () #t) (lambda () (bytevector-u8-set! address 0 2) (let ((copy (path-validation-address path))) (bytevector-u8-set! copy 1 3) (assert-equal #u8(1 1 1 1 1 1 1) (path-validation-address path)))) (lambda () (path-validation-close! path))))) (test "path-validation: zero credit refuses and exact threefold output is allowed once" (pv-with-path (lambda (path) (assert-true (and (not (path-validation-send! path 1 0)) (not (path-validation-challenge! path 1200 0)) (path-validation-credit! path 100 0) (path-validation-send! path 300 0) (not (path-validation-send! path 1 0))))))) (test "path-validation: credit for another path cannot authorize a probe" (pv-with-path (lambda (first) (pv-with-path (lambda (second) (path-validation-credit! first 400 0) (path-validation-credit! second 20 0) (assert-true (and (bytevector? (path-validation-challenge! first 1200 0)) (not (path-validation-challenge! second 1200 0)) (path-validation-send! second 60 0) (not (path-validation-send! second 1 0))))))))) (test "path-validation: matching padded challenge validates address and MTU" (pv-with-path (lambda (path) (path-validation-credit! path 400 0) (let ((challenge (path-validation-challenge! path 1200 0))) (assert-equal 'validated (path-validation-response! path challenge 1)) (assert-true (path-validation-address-valid? path)) (assert-true (path-validation-mtu-valid? path)) (assert-true (path-validation-send! path 1200 1)) (assert-false (path-validation-response! path challenge 1)))))) (test "path-validation: delayed response can match an earlier outstanding probe" (pv-with-path (lambda (path) (path-validation-credit! path 1200 0) (let* ((first (path-validation-challenge! path 1200 0)) (second (path-validation-challenge! path 1200 1000))) (assert-false (bytes=? first second)) ;; No source-address argument: the connection finds the challenged ;; path by token even when the response arrives on its old path. (assert-equal 'validated (path-validation-response! path first 1500)))))) (test "path-validation: short probe requires a separate padded MTU validation" (pv-with-path (lambda (path) (path-validation-credit! path 100 0) (let ((small (path-validation-challenge! path 100 0))) (assert-true (and (eq? 'address-validated (path-validation-response! path small 1)) (path-validation-address-valid? path) (not (path-validation-mtu-valid? path)) (eq? 'validating (path-validation-status path)) (not (path-validation-challenge! path 100 1)) (let ((large (path-validation-challenge! path 1200 1))) (and (bytevector? large) (eq? 'validated (path-validation-response! path large 2)))))))))) (test "path-validation: probes are paced fresh and bounded before deadline" (pv-with-path (lambda (path) (path-validation-credit! path 1200 0) (let ((first (path-validation-challenge! path 1200 0))) (assert-true (and (not (path-validation-challenge! path 1200 999)) (let* ((second (path-validation-challenge! path 1200 1000)) (third (path-validation-challenge! path 1200 2000))) (and (bytevector? second) (bytevector? third) (not (bytes=? first second)) (not (bytes=? first third)) (not (bytes=? second third)) (not (path-validation-challenge! path 1200 2999)) (path-validation-tick! path 3000) (eq? 'failed (path-validation-status path)))))))))) (test "path-validation: unknown and malformed responses do not validate" (pv-with-path (lambda (path) (assert-false (path-validation-response! path #u8(0 1 2 3 4 5 6 7) 0)) (assert-error (path-validation-response! path #u8(1) 0)) (assert-false (path-validation-address-valid? path)) (assert-false (path-validation-mtu-valid? path))))) (test "path-validation: data and unknown responses cannot extend expiry" (pv-with-path (lambda (path) (path-validation-credit! path 400 0) (let ((challenge (path-validation-challenge! path 1200 0))) (path-validation-credit! path 1200 2998) (assert-true (and (not (path-validation-response! path (flip-byte challenge 0) 2999)) (path-validation-tick! path 3000) (not (path-validation-send! path 1 3000)) (not (path-validation-credit! path 1200 3000)))))))) (test "path-validation: response at the expired deadline cannot revive a path" (pv-with-path (lambda (path) (path-validation-credit! path 400 0) (let ((challenge (path-validation-challenge! path 1200 0))) (assert-true (and (not (path-validation-response! path challenge 3000)) (eq? 'failed (path-validation-status path)) (not (path-validation-address-valid? path)))))))) (test "path-validation: address reachability alone cannot survive a failed MTU phase" (pv-with-path (lambda (path) (path-validation-credit! path 100 0) (let ((small (path-validation-challenge! path 100 0))) (assert-true (and (eq? 'address-validated (path-validation-response! path small 1)) (bytevector? (path-validation-challenge! path 1200 1)) (not (path-validation-response! path small 3000)) (path-validation-tick! path 3001) (not (path-validation-address-valid? path)) (not (path-validation-mtu-valid? path)))))))) (test "path-validation: mutating a returned challenge does not mutate the matcher" (pv-with-path (lambda (path) (path-validation-credit! path 400 0) (let* ((challenge (path-validation-challenge! path 1200 0)) (original (bytevector-copy challenge))) (bytevector-u8-set! challenge 0 (bitwise-xor 1 (bytevector-u8-ref challenge 0))) (assert-true (and (not (path-validation-response! path challenge 1)) (eq? 'validated (path-validation-response! path original 1)))))))) (test "path-validation: invalid size and backwards time cannot spend credit" (pv-with-path (lambda (path) (path-validation-credit! path 100 10) (assert-error (path-validation-send! path 1 9)) (assert-error (path-validation-send! path 0 10)) (assert-error (path-validation-send! path 1201 10)) (assert-error (path-validation-credit! path 65528 10)) (assert-true (and (path-validation-send! path 300 10) (not (path-validation-send! path 1 10))))))) (test "path-validation: an already validated original path needs no amplification credit" (let ((path (make-path-validation #u8(1) 0 validated?: #t mtu?: #t))) (dynamic-wind (lambda () #t) (lambda () (assert-equal 'validated (path-validation-status path)) (assert-true (path-validation-send! path 1200 0)) (assert-false (path-validation-tick! path 1000000)) (assert-true (path-validation-mtu-valid? path))) (lambda () (path-validation-close! path))))) (test "path-validation: idempotent close retires validation and refuses further work" (pv-with-path (lambda (path) (path-validation-credit! path 400 0) (let ((challenge (path-validation-challenge! path 1200 0))) (path-validation-close! path) (path-validation-close! path) (assert-equal 'closed (path-validation-status path)) (assert-false (path-validation-address-valid? path)) (assert-false (path-validation-mtu-valid? path)) (assert-error (path-validation-credit! path 400 0)) (assert-error (path-validation-send! path 1 0)) (assert-error (path-validation-challenge! path 1200 0)) (assert-error (path-validation-response! path challenge 0)))))))test/test-peer-ids.sgladded
(import (sigil test) (sigil quic bytes) (sigil quic wire) (sigil quic frame) (sigil quic error) (sigil quic connection peer ids))(define (pic-with-pool procedure) (let ((pool (make-peer-id-pool #u8(1)))) (dynamic-wind (lambda () #t) (lambda () (procedure pool)) (lambda () (peer-id-pool-close! pool)))))(define (pic-frame sequence retire cid token) (make-quic-frame 24 (list (cons 'sequence sequence) (cons 'retire-prior-to retire) (cons 'connection-id cid) (cons 'token token))))(define (pic-new! pool sequence retire) (peer-id-pool-receive! pool (pic-frame sequence retire (bytevector (+ sequence 1)) (make-bytevector 16 0))))(define (pic-error? code procedure) (guard (exn (else (and (quic-error? exn) (= code (quic-error-code exn)) (= 24 (quic-error-frame-type exn))))) (procedure) #f))(define (pic-retiring pool) (map (lambda (frame) (frame-ref frame 'sequence)) (peer-id-pool-retirements pool)))(test-group "Peer CID capacity, retirement and path ownership" (test "peer-ids: invalid construction refuses ambiguous authenticated metadata" (for-each (lambda (procedure) (assert-error (procedure))) (list (lambda () (make-peer-id-pool (make-bytevector 21 0))) (lambda () (make-peer-id-pool #u8(1) limit: 1)) (lambda () (make-peer-id-pool #u8(1) limit: 9)) (lambda () (make-peer-id-pool #u8(1) token: #u8(0))) (lambda () (make-peer-id-pool #u8() token: (make-bytevector 16 0))) (lambda () (make-peer-id-pool #u8(1) preferred: (cons #u8(1) (make-bytevector 16 0)))) (lambda () (make-peer-id-pool #u8() preferred: (cons #u8(2) (make-bytevector 16 0))))))) (test "peer-ids: initial CID is owned and already assigned to its original path" (let* ((cid (make-bytevector 1 1)) (pool (make-peer-id-pool cid))) (dynamic-wind (lambda () #t) (lambda () (bytevector-u8-set! cid 0 3) (let ((copy (peer-id-pool-id pool 0))) (bytevector-u8-set! copy 0 4) (assert-true (and (bytes=? #u8(1) (peer-id-pool-id pool 0)) (not (peer-id-pool-acquire! pool)) (equal? '(0) (peer-id-pool-sequences pool)))))) (lambda () (peer-id-pool-close! pool))))) (test "peer-ids: incoming CID and token are owned and assigned at most once" (pic-with-pool (lambda (pool) (let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0)) (frame (pic-frame 1 0 cid token))) (peer-id-pool-receive! pool frame) (bytevector-u8-set! cid 0 3) (bytevector-u8-set! token 0 5) (assert-true (and (bytes=? #u8(2) (peer-id-pool-id pool 1)) (pic-new! pool 1 0) (= 1 (peer-id-pool-acquire! pool)) (not (peer-id-pool-acquire! pool)) (= 2 (peer-id-pool-count pool)))))))) (test "peer-ids: duplicate advertisement neither consumes capacity nor releases ownership" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (peer-id-pool-acquire! pool) (assert-true (and (pic-new! pool 1 0) (= 2 (peer-id-pool-count pool)) (not (peer-id-pool-acquire! pool)) (null? (pic-retiring pool))))))) (test "peer-ids: conflicting sequence CID or token and reused CID are typed violations" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (assert-true (and (pic-error? 10 (lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(3) (make-bytevector 16 0))))) (pic-error? 10 (lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(2) (make-bytevector 16 1))))) (pic-error? 10 (lambda () (peer-id-pool-receive! pool (pic-frame 2 0 #u8(2) (make-bytevector 16 0))))) (bytes=? #u8(2) (peer-id-pool-id pool 1)) (= 2 (peer-id-pool-count pool))))))) (test "peer-ids: active limit failure leaves the original inventory unchanged" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (assert-true (and (pic-error? 9 (lambda () (pic-new! pool 2 0))) (equal? '(0 1) (peer-id-pool-sequences pool)) (not (peer-id-pool-id pool 2)) (null? (pic-retiring pool))))))) (test "peer-ids: RetirePriorTo frees active capacity before admitting its replacement" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (assert-true (and (pic-new! pool 2 1) (= 2 (peer-id-pool-count pool)) (not (peer-id-pool-id pool 0)) (equal? '(1 2) (peer-id-pool-sequences pool)) (equal? '(0) (pic-retiring pool))))))) (test "peer-ids: reordered smaller RetirePriorTo cannot undo a peer retirement floor" (pic-with-pool (lambda (pool) (pic-new! pool 2 2) (assert-true (and (not (pic-new! pool 1 0)) (= 1 (peer-id-pool-count pool)) (not (peer-id-pool-id pool 1)) (equal? '(0 1) (pic-retiring pool)) (= 2 (peer-id-pool-acquire! pool))))))) (test "peer-ids: acknowledged retirement cannot resurrect while an older path stays live" (pic-with-pool (lambda (pool) (pic-new! pool 2 0) (peer-id-pool-retire! pool 2) (assert-true (and (peer-id-pool-ack! pool 2) (not (peer-id-pool-ack! pool 2)) (bytes=? #u8(1) (peer-id-pool-id pool 0)) (not (pic-new! pool 2 0)) (not (peer-id-pool-id pool 2)) (not (peer-id-pool-acquire! pool)) (equal? '(2) (pic-retiring pool))))))) (test "peer-ids: unseen older sequence gaps are retired without displacing a live exception" (pic-with-pool (lambda (pool) (pic-new! pool 3 0) (peer-id-pool-retire! pool 3) (peer-id-pool-ack! pool 3) (assert-true (and (not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1)) (= 1 (peer-id-pool-count pool)) (bytes=? #u8(1) (peer-id-pool-id pool 0)) (equal? '(1) (pic-retiring pool))))))) (test "peer-ids: twice-limit pending retirement budget is bounded and errors are atomic" (pic-with-pool (lambda (pool) (for-each (lambda (sequence) (pic-new! pool sequence 0) (peer-id-pool-retire! pool sequence)) '(2 3 4 5)) (pic-new! pool 6 0) (assert-true (and (pic-error? 9 (lambda () (pic-new! pool 1 0))) (pic-error? 9 (lambda () (peer-id-pool-retire! pool 6))) (pic-error? 9 (lambda () (pic-new! pool 7 7))) (= 2 (peer-id-pool-count pool)) (equal? '(0 6) (peer-id-pool-sequences pool)) (equal? '(2 3 4 5) (pic-retiring pool)) (peer-id-pool-ack! pool 2) (peer-id-pool-retire! pool 6) (equal? '(3 4 5 6) (pic-retiring pool))))))) (test "peer-ids: retiring IDs remain pending and cannot be assigned again" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (peer-id-pool-acquire! pool) (assert-true (and (peer-id-pool-retire! pool 1) (not (peer-id-pool-retire! pool 1)) (not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1)) (not (peer-id-pool-acquire! pool)) (equal? '(1) (pic-retiring pool)) (equal? '(1) (pic-retiring pool))))))) (test "peer-ids: zero-length peer source disables spare issue and retirement" (let ((pool (make-peer-id-pool #u8()))) (dynamic-wind (lambda () #t) (lambda () (assert-true (and (bytes=? #u8() (peer-id-pool-id pool 0)) (pic-error? 10 (lambda () (pic-new! pool 1 0))) (not (peer-id-pool-acquire! pool)))) (assert-error (peer-id-pool-retire! pool 0))) (lambda () (peer-id-pool-close! pool))))) (test "peer-ids: authenticated preferred CID is owned sequence one and consumes capacity" (let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0)) (pool (make-peer-id-pool #u8(1) preferred: (cons cid token)))) (dynamic-wind (lambda () #t) (lambda () (bytevector-u8-set! cid 0 4) (bytevector-u8-set! token 0 5) (assert-true (and (= 2 (peer-id-pool-count pool)) (pic-new! pool 1 0) (= 1 (peer-id-pool-acquire! pool)) (pic-error? 9 (lambda () (pic-new! pool 2 0)))))) (lambda () (peer-id-pool-close! pool))))) (test "peer-ids: acknowledgment never forgets a live or unknown CID" (pic-with-pool (lambda (pool) (assert-true (and (not (peer-id-pool-ack! pool 0)) (not (peer-id-pool-ack! pool 1)) (bytes=? #u8(1) (peer-id-pool-id pool 0)) (= 1 (peer-id-pool-count pool))))))) (test "peer-ids: maximum wire sequence retires without overflow or resurrection" (pic-with-pool (lambda (pool) (let ((frame (pic-frame quic-varint-maximum 0 #u8(2) (make-bytevector 16 0)))) (peer-id-pool-receive! pool frame) (peer-id-pool-retire! pool quic-varint-maximum) (peer-id-pool-ack! pool quic-varint-maximum) (assert-true (and (not (peer-id-pool-receive! pool frame)) (not (peer-id-pool-id pool quic-varint-maximum)) (bytes=? #u8(1) (peer-id-pool-id pool 0)) (equal? (list quic-varint-maximum) (pic-retiring pool)))))))) (test "peer-ids: malformed NEW fields are typed frame errors without effects" (pic-with-pool (lambda (pool) (for-each (lambda (frame) (assert-true (pic-error? 7 (lambda () (peer-id-pool-receive! pool frame))))) (list (pic-frame 1 2 #u8(2) (make-bytevector 16 0)) (pic-frame -1 0 #u8(2) (make-bytevector 16 0)) (pic-frame 1 0 #u8() (make-bytevector 16 0)) (pic-frame 1 0 #u8(2) #u8(0)))) (assert-equal '(0) (peer-id-pool-sequences pool)) (assert-equal '() (pic-retiring pool))))) (test "peer-ids: initial sequence learns a token once and then enforces its binding" (pic-with-pool (lambda (pool) (let* ((token (make-bytevector 16 0)) (frame (pic-frame 0 0 #u8(1) token))) (peer-id-pool-receive! pool frame) (bytevector-u8-set! token 0 1) (assert-true (and (pic-error? 10 (lambda () (peer-id-pool-receive! pool frame))) (peer-id-pool-receive! pool (pic-frame 0 0 #u8(1) (make-bytevector 16 0))) (= 1 (peer-id-pool-count pool)) (not (peer-id-pool-acquire! pool)))))))) (test "peer-ids: a duplicate sequence can advance RetirePriorTo without releasing its path" (let ((pool (make-peer-id-pool #u8(1) limit: 3))) (dynamic-wind (lambda () #t) (lambda () (pic-new! pool 1 0) (pic-new! pool 2 0) (peer-id-pool-acquire! pool) (peer-id-pool-acquire! pool) (assert-true (and (pic-new! pool 2 2) (= 1 (peer-id-pool-count pool)) (equal? '(0 1) (pic-retiring pool)) (not (peer-id-pool-acquire! pool)) (not (pic-new! pool 1 0)) (bytes=? #u8(3) (peer-id-pool-id pool 2))))) (lambda () (peer-id-pool-close! pool))))) (test "peer-ids: close clears inventories and refuses subsequent mutations" (pic-with-pool (lambda (pool) (pic-new! pool 1 0) (peer-id-pool-retire! pool 1) (peer-id-pool-close! pool) (peer-id-pool-close! pool) (assert-equal 0 (peer-id-pool-count pool)) (assert-equal '() (peer-id-pool-sequences pool)) (assert-equal '() (pic-retiring pool)) (assert-false (peer-id-pool-id pool 0)) (assert-error (pic-new! pool 1 0)) (assert-error (peer-id-pool-acquire! pool)) (assert-error (peer-id-pool-retire! pool 1)) (assert-error (peer-id-pool-ack! pool 1))))))test/test-random-ids.sgladded
(import (sigil test) (sigil quic bytes) (sigil quic frame) (sigil quic connection ids))(define (ri-with-pool initial procedure (keys: (maximum 256))) (let ((pool (make-local-id-pool initial maximum: maximum))) (dynamic-wind (lambda () #t) (lambda () (procedure pool)) (lambda () (local-id-pool-close! pool)))))(test-group "Bounded random spare-CID reservations" (test "random-ids: fresh reservation stays unroutable until its exact advertisement commits" (ri-with-pool #u8(1 2 3 4 5 6 7 8) (lambda (pool) (let ((frame (local-id-pool-reserve-random! pool))) (assert-true (and frame (= 1 (frame-ref frame 'sequence)) (= 8 (bytevector-length (frame-ref frame 'connection-id))) (= 16 (bytevector-length (frame-ref frame 'token))) (not (bytes=? #u8(1 2 3 4 5 6 7 8) (frame-ref frame 'connection-id))) (not (local-id-pool-route pool (frame-ref frame 'connection-id))) (local-id-pool-check! pool frame) (local-id-pool-commit! pool frame) (= 1 (local-id-pool-route pool (frame-ref frame 'connection-id))))))))) (test "random-ids: queued reservations consume peer capacity without making routes" (ri-with-pool #u8(1) (lambda (pool) (let ((frame (local-id-pool-reserve-random! pool))) (assert-true (and frame (= 2 (local-id-pool-count pool)) (not (local-id-pool-route pool (frame-ref frame 'connection-id))) (not (local-id-pool-reserve-random! pool)))))))) (test "random-ids: zero-length routing cannot acquire spare IDs" (ri-with-pool #u8() (lambda (pool) (assert-true (and (not (local-id-pool-reserve-random! pool)) (= 1 (local-id-pool-count pool))))))) (test "random-ids: lifetime exhaustion preserves live routes and refuses sequence reuse" (ri-with-pool #u8(1) (lambda (pool) (let ((frame (local-id-pool-reserve-random! pool))) (assert-true (and frame (local-id-pool-commit! pool frame) (local-id-pool-retire! pool 1 #u8(1)) (= 1 (local-id-pool-count pool)) (not (local-id-pool-reserve-random! pool)) (= 0 (local-id-pool-route pool #u8(1))))))) maximum: 2)))