TCP from Scratch
C++23 & Rust โ RFC 793 Implementation
A complete TCP/IP stack over raw Linux AF_PACKET sockets โ no OS TCP involvement
github.com/sidx007/TCP-in-Cpp-and-Rust
C++23 & Rust โ RFC 793 Implementation
A complete TCP/IP stack over raw Linux AF_PACKET sockets โ no OS TCP involvement
github.com/sidx007/TCP-in-Cpp-and-Rust
Two self-contained TCP stacks โ identical logic, different languages. Both open raw Ethernet sockets directly and implement the full TCP state machine in user space. The OS kernel is bypassed entirely for protocol logic.
Full RFC 793/9293 TCP states โ SYN_SENT, ESTABLISHED, FIN_WAIT, TIME_WAIT, simultaneous close, RST handling.
Retransmission with RFC 6298 SRTT/RTTVAR/RTO. Sliding window flow control. Delayed ACK (200ms or every 2 segments).
AF_PACKET SOCK_RAW sockets. ARP resolution for MAC discovery. RFC 1071 Internet checksum computed in software โ no hardware offload.
RX thread, timer thread (100ms), per-connection mutex. 64 KiB circular send/recv buffers. ISN from /dev/urandom.
| # | Test | What it checks |
|---|---|---|
| T1 | 3-way handshake | Connect succeeds, reaches ESTABLISHED |
| T2 | Echo small msg | "hello" โ "hello" round-trip |
| T3 | Echo 1 MB | SHA-256 match, zero corruption |
| T4 | Clean close | FIN/ACK exchange, CLOSED state |
| T5 | RST handling | Abrupt close tears down connection |
| T6 | Retransmit | iptables packet loss โ transfer still completes |
| T7 | Checksum reject | Corrupt byte silently dropped |
| T8 | Simultaneous connect | Two custom-stack peers connect each other |
| T9 | 50 connections | No resource leak across sequential connections |
| T10 | TIME_WAIT expiry | Port reusable after 2รMSL (60s) |
Note: Both require CAP_NET_RAW or root. Set iptables to drop kernel RSTs on port 9999 before testing โ otherwise the OS will interfere with your handshake.