hn.today

What happens if you write a TCP stack in Python? (2014)

jvns.ca10 points0 comments
Screenshot of What happens if you write a TCP stack in Python? (2014)

A Hacker School project explores TCP by building a miniature network stack in Python with Scapy, aiming to request and retrieve a webpage without relying on the operating system’s TCP implementation. The account explains the basic handshake - SYN, SYN-ACK, ACK - and why TCP sequence numbers and acknowledgments track delivered data and allow missing packets to be resent. The first attempt fails in an unexpected way: after the program sends a SYN and receives a SYN-ACK, the computer’s kernel sends a reset because it sees a connection it did not initiate. To prevent this interference, the experiment uses ARP spoofing and a different apparent IP address, so the kernel ignores the incoming traffic and the Python program can complete the handshake.

After that, getting Google’s HTML requires assembling an HTTP GET, handling multiple response packets, correcting sequence-number bugs, and closing the connection. The resulting implementation is fragile but works: a curl-like program retrieves a page about a quarter of the time. Packet captures also reveal that the server sends data faster than the Python program appears to acknowledge it; the original explanation blames Python’s speed, though the author later notes that diagnosis may be wrong. The larger lesson is that TCP is far more involved than its basic handshake suggests, yet a rough implementation can still communicate with real servers. Writing it in Python is presented as a valuable, enjoyable way to learn networking, not as a practical approach to production TCP software.

Read on jvns.ca0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Aristotle Expands into Rust

Aristotle has expanded its formal verification platform to Rust, allowing developers to prove code meets specifications and contains no undefined behavior, including in unsafe blocks. It can also verify that Rust rewrites behave like their original C code, targeting critical software such as smart contracts and infrastructure. (harmonic.fun)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.