Loading...
Searching...
No Matches
coap_address.h
Go to the documentation of this file.
/*
* coap_address.h -- representation of network addresses
*
* Copyright (C) 2010-2011,2015-2016,2022-2024 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_ADDRESS_H_
#define COAP_ADDRESS_H_
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "libcoap.h"
#include "coap3/coap_pdu.h"
#if defined(WITH_LWIP)
#include <lwip/ip_addr.h>
struct coap_address_t {
uint16_t port;
ip_addr_t addr;
};
COAP_STATIC_INLINE uint16_t
coap_address_get_port(const coap_address_t *addr) {
return addr->port;
}
COAP_STATIC_INLINE void
coap_address_set_port(coap_address_t *addr, uint16_t port) {
addr->port = port;
}
#define _coap_address_equals_impl(A, B) \
((A)->port == (B)->port && \
(!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
#elif defined(WITH_CONTIKI)
#include "uip.h"
struct coap_address_t {
uip_ipaddr_t addr;
uint16_t port;
};
COAP_STATIC_INLINE uint16_t
coap_address_get_port(const coap_address_t *addr) {
return uip_ntohs(addr->port);
}
COAP_STATIC_INLINE void
coap_address_set_port(coap_address_t *addr, uint16_t port) {
addr->port = uip_htons(port);
}
#define _coap_address_equals_impl(A,B) \
((A)->port == (B)->port && \
uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
#define _coap_address_isany_impl(A) 0
#define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
#elif defined(RIOT_VERSION)
#include "net/sock.h"
#include "net/af.h"
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN IPV6_ADDR_MAX_STR_LEN
#endif /* !INET6_ADDRSTRLEN */
struct coap_address_t {
struct _sock_tl_ep riot;
};
#define _coap_address_isany_impl(A) 0
#define _coap_address_equals_impl(A, B) \
((A)->riot.family == (B)->riot.family && \
(A)->riot.port == (B)->riot.port && \
memcmp(&(A)->riot, &(B)->riot, (A)->riot.family == AF_INET6 ? \
sizeof((A)->riot.addr.ipv6) : sizeof((A)->riot.addr.ipv4)) == 0)
#define _coap_is_mcast_impl(Address) ((Address)->riot.addr.ipv6[0] == 0xff)
COAP_STATIC_INLINE uint16_t
coap_address_get_port(const coap_address_t *addr) {
return addr->riot.port;
}
COAP_STATIC_INLINE void
coap_address_set_port(coap_address_t *addr, uint16_t port) {
addr->riot.port = port;
}
#else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */
#ifdef _WIN32
#define sa_family_t short
#endif /* _WIN32 */
#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t))
struct coap_sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */
};
struct coap_address_t {
socklen_t size;
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct coap_sockaddr_un cun; /* CoAP shortened special */
} addr;
};
uint16_t coap_address_get_port(const coap_address_t *addr);
void coap_address_set_port(coap_address_t *addr, uint16_t port);
int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
int _coap_address_isany_impl(const coap_address_t *a);
#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
typedef struct coap_addr_info_t {
struct coap_addr_info_t *next;
coap_uri_scheme_t scheme;
coap_proto_t proto;
coap_address_t addr;
} coap_addr_info_t;
uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
coap_proto_t use_unix_proto);
typedef enum coap_resolve_type_t {
COAP_RESOLVE_TYPE_LOCAL,
COAP_RESOLVE_TYPE_REMOTE,
} coap_resolve_type_t;
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);
void coap_free_address_info(coap_addr_info_t *info_list);
void coap_address_init(coap_address_t *addr);
int coap_address_set_unix_domain(coap_address_t *addr,
const uint8_t *host, size_t host_len);
/* Convenience function to copy IPv6 addresses without garbage. */
#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
COAP_STATIC_INLINE void
coap_address_copy(coap_address_t *dst, const coap_address_t *src) {
memcpy(dst, src, sizeof(coap_address_t));
}
#else /* ! WITH_LWIP && ! WITH_CONTIKI */
void coap_address_copy(coap_address_t *dst, const coap_address_t *src);
#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
#if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION)
COAP_STATIC_INLINE int
coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
assert(a);
assert(b);
return _coap_address_equals_impl(a, b);
}
#endif
COAP_STATIC_INLINE int
coap_address_isany(const coap_address_t *a) {
assert(a);
return _coap_address_isany_impl(a);
}
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
int coap_is_mcast(const coap_address_t *a);
int coap_is_bcast(const coap_address_t *a);
int coap_is_af_unix(const coap_address_t *a);
#else /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */
COAP_STATIC_INLINE int
coap_is_mcast(const coap_address_t *a) {
return a && _coap_is_mcast_impl(a);
}
COAP_STATIC_INLINE int
coap_is_af_unix(const coap_address_t *a) {
(void)a;
return 0;
}
#endif /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */
#endif /* COAP_ADDRESS_H_ */
coap_address_isany
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
coap_address_set_port
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
coap_address_set_unix_domain
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
coap_free_address_info
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:795
coap_is_af_unix
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
coap_is_bcast
int coap_is_bcast(const coap_address_t *a)
Checks if given address a denotes a broadcast address.
Definition coap_address.c:164
coap_address_init
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
coap_is_mcast
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
coap_resolve_type_t values
Definition coap_address.h:205
COAP_RESOLVE_TYPE_LOCAL
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
Definition coap_address.h:206
COAP_RESOLVE_TYPE_REMOTE
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
Definition coap_address.h:207
_coap_address_isany_impl
int _coap_address_isany_impl(const coap_address_t *a)
Definition coap_address.c:835
coap_address_get_port
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
coap_get_available_scheme_hint_bits
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:311
coap_resolve_address_info
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:486
COAP_UNIX_PATH_MAX
#define COAP_UNIX_PATH_MAX
Definition coap_address.h:140
coap_address_copy
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition coap_address.c:806
coap_address_equals
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
coap_pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_uri_scheme_t
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:28
coap_proto_t
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:312
libcoap.h
Platform specific header file for CoAP stack.
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition libcoap.h:53
coap_addr_info_t
Resolved addresses information.
Definition coap_address.h:179
coap_addr_info_t::scheme
coap_uri_scheme_t scheme
CoAP scheme to use.
Definition coap_address.h:181
coap_addr_info_t::proto
coap_proto_t proto
CoAP protocol to use.
Definition coap_address.h:182
coap_addr_info_t::next
struct coap_addr_info_t * next
Next entry in the chain.
Definition coap_address.h:180
coap_addr_info_t::addr
coap_address_t addr
The address to connect / bind to.
Definition coap_address.h:183
coap_address_t
Multi-purpose address abstraction.
Definition coap_address.h:148
coap_address_t::size
socklen_t size
size of addr
Definition coap_address.h:149
coap_address_t::sin
struct sockaddr_in sin
Definition coap_address.h:152
coap_address_t::cun
struct coap_sockaddr_un cun
Definition coap_address.h:154
coap_address_t::sin6
struct sockaddr_in6 sin6
Definition coap_address.h:153
coap_address_t::sa
struct sockaddr sa
Definition coap_address.h:151
coap_address_t::addr
union coap_address_t::@357057253077315202212363270045173237257316000025 addr
coap_sockaddr_un
Definition coap_address.h:142
coap_sockaddr_un::sun_path
char sun_path[COAP_UNIX_PATH_MAX]
Definition coap_address.h:144
coap_sockaddr_un::sun_family
sa_family_t sun_family
Definition coap_address.h:143
coap_str_const_t
CoAP string data definition with const data.
Definition coap_str.h:46