Skip to content

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
wants to merge 9 commits into from
Closed

Add ability to set start and end ip for softAP #6422

wants to merge 9 commits into from

Conversation

mbm60
Copy link
Contributor

@mbm60 mbm60 commented Aug 14, 2019

Base on #3562 and discussed in #6031

@JAndrassy
Copy link
Contributor

dhcp_start = local_ip;
dhcp_start[3] += 99;

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");
Copy link
Collaborator

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 .

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

@mbm60 mbm60 Aug 29, 2019

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 :

Capture

Copy link
Collaborator

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).

Copy link
Contributor Author

@mbm60 mbm60 Aug 31, 2019

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();

@mbm60
Copy link
Contributor Author

mbm60 commented Aug 16, 2019

@JAndrassy

someone could use .200 as local IP

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)

@mbm60 mbm60 changed the title Added ability to set start and end ip for softAP Add ability to set start and end ip for softAP Aug 16, 2019
@JAndrassy
Copy link
Contributor

JAndrassy commented Aug 17, 2019

@JAndrassy

someone could use .200 as local IP

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

@mbm60
Copy link
Contributor Author

mbm60 commented Aug 17, 2019

@JAndrassy

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();

@mbm60
Copy link
Contributor Author

mbm60 commented Aug 26, 2019

Maybe i should close this PR because of no feedback.

@devyte
Copy link
Collaborator

devyte commented Aug 27, 2019

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");
Copy link
Collaborator

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);
Copy link
Collaborator

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.

Copy link
Contributor Author

@mbm60 mbm60 Aug 29, 2019

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) {
Copy link
Collaborator

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:

  1. Don't add additional parameters here. Instead, implement a method overload that has the additional parameters.
  2. 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.
  3. Refactor both methods to not have duplicate code.

@JAndrassy
Copy link
Contributor

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?

@mbm60 mbm60 closed this Sep 7, 2019
@mbm60 mbm60 deleted the mbm60-patch-1 branch September 7, 2019 20:49
@mbm60 mbm60 restored the mbm60-patch-1 branch September 20, 2019 16:48
@mbm60 mbm60 deleted the mbm60-patch-1 branch October 10, 2019 08:17
@mbm60 mbm60 restored the mbm60-patch-1 branch November 12, 2019 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants