code wiki / (root) / nx_dht_ping.nx

nx_dht_ping.nx source

↩ module page · 52 lines · 3145 B

1// nx_dht_ping.nx -- LIVE DHT reachability probe (X-TORRENT-LIVE-002 R4b). Resolves real BEP-5 2// bootstrap nodes and KRPC-pings them over UDP:6881; GREEN if any returns a valid response dict. 3// This is the EMPIRICAL test of whether the DHT is reachable from here, or whether the ISP/network 4// blocks UDP:6881 (the same class of blocking that already kills UDP tracker ports :1337/:6969). 5// Composes nx_dht (KRPC build/parse) + nx_dns_a (resolve) + raw UDP syscalls. NO external resolver. 6// license_tier: ORIGINAL layer: peer-discovery probe 7// module: nishi-core.torrent.dht_ping 8// depends: nishi-core.torrent.dht, nishi-core.dns_a 9import "nx_dht.nx" 10import "nx_dns_a.nx" 11const K_MAGIC_6881: i64 = 6881 12const K_MAGIC_2048: i64 = 2048 13 14func dp_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 15func dp_p2(s: *u8) -> i64 { sys_write(1, s, dp_strlen(s)); return 0 } 16 17func dp_sockaddr(out: *u8, port: i64, a: i64, b: i64, c: i64, d: i64) -> i64 { 18 out[0] = 2 as u8; out[1] = 0 as u8 19 out[2] = ((port >> 8) & 0xff) as u8; out[3] = (port & 0xff) as u8 20 out[4] = a as u8; out[5] = b as u8; out[6] = c as u8; out[7] = d as u8 21 var i: i64 = 8; while i < 16 { out[i] = 0 as u8; i = i + 1 } 22 return 16 23} 24 25// ping one bootstrap host over UDP:6881. returns: 1 = valid KRPC response, 0 = no/invalid reply, -1 = dns/socket fail. 26func dp_try(host: *u8, nid: *u8) -> i64 { 27 let ip4: *u8 = sys_mmap(4) 28 if nx_dns_a_resolve(host, ip4) != 1 { dp_p2(" (dns fail) " as *u8); dp_p2(host); dp_p2("\n" as *u8); return 0 - 1 } 29 let fd: i64 = sys_socket(AF_INET, SOCK_DGRAM, 0); if fd < 0 { return 0 - 1 } 30 sys_set_socket_timeout(fd, 5) 31 let dest: *u8 = sys_mmap(16); dp_sockaddr(dest, K_MAGIC_6881, ip4[0] as i64, ip4[1] as i64, ip4[2] as i64, ip4[3] as i64) 32 let tx: *u8 = sys_mmap(2); tx[0] = 97 as u8; tx[1] = 97 as u8 33 let q: *u8 = sys_mmap(128); let ql: i64 = dht_build_ping(nid, tx, q) 34 sys_sendto(fd, q, ql, 0, dest, 16) 35 let r: *u8 = sys_mmap(K_MAGIC_2048); let n: i64 = sys_recvfrom(fd, r, K_MAGIC_2048, 0, 0 as *u8, 0 as *i64) 36 sys_close(fd) 37 dp_p2(" " as *u8); dp_p2(host) 38 if n < 1 { dp_p2(" -> no reply (blocked/timeout)\n" as *u8); return 0 } 39 if nx_bc_dict_get(r, 0, n, "r" as *u8, 1) >= 0 { dp_p2(" -> RESPONDED (DHT reachable)\n" as *u8); return 1 } 40 dp_p2(" -> reply but not a KRPC response\n" as *u8); return 0 41} 42 43func main() -> i64 { 44 let nid: *u8 = sys_mmap(20); var i: i64 = 0; while i < 20 { nid[i] = (0x41 + i) as u8; i = i + 1 } // a fixed 20-byte node id (fine for a ping) 45 dp_p2("DHT bootstrap reachability (UDP:6881):\n" as *u8) 46 var ok: i64 = 0 47 if dp_try("router.bittorrent.com" as *u8, nid) == 1 { ok = 1 } 48 if dp_try("dht.transmissionbt.com" as *u8, nid) == 1 { ok = 1 } 49 if dp_try("router.utorrent.com" as *u8, nid) == 1 { ok = 1 } 50 if ok == 1 { dp_p2("DHT-PING verdict=GREEN (DHT is reachable -> get_peers can find trackerless peers)\n" as *u8); sys_exit(0); return 0 } 51 dp_p2("DHT-PING verdict=RED (UDP:6881 unreachable here -> ISP/net blocks DHT; need anti-block)\n" as *u8); sys_exit(1); return 1 52}