esp8266 / esp8266/Arduino

DHCP range problematic in AP mode

Open
#3,532 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

Hello.

I just noticed possible problems when calling WiFi.softAPConfig().
It assumes everybody uses a class-C network, a fixed mask of 24 bits and places the ESP at the start of the network (.1).
If the address given is over 55, the DHCP range collides with the reserved address (.255) and/or the network address (.0).
So I slightly modified the code to accept two optional arguments: dhcp_start and dhcp_end .
That makes it possible for the user to choose more freely the address range to use (I still don't check for network/broadcast collisions: with a great power...), including a whole class-A if desired (not that it's really useful with at most 4 clients, but anyway...).
If the optional arguments are not supplied, the old behaviour is retained, so it won't break existing code.

Hope it can be useful for others.

diff -u ESP8266WiFiAP.h.ori ESP8266WiFiAP.h
```
--- ESP8266WiFiAP.h.ori 2016-09-27 19:02:02.904865345 +0200
+++ ESP8266WiFiAP.h 2017-08-18 22:29:38.061691447 +0200
@@ -37,7 +37,7 @@
public:

bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0);
- bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
+ bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, uint32_t dhcp_start=0, uint32_t dhcp_end=0);
bool softAPdisconnect(bool wifioff = false);

uint8_t softAPgetStationNum();
```

diff -u ESP8266WiFiAP.cpp.ori ESP8266WiFiAP.cpp
```
--- ESP8266WiFiAP.cpp.ori 2016-09-27 19:02:02.900865537 +0200
+++ ESP8266WiFiAP.cpp 2017-08-18 22:38:50.736674336 +0200
@@ -175,8 +175,10 @@
* @param local_ip access point IP
* @param gateway gateway IP
* @param subnet subnet mask
+ * @param dhcp_start DHCP range start
+ * @param dhcp_end DHCP range end
*/
-bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
+bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, uint32_t dhcp_start, uint32_t 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
@@ -201,11 +203,19 @@

struct dhcps_lease dhcp_lease;
IPAddress ip = local_ip;
- ip[3] += 99;
+ if(dhcp_start) {
+ ip=(dhcp_start & !info.netmask.addr) + (info.ip.addr&info.netmask.addr);
+ } else {
+ 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;
+ if(dhcp_end) {
+ ip=(dhcp_end & !info.netmask.addr) + (info.ip.addr&info.netmask.addr);
+ } else {
+ ip[3] += 100;
+ }
dhcp_lease.end_ip.addr = static_cast(ip);
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.