nx_wire_logger.nx source
↩ module page · 57 lines · 1777 B
1// nx_wire_logger.nx -- bits-up TCP wire logger.
2//
3// Replaces external packet capture tools (Wireshark, tcpdump) with a
4// native NishiLang primitive that interposes on send/recv at the TCP
5// fd boundary. Direction-labeled hex dumps go to a chosen log fd
6// (typically stderr=2 or a dedicated file).
7//
8// Substrate-honesty principle: any time we'd reach for Wireshark to
9// "see the bytes," we owe ourselves a native primitive. This is
10// that primitive.
11//
12// API:
13// nx_wire_send(fd, log_fd, label, label_len, buf, len) -> i64
14// Calls sys_write(fd, buf, len); if log_fd > 0, hex-dumps the
15// same bytes to log_fd with the label prefix. Returns sys_write
16// result (bytes written, or negative on error).
17//
18// nx_wire_recv(fd, log_fd, label, label_len, buf, max) -> i64
19// Calls sys_read(fd, buf, max); if log_fd > 0, hex-dumps the
20// first n bytes (n = read result) to log_fd. Returns sys_read
21// result.
22//
23// Per Cardinals 9, 22. Composes nx_hex_dump.
24//
25// license_tier: ORIGINAL
26// lineage_id: nishi_wire_logger_q10
27
28import "nx_syscalls.nx"
29import "nx_hex_dump.nx"
30
31func nx_wire_send(fd: i64, log_fd: i64,
32 label: *u8, label_len: i64,
33 buf: *u8, len: i64) -> i64 {
34 let rc: i64 = sys_write(fd, buf, len)
35 if log_fd > 0 {
36 if rc > 0 {
37 nx_hex_dump_to_fd(log_fd, label, label_len, buf, rc)
38 }
39 }
40 return rc
41}
42
43func nx_wire_recv(fd: i64, log_fd: i64,
44 label: *u8, label_len: i64,
45 buf: *u8, max: i64) -> i64 {
46 let rc: i64 = sys_read(fd, buf, max)
47 if log_fd > 0 {
48 if rc > 0 {
49 nx_hex_dump_to_fd(log_fd, label, label_len, buf, rc)
50 }
51 }
52 return rc
53}
54
55func main() -> i64 {
56 return 0
57}