-
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
Conversation
someone could use .200 as local IP |
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 comment
The 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 are other attempts for a proper API for network calculations like #6196 .
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.
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 :
If the local IP is 192.168.4.1, the start IP and end IP must be in range 192.168.4 .
The comparison with the netmask you are looking for is done elsewhere.
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.
@d-a-v comment is correct. What happens if the subnet mask is e. g. 255.255.0.0? Then this test is incorrect.
The calculation required is a bit more elaborate.
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.
@d-a-v @devyte
I understand what you mean and you are absolutely right. But if the subnetmask is e.g 255.255.0.0 and local_ip is 192.168.1.1 and the dhcp_start or dhcp_end is e.g 192.168.5.10, the "[APConfig] wifi_set_ip_info failed" error occurs in debug. Because of the following lines in the "esp-dhcpserver.c" file :
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.
Because you are right saying it is dependent on dhcp server,
I propose you test and exit with an error when netmask != IPAddress(255,255,255,0)
.
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.
@d-a-v Thanks for your consideration.
Since the "DHCPS_MAX_LEASE" is limited to 100, we don't need more than 254 IPs in a range. And if we were to limit the "subnet mask" to 255.255.255.0, why would there be an option to set the "subnet mask"? And wouldn't it be better to implement "softAPConfig" without it as follows?
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();
This part of the code applies the default value and if someone needs another start and end IP, must be set wiith --> softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end) |
with .200 as IP of the AP, the default dhcp start IP would be .299 |
It was in the old code and I just used it again. IPAddress ip = local_ip;
ip[3] += 99;
dhcp_lease.start_ip.addr = ip.v4(); |
Maybe i should close this PR because of no feedback. |
no feedback means some of us haven't gotten to it yet |
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 comment
The 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.
The calculation required is a bit more elaborate.
As it depend from your first address, and need to be done BEFORE any request from client, | ||
this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). | ||
/* Start Access Point. You can remove the password parameter if you want the AP to be open. */ | ||
WiFi.softAP(ssid, password); |
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
*/ | ||
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 comment
The 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:
- Don't add additional parameters here. Instead, implement a method overload that has the additional parameters.
- Change the current method to add a bool dhcpEnabled at the end with default true. When true, dhcp is enabled per current defaults. When false, dhcp setup is skipped altogether.
- Refactor both methods to not have duplicate code.
and the default start IP and end IP calculation? if the end IP calculation overflows the range will have start IP larger then end IP. is it OK for DHCP server? |
Merge branch 'master' into mbm60-patch-1
Base on #3562 and discussed in #6031