]> 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 4a58b2d490dbbab06898f434219839d9f66f0dde..4848a49962a8d846ccb56ed983ccf1143b55c5f6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,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 8e54bec05f3a92b6ba7fdc4f6da8d26646b1ad19..ec99f040569751ea15a533d3ee37e4bfb95fc409 100644 (file)
@@ -36,7 +36,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 8631cd407e53fadb44747642ce493d24894f1d64..df2351ca320a97e238a51c662ef0a33c56d113de 100644 (file)
@@ -15,6 +15,7 @@
 #include "lwip/tcpip.h"
 #include "lwip/sys.h"
 #include "netif/ethernet.h"
+#include "lwip/dhcp.h"
 #include "spinlock.h"
 
 #include "e1000.h"
@@ -148,6 +149,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. */