Commit5245f5a4Recorded18 Oct 2026Repositorysigil-quic

Support configurable IPv4 and IPv6 QUIC endpoints

Message

Bind a checked numeric local interface and route protected output, admission and Retry identity through canonical actual peer host/port keys. Preserve the existing IPv4 tuple format and bound the distinct IPv6 representation.

Requires the shared family-aware UDP socket APIs from Sigil d237e25.

Changed
 README.md                    |  81 ++++++++++++++++++++++++++++++++++++++--------------------
 scripts/native-tls-tests.mjs |  10 ++++++--
 src/sigil/quic/address.sgl   |  97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/quic/endpoint.sgl  | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------------
 test/test-address.sgl        |  71 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-endpoint-bind.sgl  | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 556 insertions(+), 96 deletions(-)
Diff
README.mdmodified
@@ -1,7 +1,7 @@
1
# sigil-quic
2
3
Experimental QUIC v1 packet protection and a TLS 1.3 handshake engine in Sigil.
−4
The experimental endpoint is restricted to private loopback testing.
+4
The experimental endpoint supports private numeric IPv4/IPv6 interface testing.
5
6
`(sigil quic tls)` exports SHA-256 HKDF-Expand-Label, key-schedule
7
transitions, traffic updates, Finished MACs and the client/server handshake.
@@ -11,6 +11,12 @@ protection with the AES-128 single-block primitive in sigil-crypto.
11
12
The package requires sigil-crypto ^0.18.1, which includes the AES block API.
13
Install released dependencies normally, without development redirects.
+14
Configured UDP endpoints additionally require the shared `(sigil socket)`
+15
family-aware `udp-socket` and `socket-normalize-address` APIs, introduced by
+16
Sigil commit `d237e25c6d881300cf37eedbc657b2203badec0f`. That shared runtime
+17
change has not been released yet: the current published runtime alone cannot
+18
run the configured endpoint. The focused endpoint gate uses a runtime built
+19
with that change; upstream availability is required for normal consumption.
20
RFC 9001 tests derive real AES masks and check complete client/server Initial
21
packets, sample selection, payload authentication and inverse processing.
22
@@ -284,7 +290,7 @@ response to the challenge's source. Responses precede application backlog,
290
expire while blocked and are not retransmitted on PTO. Independent candidate
291
and previous-path validation uses fresh, paced probes and separate three-times
292
received-byte budgets. Promotion requires reachability and a 1200-byte MTU
−287
check. The UDP endpoint below supplies this address-bound adapter on loopback.
+293
check. The UDP endpoint below supplies this address-bound adapter on numeric interfaces.
294
295
Zero-length CID policy is enforced after packet authentication and before any
296
application handler: NEW_CONNECTION_ID is forbidden when sending a zero-length
@@ -502,29 +508,43 @@ rotation and path policy remain separate work.
508
before the full suite passes. Stream tests include protected bidirectional
509
transfer beyond the receive-window capacity and reset/cancellation under loss.
510
−505
`(sigil quic endpoint)` is a private IPv4 loopback UDP adapter. It binds only
−506
`127.0.0.1` and uses a dedicated nonblocking socket for one connection. It binds
−507
the original peer before the handshake, sends each protected datagram to its
−508
recorded destination and validates confirmed-server peer port changes. Clients
−509
keep their original server-source restriction. Configurable interface binds,
−510
IPv6 and multiple connections remain outside this adapter.
−511
−512
`open-quic-endpoint port: 0` chooses an ephemeral port, available through
−513
`quic-endpoint-port`. Attach a fresh client connection/stream with
−514
`quic-endpoint-attach! endpoint connection peer-port`; this starts its TLS
−515
handshake. A server calls `quic-endpoint-accept! endpoint factory` instead.
−516
The factory receives original destination/client source CIDs, constructs a
−517
server connection, attaches its stream handler and returns that connection.
−518
An optional `peer-port:` pins the expected source before admission; otherwise
−519
the first authenticated Initial pins it. Before invoking the factory, the
−520
endpoint opens that Initial with ephemeral client Initial keys and requires
−521
a decoded, nonempty CRYPTO frame at offset zero. Keys are discarded after
−522
this check. Initial integrity is not address validation: Retry remains
−523
necessary before public operation. Failed candidates before confirmation
−524
release the slot and discard their output while preserving the original
−525
factory and configured port restriction. After confirmation, a server passes
−526
candidate source ports to authenticated path validation; the admission pin
−527
does not prevent NAT rebinding. The endpoint retains one connection throughout.
+511
`(sigil quic endpoint)` uses a dedicated nonblocking IPv4 or IPv6 UDP socket
+512
for one connection. It binds the original peer before the handshake, sends
+513
each protected datagram to its recorded destination and validates confirmed
+514
server peer address changes. Clients keep their original server source.
+515
Multiple connections and cross-family migration remain outside this adapter.
+516
+517
`(open-quic-endpoint host: "<assigned-numeric-IP>" port: 4433)` chooses the
+518
socket family from the checked host. The default host is `127.0.0.1`; port 0
+519
chooses an ephemeral port. IPv4 `0.0.0.0` and IPv6 `::` select wildcard binds;
+520
IPv6 sockets are IPv6-only. Numeric decimal IPv6 scope IDs are supported;
+521
hostnames and interface-name scope IDs are rejected. `quic-endpoint-host`,
+522
`quic-endpoint-family` and `quic-endpoint-port` expose the actual local bind.
+523
The host accessor returns an owned string.
+524
+525
Attach a fresh client connection/stream with
+526
`(quic-endpoint-attach! endpoint connection peer-port peer-host: "<numeric-peer-IP>")`;
+527
this starts its TLS handshake. The peer must match the socket family; its
+528
omitted host defaults to that family's loopback. A server calls
+529
`quic-endpoint-accept! endpoint factory` instead. Its factory receives original
+530
destination/client source CIDs, constructs a server connection, attaches its
+531
stream handler and returns that connection. Optional `peer-host:` and
+532
`peer-port:` filters restrict admission independently; the first authenticated
+533
Initial pins any unspecified values. Before invoking the factory, the endpoint
+534
opens that Initial with ephemeral client Initial keys and requires a decoded,
+535
nonempty CRYPTO frame at offset zero. Keys are discarded after this check.
+536
Initial integrity is not address validation: Retry remains necessary before
+537
public operation. Failed candidates before confirmation release the slot and
+538
discard their output while preserving the factory and both configured filters.
+539
After confirmation, server candidate sources go through authenticated path
+540
validation; the admission filters do not prevent NAT rebinding. The endpoint
+541
retains one connection throughout.
+542
+543
`(sigil quic address)` supplies bounded canonical peer keys through
+544
`quic-address`, `quic-address?`, `quic-address-host`, `quic-address-port` and
+545
`quic-address-family`. IPv4 retains its original six-byte address/port tuple.
+546
IPv6 uses distinct bounded framing with a canonical numeric host and scope;
+547
these keys are local path/Retry identities and never QUIC wire addresses.
548
549
`quic-endpoint-peer-address` returns owned canonical peer bytes or `#f` before
550
admission. `quic-endpoint-take-path-events!` drains the connection's bounded
@@ -537,13 +557,13 @@ instead. Its factory receives three copied IDs: original destination, client
557
source and Retry source. Construct the server with the third ID as
558
`retry-source-id:`, attach its stream handler, and return the owned connection.
559
The endpoint checks the returned connection's CID metadata and aborts a
−540
rejected return. An optional `peer-port:` restricts admission;
+560
rejected return. Optional `peer-host:` and `peer-port:` restrict admission;
561
`token-lifetime:` is 1–60,000 milliseconds, default 10,000.
562
563
The endpoint issues a challenge only after authenticating a tokenless Initial
564
of at least 1,200 bytes with nonempty CRYPTO at offset zero. It retains no
565
challenge queue or peer pin. Before invoking the factory or pinning a source,
−546
it validates the private token against the actual loopback address and UDP
+566
it validates the private token against the actual canonical source host and UDP
567
port, current source/destination CIDs and monotonic time, then authenticates
568
the retried Initial. Invalid token-bearing Initials drop without another Retry.
569
Each receive pass permits at most 32 challenge attempts; each challenge fits
@@ -551,7 +571,7 @@ Each receive pass permits at most 32 challenge attempts; each challenge fits
571
The endpoint owns a fresh token key, keeps it across failed tentative
572
connections, and retires it on confirmation or endpoint close. The connection
573
keeps its conservative amplification limit until authenticated Handshake.
−554
This path remains restricted to loopback pending admission tests and review.
+574
This adapter remains experimental and requires independent review before public use.
575
576
Call `quic-endpoint-step! endpoint wait-ms: milliseconds` repeatedly alongside
577
application reads/writes. Each call waits at most 50 milliseconds and drives
@@ -627,6 +647,11 @@ On the test machine, `QUIC_ENDPOINT_RED=1 sigil test test/test-endpoint.sgl`
647
must fail exactly two controls before the full suite passes. Endpoint tests
648
use actual loopback UDP, arbitrary binary payloads beyond the receive window,
649
peer-port filtering, bounded admission and failure/timeout cleanup.
+650
The focused native selector `scripts/native-tls-tests.mjs --endpoint-bind`
+651
adds canonical address tests and Retry/stream/DATAGRAM exchanges on IPv4, IPv6
+652
and wildcard binds. Assigned-interface cases accept `QUIC_TEST_INTERFACE_IPV4`
+653
and `QUIC_TEST_INTERFACE_IPV6`; absent values use loopback fixtures. Run these
+654
tests on a runtime providing the shared socket APIs described above.
655
656
Receive work is bounded to eight parsed packets per datagram, including
657
unrouted packets and authentication failures. Excess coalesced packets are
scripts/native-tls-tests.mjsmodified
@@ -64,6 +64,8 @@ const files = [
64
'test-endpoint-admission.sgl',
65
'test-endpoint-retry.sgl',
66
'test-endpoint-network.sgl',
+67
'test-address.sgl',
+68
'test-endpoint-bind.sgl',
69
];
70
const cleanupFaults = process.argv.includes('--cleanup-faults');
71
const secretCleanup = process.argv.includes('--secret-cleanup');
@@ -97,7 +99,8 @@ const pathManager = process.argv.includes('--path-manager');
99
const connectionNetwork = process.argv.includes('--connection-network');
100
const networkIntegration = process.argv.includes('--network-integration');
101
const endpointNetwork = process.argv.includes('--endpoint-network');
−100
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry, endpointRetry, retryCleanup, datagram, pathValidation, localIds, peerIds, pathManager, connectionNetwork, networkIntegration, endpointNetwork].filter(Boolean).length > 1) {
+102
const endpointBind = process.argv.includes('--endpoint-bind');
+103
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, endpointBind].filter(Boolean).length > 1) {
104
throw new Error('Select one focused test suite');
105
}
106
if (cleanupFaults) {
@@ -196,6 +199,9 @@ if (networkIntegration) {
199
if (endpointNetwork) {
200
files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl');
201
}
+202
if (endpointBind) {
+203
files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl', 'test-address.sgl', 'test-endpoint-bind.sgl');
+204
}
205
const bodies = files.map(file => {
206
const text = readFileSync(join(root, 'test', file), 'utf8');
207
const boundary = text.indexOf('\n\n');
@@ -221,7 +227,7 @@ writeFileSync(join(source, 'native.sgl'), `(define-library (sigil quic tests nat
227
(sigil quic connection space)
228
(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)' : ''}
229
(sigil quic stream)
−224
(sigil quic endpoint)${files.includes('test-path-validation.sgl') ? '\n (sigil quic path validation)' : ''}${files.includes('test-path-manager.sgl') ? '\n (sigil quic path manager)' : ''}
+230
(sigil quic endpoint)${files.includes('test-address.sgl') || files.includes('test-endpoint-bind.sgl') ? '\n (sigil quic address)' : ''}${files.includes('test-path-validation.sgl') ? '\n (sigil quic path validation)' : ''}${files.includes('test-path-manager.sgl') ? '\n (sigil quic path manager)' : ''}
231
(sigil quic stream state)
232
(sigil quic transport parameters)
233
(sigil quic tls)
src/sigil/quic/address.sgladded
@@ -0,0 +1,97 @@
+1
;;; Canonical endpoint path identity for numeric IPv4 and IPv6 addresses.
+2
;;; These bytes are local keys for Retry/path state, never QUIC wire fields.
+3
(define-library (sigil quic address)
+4
(import (sigil io)
+5
(sigil socket)
+6
(sigil quic bytes))
+7
(export quic-address quic-address? quic-address-host
+8
quic-address-port quic-address-family)
+9
(begin
+10
(define (port? value)
+11
(and (integer? value) (exact? value) (<= 1 value 65535)))
+12
+13
(define (ipv6-host? host)
+14
(let loop ((index 0))
+15
(and (< index (string-length host))
+16
(or (char=? (string-ref host index) #\:)
+17
(loop (+ index 1))))))
+18
+19
;; Only called after native numeric normalization. Keep the IPv4 tuple
+20
;; identical to existing loopback keys, including its six-byte length.
+21
(define (ipv4-octets host)
+22
(let loop ((index 0) (value 0) (parts '()))
+23
(if (= index (string-length host))
+24
(reverse (cons value parts))
+25
(let ((character (string-ref host index)))
+26
(if (char=? character #\.)
+27
(loop (+ index 1) 0 (cons value parts))
+28
(loop (+ index 1)
+29
(+ (* value 10) (- (char->integer character) 48))
+30
parts))))))
+31
+32
(define (quic-address host port)
+33
(unless (and (string? host) (port? port))
+34
(error "QUIC address: expected numeric host and nonzero port"))
+35
(let ((canonical (socket-normalize-address host)))
+36
(unless canonical
+37
(error "QUIC address: invalid numeric host"))
+38
(if (ipv6-host? canonical)
+39
;; Header: family, port high/low, two reserved zero bytes.
+40
;; Minimum length is seven, so no IPv6 key aliases an IPv4 tuple.
+41
(let ((bytes (string->utf8 canonical)))
+42
(unless (<= 2 (bytevector-length bytes) 59)
+43
(error "QUIC address: numeric host exceeds path-key bound"))
+44
(bytes-append
+45
(bytevector 6 (quotient port 256) (modulo port 256) 0 0)
+46
bytes))
+47
(apply bytevector
+48
(append (ipv4-octets canonical)
+49
(list (quotient port 256) (modulo port 256)))))))
+50
+51
(define (decode-address address)
+52
(and (bytevector? address)
+53
(let ((length (bytevector-length address)))
+54
(cond
+55
((= length 6)
+56
(let ((port (+ (* 256 (bytevector-u8-ref address 4))
+57
(bytevector-u8-ref address 5))))
+58
(and (port? port)
+59
(list 'ipv4
+60
(string-append
+61
(number->string (bytevector-u8-ref address 0)) "."
+62
(number->string (bytevector-u8-ref address 1)) "."
+63
(number->string (bytevector-u8-ref address 2)) "."
+64
(number->string (bytevector-u8-ref address 3)))
+65
port))))
+66
((and (<= 7 length 64)
+67
(= 6 (bytevector-u8-ref address 0))
+68
(= 0 (bytevector-u8-ref address 3))
+69
(= 0 (bytevector-u8-ref address 4)))
+70
(let ((port (+ (* 256 (bytevector-u8-ref address 1))
+71
(bytevector-u8-ref address 2))))
+72
(and (port? port)
+73
(guard (exn (else #f))
+74
(let* ((bytes (bytevector-copy address 5))
+75
(host (utf8->string bytes))
+76
(canonical (socket-normalize-address host)))
+77
(and canonical
+78
(ipv6-host? canonical)
+79
(bytes=? bytes (string->utf8 canonical))
+80
(list 'ipv6 canonical port)))))))
+81
(else #f)))))
+82
+83
(define (quic-address? address)
+84
(and (decode-address address) #t))
+85
+86
(define (require-address address)
+87
(or (decode-address address)
+88
(error "QUIC address: invalid canonical path key")))
+89
+90
(define (quic-address-host address)
+91
(cadr (require-address address)))
+92
+93
(define (quic-address-port address)
+94
(caddr (require-address address)))
+95
+96
(define (quic-address-family address)
+97
(car (require-address address)))))
src/sigil/quic/endpoint.sglmodified
@@ -1,6 +1,5 @@
−1
;;; Private Q1 UDP adapter. Loopback only until independent security review.
+1
;;; One-connection UDP adapter with explicit numeric interface binding.
2
;;; One connection, addressed protected output and bounded work per step.
−3
;;; The address codec/bind below remains loopback-only pending shared UDP support.
3
(define-library (sigil quic endpoint)
4
(import (sigil crypto)
5
(sigil struct)
@@ -8,11 +7,13 @@
7
(sigil socket)
8
(sigil time)
9
(sigil quic bytes)
+10
(sigil quic address)
11
(sigil quic packet)
12
(sigil quic frame)
13
(sigil quic retry token)
14
(sigil quic connection))
15
(export open-quic-endpoint quic-endpoint? quic-endpoint-port
+16
quic-endpoint-host quic-endpoint-family
17
quic-endpoint-connection quic-endpoint-closed? quic-endpoint-failure
18
quic-endpoint-attach! quic-endpoint-accept! quic-endpoint-accept-retry!
19
quic-endpoint-peer-address quic-endpoint-take-path-events!
@@ -22,12 +23,16 @@
23
(define-struct quic-endpoint
24
(socket)
25
(port)
+26
(bind-host)
+27
(family)
28
(origin)
29
(frequency)
30
(peer-port default: #f mutable: #t)
+31
(peer-host default: #f mutable: #t)
32
(connection default: #f mutable: #t)
33
(acceptor default: #f mutable: #t)
34
(accept-port default: #f mutable: #t)
+35
(accept-host default: #f mutable: #t)
36
(retry-manager default: #f mutable: #t)
37
(retry-attempts default: 0 mutable: #t)
38
(pending default: '() mutable: #t)
@@ -39,22 +44,38 @@
44
(define (port? port minimum)
45
(and (integer? port) (exact? port) (<= minimum port 65535)))
46
−42
(define (open-quic-endpoint (keys: (port 0)))
+47
(define (open-quic-endpoint (keys: (host "127.0.0.1") (port 0)))
48
(unless (port? port 0) (error "QUIC endpoint: invalid local port"))
−44
(let ((socket (udp-socket)))
+49
(unless (string? host) (error "QUIC endpoint: expected numeric bind host"))
+50
(let* ((canonical (socket-normalize-address host))
+51
(family (and canonical (quic-address-family (quic-address canonical 1))))
+52
(socket (and family (udp-socket family))))
+53
(unless canonical (error "QUIC endpoint: invalid numeric bind host"))
54
(unless socket (error "QUIC endpoint: UDP socket creation failed"))
55
(guard (exn (else (socket-close socket) (raise exn)))
−47
;; The literal is intentional: the native IPv4 bind API does not
−48
;; reliably reject an invalid host before falling back to INADDR_ANY.
−49
(unless (udp-bind socket port "127.0.0.1") (error "QUIC endpoint: loopback bind failed"))
+56
(unless (udp-bind socket port canonical) (error "QUIC endpoint: numeric bind failed"))
57
(unless (socket-set-non-blocking! socket #t) (error "QUIC endpoint: nonblocking setup failed"))
58
(let ((address (socket-local-address socket)))
−52
(unless (and (pair? address) (equal? (car address) "127.0.0.1")
−53
(pair? (cdr address)) (port? (cadr address) 1))
+59
(unless (and (pair? address) (equal? (car address) canonical)
+60
(pair? (cdr address)) (port? (cadr address) 1)
+61
(or (= port 0) (= port (cadr address))))
62
(error "QUIC endpoint: unexpected bound address"))
63
(quic-endpoint socket: socket port: (cadr address)
+64
bind-host: canonical family: family
65
origin: (current-jiffy) frequency: (jiffies-per-second))))))
66
+67
(define (quic-endpoint-host endpoint)
+68
(let ((host (quic-endpoint-bind-host endpoint)))
+69
(substring host 0 (string-length host))))
+70
+71
(define (canonical-peer-host endpoint host)
+72
(let* ((value (or host
+73
(if (eq? (quic-endpoint-family endpoint) 'ipv6) "::1" "127.0.0.1")))
+74
(address (quic-address value 1)))
+75
(unless (eq? (quic-address-family address) (quic-endpoint-family endpoint))
+76
(error "QUIC endpoint: peer address family differs from socket"))
+77
(quic-address-host address)))
+78
79
(define (endpoint-now endpoint)
80
(quotient (* 1000 (- (current-jiffy) (quic-endpoint-origin endpoint))) (quic-endpoint-frequency endpoint)))
81
@@ -63,35 +84,43 @@
84
(quic-endpoint-acceptor endpoint))
85
(error "QUIC endpoint: closed or already attached")))
86
−66
(define (quic-endpoint-attach! endpoint connection peer-port)
+87
(define (quic-endpoint-attach! endpoint connection peer-port (keys: (peer-host #f)))
88
(require-unattached! endpoint)
89
(unless (and (quic-connection? connection) (not (quic-closed? connection)) (port? peer-port 1))
90
(error "QUIC endpoint: invalid connection or peer port"))
−70
(set-quic-endpoint-peer-port! endpoint peer-port)
−71
(set-quic-endpoint-connection! endpoint connection)
−72
(guard (exn (else (quic-endpoint-close! endpoint) (raise exn)))
−73
(quic-bind-peer-address! connection (retry-address peer-port))
−74
(when (eq? (quic-role connection) 'client) (quic-start! connection (endpoint-now endpoint)))))
+91
(let ((host (canonical-peer-host endpoint peer-host)))
+92
(set-quic-endpoint-peer-port! endpoint peer-port)
+93
(set-quic-endpoint-peer-host! endpoint host)
+94
(set-quic-endpoint-connection! endpoint connection)
+95
(guard (exn (else (quic-endpoint-close! endpoint) (raise exn)))
+96
(quic-bind-peer-address! connection (quic-address host peer-port))
+97
(when (eq? (quic-role connection) 'client)
+98
(quic-start! connection (endpoint-now endpoint))))))
99
100
;; The factory receives copied (original-DCID, client-SCID). It constructs
101
;; the server and attaches its application/stream handler before returning.
−78
(define (quic-endpoint-accept! endpoint factory (keys: (peer-port #f)))
+102
(define (quic-endpoint-accept! endpoint factory
+103
(keys: (peer-port #f) (peer-host #f)))
104
(require-unattached! endpoint)
105
(unless (and (procedure? factory) (or (not peer-port) (port? peer-port 1)))
106
(error "QUIC endpoint: invalid server factory or peer port"))
−82
(set-quic-endpoint-peer-port! endpoint peer-port)
−83
(set-quic-endpoint-accept-port! endpoint peer-port)
−84
(set-quic-endpoint-acceptor! endpoint factory))
+107
(let ((host (and peer-host (canonical-peer-host endpoint peer-host))))
+108
(set-quic-endpoint-peer-port! endpoint peer-port)
+109
(set-quic-endpoint-peer-host! endpoint host)
+110
(set-quic-endpoint-accept-port! endpoint peer-port)
+111
(set-quic-endpoint-accept-host! endpoint host)
+112
(set-quic-endpoint-acceptor! endpoint factory)))
113
114
;; This factory receives copied (original-DCID, client-SCID, Retry-SCID).
115
;; It must return an owned, fresh retry-aware server with its handler set.
116
;; The endpoint owns the private token key until confirmation or close.
117
(define (quic-endpoint-accept-retry! endpoint factory
−90
(keys: (peer-port #f) (token-lifetime 10000)))
+118
(keys: (peer-port #f) (peer-host #f) (token-lifetime 10000)))
119
(require-unattached! endpoint)
120
(unless (and (procedure? factory) (or (not peer-port) (port? peer-port 1)))
121
(error "QUIC endpoint: invalid server factory or peer port"))
−94
(let ((manager (make-retry-token-manager lifetime: token-lifetime)))
+122
(let* ((host (and peer-host (canonical-peer-host endpoint peer-host)))
+123
(manager (make-retry-token-manager lifetime: token-lifetime)))
124
(guard (exn
125
(else
126
(close-retry-token-manager! manager)
@@ -99,7 +128,9 @@
128
(raise exn)))
129
(set-quic-endpoint-retry-manager! endpoint manager)
130
(set-quic-endpoint-peer-port! endpoint peer-port)
+131
(set-quic-endpoint-peer-host! endpoint host)
132
(set-quic-endpoint-accept-port! endpoint peer-port)
+133
(set-quic-endpoint-accept-host! endpoint host)
134
(set-quic-endpoint-acceptor! endpoint factory))))
135
136
(define (retire-retry-manager! endpoint)
@@ -107,22 +138,13 @@
138
(set-quic-endpoint-retry-manager! endpoint #f)
139
(when manager (close-retry-token-manager! manager))))
140
−110
;; Address bytes are the same canonical loopback-IP/port tuple used by
−111
;; Retry. General numeric IPv4/IPv6 encoding belongs to the shared socket
−112
;; contract; this adapter slice deliberately retains the existing bind.
−113
(define (output-port output)
+141
;; Protected output retains its original canonical destination. Neither
+142
;; path promotion nor a newer received packet may rewrite that address.
+143
(define (output-target endpoint output)
144
(let ((address (quic-output-address output)))
−115
(unless (and (= (bytevector-length address) 6)
−116
(= (bytevector-u8-ref address 0) 127)
−117
(= (bytevector-u8-ref address 1) 0)
−118
(= (bytevector-u8-ref address 2) 0)
−119
(= (bytevector-u8-ref address 3) 1))
−120
(error "QUIC endpoint: output has an unsupported address"))
−121
(let ((port (+ (* 256 (bytevector-u8-ref address 4))
−122
(bytevector-u8-ref address 5))))
−123
(unless (port? port 1)
−124
(error "QUIC endpoint: output has an invalid port"))
−125
port)))
+145
(unless (eq? (quic-address-family address) (quic-endpoint-family endpoint))
+146
(error "QUIC endpoint: output address family differs from socket"))
+147
(cons (quic-address-host address) (quic-address-port address))))
148
149
(define (quic-endpoint-peer-address endpoint)
150
(let ((connection (quic-endpoint-connection endpoint)))
@@ -149,8 +171,9 @@
171
(when (and (quic-output-valid? connection output)
172
(< (- now queued-at) 1000))
173
(let* ((packet (quic-output-bytes output))
+174
(target (output-target endpoint output))
175
(sent (udp-send (quic-endpoint-socket endpoint) packet
−153
"127.0.0.1" (output-port output))))
+176
(car target) (cdr target))))
177
(cond
178
((and sent (= sent (bytevector-length packet))) #t)
179
(sent (error "QUIC endpoint: partial UDP datagram send"))
@@ -203,12 +226,8 @@
226
(decode-frames (opened-quic-packet-payload opened) 'initial 'client))))))
227
(lambda () (discard-packet-keys! keys)))))
228
−206
;; The native socket is IPv4 and receive! has checked the literal address.
−207
;; Bind the token to the actual UDP source port, not a requested peer port.
−208
(define (retry-address peer-port)
−209
(bytevector 127 0 0 1 (quotient peer-port 256) (modulo peer-port 256)))
−210
−211
(define (send-retry! endpoint bytes header peer-port now)
+229
;; Retry authenticates the entire actual source host/port path key.
+230
(define (send-retry! endpoint bytes header address now)
231
;; Count attempts before authentication, including malformed ciphertext.
232
;; No source pin, connection, output queue or per-source state is retained.
233
(when (< (quic-endpoint-retry-attempts endpoint) 32)
@@ -219,18 +238,19 @@
238
(random-id (random-bytes 8))
239
(retry (if (bytes=? original random-id) (flip-byte random-id 0) random-id))
240
(token (make-retry-token (quic-endpoint-retry-manager endpoint)
−222
(retry-address peer-port) original retry client now))
+241
address original retry client now))
242
(packet (encode-retry-packet original client retry token)))
243
(when (or (> (bytevector-length packet) 1200)
244
(> (bytevector-length packet) (* 3 (bytevector-length bytes))))
245
(error "QUIC endpoint: Retry output budget exceeded"))
246
;; A blocked send is a drop. A retransmitted Initial can request
247
;; another challenge without preserving any unsent output here.
−229
(let ((sent (udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" peer-port)))
+248
(let ((sent (udp-send (quic-endpoint-socket endpoint) packet
+249
(quic-address-host address) (quic-address-port address))))
250
(when (and sent (not (= sent (bytevector-length packet))))
251
(error "QUIC endpoint: partial Retry datagram send")))))))
252
−233
(define (admit-retry-connection! endpoint original client retry peer-port)
+253
(define (admit-retry-connection! endpoint original client retry address)
254
(let ((connection #f) (transferred? #f))
255
(dynamic-wind
256
(lambda () #t)
@@ -252,16 +272,17 @@
272
(bytes=? (quic-local-id connection) (cdr source-parameter))
273
(bytes=? retry (cdr retry-parameter)))
274
(error "QUIC endpoint: Retry factory connection ID metadata mismatch")))
−255
(quic-bind-peer-address! connection (retry-address peer-port))
−256
(set-quic-endpoint-peer-port! endpoint peer-port)
+275
(quic-bind-peer-address! connection address)
+276
(set-quic-endpoint-peer-port! endpoint (quic-address-port address))
+277
(set-quic-endpoint-peer-host! endpoint (quic-address-host address))
278
(set-quic-endpoint-connection! endpoint connection)
279
(set! transferred? #t))
280
(lambda ()
281
(when (and (not transferred?) (quic-connection? connection))
282
(quic-abort! connection))))))
283
−263
(define (admit! endpoint bytes peer-port now)
−264
(when (and (quic-endpoint-acceptor endpoint) (port? peer-port 1)
+284
(define (admit! endpoint bytes address now)
+285
(when (and (quic-endpoint-acceptor endpoint) (quic-address? address)
286
(>= (bytevector-length bytes) 1200))
287
(let ((header (guard (exn (else #f)) (decode-packet-header bytes 0 0))))
288
(when (and header (eq? (quic-header-kind header) 'initial)
@@ -269,16 +290,16 @@
290
(cond
291
((quic-endpoint-retry-manager endpoint)
292
(if (= 0 (bytevector-length (quic-header-token header)))
−272
(send-retry! endpoint bytes header peer-port now)
+293
(send-retry! endpoint bytes header address now)
294
;; Invalid token-bearing Initials never get a second Retry.
295
(let ((original (validate-retry-token (quic-endpoint-retry-manager endpoint)
−275
(retry-address peer-port)
+296
address
297
(quic-header-destination-id header)
298
(quic-header-source-id header)
299
(quic-header-token header) now)))
300
(when (and original (authenticated-initial? bytes header))
301
(admit-retry-connection! endpoint original
−281
(quic-header-source-id header) (quic-header-destination-id header) peer-port)))))
+302
(quic-header-source-id header) (quic-header-destination-id header) address)))))
303
((authenticated-initial? bytes header)
304
(let ((connection ((quic-endpoint-acceptor endpoint)
305
(bytevector-copy (quic-header-destination-id header))
@@ -292,11 +313,12 @@
313
(eq? (quic-role connection) 'server)
314
(not (quic-closed? connection)))
315
(error "QUIC endpoint: factory must return a fresh server connection"))
−295
(quic-bind-peer-address! connection (retry-address peer-port))
−296
(set-quic-endpoint-peer-port! endpoint peer-port)
+316
(quic-bind-peer-address! connection address)
+317
(set-quic-endpoint-peer-port! endpoint (quic-address-port address))
+318
(set-quic-endpoint-peer-host! endpoint (quic-address-host address))
319
(set-quic-endpoint-connection! endpoint connection)))))))))
320
−299
;; Preserve the configured admission port, not the tentative source pin.
+321
;; Preserve configured admission filters, not the tentative source pin.
322
;; A failure before confirmation releases the slot without closing UDP.
323
(define (reconcile-admission! endpoint now)
324
(let ((connection (quic-endpoint-connection endpoint)))
@@ -312,17 +334,20 @@
334
(set-quic-endpoint-pending! endpoint '())
335
(set-quic-endpoint-pending-since! endpoint #f)
336
(set-quic-endpoint-connection! endpoint #f)
−315
(set-quic-endpoint-peer-port! endpoint (quic-endpoint-accept-port endpoint)))))))
+337
(set-quic-endpoint-peer-port! endpoint (quic-endpoint-accept-port endpoint))
+338
(set-quic-endpoint-peer-host! endpoint (quic-endpoint-accept-host endpoint)))))))
339
340
;; The configured admission port applies before confirmation. A confirmed
341
;; server must authenticate candidate paths in the connection; a client
342
;; keeps its original server-source restriction.
−320
(define (receive-port? endpoint port)
+343
(define (receive-peer? endpoint host port)
344
(let ((connection (quic-endpoint-connection endpoint)))
345
(or (and connection (eq? (quic-role connection) 'server)
346
(quic-confirmed? connection))
−324
(not (quic-endpoint-peer-port endpoint))
−325
(= port (quic-endpoint-peer-port endpoint)))))
+347
(and (or (not (quic-endpoint-peer-port endpoint))
+348
(= port (quic-endpoint-peer-port endpoint)))
+349
(or (not (quic-endpoint-peer-host endpoint))
+350
(equal? host (quic-endpoint-peer-host endpoint)))))))
351
352
(define (receive! endpoint now)
353
;; Out-of-peer noise must not consume the connection's receive budget.
@@ -332,19 +357,25 @@
357
(when (and (< reads 96) (< admitted 32))
358
(let ((received (udp-receive (quic-endpoint-socket endpoint) (+ packet-datagram-limit 1))))
359
(when received
−335
(let ((processed? #f))
−336
(when (and (equal? (cadr received) "127.0.0.1")
−337
(port? (caddr received) 1)
−338
(receive-port? endpoint (caddr received)))
+360
(let* ((processed? #f)
+361
(host (cadr received))
+362
(port (caddr received))
+363
(address
+364
(and (string? host) (port? port 1)
+365
(guard (exn (else #f)) (quic-address host port)))))
+366
(when (and address
+367
(eq? (quic-address-family address) (quic-endpoint-family endpoint))
+368
(receive-peer? endpoint (quic-address-host address) port))
369
;; Native UDP strings retain byte_length/data; string->utf8
370
;; copies these exact bytes, including NUL and invalid UTF-8.
371
(let ((bytes (string->utf8 (car received))))
372
(when (<= 1 (bytevector-length bytes) packet-datagram-limit)
−343
(unless (quic-endpoint-connection endpoint) (admit! endpoint bytes (caddr received) now))
+373
(unless (quic-endpoint-connection endpoint)
+374
(admit! endpoint bytes address now))
375
(when (quic-endpoint-connection endpoint)
376
(set! processed? #t)
377
(quic-receive! (quic-endpoint-connection endpoint) bytes now
−347
address: (retry-address (caddr received)))
+378
address: address)
379
(reconcile-admission! endpoint now)))))
380
(loop (+ reads 1) (+ admitted (if processed? 1 0)))))))))
381
test/test-address.sgladded
@@ -0,0 +1,71 @@
+1
(import (sigil test)
+2
(sigil io)
+3
(sigil quic bytes)
+4
(sigil quic address))
+5
+6
(test-group "Canonical endpoint address keys"
+7
(test "address: existing IPv4 loopback tuple is byte compatible"
+8
(let ((address (quic-address "127.0.0.1" 443)))
+9
(assert-equal #u8(127 0 0 1 1 187) address)
+10
(assert-equal "127.0.0.1" (quic-address-host address))
+11
(assert-equal 443 (quic-address-port address))
+12
(assert-equal 'ipv4 (quic-address-family address))))
+13
(test "address: explicit documentation IPv4 address round trips"
+14
(let ((address (quic-address "192.0.2.1" 4433)))
+15
(assert-equal #u8(192 0 2 1 17 81) address)
+16
(assert-equal "192.0.2.1" (quic-address-host address))))
+17
(test "address: IPv6 cannot alias an arbitrary six-byte IPv4 tuple"
+18
(assert-false
+19
(bytes=? (quic-address "6.195.80.58" 14897)
+20
(quic-address "::1" 50000))))
+21
(test "address: equivalent IPv6 spellings share one path identity"
+22
(let ((address (quic-address "2001:0DB8:0000:0:0:0:0:1" 65535)))
+23
(assert-true (bytes=? address (quic-address "2001:db8::1" 65535)))
+24
(assert-equal "2001:db8::1" (quic-address-host address))
+25
(assert-equal 65535 (quic-address-port address))
+26
(assert-equal 'ipv6 (quic-address-family address))))
+27
(test "address: numeric IPv6 scopes remain part of path identity"
+28
(assert-true
+29
(bytes=? (quic-address "fe80::1%00042" 1)
+30
(quic-address "fe80::1%42" 1)))
+31
(assert-false
+32
(bytes=? (quic-address "fe80::1%2" 1)
+33
(quic-address "fe80::1%3" 1)))
+34
(assert-equal "fe80::1%4294967295"
+35
(quic-address-host (quic-address "fe80::1%4294967295" 1))))
+36
(test "address: decoder rejects noncanonical IPv6 text and reserved header bits"
+37
(assert-false
+38
(quic-address?
+39
(bytes-append #u8(6 1 187 0 0) (string->utf8 "FE80:0::1"))))
+40
(assert-false
+41
(quic-address? (bytes-append #u8(6 1 187 1 0) (string->utf8 "::1")))))
+42
(test "address: IPv6 framing rejects IPv4 text, NUL and invalid bytes"
+43
(for-each
+44
(lambda (body)
+45
(assert-false (quic-address? (bytes-append #u8(6 1 187 0 0) body))))
+46
(list (string->utf8 "127.0.0.1") #u8(58 58 49 0) #u8(255 128))))
+47
(test "address: both address families reject zero peer ports"
+48
(assert-false (quic-address? #u8(127 0 0 1 0 0)))
+49
(assert-false
+50
(quic-address? (bytes-append #u8(6 0 0 0 0) (string->utf8 "::1")))))
+51
(test "address: decoder bounds malformed input before copying"
+52
(for-each
+53
(lambda (value) (assert-false (quic-address? value)))
+54
(list #f "::1" #u8() #u8(6) (make-bytevector 5 0)
+55
(make-bytevector 65 0))))
+56
(test "address: constructor rejects names, bad literals and invalid ports"
+57
(for-each
+58
(lambda (host) (assert-error (quic-address host 443)))
+59
(list "localhost" "::1%eth0" "::1%4294967296" "127.0.0.999" #f))
+60
(for-each
+61
(lambda (port) (assert-error (quic-address "127.0.0.1" port)))
+62
(list 0 -1 65536 1.0 #f)))
+63
(test "address: wildcard local binds remain representable in both families"
+64
(assert-equal "0.0.0.0" (quic-address-host (quic-address "0.0.0.0" 1)))
+65
(assert-equal "::" (quic-address-host (quic-address "::" 1))))
+66
(test "address: decoded IPv6 host does not borrow mutable path-key bytes"
+67
(let* ((address (quic-address "::1" 443))
+68
(host (quic-address-host address)))
+69
(bytevector-u8-set! address (- (bytevector-length address) 1) 50)
+70
(assert-equal "::1" host)
+71
(assert-equal "::2" (quic-address-host address)))))
test/test-endpoint-bind.sgladded
@@ -0,0 +1,230 @@
+1
(import (sigil test)
+2
(sigil io)
+3
(sigil process)
+4
(sigil socket)
+5
(sigil crypto)
+6
(sigil quic bytes)
+7
(sigil quic wire)
+8
(sigil quic packet)
+9
(sigil quic address)
+10
(sigil quic connection)
+11
(sigil quic stream)
+12
(sigil quic endpoint))
+13
+14
(define ec-original #u8(51 52 53 54 55 56 57 58))
+15
(define ec-client-id #u8(61 62 63 64 65 66 67 68))
+16
+17
;; Pair: client/server connections, client/server streams, client/server
+18
;; endpoints, factory call count. Every resource is owned before later setup.
+19
(define (ec-with-pair server-bind client-bind server-target client-target procedure)
+20
(let ((key #f) (client #f) (server #f) (client-endpoint #f)
+21
(server-endpoint #f) (pair #f))
+22
(dynamic-wind
+23
(lambda () #t)
+24
(lambda ()
+25
(set! key (ecdsa-p256-generate-keypair))
+26
(set! client
+27
(make-quic-client (list #u8(113 49)) "example.test"
+28
(append (stream-transport-parameters 'client)
+29
(list (cons 32 (encode-varint 1200))))
+30
(lambda (certificates name)
+31
(and (equal? certificates (list #u8(48 1 0))) (cdr key)))
+32
destination-id: ec-original local-id: ec-client-id))
+33
(set! client-endpoint (open-quic-endpoint host: client-bind))
+34
(set! server-endpoint (open-quic-endpoint host: server-bind))
+35
(set! pair
+36
(vector client #f (make-quic-stream client) #f
+37
client-endpoint server-endpoint 0))
+38
(quic-endpoint-accept-retry! server-endpoint
+39
(lambda (original source retry)
+40
(vector-set! pair 6 (+ 1 (vector-ref pair 6)))
+41
(set! server
+42
(make-quic-server (list #u8(113 49))
+43
(append (stream-transport-parameters 'server)
+44
(list (cons 32 (encode-varint 1200))))
+45
(list #u8(48 1 0)) (car key) original source
+46
retry-source-id: retry))
+47
(vector-set! pair 1 server)
+48
(vector-set! pair 3 (make-quic-stream server))
+49
server)
+50
peer-host: client-target peer-port: (quic-endpoint-port client-endpoint))
+51
(quic-endpoint-attach! client-endpoint client
+52
(quic-endpoint-port server-endpoint) peer-host: server-target)
+53
(procedure pair))
+54
(lambda ()
+55
(when client-endpoint (quic-endpoint-close! client-endpoint))
+56
(when server-endpoint (quic-endpoint-close! server-endpoint))
+57
(when client (quic-abort! client))
+58
(when server (quic-abort! server))
+59
(when (and (pair? key) (bytevector? (car key)))
+60
(do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))
+61
(bytevector-u8-set! (car key) index 0)))))))
+62
+63
(define (ec-await pair stage predicate)
+64
(guard (exn
+65
(else
+66
(println "BIND-PUMP stage=~a exception=~a" stage exn)
+67
(raise exn)))
+68
(let loop ((round 0))
+69
(cond
+70
((predicate) #t)
+71
((or (= round 2000)
+72
(quic-endpoint-closed? (vector-ref pair 4))
+73
(quic-endpoint-closed? (vector-ref pair 5)))
+74
(println "BIND-PUMP stage=~a round=~a factories=~a" stage round (vector-ref pair 6))
+75
(for-each
+76
(lambda (index)
+77
(let ((endpoint (vector-ref pair index)))
+78
(println "BIND-ENDPOINT index=~a host=~a closed=~a failure=~a"
+79
index (quic-endpoint-host endpoint)
+80
(quic-endpoint-closed? endpoint) (quic-endpoint-failure endpoint))))
+81
(list 4 5))
+82
(for-each
+83
(lambda (index)
+84
(let ((connection (vector-ref pair index)))
+85
(when connection
+86
(println "BIND-CONNECTION index=~a complete=~a confirmed=~a closed=~a failure=~a"
+87
index (quic-complete? connection) (quic-confirmed? connection)
+88
(quic-closed? connection) (quic-failure connection)))))
+89
(list 0 1))
+90
#f)
+91
(else
+92
(quic-endpoint-step! (vector-ref pair 4))
+93
(quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
+94
(loop (+ round 1)))))))
+95
+96
(define (ec-require stage condition)
+97
(unless condition (error (string-append "Configured endpoint fixture: " stage)))
+98
#t)
+99
+100
(define (ec-exchange server-bind client-bind server-target client-target)
+101
(ec-with-pair server-bind client-bind server-target client-target
+102
(lambda (pair)
+103
(and
+104
(ec-await pair 'confirmation
+105
(lambda ()
+106
(and (vector-ref pair 1)
+107
(quic-confirmed? (vector-ref pair 0))
+108
(quic-confirmed? (vector-ref pair 1)))))
+109
(let ((client (vector-ref pair 0)) (server (vector-ref pair 1))
+110
(client-stream (vector-ref pair 2)) (server-stream (vector-ref pair 3))
+111
(request #u8()) (response #u8()) (client-datagram #f) (server-datagram #f))
+112
(and
+113
(ec-require "single Retry admission" (= 1 (vector-ref pair 6)))
+114
(ec-require "server actual peer"
+115
(bytes=?
+116
(quic-address client-target (quic-endpoint-port (vector-ref pair 4)))
+117
(quic-endpoint-peer-address (vector-ref pair 5))))
+118
(ec-require "client actual peer"
+119
(bytes=?
+120
(quic-address server-target (quic-endpoint-port (vector-ref pair 5)))
+121
(quic-endpoint-peer-address (vector-ref pair 4))))
+122
(ec-require "client binary write"
+123
(= 4 (quic-stream-write! client-stream #u8(0 255 128 4) fin?: #t)))
+124
(ec-require "client DATAGRAM queue" (quic-send-datagram! client #u8(255 1)))
+125
;; The client's STREAM must arrive before the server can write
+126
;; on that client-initiated bidirectional stream.
+127
(ec-await pair 'request
+128
(lambda ()
+129
(set! request (bytes-append request (quic-stream-read! server-stream)))
+130
(unless server-datagram (set! server-datagram (quic-read-datagram! server)))
+131
(and (quic-stream-eof? server-stream) server-datagram)))
+132
(ec-require "server binary write"
+133
(= 5 (quic-stream-write! server-stream #u8(255 0 128 7 8) fin?: #t)))
+134
(ec-require "server DATAGRAM queue" (quic-send-datagram! server #u8(0 2)))
+135
(ec-await pair 'response
+136
(lambda ()
+137
(set! response (bytes-append response (quic-stream-read! client-stream)))
+138
(unless client-datagram (set! client-datagram (quic-read-datagram! client)))
+139
(and (quic-stream-eof? client-stream) client-datagram)))
+140
(bytes=? #u8(0 255 128 4) request)
+141
(bytes=? #u8(255 0 128 7 8) response)
+142
(bytes=? #u8(0 2) client-datagram)
+143
(bytes=? #u8(255 1) server-datagram)))))))
+144
+145
(define (ec-bound? host family)
+146
(let ((endpoint #f))
+147
(dynamic-wind
+148
(lambda () #t)
+149
(lambda ()
+150
(guard (exn (else #f))
+151
(set! endpoint (open-quic-endpoint host: host))
+152
(and (equal? host (quic-endpoint-host endpoint))
+153
(eq? family (quic-endpoint-family endpoint))
+154
(<= 1 (quic-endpoint-port endpoint) 65535))))
+155
(lambda () (when endpoint (quic-endpoint-close! endpoint))))))
+156
+157
(define (ec-initial)
+158
(let* ((keys (initial-keys ec-original 'client))
+159
(header (encode-packet-header 'initial ec-original ec-client-id #u8() 0 4 17
+160
length-width: 2))
+161
(payload
+162
(bytes-append #u8(6 0 1 1)
+163
(make-bytevector (- 1200 (bytevector-length header) 16 4) 0))))
+164
(dynamic-wind
+165
(lambda () #t)
+166
(lambda ()
+167
(seal-quic-packet keys 'initial ec-original ec-client-id 0 payload length-width: 2))
+168
(lambda () (discard-packet-keys! keys)))))
+169
+170
(define (ec-restricted-host?)
+171
(let ((endpoint #f) (pinned #f) (other #f) (calls 0))
+172
(dynamic-wind
+173
(lambda () #t)
+174
(lambda ()
+175
(guard (exn (else #f))
+176
(set! endpoint (open-quic-endpoint))
+177
(set! pinned (udp-socket 'ipv4))
+178
(set! other (udp-socket 'ipv4))
+179
(and pinned other
+180
(udp-bind pinned 0 "127.0.0.2")
+181
(let ((port (cadr (socket-local-address pinned))))
+182
(and
+183
(udp-bind other port "127.0.0.3")
+184
(socket-set-non-blocking! other #t)
+185
(begin
+186
(quic-endpoint-accept-retry! endpoint
+187
(lambda (original source retry)
+188
(set! calls (+ calls 1))
+189
(error "endpoint bind fixture: unexpected admission"))
+190
peer-host: "127.0.0.2" peer-port: port)
+191
(= 1200 (udp-send other (ec-initial) "127.0.0.1"
+192
(quic-endpoint-port endpoint))))
+193
(begin
+194
(do ((round 0 (+ round 1))) ((= round 5))
+195
(quic-endpoint-step! endpoint wait-ms: 1))
+196
(and (= calls 0) (not (quic-endpoint-connection endpoint))
+197
(not (quic-endpoint-closed? endpoint))
+198
(not (udp-receive other 65535)))))))))
+199
(lambda ()
+200
(when other (socket-close other))
+201
(when pinned (socket-close pinned))
+202
(when endpoint (quic-endpoint-close! endpoint))))))
+203
+204
(test-group "Configured QUIC UDP endpoints"
+205
(test "endpoint-bind: IPv4 alias exposes the actual bound host and family"
+206
(assert-true (ec-bound? "127.0.0.2" 'ipv4)))
+207
(test "endpoint-bind: IPv6 loopback exposes the actual bound host and family"
+208
(assert-true (ec-bound? "::1" 'ipv6)))
+209
(test "endpoint-bind: invalid numeric hosts and ports are rejected"
+210
(for-each
+211
(lambda (host) (assert-error (open-quic-endpoint host: host)))
+212
(list #f "localhost" "127.0.0.999" "::1%eth0"))
+213
(assert-error (open-quic-endpoint host: "::1" port: -1))
+214
(assert-error (open-quic-endpoint host: "127.0.0.2" port: 65536)))
+215
(test "endpoint-bind: distinct IPv4 addresses complete Retry and binary transfer"
+216
(assert-true (ec-exchange "127.0.0.1" "127.0.0.2" "127.0.0.1" "127.0.0.2")))
+217
(test "endpoint-bind: IPv6 completes Retry and binary transfer"
+218
(assert-true (ec-exchange "::1" "::1" "::1" "::1")))
+219
(test "endpoint-bind: IPv4 wildcard returns traffic to the actual peer"
+220
(assert-true (ec-exchange "0.0.0.0" "127.0.0.2" "127.0.0.1" "127.0.0.2")))
+221
(test "endpoint-bind: IPv6 wildcard completes Retry and transfer"
+222
(assert-true (ec-exchange "::" "::1" "::1" "::1")))
+223
(test "endpoint-bind: configured peer host restricts admission at the pinned port"
+224
(assert-true (ec-restricted-host?)))
+225
(test "endpoint-bind: assigned IPv4 interface completes Retry and transfer"
+226
(let ((host (or (getenv "QUIC_TEST_INTERFACE_IPV4") "127.0.0.2")))
+227
(assert-true (ec-exchange host host host host))))
+228
(test "endpoint-bind: assigned IPv6 interface completes Retry and transfer"
+229
(let ((host (or (getenv "QUIC_TEST_INTERFACE_IPV6") "::1")))
+230
(assert-true (ec-exchange host host host host)))))