/src/libcoap/include/coap3/coap_address.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * coap_address.h -- representation of network addresses |
3 | | * |
4 | | * Copyright (C) 2010-2011,2015-2016,2022-2025 Olaf Bergmann <bergmann@tzi.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | * |
8 | | * This file is part of the CoAP library libcoap. Please see README for terms |
9 | | * of use. |
10 | | */ |
11 | | |
12 | | /** |
13 | | * @file coap_address.h |
14 | | * @brief Representation of network addresses |
15 | | */ |
16 | | |
17 | | #ifndef COAP_ADDRESS_H_ |
18 | | #define COAP_ADDRESS_H_ |
19 | | |
20 | | #include <assert.h> |
21 | | #include <stdint.h> |
22 | | #include <string.h> |
23 | | #include <sys/types.h> |
24 | | #include "libcoap.h" |
25 | | |
26 | | #include "coap3/coap_pdu.h" |
27 | | |
28 | | #if defined(WITH_LWIP) |
29 | | |
30 | | #include <lwip/ip_addr.h> |
31 | | |
32 | | struct coap_address_t { |
33 | | uint16_t port; |
34 | | ip_addr_t addr; |
35 | | }; |
36 | | |
37 | | /** |
38 | | * Returns the port from @p addr in host byte order. |
39 | | */ |
40 | | COAP_STATIC_INLINE uint16_t |
41 | | coap_address_get_port(const coap_address_t *addr) { |
42 | | return addr->port; |
43 | | } |
44 | | |
45 | | /** |
46 | | * Sets the port field of @p addr to @p port (in host byte order). |
47 | | */ |
48 | | COAP_STATIC_INLINE void |
49 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
50 | | addr->port = port; |
51 | | } |
52 | | |
53 | | #define _coap_address_equals_impl(A, B) \ |
54 | | ((A)->port == (B)->port && \ |
55 | | (!!ip_addr_cmp(&(A)->addr,&(B)->addr))) |
56 | | |
57 | | #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr) |
58 | | |
59 | | #elif defined(WITH_CONTIKI) |
60 | | |
61 | | #include "uip.h" |
62 | | |
63 | | struct coap_address_t { |
64 | | uip_ipaddr_t addr; |
65 | | uint16_t port; |
66 | | }; |
67 | | |
68 | | /** |
69 | | * Returns the port from @p addr in host byte order. |
70 | | */ |
71 | | COAP_STATIC_INLINE uint16_t |
72 | | coap_address_get_port(const coap_address_t *addr) { |
73 | | return uip_ntohs(addr->port); |
74 | | } |
75 | | |
76 | | /** |
77 | | * Sets the port field of @p addr to @p port (in host byte order). |
78 | | */ |
79 | | COAP_STATIC_INLINE void |
80 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
81 | | addr->port = uip_htons(port); |
82 | | } |
83 | | |
84 | | #define _coap_address_equals_impl(A,B) \ |
85 | | ((A)->port == (B)->port && \ |
86 | | uip_ipaddr_cmp(&((A)->addr),&((B)->addr))) |
87 | | |
88 | | /** @todo implementation of _coap_address_isany_impl() for Contiki */ |
89 | | #define _coap_address_isany_impl(A) 0 |
90 | | |
91 | | #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr)) |
92 | | |
93 | | #elif defined(RIOT_VERSION) |
94 | | |
95 | | #include "net/sock.h" |
96 | | #include "net/af.h" |
97 | | |
98 | | #ifndef INET6_ADDRSTRLEN |
99 | | #define INET6_ADDRSTRLEN IPV6_ADDR_MAX_STR_LEN |
100 | | #endif /* !INET6_ADDRSTRLEN */ |
101 | | |
102 | | struct coap_address_t { |
103 | | struct _sock_tl_ep riot; |
104 | | }; |
105 | | |
106 | | #define _coap_address_isany_impl(A) 0 |
107 | | |
108 | | #define _coap_address_equals_impl(A, B) \ |
109 | | ((A)->riot.family == (B)->riot.family && \ |
110 | | (A)->riot.port == (B)->riot.port && \ |
111 | | memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \ |
112 | | sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0) |
113 | | |
114 | | #define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff) |
115 | | |
116 | | /** |
117 | | * Returns the port from @p addr in host byte order. |
118 | | */ |
119 | | COAP_STATIC_INLINE uint16_t |
120 | | coap_address_get_port(const coap_address_t *addr) { |
121 | | return addr->riot.port; |
122 | | } |
123 | | |
124 | | /** |
125 | | * Sets the port field of @p addr to @p port (in host byte order). |
126 | | */ |
127 | | COAP_STATIC_INLINE void |
128 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
129 | | addr->riot.port = port; |
130 | | } |
131 | | |
132 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */ |
133 | | |
134 | | #ifdef _WIN32 |
135 | | #define sa_family_t short |
136 | | #endif /* _WIN32 */ |
137 | | |
138 | 0 | #define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t)) |
139 | | |
140 | | struct coap_sockaddr_un { |
141 | | sa_family_t sun_family; /* AF_UNIX */ |
142 | | char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */ |
143 | | }; |
144 | | |
145 | | /** Multi-purpose address abstraction */ |
146 | | struct coap_address_t { |
147 | | socklen_t size; /**< size of addr */ |
148 | | union { |
149 | | struct sockaddr sa; |
150 | | struct sockaddr_in sin; |
151 | | struct sockaddr_in6 sin6; |
152 | | struct coap_sockaddr_un cun; /* CoAP shortened special */ |
153 | | } addr; |
154 | | }; |
155 | | |
156 | | /** |
157 | | * Returns the port from @p addr in host byte order. |
158 | | */ |
159 | | uint16_t coap_address_get_port(const coap_address_t *addr); |
160 | | |
161 | | /** |
162 | | * Set the port field of @p addr to @p port (in host byte order). |
163 | | */ |
164 | | void coap_address_set_port(coap_address_t *addr, uint16_t port); |
165 | | |
166 | | /** |
167 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
168 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
169 | | * @c NULL; |
170 | | */ |
171 | | int coap_address_equals(const coap_address_t *a, const coap_address_t *b); |
172 | | |
173 | | int _coap_address_isany_impl(const coap_address_t *a); |
174 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
175 | | |
176 | | /** Resolved addresses information */ |
177 | | typedef struct coap_addr_info_t { |
178 | | struct coap_addr_info_t *next; /**< Next entry in the chain */ |
179 | | coap_uri_scheme_t scheme; /**< CoAP scheme to use */ |
180 | | coap_proto_t proto; /**< CoAP protocol to use */ |
181 | | coap_address_t addr; /**< The address to connect / bind to */ |
182 | | } coap_addr_info_t; |
183 | | |
184 | | /** |
185 | | * Determine and set up scheme_hint_bits for a server that can be used in |
186 | | * a call to coap_resolve_address_info(). |
187 | | * |
188 | | * @param have_pki_psk Set to @c 1 if PSK/PKI information is known else @c 0. |
189 | | * @param ws_check Set to @c 1 is WebSockets is to be included in the list |
190 | | * else @c 0. |
191 | | * @param use_unix_proto Set to the appropriate protocol to use for Unix |
192 | | * sockets, else set to COAP_PROTO_NONE for INET / INET6 |
193 | | * sockets. |
194 | | * @return A bit mask of the available CoAP protocols (can be @c 0 if none) |
195 | | * suitable for passing to coap_resolve_address_info(). |
196 | | */ |
197 | | uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, |
198 | | coap_proto_t use_unix_proto); |
199 | | |
200 | | /** |
201 | | * coap_resolve_type_t values |
202 | | */ |
203 | | typedef enum coap_resolve_type_t { |
204 | | COAP_RESOLVE_TYPE_LOCAL, /**< local side of session */ |
205 | | COAP_RESOLVE_TYPE_REMOTE, /**< remote side of session */ |
206 | | } coap_resolve_type_t; |
207 | | |
208 | | /** |
209 | | * Resolve the specified @p address into a set of coap_address_t that can |
210 | | * be used to bind() (local) or connect() (remote) to. |
211 | | * |
212 | | * @param address The Address to resolve. |
213 | | * @param port The unsecured protocol port to use. |
214 | | * @param secure_port The secured protocol port to use. |
215 | | * @param ws_port The unsecured WebSockets port to use. |
216 | | * @param ws_secure_port The secured WebSockets port to use. |
217 | | * @param ai_hints_flags AI_* Hint flags to use for internal getaddrinfo(). |
218 | | * @param scheme_hint_bits Which schemes to return information for. One or |
219 | | * more of COAP_URI_SCHEME_*_BIT or'd together. |
220 | | * @param type COAP_ADDRESS_TYPE_LOCAL or COAP_ADDRESS_TYPE_REMOTE |
221 | | * |
222 | | * @return One or more linked sets of coap_addr_info_t or @c NULL if error. |
223 | | */ |
224 | | coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *address, |
225 | | uint16_t port, |
226 | | uint16_t secure_port, |
227 | | uint16_t ws_port, |
228 | | uint16_t ws_secure_port, |
229 | | int ai_hints_flags, |
230 | | int scheme_hint_bits, |
231 | | coap_resolve_type_t type); |
232 | | |
233 | | /** |
234 | | * Free off the one or more linked sets of coap_addr_info_t returned from |
235 | | * coap_resolve_address_info(). |
236 | | * |
237 | | * @param info_list The set of coap_addr_info_t to free off. |
238 | | */ |
239 | | void coap_free_address_info(coap_addr_info_t *info_list); |
240 | | |
241 | | /** |
242 | | * Resets the given coap_address_t object @p addr to its default values. In |
243 | | * particular, the member size must be initialized to the available size for |
244 | | * storing addresses. |
245 | | * |
246 | | * @param addr The coap_address_t object to initialize. |
247 | | */ |
248 | | void coap_address_init(coap_address_t *addr); |
249 | | |
250 | | /** |
251 | | * Copy the parsed unix domain host into coap_address_t structure |
252 | | * translating %2F into / on the way. All other fields set as appropriate. |
253 | | * |
254 | | * @param addr coap_address_t to update. |
255 | | * @param host The parsed host from the CoAP URI with potential %2F encoding. |
256 | | * @param host_len The length of the parsed host from the CoAP URI with |
257 | | * potential %2F encoding. |
258 | | * |
259 | | * @return @c 1 success, @c 0 failure. |
260 | | */ |
261 | | int coap_address_set_unix_domain(coap_address_t *addr, |
262 | | const uint8_t *host, size_t host_len); |
263 | | |
264 | | /* Convenience function to copy IPv6 addresses without garbage. */ |
265 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION) |
266 | | COAP_STATIC_INLINE void |
267 | | coap_address_copy(coap_address_t *dst, const coap_address_t *src) { |
268 | | memcpy(dst, src, sizeof(coap_address_t)); |
269 | | } |
270 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI */ |
271 | | void coap_address_copy(coap_address_t *dst, const coap_address_t *src); |
272 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
273 | | |
274 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION) |
275 | | /** |
276 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
277 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
278 | | * @c NULL; |
279 | | */ |
280 | | COAP_STATIC_INLINE int |
281 | | coap_address_equals(const coap_address_t *a, const coap_address_t *b) { |
282 | | assert(a); |
283 | | assert(b); |
284 | | return _coap_address_equals_impl(a, b); |
285 | | } |
286 | | #endif |
287 | | |
288 | | /** |
289 | | * Checks if given address object @p a denotes the wildcard address. This |
290 | | * function returns @c 1 if this is the case, @c 0 otherwise. The parameters @p |
291 | | * a must not be @c NULL; |
292 | | */ |
293 | | COAP_STATIC_INLINE int |
294 | 0 | coap_address_isany(const coap_address_t *a) { |
295 | 0 | assert(a); |
296 | 0 | return _coap_address_isany_impl(a); |
297 | 0 | } Unexecuted instantiation: coap_debug.c:coap_address_isany Unexecuted instantiation: coap_encode.c:coap_address_isany Unexecuted instantiation: coap_net.c:coap_address_isany Unexecuted instantiation: coap_netif.c:coap_address_isany Unexecuted instantiation: coap_notls.c:coap_address_isany Unexecuted instantiation: coap_option.c:coap_address_isany Unexecuted instantiation: coap_oscore.c:coap_address_isany Unexecuted instantiation: coap_pdu.c:coap_address_isany Unexecuted instantiation: coap_proxy.c:coap_address_isany Unexecuted instantiation: coap_prng.c:coap_address_isany Unexecuted instantiation: coap_resource.c:coap_address_isany Unexecuted instantiation: coap_session.c:coap_address_isany Unexecuted instantiation: coap_sha1.c:coap_address_isany Unexecuted instantiation: coap_str.c:coap_address_isany Unexecuted instantiation: coap_subscribe.c:coap_address_isany Unexecuted instantiation: coap_tcp.c:coap_address_isany Unexecuted instantiation: coap_threadsafe.c:coap_address_isany Unexecuted instantiation: coap_time.c:coap_address_isany Unexecuted instantiation: coap_uri.c:coap_address_isany Unexecuted instantiation: coap_ws.c:coap_address_isany Unexecuted instantiation: oscore.c:coap_address_isany Unexecuted instantiation: oscore_cbor.c:coap_address_isany Unexecuted instantiation: oscore_context.c:coap_address_isany Unexecuted instantiation: oscore_cose.c:coap_address_isany Unexecuted instantiation: oscore_crypto.c:coap_address_isany Unexecuted instantiation: coap_address.c:coap_address_isany Unexecuted instantiation: coap_async.c:coap_address_isany Unexecuted instantiation: coap_block.c:coap_address_isany Unexecuted instantiation: coap_cache.c:coap_address_isany Unexecuted instantiation: coap_dtls.c:coap_address_isany Unexecuted instantiation: coap_hashkey.c:coap_address_isany Unexecuted instantiation: coap_io.c:coap_address_isany Unexecuted instantiation: coap_layers.c:coap_address_isany Unexecuted instantiation: coap_mem.c:coap_address_isany |
298 | | |
299 | | #if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) |
300 | | |
301 | | /** |
302 | | * Checks if given address @p a denotes a multicast address. This function |
303 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
304 | | */ |
305 | | int coap_is_mcast(const coap_address_t *a); |
306 | | |
307 | | /** |
308 | | * Checks if given address @p a denotes a broadcast address. This function |
309 | | * returns @c 1 if @p a is broadcast, @c 0 otherwise. |
310 | | */ |
311 | | int coap_is_bcast(const coap_address_t *a); |
312 | | |
313 | | /** |
314 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
315 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
316 | | */ |
317 | | int coap_is_af_unix(const coap_address_t *a); |
318 | | |
319 | | #else /* WITH_CONTIKI || RIOT_VERSION */ |
320 | | |
321 | | /** |
322 | | * Checks if given address @p a denotes a multicast address. This function |
323 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
324 | | */ |
325 | | COAP_STATIC_INLINE int |
326 | | coap_is_mcast(const coap_address_t *a) { |
327 | | return a && _coap_is_mcast_impl(a); |
328 | | } |
329 | | |
330 | | /** |
331 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
332 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
333 | | */ |
334 | | COAP_STATIC_INLINE int |
335 | | coap_is_af_unix(const coap_address_t *a) { |
336 | | (void)a; |
337 | | return 0; |
338 | | } |
339 | | |
340 | | #endif /* WITH_CONTIKI || RIOT_VERSION */ |
341 | | |
342 | | #endif /* COAP_ADDRESS_H_ */ |