code wiki / (root) / nx_quic_wire.nx

nx_quic_wire.nx source

↩ module page · 93 lines · 4313 B

1// nx_quic_wire.nx -- RUNG 1 of the sovereign QUIC datagram transport (operator 2026-06-23: "yes i even 2// want quic"). The resilience S-class exceed (FEC that BEATS ARQ) only materializes over an UNRELIABLE 3// datagram transport; browsers reach that only via WebTransport = QUIC. So we build QUIC ourselves, 4// sovereign, rung by rung, each gated against the real RFCs. This is the foundation EVERYTHING in QUIC 5// rests on: variable-length integers (RFC 9000 sec 16) + the DATAGRAM frame (RFC 9221) that carries the 6// unreliable video+FEC payload. Next rungs: packet headers/number, CRYPTO+TLS1.3 handshake, AEAD packet 7// protection, ACK/loss/congestion, the H3/WebTransport session, then wire to the relay + FEC. 8// No float. Pure integer wire codec. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10const K_MAGIC_16384: i64 = 16384 11const K_MAGIC_1073741824: i64 = 1073741824 12const K_MAGIC_4611686018427387904: i64 = 4611686018427387904 13 14// ---- QUIC varint (RFC 9000 sec 16): 2 MSBs of byte0 pick the length (00=1,01=2,10=4,11=8), big-endian. 15// encode val in the SMALLEST form. returns bytes written (1/2/4/8); 0 if val<0 or val>=2^62 (out of range). 16func quic_varint_encode(out: *u8, val: i64) -> i64 { 17 if val < 0 { return 0 } 18 if val < 64 { // 2^6 -> 1 byte, prefix 00 19 out[0] = val as u8 20 return 1 21 } 22 if val < K_MAGIC_16384 { // 2^14 -> 2 bytes, prefix 01 23 out[0] = (0x40 | ((val >> 8) & 0x3f)) as u8 24 out[1] = (val & 0xff) as u8 25 return 2 26 } 27 if val < K_MAGIC_1073741824 { // 2^30 -> 4 bytes, prefix 10 28 out[0] = (0x80 | ((val >> 24) & 0x3f)) as u8 29 out[1] = ((val >> 16) & 0xff) as u8 30 out[2] = ((val >> 8) & 0xff) as u8 31 out[3] = (val & 0xff) as u8 32 return 4 33 } 34 if val >= K_MAGIC_4611686018427387904 { return 0 } // 2^62 -> out of QUIC varint range 35 out[0] = (0xc0 | ((val >> 56) & 0x3f)) as u8 // 2^62 -> 8 bytes, prefix 11 36 out[1] = ((val >> 48) & 0xff) as u8 37 out[2] = ((val >> 40) & 0xff) as u8 38 out[3] = ((val >> 32) & 0xff) as u8 39 out[4] = ((val >> 24) & 0xff) as u8 40 out[5] = ((val >> 16) & 0xff) as u8 41 out[6] = ((val >> 8) & 0xff) as u8 42 out[7] = (val & 0xff) as u8 43 return 8 44} 45 46// decode a QUIC varint from buf[0..n). writes the value to out_val[0]. returns bytes consumed (1/2/4/8), 47// or 0 if the buffer is too short for the indicated length. 48func quic_varint_decode(buf: *u8, n: i64, out_val: *i64) -> i64 { 49 if n < 1 { return 0 } 50 let b0: i64 = buf[0] as i64 51 let pfx: i64 = (b0 >> 6) & 3 52 if pfx == 0 { out_val[0] = b0 & 0x3f; return 1 } 53 if pfx == 1 { 54 if n < 2 { return 0 } 55 out_val[0] = ((b0 & 0x3f) << 8) | (buf[1] as i64) 56 return 2 57 } 58 if pfx == 2 { 59 if n < 4 { return 0 } 60 out_val[0] = ((b0 & 0x3f) << 24) | ((buf[1] as i64) << 16) | ((buf[2] as i64) << 8) | (buf[3] as i64) 61 return 4 62 } 63 if n < 8 { return 0 } 64 out_val[0] = ((b0 & 0x3f) << 56) | ((buf[1] as i64) << 48) | ((buf[2] as i64) << 40) | ((buf[3] as i64) << 32) | ((buf[4] as i64) << 24) | ((buf[5] as i64) << 16) | ((buf[6] as i64) << 8) | (buf[7] as i64) 65 return 8 66} 67 68// DATAGRAM frame (RFC 9221) type 0x31 (with Length): [0x31][Length varint][payload]. carries the 69// unreliable video+FEC bytes. returns total frame length written. 70func quic_datagram_encode(out: *u8, payload: *u8, plen: i64) -> i64 { 71 out[0] = 0x31 as u8 72 let lv: i64 = quic_varint_encode((out as i64 + 1) as *u8, plen) 73 if lv == 0 { return 0 } 74 var i: i64 = 0 75 while i < plen { out[1 + lv + i] = payload[i]; i = i + 1 } 76 return 1 + lv + plen 77} 78 79// parse a DATAGRAM frame at buf[0..n). writes payload offset to poff[0]. returns payload length, 80// or -1 if not a 0x31 DATAGRAM frame / truncated. 81func quic_datagram_parse(buf: *u8, n: i64, poff: *i64) -> i64 { 82 if n < 1 { return 0 - 1 } 83 if (buf[0] as i64) != 0x31 { return 0 - 1 } 84 let lvbox: *i64 = sys_mmap(8) as *i64 85 let lv: i64 = quic_varint_decode((buf as i64 + 1) as *u8, n - 1, lvbox) 86 if lv == 0 { return 0 - 1 } 87 let plen: i64 = lvbox[0] 88 if 1 + lv + plen > n { return 0 - 1 } 89 poff[0] = 1 + lv 90 return plen 91} 92 93func main() -> i64 { return 0 }