Skip to content

Commit 84c44cd

Browse files
authored
Merge pull request #969 from pennam/dhcp-hostname-support
WiFi and Eth Dhcp hostname support
2 parents adecc86 + da99e8b commit 84c44cd

7 files changed

+291
-1
lines changed

libraries/Ethernet/src/Ethernet.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ int arduino::EthernetClass::_begin(uint8_t *mac, unsigned long timeout, unsigned
2424
return (linkStatus() == LinkON ? 1 : 0);
2525
}
2626

27+
int arduino::EthernetClass::setHostname(const char* hostname) {
28+
eth_if->set_hostname(hostname);
29+
return 1;
30+
}
31+
2732
int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip) {
2833
IPAddress dns = ip;
2934
dns[3] = 1;

libraries/Ethernet/src/Ethernet.h

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class EthernetClass : public MbedSocketClass {
5656
EthernetClass(EthernetInterface *_if)
5757
: eth_if(_if){};
5858

59+
// When using DHCP the hostname provided will be used.
60+
int setHostname(const char* hostname);
61+
5962
// Initialise the Ethernet shield to use the provided MAC address and
6063
// gain the rest of the configuration through DHCP.
6164
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded

libraries/WiFi/src/WiFi.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ int arduino::WiFiClass::begin(const char* ssid) {
9696
return begin(ssid, NULL, ENC_TYPE_NONE);
9797
}
9898

99+
int arduino::WiFiClass::setHostname(const char* hostname) {
100+
wifi_if->set_hostname(hostname);
101+
return 1;
102+
}
103+
99104
//Config Wifi to set Static IP && Disable DHCP
100105
void arduino::WiFiClass::config(const char* localip, const char* netmask, const char* gateway){
101106
SocketHelpers::config(IPAddress(localip), dnsIP(0), IPAddress(gateway), IPAddress(netmask));

libraries/WiFi/src/WiFi.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ class WiFiClass : public MbedSocketClass {
7575
* must be between ASCII 32-126 (decimal).
7676
*/
7777
int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN);
78-
78+
79+
// When using DHCP the hostname provided will be used.
80+
int setHostname(const char* hostname);
81+
7982
// Inherit config methods from the parent class
8083
using MbedSocketClass::config;
8184

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
From 75b34cc20a33c05cbc01b3fbdabbfc40ae034bef Mon Sep 17 00:00:00 2001
2+
From: Guilherme Ricioli <[email protected]>
3+
Date: Mon, 15 Apr 2024 17:40:59 -0300
4+
Subject: [PATCH] Add methods for setting hostname
5+
6+
In the same way it is done for setting MAC address, add methods for
7+
setting hostname. The underlying network stack can then request this
8+
to the local DNS through DHCP.
9+
---
10+
.../include/netsocket/EMACInterface.h | 8 ++++++
11+
.../include/netsocket/NetworkInterface.h | 16 ++++++++++++
12+
.../netsocket/include/netsocket/nsapi_types.h | 10 +++++++
13+
.../netsocket/source/EMACInterface.cpp | 26 +++++++++++++++++++
14+
.../netsocket/source/NetworkInterface.cpp | 10 +++++++
15+
.../doubles/NetworkInterface_stub.cpp | 10 +++++++
16+
.../test_NetworkInterface.cpp | 11 ++++++++
17+
7 files changed, 91 insertions(+)
18+
19+
diff --git a/connectivity/netsocket/include/netsocket/EMACInterface.h b/connectivity/netsocket/include/netsocket/EMACInterface.h
20+
index 8cf47cb703..c06aeb850e 100644
21+
--- a/connectivity/netsocket/include/netsocket/EMACInterface.h
22+
+++ b/connectivity/netsocket/include/netsocket/EMACInterface.h
23+
@@ -83,6 +83,12 @@ public:
24+
/** @copydoc NetworkInterface::disconnect */
25+
nsapi_error_t disconnect() override;
26+
27+
+ /** @copydoc NetworkInterface::get_hostname */
28+
+ const char *get_hostname() override;
29+
+
30+
+ /** @copydoc NetworkInterface::set_hostname */
31+
+ nsapi_error_t set_hostname(const char *hostname) override;
32+
+
33+
/** @copydoc NetworkInterface::get_mac_address */
34+
const char *get_mac_address() override;
35+
36+
@@ -146,6 +152,8 @@ protected:
37+
OnboardNetworkStack::Interface *_interface = nullptr;
38+
bool _dhcp = true;
39+
bool _blocking = true;
40+
+ bool _hostname_set = false;
41+
+ char _hostname[NSAPI_HOSTNAME_SIZE];
42+
bool _hw_mac_addr_set = false;
43+
char _mac_address[NSAPI_MAC_SIZE];
44+
char _ip_address[NSAPI_IPv6_SIZE] {};
45+
diff --git a/connectivity/netsocket/include/netsocket/NetworkInterface.h b/connectivity/netsocket/include/netsocket/NetworkInterface.h
46+
index 9071a1e40b..81f6011950 100644
47+
--- a/connectivity/netsocket/include/netsocket/NetworkInterface.h
48+
+++ b/connectivity/netsocket/include/netsocket/NetworkInterface.h
49+
@@ -90,6 +90,22 @@ public:
50+
*/
51+
virtual void set_as_default();
52+
53+
+ /** Get hostname.
54+
+ *
55+
+ * @return Hostname if configured, null otherwise
56+
+ */
57+
+ virtual const char *get_hostname();
58+
+
59+
+ /** Set hostname.
60+
+ *
61+
+ * @param hostname Hostname string
62+
+ * @retval NSAPI_ERROR_OK on success
63+
+ * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
64+
+ * @retval NSAPI_ERROR_PARAMETER if hostname is not valid
65+
+ * @retval NSAPI_ERROR_BUSY if hostname couldn't be set
66+
+ */
67+
+ virtual nsapi_error_t set_hostname(const char *hostname);
68+
+
69+
/** Get the local MAC address.
70+
*
71+
* Provided MAC address is intended for info or debug purposes and
72+
diff --git a/connectivity/netsocket/include/netsocket/nsapi_types.h b/connectivity/netsocket/include/netsocket/nsapi_types.h
73+
index 3b496d5087..28dbcc9a38 100644
74+
--- a/connectivity/netsocket/include/netsocket/nsapi_types.h
75+
+++ b/connectivity/netsocket/include/netsocket/nsapi_types.h
76+
@@ -196,6 +196,16 @@ typedef enum nsapi_security {
77+
*/
78+
#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
79+
80+
+/** Maximum size of hostname
81+
+ *
82+
+ * According to RFC 1034 [1], Section 3.1 "Name space specifications and
83+
+ * terminology", 63 is the maximum size of a hostname. +1 for the string
84+
+ * terminator.
85+
+ *
86+
+ * [1] https://www.rfc-editor.org/rfc/rfc1034
87+
+ */
88+
+#define NSAPI_HOSTNAME_SIZE 64
89+
+
90+
/** Maximum size of MAC address representation
91+
*/
92+
#define NSAPI_MAC_SIZE 18
93+
diff --git a/connectivity/netsocket/source/EMACInterface.cpp b/connectivity/netsocket/source/EMACInterface.cpp
94+
index f48bc0a185..de8d9753d7 100644
95+
--- a/connectivity/netsocket/source/EMACInterface.cpp
96+
+++ b/connectivity/netsocket/source/EMACInterface.cpp
97+
@@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect()
98+
return NSAPI_ERROR_NO_CONNECTION;
99+
}
100+
101+
+const char *EMACInterface::get_hostname()
102+
+{
103+
+ if (_hostname_set) {
104+
+ return _hostname;
105+
+ }
106+
+ return nullptr;
107+
+}
108+
+
109+
+nsapi_error_t EMACInterface::set_hostname(const char *hostname)
110+
+{
111+
+ if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE - 1) {
112+
+ return NSAPI_ERROR_PARAMETER;
113+
+ }
114+
+
115+
+ if (_interface) {
116+
+ // can't set hostname once initialized
117+
+ return NSAPI_ERROR_BUSY;
118+
+ }
119+
+
120+
+ memset(_hostname, 0, NSAPI_HOSTNAME_SIZE);
121+
+ strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE - 1);
122+
+ _hostname_set = true;
123+
+
124+
+ return NSAPI_ERROR_OK;
125+
+}
126+
+
127+
const char *EMACInterface::get_mac_address()
128+
{
129+
if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
130+
diff --git a/connectivity/netsocket/source/NetworkInterface.cpp b/connectivity/netsocket/source/NetworkInterface.cpp
131+
index 0f237f0e19..649df0f9b3 100644
132+
--- a/connectivity/netsocket/source/NetworkInterface.cpp
133+
+++ b/connectivity/netsocket/source/NetworkInterface.cpp
134+
@@ -29,6 +29,16 @@ void NetworkInterface::set_as_default()
135+
136+
}
137+
138+
+const char *NetworkInterface::get_hostname()
139+
+{
140+
+ return 0;
141+
+}
142+
+
143+
+nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
144+
+{
145+
+ return NSAPI_ERROR_UNSUPPORTED;
146+
+}
147+
+
148+
const char *NetworkInterface::get_mac_address()
149+
{
150+
return 0;
151+
diff --git a/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp b/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp
152+
index 020a551ba9..c849704a35 100644
153+
--- a/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp
154+
+++ b/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp
155+
@@ -21,6 +21,16 @@
156+
157+
158+
// Default network-interface state
159+
+const char *NetworkInterface::get_hostname()
160+
+{
161+
+ return 0;
162+
+}
163+
+
164+
+nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
165+
+{
166+
+ return NSAPI_ERROR_UNSUPPORTED;
167+
+}
168+
+
169+
const char *NetworkInterface::get_mac_address()
170+
{
171+
return 0;
172+
diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp b/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp
173+
index 1a928c36ee..27433ffaa1 100644
174+
--- a/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp
175+
+++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp
176+
@@ -68,6 +68,17 @@ TEST_F(TestNetworkInterface, constructor)
177+
}
178+
179+
// get_default_instance is tested along with the implementations of NetworkInterface.
180+
+TEST_F(TestNetworkInterface, get_hostname)
181+
+{
182+
+ char *n = 0;
183+
+ EXPECT_EQ(iface->get_hostname(), n);
184+
+}
185+
+
186+
+TEST_F(TestNetworkInterface, set_hostname)
187+
+{
188+
+ char *hostname;
189+
+ EXPECT_EQ(iface->set_hostname(hostname), NSAPI_ERROR_UNSUPPORTED);
190+
+}
191+
192+
TEST_F(TestNetworkInterface, get_mac_address)
193+
{
194+
--
195+
2.45.2
196+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
From 091ea74d6956d6684bcd88ed842a73218a7b8bd3 Mon Sep 17 00:00:00 2001
2+
From: Guilherme Ricioli <[email protected]>
3+
Date: Tue, 16 Apr 2024 10:50:48 -0300
4+
Subject: [PATCH] Request hostname through DHCP
5+
6+
If hostname is provided, request it to local DNS through DHCP.
7+
---
8+
connectivity/lwipstack/source/LWIPInterface.cpp | 6 ++++++
9+
connectivity/netsocket/include/netsocket/NetworkInterface.h | 4 +++-
10+
2 files changed, 9 insertions(+), 1 deletion(-)
11+
12+
diff --git a/connectivity/lwipstack/source/LWIPInterface.cpp b/connectivity/lwipstack/source/LWIPInterface.cpp
13+
index dfefebcb8b..64869a3538 100644
14+
--- a/connectivity/lwipstack/source/LWIPInterface.cpp
15+
+++ b/connectivity/lwipstack/source/LWIPInterface.cpp
16+
@@ -437,6 +437,7 @@ LWIP::Interface::Interface() :
17+
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface)
18+
{
19+
#if LWIP_ETHERNET
20+
+ const char *hostname;
21+
Interface *interface = new (std::nothrow) Interface();
22+
if (!interface) {
23+
return NSAPI_ERROR_NO_MEMORY;
24+
@@ -445,6 +446,11 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
25+
interface->memory_manager = &memory_manager;
26+
interface->ppp_enabled = false;
27+
28+
+ hostname = user_network_interface->get_hostname();
29+
+ if (hostname) {
30+
+ netif_set_hostname(&interface->netif, hostname);
31+
+ }
32+
+
33+
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
34+
netif->interface.hwaddr[0] = MBED_MAC_ADDR_0;
35+
netif->interface.hwaddr[1] = MBED_MAC_ADDR_1;
36+
diff --git a/connectivity/netsocket/include/netsocket/NetworkInterface.h b/connectivity/netsocket/include/netsocket/NetworkInterface.h
37+
index 81f6011950..22355767ce 100644
38+
--- a/connectivity/netsocket/include/netsocket/NetworkInterface.h
39+
+++ b/connectivity/netsocket/include/netsocket/NetworkInterface.h
40+
@@ -102,7 +102,9 @@ public:
41+
* @retval NSAPI_ERROR_OK on success
42+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
43+
* @retval NSAPI_ERROR_PARAMETER if hostname is not valid
44+
- * @retval NSAPI_ERROR_BUSY if hostname couldn't be set
45+
+ * @retval NSAPI_ERROR_BUSY if hostname couldn't be set (e.g. for
46+
+ * LwIP stack, hostname can only be set before calling
47+
+ * \c EthernetInterface::connect method)
48+
*/
49+
virtual nsapi_error_t set_hostname(const char *hostname);
50+
51+
--
52+
2.45.2
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From b6c62d169e750de6e5af3a47bc6c0c2a1fc81c88 Mon Sep 17 00:00:00 2001
2+
From: pennam <[email protected]>
3+
Date: Thu, 3 Oct 2024 18:05:26 +0200
4+
Subject: [PATCH] WhdSTA allow hostname configuration
5+
6+
---
7+
.../drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp | 2 +-
8+
1 file changed, 1 insertion(+), 1 deletion(-)
9+
10+
diff --git a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
11+
index 509a2c0981..ba1fa7900a 100644
12+
--- a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
13+
+++ b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
14+
@@ -280,7 +280,7 @@ nsapi_error_t WhdSTAInterface::connect()
15+
_iface_shared.if_status_flags |= IF_STATUS_STA_UP;
16+
_iface_shared.default_if_cfg = DEFAULT_IF_STA;
17+
if (!_interface) {
18+
- nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
19+
+ nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface, this);
20+
if (err != NSAPI_ERROR_OK) {
21+
_interface = NULL;
22+
return err;
23+
--
24+
2.45.2
25+

0 commit comments

Comments
 (0)