-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ad6ff9
Added ability to set start and end ip for softAP
mbm60 802b68b
Update ESP8266WiFiAP.cpp
mbm60 9f1b343
Fix Static DHCP Lease
mbm60 eeeffbc
Update StaticLease.ino
mbm60 5bc4914
Update StaticLease.ino
mbm60 2374085
Merge branch 'master' into mbm60-patch-1
mbm60 2b7acf0
Merge pull request #1 from esp8266/master
mbm60 47bf960
Add some sainity check
mbm60 560460d
Fix some includes.
mbm60 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,13 @@ | |
#include "ESP8266WiFi.h" | ||
#include "ESP8266WiFiGeneric.h" | ||
#include "ESP8266WiFiAP.h" | ||
#include "lwip/def.h" | ||
|
||
#if LWIP_VERSION_MAJOR == 1 | ||
#include "lwip/app/dhcpserver.h" | ||
#else | ||
#include "dhcpserver.h" | ||
#endif | ||
|
||
extern "C" { | ||
#include "c_types.h" | ||
|
@@ -37,8 +44,6 @@ extern "C" { | |
|
||
#include "debug.h" | ||
|
||
|
||
|
||
// ----------------------------------------------------------------------------------------------------------------------- | ||
// ---------------------------------------------------- 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 | ||
|
@@ -206,17 +213,48 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA | |
|
||
if ( !local_ip.isV4() | ||
|| !subnet.isV4() | ||
|| !dhcp_start.isV4() | ||
|| !dhcp_end.isV4() | ||
#if LWIP_IPV6 | ||
// uninitialized gateway is valid | ||
|| gateway.isV6() | ||
#endif | ||
) { | ||
return false; | ||
} | ||
|
||
if( subnet != IPAddress(255,255,255,0)) { | ||
DEBUG_WIFI("[APConfig] invalid netmask. only netmask \"255.255.255.0\" is valid!\n"); | ||
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()); | ||
|
||
if ((start_ip <= softap_ip) | ||
|| ( end_ip <= softap_ip)) { | ||
DEBUG_WIFI("[APConfig] config ip information can't contain local ip!\n"); | ||
return false; | ||
} | ||
|
||
if ((end_ip - start_ip) > DHCPS_MAX_LEASE) { | ||
DEBUG_WIFI("[APConfig] the value of dhcp lease is greater than \"DHCPS_MAX_LEASE\"!\n"); | ||
return false; | ||
} | ||
|
||
softap_ip >>= 8; | ||
if ((start_ip >> 8 != softap_ip) | ||
|| (end_ip >> 8 != softap_ip)) { | ||
DEBUG_WIFI("[APConfig] config ip information must be in the same segment as the local ip!\n"); | ||
return false; | ||
} | ||
|
||
if(!wifi_softap_dhcps_stop()) { | ||
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); | ||
|
@@ -228,13 +266,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 +316,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] += 1; | ||
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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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