Skip to content

Commit 6174f40

Browse files
guilhermercpennam
authored andcommitted
Add methods for setting hostname
In the same way it is done for setting MAC address, add methods for setting hostname. The underlying network stack can then request this to the local DNS through DHCP.
1 parent 4369443 commit 6174f40

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

connectivity/netsocket/include/netsocket/EMACInterface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class EMACInterface : public virtual NetworkInterface {
8383
/** @copydoc NetworkInterface::disconnect */
8484
nsapi_error_t disconnect() override;
8585

86+
/** @copydoc NetworkInterface::get_hostname */
87+
const char *get_hostname() override;
88+
89+
/** @copydoc NetworkInterface::set_hostname */
90+
nsapi_error_t set_hostname(const char *hostname) override;
91+
8692
/** @copydoc NetworkInterface::get_mac_address */
8793
const char *get_mac_address() override;
8894

@@ -146,6 +152,8 @@ class EMACInterface : public virtual NetworkInterface {
146152
OnboardNetworkStack::Interface *_interface = nullptr;
147153
bool _dhcp = true;
148154
bool _blocking = true;
155+
bool _hostname_set = false;
156+
char _hostname[NSAPI_HOSTNAME_SIZE];
149157
bool _hw_mac_addr_set = false;
150158
char _mac_address[NSAPI_MAC_SIZE];
151159
char _ip_address[NSAPI_IPv6_SIZE] {};

connectivity/netsocket/include/netsocket/NetworkInterface.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class NetworkInterface: public DNS {
9090
*/
9191
virtual void set_as_default();
9292

93+
/** Get hostname.
94+
*
95+
* @return Hostname if configured, null otherwise
96+
*/
97+
virtual const char *get_hostname();
98+
99+
/** Set hostname.
100+
*
101+
* @param hostname Hostname string
102+
* @retval NSAPI_ERROR_OK on success
103+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
104+
* @retval NSAPI_ERROR_PARAMETER if hostname is not valid
105+
* @retval NSAPI_ERROR_BUSY if hostname couldn't be set
106+
*/
107+
virtual nsapi_error_t set_hostname(const char *hostname);
108+
93109
/** Get the local MAC address.
94110
*
95111
* Provided MAC address is intended for info or debug purposes and

connectivity/netsocket/include/netsocket/nsapi_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ typedef enum nsapi_security {
196196
*/
197197
#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
198198

199+
/** Maximum size of hostname
200+
*
201+
* According to RFC 1034 [1], Section 3.1 "Name space specifications and
202+
* terminology", 63 is the maximum size of a hostname. +1 for the string
203+
* terminator.
204+
*
205+
* [1] https://www.rfc-editor.org/rfc/rfc1034
206+
*/
207+
#define NSAPI_HOSTNAME_SIZE 64
208+
199209
/** Maximum size of MAC address representation
200210
*/
201211
#define NSAPI_MAC_SIZE 18

connectivity/netsocket/source/EMACInterface.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect()
8888
return NSAPI_ERROR_NO_CONNECTION;
8989
}
9090

91+
const char *EMACInterface::get_hostname()
92+
{
93+
if (_hostname_set) {
94+
return _hostname;
95+
}
96+
return nullptr;
97+
}
98+
99+
nsapi_error_t EMACInterface::set_hostname(const char *hostname)
100+
{
101+
if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE - 1) {
102+
return NSAPI_ERROR_PARAMETER;
103+
}
104+
105+
if (_interface) {
106+
// can't set hostname once initialized
107+
return NSAPI_ERROR_BUSY;
108+
}
109+
110+
memset(_hostname, 0, NSAPI_HOSTNAME_SIZE);
111+
strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE - 1);
112+
_hostname_set = true;
113+
114+
return NSAPI_ERROR_OK;
115+
}
116+
91117
const char *EMACInterface::get_mac_address()
92118
{
93119
if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {

connectivity/netsocket/source/NetworkInterface.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ void NetworkInterface::set_as_default()
2929

3030
}
3131

32+
const char *NetworkInterface::get_hostname()
33+
{
34+
return 0;
35+
}
36+
37+
nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
38+
{
39+
return NSAPI_ERROR_UNSUPPORTED;
40+
}
41+
3242
const char *NetworkInterface::get_mac_address()
3343
{
3444
return 0;

connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222

2323
// Default network-interface state
24+
const char *NetworkInterface::get_hostname()
25+
{
26+
return 0;
27+
}
28+
29+
nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
30+
{
31+
return NSAPI_ERROR_UNSUPPORTED;
32+
}
33+
2434
const char *NetworkInterface::get_mac_address()
2535
{
2636
return 0;

connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ TEST_F(TestNetworkInterface, constructor)
6868
}
6969

7070
// get_default_instance is tested along with the implementations of NetworkInterface.
71+
TEST_F(TestNetworkInterface, get_hostname)
72+
{
73+
char *n = 0;
74+
EXPECT_EQ(iface->get_hostname(), n);
75+
}
76+
77+
TEST_F(TestNetworkInterface, set_hostname)
78+
{
79+
char *hostname;
80+
EXPECT_EQ(iface->set_hostname(hostname), NSAPI_ERROR_UNSUPPORTED);
81+
}
7182

7283
TEST_F(TestNetworkInterface, get_mac_address)
7384
{

0 commit comments

Comments
 (0)