code wiki / _hdl_build / nx_home_registry_gate.nx
nx_home_registry_gate.nx source
↩ module page · 98 lines · 7559 B
1// nx_home_registry_gate.nx -- SOVEREIGN proof of nx_home H1 data-driven registry (#11). Writes a TSV registry of
2// CUSTOM devices, forks /tmp/nx_home.sov.elf pointed at it, and proves over a loopback socket (no python/shell)
3// that the server loaded the FILE's devices -- not the embedded defaults:
4// R1 custom devices present (office/garage/porch) R2 custom names loaded (Garage Light)
5// R3 data-driven, NOT embedded (no living_room) R4 exactly 3 devices (count ids)
6// R5 command works on a loaded device (garage on:1, bri:50) R6 unknown device -> structured error
7// Then POST /home/shutdown. GREEN iff 6/6. Requires /tmp/nx_home.sov.elf. knowledge/status/nx_home_registry_gate.log.
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
11import "nx_gate_emit_lib.nx"
12
13func g_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=(48 as u8);k=1}; while m>0{t[k]=((48+(m%10)) as u8);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
14func g_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
15func g_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=(48 as u8);k=1}; while m>0{t[k]=((48+(m%10)) as u8);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
16func g_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
17func g_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ dst[off+i]=s[i]; i=i+1 } return off+i }
18func g_contains(hay: *u8, n: i64, needle: *u8) -> i64 {
19 let nl: i64=g_strlen(needle); if nl==0 { return 0 }
20 var i: i64=0
21 while i+nl<=n { var j: i64=0; var hit: i64=1; while j<nl { if hay[i+j]!=needle[j] { hit=0; j=nl } else { j=j+1 } } if hit==1 { return 1 } i=i+1 }
22 return 0
23}
24func g_count(hay: *u8, n: i64, needle: *u8) -> i64 {
25 let nl: i64=g_strlen(needle); if nl==0 { return 0 }
26 var cnt: i64=0; var i: i64=0
27 while i+nl<=n { var j: i64=0; var hit: i64=1; while j<nl { if hay[i+j]!=needle[j] { hit=0; j=nl } else { j=j+1 } } if hit==1 { cnt=cnt+1; i=i+nl } else { i=i+1 } }
28 return cnt
29}
30func g_sockaddr(buf: *u8, port: i64, a: i64, b: i64, c: i64, d: i64) -> i64 {
31 var i: i64=0; while i<16 { buf[i]=0 as u8; i=i+1 }
32 buf[0]=2 as u8; buf[2]=((port>>8)&0xff) as u8; buf[3]=(port&0xff) as u8
33 buf[4]=a as u8; buf[5]=b as u8; buf[6]=c as u8; buf[7]=d as u8; return 0
34}
35func http_req(port: i64, req: *u8, reqlen: i64, resp: *u8, cap: i64) -> i64 {
36 let dest: *u8=sys_mmap(16); g_sockaddr(dest, port, 127, 0, 0, 1)
37 var fd: i64=0; var connected: i64=0; var tries: i64=0
38 while connected==0 {
39 fd=sys_socket(AF_INET, SOCK_STREAM, 0); if fd<0 { return 0-1 }
40 if nx_connect_bounded(fd, dest, 16, NX_CONN_DEFAULT_MS)==0 { connected=1 } else { sys_close(fd); tries=tries+1; if tries>40000 { return 0-2 } }
41 }
42 sys_write(fd, req, reqlen)
43 var total: i64=0; var r: i64=sys_read(fd, resp, cap)
44 while r>0 { total=total+r; if total>=cap { r=0 } else { r=sys_read(fd, ((resp as i64)+total) as *u8, cap-total) } }
45 sys_close(fd); return total
46}
47func tab(b: *u8, o: i64) -> i64 { b[o]=9 as u8; return o+1 }
48func nl(b: *u8, o: i64) -> i64 { b[o]=10 as u8; return o+1 }
49func write_reg(path: *u8) -> i64 {
50 let b: *u8=sys_mmap(2048); var o: i64=0
51 o=g_cat(b,o,"office" as *u8); o=tab(b,o); o=g_cat(b,o,"Office" as *u8); o=tab(b,o); o=g_cat(b,o,"office" as *u8); o=tab(b,o); o=g_cat(b,o,"light" as *u8); o=nl(b,o)
52 o=g_cat(b,o,"garage" as *u8); o=tab(b,o); o=g_cat(b,o,"Garage Light" as *u8); o=tab(b,o); o=g_cat(b,o,"garage" as *u8); o=tab(b,o); o=g_cat(b,o,"light" as *u8); o=nl(b,o)
53 o=g_cat(b,o,"porch" as *u8); o=tab(b,o); o=g_cat(b,o,"Porch" as *u8); o=tab(b,o); o=g_cat(b,o,"outside" as *u8); o=tab(b,o); o=g_cat(b,o,"light" as *u8); o=nl(b,o)
54 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 } sys_write(fd, b, o); sys_close(fd); return o
55}
56
57func main() -> i64 {
58 g_puts("=== NX_HOME REGISTRY GATE: data-driven device registry (#11), Nishi loopback ===\n" as *u8)
59 let REG: *u8="/tmp/home_reg.tsv" as *u8
60 write_reg(REG)
61 let GETSTATE: *u8="GET /home/state HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n" as *u8
62 let CMDGAR: *u8="POST /home/cmd?id=garage&on=1&bri=50 HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" as *u8
63 let CMDBAD: *u8="POST /home/cmd?id=living_room HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" as *u8
64 let SHUTDOWN: *u8="POST /home/shutdown HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" as *u8
65
66 let pid: i64=sys_fork()
67 if pid==0 {
68 let o: i64=sys_openat_wr("/tmp/nx_home_reg_srv.log" as *u8, 0x1a4); if o>=0 { sys_dup3(o,1,0); sys_dup3(o,2,0) }
69 let argv: *i64=sys_mmap(8*8) as *i64
70 argv[0]="/tmp/nx_home.sov.elf" as *u8 as i64; argv[1]="8201" as *u8 as i64; argv[2]=REG as i64; argv[3]=0
71 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0
72 sys_execve("/tmp/nx_home.sov.elf" as *u8, argv, envp); sys_exit(127)
73 }
74
75 let r1: *u8=sys_mmap(65536); let n1: i64=http_req(8201, GETSTATE, g_strlen(GETSTATE), r1, 65536)
76 let r2: *u8=sys_mmap(65536); let n2: i64=http_req(8201, CMDGAR, g_strlen(CMDGAR), r2, 65536)
77 let r3: *u8=sys_mmap(65536); let n3: i64=http_req(8201, CMDBAD, g_strlen(CMDBAD), r3, 65536)
78 let r4: *u8=sys_mmap(4096); http_req(8201, SHUTDOWN, g_strlen(SHUTDOWN), r4, 4096)
79 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
80
81 var pass: i64=0; let rows: i64=6
82 var custom: i64=0; if g_contains(r1,n1,"office" as *u8)==1 { if g_contains(r1,n1,"garage" as *u8)==1 { if g_contains(r1,n1,"porch" as *u8)==1 { custom=1 } } }
83 pass=pass+g_check(" R1 custom devices loaded (office/garage/porch)" as *u8, custom)
84 pass=pass+g_check(" R2 custom names loaded ('Garage Light')" as *u8, g_contains(r1,n1,"Garage Light" as *u8))
85 var noembed: i64=0; if g_contains(r1,n1,"living_room" as *u8)==0 { noembed=1 }
86 pass=pass+g_check(" R3 DATA-DRIVEN: embedded defaults NOT used" as *u8, noembed)
87 var three: i64=0; if g_count(r1,n1,"\"id\":\"" as *u8)==3 { three=1 }
88 pass=pass+g_check(" R4 exactly 3 devices from file" as *u8, three)
89 var cmdok: i64=0; if g_contains(r2,n2,"\"on\":1" as *u8)==1 { if g_contains(r2,n2,"\"brightness\":50" as *u8)==1 { cmdok=1 } }
90 pass=pass+g_check(" R5 command works on a loaded device (garage)" as *u8, cmdok)
91 pass=pass+g_check(" R6 unknown device (living_room) -> structured error" as *u8, g_contains(r3,n3,"unknown device" as *u8))
92
93 g_puts("----\nNX-HOME-REG rows=" as *u8); g_num(rows); g_puts(" pass=" as *u8); g_num(pass); g_puts("\n" as *u8)
94 let lg: i64=sys_openat_append("knowledge/status/nx_home_registry_gate.log" as *u8, 0x1a4)
95 if lg>=0 { g_w(lg, "NX-HOME-REG rows=" as *u8); g_wn(lg, rows); g_w(lg, " pass=" as *u8); g_wn(lg, pass); if pass==rows { g_w(lg, " verdict=GREEN\n" as *u8) } else { g_w(lg, " verdict=RED\n" as *u8) } sys_close(lg) }
96 if pass==rows { g_puts("NX-HOME-REG GREEN (data-driven device registry proven)\n" as *u8); sys_exit(0); return 0 }
97 g_puts("NX-HOME-REG RED\n" as *u8); sys_exit(1); return 1
98}