code wiki / (root) / nx_mse_seed_gate.nx

nx_mse_seed_gate.nx source

↩ module page · 96 lines · 6586 B

1// nx_mse_seed_gate.nx -- proves the SEEDER's MSE path end-to-end: an ENCRYPTED (mse_connect) leecher pulls a 2// sha1-verified piece from nx_torrent_seed's ts_serve_peer (which demuxes the non-0x13 first byte -> MSE). 3// This is the R5 seeder-side live proof. license_tier: ORIGINAL depends: nx_torrent_seed (pulls mse_wire+peerwire+sha1) 4import "nx_torrent_seed.nx" 5import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 6import "nx_sha1.nx" 7import "nx_gate_verdict.nx" 8 9const MSG_PORT: i64 = 54011 10const G2_PLEN: i64 = 64 11const G2_TOTAL: i64 = 200 // 4 pieces (64,64,64,8) 12const G2_BLK: i64 = 16 13const G2_PATH: *u8 = "/tmp/_nx_mse_seed_src.bin" as *u8 14 15func g2_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 16func g2_n(v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; 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} let o: *u8=sys_mmap(28); var i: i64=0; while i<k {o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 17func g2_yn(b: i64) -> *u8 { if b==1 { return "1" as *u8 } return "0" as *u8 } 18 19func main() -> i64 { 20 g2_p("MSE-SEED-GATE authored=organ (encrypted leecher <-> demuxing seeder)\n" as *u8) 21 let filebuf: *u8 = sys_mmap(G2_TOTAL+16); var i: i64=0; while i<G2_TOTAL { filebuf[i]=((i*151+29)&0xff) as u8; i=i+1 } 22 let wf: i64 = sys_openat_wr(G2_PATH, 0x1a4); if wf<0 { g2_p("file=FAIL verdict=RED\n" as *u8); sys_exit(1); return 1 } 23 sys_write(wf, filebuf, G2_TOTAL); sys_close(wf) 24 let npc: i64 = (G2_TOTAL+G2_PLEN-1)/G2_PLEN 25 let ih: *u8 = sys_mmap(24); i=0; while i<20 { ih[i]=(0x30+i) as u8; i=i+1 } 26 let seed_pid: *u8 = sys_mmap(24); i=0; while i<20 { seed_pid[i]=83 as u8; i=i+1 } 27 let leech_pid: *u8 = sys_mmap(24); i=0; while i<20 { leech_pid[i]=76 as u8; i=i+1 } 28 let sa: *u8 = sys_mmap(16); ts_sockaddr(sa, MSG_PORT, 127,0,0,1) 29 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 30 var bound: i64=0 31 if lfd>=0 { let one: *u8=sys_mmap(4); one[0]=1 as u8; one[1]=0 as u8; one[2]=0 as u8; one[3]=0 as u8; sys_setsockopt(lfd, SOL_SOCKET, 2, one, 4); sys_set_socket_timeout(lfd, 5); if sys_bind(lfd, sa, 16)>=0 { if sys_listen(lfd, 4)>=0 { bound=1 } } } 32 if bound==0 { g2_p("bound=0 verdict=RED\n" as *u8); sys_exit(1); return 1 } 33 34 let pid: i64 = sys_fork() 35 if pid==0 { 36 // SEEDER (the organ under test): accept + ts_serve_peer -> demux sees MSE first byte -> encrypted serve 37 let afd: i64 = sys_accept(lfd) 38 if afd>=0 { ts_serve_peer(afd, ih, seed_pid, G2_PATH, G2_PLEN, G2_TOTAL, npc); sys_close(afd) } 39 sys_close(lfd); sys_exit(0) 40 } 41 42 // ENCRYPTED LEECHER 43 let cfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0); sys_set_socket_timeout(cfd, 5) 44 var conn: i64=0; var mse_ok: i64=0; var hs_ok: i64=0; var bf_ok: i64=0; var unchoked: i64=0; var blocks_ok: i64=0; var sha_ok: i64=0 45 if nx_connect_bounded(cfd, sa, 16, NX_CONN_DEFAULT_MS)>=0 { conn=1 } 46 if conn==1 { 47 let xa: *i64 = sys_mmap((MSE_N+2)*8) as *i64; bi_zero(xa, MSE_N); xa[0]=0x0abcdef; xa[1]=0x1122334; xa[2]=0x5566778 48 let ctx: *MseCtx = sys_mmap(MSE_CTX_BYTES) as *MseCtx 49 if mse_connect(cfd, ih, xa, ctx) == 1 { 50 mse_ok = 1 51 let ch: *u8 = sys_mmap(128); nx_pw_build_handshake(ih, leech_pid, ch); mse_write(ctx, cfd, ch, 68) // our BT handshake (encrypted) 52 let ph: *u8 = sys_mmap(128); if mse_read(ctx, cfd, ph, 68)==68 { let gih: *u8=sys_mmap(20); let gpid: *u8=sys_mmap(20); if nx_pw_parse_handshake(ph,68,gih,gpid)==1 { hs_ok=1 } } 53 if hs_ok==1 { 54 let mb: *u8 = sys_mmap(4096); var guard: i64=0 55 while unchoked==0 { if guard>=8 { guard=99 } else { 56 let ml: i64 = ts_read_msg_ctx(ctx, cfd, mb, 4096) 57 if ml==0-1 { guard=99 } else { if ml>0 { 58 let id: i64 = nx_pw_msg_id(mb) 59 if id==NX_PW_BITFIELD { let bfp: *u8=mb+5; let bl: i64=ml-1; var all: i64=1; var pp: i64=0; while pp<npc { if nx_pw_bitfield_has(bfp,bl,pp)!=1 { all=0 } pp=pp+1 } bf_ok=all } 60 if id==NX_PW_UNCHOKE { unchoked=1 } 61 } } 62 guard=guard+1 63 } } 64 if unchoked==1 { 65 let piece1: *u8 = sys_mmap(G2_PLEN+16); let nblk: i64=G2_PLEN/G2_BLK; var got: i64=1; var bi: i64=0 66 while bi<nblk { 67 let rq: *u8=sys_mmap(32); let rl: i64=nx_pw_build_request(1, bi*G2_BLK, G2_BLK, rq); mse_write(ctx, cfd, rq, rl) 68 let pb: *u8=sys_mmap(256); let pl: i64=ts_read_msg_ctx(ctx, cfd, pb, 256) 69 if pl>=9 { if nx_pw_msg_id(pb)==NX_PW_PIECE { let bg: i64=_pw_get_u32(pb,9); var j: i64=0; while j<G2_BLK { piece1[bg+j]=pb[13+j]; j=j+1 } } else { got=0 } } else { got=0 } 70 bi=bi+1 71 } 72 blocks_ok=got 73 if blocks_ok==1 { 74 let gh: *u8=sys_mmap(20); let eh: *u8=sys_mmap(20); let slice: *u8=filebuf+G2_PLEN 75 sha1(piece1, G2_PLEN, gh); sha1(slice, G2_PLEN, eh) 76 var se: i64=1; var j: i64=0; while j<20 { if gh[j]!=eh[j] { se=0; j=20 } else { j=j+1 } } sha_ok=se 77 } 78 } 79 } 80 } 81 } 82 let st: *i64 = sys_mmap(16) as *i64; sys_wait4(pid, st, 0); sys_close(cfd); sys_close(lfd) 83 g2_p(" conn=" as *u8); g2_p(g2_yn(conn)); g2_p(" mse_connect=" as *u8); g2_p(g2_yn(mse_ok)); g2_p(" bt_handshake=" as *u8); g2_p(g2_yn(hs_ok)) 84 g2_p(" bitfield_all=" as *u8); g2_p(g2_yn(bf_ok)); g2_p(" unchoked=" as *u8); g2_p(g2_yn(unchoked)); g2_p(" blocks=" as *u8); g2_p(g2_yn(blocks_ok)); g2_p(" sha1_verify=" as *u8); g2_p(g2_yn(sha_ok)); g2_p("\n" as *u8) 85 var allok: i64=0 86 if conn==1 { if mse_ok==1 { if hs_ok==1 { if bf_ok==1 { if unchoked==1 { if blocks_ok==1 { if sha_ok==1 { allok=1 } } } } } } } 87 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 88 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 89 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 90 let ctr__dry: *i64 = gv_ctr() 91 ctr__dry[0] = allok 92 ctr__dry[1] = 1 93 let rc__dry: i64 = gv_verdict("MSE-SEED-GATE" as *u8, ctr__dry, "encrypted leecher pulled a sha1-verified piece through the MSE seeder path)" as *u8) 94 sys_exit(rc__dry) 95 return rc__dry 96}