@@ -476,9 +476,10 @@ String ESP8266WiFiSTAClass::hostname(void) {
476
476
* @param aHostname max length:24
477
477
* @return ok
478
478
*/
479
- bool ESP8266WiFiSTAClass::hostname (String aHostname) {
479
+ bool ESP8266WiFiSTAClass::hostname (const char * aHostname) {
480
480
/*
481
481
vvvv RFC952 vvvv
482
+ ASSUMPTIONS
482
483
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
483
484
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
484
485
sign (-), and period (.). Note that periods are only allowed when
@@ -496,38 +497,34 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
496
497
^^^^ RFC952 ^^^^
497
498
498
499
- 24 chars max
499
- - only a..z A..Z 0..9 -]
500
+ - only a..z A..Z 0..9 '-'
500
501
- no '-' as last char
501
502
*/
502
503
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 " );
505
510
return false ;
506
511
}
507
512
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++)
517
516
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 ;
523
520
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
+ }
525
524
525
+ bool ret = wifi_station_set_hostname (aHostname);
526
526
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);
531
528
return false ;
532
529
}
533
530
@@ -543,16 +540,14 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
543
540
// renew already started DHCP leases
544
541
err_t lwipret = dhcp_renew (intf);
545
542
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 );
550
545
ret = false ;
551
546
}
552
547
}
553
548
}
554
549
555
- return ret;
550
+ return ret && compliant ;
556
551
}
557
552
558
553
/* *
0 commit comments