Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hostname request in lwIP interface #15506

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions connectivity/lwipstack/source/LWIPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ LWIP::Interface::Interface() :
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface)
{
#if LWIP_ETHERNET
const char *hostname;
Interface *interface = new (std::nothrow) Interface();
if (!interface) {
return NSAPI_ERROR_NO_MEMORY;
Expand All @@ -441,6 +442,11 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
interface->memory_manager = &memory_manager;
interface->ppp_enabled = false;

hostname = user_network_interface->get_hostname();
if(hostname) {
netif_set_hostname(&interface->netif, hostname);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does doing this in add_ethernet_interface() mean that you have to call set_hostname() before adding the Ethernet interface? What about if the user calls set_hostname() later? We should probably document this in the NetworkInterface comments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, EthernetInterface::set_hostname should be called before EthernetInterface::connect (which calls LWIP::add_ethernet_interface). If it's called later, EthernetInterface::set_hostname will return NSAPI_ERROR_BUSY, as the interface is already initialized.

This behaves exact like EthernetInterface::set_mac_address.

Copy link
Author

@guilhermerc guilhermerc Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rewrite NetworkInterface::set_hostname comments to address this behavior:

     *  @retval         NSAPI_ERROR_BUSY if interface is already initialized

I just think we should change it in both NetworkInterface::set_hostname and NetworkInterface::set_mac_address comments for consistency. What do you think?

}

#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
netif->interface.hwaddr[0] = MBED_MAC_ADDR_0;
netif->interface.hwaddr[1] = MBED_MAC_ADDR_1;
Expand Down
8 changes: 8 additions & 0 deletions connectivity/netsocket/include/netsocket/EMACInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class EMACInterface : public virtual NetworkInterface {
/** @copydoc NetworkInterface::disconnect */
nsapi_error_t disconnect() override;

/** @copydoc NetworkInterface::get_hostname */
const char *get_hostname() override;

/** @copydoc NetworkInterface::set_hostname */
nsapi_error_t set_hostname(const char *hostname) override;

/** @copydoc NetworkInterface::get_mac_address */
const char *get_mac_address() override;

Expand Down Expand Up @@ -146,6 +152,8 @@ class EMACInterface : public virtual NetworkInterface {
OnboardNetworkStack::Interface *_interface = nullptr;
bool _dhcp = true;
bool _blocking = true;
bool _hostname_set = false;
char _hostname[NSAPI_HOSTNAME_SIZE];
bool _hw_mac_addr_set = false;
char _mac_address[NSAPI_MAC_SIZE];
char _ip_address[NSAPI_IPv6_SIZE] {};
Expand Down
16 changes: 16 additions & 0 deletions connectivity/netsocket/include/netsocket/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ class NetworkInterface: public DNS {
*/
virtual void set_as_default();

/** Get hostname.
*
* @return Hostname if configured, null otherwise
*/
virtual const char *get_hostname();

/** Set hostname.
*
* @param hostname Hostname string
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if hostname is not valid
* @retval NSAPI_ERROR_BUSY if hostname can't be set
*/
virtual nsapi_error_t set_hostname(const char *hostname);

/** Get the local MAC address.
*
* Provided MAC address is intended for info or debug purposes and
Expand Down
10 changes: 10 additions & 0 deletions connectivity/netsocket/include/netsocket/nsapi_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ typedef enum nsapi_security {
*/
#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES

/** Maximum size of hostname
*
* According to RFC 1034 [1], Section 3.1 "Name space specifications and
* terminology", 63 is the maximum size of a hostname. +1 for the string
* terminator.
*
* [1] https://www.rfc-editor.org/rfc/rfc1034
*/
#define NSAPI_HOSTNAME_SIZE 64

/** Maximum size of MAC address representation
*/
#define NSAPI_MAC_SIZE 18
Expand Down
26 changes: 26 additions & 0 deletions connectivity/netsocket/source/EMACInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect()
return NSAPI_ERROR_NO_CONNECTION;
}

const char *EMACInterface::get_hostname()
{
if (_hostname_set) {
return _hostname;
}
return nullptr;
}

nsapi_error_t EMACInterface::set_hostname(const char *hostname)
{
if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE-1) {
return NSAPI_ERROR_PARAMETER;
}

if (_interface) {
// can't set hostname once initialized
return NSAPI_ERROR_BUSY;
}

memset(_hostname, 0, NSAPI_HOSTNAME_SIZE);
strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE-1);
_hostname_set = true;

return NSAPI_ERROR_OK;
}

const char *EMACInterface::get_mac_address()
{
if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
Expand Down
10 changes: 10 additions & 0 deletions connectivity/netsocket/source/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ void NetworkInterface::set_as_default()

}

const char *NetworkInterface::get_hostname()
{
return 0;
}

nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
{
return NSAPI_ERROR_UNSUPPORTED;
}

const char *NetworkInterface::get_mac_address()
{
return 0;
Expand Down