1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Added support for user-supplied DHCP range, with basic sanity checks (#3562)

This commit is contained in:
NdK 2017-09-21 10:52:30 +02:00 committed by Ivan Grokhotkov
parent 41a8fdb4a9
commit bdf2296a7d
2 changed files with 63 additions and 24 deletions

View File

@ -179,8 +179,10 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
* @param local_ip access point IP * @param local_ip access point IP
* @param gateway gateway IP * @param gateway gateway IP
* @param subnet subnet mask * @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()); 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)) { if(!WiFi.enableAP(true)) {
// enable AP failed // enable AP failed
@ -204,35 +206,52 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
} }
struct dhcps_lease dhcp_lease; struct dhcps_lease dhcp_lease;
IPAddress ip = local_ip;
ip[3] += 99;
dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
ip[3] += 100; uint32_t net_addr = info.ip.addr & info.netmask.addr;
uint32_t bcast_addr = net_addr | !info.netmask.addr;
// Assign user-supplied range, checking its validity
IPAddress ip = (static_cast<uint32_t>(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) {
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
} else {
dhcp_lease.start_ip.addr=0;
}
ip = (static_cast<uint32_t>(dhcp_end) & !info.netmask.addr) | net_addr;
dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip); dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); 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());
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { } else {
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); dhcp_lease.end_ip.addr=0;
ret = false;
} }
// set lease time to 720min --> 12h if(dhcp_lease.start_ip.addr && dhcp_lease.end_ip.addr) {
if(!wifi_softap_set_dhcps_lease_time(720)) { if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
ret = false; ret = false;
} }
uint8 mode = 1; // set lease time to 720min --> 12h
if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) { if(!wifi_softap_set_dhcps_lease_time(720)) {
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
ret = false; ret = false;
} }
if(!wifi_softap_dhcps_start()) { uint8 mode = 1;
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n"); if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
ret = false; 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 // 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) * Disconnect from the network (close AP)

View File

@ -37,6 +37,7 @@ class ESP8266WiFiAPClass {
public: public:
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); 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 softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
bool softAPdisconnect(bool wifioff = false); bool softAPdisconnect(bool wifioff = false);