code wiki / (root) / nx_dns_resolve_a_record_live_test.nx

nx_dns_resolve_a_record_live_test.nx source

↩ module page · 44 lines · 1671 B

1// nx_dns_resolve_a_record_live_test.nx -- LIVE-FIRE KAT for the 2// wired-up DNS A-record resolver. Queries Cloudflare 1.1.1.1 3// for "example.com" and asserts a non-zero IPv4 is returned. 4// 5// Closes the 6-step stub gap that previously left the wire-up 6// as comments only. The substrate's DNS layer is now load- 7// bearing against real DNS servers. 8// 9// expect_exit: 0 10// license_tier: ORIGINAL 11 12import "nx_syscalls.nx" 13import "nx_dns_resolve_a_record.nx" 14 15func main() -> i64 { 16 let host: *u8 = sys_mmap(32) 17 host[0]=0x65; host[1]=0x78; host[2]=0x61; host[3]=0x6d 18 host[4]=0x70; host[5]=0x6c; host[6]=0x65; host[7]=0x2e 19 host[8]=0x63; host[9]=0x6f; host[10]=0x6d // "example.com" 20 21 let r: *DnsResolveResult = nx_dns_resolve_default(host, 11, 1718452800) 22 if r.verdict != NX_DNS_R_OK { return 1 } 23 if r.ipv4_packed == 0 { return 2 } 24 25 // Dump the resolved IP to stderr for visibility. 26 let lab: *u8 = sys_mmap(16) 27 lab[0]=0x49; lab[1]=0x50; lab[2]=0x3D // "IP=" 28 sys_write(2, lab, 3) 29 let dig: *u8 = sys_mmap(64) 30 var pos: i64 = 0 31 var byte_idx: i64 = 0 32 while byte_idx < 4 { 33 let b: i64 = (r.ipv4_packed >> ((3 - byte_idx) * 8)) & 0xff 34 if b >= 100 { dig[pos] = (0x30 + (b / 100)) as u8; pos = pos + 1 } 35 if b >= 10 { dig[pos] = (0x30 + ((b / 10) % 10)) as u8; pos = pos + 1 } 36 dig[pos] = (0x30 + (b % 10)) as u8; pos = pos + 1 37 if byte_idx < 3 { dig[pos] = 0x2E; pos = pos + 1 } // "." 38 byte_idx = byte_idx + 1 39 } 40 dig[pos] = 0x0A; pos = pos + 1 // "\n" 41 sys_write(2, dig, pos) 42 43 return 0 44}