feat(net): real UDP transport, BSD datagram sockets, DNS resolver, DHCP client

Turn the networking stubs into a working transport/application stack on top of
the existing L2/L3 (Ethernet/ARP/IPv4/ICMP) core.

UDP (net/ipv4/udp.c)
  - Port-demux table with bound receive handlers, pseudo-header checksum,
    ephemeral-port allocation, limited-broadcast send (0.0.0.0 -> 255.255.255.255
    for DHCP before the interface has an address), and rx/tx/drop counters.
  - Wired into the IPv4 receive path (IP_PROTO_UDP dispatch in net/core/net.c).

BSD sockets (net/core/socket.c)
  - Real SOCK_DGRAM sockets backed by UDP: bind installs a UDP handler that fills
    a per-socket receive ring; send/sendto and recv/recvfrom move datagrams;
    ephemeral auto-bind, connect() memorises the peer. Socket table + I/O stats
    exposed for introspection.

DNS resolver (net/ipv4/dns.c)
  - Builds and parses real A-record queries over UDP/53 (label encoding,
    compression-pointer aware answer walk), cache-first with TTL, static-hosts
    and dotted-quad fast paths, and a bounded timeout that degrades to fallback
    instead of hanging.

DHCP client (net/ipv4/dhcp.c)
  - Full DISCOVER/OFFER/REQUEST/ACK handshake over UDP broadcast, option parsing
    (mask/router/dns/lease/server-id), applies the lease to the primary
    interface, bounded timeouts keep the static config on no response.

Fix (net/ipv4/dns_cache.c): the cache stored IPs as 0xAABBCCDD numeric while the
rest of the stack uses network-byte-order-in-memory (ip_parse/ip_to_str), so a
resolved address came back byte-swapped (nslookup github.com -> 100.121.82.140).
Seeds and dump now use ip_parse/ip_to_str; verified 140.82.121.4.

Userland (lazybox): live ifconfig (real interface + NIC counters, loopback),
netstat with live UDP bindings and transport stats, new `sockstat`, richer
nslookup. Verified in QEMU: boot clean on x86_64/aarch64/armv8i, login as SUB,
ping/nslookup/dhclient/netstat/sockstat all run; DHCP/DNS wire replies are
blocked by the sandbox's slirp but time out cleanly and fall back as designed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fh5z6V7nGPbHvqVG5p4CXX
This commit is contained in:
subhobhai943andClaude Opus 4.8 committed 2026-08-26 10:20:58 +05:30
1 parent 2c2e2d536e
commit cd840d5799
10 files changed
+924 -57

No files matched your search

+15 -12
View File
@@ -1,5 +1,11 @@
// DNS Resolver Cache with TTL Management and Metrics for SUB-OS
// DNS Resolver Cache with TTL Management and Metrics for SUB-OS.
//
// IPs are stored in the same convention as the rest of the network stack:
// network byte order in memory (the first octet in the lowest byte), exactly as
// produced by ip_parse() and consumed by ip_to_str(). Storing them any other
// way would hand dns_resolve() callers a byte-swapped address.
#include <net/dns_cache.h>
#include <net/net.h>
#include <lib/string.h>
#include <lib/printf.h>
#include <kernel/printk.h>
@@ -10,12 +16,12 @@ static uint64_t total_hits = 0;
void dns_cache_init(void) {
memset(dns_table, 0, sizeof(dns_table));
// Seed common local and well-known hosts
dns_cache_insert("localhost", 0x7F000001, 86400); // 127.0.0.1
dns_cache_insert("gateway.local", 0x0A000202, 3600); // 10.0.2.2
dns_cache_insert("dns.google", 0x08080808, 7200); // 8.8.8.8
dns_cache_insert("one.one.one.one", 0x01010101, 7200); // 1.1.1.1
dns_cache_insert("github.com", 0x8C527964, 300); // 140.82.121.4
// Seed common local and well-known hosts (network byte order via ip_parse).
dns_cache_insert("localhost", ip_parse("127.0.0.1"), 86400);
dns_cache_insert("gateway.local", ip_parse("10.0.2.2"), 3600);
dns_cache_insert("dns.google", ip_parse("8.8.8.8"), 7200);
dns_cache_insert("one.one.one.one", ip_parse("1.1.1.1"), 7200);
dns_cache_insert("github.com", ip_parse("140.82.121.4"), 300);
printk(KERN_INFO "DNS: High-performance DNS Cache table initialized (Capacity: %d)\n", DNS_CACHE_MAX_ENTRIES);
}
@@ -63,7 +69,7 @@ void dns_cache_insert(const char* hostname, uint32_t ip, uint32_t ttl) {
void dns_cache_flush(void) {
memset(dns_table, 0, sizeof(dns_table));
dns_cache_insert("localhost", 0x7F000001, 86400);
dns_cache_insert("localhost", ip_parse("127.0.0.1"), 86400);
printk(ANSI_YELLOW "DNS: Resolver cache flushed.\n" ANSI_RESET);
}
@@ -78,11 +84,8 @@ void dns_cache_dump(void) {
for (int i = 0; i < DNS_CACHE_MAX_ENTRIES; i++) {
if (dns_table[i].active) {
uint32_t ip = dns_table[i].ip;
char ip_str[20];
snprintf(ip_str, sizeof(ip_str), "%u.%u.%u.%u",
(ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
(ip >> 8) & 0xFF, ip & 0xFF);
ip_to_str(dns_table[i].ip, ip_str);
printk("%-28s " ANSI_BRIGHT_YELLOW "%-16s" ANSI_RESET " %8u %8llu\n",
dns_table[i].hostname, ip_str,