8000 move hostname() from STA to generic interface · esp8266/Arduino@bd8b32a · GitHub
[go: up one dir, main page]

Skip to content

Commit bd8b32a

Browse files
committed
move hostname() from STA to generic interface
1 parent 6b27eaa commit bd8b32a

File tree

6 files changed

+117
-99
lines changed

6 files changed

+117
-99
lines changed

cores/esp8266/lwIPIntf.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11

2+
extern "C" {
3+
#include "lwip/err.h"
4+
#include "lwip/dns.h"
5+
#include "lwip/dhcp.h"
6+
#include "lwip/init.h" // LWIP_VERSION_
7+
#if LWIP_IPV6
8+
#include "lwip/netif.h" // struct netif
9+
#endif
10+
}
11+
12+
#include "debug.h"
213
#include "lwIPIntf.h"
314

415
// args | esp order arduino order
@@ -38,3 +49,95 @@ bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1
3849

3950
return true;
4051
}
52+
53+
/**
54+
* Get ESP8266 station DHCP hostname
55+
* @return hostname
56+
*/
57+
String LwipIntf::hostname(void) {
58+
return wifi_station_get_hostname();
59+
}
60+
61+
/**
62+
* Set ESP8266 station DHCP hostname
63+
* @param aHostname max length:24
64+
* @return ok
65+
*/
66+
bool LwipIntf::hostname(const char* aHostname) {
67+
/*
68+
vvvv RFC952 vvvv
69+
ASSUMPTIONS
70+
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
71+
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
72+
sign (-), and period (.). Note that periods are only allowed when
73+
they serve to delimit components of "domain style names". (See
74+
RFC-921, "Domain Name System Implementation Schedule", for
75+
background). No blank or space characters are permitted as part of a
76+
name. No distinction is made between upper and lower case. The first
77+
character must be an alpha character. The last character must not be
78+
a minus sign or period. A host which serves as a GATEWAY should have
79+
"-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as
80+
Internet gateways should not use "-GATEWAY" and "-GW" as part of
81+
their names. A host which is a TAC should have "-TAC" as the last
82+
part of its host name, if it is a DoD host. Single character names
83+
or nicknames are not allowed.
84+
^^^^ RFC952 ^^^^
85+
86+
- 24 chars max
87+
- only a..z A..Z 0..9 '-'
88+
- no '-' as last char
89+
*/
90+
91+
size_t len = strlen(aHostname);
92+
93+
if (len == 0 || len > 32) {
94+
// nonos-sdk limit is 32
95+
// (dhcp hostname option minimum size is ~60)
96+
DEBUGV("WiFi.(set)hostname(): empty or large(>32) name\n");
97+
return false;
98+
}
99+
100+
// check RFC compliance
101+
bool compliant = (len <= 24);
102+
for (size_t i = 0; compliant && i < len; i++)
103+
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
104+
compliant = false;
105+
if (aHostname[len - 1] == '-')
106+
compliant = false;
107+
108+
if (!compliant) {
109+
DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname);
110+
}
111+
112+
bool ret = wifi_station_set_hostname(aHostname);
113+
if (!ret) {
114+
DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
115+
return false;
116+
}
117+
118+
// now we should inform dhcp server for this change, using lwip_renew()
119+
// looping through all existing interface
120+
// harmless for AP, also compatible with ethernet adapters (to come)
121+
for (netif* intf = netif_list; intf; intf = intf->next) {
122+
123+
// unconditionally update all known interfaces
124+
#if LWIP_VERSION_MAJOR == 1
125+
intf->hostname = (char*)wifi_station_get_hostname();
126+
#else
127+
intf->hostname = wifi_station_get_hostname();
128+
#endif
129+
130+
if (netif_dhcp_data(intf) != nullptr) {
131+
// renew already started DHCP leases
132+
err_t lwipret = dhcp_renew(intf);
133+
if (lwipret != ERR_OK) {
134+
DEBUGV("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n",
135+
intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num);
136+
ret = false;
137+
}
138+
}
139+
}
140+
141+
return ret && compliant;
142+
}
143+

cores/esp8266/lwIPIntf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class LwipIntf {
2323
bool ipAddressReorder (const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2, const IPAddress& arg3,
2424
IPAddress& gateway, IPAddress& netmask, IPAddress& dns1);
2525

26+
String hostname();
27+
bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); }
28+
bool hostname(const char* aHostname);
29+
2630
};
2731

2832
#endif // _LWIPINTF_H

cores/esp8266/lwIPIntfDev.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <lwip/netif.h>
1212
#include <lwip/etharp.h>
1313
#include <lwip/dhcp.h>
14+
#include <lwip/apps/sntp.h>
1415

1516
#include <user_interface.h> // wifi_get_macaddr()
1617

