Skip to content

Commit e928632

Browse files
NdK73d-a-v
authored andcommitted
Added support for user-supplied DHCP range, with basic sanity checks (esp8266#3562)
1 parent 65d60d4 commit e928632

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

+61-23
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
179179
* @param local_ip access point IP
180180
* @param gateway gateway IP
181181
* @param subnet subnet mask
182+
* @param dhcp_start first IP assigned by DHCP
183+
* @param dhcp_end last IP assigned by DHCP
182184
*/
183-
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
185+
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end) {
184186
DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str());
185187
if(!WiFi.enableAP(true)) {
186188
// enable AP failed
@@ -204,35 +206,52 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
204206
}
205207

206208
struct dhcps_lease dhcp_lease;
207-
IPAddress ip = local_ip;
208-
ip[3] += 99;
209-
dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
210-
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
211209

212-
ip[3] += 100;
213-
dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
214-
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
210+
uint32_t net_addr = info.ip.addr & info.netmask.addr;
211+
uint32_t bcast_addr = net_addr | !info.netmask.addr;
215212

216-
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
217-
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
218-
ret = false;
219-
}
213+
// Assign user-supplied range, checking its validity
214+
IPAddress ip = (static_cast<uint32_t>(dhcp_start) & !info.netmask.addr) | net_addr;
220215

221-
// set lease time to 720min --> 12h
222-
if(!wifi_softap_set_dhcps_lease_time(720)) {
223-
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
224-
ret = false;
216+
dhcp_lease.start_ip.addr = ip;
217+
if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) {
218+
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
219+
} else {
220+
dhcp_lease.start_ip.addr=0;
225221
}
226222

227-
uint8 mode = 1;
228-
if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
229-
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
230-
ret = false;
223+
ip = (static_cast<uint32_t>(dhcp_end) & !info.netmask.addr) | net_addr;
224+
dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
225+
if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) {
226+
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
227+
} else {
228+
dhcp_lease.end_ip.addr=0;
231229
}
232230

233-
if(!wifi_softap_dhcps_start()) {
234-
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
235-
ret = false;
231+
if(dhcp_lease.start_ip.addr && dhcp_lease.end_ip.addr) {
232+
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
233+
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
234+
ret = false;
235+
}
236+
237+
// set lease time to 720min --> 12h
238+
if(!wifi_softap_set_dhcps_lease_time(720)) {
239+
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
240+
ret = false;
241+
}
242+
243+
uint8 mode = 1;
244+
if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
245+
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
246+
ret = false;
247+
}
248+
249+
if(!wifi_softap_dhcps_start()) {
250+
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
251+
ret = false;
252+
}
253+
} else {
254+
DEBUG_WIFI("[APConfig] DHCP daemon not started (range error or user request)\n");
236255
}
237256

238257
// check config
@@ -254,6 +273,25 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
254273
}
255274

256275

276+
/**
277+
* Configure access point
278+
* @param local_ip access point IP
279+
* @param gateway gateway IP
280+
* @param subnet subnet mask
281+
*/
282+
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
283+
IPAddress dhcp_start;
284+
IPAddress dhcp_end;
285+
286+
// calculate dhcp_start and DHCP_end as done in the old code
287+
dhcp_start = local_ip;
288+
dhcp_start[3] += 99;
289+
dhcp_end = dhcp_start;
290+
dhcp_end[3] += 100;
291+
292+
softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end);
293+
}
294+
257295

258296
/**
259297
* Disconnect from the network (close AP)

libraries/ESP8266WiFi/src/ESP8266WiFiAP.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ESP8266WiFiAPClass {
3737
public:
3838

3939
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
40+
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end);
4041
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
4142
bool softAPdisconnect(bool wifioff = false);
4243

0 commit comments

Comments
 (0)