code wiki / (root) / nx_netprobe_lib.nx

nx_netprobe_lib.nx source

↩ module page · 46 lines · 2528 B

1// nx_netprobe_lib.nx -- THE one act a caller performs to ask "is this TCP endpoint reachable?": 2// a bounded non-blocking connect + poll(POLLOUT) + getsockopt(SO_ERROR). Library only (NO main), so 3// any organ imports it without a duplicate entry point. 4// 5// WHY (DRY consolidation, 2026-08-12): this exact probe existed TWICE -- go_worker_up inside the live 6// nx_gen_worker daemon (the multi-worker liveness selector) and go_probe_up inside nx_swarm_heal. 7// nx_gen_worker has a main() so it could not be imported, which is how the copy was born. Extracting 8// the ONE act both perform is the fix (rule 15); both now call np_probe_up. 9// 10// STRONGEST-EVIDENCE contract: reachable ONLY when getsockopt SO_ERROR == 0 (not merely poll-writable), 11// so a refused/half-open connection reads DOWN. Fail-safe: any socket/syscall failure -> 0 (down). 12// license_tier: ORIGINAL No hw writes (Rule 26). 13import "nx_syscalls.nx" 14 15// host octets a.b.c.d + port -> 1 reachable / 0 down (timeout_ms bounds the wait). 16func np_probe_up_to(ha: i64, hb: i64, hc: i64, hd: i64, port: i64, timeout_ms: i64) -> i64 { 17 let fd: i64 = sys_socket(2, 1, 0) 18 if fd < 0 { return 0 } 19 let fl: i64 = __syscall(72, fd, 3, 0, 0, 0, 0) // F_GETFL 20 __syscall(72, fd, 4, fl | 2048, 0, 0, 0) // F_SETFL | O_NONBLOCK 21 let a: *u8 = sys_mmap(16) 22 a[0]=2 as u8; a[1]=0 as u8; a[2]=((port>>8)&0xff) as u8; a[3]=(port&0xff) as u8 23 a[4]=ha as u8; a[5]=hb as u8; a[6]=hc as u8; a[7]=hd as u8 24 var zi: i64=8; while zi<16 { a[zi]=0 as u8; zi=zi+1 } 25 let rc: i64 = sys_connect(fd, a, 16) 26 var up: i64 = 0 27 if rc == 0 { up = 1 } 28 else { if rc == (0 - 115) { // EINPROGRESS -> wait for connect to settle 29 let pfd: *u8 = sys_mmap(8) 30 pfd[0]=(fd&0xff) as u8; pfd[1]=((fd>>8)&0xff) as u8; pfd[2]=((fd>>16)&0xff) as u8; pfd[3]=((fd>>24)&0xff) as u8 31 pfd[4]=4 as u8; pfd[5]=0 as u8 // POLLOUT 32 if sys_poll(pfd, 1, timeout_ms) > 0 { 33 let so: *i64 = sys_mmap(16) as *i64; so[0]=0 34 let sl: *i64 = sys_mmap(16) as *i64; sl[0]=4 35 __syscall(55, fd, 1, 4, so as i64, sl as i64, 0) // getsockopt SOL_SOCKET SO_ERROR 36 if (so[0] & 0xffffffff) == 0 { up = 1 } 37 } 38 } } 39 sys_close(fd) 40 return up 41} 42 43// the common 2-second-bound form (matches both original inline probes). 44func np_probe_up(ha: i64, hb: i64, hc: i64, hd: i64, port: i64) -> i64 { 45 return np_probe_up_to(ha, hb, hc, hd, port, 2000) 46}