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);
76#if COAP_AF_LLC_SUPPORT
78 return addr->
addr.llc.sllc_sap;
89 switch (addr->
addr.
sa.sa_family) {
92 addr->
addr.
sin.sin_port = htons(port);
97 addr->
addr.
sin6.sin6_port = htons(port);
100#if COAP_AF_LLC_SUPPORT
102 addr->
addr.llc.sllc_sap = port & 0xFF;
111 assert(addr !=
NULL);
112 switch (addr->
addr.
sa.sa_family) {
115 memset(&addr->
addr.
sin.sin_addr, 0,
sizeof(addr->
addr.
sin.sin_addr));
137 switch (a->
addr.
sa.sa_family) {
142 sizeof(
struct in_addr)) == 0;
148 sizeof(
struct in6_addr)) == 0;
150#if COAP_AF_UNIX_SUPPORT
163#if COAP_AF_UNIX_SUPPORT
164 return a->
addr.
sa.sa_family == AF_UNIX;
173#if COAP_AF_LLC_SUPPORT
174 return a->
addr.
sa.sa_family == AF_LLC;
190 switch (a->
addr.
sa.sa_family) {
193 return IN_MULTICAST(ntohl(a->
addr.
sin.sin_addr.s_addr));
198 return IN6_IS_ADDR_MULTICAST(&a->
addr.
sin6.sin6_addr) ||
199 (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr) &&
200 IN_MULTICAST(ntohl(a->
addr.
sin6.sin6_addr.s6_addr[12])));
202 return a->
addr.
sin6.sin6_addr.s6_addr[0] == 0xff;
212#define COAP_BCST_CNT 15
216#ifndef COAP_BCST_REFRESH_SECS
217#define COAP_BCST_REFRESH_SECS 30
220#if COAP_IPV4_SUPPORT && !defined(__ZEPHYR__) && (defined(HAVE_GETIFADDRS) || defined(_WIN32))
221static int bcst_cnt = -1;
230#if defined(HAVE_GETIFADDRS) && !defined(__ZEPHYR__)
239 switch (a->
addr.
sa.sa_family) {
242 ipv4.s_addr = a->
addr.
sin.sin_addr.s_addr;
248 if (IN6_IS_ADDR_V4MAPPED(&a->
addr.
sin6.sin6_addr)) {
249 memcpy(&ipv4, &a->
addr.
sin6.sin6_addr.s6_addr[12],
sizeof(ipv4));
260#ifndef INADDR_BROADCAST
261#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
263 if (ipv4.s_addr == INADDR_BROADCAST)
266#if defined(HAVE_GETIFADDRS) && !defined(__ZEPHYR__)
268 if (bcst_cnt == -1 ||
271 struct ifaddrs *ifa =
NULL;
274 if (getifaddrs(&ifa) != 0) {
275 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
282 if (ife->ifa_addr && ife->ifa_addr->sa_family == AF_INET &&
283 ife->ifa_flags & IFF_BROADCAST) {
284 struct in_addr netmask;
290 netmask.s_addr = ((
struct sockaddr_in *)ife->ifa_netmask)->sin_addr.s_addr;
291 if (netmask.s_addr != 0xffffffff) {
292 b_ipv4[bcst_cnt].s_addr = ((
struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr |
300 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
304 for (i = 0; i < bcst_cnt; i++) {
305 if (ipv4.s_addr == b_ipv4[i].s_addr)
316 if (bcst_cnt == -1 ||
321 PMIB_IPADDRTABLE pIPAddrTable;
330 if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
335 if (pIPAddrTable ==
NULL) {
336 coap_log_warn(
"coap_is_bcst: Cannot determine any broadcast addresses\n");
341 if ((dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0)) != NO_ERROR) {
342 coap_log_warn(
"GetIpAddrTable failed with error %d\n", dwRetVal);
349 for (i = 0; i < (int)pIPAddrTable->dwNumEntries && bcst_cnt <
COAP_BCST_CNT; i++) {
350 struct in_addr netmask;
353 netmask.s_addr = (u_long)pIPAddrTable->table[i].dwMask;
354 if (netmask.s_addr != 0xffffffff) {
355 b_ipv4[bcst_cnt].s_addr = (u_long)pIPAddrTable->table[i].dwAddr |
360 if (i != (
int)pIPAddrTable->dwNumEntries) {
361 coap_log_warn(
"coap_is_bcst: Insufficient space for broadcast addresses\n");
370 for (i = 0; i < bcst_cnt; i++) {
371 if (ipv4.s_addr == b_ipv4[i].s_addr)
386#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
394 const uint8_t *host,
size_t host_len) {
395#if COAP_AF_UNIX_SUPPORT
401 for (i = 0; i < host_len; i++) {
402 if ((host_len - i) >= 3 && host[i] ==
'%' && host[i+1] ==
'2' &&
403 (host[i+2] ==
'F' || host[i+2] ==
'f')) {
425#if COAP_AF_LLC_SUPPORT
427#include <netinet/ether.h>
432 const uint8_t *host,
size_t host_len) {
433#if COAP_AF_LLC_SUPPORT
434 char addrstr[HW_ADDRSTRLEN + 1] = { 0 };
435 struct ether_addr *ether_addr =
NULL;
436 char sapstr[3] = { 0 };
439 if (host_len < LLC_HOST_LEN)
440 coap_log_err(
"%s(): Invalid address length (%zu)\n", __func__, host_len);
443 strncpy(addrstr, (
const char *)host, HW_ADDRSTRLEN);
445 ether_addr = ether_aton(addrstr);
449 host += HW_ADDRSTRLEN + 2;
450 strncpy(sapstr, (
const char *)host, 2);
452 sap = strtoul(sapstr,
NULL, 16);
453 if (sap == ULONG_MAX)
458 addr->
size =
sizeof(addr->
addr.llc);
460 addr->
addr.llc.sllc_family = AF_LLC;
461 addr->
addr.llc.sllc_arphrd = ARPHRD_ETHER;
462 addr->
addr.llc.sllc_sap = sap & 0xFF;
464 memcpy(addr->
addr.llc.sllc_mac, ether_addr, IFHWADDRLEN);
480 if (port == 0 && update_port0)
494 uint32_t scheme_hint_bits = 0;
500 scheme_hint_bits |= 1 << scheme;
505 scheme_hint_bits |= 1 << scheme;
510 scheme_hint_bits |= 1 << scheme;
515 scheme_hint_bits |= 1 << scheme;
520 scheme_hint_bits |= 1 << scheme;
525 scheme_hint_bits |= 1 << scheme;
560 return scheme_hint_bits;
624 uint16_t port, uint16_t secure_port, uint16_t ws_port,
625 uint16_t ws_secure_port,
666#if defined(WITH_LWIP) && !(LWIP_DNS)
671 uint16_t secure_port,
673 uint16_t ws_secure_port,
675 int scheme_hint_bits,
683 (void)ai_hints_flags;
685 if (address ==
NULL || address->
length == 0) {
686 memset(&addr_ip, 0,
sizeof(addr_ip));
688 if (ipaddr_aton((
const char *)address->
s, &addr_ip) <= 0) {
689 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
694 if (scheme_hint_bits & (1 << scheme)) {
705 info_prev->
next = info;
712 ws_secure_port, type);
718#elif !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
723 uint16_t secure_port,
725 uint16_t ws_secure_port,
727 int scheme_hint_bits,
730 struct addrinfo *res, *ainfo;
731 struct addrinfo hints;
732 static char addrstr[256];
740#if COAP_AF_UNIX_SUPPORT
749 if (scheme_hint_bits & (1 << scheme)) {
769#if COAP_AF_LLC_SUPPORT
772 if (scheme_hint_bits & (1 << scheme)) {
793 memset(addrstr, 0,
sizeof(addrstr));
794 if (address && address->
length) {
795 if (address->
length >=
sizeof(addrstr)) {
799 memcpy(addrstr, address->
s, address->
length);
801 memcpy(addrstr,
"localhost", 9);
804 memset((
char *)&hints, 0,
sizeof(hints));
805 hints.ai_socktype = 0;
806 hints.ai_family = AF_UNSPEC;
807 hints.ai_flags = ai_hints_flags;
809 error = getaddrinfo(addrstr,
NULL, &hints, &res);
812#if defined(WITH_LWIP)
815 coap_log_warn(
"getaddrinfo: %s: %s\n", addrstr, gai_strerror(error));
820 for (ainfo = res; ainfo !=
NULL; ainfo = ainfo->ai_next) {
821#if !defined(WITH_LWIP)
822 if (ainfo->ai_addrlen > (socklen_t)
sizeof(info->
addr.
addr))
826 switch (ainfo->ai_family) {
834 if (scheme_hint_bits & (1 << scheme)) {
840#if !defined(WITH_LWIP)
841 info->
addr.
size = (socklen_t)ainfo->ai_addrlen;
842 memcpy(&info->
addr.
addr, ainfo->ai_addr, ainfo->ai_addrlen);
844 memset(&info->
addr, 0,
sizeof(info->
addr));
845 switch (ainfo->ai_family) {
847 struct sockaddr_in6 *sock6;
850 struct sockaddr_in *sock4;
852 sock4 = (
struct sockaddr_in *)ainfo->ai_addr;
853 info->
addr.port = ntohs(sock4->sin_port);
854 memcpy(&info->
addr.
addr, &sock4->sin_addr, 4);
856 info->
addr.
addr.type = IPADDR_TYPE_V4;
862 sock6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
863 info->
addr.port = ntohs(sock6->sin6_port);
864 memcpy(&info->
addr.
addr, &sock6->sin6_addr, 16);
865#if LWIP_IPV6 && LWIP_IPV4
866 info->
addr.
addr.type = IPADDR_TYPE_V6;
875 ws_secure_port, type);
878 info_tmp = info_list;
885 info_tmp = info_tmp->
next;
897 info_prev->
next = info;
913#elif defined(RIOT_VERSION)
915#include "net/utils.h"
920 uint16_t secure_port,
922 uint16_t ws_secure_port,
924 int scheme_hint_bits,
927 ipv6_addr_t addr_ipv6;
930 ipv4_addr_t addr_ipv4;
932 netif_t *netif =
NULL;
937 (void)ai_hints_flags;
938 int family = AF_UNSPEC;
940 if (address ==
NULL || address->
length == 0) {
941 memset(&addr_ipv6, 0,
sizeof(addr_ipv6));
949 if (netutils_get_ipv6(&addr_ipv6, &netif, (
const char *)address->
s) >= 0) {
954 if (family == AF_UNSPEC &&
955 netutils_get_ipv4(&addr_ipv4, (
const char *)address->
s) >= 0) {
959 if (family == AF_UNSPEC) {
960 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
965 if (scheme_hint_bits & (1 << scheme)) {
976 info_prev->
next = info;
983 info->
addr.riot.family = AF_INET6;
984 memcpy(&info->
addr.riot.
addr.ipv6, &addr_ipv6,
986 info->
addr.riot.netif = netif ? (uint32_t)netif_get_id(netif) : 0;
991 info->
addr.riot.family = AF_INET;
992 memcpy(&info->
addr.riot.
addr.ipv4, &addr_ipv4,
1001 ws_secure_port, type);
1007#elif defined(WITH_CONTIKI)
1009#include <os/net/ipv6/uiplib.h>
1014 uint16_t secure_port,
1016 uint16_t ws_secure_port,
1018 int scheme_hint_bits,
1020 uip_ipaddr_t addr_ip;
1027 (void)ai_hints_flags;
1029 if (address ==
NULL || address->
length == 0) {
1030 memset(&addr_ip, 0,
sizeof(addr_ip));
1032#if COAP_IPV6_SUPPORT
1033 if (uiplib_ip6addrconv((
const char *)address->
s, (uip_ip6addr_t *)&addr_ip) > 0) {
1037#if COAP_IPV4_SUPPORT
1039 uiplib_ip4addrconv((
const char *)address->
s, (uip_ip4addr_t *)&addr_ip) > 0) {
1044 coap_log_err(
"coap_resolve_address_info: Unable to parse '%s'\n", address->
s);
1049 if (scheme_hint_bits & (1 << scheme)) {
1060 info_prev->
next = info;
1067 ws_secure_port, type);
1075#error "OS type not supported"
1080 uint16_t secure_port,
1082 uint16_t ws_secure_port,
1084 int scheme_hint_bits,
1101#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
1104#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
1109#if COAP_IPV6_SUPPORT
1110 if (src->
addr.
sa.sa_family == AF_INET6) {
1117#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
1120#if COAP_IPV4_SUPPORT
1121 if (src->
addr.
sa.sa_family == AF_INET) {
1134 switch (a->
addr.
sa.sa_family) {
1135#if COAP_IPV4_SUPPORT
1137 return a->
addr.
sin.sin_addr.s_addr == INADDR_ANY;
1139#if COAP_IPV6_SUPPORT
1141 return memcmp(&in6addr_any,
1143 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.
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.
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.
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...
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.
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)
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_llc(const coap_str_const_t *host)
Determines from the host whether this is an LLC socket request.
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 Note: coap_layers_coap[] needs updating if extended.
#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