Skip to content

Commit 7c5be91

Browse files
d-a-vdevyte
authored andcommitted
dynamic WiFi.hostname("newname") (#5652)
* dynamic WiFi.hostname("newname") * WiFi.hostname() back to String return type * no silent hostname fix but proceed with debug message and returning false
1 parent ece9390 commit 7c5be91

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
extern "C" {
3838
#include "lwip/err.h"
3939
#include "lwip/dns.h"
40+
#include "lwip/dhcp.h"
4041
#include "lwip/init.h" // LWIP_VERSION_
4142
#if LWIP_IPV6
4243
#include "lwip/netif.h" // struct netif
@@ -467,38 +468,86 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
467468
* @return hostname
468469
*/
469470
String ESP8266WiFiSTAClass::hostname(void) {
470-
return String(wifi_station_get_hostname());
471+
return wifi_station_get_hostname();
471472
}
472473

473-
474474
/**
475475
* Set ESP8266 station DHCP hostname
476-
* @param aHostname max length:32
476+
* @param aHostname max length:24
477477
* @return ok
478478
*/
479-
bool ESP8266WiFiSTAClass::hostname(char* aHostname) {
480-
if(strlen(aHostname) > 32) {
479+
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
480+
/*
481+
vvvv RFC952 vvvv
482+
ASSUMPTIONS
483+
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
484+
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
485+
sign (-), and period (.). Note that periods are only allowed when
486+
they serve to delimit components of "domain style names". (See
487+
RFC-921, "Domain Name System Implementation Schedule", for
488+
background). No blank or space characters are permitted as part of a
489+
name. No distinction is made between upper and lower case. The first
490+
character must be an alpha character. The last character must not be
491+
a minus sign or period. A host which serves as a GATEWAY should have
492+
"-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as
493+
Internet gateways should not use "-GATEWAY" and "-GW" as part of
494+
their names. A host which is a TAC should have "-TAC" as the last
495+
part of its host name, if it is a DoD host. Single character names
496+
or nicknames are not allowed.
497+
^^^^ RFC952 ^^^^
498+
499+
- 24 chars max
500+
- only a..z A..Z 0..9 '-'
501+
- no '-' as last char
502+
*/
503+
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");
481510
return false;
482511
}
483-
return wifi_station_set_hostname(aHostname);
484-
}
485512

486-
/**
487-
* Set ESP8266 station DHCP hostname
488-
* @param aHostname max length:32
489-
* @return ok
490-
*/
491-
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
492-
return hostname((char*) aHostname);
493-
}
513+
// check RFC compliance
514+
bool compliant = (len <= 24);
515+
for (size_t i = 0; compliant && i < len; i++)
516+
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
517+
compliant = false;
518+
if (aHostname[len - 1] == '-')
519+
compliant = false;
494520

495-
/**
496-
* Set ESP8266 station DHCP hostname
497-
* @param aHostname max length:32
498-
* @return ok
499-
*/
500-
bool ESP8266WiFiSTAClass::hostname(const String& aHostname) {
501-
return hostname((char*) aHostname.c_str());
521+
if (!compliant) {
522+
DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname);
523+
}
524+
525+
bool ret = wifi_station_set_hostname(aHostname);
526+
if (!ret) {
527+
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
528+
return false;
529+
}
530+
531+
// now we should inform dhcp server for this change, using lwip_renew()
532+
// looping through all existing interface
533+
// harmless for AP, also compatible with ethernet adapters (to come)
534+
for (netif* intf = netif_list; intf; intf = intf->next) {
535+
536+
// unconditionally update all known interfaces
537+
intf->hostname = wifi_station_get_hostname();
538+
539+
if (netif_dhcp_data(intf) != nullptr) {
540+
// renew already started DHCP leases
541+
err_t lwipret = dhcp_renew(intf);
542+
if (lwipret != ERR_OK) {
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);
545+
ret = false;
546+
}
547+
}
548+
}
549+
550+
return ret && compliant;
502551
}
503552

504553
/**

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ class ESP8266WiFiSTAClass {
7070
IPAddress dnsIP(uint8_t dns_no = 0);
7171

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

7776
// STA WiFi info
7877
wl_status_t status();

tools/sdk/include/user_interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ bool wifi_station_dhcpc_stop(void);
328328
enum dhcp_status wifi_station_dhcpc_status(void);
329329
bool wifi_station_dhcpc_set_maxtry(uint8 num);
330330

331-
char* wifi_station_get_hostname(void);
332-
bool wifi_station_set_hostname(char *name);
331+
const char* wifi_station_get_hostname(void);
332+
bool wifi_station_set_hostname(const char *name);
333333

334334
int wifi_station_set_cert_key(uint8 *client_cert, int client_cert_len,
335335
uint8 *private_key, int private_key_len,

0 commit comments

Comments
 (0)