From dd763d59464d7c55caace6b64f5aefbe0322d793 Mon Sep 17 00:00:00 2001 From: NdK Date: Fri, 1 Sep 2017 20:58:58 +0200 Subject: [PATCH 1/2] Added support for user-supplied DHCP range, with basic sanity checks --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 84 +++++++++++++++------ libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 1 + 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index a424eece10..cc5847dece 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -179,8 +179,10 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch * @param local_ip access point IP * @param gateway gateway IP * @param subnet subnet mask + * @param dhcp_start first IP assigned by DHCP + * @param dhcp_end last IP assigned by DHCP */ -bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end) { DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str()); if(!WiFi.enableAP(true)) { // enable AP failed @@ -204,35 +206,52 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA } struct dhcps_lease dhcp_lease; - IPAddress ip = local_ip; - ip[3] += 99; - dhcp_lease.start_ip.addr = static_cast(ip); - DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); - ip[3] += 100; - dhcp_lease.end_ip.addr = static_cast(ip); - DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); + uint32_t net_addr = info.ip.addr & info.netmask.addr; + uint32_t bcast_addr = net_addr | !info.netmask.addr; - if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { - DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); - ret = false; - } + // Assign user-supplied range, checking its validity + IPAddress ip = (static_cast(dhcp_start) & !info.netmask.addr) | net_addr; - // set lease time to 720min --> 12h - if(!wifi_softap_set_dhcps_lease_time(720)) { - DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); - ret = false; + dhcp_lease.start_ip.addr = ip; + if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) { + DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); + } else { + dhcp_lease.start_ip.addr=0; } - uint8 mode = 1; - if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) { - DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); - ret = false; + ip = (static_cast(dhcp_end) & !info.netmask.addr) | net_addr; + dhcp_lease.end_ip.addr = static_cast(ip); + if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) { + DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); + } else { + dhcp_lease.end_ip.addr=0; } - if(!wifi_softap_dhcps_start()) { - DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n"); - ret = false; + if(dhcp_lease.start_ip.addr && dhcp_lease.end_ip.addr) { + if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { + DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); + ret = false; + } + + // set lease time to 720min --> 12h + if(!wifi_softap_set_dhcps_lease_time(720)) { + DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); + ret = false; + } + + uint8 mode = 1; + if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) { + DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); + ret = false; + } + + if(!wifi_softap_dhcps_start()) { + DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n"); + ret = false; + } + } else { + DEBUG_WIFI("[APConfig] DHCP daemon not started (range error or user request)\n"); } // check config @@ -254,6 +273,25 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA } +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + IPAddress dhcp_start; + IPAddress dhcp_end; + + // calculate dhcp_start and DHCP_end as done in the old code + dhcp_start = local_ip; + dhcp_start[3] += 99; + dhcp_end = dhcp_start; + dhcp_end[3] += 100; + + softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end); +} + /** * Disconnect from the network (close AP) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index fae8efd567..0e734d9c84 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -37,6 +37,7 @@ class ESP8266WiFiAPClass { public: bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); + bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end); bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPdisconnect(bool wifioff = false); From 3952a4e64dbd2d96e9661df39ffebd86fb826e53 Mon Sep 17 00:00:00 2001 From: NdK Date: Fri, 22 Sep 2017 18:30:34 +0200 Subject: [PATCH 2/2] Fixed bit-negation --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index cc5847dece..adc2f42283 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -208,10 +208,10 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA struct dhcps_lease dhcp_lease; uint32_t net_addr = info.ip.addr & info.netmask.addr; - uint32_t bcast_addr = net_addr | !info.netmask.addr; + uint32_t bcast_addr = net_addr | ~info.netmask.addr; // Assign user-supplied range, checking its validity - IPAddress ip = (static_cast(dhcp_start) & !info.netmask.addr) | net_addr; + IPAddress ip = (static_cast(dhcp_start) & ~info.netmask.addr) | net_addr; dhcp_lease.start_ip.addr = ip; if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) { @@ -220,7 +220,7 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA dhcp_lease.start_ip.addr=0; } - ip = (static_cast(dhcp_end) & !info.netmask.addr) | net_addr; + ip = (static_cast(dhcp_end) & ~info.netmask.addr) | net_addr; dhcp_lease.end_ip.addr = static_cast(ip); if(ip != net_addr && ip != bcast_addr && ip != info.ip.addr && ip != info.gw.addr) { DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());