Browse Source

net: add set hostname function

This commit adds a function to set the hostname on runtime.

Signed-off-by: Gerhard Jörges <joerges@metratec.com>
pull/67730/head
Gerhard Jörges 2 years ago committed by Carles Cufí
parent
commit
72a51c7ec4
  1. 27
      include/zephyr/net/hostname.h
  2. 17
      subsys/net/Kconfig.hostname
  3. 21
      subsys/net/hostname.c

27
include/zephyr/net/hostname.h

@ -22,10 +22,16 @@ extern "C" {
* @{ * @{
*/ */
#if defined(CONFIG_NET_HOSTNAME_MAX_LEN)
#define NET_HOSTNAME_MAX_LEN \ #define NET_HOSTNAME_MAX_LEN \
MAX(CONFIG_NET_HOSTNAME_MAX_LEN, \
(sizeof(CONFIG_NET_HOSTNAME) - 1 + \ (sizeof(CONFIG_NET_HOSTNAME) - 1 + \
(IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? \ (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0)))
sizeof("0011223344556677") - 1 : 0)) #else
#define NET_HOSTNAME_MAX_LEN \
(sizeof(CONFIG_NET_HOSTNAME) - 1 + \
(IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0))
#endif
#if defined(CONFIG_NET_HOSTNAME_ENABLE) #if defined(CONFIG_NET_HOSTNAME_ENABLE)
#define NET_HOSTNAME_SIZE NET_HOSTNAME_MAX_LEN + 1 #define NET_HOSTNAME_SIZE NET_HOSTNAME_MAX_LEN + 1
@ -49,6 +55,23 @@ static inline const char *net_hostname_get(void)
} }
#endif /* CONFIG_NET_HOSTNAME_ENABLE */ #endif /* CONFIG_NET_HOSTNAME_ENABLE */
/**
* @brief Set the device hostname
*
* @param host new hostname as char array.
* @param len Length of the hostname array.
*
* @return 0 if ok, <0 on error
*/
#if defined(CONFIG_NET_HOSTNAME_DYNAMIC)
int net_hostname_set(char *host, size_t len);
#else
static inline int net_hostname_set(char *host, size_t len)
{
return -ENOTSUP;
}
#endif
/** /**
* @brief Initialize and set the device hostname. * @brief Initialize and set the device hostname.
* *

17
subsys/net/Kconfig.hostname

@ -17,10 +17,25 @@ config NET_HOSTNAME
help help
The string should be a valid hostname. The string should be a valid hostname.
config NET_HOSTNAME_DYNAMIC
bool "Allow the hostname to be set by the application"
depends on !NET_HOSTNAME_UNIQUE_UPDATE
help
This will enable the net_hostname_set() function. NET_HOSTNAME
will be used as default hostname.
config NET_HOSTNAME_MAX_LEN
int "The maximum allowed hostname length"
depends on NET_HOSTNAME_DYNAMIC
range 1 63
default 63
help
This will set the number of bytes allocateed for the hostname.
config NET_HOSTNAME_UNIQUE config NET_HOSTNAME_UNIQUE
bool "Make hostname unique" bool "Make hostname unique"
help help
This will append link address to hostname to create a unique This will append link address to NET_HOSTNAME to create a unique
hostname. For example, zephyr00005e005357 could be the hostname hostname. For example, zephyr00005e005357 could be the hostname
if this setting is enabled. if this setting is enabled.

21
subsys/net/hostname.c

@ -37,6 +37,23 @@ const char *net_hostname_get(void)
return hostname; return hostname;
} }
#if defined(CONFIG_NET_HOSTNAME_DYNAMIC)
int net_hostname_set(char *host, size_t len)
{
if (len > NET_HOSTNAME_MAX_LEN) {
return -ENOMEM;
}
memcpy(hostname, host, len);
hostname[len] = 0;
NET_DBG("New hostname %s", hostname);
trigger_net_event();
return 0;
}
#endif
#if defined(CONFIG_NET_HOSTNAME_UNIQUE) #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
int net_hostname_set_postfix(const uint8_t *hostname_postfix, int net_hostname_set_postfix(const uint8_t *hostname_postfix,
int postfix_len) int postfix_len)
@ -62,8 +79,8 @@ int net_hostname_set_postfix(const uint8_t *hostname_postfix,
} }
for (i = 0; i < postfix_len; i++, pos += 2) { for (i = 0; i < postfix_len; i++, pos += 2) {
snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], 2 + 1, "%02x",
2 + 1, "%02x", hostname_postfix[i]); hostname_postfix[i]);
} }
NET_DBG("New hostname %s", hostname); NET_DBG("New hostname %s", hostname);

Loading…
Cancel
Save