Master the fundamental concepts of tcp/ip from scratch through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksTUN and TAP are virtual kernel interfaces that let user-space programs inject and read raw packets. TUN works at layer 3 (IP packets to 10.0.0.1 with no Ethernet header). TAP works at layer 2 (full Ethernet frames with MAC addresses and EtherType 0x0800).
cLoading…
Flags:
IFF_TUN: layer 3, IP packets onlyIFF_TAP: layer 2, Ethernet framesIFF_NO_PI: skip the 4-byte packet info headerRead and write the fd like a file: read(fd, buf, 1500) returns an IP packet; write(fd, buf, len) injects one into the kernel routing table.
This task asks you to set up a TUN interface and demonstrate packet I/O. This is the same kernel interface that lets you build and test a user-space TCP stack without touching kernel code. WireGuard creates a TUN device to intercept IP packets bound for 10.0.0.0/24 before encrypting and tunneling them over UDP to a peer endpoint.
Given an IPv4 packet descriptor, decide whether it crosses a TUN (layer 3) or TAP (layer 2) interface, compute the framing that interface carries, and compute the IPv4 header checksum (RFC 1071). A TAP device wraps the IP packet in a 14-byte Ethernet header (EtherType 0x0800); a TUN device carries the IP packet bare, so what read() returns is exactly the IP packet.
Input (stdin): one line: src_ip dst_ip payload_len ttl protocol has_mac. has_mac = 0 means TUN (the stack hands us a bare IP packet); has_mac = 1 means TAP (an Ethernet frame is needed). The IP header is the standard 20-byte form: version 4, IHL 5, TOS 0, identification 0, DF set, no fragment offset.
Output (stdout):