1 /** @file
2 * @brief Hostname configuration
3 */
4
5 /*
6 * Copyright (c) 2017 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #include <logging/log.h>
12 LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
13
14 #include <zephyr.h>
15
16 #include <net/hostname.h>
17 #include <net/net_core.h>
18
19 static char hostname[NET_HOSTNAME_MAX_LEN + 1];
20
net_hostname_get(void)21 const char *net_hostname_get(void)
22 {
23 return hostname;
24 }
25
26 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
net_hostname_set_postfix(const uint8_t * hostname_postfix,int postfix_len)27 int net_hostname_set_postfix(const uint8_t *hostname_postfix,
28 int postfix_len)
29 {
30 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
31 static bool postfix_set;
32 #endif
33 int pos = 0;
34 int i;
35
36 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
37 if (postfix_set) {
38 return -EALREADY;
39 }
40 #endif
41
42 NET_ASSERT(postfix_len > 0);
43
44 /* Note that we convert the postfix to hex (2 chars / byte) */
45 if ((postfix_len * 2) >
46 (NET_HOSTNAME_MAX_LEN - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
47 return -EMSGSIZE;
48 }
49
50 for (i = 0; i < postfix_len; i++, pos += 2) {
51 snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos],
52 2 + 1, "%02x", hostname_postfix[i]);
53 }
54
55 NET_DBG("New hostname %s", log_strdup(hostname));
56
57 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
58 postfix_set = true;
59 #endif
60
61 return 0;
62 }
63 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
64
net_hostname_init(void)65 void net_hostname_init(void)
66 {
67 memcpy(hostname, CONFIG_NET_HOSTNAME, sizeof(CONFIG_NET_HOSTNAME) - 1);
68
69 NET_DBG("Hostname set to %s", CONFIG_NET_HOSTNAME);
70 }
71