libcoap 4.3.5-develop-2ddf01b
Loading...
Searching...
No Matches
coap_address.h
Go to the documentation of this file.
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
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
29extern "C" {
30#endif
31
32#if defined(WITH_LWIP)
33
34#include <lwip/ip_addr.h>
35
36struct coap_address_t {
37 uint16_t port;
38 ip_addr_t addr;
39};
40
44COAP_STATIC_INLINE uint16_t
46 return addr->port;
47}
48
53coap_address_set_port(coap_address_t *addr, uint16_t port) {
54 addr->port = port;
55}
56
62 memset(&addr->addr, 0, sizeof(addr->addr));
63}
64
65#define _coap_address_equals_impl(A, B) \
66 ((A)->port == (B)->port && \
67 (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
68
69#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
70
71#elif defined(WITH_CONTIKI)
72
73#include "uip.h"
74
75struct coap_address_t {
76 uip_ipaddr_t addr;
77 uint16_t port;
78};
79
83COAP_STATIC_INLINE uint16_t
85 return uip_ntohs(addr->port);
86}
87
92coap_address_set_port(coap_address_t *addr, uint16_t port) {
93 addr->port = uip_htons(port);
94}
95
101 memset(&addr->addr, 0, sizeof(addr->addr));
102}
103
104#define _coap_address_equals_impl(A,B) \
105 ((A)->port == (B)->port && \
106 uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
107
109#define _coap_address_isany_impl(A) 0
110
111#define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
112
113#elif defined(RIOT_VERSION)
114
115#include "net/sock.h"
116#include "net/af.h"
117
118#ifndef INET6_ADDRSTRLEN
119#define INET6_ADDRSTRLEN IPV6_ADDR_MAX_STR_LEN
120#endif /* !INET6_ADDRSTRLEN */
121
122struct coap_address_t {
123 struct _sock_tl_ep riot;
124};
125
126#define _coap_address_isany_impl(A) 0
127
128#define _coap_address_equals_impl(A, B) \
129 ((A)->riot.family == (B)->riot.family && \
130 (A)->riot.port == (B)->riot.port && \
131 memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \
132 sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0)
133
134#define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff)
135
139COAP_STATIC_INLINE uint16_t
141 return addr->riot.port;
142}
143
148coap_address_set_port(coap_address_t *addr, uint16_t port) {
149 addr->riot.port = port;
150}
151
157 memset(&addr->riot.addr, 0, sizeof(addr->riot.addr));
158}
159
160#else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */
161
162#ifdef _WIN32
163#define sa_family_t short
164#endif /* _WIN32 */
165
166#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t))
167
169 sa_family_t sun_family; /* AF_UNIX */
170 char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */
171};
172
175 socklen_t size;
176 union {
177 struct sockaddr sa;
178 struct sockaddr_in sin;
179 struct sockaddr_in6 sin6;
180 struct coap_sockaddr_un cun; /* CoAP shortened special */
182};
183
187uint16_t coap_address_get_port(const coap_address_t *addr);
188
192void coap_address_set_port(coap_address_t *addr, uint16_t port);
193
198
205
207#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
208
216
230uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
231 coap_proto_t use_unix_proto);
232
240
258 uint16_t port,
259 uint16_t secure_port,
260 uint16_t ws_port,
261 uint16_t ws_secure_port,
262 int ai_hints_flags,
263 int scheme_hint_bits,
265
273
282
295 const uint8_t *host, size_t host_len);
296
297/* Convenience function to copy IPv6 addresses without garbage. */
298#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
301 memcpy(dst, src, sizeof(coap_address_t));
302}
303#else /* ! WITH_LWIP && ! WITH_CONTIKI */
304void coap_address_copy(coap_address_t *dst, const coap_address_t *src);
305#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
306
307#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
315 assert(a);
316 assert(b);
317 return _coap_address_equals_impl(a, b);
318}
319#endif
320
328 assert(a);
329 return _coap_address_isany_impl(a);
330}
331
332#if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
333
338int coap_is_mcast(const coap_address_t *a);
339
344int coap_is_bcast(const coap_address_t *a);
345
350int coap_is_af_unix(const coap_address_t *a);
351
352#else /* WITH_CONTIKI || RIOT_VERSION */
353
360 return a && _coap_is_mcast_impl(a);
361}
362
369 (void)a;
370 return 0;
371}
372
373#endif /* WITH_CONTIKI || RIOT_VERSION */
374
375#ifdef __cplusplus
376}
377#endif
378
379#endif /* COAP_ADDRESS_H_ */
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t *host, size_t host_len)
Copy the parsed unix domain host into coap_address_t structure translating %2F into / on the way.
void coap_free_address_info(coap_addr_info_t *info_list)
Free off the one or more linked sets of coap_addr_info_t returned from coap_resolve_address_info().
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
int coap_is_bcast(const coap_address_t *a)
Checks if given address a denotes a broadcast address.
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
coap_resolve_type_t
coap_resolve_type_t values
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
int _coap_address_isany_impl(const coap_address_t *a)
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, coap_proto_t use_unix_proto)
Determine and set up scheme_hint_bits for a server that can be used in a call to coap_resolve_address...
void coap_address_clr_addr(coap_address_t *addr)
Sets the addr field of addr to 0.
coap_addr_info_t * coap_resolve_address_info(const coap_str_const_t *address, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits, coap_resolve_type_t type)
Resolve the specified address into a set of coap_address_t that can be used to bind() (local) or conn...
#define COAP_UNIX_PATH_MAX
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Pre-defined constants that reflect defaults for CoAP.
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:32
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:318
Platform specific header file for CoAP stack.
#define COAP_STATIC_INLINE
Definition libcoap.h:57
Resolved addresses information.
coap_uri_scheme_t scheme
CoAP scheme to use.
coap_proto_t proto
CoAP protocol to use.
struct coap_addr_info_t * next
Next entry in the chain.
coap_address_t addr
The address to connect / bind to.
Multi-purpose address abstraction.
socklen_t size
size of addr
struct sockaddr_in sin
struct coap_sockaddr_un cun
struct sockaddr_in6 sin6
struct sockaddr sa
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
sa_family_t sun_family
CoAP string data definition with const data.
Definition coap_str.h:49