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 	ARG_UNUSED(host);
79 	ARG_UNUSED(len);
80 	return -ENOTSUP;
81 }
82 #endif
83 
84 /**
85  * @brief Initialize and set the device hostname.
86  *
87  */
88 #if defined(CONFIG_NET_HOSTNAME_ENABLE)
89 void net_hostname_init(void);
90 #else
net_hostname_init(void)91 static inline void net_hostname_init(void)
92 {
93 }
94 #endif /* CONFIG_NET_HOSTNAME_ENABLE */
95 
96 /**
97  * @brief Set the device hostname postfix
98  *
99  * @details Convert the hostname postfix to hexadecimal value and set the
100  * device hostname with the converted value. This is only used if
101  * CONFIG_NET_HOSTNAME_UNIQUE is set.
102  *
103  * @param hostname_postfix Usually link address. The function will convert this
104  * to a hexadecimal string.
105  * @param postfix_len Length of the hostname_postfix array.
106  *
107  * @return 0 if ok, <0 if error
108  */
109 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
110 int net_hostname_set_postfix(const uint8_t *hostname_postfix,
111 			      int postfix_len);
112 #else
net_hostname_set_postfix(const uint8_t * hostname_postfix,int postfix_len)113 static inline int net_hostname_set_postfix(const uint8_t *hostname_postfix,
114 					   int postfix_len)
115 {
116 	ARG_UNUSED(hostname_postfix);
117 	ARG_UNUSED(postfix_len);
118 	return -EMSGSIZE;
119 }
120 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
121 
122 /**
123  * @brief Set the postfix string for the network hostname.
124  *
125  * @details Set the hostname postfix string for the network hostname as is, without any conversion.
126  * This is only used if CONFIG_NET_HOSTNAME_UNIQUE is set. The function checks if the combined
127  * length of the default hostname (defined by CONFIG_NET_HOSTNAME) and the postfix does not exceed
128  * NET_HOSTNAME_MAX_LEN. If the postfix is too long, the function returns an
129  * error.
130  *
131  * @param hostname_postfix Pointer to the postfix string to be appended to the network hostname.
132  * @param postfix_len Length of the hostname_postfix array.
133  *
134  * @return 0 if ok, <0 if error
135  */
136 #if defined(CONFIG_NET_HOSTNAME_UNIQUE)
137 int net_hostname_set_postfix_str(const uint8_t *hostname_postfix,
138 			     int postfix_len);
139 #else
net_hostname_set_postfix_str(const uint8_t * hostname_postfix,int postfix_len)140 static inline int net_hostname_set_postfix_str(const uint8_t *hostname_postfix,
141 					   int postfix_len)
142 {
143 	ARG_UNUSED(hostname_postfix);
144 	ARG_UNUSED(postfix_len);
145 	return -EMSGSIZE;
146 }
147 #endif /* CONFIG_NET_HOSTNAME_UNIQUE */
148 
149 /**
150  * @}
151  */
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif /* ZEPHYR_INCLUDE_NET_HOSTNAME_H_ */
158