Skip to content

Commit 4a3065b

Browse files
committed
Add methods for setting hostname
In the same ways it is done for setting user MAC address, add methods for setting hostname. The underlying network stack can then request this to the local DNS through DHCP.
1 parent ef8181a commit 4a3065b

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

connectivity/netsocket/include/netsocket/EMACInterface.h

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

89+
/** @copydoc NetworkInterface::get_hostname */
90+
const char *get_hostname() override;
91+
92+
/** @copydoc NetworkInterface::set_hostname */
93+
nsapi_error_t set_hostname(const char *hostname) override;
94+
8995
/** @copydoc NetworkInterface::get_mac_address */
9096
const char *get_mac_address() override;
9197

@@ -149,6 +155,8 @@ class EMACInterface : public virtual NetworkInterface {
149155
OnboardNetworkStack::Interface *_interface = nullptr;
150156
bool _dhcp = true;
151157
bool _blocking = true;
158+
bool _hostname_set = false;
159+
char _hostname[NSAPI_HOSTNAME_SIZE];
152160
bool _hw_mac_addr_set = false;
153161
char _mac_address[NSAPI_MAC_SIZE];
154162
char _ip_address[NSAPI_IPv6_SIZE] {};

connectivity/netsocket/include/netsocket/NetworkInterface.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ 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 (e.g. for
106+
* LwIP stack, hostname can only be set before calling
107+
* \c EthernetInterface::connect method)
108+
*/
109+
virtual nsapi_error_t set_hostname(const char *hostname);
110+
93111
/** Get the local MAC address.
94112
*
95113
* 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;

0 commit comments

Comments
 (0)