Skip to content

Commit

Permalink
WiFi - static IP auto gw,mask,dns as in Arduino libraries (#9031)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy authored Nov 24, 2023
1 parent 32c858d commit 9a4e178
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
return true;
}

bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress dns) {

if (!local_ip.isSet())
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);

if (!local_ip.isV4())
return false;

IPAddress gw(local_ip);
gw[3] = 1;

This comment has been minimized.

Copy link
@TD-er

TD-er Dec 4, 2023

Contributor

Just wondering what this function does here.
It is really tricky to assume you have a /24 subnet and even so to assume your router's IP-address is ending with .1

So why is this function added?
There are quite a few ISP issued modem/routers which use .254 as their default gateway IP.
By introducing a function like this, you can be sure people will use it and then some users may need to alter the code to make it work in their setup.

I did a quick Google search and found this list of brands which have their default gateway IP ending with .254:

  • Linksys
  • Westell
  • SparkLAN
  • 2Wire
  • Motorola
  • 3Com
  • Aztech
  • CenturyLink
  • Thomson
  • Alcatel
  • Netopia Cayman
  • Billion

And for those with a separate (V)LAN for their IoT stuff, it also makes sense to use "the other end" of the range as gateway for that subnet.

This comment has been minimized.

Copy link
@JAndrassy

JAndrassy Dec 4, 2023

Author Contributor

these are defaults in the Arduino API established by the Arduino Ethernet library (2008) and Arduino WiFi library (2011).

This comment has been minimized.

Copy link
@TD-er

TD-er Dec 4, 2023

Contributor

Then I think it should at least have a big warning included stating this is absolutely not a universally applicable assumption.

I've seen (and had myself) ISP provided routers which had .254 as default gateway address.
And if this is as "hardcoded" in code as it is when using this function, new users may find it hard to find out why code isn't working on their network.

N.B. the Netopia routers I had, which had .254 as gateway were issued around that time the Arduino Ethernet library apparently defined it in their API.

I'm all in favor of making things uniform, but not when apparent faults will be repeated.

This comment has been minimized.

Copy link
@d-a-v

d-a-v Dec 6, 2023

Collaborator

I agree with both of you. My routers are also .254 and I was also disappointed with this .1. However Arduino API compatibility and usage portability is quite unchangeable, this discouraged me to comment on that.

A fair compromise would be to leave it as-is but deprecate it so users are warned at compile time that default router value would likely be incorrect.

This comment has been minimized.

Copy link
@TD-er

TD-er Dec 6, 2023

Contributor

Yep, adding some kind of compile-time warning or deprecated attribute with a message would be the best solution I think.

N.B. the same then applies to the pending PR for ESP32 with the same changes.

if (!dns.isSet()) {
dns = gw;
}
return config(local_ip, dns, gw);
}

/**
* Change DNS for static IP configuration
* @param dns1 Static DNS server 1
Expand Down
5 changes: 5 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class ESP8266WiFiSTAClass: public LwipIntf {
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = INADDR_ANY, IPAddress dns2 = INADDR_ANY);

// two and one parameter version. 2nd parameter is DNS like in Arduino
// IPv4 only
bool config(IPAddress local_ip, IPAddress dns = INADDR_ANY);

bool setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY);

bool reconnect();
Expand Down

0 comments on commit 9a4e178

Please sign in to comment.