libcoap 4.3.4-develop-9f1418e
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-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
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
32struct coap_address_t {
33 uint16_t port;
34 ip_addr_t addr;
35};
36
40COAP_STATIC_INLINE uint16_t
42 return addr->port;
43}
44
49coap_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
65struct coap_address_t {
66 uip_ipaddr_t addr;
67 uint16_t port;
68};
69
73COAP_STATIC_INLINE uint16_t
75 return uip_ntohs(addr->port);
76}
77
82coap_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
91#define _coap_address_isany_impl(A) 0
92
93#define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
94
95#elif defined(RIOT_VERSION)
96
97#include "net/sock.h"
98#include "net/af.h"
99
100#ifndef INET6_ADDRSTRLEN
101#define INET6_ADDRSTRLEN IPV6_ADDR_MAX_STR_LEN
102#endif /* !INET6_ADDRSTRLEN */
103
104struct coap_address_t {
105 struct _sock_tl_ep riot;
106};
107
108#define _coap_address_isany_impl(A) 0
109
110#define _coap_address_equals_impl(A, B) \
111 ((A)->riot.family == (B)->riot.family && \
112 (A)->riot.port == (B)->riot.port && \
113 memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \
114 sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0)
115
116#define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff)
117
121COAP_STATIC_INLINE uint16_t
123 return addr->riot.port;
124}
125
130coap_address_set_port(coap_address_t *addr, uint16_t port) {
131 addr->riot.port = port;
132}
133
134#else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */
135
136#ifdef _WIN32
137#define sa_family_t short
138#endif /* _WIN32 */
139
140#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t))
141
143 sa_family_t sun_family; /* AF_UNIX */
144 char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */
145};
146
149 socklen_t size;
150 union {
151 struct sockaddr sa;
152 struct sockaddr_in sin;
153 struct sockaddr_in6 sin6;
154 struct coap_sockaddr_un cun; /* CoAP shortened special */
156};
157
161uint16_t coap_address_get_port(const coap_address_t *addr);
162
166void coap_address_set_port(coap_address_t *addr, uint16_t port);
167
174
176#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
177
179typedef struct coap_addr_info_t {
185
199uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
200 coap_proto_t use_unix_proto);
201
209
227 uint16_t port,
228 uint16_t secure_port,
229 uint16_t ws_port,
230 uint16_t ws_secure_port,
231 int ai_hints_flags,
232 int scheme_hint_bits,
234
242
251
264 const uint8_t *host, size_t host_len);
265
266/* Convenience function to copy IPv6 addresses without garbage. */
267#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
270 memcpy(dst, src, sizeof(coap_address_t));
271}
272#else /* ! WITH_LWIP && ! WITH_CONTIKI */
273void coap_address_copy(coap_address_t *dst, const coap_address_t *src);
274#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
275
276#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
284 assert(a);
285 assert(b);
286 return _coap_address_equals_impl(a, b);
287}
288#endif
289
297 assert(a);
298 return _coap_address_isany_impl(a);
299}
300
301#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
302
307int coap_is_mcast(const coap_address_t *a);
308
313int coap_is_bcast(const coap_address_t *a);
314
319int coap_is_af_unix(const coap_address_t *a);
320
321#else /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */
322
329 return a && _coap_is_mcast_impl(a);
330}
331
338 (void)a;
339 return 0;
340}
341
342#endif /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */
343
344#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.
Definition: coap_address.h:296
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
Definition: coap_address.c:62
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.
Definition: coap_address.c:263
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().
Definition: coap_address.c:732
struct coap_addr_info_t coap_addr_info_t
Resolved addresses information.
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
Definition: coap_address.c:109
int coap_is_bcast(const coap_address_t *a)
Checks if given address a denotes a broadcast address.
Definition: coap_address.c:164
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:253
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: coap_address.c:119
coap_resolve_type_t
coap_resolve_type_t values
Definition: coap_address.h:205
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
Definition: coap_address.h:206
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
Definition: coap_address.h:207
int _coap_address_isany_impl(const coap_address_t *a)
Definition: coap_address.c:772
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:44
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...
Definition: coap_address.c:312
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...
Definition: coap_address.c:487
#define COAP_UNIX_PATH_MAX
Definition: coap_address.h:140
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.c:743
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:81
Pre-defined constants that reflect defaults for CoAP.
coap_uri_scheme_t
The scheme specifiers.
Definition: coap_uri.h:28
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:312
Platform specific header file for CoAP stack.
#define COAP_STATIC_INLINE
Definition: libcoap.h:53
Resolved addresses information.
Definition: coap_address.h:179
coap_uri_scheme_t scheme
CoAP scheme to use.
Definition: coap_address.h:181
coap_proto_t proto
CoAP protocol to use.
Definition: coap_address.h:182
struct coap_addr_info_t * next
Next entry in the chain.
Definition: coap_address.h:180
coap_address_t addr
The address to connect / bind to.
Definition: coap_address.h:183
Multi-purpose address abstraction.
Definition: coap_address.h:148
socklen_t size
size of addr
Definition: coap_address.h:149
struct sockaddr_in sin
Definition: coap_address.h:152
struct coap_sockaddr_un cun
Definition: coap_address.h:154
struct sockaddr_in6 sin6
Definition: coap_address.h:153
struct sockaddr sa
Definition: coap_address.h:151
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
Definition: coap_address.h:144
sa_family_t sun_family
Definition: coap_address.h:143
CoAP string data definition with const data.
Definition: coap_str.h:46