Skip to content

Commit 95b8e7e

Browse files
authored
Fixes DHCP Server Lease Range for any AP Server Static IP Address (#6296)
* Fixes DHCP Server Lease Range for any AP Server Static IP Address * Fixes DHCP in APMode when Static IP is out of subnet range
1 parent e8d6050 commit 95b8e7e

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

Diff for: libraries/WiFi/src/WiFiAP.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,7 @@ bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress
205205
}
206206

207207
err = set_esp_interface_ip(ESP_IF_WIFI_AP, local_ip, gateway, subnet);
208-
209-
// testing effectiveness of the operation beyond internal DHCP Client process
210-
esp_netif_ip_info_t ip;
211-
if(esp_netif_get_ip_info(get_esp_interface_netif(ESP_IF_WIFI_AP), &ip) != ESP_OK){
212-
log_e("Netif Get IP Failed!");
213-
return false;
214-
}
215-
bool ip_ok = IPAddress(ip.ip.addr) == local_ip;
216-
bool gw_ok = IPAddress(ip.gw.addr) == gateway;
217-
bool mk_ok = IPAddress(ip.netmask.addr) == subnet;
218-
219-
if (ip_ok && gw_ok && mk_ok) {
220-
return true;
221-
} else {
222-
log_e("Failed setting: %s %s %s", ip_ok ? "" : "Static IP", gw_ok ? "" : "- Gateway", mk_ok ? "" : "- Netmask");
223-
return false;
224-
}
208+
return err == ESP_OK;
225209
}
226210

227211

Diff for: libraries/WiFi/src/WiFiGeneric.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,20 @@ esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPA
138138

139139
dhcps_lease_t lease;
140140
lease.enable = true;
141-
lease.start_ip.addr = static_cast<uint32_t>(local_ip) + (1 << 24);
142-
lease.end_ip.addr = static_cast<uint32_t>(local_ip) + (11 << 24);
143-
141+
uint32_t dhcp_ipaddr = static_cast<uint32_t>(local_ip);
142+
// prevents DHCP lease range to overflow subnet/24 range
143+
// there will be 11 addresses for DHCP to lease
144+
uint8_t leaseStart = (uint8_t)(~subnet[3] - 12);
145+
if ((local_ip[3]) < leaseStart) {
146+
lease.start_ip.addr = dhcp_ipaddr + (1 << 24);
147+
lease.end_ip.addr = dhcp_ipaddr + (11 << 24);
148+
} else {
149+
// make range stay in the begining of the netmask range
150+
dhcp_ipaddr = (dhcp_ipaddr & 0x00FFFFFF);
151+
lease.start_ip.addr = dhcp_ipaddr + (1 << 24);
152+
lease.end_ip.addr = dhcp_ipaddr + (11 << 24);
153+
}
154+
log_v("DHCP Server Range: %s to %s", IPAddress(lease.start_ip.addr).toString(), IPAddress(lease.end_ip.addr).toString());
144155
err = tcpip_adapter_dhcps_option(
145156
(tcpip_adapter_dhcp_option_mode_t)TCPIP_ADAPTER_OP_SET,
146157
(tcpip_adapter_dhcp_option_id_t)REQUESTED_IP_ADDRESS,

0 commit comments

Comments
 (0)