cybersecurity · intermediate · ~20 min
Defender's /proc/net/tcp parser — the basis of ss / netstat.
Parse one line of Linux's /proc/net/tcp and decide whether it is a socket listening on a public interface — the core check behind tools like ss and netstat.
Each line looks like this (only the first columns matter here):
sl local_address rem_address st ...
0: 00000000:0050 00000000:0000 0A ...
local_address is IIIIIIII:PPPP — I is the IPv4 address in the kernel's hex form and P is the port. The third column (st) is the socket state, where 0A means LISTEN.
Implement int is_public_listen(const char *line) that returns 1 when the line is both:
0A (LISTEN), and0.0.0.0, i.e. its local_address begins with 00000000:.Otherwise return 0. Ignore the leading sl index and any whitespace.
line: one fixed line of /proc/net/tcp text the grader provides (a NUL-terminated string; may be empty).Returns 1 for a public LISTEN socket, 0 otherwise.
" 0: 00000000:0050 00000000:0000 0A ..." -> 1 (LISTEN on 0.0.0.0:80)
" 1: 0100007F:1F90 00000000:0000 0A ..." -> 0 (loopback 127.0.0.1 only)
" 2: 00000000:0050 0A0A0A0A:F123 01 ..." -> 0 (ESTABLISHED, not LISTEN)
"" -> 0
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 fixed line of /proc/net/tcp text (NUL-terminated; may be empty).
1 if the line is a LISTEN (state 0A) bound to 0.0.0.0; otherwise 0.
Pure string scan; ignore the leading sl index and whitespace.
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.