๐Ÿ”Œ

TCP from Scratch

C++23 & Rust โ€” RFC 793 Implementation

A complete TCP/IP stack over raw Linux AF_PACKET sockets โ€” no OS TCP involvement

C++23 Rust (stable) AF_PACKET RFC 793 RFC 9293 RFC 6298 Timers
2
Full Implementations
10
Integration Tests
5
RFCs Implemented

github.com/sidx007/TCP-in-Cpp-and-Rust

Overview

What's implemented

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.

State Machine

Full RFC 793/9293 TCP states โ€” SYN_SENT, ESTABLISHED, FIN_WAIT, TIME_WAIT, simultaneous close, RST handling.

Reliability

Retransmission with RFC 6298 SRTT/RTTVAR/RTO. Sliding window flow control. Delayed ACK (200ms or every 2 segments).

Raw I/O

AF_PACKET SOCK_RAW sockets. ARP resolution for MAC discovery. RFC 1071 Internet checksum computed in software โ€” no hardware offload.

Threading

RX thread, timer thread (100ms), per-connection mutex. 64 KiB circular send/recv buffers. ISN from /dev/urandom.

Architecture

Stack Layers

Application Layer โ€” Echo server demo (port 9999)
โ†“
TCP Socket API โ€” connect / listen / accept / send / recv / close
โ†“
TCP Protocol Engine โ€” state machine ยท retransmit ยท timers
โ†“
Send Buffer โ€” 64 KiB circular
Recv Buffer โ€” 64 KiB circular
โ†“
IP / Checksum Layer โ€” IPv4 header build + TCP checksum
โ†“
Raw Socket I/O โ€” AF_PACKET SOCK_RAW read/write + ARP
Tests

Integration Tests T1โ€“T10

#TestWhat it checks
T13-way handshakeConnect succeeds, reaches ESTABLISHED
T2Echo small msg"hello" โ†’ "hello" round-trip
T3Echo 1 MBSHA-256 match, zero corruption
T4Clean closeFIN/ACK exchange, CLOSED state
T5RST handlingAbrupt close tears down connection
T6Retransmitiptables packet loss โ€” transfer still completes
T7Checksum rejectCorrupt byte silently dropped
T8Simultaneous connectTwo custom-stack peers connect each other
T950 connectionsNo resource leak across sequential connections
T10TIME_WAIT expiryPort reusable after 2ร—MSL (60s)
C++23 vs Rust

Two implementations, same spec

โš™๏ธ C++23
Build: CMake + GCC/Clang
No external deps โ€” pure libc + STL
Modules: raw_socket, ip, tcp_header, tcp_buffer, tcp_connection, tcp_listener, tcp_stream, timer
Run: sudo ./build/echo_server eth0
๐Ÿฆ€ Rust (stable)
Build: Cargo
Only libc, log, env_logger
All unsafe isolated in raw_socket.rs โ€” libc socket calls only
Run: sudo cargo run --bin echo_server -- eth0

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.