A TCP/IP stack implementation in Rust from scratch, built on top of a TUN device.
This project implements the core TCP protocol following RFC 793 specifications. It creates a virtual network interface (TUN device) and handles TCP connections at the packet level, including connection establishment (three-way handshake) and state management.
- Custom TCP state machine (Listen, SynRcvd, Closed)
- Three-way handshake implementation (SYN, SYN-ACK, ACK)
- Send and Receive Sequence Space management
- Connection tracking using a quad (source IP:port, destination IP:port)
- Raw packet parsing and construction
etherparse- For parsing and constructing IP and TCP headerstun-tap- For creating and managing TUN/TAP devices
- Creates a TUN device named "tun0"
- Listens for incoming packets on the interface
- Parses IPv4 and TCP headers
- Manages TCP connections in a HashMap indexed by connection quads
- Handles TCP state transitions and sends appropriate responses
# Run with appropriate permissions (requires root/sudo for TUN device creation)
# Better to run the shell command ./run.sh
/run.shmain.rs- Main event loop, packet reception, and connection managementtcp.rs- TCP connection state machine and packet handling logic
This project is designed to understand:
- How TCP works at a low level
- Network packet structure and parsing
- State machine implementation
- Systems programming in Rust