/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-2024 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 | | #define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr) |
60 | | |
61 | | #elif defined(WITH_CONTIKI) |
62 | | |
63 | | #include "uip.h" |
64 | | |
65 | | struct coap_address_t { |
66 | | uip_ipaddr_t addr; |
67 | | uint16_t port; |
68 | | }; |
69 | | |
70 | | /** |
71 | | * Returns the port from @p addr in host byte order. |
72 | | */ |
73 | | COAP_STATIC_INLINE uint16_t |
74 | | coap_address_get_port(const coap_address_t *addr) { |
75 | | return uip_ntohs(addr->port); |
76 | | } |
77 | | |
78 | | /** |
79 | | * Sets the port field of @p addr to @p port (in host byte order). |
80 | | */ |
81 | | COAP_STATIC_INLINE void |
82 | | coap_address_set_port(coap_address_t *addr, uint16_t port) { |
83 | | addr->port = uip_htons(port); |
84 | | } |
85 | | |
86 | | #define _coap_address_equals_impl(A,B) \ |
87 | | ((A)->port == (B)->port && \ |
88 | | uip_ipaddr_cmp(&((A)->addr),&((B)->addr))) |
89 | | |
90 | | /** @todo implementation of _coap_address_isany_impl() for Contiki */ |
91 | | #define _coap_address_isany_impl(A) 0 |
92 | | |
93 | | #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr)) |
94 | | |
95 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI */ |
96 | | |
97 | | #ifdef _WIN32 |
98 | | #define sa_family_t short |
99 | | #endif /* _WIN32 */ |
100 | | |
101 | 0 | #define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t)) |
102 | | |
103 | | struct coap_sockaddr_un { |
104 | | sa_family_t sun_family; /* AF_UNIX */ |
105 | | char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */ |
106 | | }; |
107 | | |
108 | | /** Multi-purpose address abstraction */ |
109 | | struct coap_address_t { |
110 | | socklen_t size; /**< size of addr */ |
111 | | union { |
112 | | struct sockaddr sa; |
113 | | struct sockaddr_in sin; |
114 | | struct sockaddr_in6 sin6; |
115 | | struct coap_sockaddr_un cun; /* CoAP shortened special */ |
116 | | } addr; |
117 | | }; |
118 | | |
119 | | /** |
120 | | * Returns the port from @p addr in host byte order. |
121 | | */ |
122 | | uint16_t coap_address_get_port(const coap_address_t *addr); |
123 | | |
124 | | /** |
125 | | * Set the port field of @p addr to @p port (in host byte order). |
126 | | */ |
127 | | void coap_address_set_port(coap_address_t *addr, uint16_t port); |
128 | | |
129 | | /** |
130 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
131 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
132 | | * @c NULL; |
133 | | */ |
134 | | int coap_address_equals(const coap_address_t *a, const coap_address_t *b); |
135 | | |
136 | | int _coap_address_isany_impl(const coap_address_t *a); |
137 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
138 | | |
139 | | /** Resolved addresses information */ |
140 | | typedef struct coap_addr_info_t { |
141 | | struct coap_addr_info_t *next; /**< Next entry in the chain */ |
142 | | coap_uri_scheme_t scheme; /**< CoAP scheme to use */ |
143 | | coap_proto_t proto; /**< CoAP protocol to use */ |
144 | | coap_address_t addr; /**< The address to connect / bind to */ |
145 | | } coap_addr_info_t; |
146 | | |
147 | | /** |
148 | | * Determine and set up scheme_hint_bits for a server that can be used in |
149 | | * a call to coap_resolve_address_info(). |
150 | | * |
151 | | * @param have_pki_psk Set to @c 1 if PSK/PKI information is known else @c 0. |
152 | | * @param ws_check Set to @c 1 is WebSockets is to be included in the list |
153 | | * else @c 0. |
154 | | * @param use_unix_proto Set to the appropriate protocol to use for Unix |
155 | | * sockets, else set to COAP_PROTO_NONE for INET / INET6 |
156 | | * sockets. |
157 | | * @return A bit mask of the available CoAP protocols (can be @c 0 if none) |
158 | | * suitable for passing to coap_resolve_address_info(). |
159 | | */ |
160 | | uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, |
161 | | coap_proto_t use_unix_proto); |
162 | | |
163 | | /** |
164 | | * coap_resolve_type_t values |
165 | | */ |
166 | | typedef enum coap_resolve_type_t { |
167 | | COAP_RESOLVE_TYPE_LOCAL, /**< local side of session */ |
168 | | COAP_RESOLVE_TYPE_REMOTE, /**< remote side of session */ |
169 | | } coap_resolve_type_t; |
170 | | |
171 | | /** |
172 | | * Resolve the specified @p address into a set of coap_address_t that can |
173 | | * be used to bind() (local) or connect() (remote) to. |
174 | | * |
175 | | * @param address The Address to resolve. |
176 | | * @param port The unsecured protocol port to use. |
177 | | * @param secure_port The secured protocol port to use. |
178 | | * @param ws_port The unsecured WebSockets port to use. |
179 | | * @param ws_secure_port The secured WebSockets port to use. |
180 | | * @param ai_hints_flags AI_* Hint flags to use for internal getaddrinfo(). |
181 | | * @param scheme_hint_bits Which schemes to return information for. One or |
182 | | * more of COAP_URI_SCHEME_*_BIT or'd together. |
183 | | * @param type COAP_ADDRESS_TYPE_LOCAL or COAP_ADDRESS_TYPE_REMOTE |
184 | | * |
185 | | * @return One or more linked sets of coap_addr_info_t or @c NULL if error. |
186 | | */ |
187 | | coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *address, |
188 | | uint16_t port, |
189 | | uint16_t secure_port, |
190 | | uint16_t ws_port, |
191 | | uint16_t ws_secure_port, |
192 | | int ai_hints_flags, |
193 | | int scheme_hint_bits, |
194 | | coap_resolve_type_t type); |
195 | | |
196 | | /** |
197 | | * Free off the one or more linked sets of coap_addr_info_t returned from |
198 | | * coap_resolve_address_info(). |
199 | | * |
200 | | * @param info_list The set of coap_addr_info_t to free off. |
201 | | */ |
202 | | void coap_free_address_info(coap_addr_info_t *info_list); |
203 | | |
204 | | /** |
205 | | * Resets the given coap_address_t object @p addr to its default values. In |
206 | | * particular, the member size must be initialized to the available size for |
207 | | * storing addresses. |
208 | | * |
209 | | * @param addr The coap_address_t object to initialize. |
210 | | */ |
211 | | void coap_address_init(coap_address_t *addr); |
212 | | |
213 | | /** |
214 | | * Copy the parsed unix domain host into coap_address_t structure |
215 | | * translating %2F into / on the way. All other fields set as appropriate. |
216 | | * |
217 | | * @param addr coap_address_t to update. |
218 | | * @param host The parsed host from the CoAP URI with potential %2F encoding. |
219 | | * @param host_len The length of the parsed host from the CoAP URI with |
220 | | * potential %2F encoding. |
221 | | * |
222 | | * @return @c 1 success, @c 0 failure. |
223 | | */ |
224 | | int coap_address_set_unix_domain(coap_address_t *addr, |
225 | | const uint8_t *host, size_t host_len); |
226 | | |
227 | | /* Convenience function to copy IPv6 addresses without garbage. */ |
228 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) |
229 | | COAP_STATIC_INLINE void |
230 | | coap_address_copy(coap_address_t *dst, const coap_address_t *src) { |
231 | | memcpy(dst, src, sizeof(coap_address_t)); |
232 | | } |
233 | | #else /* ! WITH_LWIP && ! WITH_CONTIKI */ |
234 | | void coap_address_copy(coap_address_t *dst, const coap_address_t *src); |
235 | | #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ |
236 | | |
237 | | #if defined(WITH_LWIP) || defined(WITH_CONTIKI) |
238 | | /** |
239 | | * Compares given address objects @p a and @p b. This function returns @c 1 if |
240 | | * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be |
241 | | * @c NULL; |
242 | | */ |
243 | | COAP_STATIC_INLINE int |
244 | | coap_address_equals(const coap_address_t *a, const coap_address_t *b) { |
245 | | assert(a); |
246 | | assert(b); |
247 | | return _coap_address_equals_impl(a, b); |
248 | | } |
249 | | #endif |
250 | | |
251 | | /** |
252 | | * Checks if given address object @p a denotes the wildcard address. This |
253 | | * function returns @c 1 if this is the case, @c 0 otherwise. The parameters @p |
254 | | * a must not be @c NULL; |
255 | | */ |
256 | | COAP_STATIC_INLINE int |
257 | 0 | coap_address_isany(const coap_address_t *a) { |
258 | 0 | assert(a); |
259 | 0 | return _coap_address_isany_impl(a); |
260 | 0 | } Unexecuted instantiation: coap_debug.c:coap_address_isany Unexecuted instantiation: coap_encode.c:coap_address_isany Unexecuted instantiation: coap_notls.c:coap_address_isany Unexecuted instantiation: coap_option.c:coap_address_isany Unexecuted instantiation: coap_pdu.c:coap_address_isany Unexecuted instantiation: coap_prng.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_tcp.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: coap_address.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 Unexecuted instantiation: coap_net.c:coap_address_isany Unexecuted instantiation: coap_netif.c:coap_address_isany Unexecuted instantiation: coap_oscore.c:coap_address_isany Unexecuted instantiation: coap_resource.c:coap_address_isany Unexecuted instantiation: coap_subscribe.c:coap_address_isany Unexecuted instantiation: coap_threadsafe.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 |
261 | | |
262 | | #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) |
263 | | |
264 | | /** |
265 | | * Checks if given address @p a denotes a multicast address. This function |
266 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
267 | | */ |
268 | | int coap_is_mcast(const coap_address_t *a); |
269 | | |
270 | | /** |
271 | | * Checks if given address @p a denotes a broadcast address. This function |
272 | | * returns @c 1 if @p a is broadcast, @c 0 otherwise. |
273 | | */ |
274 | | int coap_is_bcast(const coap_address_t *a); |
275 | | |
276 | | /** |
277 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
278 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
279 | | */ |
280 | | int coap_is_af_unix(const coap_address_t *a); |
281 | | |
282 | | #else /* WITH_LWIP || WITH_CONTIKI */ |
283 | | |
284 | | /** |
285 | | * Checks if given address @p a denotes a multicast address. This function |
286 | | * returns @c 1 if @p a is multicast, @c 0 otherwise. |
287 | | */ |
288 | | COAP_STATIC_INLINE int |
289 | | coap_is_mcast(const coap_address_t *a) { |
290 | | return a && _coap_is_mcast_impl(a); |
291 | | } |
292 | | |
293 | | /** |
294 | | * Checks if given address @p a denotes a AF_UNIX address. This function |
295 | | * returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise. |
296 | | */ |
297 | | COAP_STATIC_INLINE int |
298 | | coap_is_af_unix(const coap_address_t *a) { |
299 | | (void)a; |
300 | | return 0; |
301 | | } |
302 | | |
303 | | #endif /* WITH_LWIP || WITH_CONTIKI */ |
304 | | |
305 | | #endif /* COAP_ADDRESS_H_ */ |