]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
feat: DHCP client via lwIP (net_dhcp_start with 10s timeout)
authorTulio A M Mendes <[email protected]>
Sun, 15 Feb 2026 01:18:19 +0000 (22:18 -0300)
committerTulio A M Mendes <[email protected]>
Sun, 15 Feb 2026 01:18:19 +0000 (22:18 -0300)
- Added dhcp.c and acd.c to lwIP build sources
- net_dhcp_start() starts DHCP on E1000 netif, waits up to 10s
- Falls back to static IP if DHCP times out
- LWIP_DHCP already enabled in lwipopts.h
- 35/35 smoke tests pass, cppcheck clean

Makefile
include/net/lwipopts.h
src/net/e1000_netif.c

index 68eebaafb3e6749e0c9f4cf420a8d6dc34bf31ce..4c4bcce03b755561de39b337c149faee6bd5a837 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,8 @@ ifeq ($(ARCH),x86)
         $(LWIPDIR)/core/udp.c $(LWIPDIR)/core/dns.c
     LWIP_IPV4 := $(LWIPDIR)/core/ipv4/etharp.c $(LWIPDIR)/core/ipv4/icmp.c \
         $(LWIPDIR)/core/ipv4/ip4.c $(LWIPDIR)/core/ipv4/ip4_addr.c \
-        $(LWIPDIR)/core/ipv4/ip4_frag.c
+        $(LWIPDIR)/core/ipv4/ip4_frag.c $(LWIPDIR)/core/ipv4/dhcp.c \
+        $(LWIPDIR)/core/ipv4/acd.c
     LWIP_NETIF := $(LWIPDIR)/netif/ethernet.c
     LWIP_API := $(LWIPDIR)/api/api_lib.c $(LWIPDIR)/api/api_msg.c \
         $(LWIPDIR)/api/err.c $(LWIPDIR)/api/if_api.c $(LWIPDIR)/api/netbuf.c \
index 01d12ecf1cfbdb5fd2587b73db84c1d518b7634b..f47b10dce7a9df7a384a9dec0318ad1ba4b9d625 100644 (file)
@@ -45,7 +45,7 @@
 #define LWIP_ICMP               1
 #define LWIP_UDP                1
 #define LWIP_TCP                1
-#define LWIP_DHCP               0
+#define LWIP_DHCP               1
 #define LWIP_AUTOIP             0
 #define LWIP_DNS                1
 #define LWIP_NO_CTYPE_H         1
index 30c32a7edd05771b2cd65535036d6f58df7cfdc5..d8d48d3e6f10ec436321355df36205733236bdeb 100644 (file)
@@ -24,6 +24,7 @@
 #include "lwip/tcpip.h"
 #include "lwip/sys.h"
 #include "netif/ethernet.h"
+#include "lwip/dhcp.h"
 #include "spinlock.h"
 
 #include "e1000.h"
@@ -157,6 +158,33 @@ void net_init(void) {
     kprintf("[NET] lwIP initialized (interrupt-driven RX), IP=10.0.2.15\n");
 }
 
+void net_dhcp_start(void) {
+    if (!net_initialized) {
+        kprintf("[NET] DHCP: network not initialized.\n");
+        return;
+    }
+
+    kprintf("[NET] Starting DHCP client...\n");
+    dhcp_start(&e1000_nif);
+
+    /* Wait up to 10 seconds for DHCP to complete */
+    extern uint32_t get_tick_count(void);
+    uint32_t deadline = get_tick_count() + 500; /* 10s at ~50Hz */
+    while (get_tick_count() < deadline) {
+        if (dhcp_supplied_address(&e1000_nif)) {
+            kprintf("[NET] DHCP: got IP %d.%d.%d.%d\n",
+                    ip4_addr1(netif_ip4_addr(&e1000_nif)),
+                    ip4_addr2(netif_ip4_addr(&e1000_nif)),
+                    ip4_addr3(netif_ip4_addr(&e1000_nif)),
+                    ip4_addr4(netif_ip4_addr(&e1000_nif)));
+            return;
+        }
+        extern void process_sleep(uint32_t ticks);
+        process_sleep(5);
+    }
+    kprintf("[NET] DHCP: timeout, keeping static IP.\n");
+}
+
 void net_poll(void) {
     /* No-op: RX is now handled by the interrupt-driven rx_thread.
      * Kept for backward compatibility — callers can safely remove. */