Skip to content

Commit 19bfa7f

Browse files
committed
no silent hostname fix but proceed with debug message and returning false
1 parent 51bbae1 commit 19bfa7f

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+23-28
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ String ESP8266WiFiSTAClass::hostname(void) {
476476
* @param aHostname max length:24
477477
* @return ok
478478
*/
479-
bool ESP8266WiFiSTAClass::hostname(String aHostname) {
479+
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
480480
/*
481481
vvvv RFC952 vvvv
482+
ASSUMPTIONS
482483
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
483484
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
484485
sign (-), and period (.). Note that periods are only allowed when
@@ -496,38 +497,34 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
496497
^^^^ RFC952 ^^^^
497498
498499
- 24 chars max
499-
- only a..z A..Z 0..9 -]
500+
- only a..z A..Z 0..9 '-'
500501
- no '-' as last char
501502
*/
502503

503-
if (aHostname.length() == 0) {
504-
DEBUGV("WiFi.(set)hostname(): empty name\n");
504+
size_t len = strlen(aHostname);
505+
506+
if (len == 0 || len > 32) {
507+
// nonos-sdk limit is 32
508+
// (dhcp hostname option minimum size is ~60)
509+
DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n");
505510
return false;
506511
}
507512

508-
// rework hostname to be RFC compliant
509-
510-
if (aHostname.length() > 24) {
511-
// nonos-sdk allows 32, but
512-
// dhcp server may require RFC compliance
513-
aHostname.remove(24);
514-
}
515-
for (size_t i = 0; i < aHostname.length(); i++)
516-
// replace unallowed chars by dashes
513+
// check RFC compliance
514+
bool compliant = (len <= 24);
515+
for (size_t i = 0; compliant && i < len; i++)
517516
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
518-
aHostname[i] = '-';
519-
if (aHostname[aHostname.length() - 1] == '-') {
520-
// check for ending dash
521-
aHostname[aHostname.length() - 1] = 'x';
522-
}
517+
compliant = false;
518+
if (aHostname[len - 1] == '-')
519+
compliant = false;
523520

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

525+
bool ret = wifi_station_set_hostname(aHostname);
526526
if (!ret) {
527-
#ifdef DEBUG_ESP_CORE
528-
static const char fmt[] PROGMEM = "WiFi.hostname(%s): wifi_station_set_hostname() failed\n";
529-
DEBUGV(fmt, aHostname.c_str());
530-
#endif
527+
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
531528
return false;
532529
}
533530

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

555-
return ret;
550+
return ret && compliant;
556551
}
557552

558553
/**

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class ESP8266WiFiSTAClass {
7070
IPAddress dnsIP(uint8_t dns_no = 0);
7171

7272
String hostname();
73-
bool hostname(String aHostname);
73+
bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); }
74+
bool hostname(const char* aHostname);
7475

7576
// STA WiFi info
7677
wl_status_t status();

0 commit comments

Comments
 (0)