@@ -254,10 +255,14 @@ err_t LwipIntfDev<RawDev>::netif_init ()
254255
template <class RawDev>
255256
void LwipIntfDev<RawDev>::netif_status_callback ()
256257
{
257-
//XXX is it wise ?
258-
if (_default && connected())
259-
netif_set_default(&_netif);
260-
else if (netif_default == &_netif && !connected())
258+
if (connected())
259+
{
260+
if (_default)
261+
netif_set_default(&_netif);
262+
sntp_stop();
263+
sntp_init();
264+
}
265+
else if (netif_default == &_netif)
261266
netif_set_default(nullptr);
262267
}
263268

cores/esp8266/time.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void configTime(const char* tz, const char* server1, const char* server2, const
122122
char tzram[strlen_P(tz) + 1];
123123
memcpy_P(tzram, tz, sizeof(tzram));
124124
setenv("TZ", tzram, 1/*overwrite*/);
125+
sntp_set_timezone_in_seconds(0);
125126
tzset();
126127

127128
sntp_init();

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -516,97 +516,6 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
516516
}
517517

518518

519-
/**
520-
* Get ESP8266 station DHCP hostname
521-
* @return hostname
522-
*/
523-
String ESP8266WiFiSTAClass::hostname(void) {
524-
return wifi_station_get_hostname();
525-
}
526-
527-
/**
528-
* Set ESP8266 station DHCP hostname
529-
* @param aHostname max length:24
530-
* @return ok
531-
*/
532-
bool ESP8266WiFiSTAClass::hostname(const char* aHostname) {
533-
/*
534-
vvvv RFC952 vvvv
535-
ASSUMPTIONS
536-
1. A "name" (Net, Host, Gateway, or Domain name) is a text string up
537-
to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus
538-
sign (-), and period (.). Note that periods are only allowed when
539-
they serve to delimit components of "domain style names". (See
540-
RFC-921, "Domain Name System Implementation Schedule", for
541-
background). No blank or space characters are permitted as part of a
542-
name. No distinction is made between upper and lower case. The first
543-
character must be an alpha character. The last character must not be
544-
a minus sign or period. A host which serves as a GATEWAY should have
545-
"-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as
546-
Internet gateways should not use "-GATEWAY" and "-GW" as part of
547-
their names. A host which is a TAC should have "-TAC" as the last
548-
part of its host name, if it is a DoD host. Single character names
549-
or nicknames are not allowed.
550-
^^^^ RFC952 ^^^^
551-
552-
- 24 chars max
553-
- only a..z A..Z 0..9 '-'
554-
- no '-' as last char
555-
*/
556-
557-
size_t len = strlen(aHostname);
558-
559-
if (len == 0 || len > 32) {
560-
// nonos-sdk limit is 32
561-
// (dhcp hostname option minimum size is ~60)
562-
DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n");
563-
return false;
564-
}
565-
566-
// check RFC compliance
567-
bool compliant = (len <= 24);
568-
for (size_t i = 0; compliant && i < len; i++)
569-
if (!isalnum(aHostname[i]) && aHostname[i] != '-')
570-
compliant = false;
571-
if (aHostname[len - 1] == '-')
572-
compliant = false;
573-
574-
if (!compliant) {
575-
DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname);
576-
}
577-
578-
bool ret = wifi_station_set_hostname(aHostname);
579-
if (!ret) {
580-
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname);
581-
return false;
582-
}
583-
584-
// now we should inform dhcp server for this change, using lwip_renew()
585-
// looping through all existing interface
586-
// harmless for AP, also compatible with ethernet adapters (to come)
587-
for (netif* intf = netif_list; intf; intf = intf->next) {
588-
589-
// unconditionally update all known interfaces
590-
#if LWIP_VERSION_MAJOR == 1
591-
intf->hostname = (char*)wifi_station_get_hostname();
592-
#else
593-
intf->hostname = wifi_station_get_hostname();
594-
#endif
595-
596-
if (netif_dhcp_data(intf) != nullptr) {
597-
// renew already started DHCP leases
598-
err_t lwipret = dhcp_renew(intf);
599-
if (lwipret != ERR_OK) {
600-
DEBUG_WIFI_GENERIC("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n",
601-
intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num);
602-
ret = false;
603-
}
604-
}
605-
}
606-
607-
return ret && compliant;
608-
}
609-
610519
/**
611520
* Return Connection status.
612521
* @return one of the value defined in wl_status_t

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ class ESP8266WiFiSTAClass: public LwipIntf {
7070
IPAddress gatewayIP();
7171
IPAddress dnsIP(uint8_t dns_no = 0);
7272

73-
String hostname();
74-
bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); }
75-
bool hostname(const char* aHostname);
76-
7773
// STA WiFi info
7874
wl_status_t status();
7975
String SSID() const;

0 commit comments

Comments
 (0)
0