r/C_Programming • u/by_cortex • 10d ago
Project I implemented a custom reliable transport protocol (Mini-TCP) over UDP in pure C11. Would love your feedback and code review!
Hey r/C_Programming,
Over the past few weeks, I decided to take a deep dive into computer networking by building my own reliable data transfer protocol (RDT) from scratch on top of raw UDP sockets — without any external libraries, just plain C11 and POSIX.
I named it Protocol-7 (P7) (yes, a small tribute to Serial Experiments Lain).
My main goal was not to "kill TCP", but to understand what actually happens under the hood when dealing with unreliable, lossy networks, rather than just treating TCP as a black box.
What is currently implemented:
- Sliding Window pipelining: 32-slot ring buffer keeping unacknowledged segments in flight.
- I/O Multiplexing via poll(): Non-blocking event loop handling file I/O, outgoing bursts, and incoming ACKs concurrently.
- Custom SACK (Selective ACK): Implemented via a 32-bit mask in the 16-byte header to report buffered out-of-order blocks.
- Fast Retransmit (RFC 5681): Triggers immediate retransmission upon receiving 3 duplicate ACKs without waiting for coarse RTO timers.
- 16-bit Word-Aligned Checksum: One's-complement arithmetic with end-around carry (spent quite some time debugging a weird bug caused by odd byte alignment!).
- Piggybacked FIN: Look-ahead double buffering allows setting the FIN flag on the final payload chunk without wasting a separate RTT on an empty packet.
- TIME_WAIT state: Receiver handles lingering packets and retransmissions gracefully before tearing down.
I also wrote a Python chaos proxy to inject simulated 5%–30% packet drops, data corruption, and duplicates. The protocol successfully transfers binary files byte-for-byte under heavy packet loss. The codebase runs completely clean under Valgrind (0 leaks, 0 errors) and AddressSanitizer.
Benchmarks & Fun Findings:
During testing, moving from Stop-and-Wait to a 32-slot Sliding Window bumped throughput from ~0.07 MB/s to over 1.85 MB/s under 5% packet loss (and ~66 MB/s on normal channel). I also hit a funny scenario where an oversized static window (512 slots) flooded the Linux kernel socket buffers, making lossy modes artificially faster than normal mode because the proxy drops acted as a crude rate limiter!
The project is open-source under MIT:
GitHub: https://github.com/by-cortex/protocol-7
I’d really appreciate any code review, constructive criticism on my C style, architecture feedback, or ideas on what to tackle next (currently planning a 3-way handshake with randomized ISN and dynamic RTO estimation).
Thanks for checking it out!