libcoap 4.3.5-develop-93694e6
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
16
17#ifndef COAP_ADDRESS_H_
18#define COAP_ADDRESS_H_
19
20#include "coap3/coap_pdu.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#if defined(WITH_LWIP)
27
28#include <lwip/ip_addr.h>
29#include <string.h>
30
31struct coap_address_t {
32 uint16_t port;
33 ip_addr_t addr;
34};
35
39COAP_STATIC_INLINE uint16_t
41 return addr->port;
42}
43
48coap_address_set_port(coap_address_t *addr, uint16_t port) {
49 addr->port = port;
50}
51
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
70struct coap_address_t {
71 uip_ipaddr_t addr;
72 uint16_t port;
73};
74
78COAP_STATIC_INLINE uint16_t
80 return uip_ntohs(addr->port);
81}
82
87coap_address_set_port(coap_address_t *addr, uint16_t port) {
88 addr->port = uip_htons(port);
89}
90
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
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
118struct coap_address_t {
119 struct _sock_tl_ep riot;
120};
121
122#define _coap_address_isany_impl(A) (0)
123
124#ifdef SOCK_HAS_IPV6
125#define _coap_address_equals_impl(A, B) \
126 ((A)->riot.family == (B)->riot.family && \
127 (A)->riot.port == (B)->riot.port && \
128 memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \
129 sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0)
130
131#define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff)
132#else /* !SOCK_HAS_IPV6 */
133#define _coap_address_equals_impl(A, B) \
134 ((A)->riot.family == (B)->riot.family && \
135 (A)->riot.port == (B)->riot.port && \
136 memcmp(&(A)->riot, &(B)->riot, sizeof((A)->riot.addr.ipv4)) == 0)
137
138#define _coap_is_mcast_impl(Address) (0)
139#endif /* !SOCK_HAS_IPV6 */
140
144COAP_STATIC_INLINE uint16_t
146 return addr->riot.port;
147}
148
153coap_address_set_port(coap_address_t *addr, uint16_t port) {
154 addr->riot.port = port;
155}
156
162 memset(&addr->riot.addr, 0, sizeof(addr->riot.addr));
163}
164
165#else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */
166
167#if COAP_AF_LLC_SUPPORT
168
169/* Including net/if.h after linux/llc.h results in redefinitions of numerous
170 * symbols.
171 */
172#include <net/if.h>
173#include <linux/llc.h>
174
175/* 00:11:22:33:44:55 */
176#define HW_ADDRSTRLEN 17
177/* llc[00:11:22:33:44:55]:DC */
178#define LLC_HOST_LEN (4 + HW_ADDRSTRLEN + 4)
179
180#endif /* COAP_AF_LLC_SUPPORT */
181
182#ifdef _WIN32
183#define sa_family_t short
184#endif /* _WIN32 */
185
186#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t))
187
189 sa_family_t sun_family; /* AF_UNIX */
190 char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */
191};
192
195 socklen_t size;
196 union {
197 struct sockaddr sa;
198 struct sockaddr_in sin;
199 struct sockaddr_in6 sin6;
200 struct coap_sockaddr_un cun; /* CoAP shortened special */
201#if COAP_AF_LLC_SUPPORT
202 struct sockaddr_llc llc;
203#endif /* COAP_AF_LLC_SUPPORT */
205};
206
210uint16_t coap_address_get_port(const coap_address_t *addr);
211
215void coap_address_set_port(coap_address_t *addr, uint16_t port);
216
221
228
230#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
231
239
253uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
254 coap_proto_t use_proto);
255
263
284 uint16_t port,
285 uint16_t secure_port,
286 uint16_t ws_port,
287 uint16_t ws_secure_port,
288 int ai_hints_flags,
289 int scheme_hint_bits,
291
299
308
321 const uint8_t *host, size_t host_len);
322
333 const uint8_t *host, size_t host_len);
334
335/* Convenience function to copy IPv6 addresses without garbage. */
336#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
339 memcpy(dst, src, sizeof(coap_address_t));
340}
341#else /* ! WITH_LWIP && ! WITH_CONTIKI */
342void coap_address_copy(coap_address_t *dst, const coap_address_t *src);
343#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
344
345#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
353 if (a && b)
354 return _coap_address_equals_impl(a, b);
355 return 0;
356}
357#endif
358
366 if (a)
367 return _coap_address_isany_impl(a);
368 return 0;
369}
370
371#if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
372
377int coap_is_mcast(const coap_address_t *a);
378
383int coap_is_bcast(const coap_address_t *a);
384
389int coap_is_af_unix(const coap_address_t *a);
390
395int coap_is_af_llc(const coap_address_t *a);
396
397#else /* WITH_CONTIKI || RIOT_VERSION */
398
405 return a && _coap_is_mcast_impl(a);
406}
407
414 (void)a;
415 return 0;
416}
417
418#endif /* WITH_CONTIKI || RIOT_VERSION */
419
420#ifdef __cplusplus
421}
422#endif
423
424#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.
COAP_API 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...
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.
uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, coap_proto_t use_proto)
Determine and set up scheme_hint_bits for a server that can be used in a call to coap_resolve_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.
void coap_address_clr_addr(coap_address_t *addr)
Sets the addr field of addr to 0.
int coap_address_set_llc(coap_address_t *addr, const uint8_t *host, size_t host_len)
Copy the parsed LLC host into addr, setting other fields as appropriate.
#define COAP_UNIX_PATH_MAX
int coap_is_af_llc(const coap_address_t *a)
Checks if given address a denotes an AF_LLC address.
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.
#define COAP_API
Pre-defined constants that reflect defaults for CoAP.
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:30
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
#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::@236157306151077000227147123371042320347205022262 addr
char sun_path[COAP_UNIX_PATH_MAX]
sa_family_t sun_family
CoAP string data definition with const data.
Definition coap_str.h:47