-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add ability to set start and end ip for softAP #6422
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
Changes from 5 commits
5ad6ff9
802b68b
9f1b343
eeeffbc
5bc4914
2374085
2b7acf0
47bf960
560460d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,12 @@ extern "C" { | |
|
||
#include "debug.h" | ||
|
||
|
||
#ifndef htonl | ||
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ | ||
mbm60 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
((x)<< 8 & 0x00FF0000UL) | \ | ||
((x)>> 8 & 0x0000FF00UL) | \ | ||
((x)>>24 & 0x000000FFUL) ) | ||
#endif | ||
|
||
// ----------------------------------------------------------------------------------------------------------------------- | ||
// ---------------------------------------------------- Private functions ------------------------------------------------ | ||
|
@@ -194,8 +199,10 @@ bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, in | |
* @param local_ip access point IP | ||
* @param gateway gateway IP (0.0.0.0 to disable) | ||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that currently the dhcp server is always on, and there is no way to turn it off. I suggest the following:
|
||
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 | ||
|
@@ -214,10 +221,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA | |
return false; | ||
} | ||
struct ip_info info; | ||
|
||
info.ip.addr = local_ip.v4(); | ||
info.gw.addr = gateway.v4(); | ||
info.netmask.addr = subnet.v4(); | ||
|
||
|
||
uint32_t softap_ip = htonl(info.ip.addr); | ||
uint32_t start_ip = htonl(dhcp_start.v4()); | ||
uint32_t end_ip = htonl(dhcp_end.v4()); | ||
softap_ip >>= 8; | ||
if (( start_ip >> 8 != softap_ip ) || ( end_ip >> 8 != softap_ip )) { | ||
DEBUG_WIFI("[APConfig] dhcp_start or dhcp_end isn't in range of local_ip Address!\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is not right. v4 addresses need to be shifted by the number of zeroed bits in the netmask address. It is indeed 8 with 255.255.255.0 but since netmask is not hardcoded, "8" neither should be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. But this part of the code compares the start and the end IP with the local IP to be in the same range. And I copied it from the esp-dhcpserver.c file. for example : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @d-a-v comment is correct. What happens if the subnet mask is e. g. 255.255.0.0? Then this test is incorrect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @d-a-v @devyte There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because you are right saying it is dependent on dhcp server, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @d-a-v Thanks for your consideration. bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress dhcp_start, IPAddress dhcp_end) {
IPAddress subnet(255, 255, 255, 0);
info.netmask.addr = subnet.v4(); |
||
DEBUG_WIFI("[APConfig] set dfault vaule\n"); | ||
dhcp_start = local_ip; | ||
dhcp_start[3] += 99; | ||
dhcp_end = dhcp_start; | ||
dhcp_end[3] += 100; | ||
} | ||
|
||
if(!wifi_softap_dhcps_stop()) { | ||
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); | ||
} | ||
|
@@ -228,13 +249,14 @@ 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 = ip.v4(); | ||
dhcp_lease.enable = true; | ||
|
||
IPAddress ip = dhcp_start; | ||
dhcp_lease.start_ip.addr = dhcp_start.v4(); | ||
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); | ||
|
||
ip[3] += 100; | ||
dhcp_lease.end_ip.addr = ip.v4(); | ||
ip = dhcp_end; | ||
dhcp_lease.end_ip.addr = dhcp_end.v4(); | ||
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); | ||
|
||
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { | ||
|
@@ -277,7 +299,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA | |
return ret; | ||
} | ||
|
||
/** | ||
* 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; | ||
|
||
dhcp_start = local_ip; | ||
dhcp_start[3] += 99; | ||
dhcp_end = dhcp_start; | ||
dhcp_end[3] += 100; | ||
|
||
bool ret = softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end); | ||
return ret; | ||
} | ||
|
||
/** | ||
* Disconnect from the network (close AP) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting the softAP before setting up the static leases allows a race condition, where a client could potentially connect as soon as the AP is up, but before the leases are set up. In that case, the lease setup could lead to an address clash. This is the reason behind the current call order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but I did test it and if it is before start softAP, "StaticLease" won't work