18#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
20#ifdef HAVE_ARPA_INET_H
23#ifdef HAVE_NETINET_IN_H
24#include <netinet/in.h>
26#ifdef HAVE_SYS_SOCKET_H
27#include <sys/socket.h>
40#define IN_MULTICAST(a) ((((long int) (a)) & 0xf0000000) == 0xe0000000)
42#ifndef IN6_IS_ADDR_MULTICAST
43#define IN6_IS_ADDR_MULTICAST(a) ((a)->s6_addr[0] == 0xff)
45#ifndef IN6_IS_ADDR_V4MAPPED
46#define IN6_IS_ADDR_V4MAPPED(a) \
47 ((((a)->s6_addr32[0]) == 0) && (((a)->s6_addr32[1]) == 0) && \
48 (((a)->s6_addr32[2]) == htonl(0xffff)))
54#define IN_MULTICAST(Address) (0)
60 switch (addr->
addr.
sa.sa_family) {
63 return ntohs(addr->
addr.
sin.sin_port);
67 return ntohs(addr->
addr.
sin6.sin6_port);
78 switch (addr->
addr.
sa.sa_family) {
81 addr->
addr.
sin.sin_port = htons(port);
86 addr->
addr.
sin6.sin6_port = htons(port);
103 switch (a->
addr.
sa.sa_family) {
108 sizeof(
struct in_addr)) == 0;
114 sizeof(
struct in6_addr)) == 0;
116#if COAP_AF_UNIX_SUPPORT
129#if COAP_AF_UNIX_SUPPORT
130 return a->
addr.
sa.sa_family == AF_UNIX;
146 switch (a->
addr.
sa.sa_family) {
149 return IN_MULTICAST(ntohl(a->
addr.
sin.sin_addr.s_addr));
154 return IN6_IS_ADDR_MULTICAST(&a->
addr.
sin6.sin6_addr) ||
155 (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr) &&
156 IN_MULTICAST(ntohl(a->
addr.
sin6.sin6_addr.s6_addr[12])));
158 return a->
addr.
sin6.sin6_addr.s6_addr[0] == 0xff;
168#define COAP_BCST_CNT 15
172#ifndef COAP_BCST_REFRESH_SECS
173#define COAP_BCST_REFRESH_SECS 30
176#if (COAP_IPV4_SUPPORT && defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)) || defined(_WIN32)
177static int bcst_cnt = -1;
186#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)
195 switch (a->
addr.
sa.sa_family) {
198 ipv4.s_addr = a->
addr.
sin.sin_addr.s_addr;
204 if (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr)) {
205 memcpy(&ipv4, &a->
addr.
sin6.sin6_addr.s6_addr[12],
sizeof(ipv4));
216#ifndef INADDR_BROADCAST
217#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
219 if (ipv4.s_addr == INADDR_BROADCAST)
222#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__) && !defined(_WIN32)
224 if (bcst_cnt == -1 ||
227 struct ifaddrs *ifa = NULL;
230 if (getifaddrs(&ifa) != 0) {
231 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
238 if (ife->ifa_addr && ife->ifa_addr->sa_family == AF_INET &&
239 ife->ifa_flags & IFF_BROADCAST) {
240 struct in_addr netmask;
246 netmask.s_addr = ((
struct sockaddr_in *)ife->ifa_netmask)->sin_addr.s_addr;
247 if (netmask.s_addr != 0xffffffff) {
248 b_ipv4[bcst_cnt].s_addr = ((
struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr |
256 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
260 for (i = 0; i < bcst_cnt; i++) {
261 if (ipv4.s_addr == b_ipv4[i].s_addr)
269#if !defined(__MINGW32__)
270#pragma comment(lib, "iphlpapi.lib")
277 if (bcst_cnt == -1 ||
282 PMIB_IPADDRTABLE pIPAddrTable;
291 if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
296 if (pIPAddrTable == NULL) {
297 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
302 if ((dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0)) != NO_ERROR) {
303 coap_log_warn(
"GetIpAddrTable failed with error %d\n", dwRetVal);
310 for (i = 0; i < (int)pIPAddrTable->dwNumEntries && bcst_cnt <
COAP_BCST_CNT; i++) {
311 struct in_addr netmask;
314 netmask.s_addr = (u_long)pIPAddrTable->table[i].dwMask;
315 if (netmask.s_addr != 0xffffffff) {
316 b_ipv4[bcst_cnt].s_addr = (u_long)pIPAddrTable->table[i].dwAddr |
321 if (i != (
int)pIPAddrTable->dwNumEntries) {
322 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
331 for (i = 0; i < bcst_cnt; i++) {
332 if (ipv4.s_addr == b_ipv4[i].s_addr)
347#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
355 const uint8_t *host,
size_t host_len) {
356#if COAP_AF_UNIX_SUPPORT
362 for (i = 0; i < host_len; i++) {
363 if ((host_len - i) >= 3 && host[i] ==
'%' && host[i+1] ==
'2' &&
364 (host[i+2] ==
'F' || host[i+2] ==
'f')) {
390 if (port == 0 && update_port0)
404 uint32_t scheme_hint_bits = 0;
410 scheme_hint_bits |= 1 << scheme;
415 scheme_hint_bits |= 1 << scheme;
420 scheme_hint_bits |= 1 << scheme;
425 scheme_hint_bits |= 1 << scheme;
430 scheme_hint_bits |= 1 << scheme;
435 scheme_hint_bits |= 1 << scheme;
445 switch (use_unix_proto) {
470 return scheme_hint_bits;
534 uint16_t port, uint16_t secure_port, uint16_t ws_port,
535 uint16_t ws_secure_port,
579 uint16_t secure_port,
581 uint16_t ws_secure_port,
583 int scheme_hint_bits,
585#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
587 struct addrinfo *res, *ainfo;
588 struct addrinfo hints;
589 static char addrstr[256];
597#if COAP_AF_UNIX_SUPPORT
606 if (scheme_hint_bits & (1 << scheme)) {
627 memset(addrstr, 0,
sizeof(addrstr));
628 if (address && address->
length)
629 memcpy(addrstr, address->
s, address->
length);
631 memcpy(addrstr,
"localhost", 9);
633 memset((
char *)&hints, 0,
sizeof(hints));
634 hints.ai_socktype = 0;
635 hints.ai_family = AF_UNSPEC;
636 hints.ai_flags = ai_hints_flags;
638 error = getaddrinfo(addrstr, NULL, &hints, &res);
641 coap_log_warn(
"getaddrinfo: %s: %s\n", addrstr, gai_strerror(error));
645 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
646#if !defined(WITH_LWIP)
647 if (ainfo->ai_addrlen > (socklen_t)
sizeof(info->
addr.
addr))
651 switch (ainfo->ai_family) {
659 if (scheme_hint_bits & (1 << scheme)) {
665#if !defined(WITH_LWIP)
666 info->
addr.
size = (socklen_t)ainfo->ai_addrlen;
667 memcpy(&info->
addr.
addr, ainfo->ai_addr, ainfo->ai_addrlen);
669 memset(&info->
addr, 0,
sizeof(info->
addr));
670 switch (ainfo->ai_family) {
672 struct sockaddr_in6 *sock6;
675 struct sockaddr_in *sock4;
677 sock4 = (
struct sockaddr_in *)ainfo->ai_addr;
678 info->
addr.port = ntohs(sock4->sin_port);
679 memcpy(&info->
addr.
addr, &sock4->sin_addr, 4);
681 info->
addr.
addr.type = IPADDR_TYPE_V4;
687 sock6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
688 info->
addr.port = ntohs(sock6->sin6_port);
689 memcpy(&info->
addr.
addr, &sock6->sin6_addr, 16);
690#if LWIP_IPV6 && LWIP_IPV4
691 info->
addr.
addr.type = IPADDR_TYPE_V6;
700 ws_secure_port, type);
703 info_tmp = info_list;
710 info_tmp = info_tmp->
next;
722 info_prev->
next = info;
737#elif defined(RIOT_VERSION)
739#include "net/utils.h"
741 ipv6_addr_t addr_ipv6;
744 ipv4_addr_t addr_ipv4;
746 netif_t *netif = NULL;
751 (void)ai_hints_flags;
752 int family = AF_UNSPEC;
754 if (address == NULL || address->
length == 0) {
755 memset(&addr_ipv6, 0,
sizeof(addr_ipv6));
763 if (netutils_get_ipv6(&addr_ipv6, &netif, (
const char *)address->
s) >= 0) {
768 if (family == AF_UNSPEC &&
769 netutils_get_ipv4(&addr_ipv4, (
const char *)address->
s) >= 0) {
773 if (family == AF_UNSPEC) {
774 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
779 if (scheme_hint_bits & (1 << scheme)) {
790 info_prev->
next = info;
797 info->
addr.riot.family = AF_INET6;
798 memcpy(&info->
addr.riot.
addr.ipv6, &addr_ipv6,
800 info->
addr.riot.netif = netif ? (uint32_t)netif_get_id(netif) : 0;
805 info->
addr.riot.family = AF_INET;
806 memcpy(&info->
addr.riot.
addr.ipv4, &addr_ipv4,
815 ws_secure_port, type);
820#elif defined(WITH_CONTIKI)
822#include <os/net/ipv6/uiplib.h>
823 uip_ipaddr_t addr_ip;
830 (void)ai_hints_flags;
832 if (address == NULL || address->
length == 0) {
833 memset(&addr_ip, 0,
sizeof(addr_ip));
836 if (uiplib_ip6addrconv((
const char *)address->
s, (uip_ip6addr_t *)&addr_ip) > 0) {
841 if (family == AF_UNSPEC &&
842 uiplib_ip4addrconv((
const char *)address->
s, (uip_ip4addr_t *)&addr_ip) > 0) {
847 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
852 if (scheme_hint_bits & (1 << scheme)) {
863 info_prev->
next = info;
870 ws_secure_port, type);
875#bad OS type not supported
890#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
893#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
899 if (src->
addr.
sa.sa_family == AF_INET6) {
906#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
910 if (src->
addr.
sa.sa_family == AF_INET) {
923 switch (a->
addr.
sa.sa_family) {
926 return a->
addr.
sin.sin_addr.s_addr == INADDR_ANY;
930 return memcmp(&in6addr_any,
932 sizeof(in6addr_any)) == 0;
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.
#define COAP_BCST_REFRESH_SECS
void coap_free_address_info(coap_addr_info_t *info)
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.
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...
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_copy(coap_address_t *dst, const coap_address_t *src)
static void update_coap_addr_port(coap_uri_scheme_t scheme, coap_addr_info_t *info, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, coap_resolve_type_t type)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void update_port(coap_address_t *addr, uint16_t port, uint16_t default_port, int update_port0)
static coap_addr_info_t * get_coap_addr_info(coap_uri_scheme_t scheme)
coap_resolve_type_t
coap_resolve_type_t values
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
#define COAP_UNIX_PATH_MAX
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
int coap_host_is_unix_domain(const coap_str_const_t *host)
Determines from the host whether this is a Unix Domain socket request.
coap_uri_scheme_t
The scheme specifiers.
@ COAP_URI_SCHEME_COAPS_WS
@ COAP_URI_SCHEME_COAPS_TCP
@ COAP_URI_SCHEME_COAP_TCP
@ COAP_URI_SCHEME_COAP_WS
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
#define coap_log_warn(...)
#define coap_log_err(...)
#define COAP_DEFAULT_PORT
coap_proto_t
CoAP protocol types.
#define COAPS_DEFAULT_PORT
int coap_tcp_is_supported(void)
Check whether TCP is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_ws_is_supported(void)
Check whether WebSockets is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
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 coap_sockaddr_un cun
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string