Hostname alphanumeric (#1048)

* Use string derived from serverDescription for wifi.hostname()

The code was sending illegal hostname strings to WiFi.hostname() (which is then submitted to DHCP and often times to DNS.)  A valid hostname contains only alphanumeric characters and hyphens (though it can't start with a hypen.)  This change simply alters the value passed to wifi.hostname() by replacing all non alphanum chars with hyphens while ensuring the first char is never a hyphen.  If the resulting hostname is empty, it uses the escapedMac value (which I'm assuming is initialized by the time this code executes.)

This change would result issue #1033

* replace string with char array
prefix wled
improve documentation

Co-authored-by: garyd9 <garyd9@hotmail.com>
Co-authored-by: Gary Dezern <gdezern@internal.youforgot.net>
This commit is contained in:
Aircoookie 2020-07-19 16:24:26 +02:00 committed by GitHub
parent 2cd8ee4a13
commit ec6a243e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View File

@ -2,7 +2,9 @@
### Development versions after 0.10.0 release
#### Build 2007020
#### Build 2007190
- Fixed hostname containing illegal characters (#1035)
#### Build 2006251

View File

@ -801,6 +801,16 @@ void WS2812FX::handle_palette(void)
}
}
/*
* Gets a single color from the currently selected palette.
* @param i Palette Index (if mapping is true, the full palette will be SEGLEN long, if false, 255). Will wrap around automatically.
* @param mapping if true, LED position in segment is considered for color
* @param wrap FastLED palettes will usally wrap back to the start smoothly. Set false to get a hard edge
* @param mcol If the default palette 0 is selected, return the standard color 0, 1 or 2 instead. If >2, Party palette is used instead
* @param pbri Value to scale the brightness of the returned color by. Default is 255. (no scaling)
* @returns Single color from palette
*/
uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri)
{
if (SEGMENT.palette == 0 && mcol < 3) return SEGCOLOR(mcol); //WS2812FX default
@ -812,6 +822,7 @@ uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8
return fastled_col.r*65536 + fastled_col.g*256 + fastled_col.b;
}
//@returns `true` if color, mode, speed, intensity and palette match
bool WS2812FX::segmentsAreIdentical(Segment* a, Segment* b)
{
//if (a->start != b->start) return false;

View File

@ -324,15 +324,41 @@ void WLED::initConnection()
DEBUG_PRINT(clientSSID);
DEBUG_PRINTLN("...");
// convert the "serverDescription" into a valid DNS hostname (alphanumeric)
char hostname[25] = "wled-";
const char *pC = serverDescription;
uint8_t pos = 5;
while (*pC && pos < 24) { // while !null and not over length
if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname
hostname[pos] = *pC;
pos++;
} else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') {
hostname[pos] = '-';
pos++;
}
// else do nothing - no leading hyphens and do not include hyphens for all other characters.
pC++;
}
// if the hostname is left blank, use the mac address/default mdns name
if (pos < 6) {
sprintf(hostname + 5, "%*s", 6, escapedMac.c_str() + 6);
} else { //last character must not be hyphen
while (pos > 0 && hostname[pos -1] == '-') {
hostname[pos -1] = 0;
pos--;
}
}
#ifdef ESP8266
WiFi.hostname(serverDescription);
WiFi.hostname(hostname);
#endif
WiFi.begin(clientSSID, clientPass);
#ifdef ARDUINO_ARCH_ESP32
WiFi.setSleep(!noWifiSleep);
WiFi.setHostname(serverDescription);
WiFi.setHostname(hostname);
#else
wifi_set_sleep_type((noWifiSleep) ? NONE_SLEEP_T : MODEM_SLEEP_T);
#endif

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2007020
#define VERSION 2007190
// ESP8266-01 (blue) got too little storage space to work with all features of WLED. To use it, you must use ESP8266 Arduino Core v2.4.2 and the setting 512K(No SPIFFS).