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)
59#if !defined(__MINGW32__)
60#pragma comment(lib, "iphlpapi.lib")
67 switch (addr->
addr.
sa.sa_family) {
70 return ntohs(addr->
addr.
sin.sin_port);
74 return ntohs(addr->
addr.
sin6.sin6_port);
85 switch (addr->
addr.
sa.sa_family) {
88 addr->
addr.
sin.sin_port = htons(port);
93 addr->
addr.
sin6.sin6_port = htons(port);
110 switch (a->
addr.
sa.sa_family) {
115 sizeof(
struct in_addr)) == 0;
121 sizeof(
struct in6_addr)) == 0;
123#if COAP_AF_UNIX_SUPPORT
136#if COAP_AF_UNIX_SUPPORT
137 return a->
addr.
sa.sa_family == AF_UNIX;
153 switch (a->
addr.
sa.sa_family) {
156 return IN_MULTICAST(ntohl(a->
addr.
sin.sin_addr.s_addr));
161 return IN6_IS_ADDR_MULTICAST(&a->
addr.
sin6.sin6_addr) ||
162 (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr) &&
163 IN_MULTICAST(ntohl(a->
addr.
sin6.sin6_addr.s6_addr[12])));
165 return a->
addr.
sin6.sin6_addr.s6_addr[0] == 0xff;
175#define COAP_BCST_CNT 15
179#ifndef COAP_BCST_REFRESH_SECS
180#define COAP_BCST_REFRESH_SECS 30
183#if (COAP_IPV4_SUPPORT && defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)) || defined(_WIN32)
184static int bcst_cnt = -1;
193#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)
202 switch (a->
addr.
sa.sa_family) {
205 ipv4.s_addr = a->
addr.
sin.sin_addr.s_addr;
211 if (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr)) {
212 memcpy(&ipv4, &a->
addr.
sin6.sin6_addr.s6_addr[12],
sizeof(ipv4));
223#ifndef INADDR_BROADCAST
224#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
226 if (ipv4.s_addr == INADDR_BROADCAST)
229#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__) && !defined(_WIN32)
231 if (bcst_cnt == -1 ||
234 struct ifaddrs *ifa = NULL;
237 if (getifaddrs(&ifa) != 0) {
238 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
245 if (ife->ifa_addr && ife->ifa_addr->sa_family == AF_INET &&
246 ife->ifa_flags & IFF_BROADCAST) {
247 struct in_addr netmask;
253 netmask.s_addr = ((
struct sockaddr_in *)ife->ifa_netmask)->sin_addr.s_addr;
254 if (netmask.s_addr != 0xffffffff) {
255 b_ipv4[bcst_cnt].s_addr = ((
struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr |
263 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
267 for (i = 0; i < bcst_cnt; i++) {
268 if (ipv4.s_addr == b_ipv4[i].s_addr)
279 if (bcst_cnt == -1 ||
284 PMIB_IPADDRTABLE pIPAddrTable;
293 if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
298 if (pIPAddrTable == NULL) {
299 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
304 if ((dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0)) != NO_ERROR) {
305 coap_log_warn(
"GetIpAddrTable failed with error %d\n", dwRetVal);
312 for (i = 0; i < (int)pIPAddrTable->dwNumEntries && bcst_cnt <
COAP_BCST_CNT; i++) {
313 struct in_addr netmask;
316 netmask.s_addr = (u_long)pIPAddrTable->table[i].dwMask;
317 if (netmask.s_addr != 0xffffffff) {
318 b_ipv4[bcst_cnt].s_addr = (u_long)pIPAddrTable->table[i].dwAddr |
323 if (i != (
int)pIPAddrTable->dwNumEntries) {
324 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
333 for (i = 0; i < bcst_cnt; i++) {
334 if (ipv4.s_addr == b_ipv4[i].s_addr)
349#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
357 const uint8_t *host,
size_t host_len) {
358#if COAP_AF_UNIX_SUPPORT
364 for (i = 0; i < host_len; i++) {
365 if ((host_len - i) >= 3 && host[i] ==
'%' && host[i+1] ==
'2' &&
366 (host[i+2] ==
'F' || host[i+2] ==
'f')) {
392 if (port == 0 && update_port0)
406 uint32_t scheme_hint_bits = 0;
412 scheme_hint_bits |= 1 << scheme;
417 scheme_hint_bits |= 1 << scheme;
422 scheme_hint_bits |= 1 << scheme;
427 scheme_hint_bits |= 1 << scheme;
432 scheme_hint_bits |= 1 << scheme;
437 scheme_hint_bits |= 1 << scheme;
447 switch (use_unix_proto) {
472 return scheme_hint_bits;
536 uint16_t port, uint16_t secure_port, uint16_t ws_port,
537 uint16_t ws_secure_port,
578#if defined(WITH_LWIP) && !(LWIP_DNS && LWIP_SOCKET && LWIP_COMPAT_SOCKETS)
583 uint16_t secure_port,
585 uint16_t ws_secure_port,
587 int scheme_hint_bits,
595 (void)ai_hints_flags;
597 if (address == NULL || address->
length == 0) {
598 memset(&addr_ip, 0,
sizeof(addr_ip));
600 if (ipaddr_aton((
const char *)address->
s, &addr_ip) <= 0) {
601 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
606 if (scheme_hint_bits & (1 << scheme)) {
617 info_prev->
next = info;
624 ws_secure_port, type);
630#elif !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
635 uint16_t secure_port,
637 uint16_t ws_secure_port,
639 int scheme_hint_bits,
642 struct addrinfo *res, *ainfo;
643 struct addrinfo hints;
644 static char addrstr[256];
652#if COAP_AF_UNIX_SUPPORT
661 if (scheme_hint_bits & (1 << scheme)) {
682 memset(addrstr, 0,
sizeof(addrstr));
683 if (address && address->
length) {
684 if (address->
length >=
sizeof(addrstr)) {
688 memcpy(addrstr, address->
s, address->
length);
690 memcpy(addrstr,
"localhost", 9);
693 memset((
char *)&hints, 0,
sizeof(hints));
694 hints.ai_socktype = 0;
695 hints.ai_family = AF_UNSPEC;
696 hints.ai_flags = ai_hints_flags;
698 error = getaddrinfo(addrstr, NULL, &hints, &res);
701#if defined(WITH_LWIP)
704 coap_log_warn(
"getaddrinfo: %s: %s\n", addrstr, gai_strerror(error));
709 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
710#if !defined(WITH_LWIP)
711 if (ainfo->ai_addrlen > (socklen_t)
sizeof(info->
addr.
addr))
715 switch (ainfo->ai_family) {
723 if (scheme_hint_bits & (1 << scheme)) {
729#if !defined(WITH_LWIP)
730 info->
addr.
size = (socklen_t)ainfo->ai_addrlen;
731 memcpy(&info->
addr.
addr, ainfo->ai_addr, ainfo->ai_addrlen);
733 memset(&info->
addr, 0,
sizeof(info->
addr));
734 switch (ainfo->ai_family) {
736 struct sockaddr_in6 *sock6;
739 struct sockaddr_in *sock4;
741 sock4 = (
struct sockaddr_in *)ainfo->ai_addr;
742 info->
addr.port = ntohs(sock4->sin_port);
743 memcpy(&info->
addr.
addr, &sock4->sin_addr, 4);
745 info->
addr.
addr.type = IPADDR_TYPE_V4;
751 sock6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
752 info->
addr.port = ntohs(sock6->sin6_port);
753 memcpy(&info->
addr.
addr, &sock6->sin6_addr, 16);
754#if LWIP_IPV6 && LWIP_IPV4
755 info->
addr.
addr.type = IPADDR_TYPE_V6;
764 ws_secure_port, type);
767 info_tmp = info_list;
774 info_tmp = info_tmp->
next;
786 info_prev->
next = info;
802#elif defined(RIOT_VERSION)
804#include "net/utils.h"
809 uint16_t secure_port,
811 uint16_t ws_secure_port,
813 int scheme_hint_bits,
816 ipv6_addr_t addr_ipv6;
819 ipv4_addr_t addr_ipv4;
821 netif_t *netif = NULL;
826 (void)ai_hints_flags;
827 int family = AF_UNSPEC;
829 if (address == NULL || address->
length == 0) {
830 memset(&addr_ipv6, 0,
sizeof(addr_ipv6));
838 if (netutils_get_ipv6(&addr_ipv6, &netif, (
const char *)address->
s) >= 0) {
843 if (family == AF_UNSPEC &&
844 netutils_get_ipv4(&addr_ipv4, (
const char *)address->
s) >= 0) {
848 if (family == AF_UNSPEC) {
849 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
854 if (scheme_hint_bits & (1 << scheme)) {
865 info_prev->
next = info;
872 info->
addr.riot.family = AF_INET6;
873 memcpy(&info->
addr.riot.
addr.ipv6, &addr_ipv6,
875 info->
addr.riot.netif = netif ? (uint32_t)netif_get_id(netif) : 0;
880 info->
addr.riot.family = AF_INET;
881 memcpy(&info->
addr.riot.
addr.ipv4, &addr_ipv4,
890 ws_secure_port, type);
896#elif defined(WITH_CONTIKI)
898#include <os/net/ipv6/uiplib.h>
903 uint16_t secure_port,
905 uint16_t ws_secure_port,
907 int scheme_hint_bits,
909 uip_ipaddr_t addr_ip;
916 (void)ai_hints_flags;
918 if (address == NULL || address->
length == 0) {
919 memset(&addr_ip, 0,
sizeof(addr_ip));
922 if (uiplib_ip6addrconv((
const char *)address->
s, (uip_ip6addr_t *)&addr_ip) > 0) {
928 uiplib_ip4addrconv((
const char *)address->
s, (uip_ip4addr_t *)&addr_ip) > 0) {
933 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
938 if (scheme_hint_bits & (1 << scheme)) {
949 info_prev->
next = info;
956 ws_secure_port, type);
964#error "OS type not supported"
969 uint16_t secure_port,
971 uint16_t ws_secure_port,
973 int scheme_hint_bits,
990#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
993#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
999 if (src->
addr.
sa.sa_family == AF_INET6) {
1006#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
1009#if COAP_IPV4_SUPPORT
1010 if (src->
addr.
sa.sa_family == AF_INET) {
1023 switch (a->
addr.
sa.sa_family) {
1024#if COAP_IPV4_SUPPORT
1026 return a->
addr.
sin.sin_addr.s_addr == INADDR_ANY;
1028#if COAP_IPV6_SUPPORT
1030 return memcmp(&in6addr_any,
1032 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 *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