Networking in C · intermediate · ~12 min
Find the hostname inside a TLS Server Name Indication extension.
Two big-endian length reads + a type check, then a bounded copy of the host bytes.
SNI is the one plaintext hostname in a TLS session — the field monitors and filters depend on.
TLS encrypts everything — except the ClientHello, which is sent in the clear before keys exist. Inside it, the SNI extension carries the target hostname in plaintext. That's the field every passive TLS monitor, SNI-based filter, and traffic classifier reads.
[server_name_list_len : 2] big-endian
[name_type : 1] 0x00 = host_name
[name_len : 2] big-endian
[host bytes ...]
All multi-byte lengths are big-endian (network byte order).
Implement int extract_sni(const uint8_t *ext, size_t n, char *out, size_t cap)
that reads the list length, checks the type is 0x00, reads the host length,
and copies the host into out — validating every length against n and the
output against cap.
list_len, type==0, name_len, copy host. Big-endian; bounds-check against n and cap.