|
37 | 37 | extern "C" {
|
38 | 38 | #include "lwip/err.h"
|
39 | 39 | #include "lwip/dns.h"
|
| 40 | +#include "lwip/dhcp.h" |
40 | 41 | #include "lwip/init.h" // LWIP_VERSION_
|
41 | 42 | #if LWIP_IPV6
|
42 | 43 | #include "lwip/netif.h" // struct netif
|
@@ -467,38 +468,86 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
|
467 | 468 | * @return hostname
|
468 | 469 | */
|
469 | 470 | String ESP8266WiFiSTAClass::hostname(void) {
|
470 |
| - return String(wifi_station_get_hostname()); |
| 471 | + return wifi_station_get_hostname(); |
471 | 472 | }
|
472 | 473 |
|
473 |
| - |
474 | 474 | /**
|
475 | 475 | * Set ESP8266 station DHCP hostname
|
476 |
| - * @param aHostname max length:32 |
| 476 | + * @param aHostname max length:24 |
477 | 477 | * @return ok
|
478 | 478 | */
|
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"); |
481 | 510 | return false;
|
482 | 511 | }
|
483 |
| - return wifi_station_set_hostname(aHostname); |
484 |
| -} |
485 | 512 |
|
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; |
494 | 520 |
|
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; |
502 | 551 | }
|
503 | 552 |
|
504 | 553 | /**
|
|
0 commit comments