Coverage Report

Created: 2025-12-20 06:55

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