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