Skip to content

Commit

Permalink
add API to update hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanseifert committed Dec 13, 2024
1 parent 2fa63ad commit e306289
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/zephyr/net/hostname.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ extern "C" {
(IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? \
sizeof("0011223344556677") - 1 : 0))

/**
* @brief Set the device hostname
*/
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
int net_hostname_set(const char *newHostname);
#else
int net_hostname_set(const char *newHostname) {
return -ENOTSUP;
}
#endif


/**
* @brief Get the device hostname
*
Expand Down
21 changes: 21 additions & 0 deletions subsys/net/hostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,39 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);

#include <string.h>

#include <zephyr/zephyr.h>

#include <zephyr/net/hostname.h>
#include <zephyr/net/net_core.h>

static char hostname[NET_HOSTNAME_MAX_LEN + 1];

#if IS_ENABLED(CONFIG_LOG_BACKEND_NET)
extern int log_backend_net_set_hostname(const char *newHostname);
#endif

const char *net_hostname_get(void)
{
return hostname;
}

int net_hostname_set(const char *newHostname) {
if(!newHostname) {
return -EFAULT;
}

strncpy(hostname, newHostname, NET_HOSTNAME_MAX_LEN);

// update syslog hostname if enabled
#if IS_ENABLED(CONFIG_LOG_BACKEND_NET)
return log_backend_net_set_hostname(hostname);
#else
return 0;
#endif
}

#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
int net_hostname_set_postfix(const uint8_t *hostname_postfix,
int postfix_len)
Expand Down

0 comments on commit e306289

Please sign in to comment.