cybersecurity · intermediate · ~20 min
Defender's /proc/net/tcp parser — the basis of ss / netstat.
/proc/net/tcp formats each TCP socket as one line. Columns we care about:
sl local_address rem_address st ...
0: 00000000:0050 00000000:0000 0A ...
local_address is IIIIIIII:PPPP where I is the IPv4 address (in
host byte order, but the hex digits are little-endian per nibble — the
canonical form is reversed-byte) and P is the port.
For this exercise we simplify: implement
int is_public_listen(const char *line) returning 1 if the line:
0A (LISTEN), and00000000: (i.e. bound to 0.0.0.0).Else return 0. Ignore the leading sl index and whitespace.
The single most useful Linux defender query is 'what's listening?'. /proc/net/tcp answers it; parsing it lets you write the same tool that powers ss/netstat.
One line of /proc/net/tcp.
0/1.
Pure string scan.
int is_public_listen(const char *line) { /* TODO */ (void)line; return 0; }
Comparing the state column without skipping the address columns.
Line with extra whitespace; line for IPv6 (different format — return 0).
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.