net: hostname: define public macro representing maximum hostname length

Add a macro in public header that represents maximum hostname string
length without terminating NULL character. This will allow other
modules, such as offloaded network drivers, to know how much space is
needed to allocate in order to fit whole hostname.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
This commit is contained in:
Marcin Niestroj 2020-11-16 22:02:17 +01:00 committed by Anas Nashif
parent f095e2fd05
commit aac0f98d1d
2 changed files with 8 additions and 9 deletions

View file

@ -22,6 +22,11 @@ extern "C" {
* @{
*/
#define NET_HOSTNAME_MAX_LEN \
(sizeof(CONFIG_NET_HOSTNAME) - 1 + \
(IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? \
sizeof("0011223344556677") - 1 : 0))
/**
* @brief Get the device hostname
*

View file

@ -13,16 +13,10 @@ LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
#include <zephyr.h>
#include <net/hostname.h>
#include <net/net_core.h>
#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
/* Allocate extra space to append MAC address to hostname */
#define EXTRA_SPACE (8 * 2)
#else
#define EXTRA_SPACE 0
#endif /* CONFIG_NET_HOSTNAME_UNIQUE */
static char hostname[sizeof(CONFIG_NET_HOSTNAME) + EXTRA_SPACE];
static char hostname[NET_HOSTNAME_MAX_LEN + 1];
const char *net_hostname_get(void)
{
@ -45,7 +39,7 @@ int net_hostname_set_postfix(const uint8_t *hostname_postfix,
/* Note that we convert the postfix to hex (2 chars / byte) */
if ((postfix_len * 2) >
((sizeof(hostname) - 1) - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
(NET_HOSTNAME_MAX_LEN - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
return -EMSGSIZE;
}