2017-09-21 22:30:32 +02:00
|
|
|
/** @file
|
|
|
|
* @brief Hostname configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-11-30 11:54:56 +01:00
|
|
|
LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
|
2017-09-21 22:30:32 +02:00
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2017-09-21 22:30:32 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/net/hostname.h>
|
|
|
|
#include <zephyr/net/net_core.h>
|
2023-10-18 17:46:35 +02:00
|
|
|
#include <zephyr/net/net_mgmt.h>
|
2024-01-12 14:49:54 +01:00
|
|
|
#include <zephyr/logging/log_backend_net.h>
|
2017-09-21 22:30:32 +02:00
|
|
|
|
2023-10-18 17:46:35 +02:00
|
|
|
static char hostname[NET_HOSTNAME_SIZE];
|
|
|
|
|
|
|
|
static void trigger_net_event(void)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_NET_MGMT_EVENT_INFO)) {
|
|
|
|
struct net_event_l4_hostname info;
|
|
|
|
|
|
|
|
memcpy(info.hostname, hostname, sizeof(hostname));
|
|
|
|
net_mgmt_event_notify_with_info(NET_EVENT_HOSTNAME_CHANGED, NULL,
|
|
|
|
&info, sizeof(info));
|
|
|
|
} else {
|
|
|
|
net_mgmt_event_notify(NET_EVENT_HOSTNAME_CHANGED, NULL);
|
|
|
|
}
|
2024-01-12 14:49:54 +01:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_NET)) {
|
|
|
|
log_backend_net_hostname_set(hostname, sizeof(hostname));
|
|
|
|
}
|
2023-10-18 17:46:35 +02:00
|
|
|
}
|
2017-09-21 22:30:32 +02:00
|
|
|
|
|
|
|
const char *net_hostname_get(void)
|
|
|
|
{
|
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
2024-01-10 14:20:07 +01:00
|
|
|
#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
|
|
|
|
|
2017-09-21 22:30:32 +02:00
|
|
|
#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
|
2020-05-27 18:26:57 +02:00
|
|
|
int net_hostname_set_postfix(const uint8_t *hostname_postfix,
|
2017-09-21 22:30:32 +02:00
|
|
|
int postfix_len)
|
|
|
|
{
|
2021-01-11 10:45:43 +01:00
|
|
|
#if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
|
2017-09-21 22:30:32 +02:00
|
|
|
static bool postfix_set;
|
2021-01-11 10:45:43 +01:00
|
|
|
#endif
|
2017-09-21 22:30:32 +02:00
|
|
|
int pos = 0;
|
|
|
|
int i;
|
|
|
|
|
2021-01-11 10:45:43 +01:00
|
|
|
#if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
|
2017-09-21 22:30:32 +02:00
|
|
|
if (postfix_set) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
2021-01-11 10:45:43 +01:00
|
|
|
#endif
|
2017-09-21 22:30:32 +02:00
|
|
|
|
|
|
|
NET_ASSERT(postfix_len > 0);
|
|
|
|
|
|
|
|
/* Note that we convert the postfix to hex (2 chars / byte) */
|
|
|
|
if ((postfix_len * 2) >
|
2020-11-16 22:02:17 +01:00
|
|
|
(NET_HOSTNAME_MAX_LEN - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
|
2017-09-21 22:30:32 +02:00
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < postfix_len; i++, pos += 2) {
|
2024-01-10 14:20:07 +01:00
|
|
|
snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], 2 + 1, "%02x",
|
|
|
|
hostname_postfix[i]);
|
2017-09-21 22:30:32 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 07:43:37 +02:00
|
|
|
NET_DBG("New hostname %s", hostname);
|
2017-09-21 22:30:32 +02:00
|
|
|
|
2021-01-11 10:45:43 +01:00
|
|
|
#if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
|
2017-09-21 22:30:32 +02:00
|
|
|
postfix_set = true;
|
2021-01-11 10:45:43 +01:00
|
|
|
#endif
|
2023-10-18 17:46:35 +02:00
|
|
|
trigger_net_event();
|
2017-09-21 22:30:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_HOSTNAME_UNIQUE */
|
|
|
|
|
|
|
|
void net_hostname_init(void)
|
|
|
|
{
|
|
|
|
memcpy(hostname, CONFIG_NET_HOSTNAME, sizeof(CONFIG_NET_HOSTNAME) - 1);
|
|
|
|
|
2020-11-16 20:29:30 +01:00
|
|
|
NET_DBG("Hostname set to %s", CONFIG_NET_HOSTNAME);
|
2023-10-18 17:46:35 +02:00
|
|
|
trigger_net_event();
|
2017-09-21 22:30:32 +02:00
|
|
|
}
|