# Writing an OS Kernel from Scratch – Part 35: TCP/IP Protocol Stack — Using socket to GET the Baidu Homepage!
“The NIC driver is just the hardware interface; real networking capability lies in the protocol stack. Today, we implement a minimal TCP/IP stack and use a user-mode program to download the Baidu homepage!”
In the previous article, we completed the e1000 NIC driver + PCIe support, able to send raw Ethernet frames, but unable to handle higher-layer protocols.
Real network applications need the TCP/IP protocol stack:
IP layer: routing, fragmentation
TCP layer: reliable transmission, flow control, retransmission
Today, we will: ✅ Implement a minimal IP/TCP protocol stack ✅ Provide a BSD socket API ✅ Write a user-mode HTTP client ✅ GET the Baidu homepage and save it to a file
Give your OS true internet capability!
🌐 1. Protocol Stack Architecture Design
Layered model (bottom-up):
Key design principles:
Minimal: only supports IPv4 + TCP (no UDP/ICMP/ARP cache)
User-mode friendly: provides standard socket/connect/send/recv
Single-threaded: no concurrent connections (simplifies state machine)
💡 Linux’s net/ipv4/ is the full implementation; we’re building the minimum usable subset!
📦 2. IP Layer Implementation
1. IP header structure
2. Sending IP packets
⚠️ For simplicity, the IP layer does not handle fragmentation, routing tables; MAC address uses broadcast!
🔁 3. TCP Layer Implementation
1. TCP connection state machine
2. TCP Control Block (TCB)
3. Sending TCP segments
4. Processing received TCP packets
🔑 TCP state machine simplified: only supports active connection (Client), no listening (Server)!
📞 4. BSD Socket API
1. System calls
🌍 5. User-Mode HTTP Client
1. GET request encapsulation
2. Main program
🧪 6. Test: Download the Baidu Homepage
1. QEMU startup
💡 hostfwd maps QEMU port 80 to host port 8080, but Baidu needs direct external network access. Ensure QEMU has internet access!
2. Run results
✅ Successfully performed TCP GET of Baidu homepage!
⚠️ 7. Simplifications and Limitations
1. No ARP: MAC address uses broadcast (same subnet only)
2. No retransmission: packet loss means failure
3. No congestion control: simple fixed window
4. No DNS: needs hardcoded IP
5. Single connection: no concurrent support
💡 These are reasonable simplifications for a teaching implementation; industrial-grade implementations need to complete them!
💬 Final Thoughts
The TCP/IP protocol stack is the universal language of the networking world. It enables different devices to communicate reliably, building today’s internet.
The first GET request you implemented today is the starting point of countless network applications.
🌟 The significance of the protocol stack lies not in the code, but in connecting the world.
📬 Hands-on challenge: Add a DNS client to make http_get(“www.baidu.com”, …) automatically resolve IP addresses. Share your network packet analysis in the comments!
👇 Next article you’d like to see: UDP and DHCP Client, or C Standard Library (libc) implementation?
#OS #KernelDevelopment #TCPIP #NetworkProtocolStack #socket #HTTP #WritingFromScratch
Comments