1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00

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
This commit is contained in:
david gauchard
2019-01-24 21:13:20 +01:00
committed by Develo
parent ece93908a8
commit 7c5be91430
3 changed files with 76 additions and 28 deletions

View File

@ -37,6 +37,7 @@
extern "C" { extern "C" {
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/dns.h" #include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/init.h" // LWIP_VERSION_ #include "lwip/init.h" // LWIP_VERSION_
#if LWIP_IPV6 #if LWIP_IPV6
#include "lwip/netif.h" // struct netif #include "lwip/netif.h" // struct netif
@ -467,38 +468,86 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
* @return hostname * @return hostname
*/ */
String ESP8266WiFiSTAClass::hostname(void) { String ESP8266WiFiSTAClass::hostname(void) {
return String(wifi_station_get_hostname()); return wifi_station_get_hostname();
}
/**
* Set ESP8266 station DHCP hostname
* @param aHostname max length:32
* @return ok
*/
bool ESP8266WiFiSTAClass::hostname(char* aHostname) {
if(strlen(aHostname) > 32) {
return false;
}
return wifi_station_set_hostname(aHostname);
} }
/** /**
* Set ESP8266 station DHCP hostname * Set ESP8266 station DHCP hostname
* @param aHostname max length:32 * @param aHostname max length:24
* @return ok * @return ok
*/ */
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) { bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
return hostname((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
they serve to delimit components of "domain style names". (See
RFC-921, "Domain Name System Implementation Schedule", for
background). No blank or space characters are permitted as part of a
name. No distinction is made between upper and lower case. The first
character must be an alpha character. The last character must not be
a minus sign or period. A host which serves as a GATEWAY should have
"-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as
Internet gateways should not use "-GATEWAY" and "-GW" as part of
their names. A host which is a TAC should have "-TAC" as the last
part of its host name, if it is a DoD host. Single character names
or nicknames are not allowed.
^^^^ RFC952 ^^^^
/** - 24 chars max
* Set ESP8266 station DHCP hostname - only a..z A..Z 0..9 '-'
* @param aHostname max length:32 - no '-' as last char
* @return ok */
*/
bool ESP8266WiFiSTAClass::hostname(const String& aHostname) { size_t len = strlen(aHostname);
return hostname((char*) aHostname.c_str());
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;
}
// check RFC compliance
bool compliant = (len <= 24);
for (size_t i = 0; compliant && i < len; i++)
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
compliant = false;
if (aHostname[len - 1] == '-')
compliant = false;
if (!compliant) {
DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname);
}
bool ret = wifi_station_set_hostname(aHostname);
if (!ret) {
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
return false;
}
// now we should inform dhcp server for this change, using lwip_renew()
// looping through all existing interface
// harmless for AP, also compatible with ethernet adapters (to come)
for (netif* intf = netif_list; intf; intf = intf->next) {
// unconditionally update all known interfaces
intf->hostname = wifi_station_get_hostname();
if (netif_dhcp_data(intf) != nullptr) {
// renew already started DHCP leases
err_t lwipret = dhcp_renew(intf);
if (lwipret != ERR_OK) {
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 && compliant;
} }
/** /**

View File

@ -70,9 +70,8 @@ class ESP8266WiFiSTAClass {
IPAddress dnsIP(uint8_t dns_no = 0); IPAddress dnsIP(uint8_t dns_no = 0);
String hostname(); String hostname();
bool hostname(char* aHostname); bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); }
bool hostname(const char* aHostname); bool hostname(const char* aHostname);
bool hostname(const String& aHostname);
// STA WiFi info // STA WiFi info
wl_status_t status(); wl_status_t status();

View File

@ -328,8 +328,8 @@ bool wifi_station_dhcpc_stop(void);
enum dhcp_status wifi_station_dhcpc_status(void); enum dhcp_status wifi_station_dhcpc_status(void);
bool wifi_station_dhcpc_set_maxtry(uint8 num); bool wifi_station_dhcpc_set_maxtry(uint8 num);
char* wifi_station_get_hostname(void); const char* wifi_station_get_hostname(void);
bool wifi_station_set_hostname(char *name); bool wifi_station_set_hostname(const char *name);
int wifi_station_set_cert_key(uint8 *client_cert, int client_cert_len, int wifi_station_set_cert_key(uint8 *client_cert, int client_cert_len,
uint8 *private_key, int private_key_len, uint8 *private_key, int private_key_len,