Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 61 additions & 23 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<uint32_t>(ip);
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());

ip[3] += 100;
dhcp_lease.end_ip.addr = static_cast<uint32_t>(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<uint32_t>(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<uint32_t>(dhcp_end) & !info.netmask.addr) | net_addr;
dhcp_lease.end_ip.addr = static_cast<uint32_t>(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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down