8000 dynamic WiFi.hostname("newname") by d-a-v · Pull Request #5652 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

dynamic WiFi.hostname("newname") #5652

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

Merged
merged 4 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
no silent hostname fix but proceed with debug message and returning f…
…alse
  • Loading branch information
d-a-v committed Jan 24, 2019
commit 19bfa7fcd344e7a2f0414aca6f17ed51147e83be
51 changes: 23 additions & 28 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,10 @@ String ESP8266WiFiSTAClass::hostname(void) {
* @param aHostname max length:24
* @return ok
*/
bool ESP8266WiFiSTAClass::hostname(String aHostname) {
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
/*
vvvv RFC952 vvvv
ASSUMPTIONS
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
sign (-), and period (.). Note that periods are only allowed when
Expand All @@ -496,38 +497,34 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
^^^^ RFC952 ^^^^

- 24 chars max
- only a..z A..Z 0..9 -]
- only a..z A..Z 0..9 '-'
- no '-' as last char
*/

if (aHostname.length() == 0) {
DEBUGV("WiFi.(set)hostname(): empty name\n");
size_t len = strlen(aHostname);

if (len == 0 || len > 32) {
// nonos-sdk limit is 32
// (dhcp hostname option minimum size is ~60)
DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n");
return false;
}

// rework hostname to be RFC compliant

if (aHostname.length() > 24) {
// nonos-sdk allows 32, but
// dhcp server may require RFC compliance
aHostname.remove(24);
}
for (size_t i = 0; i < aHostname.length(); i++)
// replace unallowed chars by dashes
// check RFC compliance
bool compliant = (len <= 24);
for (size_t i = 0; compliant && i < len; i++)
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
aHostname[i] = '-';
if (aHostname[aHostname.length() - 1] == '-') {
// check for ending dash
aHostname[aHostname.length() - 1] = 'x';
}
compliant = false;
if (aHostname[len - 1] == '-')
compliant = false;

bool ret = wifi_station_set_hostname(aHostname.c_str());
if (!compliant) {
DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname);
}

bool ret = wifi_station_set_hostname(aHostname);
if (!ret) {
#ifdef DEBUG_ESP_CORE
static const char fmt[] PROGMEM = "WiFi.hostname(%s): wifi_station_set_hostname() failed\n";
DEBUGV(fmt, aHostname.c_str());
#endif
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
return false;
}

Expand All @@ -543,16 +540,14 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
// renew already started DHCP leases
err_t lwipret = dhcp_renew(intf);
if (lwipret != ERR_OK) {
#ifdef DEBUG_ESP_CORE
static const char fmt[] PROGMEM = "WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n";
DEBUGV(fmt, intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num);
#endif
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n",
intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num);
ret = false;
}
}
}

return ret;
return ret && compliant;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class ESP8266WiFiSTAClass {
IPAddress dnsIP(uint8_t dns_no = 0);

String hostname();
bool hostname(String aHostname);
bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); }
bool hostname(const char* aHostname);

// STA WiFi info
wl_status_t status();
Expand Down
0