IPv4 Network Stack

Complete TCP/IP implementation

Full IPv4 Stack

guideXOS includes a complete IPv4 network stack written from scratch in C#. It provides all essential protocols for internet connectivity:

  • Ethernet - Layer 2 frame handling
  • ARP - Address Resolution Protocol
  • IPv4 - Internet Protocol v4
  • ICMP - Ping and error messages
  • UDP - User Datagram Protocol
  • TCP - Transmission Control Protocol
  • DHCP - Dynamic IP configuration
  • DNS - Domain name resolution
  • HTTP - Basic web client
✨ Features:

- Ping remote hosts
- Browse websites with built-in browser
- Connect to IRC servers
- Resolve domain names
- Automatic DHCP configuration


Protocol Stack Layers

The network stack follows the OSI model architecture:

┌─────────────────────────────────────┐
│  Layer 7: Application (HTTP, IRC)  │
├─────────────────────────────────────┤
│  Layer 4: Transport (TCP, UDP)     │
├─────────────────────────────────────┤
│  Layer 3: Network (IPv4, ICMP)     │
├─────────────────────────────────────┤
│  Layer 2: Data Link (Ethernet, ARP)│
├─────────────────────────────────────┤
│  Layer 1: Physical (NIC driver)    │
└─────────────────────────────────────┘

Packet Flow

// Outbound (Sending)
Application creates data
TCP adds header (ports, sequence, checksum)
IPv4 adds header (source/dest IP, TTL)
ARP resolves destination MAC address
Ethernet adds header (source/dest MAC)
NIC driver sends via DMA

// Inbound (Receiving)
NIC driver receives packet (IRQ)
Ethernet processes header
IPv4 processes header, routes to protocol
TCP/UDP/ICMP processes payload
Application receives data

Supported Protocols

Protocol Layer Purpose Status
Ethernet Layer 2 Frame transmission ✓ Full
ARP Layer 2 IP→MAC resolution ✓ Full
IPv4 Layer 3 Routing, addressing ✓ Full
ICMP Layer 3 Ping, diagnostics ✓ Full
UDP Layer 4 Connectionless transport ✓ Full
TCP Layer 4 Reliable stream ✓ Full
DHCP Application Auto IP config ✓ Client only
DNS Application Name resolution ✓ Client only
HTTP Application Web requests ✓ Basic GET

DHCP Client

DHCP automatically configures network settings without manual configuration:

DHCP Process

1. DISCOVER - Broadcast to find DHCP server
2. OFFER - Server offers IP address
3. REQUEST - Client requests offered IP
4. ACK - Server acknowledges, client configures
Network is now ready!

Configuration Received

  • IP Address - Your device's IP (e.g., 192.168.1.100)
  • Subnet Mask - Network size (e.g., 255.255.255.0)
  • Gateway - Router IP (e.g., 192.168.1.1)
  • DNS Server - Name resolution (e.g., 8.8.8.8)
  • Lease Time - How long configuration is valid

Usage

gx> netinit
Initializing network...
NIC detected: Intel PRO/1000
MAC Address: 52:54:00:12:34:56
Sending DHCP discover...
DHCP offer received: 192.168.1.100
Requesting IP...
DHCP ACK received!

Network Configuration:
  IP Address:   192.168.1.100
  Subnet Mask:  255.255.255.0
  Gateway:      192.168.1.1
  DNS Server:   192.168.1.1

Network ready!

DNS Resolver

The DNS client resolves domain names to IP addresses:

DNS Query Process

Application requests "google.com"
    ↓
DNS.Resolve("google.com")
    ↓
Create DNS query packet
    ↓
Send UDP packet to DNS server (port 53)
    ↓
Receive DNS response
    ↓
Parse A record for IPv4 address
    ↓
Return IP address (e.g., 142.250.80.46)

Supported Record Types

  • A Record - IPv4 address (e.g., google.com → 142.250.80.46)
  • CNAME - Alias (e.g., www.example.com → example.com)
  • MX Record - Mail server (partially supported)

Example Usage

gx> nslookup google.com
Querying DNS server 8.8.8.8...
google.com → 142.250.80.46

gx> nslookup github.com
Querying DNS server 8.8.8.8...
github.com → 140.82.113.4

gx> ping github.com
Resolving github.com...
PING 140.82.113.4: 56 bytes
Reply from 140.82.113.4: time=12ms TTL=54

TCP Implementation

Full TCP stack with reliable, ordered, connection-oriented communication:

TCP Features

  • 3-Way Handshake - SYN, SYN-ACK, ACK connection establishment
  • Reliable Delivery - Acknowledgments and retransmission
  • Sequence Numbers - Ordered packet delivery
  • Flow Control - Window size management
  • Congestion Control - Basic algorithm
  • Graceful Close - FIN/ACK teardown

Connection State Machine

CLOSED
  ↓ (connect)
SYN_SENT
  ↓ (receive SYN-ACK)
ESTABLISHED
  ↓ (send/receive data)
ESTABLISHED
  ↓ (close)
FIN_WAIT_1
  ↓ (receive ACK)
FIN_WAIT_2
  ↓ (receive FIN)
TIME_WAIT
  ↓ (2MSL timeout)
CLOSED

TCP Header

Field Size Purpose
Source Port 16 bits Sender port
Dest Port 16 bits Receiver port
Sequence Number 32 bits Byte order
ACK Number 32 bits Next expected byte
Flags 8 bits SYN/ACK/FIN/RST
Window Size 16 bits Flow control
Checksum 16 bits Error detection

Network Commands

Common network commands and their usage:

Initialize Network

gx> netinit
Starts network stack and runs DHCP

View Configuration

gx> ipconfig
Shows IP address, subnet, gateway, DNS

Ping Host

gx> ping 8.8.8.8
gx> ping google.com
Sends ICMP echo request

DNS Lookup

gx> nslookup example.com
Resolves domain to IP address

HTTP Request

gx> wget http://example.com
Downloads web page via HTTP GET
💡 Tip:

Use the Web Browser application for a graphical way to browse websites, or the IRC client to connect to chat servers!


Related Topics