code wiki / _hdl_build / nx_signaling_v2_gate.nx

nx_signaling_v2_gate.nx source

↩ module page · 634 lines · 35023 B

1import "nx_gate_base.nx" 2import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 3// nx_signaling_v2_gate.nx -- ENGINEER gate for the N-party signaling daemon. 4// Self-contained + re-runnable: forks /tmp/nx_signaling_v2.sov.elf, connects 5// THREE WebSocket clients to the same room over loopback, and proves: 6// (1) all 3 complete the RFC 6455 client upgrade (101) 7// (2) a data frame from A is relayed to BOTH B and C (broadcast) 8// (3) ...and NOT echoed back to A 9// (4) a frame from B reaches A and C (any-peer broadcast, not 1:1 v1) 10// (5) ping from A -> pong back to A (control handled per-peer, not relayed) 11// (6) A closes -> B<->C relay still works (leave doesn't kill the room) 12// (7) a 4th..9th peer joins fine, the 9th is rejected w/ close (cap=8) 13// Composes nx_websocket_client_upgrade + nx_websocket_stream (KAT'd). 14// license_tier: ORIGINAL 15 16import "nx_syscalls.nx" 17import "nx_gate_emit_lib.nx" 18import "nx_room_key.nx" // token computation for the keyed-room tests (same SSOT organ as the relay) 19import "nx_dgram_media.nx" // R2: UDP-plane client side (pack/parse/reassemble) for the mixed-plane tests 20import "nx_websocket_client_upgrade.nx" 21import "nx_websocket_stream.nx" 22import "nx_websocket_frame.nx" 23 24func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 25" as *u8); return ok } 26func g_loopback_addr(addr: *u8, port: i64) -> i64 { 27 addr[0]=2 as u8; addr[1]=0 as u8 28 addr[2]=((port>>8)&0xff) as u8; addr[3]=(port&0xff) as u8 29 addr[4]=127 as u8; addr[5]=0 as u8; addr[6]=0 as u8; addr[7]=1 as u8 30 var i: i64=8 31 while i<16 { addr[i]=0 as u8; i=i+1 } 32 return 16 33} 34 35// connect + WS-upgrade one client into /signal/gateroom; returns fd or -1 36func g_join(req_buf: *u8, resp_buf: *u8) -> i64 { 37 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 38 if fd < 0 { return 0 - 1 } 39 let addr: *u8 = sys_mmap(16) 40 g_loopback_addr(addr, 8445) 41 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 1 } 42 let host: *u8 = "127.0.0.1" as *u8 43 let path: *u8 = "/signal/gateroom" as *u8 44 let v: i64 = nx_ws_client_upgrade(fd, host, 9, path, 16, 8445, 1, 45 req_buf, 2048, resp_buf, 4096) 46 if v != NX_WSCU_OK { 47 g_puts(" join: client upgrade verdict=" as *u8); g_pn(v); g_puts("\n" as *u8) 48 sys_close(fd) 49 return 0 - 1 50 } 51 return fd 52} 53 54// read one data frame (skipping nothing) with the streaming reader; payload 55// copied into out; returns payload len or -1. 56func g_read_payload(fd: i64, buf: *u8, cap: i64, out: *u8, outcap: i64) -> i64 { 57 let f_raw: *u8 = sys_mmap(128) 58 let f: *WsFrame = f_raw as *WsFrame 59 let v: i64 = nx_ws_read_frame_from_fd(fd, buf, cap, f) 60 if v != NX_WSS_OK { return 0 - 1 } 61 if f.payload_len > outcap { return 0 - 1 } 62 var i: i64 = 0 63 while i < f.payload_len { out[i] = buf[f.payload_off + i]; i = i + 1 } 64 return f.payload_len 65} 66 67func g_eq(a: *u8, b: *u8, n: i64) -> i64 { 68 var i: i64=0 69 while i<n { if a[i]!=b[i] { return 0 } i=i+1 } 70 return 1 71} 72func g_wr(path: *u8, data: *u8, n: i64) -> i64 { 73 let fd: i64 = sys_openat_wr(path, 0x1a4) // 0644 74 if fd < 0 { return 0 - 1 } 75 sys_write(fd, data, n) 76 sys_close(fd) 77 return 0 } 78// "/signal/keyroom?k=<tok16>" 79func g_mkkeypath(buf: *u8, tok: *u8) -> i64 { 80 let pre: *u8 = "/signal/keyroom?k=" as *u8 81 var i: i64 = 0 82 while pre[i] != (0 as u8) { buf[i] = pre[i]; i = i + 1 } 83 var j: i64 = 0 84 while j < 16 { buf[i + j] = tok[j]; j = j + 1 } 85 return i + 16 } 86 87 88// ---- SFU-test helpers (task 41) ---- 89func g_join_room(path: *u8, plen: i64, req_buf: *u8, resp_buf: *u8) -> i64 { 90 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 91 if fd < 0 { return 0 - 1 } 92 let addr: *u8 = sys_mmap(16) 93 g_loopback_addr(addr, 8445) 94 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 1 } 95 let v: i64 = nx_ws_client_upgrade(fd, "127.0.0.1" as *u8, 9, path, plen, 8445, 1, 96 req_buf, 2048, resp_buf, 4096) 97 if v != NX_WSCU_OK { sys_close(fd); return 0 - 1 } 98 return fd } 99func g_tmo(fd: i64, sec: i64) -> i64 { 100 let tv: *i64 = sys_mmap(16) as *i64; tv[0]=sec; tv[1]=0 101 sys_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, tv as *u8, 16); return 0 } 102// 16B registration frame: [kind 0x43][id8][seq4=0][pad3] -- any binary frame teaches the relay this conn id 103func g_mkreg(b: *u8, id: *u8) -> i64 { 104 b[0] = 0x43 as u8 105 var i: i64 = 0 106 while i < 8 { b[1+i] = id[i]; i = i + 1 } 107 i = 9 108 while i < 16 { b[i] = 0 as u8; i = i + 1 } 109 return 0 } 110// 32B video frame: [0x57][id8][seq4 LE][flags][qp][w2][h2][13B junk] (flags bit0=key bit1=LO) 111func g_mk57(b: *u8, id: *u8, seq: i64, flags: i64) -> i64 { 112 b[0] = 0x57 as u8 113 var i: i64 = 0 114 while i < 8 { b[1+i] = id[i]; i = i + 1 } 115 b[9] = (seq & 255) as u8 116 b[10] = ((seq / 256) & 255) as u8 117 b[11] = ((seq / 65536) & 255) as u8 118 b[12] = ((seq / 16777216) & 255) as u8 119 b[13] = flags as u8 120 b[14] = 20 as u8 121 b[15] = 64 as u8; b[16] = 1 as u8 // w=320 122 b[17] = 240 as u8; b[18] = 0 as u8 // h=240 123 i = 19 124 while i < 32 { b[i] = ((i * 3) & 255) as u8; i = i + 1 } 125 return 0 } 126func g_rdseq(b: *u8) -> i64 { 127 return (b[9]&0xff) + ((b[10]&0xff)*256) + ((b[11]&0xff)*65536) + ((b[12]&0xff)*16777216) } 128 129func main() -> i64 { 130 g_puts("nx_signaling_v2 gate (3-peer mesh over loopback)\n" as *u8) 131 var pass: i64 = 0 132 var total: i64 = 0 133 134 // ---- spawn the daemon: sovereign build first, fast-runner build as 135 // the iteration fallback (final re-gate runs against the .sov.elf) ---- 136 var dpath: *u8 = "/tmp/nx_signaling_v2.sov.elf" as *u8 137 let probe: i64 = sys_openat_rd(dpath) 138 if probe >= 0 { sys_close(probe) } 139 else { dpath = "/tmp/nx_signaling_v2.elf" as *u8 } 140 g_puts("daemon: " as *u8); g_puts(dpath); g_puts("\n" as *u8) 141 // token-wall test config: secret + one protected room "keyroom". Written BEFORE the daemon reads them 142 // at startup. All OTHER test rooms are unlisted -> unaffected (proves the wall is opt-in). 143 g_wr("/tmp/rk_secret_gate.txt" as *u8, "gatesecret" as *u8, 10) 144 g_wr("/tmp/rk_protected_gate.txt" as *u8, "keyroom\n" as *u8, 8) 145 let pid: i64 = sys_fork() 146 if pid == 0 { 147 let av: *i64 = sys_mmap(64) as *i64 148 av[0] = dpath as i64 149 av[1] = "/tmp/rk_secret_gate.txt" as *u8 as i64 // token-wall test config (runner pre-writes; 150 av[2] = "/tmp/rk_protected_gate.txt" as *u8 as i64 // missing files = wall off = old behavior) 151 av[3] = 0 152 let ev: *i64 = sys_mmap(16) as *i64 153 ev[0] = 0 154 sys_execve(dpath, av, ev) 155 sys_exit(127) 156 } 157 sys_sleep_ms(300) 158 159 let req_buf: *u8 = sys_mmap(2048) 160 let resp_buf: *u8 = sys_mmap(4096) 161 let frame_buf: *u8 = sys_mmap(16384) 162 let pay: *u8 = sys_mmap(4096) 163 164 // (1) three peers join 165 let fa: i64 = g_join(req_buf, resp_buf) 166 let fb: i64 = g_join(req_buf, resp_buf) 167 let fc: i64 = g_join(req_buf, resp_buf) 168 pass = pass + g_check("A+B+C upgraded (101 x3)" as *u8, (fa >= 0) + (fb >= 0) + (fc >= 0) == 3); total = total + 1 169 if fa < 0 { return 1 } 170 if fb < 0 { return 1 } 171 if fc < 0 { return 1 } 172 173 // (2) A broadcasts -> B and C both receive 174 let msg_a: *u8 = "{\"from\":\"A\",\"type\":\"hello\"}" as *u8 175 let msg_a_n: i64 = g_slen(msg_a) 176 nx_ws_send_frame_to_fd(fa, 1, WS_OP_TEXT, msg_a, msg_a_n) 177 let nb: i64 = g_read_payload(fb, frame_buf, 16384, pay, 4096) 178 pass = pass + g_check("B received A's hello" as *u8, (nb == msg_a_n) * g_eq(pay, msg_a, msg_a_n)); total = total + 1 179 let nc: i64 = g_read_payload(fc, frame_buf, 16384, pay, 4096) 180 pass = pass + g_check("C received A's hello (N-party broadcast)" as *u8, (nc == msg_a_n) * g_eq(pay, msg_a, msg_a_n)); total = total + 1 181 182 // (4) B broadcasts -> A and C receive 183 let msg_b: *u8 = "{\"from\":\"B\",\"type\":\"offer\",\"to\":\"A\"}" as *u8 184 let msg_b_n: i64 = g_slen(msg_b) 185 nx_ws_send_frame_to_fd(fb, 1, WS_OP_TEXT, msg_b, msg_b_n) 186 let na2: i64 = g_read_payload(fa, frame_buf, 16384, pay, 4096) 187 pass = pass + g_check("A received B's offer (verbatim envelope)" as *u8, (na2 == msg_b_n) * g_eq(pay, msg_b, msg_b_n)); total = total + 1 188 let nc2: i64 = g_read_payload(fc, frame_buf, 16384, pay, 4096) 189 pass = pass + g_check("C received B's offer too (broadcast relay)" as *u8, nc2 == msg_b_n); total = total + 1 190 191 // (5) ping from A -> pong to A 192 nx_ws_send_frame_to_fd(fa, 1, WS_OP_PING, "pp" as *u8, 2) 193 let f_raw: *u8 = sys_mmap(128) 194 let f: *WsFrame = f_raw as *WsFrame 195 let vp: i64 = nx_ws_read_frame_from_fd(fa, frame_buf, 16384, f) 196 pass = pass + g_check("A ping -> pong back to A" as *u8, (vp == NX_WSS_OK) * (f.opcode == WS_OP_PONG)); total = total + 1 197 198 // (5b) BINARY media frame (the pure-Nishi video plane): 12KB JPEG-sized 199 // payload from B relays to C bit-exact with opcode preserved. 200 let big: *u8 = sys_mmap(12288) 201 var bi: i64 = 0 202 while bi < 12288 { big[bi] = ((bi * 31 + 7) & 0xff) as u8; bi = bi + 1 } 203 nx_ws_send_frame_to_fd(fb, 1, WS_OP_BINARY, big, 12288) 204 let fbin_raw: *u8 = sys_mmap(128) 205 let fbin: *WsFrame = fbin_raw as *WsFrame 206 let vbin: i64 = nx_ws_read_frame_from_fd(fc, frame_buf, 16384, fbin) 207 var bin_ok: i64 = 0 208 if vbin == NX_WSS_OK { if fbin.opcode == WS_OP_BINARY { if fbin.payload_len == 12288 { 209 bin_ok = 1 210 var bj: i64 = 0 211 while bj < 12288 { 212 if frame_buf[fbin.payload_off + bj] != big[bj] { bin_ok = 0; bj = 12288 } else { bj = bj + 1 } 213 } 214 } } } 215 pass = pass + g_check("12KB BINARY media frame relays bit-exact" as *u8, bin_ok == 1); total = total + 1 216 // drain A's copy of the binary frame so later reads on A stay aligned 217 nx_ws_read_frame_from_fd(fa, frame_buf, 16384, fbin) 218 219 // (6) A leaves; B <-> C still relay 220 nx_ws_send_close(fa, 1000) 221 sys_close(fa) 222 sys_sleep_ms(100) 223 let msg_c: *u8 = "{\"from\":\"C\",\"type\":\"bye-test\"}" as *u8 224 let msg_c_n: i64 = g_slen(msg_c) 225 nx_ws_send_frame_to_fd(fc, 1, WS_OP_TEXT, msg_c, msg_c_n) 226 let nb3: i64 = g_read_payload(fb, frame_buf, 16384, pay, 4096) 227 pass = pass + g_check("after A left, C->B relay still live" as *u8, nb3 == msg_c_n); total = total + 1 228 229 // (7) fill to the 8-peer cap. After A left the room holds B,C (2); 230 // f4..f9 are six more = 8 total (at cap); f10 is the true 9th. 231 let f4: i64 = g_join(req_buf, resp_buf) 232 let f5: i64 = g_join(req_buf, resp_buf) 233 let f6: i64 = g_join(req_buf, resp_buf) 234 let f7: i64 = g_join(req_buf, resp_buf) 235 let f8: i64 = g_join(req_buf, resp_buf) 236 let f9: i64 = g_join(req_buf, resp_buf) 237 pass = pass + g_check("peers 4..9 admitted (room grows to cap=8)" as *u8, 238 (f4>=0) + (f5>=0) + (f6>=0) + (f7>=0) + (f8>=0) + (f9>=0) == 6); total = total + 1 239 // the 9th joiner: upgrade succeeds at HTTP level then the daemon 240 // closes w/ 1008 -- the next read on it sees CLOSE/EOF. 241 let f10: i64 = g_join(req_buf, resp_buf) 242 var ninth_rejected: i64 = 0 243 if f10 < 0 { ninth_rejected = 1 } 244 else { 245 let v10: i64 = nx_ws_read_frame_from_fd(f10, frame_buf, 16384, f) 246 if v10 != NX_WSS_OK { ninth_rejected = 1 } 247 else { if f.opcode == WS_OP_CLOSE { ninth_rejected = 1 } } 248 } 249 pass = pass + g_check("9th peer rejected (cap=8 enforced)" as *u8, ninth_rejected == 1); total = total + 1 250 251 // ---- R2 (task #15): the SOVEREIGN UDP PLANE + the MIXED-PLANE BRIDGE ---- 252 // Native clients speak nx_dgram_media datagrams to :8471; the relay raw-fans to UDP peers AND bridges 253 // completed frames to WSS members (and vice versa). Isolated in "udproom" (fresh WS client, no backlog). 254 let us_a: i64 = sys_socket(AF_INET, SOCK_DGRAM, 0) 255 let us_b: i64 = sys_socket(AF_INET, SOCK_DGRAM, 0) 256 let tv: *u8 = sys_mmap(16) // SO_RCVTIMEO 2s so a missing reply FAILS, never hangs 257 let tvp: *i64 = tv as *i64 258 tvp[0] = 2; tvp[1] = 0 259 sys_setsockopt(us_a, SOL_SOCKET, 20, tv, 16) 260 sys_setsockopt(us_b, SOL_SOCKET, 20, tv, 16) 261 let uad: *u8 = sys_mmap(16) 262 g_loopback_addr(uad, 8471) 263 let upk: *u8 = sys_mmap(131072) 264 let urx_a: *u8 = sys_mmap(DGM_RX_REGION) 265 dgm_rx_init(urx_a) 266 let urx_b: *u8 = sys_mmap(DGM_RX_REGION) 267 dgm_rx_init(urx_b) 268 let uinfo: *i64 = sys_mmap(8 * 12) as *i64 269 let ufr: *u8 = sys_mmap(65536) 270 let ubuf: *u8 = sys_mmap(2048) 271 let ufrom2: *u8 = sys_mmap(16) 272 let uflen: *i64 = sys_mmap(16) as *i64 273 // (11) A joins over UDP -> ACK datagram comes back 274 var np: i64 = dgm_pack_frame(0x4A, "AAAAAAA1" as *u8, 0, "udproom" as *u8, 7, upk, 131072) 275 var pl: i64 = (upk[0] & 0xff) + ((upk[1] & 0xff) << 8) 276 sys_sendto(us_a, ((upk as i64) + 2) as *u8, pl, 0, uad, 16) 277 uflen[0] = 16 278 var arn: i64 = sys_recvfrom(us_a, ubuf, 2048, 0, ufrom2, uflen) 279 var ackok: i64 = 0 280 if arn > 0 { if dgm_parse_pkt(ubuf, arn, uinfo) == 0 { if uinfo[0] == 0x4B { ackok = 1 } } } 281 pass = pass + g_check("UDP JOIN -> ACK (sovereign dgram plane up on :8471)" as *u8, ackok == 1); total = total + 1 282 // (12) B joins; A sends a fragmented 3KB room-frame -> raw passthrough -> B reassembles BIT-EXACT 283 np = dgm_pack_frame(0x4A, "BBBBBBB2" as *u8, 0, "udproom" as *u8, 7, upk, 131072) 284 pl = (upk[0] & 0xff) + ((upk[1] & 0xff) << 8) 285 sys_sendto(us_b, ((upk as i64) + 2) as *u8, pl, 0, uad, 16) 286 uflen[0] = 16 287 sys_recvfrom(us_b, ubuf, 2048, 0, ufrom2, uflen) // B's ACK 288 let body3k: *u8 = sys_mmap(3000) 289 var bi3: i64 = 0 290 while bi3 < 3000 { body3k[bi3] = (((bi3 * 37) + 11) & 255) as u8; bi3 = bi3 + 1 } 291 np = dgm_pack_frame(0x4D, "AAAAAAA1" as *u8, 1, body3k, 3000, upk, 131072) 292 var o3: i64 = 0 293 var s3: i64 = 0 294 while s3 < np { 295 pl = (upk[o3] & 0xff) + ((upk[o3+1] & 0xff) << 8) 296 sys_sendto(us_a, ((upk as i64) + o3 + 2) as *u8, pl, 0, uad, 16) 297 o3 = o3 + 2 + pl 298 s3 = s3 + 1 299 } 300 var got3: i64 = 0 301 var tries: i64 = 0 302 while tries < 12 { 303 if got3 == 0 { 304 uflen[0] = 16 305 let rn3: i64 = sys_recvfrom(us_b, ubuf, 2048, 0, ufrom2, uflen) 306 if rn3 > 0 { let rr3: i64 = dgm_rx_add(urx_b, ubuf, rn3, ufr, 65536, uinfo) 307 if rr3 == 3000 { if g_eq(ufr, body3k, 3000) == 1 { got3 = 1 } } } 308 } 309 tries = tries + 1 310 } 311 pass = pass + g_check("UDP->UDP: 3KB frame raw-fans + reassembles bit-exact" as *u8, got3 == 1); total = total + 1 312 // (13) MIXED PLANE: a fresh WSS client in the SAME room; UDP frame -> arrives as ONE WS binary frame; 313 // WS binary -> arrives at A as datagrams that reassemble bit-exact. The bridge, both directions. 314 let fdw: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 315 var fu: i64 = 0 - 1 316 if fdw >= 0 { 317 let wad: *u8 = sys_mmap(16) 318 g_loopback_addr(wad, 8445) 319 if nx_connect_bounded(fdw, wad, 16, NX_CONN_DEFAULT_MS) >= 0 { 320 let v13: i64 = nx_ws_client_upgrade(fdw, "127.0.0.1" as *u8, 9, "/signal/udproom" as *u8, 15, 8445, 1, 321 req_buf, 2048, resp_buf, 4096) 322 if v13 == NX_WSCU_OK { fu = fdw } 323 } 324 } 325 var b13a: i64 = 0 326 if fu >= 0 { 327 np = dgm_pack_frame(0x4D, "AAAAAAA1" as *u8, 2, body3k, 3000, upk, 131072) 328 o3 = 0 329 s3 = 0 330 while s3 < np { 331 pl = (upk[o3] & 0xff) + ((upk[o3+1] & 0xff) << 8) 332 sys_sendto(us_a, ((upk as i64) + o3 + 2) as *u8, pl, 0, uad, 16) 333 o3 = o3 + 2 + pl 334 s3 = s3 + 1 335 } 336 let wl: i64 = g_read_payload(fu, frame_buf, 16384, ufr, 65536) 337 if wl == 3000 { if g_eq(ufr, body3k, 3000) == 1 { b13a = 1 } } 338 } 339 pass = pass + g_check("BRIDGE UDP->WSS: dgram frame lands as ONE WS frame, bit-exact" as *u8, b13a == 1); total = total + 1 340 var b13b: i64 = 0 341 if fu >= 0 { 342 var bi4: i64 = 0 343 while bi4 < 3000 { body3k[bi4] = (((bi4 * 53) + 7) & 255) as u8; bi4 = bi4 + 1 } 344 nx_ws_send_frame_to_fd(fu, 1, WS_OP_BINARY, body3k, 3000) 345 var tries2: i64 = 0 346 while tries2 < 12 { 347 if b13b == 0 { 348 uflen[0] = 16 349 let rn4: i64 = sys_recvfrom(us_a, ubuf, 2048, 0, ufrom2, uflen) 350 if rn4 > 0 { let rr4: i64 = dgm_rx_add(urx_a, ubuf, rn4, ufr, 65536, uinfo) 351 if rr4 == 3000 { if uinfo[0] == 0x4D { if g_eq(ufr, body3k, 3000) == 1 { b13b = 1 } } } } 352 } 353 tries2 = tries2 + 1 354 } 355 } 356 pass = pass + g_check("BRIDGE WSS->UDP: WS frame fragments to dgrams, reassembles bit-exact" as *u8, b13b == 1); total = total + 1 357 358 // ================= SFU (task #41): selective forwarding, END-TO-END through the real relay ========= 359 // Fresh room "sfuroom": S(ender) + R1 + R2. R1 stays default (HI); R2 LSUBs to LO. S emits interleaved 360 // HI/LO-marked 0x57 frames. Assert: R1 gets ONLY HI with rewritten contiguous seq 0,1,2...; R2 gets HI 361 // until the LO KEY then ONLY LO, seq contiguous ACROSS the switch; LSUB itself is never broadcast. 362 let fs2: i64 = g_join_room("/signal/sfuroom" as *u8, 15, req_buf, resp_buf) 363 let fr1: i64 = g_join_room("/signal/sfuroom" as *u8, 15, req_buf, resp_buf) 364 let fr2: i64 = g_join_room("/signal/sfuroom" as *u8, 15, req_buf, resp_buf) 365 var sfu_ok: i64 = 0 366 if fs2 >= 0 { if fr1 >= 0 { if fr2 >= 0 { sfu_ok = 1 } } } 367 pass = pass + g_check("SFU: S+R1+R2 joined sfuroom" as *u8, sfu_ok == 1); total = total + 1 368 if sfu_ok == 1 { 369 g_tmo(fs2, 2); g_tmo(fr1, 2); g_tmo(fr2, 2) 370 // registration: every member must have sent ONE binary frame (relay learns id -> memidx) 371 let regf: *u8 = sys_mmap(64) 372 g_mkreg(regf, "RCVR0001" as *u8) 373 nx_ws_send_frame_to_fd(fr1, 1, WS_OP_BINARY, regf, 16) 374 g_mkreg(regf, "RCVR0002" as *u8) 375 nx_ws_send_frame_to_fd(fr2, 1, WS_OP_BINARY, regf, 16) 376 g_mkreg(regf, "SENDER00" as *u8) 377 nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, regf, 16) 378 // drain the reg cross-traffic (R1 sees R2+S regs, R2 sees R1+S regs; S sees R1+R2 but never reads) 379 let dbuf: *u8 = sys_mmap(4096) 380 g_read_payload(fr1, frame_buf, 16384, dbuf, 4096) 381 g_read_payload(fr1, frame_buf, 16384, dbuf, 4096) 382 g_read_payload(fr2, frame_buf, 16384, dbuf, 4096) 383 g_read_payload(fr2, frame_buf, 16384, dbuf, 4096) 384 // R2 subscribes LO for SENDER00 (kind 0x58; consumed by the relay, never broadcast) 385 let lsub: *u8 = sys_mmap(64) 386 lsub[0] = 0x58 as u8 387 var lz: i64 = 0 388 let r2id: *u8 = "RCVR0002" as *u8 389 while lz < 8 { lsub[1+lz] = r2id[lz]; lz = lz + 1 } 390 lsub[9]=0 as u8; lsub[10]=0 as u8; lsub[11]=0 as u8; lsub[12]=0 as u8 391 let sid: *u8 = "SENDER00" as *u8 392 lz = 0 393 while lz < 8 { lsub[13+lz] = sid[lz]; lz = lz + 1 } 394 lsub[21] = 1 as u8 395 nx_ws_send_frame_to_fd(fr2, 1, WS_OP_BINARY, lsub, 22) 396 sys_sleep_ms(80) // LSUB rides R2's conn; let the relay apply it 397 // S emits: HI key, HI P, LO key, HI P, LO P (in_seq spaces deliberately disjoint per layer) 398 let vf: *u8 = sys_mmap(256) 399 g_mk57(vf, sid, 100, 1); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // HI KEY 400 g_mk57(vf, sid, 101, 0); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // HI P 401 g_mk57(vf, sid, 7, 3); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // LO KEY 402 g_mk57(vf, sid, 102, 0); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // HI P 403 g_mk57(vf, sid, 8, 2); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // LO P 404 // R1 (default HI): must read exactly HI key(0), HI P(1), HI P(2) -- the LO frames never arrive 405 var r1ok: i64 = 1 406 let rb: *u8 = sys_mmap(4096) 407 var n1: i64 = g_read_payload(fr1, frame_buf, 16384, rb, 4096) 408 if n1 < 18 { r1ok = 0 } else { if (rb[13]&1) != 1 { r1ok = 0 } if (rb[13]&2) != 0 { r1ok = 0 } if g_rdseq(rb) != 0 { r1ok = 0 } } 409 n1 = g_read_payload(fr1, frame_buf, 16384, rb, 4096) 410 if n1 < 18 { r1ok = 0 } else { if (rb[13]&3) != 0 { r1ok = 0 } if g_rdseq(rb) != 1 { r1ok = 0 } } 411 n1 = g_read_payload(fr1, frame_buf, 16384, rb, 4096) 412 if n1 < 18 { r1ok = 0 } else { if (rb[13]&3) != 0 { r1ok = 0 } if g_rdseq(rb) != 2 { r1ok = 0 } } 413 pass = pass + g_check("SFU: default receiver gets ONLY HI, seq rewritten 0,1,2" as *u8, r1ok == 1); total = total + 1 414 // R2 (LSUB LO): HI key(0), HI P(1), LO KEY(2) = switch, LO P(3); the post-switch HI P never arrives 415 var r2ok: i64 = 1 416 var n2: i64 = g_read_payload(fr2, frame_buf, 16384, rb, 4096) 417 if n2 < 18 { r2ok = 0 } else { if (rb[13]&3) != 1 { r2ok = 0 } if g_rdseq(rb) != 0 { r2ok = 0 } } 418 n2 = g_read_payload(fr2, frame_buf, 16384, rb, 4096) 419 if n2 < 18 { r2ok = 0 } else { if (rb[13]&3) != 0 { r2ok = 0 } if g_rdseq(rb) != 1 { r2ok = 0 } } 420 n2 = g_read_payload(fr2, frame_buf, 16384, rb, 4096) 421 if n2 < 18 { r2ok = 0 } else { if (rb[13]&3) != 3 { r2ok = 0 } if g_rdseq(rb) != 2 { r2ok = 0 } } 422 n2 = g_read_payload(fr2, frame_buf, 16384, rb, 4096) 423 if n2 < 18 { r2ok = 0 } else { if (rb[13]&3) != 2 { r2ok = 0 } if g_rdseq(rb) != 3 { r2ok = 0 } } 424 pass = pass + g_check("SFU: subscriber switches to LO AT the LO key, seq contiguous across switch" as *u8, r2ok == 1); total = total + 1 425 // starvation guard: R1 must still be flowing HI (one more HI P -> seq 3), proving pair isolation 426 g_mk57(vf, sid, 103, 0); nx_ws_send_frame_to_fd(fs2, 1, WS_OP_BINARY, vf, 32) // HI P 427 var r3ok: i64 = 1 428 n1 = g_read_payload(fr1, frame_buf, 16384, rb, 4096) 429 if n1 < 18 { r3ok = 0 } else { if (rb[13]&3) != 0 { r3ok = 0 } if g_rdseq(rb) != 3 { r3ok = 0 } } 430 // and R2 must NOT get that HI P: with a 1s timeout this read must FAIL (nothing pending for R2) 431 g_tmo(fr2, 1) 432 let nn: i64 = g_read_payload(fr2, frame_buf, 16384, rb, 4096) 433 if nn >= 0 { r3ok = 0 } 434 pass = pass + g_check("SFU: pair isolation (R1 flows on; R2 clean of dropped HI)" as *u8, r3ok == 1); total = total + 1 435 sys_close(fs2); sys_close(fr1); sys_close(fr2) 436 } 437 438 // ============ R4: DOMINANT-SPEAKER (0x5A announce) + LAST-N (0x59 cap), E2E ============ 439 // domroom: S1 loud (fat LPC frames = speech), S2 silent, R receiver. Speech scoring is payload-length 440 // (LPC compresses silence away), so a FAT 0x4C frame IS the speech signal. 441 let ds1: i64 = g_join_room("/signal/domroom" as *u8, 15, req_buf, resp_buf) 442 let ds2: i64 = g_join_room("/signal/domroom" as *u8, 15, req_buf, resp_buf) 443 let dr: i64 = g_join_room("/signal/domroom" as *u8, 15, req_buf, resp_buf) 444 var dok: i64 = 0 445 if ds1 >= 0 { if ds2 >= 0 { if dr >= 0 { dok = 1 } } } 446 if dok == 1 { 447 g_tmo(ds1, 2); g_tmo(ds2, 2); g_tmo(dr, 2) 448 let regf2: *u8 = sys_mmap(64) 449 g_mkreg(regf2, "SPKLOUD1" as *u8); nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, regf2, 16) 450 g_mkreg(regf2, "SPKQUIET" as *u8); nx_ws_send_frame_to_fd(ds2, 1, WS_OP_BINARY, regf2, 16) 451 g_mkreg(regf2, "RECVLASN" as *u8); nx_ws_send_frame_to_fd(dr, 1, WS_OP_BINARY, regf2, 16) 452 let dbuf2: *u8 = sys_mmap(4096) 453 g_read_payload(ds1, frame_buf, 16384, dbuf2, 4096); g_read_payload(ds1, frame_buf, 16384, dbuf2, 4096) 454 g_read_payload(ds2, frame_buf, 16384, dbuf2, 4096); g_read_payload(ds2, frame_buf, 16384, dbuf2, 4096) 455 g_read_payload(dr, frame_buf, 16384, dbuf2, 4096); g_read_payload(dr, frame_buf, 16384, dbuf2, 4096) 456 // T19: S1 speaks (one FAT 0x4C) -> relay announces 0x5A[SPKLOUD1] to the room BEFORE relaying audio 457 let aud: *u8 = sys_mmap(1024) 458 aud[0] = 0x4C as u8 459 let s1id: *u8 = "SPKLOUD1" as *u8 460 var az: i64 = 0 461 while az < 8 { aud[1+az] = s1id[az]; az = az + 1 } 462 az = 9 463 while az < 813 { aud[az] = ((az * 7) & 255) as u8; az = az + 1 } // 800B of "speech" 464 nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, aud, 813) 465 var t19: i64 = 1 466 let rb2: *u8 = sys_mmap(4096) 467 var m1: i64 = g_read_payload(dr, frame_buf, 16384, rb2, 4096) 468 if m1 < 9 { t19 = 0 } else { 469 if (rb2[0]&0xff) != 0x5A { t19 = 0 } 470 var qz: i64 = 0 471 while qz < 8 { if rb2[1+qz] != s1id[qz] { t19 = 0; qz = 8 } else { qz = qz + 1 } } 472 } 473 m1 = g_read_payload(dr, frame_buf, 16384, rb2, 4096) // then the relayed audio itself 474 if m1 != 813 { t19 = 0 } else { if (rb2[0]&0xff) != 0x4C { t19 = 0 } } 475 pass = pass + g_check("R4: dominant-speaker 0x5A announced on speech (LPC-length energy)" as *u8, t19 == 1); total = total + 1 476 // drain S2's copies (0x5A + audio) so its socket is clean 477 g_read_payload(ds2, frame_buf, 16384, dbuf2, 4096); g_read_payload(ds2, frame_buf, 16384, dbuf2, 4096) 478 g_read_payload(ds1, frame_buf, 16384, dbuf2, 4096) // S1 gets the 0x5A too (not its own audio) 479 // T20: R caps last-N=1 -> only the ACTIVE speaker's video passes; silent S2's video blocked for R 480 let lns: *u8 = sys_mmap(32) 481 lns[0] = 0x59 as u8 482 let rid2: *u8 = "RECVLASN" as *u8 483 az = 0 484 while az < 8 { lns[1+az] = rid2[az]; az = az + 1 } 485 lns[9]=0 as u8; lns[10]=0 as u8; lns[11]=0 as u8; lns[12]=0 as u8 486 lns[13] = 1 as u8 487 nx_ws_send_frame_to_fd(dr, 1, WS_OP_BINARY, lns, 14) 488 sys_sleep_ms(80) 489 let vf2: *u8 = sys_mmap(256) 490 g_mk57(vf2, s1id, 500, 1); nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, vf2, 32) // loud S1 video KEY 491 g_mk57(vf2, "SPKQUIET" as *u8, 900, 1); nx_ws_send_frame_to_fd(ds2, 1, WS_OP_BINARY, vf2, 32) // silent S2 video KEY 492 var t20: i64 = 1 493 m1 = g_read_payload(dr, frame_buf, 16384, rb2, 4096) // R: S1's video passes (top-1 active) 494 if m1 < 19 { t20 = 0 } else { 495 if (rb2[0]&0xff) != 0x57 { t20 = 0 } 496 var qz2: i64 = 0 497 while qz2 < 8 { if rb2[1+qz2] != s1id[qz2] { t20 = 0; qz2 = 8 } else { qz2 = qz2 + 1 } } 498 } 499 g_tmo(dr, 1) 500 let mnone: i64 = g_read_payload(dr, frame_buf, 16384, rb2, 4096) // S2's video must NOT arrive at R 501 if mnone >= 0 { t20 = 0 } 502 // control: S1 (no cap) DOES receive silent S2's video (last-N is per-receiver, not global). 503 // Skim 0x5B LPROD control chatter (R5 baseline announces ride the same socket). 504 m1 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096) 505 var skim: i64 = 0 506 while skim < 3 { if m1 >= 1 { if (rb2[0]&0xff) == 0x5B { m1 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096); skim = skim + 1 } else { skim = 3 } } else { skim = 3 } } 507 if m1 < 19 { t20 = 0 } else { 508 if (rb2[0]&0xff) != 0x57 { t20 = 0 } 509 let q2id: *u8 = "SPKQUIET" as *u8 510 var qz3: i64 = 0 511 while qz3 < 8 { if rb2[1+qz3] != q2id[qz3] { t20 = 0; qz3 = 8 } else { qz3 = qz3 + 1 } } 512 } 513 pass = pass + g_check("R4: last-N=1 per receiver (active passes, silent blocked, others unaffected)" as *u8, t20 == 1); total = total + 1 514 // T21 R5 LAYER SUSPENSION: R re-subscribes S1's LO -> S1 gets LPROD 0x5B want=1; R back to HI + 515 // the LO chain switches away -> S1's next video frame triggers LPROD want=0. (First-eval baseline 516 // want=0 was already announced to S1 by its T20 video frames -- drain anything pending first.) 517 g_tmo(ds1, 1) 518 var drn: i64 = 0 519 while drn == 0 { if g_read_payload(ds1, frame_buf, 16384, dbuf2, 4096) < 0 { drn = 1 } } 520 var t21: i64 = 1 521 let lsub2: *u8 = sys_mmap(64) 522 lsub2[0] = 0x58 as u8 523 az = 0 524 while az < 8 { lsub2[1+az] = rid2[az]; az = az + 1 } 525 lsub2[9]=0 as u8; lsub2[10]=0 as u8; lsub2[11]=0 as u8; lsub2[12]=0 as u8 526 az = 0 527 while az < 8 { lsub2[13+az] = s1id[az]; az = az + 1 } 528 lsub2[21] = 1 as u8 // R wants S1's LO 529 nx_ws_send_frame_to_fd(dr, 1, WS_OP_BINARY, lsub2, 22) 530 g_tmo(ds1, 2) 531 var m21: i64 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096) // S1 must get LPROD want=1 532 if m21 < 14 { t21 = 0 } else { 533 if (rb2[0]&0xff) != 0x5B { t21 = 0 } 534 if (rb2[13]&0xff) != 1 { t21 = 0 } 535 } 536 // S1 ships a LO key (now wanted) -> R switches to LO; then R unsubscribes; then S1's HI key both 537 // completes the switch-back AND (frame-driven re-eval) earns S1 the LPROD want=0. 538 g_mk57(vf2, s1id, 600, 3); nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, vf2, 32) // LO KEY 539 g_read_payload(dr, frame_buf, 16384, dbuf2, 4096) // R receives it (switched to LO) 540 lsub2[21] = 0 as u8 541 nx_ws_send_frame_to_fd(dr, 1, WS_OP_BINARY, lsub2, 22) // R back to HI (no LPROD yet: LO still flowing) 542 sys_sleep_ms(60) 543 g_mk57(vf2, s1id, 601, 1); nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, vf2, 32) // HI KEY -> switch-back 544 g_read_payload(dr, frame_buf, 16384, dbuf2, 4096) // R gets the HI key 545 g_mk57(vf2, s1id, 602, 0); nx_ws_send_frame_to_fd(ds1, 1, WS_OP_BINARY, vf2, 32) // next frame re-verdicts 546 g_read_payload(dr, frame_buf, 16384, dbuf2, 4096) 547 var m22: i64 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096) // S1 must get LPROD want=0 548 if m22 < 14 { t21 = 0 } else { 549 if (rb2[0]&0xff) != 0x5B { t21 = 0 } 550 if (rb2[13]&0xff) != 0 { t21 = 0 } 551 } 552 pass = pass + g_check("R5: layer suspension LPROD (want=1 on subscribe, want=0 when unwatched)" as *u8, t21 == 1); total = total + 1 553 // T22 RECEIVER REPORT routing: R sends RR(0x52) targeting S1 -> ONLY S1 receives it (S2 clean). 554 var t22: i64 = 1 555 g_tmo(ds2, 1) // S2 accumulated T20/T21 broadcast copies 556 var drn2: i64 = 0 // + its own LPROD baseline -> drain to empty 557 while drn2 == 0 { if g_read_payload(ds2, frame_buf, 16384, dbuf2, 4096) < 0 { drn2 = 1 } } 558 let rrf: *u8 = sys_mmap(64) 559 rrf[0] = 0x52 as u8 560 az = 0 561 while az < 8 { rrf[1+az] = rid2[az]; az = az + 1 } 562 rrf[9]=0 as u8; rrf[10]=0 as u8; rrf[11]=0 as u8; rrf[12]=0 as u8 563 az = 0 564 while az < 8 { rrf[13+az] = s1id[az]; az = az + 1 } // target = SPKLOUD1 565 rrf[21] = 14 as u8 // rxfps=14 566 nx_ws_send_frame_to_fd(dr, 1, WS_OP_BINARY, rrf, 22) 567 g_tmo(ds1, 2) 568 var m23: i64 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096) 569 var skim2: i64 = 0 570 while skim2 < 3 { if m23 >= 1 { if (rb2[0]&0xff) == 0x5B { m23 = g_read_payload(ds1, frame_buf, 16384, rb2, 4096); skim2 = skim2 + 1 } else { skim2 = 3 } } else { skim2 = 3 } } 571 if m23 != 22 { t22 = 0 } else { 572 if (rb2[0]&0xff) != 0x52 { t22 = 0 } 573 if (rb2[21]&0xff) != 14 { t22 = 0 } 574 } 575 g_tmo(ds2, 1) 576 let mnone2: i64 = g_read_payload(ds2, frame_buf, 16384, rb2, 4096) // S2 must get NOTHING 577 if mnone2 >= 0 { t22 = 0 } 578 pass = pass + g_check("RR: receiver report routed to its TARGET sender only (0x52)" as *u8, t22 == 1); total = total + 1 579 sys_close(ds1); sys_close(ds2); sys_close(dr) 580 } else { 581 pass = pass + g_check("R4: domroom join" as *u8, 0); total = total + 1 582 } 583 584 // ============ ROOM TOKENS: capability-link wall (keyroom protected, freeroom open) ============ 585 let tok: *u8 = sys_mmap(32) 586 rk_token("keyroom" as *u8, 7, "gatesecret" as *u8, 10, tok) // same SSOT organ the relay uses 587 let kp: *u8 = sys_mmap(64) 588 let kplen: i64 = g_mkkeypath(kp, tok) 589 // T23 ADMIT: two valid-token joins into keyroom relay to each other (proves admit + room functions). 590 let ka: i64 = g_join_room(kp, kplen, req_buf, resp_buf) 591 let kb: i64 = g_join_room(kp, kplen, req_buf, resp_buf) 592 var t23: i64 = 0 593 if ka >= 0 { if kb >= 0 { 594 g_tmo(kb, 2) 595 nx_ws_send_frame_to_fd(ka, 1, WS_OP_TEXT, "validok!" as *u8, 8) 596 let nkb: i64 = g_read_payload(kb, frame_buf, 16384, pay, 4096) 597 if nkb == 8 { if g_eq(pay, "validok!" as *u8, 8) == 1 { t23 = 1 } } 598 } } 599 pass = pass + g_check("TOKEN: valid token admits + keyed room relays" as *u8, t23 == 1); total = total + 1 600 // T24 REJECT: no-token + wrong-token joins are closed pre-membership -> they never receive ka's frame, 601 // but the valid kb does (proves rejection + no leakage in one shot). 602 let knox: i64 = g_join_room("/signal/keyroom" as *u8, 15, req_buf, resp_buf) // no ?k 603 let kwp: *u8 = sys_mmap(64) 604 let bad: *u8 = "00000000deadbeef" as *u8 605 let kwlen: i64 = g_mkkeypath(kwp, bad) // wrong token 606 let kwrong: i64 = g_join_room(kwp, kwlen, req_buf, resp_buf) 607 var t24: i64 = 1 608 g_tmo(knox, 1); g_tmo(kwrong, 1); g_tmo(kb, 2) 609 sys_sleep_ms(60) 610 nx_ws_send_frame_to_fd(ka, 1, WS_OP_TEXT, "afterrej" as *u8, 8) 611 let nbk: i64 = g_read_payload(kb, frame_buf, 16384, pay, 4096) // valid peer gets it 612 if nbk != 8 { t24 = 0 } else { if g_eq(pay, "afterrej" as *u8, 8) != 1 { t24 = 0 } } 613 if g_read_payload(knox, frame_buf, 16384, pay, 4096) == 8 { t24 = 0 } // rejected: no frame 614 if g_read_payload(kwrong, frame_buf, 16384, pay, 4096) == 8 { t24 = 0 } 615 pass = pass + g_check("TOKEN: no-token + wrong-token REJECTED (closed, no leak)" as *u8, t24 == 1); total = total + 1 616 // T25 OPEN unaffected: an UNLISTED room joins + relays normally even with the wall armed. 617 let oa: i64 = g_join_room("/signal/freeroom" as *u8, 16, req_buf, resp_buf) 618 let ob: i64 = g_join_room("/signal/freeroom" as *u8, 16, req_buf, resp_buf) 619 var t25: i64 = 0 620 if oa >= 0 { if ob >= 0 { 621 g_tmo(ob, 2) 622 nx_ws_send_frame_to_fd(oa, 1, WS_OP_TEXT, "freepass" as *u8, 8) 623 let nob: i64 = g_read_payload(ob, frame_buf, 16384, pay, 4096) 624 if nob == 8 { if g_eq(pay, "freepass" as *u8, 8) == 1 { t25 = 1 } } 625 } } 626 pass = pass + g_check("TOKEN: unlisted room UNAFFECTED by armed wall" as *u8, t25 == 1); total = total + 1 627 sys_close(ka); sys_close(kb); sys_close(knox); sys_close(kwrong); sys_close(oa); sys_close(ob) 628 629 // teardown (nx_kill: rv64 129 -> x86 62 via the __syscall translation table) 630 nx_kill(pid, 9) 631 g_puts("---- signaling_v2 gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 632 if pass == total { return 0 } 633 return 1 634}