nx_stun.nx
buildroot/runtime/nx_stun.nx
about
nx_stun.nx -- SOVEREIGN STUN (RFC 5389 / RFC 8489) message codec, NishiLang from the first byte up.
This is the FIRST primitive of a Nishi-OS / Nishi-Browser peer-to-peer stack that INTEROPERATES with the
real STUN protocol -- so a Nishi endpoint can discover its public reflexive address and NAT-traverse to
ANY standard peer -- while using ZERO third-party code. No libjuice, no libwebrtc, no Google STUN client:
every byte is ours, emitted in NishiLang, KAT'd byte-exact against the IETF's own RFC 5769 test vectors.
Provides the four things a STUN Binding exchange needs:
- the 20-byte message header (type, length, magic cookie 0x2112A442, 96-bit transaction id)
- XOR-MAPPED-ADDRESS (RFC 5389 15.2) -- the reflexive IP:port a peer learns about itself
- FINGERPRINT (15.5) = CRC-32(message) XOR 0x5354554E (integrity/demux; our own CRC-32)
- MESSAGE-INTEGRITY (15.4) = HMAC-SHA1(key, message) (auth; composes the sovereign nx_hmac_sha1)
KAT: nx_stun_gate proves decode+encode of the RFC 5769 2.2 sample response byte-exact (XOR-MAPPED-ADDRESS
-> 192.0.2.1:32853, FINGERPRINT -> 0xB5BE215B, MESSAGE-INTEGRITY matches with the RFC's password), plus a
CRC-32 check-value KAT (crc32("123456789")==0xCBF43926).
license_tier: INDEPENDENT_REDERIVE
genealogy_id: international-research-sources/ietf/rfc_5389 + rfc_5769 (test vectors)
dependencies 2 imports · 4 importers
imports: nx_syscalls.nxnx_hmac_sha1.nx
imported by: nx_ice.nxnx_stun_client.nxnx_stun_gate.nxnx_stun_server.nx
structs
| none |
consts
| 23 | const STUN_MAGIC: i64 = 0x2112A442 |
| 24 | const STUN_FP_XOR: i64 = 0x5354554E |
| 25 | const STUN_BINDING_REQUEST: i64 = 0x0001 |
| 26 | const STUN_BINDING_RESPONSE: i64 = 0x0101 |
| 27 | const STUN_ATTR_XOR_MAPPED_ADDRESS: i64 = 0x0020 |
| 28 | const STUN_ATTR_MESSAGE_INTEGRITY: i64 = 0x0008 |
| 29 | const STUN_ATTR_FINGERPRINT: i64 = 0x8028 |
functions
| 32 | func st_put16(b: *u8, off: i64, v: i64) -> i64 { b[off]=((v>>8)&0xff) as u8; b[off+1]=(v&0xff) as u8; return off+2 } |
| 33 | func st_put32(b: *u8, off: i64, v: i64) -> i64 { b[off]=((v>>24)&0xff) as u8; b[off+1]=((v>>16)&0xff) as u8; b[off+2]=((v>>8)&0xff) as u8; b[off+3]=(v&0xff) as u8; return off+4 } |
| 34 | func st_get16(b: *u8, off: i64) -> i64 { return (((b[off] as i64) & 0xff)<<8) | ((b[off+1] as i64) & 0xff) } |
| 35 | func st_get32(b: *u8, off: i64) -> i64 |
| 40 | func st_write_header(b: *u8, msg_type: i64, msg_len: i64, txid: *u8) -> i64 called by 9: ic_build_checkic_build_success_responsemainsc_queryg_respondmain+3 calls 2: st_put16st_put32 |
| 48 | func st_type(b: *u8) -> i64 { return st_get16(b, 0) } |
| 49 | func st_len(b: *u8) -> i64 { return st_get16(b, 2) } |
| 50 | func st_magic(b: *u8) -> i64 { return st_get32(b, 4) } |
| 53 | func st_xma_encode(out: *u8, port: i64, addr: i64) -> i64 |
| 62 | func st_xma_family(val: *u8) -> i64 { return (val[1] as i64) & 0xff } called by 1: main |
| 63 | func st_xma_port(val: *u8) -> i64 { return (st_get16(val,2) ^ ((STUN_MAGIC>>16)&0xffff)) & 0xffff } |
| 64 | func st_xma_addr(val: *u8) -> i64 { return (st_get32(val,4) ^ STUN_MAGIC) & 0xffffffff } |
| 67 | func nx_crc32(data: *u8, len: i64) -> i64 |
| 83 | func st_fingerprint(msg: *u8, prefix_len: i64) -> i64 { return (nx_crc32(msg, prefix_len) ^ STUN_FP_XOR) & 0xffffffff } called by 7: ic_build_checkic_verify_fingerprintic_build_success_responseg_respondmainss_build_response+1 calls 1: nx_crc32 |
| 87 | func st_message_integrity(key: *u8, klen: i64, msg: *u8, prefix_len: i64, out20: *u8) -> i64 |