From 45fa615b9ca657a689f665721b23464ab35910e9 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 31 May 2025 08:50:01 -0700 Subject: [PATCH] Send DHCP request on ::begin even if lease exist When a link comes up, we were only sending a DHCP request if the existing netif's ipaddress was 0. When a DHCP lease is acquired at first that IP is changed to the given address, and if we do another ::begin we wouldn't dhcp_start to send a new request and use the old one (until its lease expired). In the case of network changing (i.e. WiFiMulti on different nets or moving an Ethernet cable to another router) that old address would not be valid. Track if an IP address has been manually set and use that to determine if DHCP needs to be re-requested instead of looking at the old netif's ipaddress. Fixes #2974 --- libraries/lwIP_Ethernet/src/LwipIntfDev.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/lwIP_Ethernet/src/LwipIntfDev.h b/libraries/lwIP_Ethernet/src/LwipIntfDev.h index 30205b68b..9554fc2d5 100644 --- a/libraries/lwIP_Ethernet/src/LwipIntfDev.h +++ b/libraries/lwIP_Ethernet/src/LwipIntfDev.h @@ -186,6 +186,7 @@ class LwipIntfDev: public LwipIntf, public RawDev { SPIClass& _spiUnit; SPISettings _spiSettings = SPISettings(4000000, MSBFIRST, SPI_MODE0); netif _netif; + bool _isDHCP = true; uint16_t _mtu; int8_t _intrPin; @@ -282,6 +283,8 @@ bool LwipIntfDev::config(const IPAddress& localIP, const IPAddress& gate return false; } + _isDHCP = (localIP.v4() == 0); + IPAddress realGateway, realNetmask, realDns1; if (!ipAddressReorder(localIP, gateway, netmask, dns1, realGateway, realNetmask, realDns1)) { return false; @@ -389,7 +392,7 @@ bool LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu) { _phID = __addEthernetPacketHandler([this] { this->handlePackets(); }); } - if (localIP().v4() == 0) { + if (_isDHCP) { // IP not set, starting DHCP _netif.flags |= NETIF_FLAG_UP; switch (dhcp_start(&_netif)) {