1 /** @file
2 * @brief Hostname configuration definitions
3 */
4
5 /*
6 * Copyright (c) 2017 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #ifndef ZEPHYR_INCLUDE_NET_HOSTNAME_H_
12 #define ZEPHYR_INCLUDE_NET_HOSTNAME_H_
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 /**
19 * @brief Network hostname configuration library
20 * @defgroup net_hostname Network Hostname Library
21 * @since 1.10
22 * @version 0.8.0
23 * @ingroup networking
24 * @{
25 */
26
27 #if defined(CONFIG_NET_HOSTNAME_MAX_LEN)
28 #define NET_HOSTNAME_MAX_LEN \
29 MAX(CONFIG_NET_HOSTNAME_MAX_LEN, \
30 (sizeof(CONFIG_NET_HOSTNAME) - 1 + \
31 (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0)))
32 #else
33 /** Maximum hostname length */
34 #define NET_HOSTNAME_MAX_LEN \
35 (sizeof(CONFIG_NET_HOSTNAME) - 1 + \
36 (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0))
37 #endif
38
39 /** @cond INTERNAL_HIDDEN */
40
41 #if defined(CONFIG_NET_HOSTNAME_ENABLE)
42 #define NET_HOSTNAME_SIZE NET_HOSTNAME_MAX_LEN + 1
43 #else
44 #define NET_HOSTNAME_SIZE 1
45 #endif
46
47 /** @endcond */
48
49 /**
50 * @brief Get the device hostname
51 *
52 * @details Return pointer to device hostname.
53 *
54 * @return Pointer to hostname or NULL if not set.
55 */
56 #if defined(CONFIG_NET_HOSTNAME_ENABLE)
57 const char *net_hostname_get(void);
58 #else
59 static inline const char *net_hostname_get(void)
60 {
61 return "zephyr";
62 }
63 #endif /* CONFIG_NET_HOSTNAME_ENABLE */
64
65 /**
66 * @brief Set the device hostname
67 *
68 * @param host new hostname as char array.
69 * @param len Length of the hostname array.
70 *
71 * @return 0 if ok, <0 on error
72 */
73 #if defined(CONFIG_NET_HOSTNAME_DYNAMIC)
74 int net_hostname_set(char *host, size_t len);
75 #else
net_hostname_set(char * host,size_t len)76 static inline int net_hostname_set(char *host, size_t len)
77 {
78 return -ENOTSUP;
79 }
80 #endif
81
82 /**
83 * @brief Initialize and set the device hostname.
84 *
85 */
86 #if defined(CONFIG_NET_HOSTNAME_ENABLE)
87 void net_hostname_init(void);
88 #else
net_hostname_init(void)89 static inline void net_hostname_init(void)
90 {
91 }
92 #endif /* CONFIG_NET_HOSTNAME_ENABLE */
93
94 /**
95 * @brief Set the device hostname postfix
96 *
97 * @details Convert the hostname postfix to hexadecimal value and set the
98 * device hostname with the converted value. This is only used if
99 * CONFIG_NET_HOSTNAME_UNIQUE is set.
100 *
101 * @param hostname_postfix Usually link address. The function will convert this
102 * to a hexadecimal string.
103 * @param postfix_len Length of the hostname_postfix array.
104 *
105 * @return 0 if ok, <0 if error
106 */
107 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
108 int net_hostname_set_postfix(const uint8_t *hostname_postfix,
109 int postfix_len);
110 #else
net_hostname_set_postfix(const uint8_t * hostname_postfix,int postfix_len)111 static inline int net_hostname_set_postfix(const uint8_t *hostname_postfix,
112 int postfix_len)
113 {
114 ARG_UNUSED(hostname_postfix);
115 ARG_UNUSED(postfix_len);
116 return -EMSGSIZE;
117 }
118 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
119
120 /**
121 * @brief Set the postfix string for the network hostname.
122 *
123 * @details Set the hostname postfix string for the network hostname as is, without any conversion.
124 * This is only used if CONFIG_NET_HOSTNAME_UNIQUE is set. The function checks if the combined
125 * length of the default hostname (defined by CONFIG_NET_HOSTNAME) and the postfix does not exceed
126 * NET_HOSTNAME_MAX_LEN. If the postfix is too long, the function returns an
127 * error.
128 *
129 * @param hostname_postfix Pointer to the postfix string to be appended to the network hostname.
130 * @param postfix_len Length of the hostname_postfix array.
131 *
132 * @return 0 if ok, <0 if error
133 */
134 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
135 int net_hostname_set_postfix_str(const uint8_t *hostname_postfix,
136 int postfix_len);
137 #else
net_hostname_set_postfix_str(const uint8_t * hostname_postfix,int postfix_len)138 static inline int net_hostname_set_postfix_str(const uint8_t *hostname_postfix,
139 int postfix_len)
140 {
141 ARG_UNUSED(hostname_postfix);
142 ARG_UNUSED(postfix_len);
143 return -EMSGSIZE;
144 }
145 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
146
147 /**
148 * @}
149 */
150
151 #ifdef __cplusplus
152 }
153 #endif
154
155 #endif /* ZEPHYR_INCLUDE_NET_HOSTNAME_H_ */
156