code wiki / (root) / nx_win_r1probe.nx

nx_win_r1probe.nx source

↩ module page · 49 lines · 1946 B

1const K_MAGIC_5273: i64 = 5273 2const K_MAGIC_65536: i64 = 65536 3const K_MAGIC_262144: i64 = 262144 4const K_MAGIC_8000: i64 = 8000 5// nx_win_r1probe.nx -- R1 connect-thunk test: on any request, connect to 127.0.0.1:8000 (the gateway), 6// send a fixed HTTP/1.0 GET /api/characters (1.0 -> upstream closes -> no keepalive hang), relay the 7// response to the client. If :5273 returns the real character JSON -> nx_connect works + proxy works. 8func slen(s: *u8) -> i64 { var k: i64 = 0; while s[k] != (0 as u8) { k = k + 1 } return k } 9 10func sys_mmap(n: i64) -> i64 { return 0 } 11func nx_wsa() -> i64 { return 0 } 12func nx_sock() -> i64 { return 0 } 13func nx_bind(s: i64, port: i64) -> i64 { return 0 } 14func nx_listen(s: i64) -> i64 { return 0 } 15func nx_accept(s: i64) -> i64 { return 0 } 16func nx_recv(c: i64, buf: *u8, n: i64) -> i64 { return 0 } 17func nx_send(c: i64, buf: *u8, n: i64) -> i64 { return 0 } 18func nx_close(s: i64) -> i64 { return 0 } 19func nx_connect(s: i64, port: i64) -> i64 { return 0 } 20 21func main() -> i64 { 22 nx_wsa() 23 let srv: i64 = nx_sock() 24 if srv < 0 { return 1 } 25 if nx_bind(srv, K_MAGIC_5273) < 0 { return 2 } 26 if nx_listen(srv) < 0 { return 3 } 27 let req: *u8 = sys_mmap(K_MAGIC_65536) as *u8 28 let resp: *u8 = sys_mmap(K_MAGIC_262144) as *u8 29 let greq: *u8 = "GET /api/characters HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n" as *u8 30 while 1 == 1 { 31 let c: i64 = nx_accept(srv) 32 if c < 0 { return 4 } 33 nx_recv(c, req, K_MAGIC_65536) 34 let up: i64 = nx_sock() 35 if up >= 0 { 36 if nx_connect(up, K_MAGIC_8000) >= 0 { 37 nx_send(up, greq, slen(greq)) 38 var got: i64 = nx_recv(up, resp, K_MAGIC_262144) 39 while got > 0 { 40 nx_send(c, resp, got) 41 got = nx_recv(up, resp, K_MAGIC_262144) 42 } 43 } 44 nx_close(up) 45 } 46 nx_close(c) 47 } 48 return 0 49}