/src/libcoap/include/coap3/coap_address.h
Line | Count | Source |
1 | | /* |
2 | | * coap_address.h -- representation of network addresses |
3 | | * |
4 | | * Copyright (C) 2010-2011,2015-2016,2022-2026 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 "coap3/coap_pdu.h" |
21 | | |
22 | | #ifdef __cplusplus |
23 | | extern "C" { |
24 | | #endif |
25 | | |
26 | | #if defined(WITH_LWIP) |
27 | | |
28 | | #include <lwip/ip_addr.h> |
29 | | #include <string.h> |
30 | | |
31 | | struct coap_address_t { |
32 | | uint16_t port; |
33 | | ip_addr_t addr; |
34 | | }; |
35 | | |
36 | | /** |
37 | | * Returns the port from @p addr in host byte order. |
38 | | */ |
39 | | COAP_STATIC_INLINE uint16_t |
40 | | coap_address_get_port(const coap_address_t *addr) { |
41 | | return addr->port; |
42 | | } |
43 | | |
44 | | /** |
45 | | * Sets the port field of @p addr to @p port (in host byte order). |
46 | | */ |
47 | | COAP_STATIC_INLINE void |
48 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
49 | | addr->port = port; |
50 | | } |
51 | | |
52 | | /** |
53 | | * Sets the addr field of @p addr to 0. |
54 | | */ |
55 | | COAP_STATIC_INLINE void |
56 | | coap_address_clr_addr(coap_address_t *addr) { |
57 | | memset(&addr->addr, 0, sizeof(addr->addr)); |
58 | | } |
59 | | |
60 | | #define _coap_address_equals_impl(A, B) \ |
61 | | ((A)->port == (B)->port && \ |
62 | | (!!ip_addr_cmp(&(A)->addr,&(B)->addr))) |
63 | | |
64 | | #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr) |
65 | | |
66 | | #elif defined(WITH_CONTIKI) |
67 | | |
68 | | #include "uip.h" |
69 | | |
70 | | struct coap_address_t { |
71 | | uip_ipaddr_t addr; |
72 | | uint16_t port; |
73 | | }; |
74 | | |
75 | | /** |
76 | | * Returns the port from @p addr in host byte order. |
77 | | */ |
78 | | COAP_STATIC_INLINE uint16_t |
79 | | coap_address_get_port(const coap_address_t *addr) { |
80 | | return uip_ntohs(addr->port); |
81 | | } |
82 | | |
83 | | /** |
84 | | * Sets the port field of @p addr to @p port (in host byte order). |
85 | | */ |
86 | | COAP_STATIC_INLINE void |
87 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
88 | | addr->port = uip_htons(port); |
89 | | } |
90 | | |
91 | | /** |
92 | | * Sets the addr field of @p addr to 0. |
93 | | */ |
94 | | COAP_STATIC_INLINE void |
95 | | coap_address_clr_addr(coap_address_t *addr) { |
96 | | memset(&addr->addr, 0, sizeof(addr->addr)); |
97 | | } |
98 | | |
99 | | #define _coap_address_equals_impl(A,B) \ |
100 | | ((A)->port == (B)->port && \ |
101 | | uip_ipaddr_cmp(&((A)->addr),&((B)->addr))) |
102 | | |
103 | | /** @todo implementation of _coap_address_isany_impl() for Contiki */ |
104 | | #define _coap_address_isany_impl(A) 0 |
105 | | |
106 | | #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr)) |
107 | | |
108 | | #elif defined(RIOT_VERSION) |
109 | | |
110 | | #include "net/sock.h" |
111 | | #include "net/af.h" |
112 | | #include <string.h> |
113 | | |
114 | | #ifndef INET6_ADDRSTRLEN |
115 | | #define INET6_ADDRSTRLEN IPV6_ADDR_MAX_STR_LEN |
116 | | #endif /* !INET6_ADDRSTRLEN */ |
117 | | |
118 | | struct coap_address_t { |
119 | | struct _sock_tl_ep riot; |
120 | | }; |
121 | | |
122 | | #define _coap_address_isany_impl(A) 0 |
123 | | |
124 | | #define _coap_address_equals_impl(A, B) \ |
125 | | ((A)->riot.family == (B)->riot.family && \ |
126 | | (A)->riot.port == (B)->riot.port && \ |
127 | | memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \ |
128 | | sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0) |
129 | | |
130 | | #define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff) |
131 | | |
132 | | /** |
133 | | * Returns the port from @p addr in host byte order. |
134 | | */ |
135 | | COAP_STATIC_INLINE uint16_t |
136 | | coap_address_get_port(const coap_address_t *addr) { |
137 | | return addr->riot.port; |
138 | | } |
139 | | |
140 | | /** |
141 | | * Sets the port field of @p addr to @p port (in host byte order). |
142 | | */ |
143 | | COAP_STATIC_INLINE void |
144 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
145 | | addr->riot.port = port; |
146 | | } |
147 | | |
148 | | /** |
149 | | * Sets the addr field of @p addr to 0. |
150 | | */ |
151 | | COAP_STATIC_INLINE void |
152 | | coap_address_clr_addr(coap_address_t *addr) { |
153 | | memset(&addr->riot.addr, 0, sizeof(addr->riot.addr)); |
154 | | } |
155 | | |
156 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */ |
157 | | |
158 | | #if COAP_AF_LLC_SUPPORT |
159 | | |
160 | | /* Including net/if.h after linux/llc.h results in redefinitions of numerous |
161 | | * symbols. |
162 | | */ |
163 | | #include <net/if.h> |
164 | | #include <linux/llc.h> |
165 | | |
166 | | /* 00:11:22:33:44:55 */ |
167 | 0 | #define HW_ADDRSTRLEN 17 |
168 | | /* llc[00:11:22:33:44:55]:DC */ |
169 | 0 | #define LLC_HOST_LEN (4 + HW_ADDRSTRLEN + 4) |
170 | | |
171 | | #endif /* COAP_AF_LLC_SUPPORT */ |
172 | | |
173 | | #ifdef _WIN32 |
174 | | #define sa_family_t short |
175 | | #endif /* _WIN32 */ |
176 | | |
177 | 0 | #define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t)) |
178 | | |
179 | | struct coap_sockaddr_un { |
180 | | sa_family_t sun_family; /* AF_UNIX */ |
181 | | char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */ |
182 | | }; |
183 | | |
184 | | /** Multi-purpose address abstraction */ |
185 | | struct coap_address_t { |
186 | | socklen_t size; /**< size of addr */ |
187 | | union { |
188 | | struct sockaddr sa; |
189 | | struct sockaddr_in sin; |
190 | | struct sockaddr_in6 sin6; |
191 | | struct coap_sockaddr_un cun; /* CoAP shortened special */ |
192 | | #if COAP_AF_LLC_SUPPORT |
193 | | struct sockaddr_llc llc; |
194 | | #endif /* COAP_AF_LLC_SUPPORT */ |
195 | | } addr; |
196 | | }; |
197 | | |
198 | | /** |
199 | | * Returns the port from @p addr in host byte order. |
200 | | */ |
201 | | uint16_t coap_address_get_port(const coap_address_t *addr); |
202 | | |
203 | | /** |
204 | | * Set the port field of @p addr to @p port (in host byte order). |
205 | | */ |
206 | | void coap_address_set_port(coap_address_t *addr, uint16_t port); |
207 | | |
208 | | /** |
209 | | * Sets the addr field of @p addr to 0. |
210 | | */ |
211 | | void coap_address_clr_addr(coap_address_t *addr); |
212 | | |
213 | | /** |
214 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
215 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
216 | | * @c NULL; |
217 | | */ |
218 | | int coap_address_equals(const coap_address_t *a, const coap_address_t *b); |
219 | | |
220 | | int _coap_address_isany_impl(const coap_address_t *a); |
221 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
222 | | |
223 | | /** Resolved addresses information */ |
224 | | typedef struct coap_addr_info_t { |
225 | | struct coap_addr_info_t *next; /**< Next entry in the chain */ |
226 | | coap_uri_scheme_t scheme; /**< CoAP scheme to use */ |
227 | | coap_proto_t proto; /**< CoAP protocol to use */ |
228 | | coap_address_t addr; /**< The address to connect / bind to */ |
229 | | } coap_addr_info_t; |
230 | | |
231 | | /** |
232 | | * Determine and set up scheme_hint_bits for a server that can be used in |
233 | | * a call to coap_resolve_address_info(). |
234 | | * |
235 | | * @param have_pki_psk Set to @c 1 if PSK/PKI information is known else @c 0. |
236 | | * @param ws_check Set to @c 1 is WebSockets is to be included in the list |
237 | | * else @c 0. |
238 | | * @param use_proto Set to the appropriate protocol to use for Unix/LLC |
239 | | * sockets, else set to COAP_PROTO_NONE for INET / INET6 |
240 | | * sockets. |
241 | | * @return A bit mask of the available CoAP protocols (can be @c 0 if none) |
242 | | * suitable for passing to coap_resolve_address_info(). |
243 | | */ |
244 | | uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, |
245 | | coap_proto_t use_proto); |
246 | | |
247 | | /** |
248 | | * coap_resolve_type_t values |
249 | | */ |
250 | | typedef enum coap_resolve_type_t { |
251 | | COAP_RESOLVE_TYPE_LOCAL, /**< local side of session */ |
252 | | COAP_RESOLVE_TYPE_REMOTE, /**< remote side of session */ |
253 | | } coap_resolve_type_t; |
254 | | |
255 | | /** |
256 | | * Resolve the specified @p address into a set of coap_address_t that can |
257 | | * be used to bind() (local) or connect() (remote) to. |
258 | | * |
259 | | * @param address The Address to resolve. |
260 | | * @param port The unsecured protocol port to use. |
261 | | * @param secure_port The secured protocol port to use. |
262 | | * @param ws_port The unsecured WebSockets port to use. |
263 | | * @param ws_secure_port The secured WebSockets port to use. |
264 | | * @param ai_hints_flags AI_* Hint flags to use for internal getaddrinfo(). |
265 | | * @param scheme_hint_bits Which schemes to return information for. One or |
266 | | * more of COAP_URI_SCHEME_*_BIT or'd together. |
267 | | * @param type COAP_ADDRESS_TYPE_LOCAL or COAP_ADDRESS_TYPE_REMOTE |
268 | | * |
269 | | * @return One or more linked sets of coap_addr_info_t or @c NULL if error. |
270 | | */ |
271 | | coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *address, |
272 | | uint16_t port, |
273 | | uint16_t secure_port, |
274 | | uint16_t ws_port, |
275 | | uint16_t ws_secure_port, |
276 | | int ai_hints_flags, |
277 | | int scheme_hint_bits, |
278 | | coap_resolve_type_t type); |
279 | | |
280 | | /** |
281 | | * Free off the one or more linked sets of coap_addr_info_t returned from |
282 | | * coap_resolve_address_info(). |
283 | | * |
284 | | * @param info_list The set of coap_addr_info_t to free off. |
285 | | */ |
286 | | void coap_free_address_info(coap_addr_info_t *info_list); |
287 | | |
288 | | /** |
289 | | * Resets the given coap_address_t object @p addr to its default values. In |
290 | | * particular, the member size must be initialized to the available size for |
291 | | * storing addresses. |
292 | | * |
293 | | * @param addr The coap_address_t object to initialize. |
294 | | */ |
295 | | void coap_address_init(coap_address_t *addr); |
296 | | |
297 | | /** |
298 | | * Copy the parsed unix domain host into coap_address_t structure |
299 | | * translating %2F into / on the way. All other fields set as appropriate. |
300 | | * |
301 | | * @param addr coap_address_t to update. |
302 | | * @param host The parsed host from the CoAP URI with potential %2F encoding. |
303 | | * @param host_len The length of the parsed host from the CoAP URI with |
304 | | * potential %2F encoding. |
305 | | * |
306 | | * @return @c 1 success, @c 0 failure. |
307 | | */ |
308 | | int coap_address_set_unix_domain(coap_address_t *addr, |
309 | | const uint8_t *host, size_t host_len); |
310 | | |
311 | | /** |
312 | | * Copy the parsed LLC host into @p addr, setting other fields as appropriate. |
313 | | * |
314 | | * @param addr coap_address_t to update. |
315 | | * @param host The parsed host from the CoAP URI. |
316 | | * @param host_len The length of the parsed host from the CoAP URI. |
317 | | * |
318 | | * @return @c 1 success, @c 0 failure. |
319 | | */ |
320 | | int coap_address_set_llc(coap_address_t *addr, |
321 | | const uint8_t *host, size_t host_len); |
322 | | |
323 | | /* Convenience function to copy IPv6 addresses without garbage. */ |
324 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION) |
325 | | COAP_STATIC_INLINE void |
326 | | coap_address_copy(coap_address_t *dst, const coap_address_t *src) { |
327 | | memcpy(dst, src, sizeof(coap_address_t)); |
328 | | } |
329 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI */ |
330 | | void coap_address_copy(coap_address_t *dst, const coap_address_t *src); |
331 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
332 | | |
333 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION) |
334 | | /** |
335 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
336 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
337 | | * @c NULL; |
338 | | */ |
339 | | COAP_STATIC_INLINE int |
340 | | coap_address_equals(const coap_address_t *a, const coap_address_t *b) { |
341 | | if (a && b) |
342 | | return _coap_address_equals_impl(a, b); |
343 | | return 0; |
344 | | } |
345 | | #endif |
346 | | |
347 | | /** |
348 | | * Checks if given address object @p a denotes the wildcard address. This |
349 | | * function returns @c 1 if this is the case, @c 0 otherwise. The parameters @p |
350 | | * a must not be @c NULL; |
351 | | */ |
352 | | COAP_STATIC_INLINE int |
353 | 0 | coap_address_isany(const coap_address_t *a) { |
354 | 0 | if (a) |
355 | 0 | return _coap_address_isany_impl(a); |
356 | 0 | return 0; |
357 | 0 | } Unexecuted instantiation: persist_target.c:coap_address_isany Unexecuted instantiation: coap_address.c:coap_address_isany Unexecuted instantiation: coap_debug.c:coap_address_isany Unexecuted instantiation: coap_encode.c:coap_address_isany Unexecuted instantiation: coap_mem.c:coap_address_isany Unexecuted instantiation: coap_net.c:coap_address_isany Unexecuted instantiation: coap_netif.c:coap_address_isany Unexecuted instantiation: coap_openssl.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_str.c:coap_address_isany Unexecuted instantiation: coap_strm_posix.c:coap_address_isany Unexecuted instantiation: coap_subscribe.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_async.c:coap_address_isany Unexecuted instantiation: coap_block.c:coap_address_isany Unexecuted instantiation: coap_cache.c:coap_address_isany Unexecuted instantiation: coap_dgrm_posix.c:coap_address_isany Unexecuted instantiation: coap_dtls.c:coap_address_isany Unexecuted instantiation: coap_io.c:coap_address_isany Unexecuted instantiation: coap_io_posix.c:coap_address_isany Unexecuted instantiation: coap_layers.c:coap_address_isany |
358 | | |
359 | | #if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) |
360 | | |
361 | | /** |
362 | | * Checks if given address @p a denotes a multicast address. This function |
363 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
364 | | */ |
365 | | int coap_is_mcast(const coap_address_t *a); |
366 | | |
367 | | /** |
368 | | * Checks if given address @p a denotes a broadcast address. This function |
369 | | * returns @c 1 if @p a is broadcast, @c 0 otherwise. |
370 | | */ |
371 | | int coap_is_bcast(const coap_address_t *a); |
372 | | |
373 | | /** |
374 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
375 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
376 | | */ |
377 | | int coap_is_af_unix(const coap_address_t *a); |
378 | | |
379 | | /** |
380 | | * Checks if given address @p a denotes an AF_LLC address. This function |
381 | | * returns @c 1 if @p a is of type AF_LLC, @c 0 otherwise. |
382 | | */ |
383 | | int coap_is_af_llc(const coap_address_t *a); |
384 | | |
385 | | #else /* WITH_CONTIKI || RIOT_VERSION */ |
386 | | |
387 | | /** |
388 | | * Checks if given address @p a denotes a multicast address. This function |
389 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
390 | | */ |
391 | | COAP_STATIC_INLINE int |
392 | | coap_is_mcast(const coap_address_t *a) { |
393 | | return a && _coap_is_mcast_impl(a); |
394 | | } |
395 | | |
396 | | /** |
397 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
398 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
399 | | */ |
400 | | COAP_STATIC_INLINE int |
401 | | coap_is_af_unix(const coap_address_t *a) { |
402 | | (void)a; |
403 | | return 0; |
404 | | } |
405 | | |
406 | | #endif /* WITH_CONTIKI || RIOT_VERSION */ |
407 | | |
408 | | #ifdef __cplusplus |
409 | | } |
410 | | #endif |
411 | | |
412 | | #endif /* COAP_ADDRESS_H_ */ |