AtlatestRepositorysigil-http3

sigil-http3 / tree / tools / tracepunch.sgl

1;;; Spike-only sender: the endpoint's existing socket remains the sole owner.
2;;; Imported only by the private Linux build patch, never by released code.
3(define-library (sigil http3 interop punch)
4 (import (sigil io)
5 (sigil fs)
6 (sigil process)
7 (sigil time)
8 (sigil socket)
9 (sigil quic endpoint)
10 (sigil http3 interop observations))
11 (export spike-punch-step!)
12 (begin
13 (define directory (getenv "HTTP3_PUNCH_DIRECTORY"))
14 (define ports '(40000 40001 40002))
15 (define attempted? #f)
16 (define finished? #f)
17 (define owner #f)
18 (define target #f)
19 (define nonce #f)
20 (define seconds 0)
21 (define started 0)
22 (define deadline 0)
23 (define next-send 0)
24 (define count 0)
26 (define (timestamp)
27 (let ((now (current-second)))
28 (string-append (format-date now #t) "T" (format-time now #t) "Z")))
30 (define (write-receipt name reason)
31 (let* ((path (string-append directory "/" name ".json"))
32 (temporary (string-append path ".next")))
33 (write-file-string temporary
34 (string-append "{\"schema\":1,\"nonce\":" (interop-json-token nonce)
35 ",\"utc\":" (interop-json-token (timestamp))
36 ",\"reason\":" (interop-json-token reason)
37 ",\"sourceHost\":" (interop-json-token (quic-endpoint-host owner))
38 ",\"sourcePort\":" (number->string (quic-endpoint-port owner))
39 ",\"seconds\":" (number->string seconds)
40 ",\"packets\":" (number->string count)
41 ",\"elapsedMs\":"
42 (number->string
43 (quotient (* 1000 (- (current-jiffy) started)) (jiffies-per-second)))
44 "}"))
45 (rename-file temporary path)))
47 (define (finish! reason)
48 (unless finished?
49 (set! finished? #t)
50 (println "HTTP3-PUNCH time=~a event=finished reason=~a packets=~a"
51 (timestamp) reason count)
52 (write-receipt "finished" reason)))
54 (define (admit! endpoint path)
55 (unless (<= 1 (file-size path) 1024)
56 (error "Punch command exceeds bound"))
57 (let* ((command
58 (call-with-input-file path
59 (lambda (input)
60 (let ((value (read input)))
61 (unless (eof-object? (read input))
62 (error "Punch expects one command"))
63 value))))
64 (canonical
65 (and (list? command) (= (length command) 4)
66 (equal? (car command) 1)
67 (string? (caddr command))
68 (socket-normalize-address (caddr command)))))
69 (unless (and canonical
70 (string? (cadr command)) (<= 1 (string-length (cadr command)) 64)
71 (integer? (cadddr command)) (exact? (cadddr command))
72 (<= 1 (cadddr command) 180)
73 (eq? (quic-endpoint-family endpoint) 'ipv6)
74 (or (and (equal? (getenv "HTTP3_PUNCH_TEST_LOOPBACK") "1")
75 (equal? canonical "::1")
76 (equal? (quic-endpoint-host endpoint) "::1"))
77 (and (= (quic-endpoint-port endpoint) 43443)
78 (> (string-length canonical) 1)
79 (memv (string-ref canonical 0) '(#\2 #\3))
80 (not (equal? canonical (quic-endpoint-host endpoint))))))
81 (error "Invalid bounded same-socket punch command"))
82 (set! attempted? #t)
83 (set! owner endpoint)
84 (set! target canonical)
85 (set! nonce (cadr command))
86 (set! seconds (cadddr command))
87 (set! started (current-jiffy))
88 (set! deadline (+ started (* seconds (jiffies-per-second))))
89 (set! next-send started)
90 (println "HTTP3-PUNCH time=~a event=started seconds=~a source-port=~a"
91 (timestamp) seconds (quic-endpoint-port endpoint))
92 (write-receipt "started" "same-listening-socket")))
94 (define (spike-punch-step! endpoint)
95 (when directory
96 (let ((command (string-append directory "/command.scm")))
97 (when (and (not attempted?) (file-exists? command))
98 (admit! endpoint command)))
99 (when (and attempted? (not finished?))
100 (cond
101 ((or (not (eq? endpoint owner)) (quic-endpoint-closed? owner))
102 (finish! "listener-ended"))
103 ((file-exists? (string-append directory "/stop"))
104 (finish! "controller-stop"))
105 ((or (>= (current-jiffy) deadline) (>= count (* 3 seconds)))
106 (finish! "window-expired"))
107 ((>= (current-jiffy) next-send)
108 ;; Schedule from now: delayed steps never produce catch-up bursts.
109 (set! next-send (+ (current-jiffy) (jiffies-per-second)))
110 (for-each
111 (lambda (port)
112 (when (and (not finished?) (< (current-jiffy) deadline))
113 (let* ((bytes #u8(83 71 80 50 0 0 0 0))
114 (sent (quic-spike-punch-send! owner target port bytes)))
115 (set! count (+ count 1))
116 (println "HTTP3-PUNCH time=~a event=send sequence=~a port=~a bytes=8 result=~a"
117 (timestamp) count port sent)
118 (unless (and sent (= sent 8))
119 (finish! "send-failed")))))
120 ports)))
121 (flush-output-port (current-output-port)))))))