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 <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
13
14 #include <zephyr/kernel.h>
15
16 #include <zephyr/net/hostname.h>
17 #include <zephyr/net/net_core.h>
18 #include <zephyr/net/net_mgmt.h>
19 #include <zephyr/logging/log_backend_net.h>
20
21 static char hostname[NET_HOSTNAME_SIZE];
22
trigger_net_event(void)23 static void trigger_net_event(void)
24 {
25 if (IS_ENABLED(CONFIG_NET_MGMT_EVENT_INFO)) {
26 struct net_event_l4_hostname info;
27
28 memcpy(info.hostname, hostname, sizeof(hostname));
29 net_mgmt_event_notify_with_info(NET_EVENT_HOSTNAME_CHANGED, NULL,
30 &info, sizeof(info));
31 } else {
32 net_mgmt_event_notify(NET_EVENT_HOSTNAME_CHANGED, NULL);
33 }
34
35 if (IS_ENABLED(CONFIG_LOG_BACKEND_NET)) {
36 log_backend_net_hostname_set(hostname, sizeof(hostname));
37 }
38 }
39
net_hostname_get(void)40 const char *net_hostname_get(void)
41 {
42 return hostname;
43 }
44
45 #if defined(CONFIG_NET_HOSTNAME_DYNAMIC)
net_hostname_set(char * host,size_t len)46 int net_hostname_set(char *host, size_t len)
47 {
48 if (len > NET_HOSTNAME_MAX_LEN) {
49 return -ENOMEM;
50 }
51
52 memcpy(hostname, host, len);
53 hostname[len] = 0;
54
55 NET_DBG("New hostname %s", hostname);
56 trigger_net_event();
57
58 return 0;
59 }
60 #endif
61
62 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
net_hostname_set_postfix(const uint8_t * hostname_postfix,int postfix_len)63 int net_hostname_set_postfix(const uint8_t *hostname_postfix,
64 int postfix_len)
65 {
66 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
67 static bool postfix_set;
68 #endif
69 int pos = 0;
70 int i;
71
72 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
73 if (postfix_set) {
74 return -EALREADY;
75 }
76 #endif
77
78 NET_ASSERT(postfix_len > 0);
79
80 /* Note that we convert the postfix to hex (2 chars / byte) */
81 if ((postfix_len * 2) >
82 (NET_HOSTNAME_MAX_LEN - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
83 return -EMSGSIZE;
84 }
85
86 for (i = 0; i < postfix_len; i++, pos += 2) {
87 snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], 2 + 1, "%02x",
88 hostname_postfix[i]);
89 }
90
91 NET_DBG("New hostname %s", hostname);
92
93 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
94 postfix_set = true;
95 #endif
96 trigger_net_event();
97
98 return 0;
99 }
100
net_hostname_set_postfix_str(const uint8_t * hostname_postfix,int postfix_len)101 int net_hostname_set_postfix_str(const uint8_t *hostname_postfix,
102 int postfix_len)
103 {
104 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
105 static bool postfix_set;
106 #endif
107 int net_hostname_len = sizeof(CONFIG_NET_HOSTNAME) - 1;
108 int hostname_len_remain = (NET_HOSTNAME_MAX_LEN - net_hostname_len) + 1;
109
110 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
111 if (postfix_set) {
112 return -EALREADY;
113 }
114 #endif
115
116 NET_ASSERT(postfix_len > 0);
117
118 if (hostname_len_remain < postfix_len) {
119 NET_DBG("Hostname postfix length %d is exceeding limit of %d", postfix_len,
120 hostname_len_remain);
121 return -EMSGSIZE;
122 }
123
124 snprintk(&hostname[net_hostname_len], hostname_len_remain, "%s", hostname_postfix);
125
126 NET_DBG("New Unique hostname: %s", hostname);
127
128 #if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
129 postfix_set = true;
130 #endif
131 trigger_net_event();
132
133 return 0;
134 }
135 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
136
net_hostname_init(void)137 void net_hostname_init(void)
138 {
139 memcpy(hostname, CONFIG_NET_HOSTNAME, sizeof(CONFIG_NET_HOSTNAME) - 1);
140
141 NET_DBG("Hostname set to %s", CONFIG_NET_HOSTNAME);
142 trigger_net_event();
143 }
144