23#ifdef HAVE_SYS_SELECT_H
24# include <sys/select.h>
26#ifdef HAVE_SYS_SOCKET_H
27# include <sys/socket.h>
28# define OPTVAL_T(t) (t)
29# define OPTVAL_GT(t) (t)
31#ifdef HAVE_SYS_IOCTL_H
34#ifdef HAVE_NETINET_IN_H
35# include <netinet/in.h>
39# define OPTVAL_T(t) (const char*)(t)
40# define OPTVAL_GT(t) (char*)(t)
42# define CMSG_DATA WSA_CMSG_DATA
53#ifdef COAP_EPOLL_SUPPORT
55#include <sys/timerfd.h>
61#include <zephyr/posix/sys/ioctl.h>
62#include <zephyr/posix/sys/select.h>
63#define OPTVAL_T(t) (const void*)(t)
64#define OPTVAL_GT(t) (void*)(t)
67#ifdef IPV6_RECVPKTINFO
68#define IPV6_PKTINFO IPV6_RECVPKTINFO
70#define IPV6_PKTINFO IP_PKTINFO
72#ifndef IN6_IS_ADDR_V4MAPPED
73#define IN6_IS_ADDR_V4MAPPED(a) \
74 ((((a)->s6_addr32[0]) == 0) && (((a)->s6_addr32[1]) == 0) && \
75 (((a)->s6_addr32[2]) == htonl(0xffff)))
80#if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) && !defined(WITH_LWIP)
82#if defined(IP_PKTINFO)
83# define GEN_IP_PKTINFO IP_PKTINFO
84#elif defined(IP_RECVDSTADDR)
85# define GEN_IP_PKTINFO IP_RECVDSTADDR
87# error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS."
91#ifdef IPV6_RECVPKTINFO
92# define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO
93#elif defined(IPV6_PKTINFO)
94# define GEN_IPV6_PKTINFO IPV6_PKTINFO
96# error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS."
100#if COAP_SERVER_SUPPORT
112#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
114#if COAP_SERVER_SUPPORT
125 struct timeval timeout = {0, 0};
131 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_DGRAM, 0);
147 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
148 coap_log_warn(
"coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
151 switch (listen_addr->
addr.
sa.sa_family) {
154 if (setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
156 coap_log_alert(
"coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
163 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
165 coap_log_alert(
"coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
167#if !defined(ESPIDF_VERSION)
168 if (setsockopt(sock->
fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on),
170 coap_log_alert(
"coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
174 setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
178#if COAP_AF_UNIX_SUPPORT
187 if (setsockopt(sock->
fd, SOL_SOCKET, SO_RCVTIMEO, OPTVAL_T(&timeout),
189 coap_log_alert(
"coap_socket_bind_udp: setsockopt SO_RCVTIMEO: %s\n",
193 if (bind(sock->
fd, &listen_addr->
addr.
sa,
195 listen_addr->
addr.
sa.sa_family == AF_INET ?
196 (socklen_t)
sizeof(
struct sockaddr_in) :
204 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
205 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
210#if defined(RIOT_VERSION) && defined(COAP_SERVER_SUPPORT)
212 bound_addr->
addr.
sa.sa_family == AF_INET6) {
213 bound_addr->
addr.
sin6.sin6_scope_id =
214 listen_addr->
addr.
sin6.sin6_scope_id;
215 bound_addr->
addr.
sin6.sin6_flowinfo = 0;
227#if COAP_CLIENT_SUPPORT
247 sock->
fd = socket(connect_addr.
addr.
sa.sa_family, SOCK_DGRAM, 0);
261 coap_log_warn(
"coap_socket_connect_udp: ioctl FIONBIO: %s\n",
265 switch (connect_addr.
addr.
sa.sa_family) {
268 if (connect_addr.
addr.
sin.sin_port == 0)
269 connect_addr.
addr.
sin.sin_port = htons(default_port);
274 if (connect_addr.
addr.
sin6.sin6_port == 0)
275 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
277 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
279 if (errno != ENOSYS) {
280 coap_log_warn(
"coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
285#if COAP_AF_UNIX_SUPPORT
290 coap_log_alert(
"coap_socket_connect_udp: unsupported sa_family %d\n",
291 connect_addr.
addr.
sa.sa_family);
295 if (local_if && local_if->
addr.
sa.sa_family) {
296 if (local_if->
addr.
sa.sa_family != connect_addr.
addr.
sa.sa_family) {
297 coap_log_warn(
"coap_socket_connect_udp: local address family != "
298 "remote address family\n");
301 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
302 coap_log_warn(
"coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
304 if (bind(sock->
fd, &local_if->
addr.
sa,
306 local_if->
addr.
sa.sa_family == AF_INET ?
307 (socklen_t)
sizeof(
struct sockaddr_in) :
314#if COAP_AF_UNIX_SUPPORT
315 }
else if (connect_addr.
addr.
sa.sa_family == AF_UNIX) {
317 coap_log_warn(
"coap_socket_connect_udp: local address required\n");
324 if (!(local_if && local_if->
addr.
sa.sa_family)) {
329 bind_addr.
addr.
sa.sa_family = connect_addr.
addr.
sa.sa_family;
330 if (bind(sock->
fd, &bind_addr.
addr.
sa,
332 bind_addr.
addr.
sa.sa_family == AF_INET ?
333 (socklen_t)
sizeof(
struct sockaddr_in) :
342 coap_log_warn(
"coap_socket_connect_udp: getsockname for multicast socket: %s\n",
349 setsockopt(sock->
fd, SOL_SOCKET, SO_BROADCAST, OPTVAL_T(&on),
351 coap_log_warn(
"coap_socket_connect_udp: setsockopt SO_BROADCAST: %s\n",
357#if COAP_AF_UNIX_SUPPORT
358 if (connect_addr.
addr.
sa.sa_family == AF_UNIX) {
392#ifdef COAP_EPOLL_SUPPORT
393#if COAP_SERVER_SUPPORT
399 if (context != NULL) {
401 struct epoll_event event;
404 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, sock->
fd, &event);
405 if (ret == -1 && errno != ENOENT) {
413#if COAP_SERVER_SUPPORT
414#if COAP_AF_UNIX_SUPPORT
427#if COAP_CLIENT_SUPPORT
428#if COAP_AF_UNIX_SUPPORT
447#ifdef COAP_EPOLL_SUPPORT
453 struct epoll_event event;
456#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_ERR
463#if COAP_SERVER_SUPPORT
473 memset(&event, 0,
sizeof(event));
474 event.events = events;
475 event.data.ptr = sock;
477 ret = epoll_ctl(context->epfd, EPOLL_CTL_ADD, sock->
fd, &event);
490 struct epoll_event event;
493#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_ERR
500#if COAP_SERVER_SUPPORT
509 event.events = events;
510 event.data.ptr = sock;
512 ret = epoll_ctl(context->epfd, EPOLL_CTL_MOD, sock->
fd, &event);
514#if (COAP_MAX_LOGGING_LEVEL < COAP_LOG_ERR)
529#if COAP_EPOLL_SUPPORT
530 if (context->eptimerfd != -1) {
535 struct itimerspec new_value;
539 memset(&new_value, 0,
sizeof(new_value));
541 new_value.it_value.tv_nsec = 1;
547 ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
550 "coap_update_io_timer",
553#ifdef COAP_DEBUG_WAKEUP_TIMES
556 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
572#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
576coap_win_error_to_errno(
void) {
577 int w_error = WSAGetLastError();
579 case WSA_NOT_ENOUGH_MEMORY:
582 case WSA_INVALID_PARAMETER:
615 case WSAECONNABORTED:
616 errno = ECONNABORTED;
627 case WSAECONNREFUSED:
628 errno = ECONNREFUSED;
630 case WSAEADDRNOTAVAIL:
631 errno = EADDRNOTAVAIL;
634 coap_log_err(
"WSAGetLastError: %d mapping to errno failed - please fix\n",
654 r = send(sock->
fd, (
const char *)data, (
int)data_len, 0);
657#define MSG_NOSIGNAL 0
663 coap_win_error_to_errno();
666#
if EAGAIN != EWOULDBLOCK
667 errno == EWOULDBLOCK ||
671#ifdef COAP_EPOLL_SUPPORT
680 if (errno == EPIPE || errno == ECONNRESET) {
689 if (r < (ssize_t)data_len) {
691#ifdef COAP_EPOLL_SUPPORT
712 r = recv(sock->
fd, (
char *)data, (
int)data_len, 0);
714 r = recv(sock->
fd, data, data_len, 0);
718 sock->
flags &= ~COAP_SOCKET_CAN_READ;
722 sock->
flags &= ~COAP_SOCKET_CAN_READ;
724 coap_win_error_to_errno();
727#
if EAGAIN != EWOULDBLOCK
728 errno == EWOULDBLOCK ||
733 if (errno != ECONNRESET) {
739 if (r < (ssize_t)data_len)
740 sock->
flags &= ~COAP_SOCKET_CAN_READ;
746#if !defined(WITH_LWIP) && !defined(__ZEPHYR__)
747#if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
751#if !defined(__MINGW32__) && !defined(RIOT_VERSION)
766#if !defined(WITH_CONTIKI) && !defined(SOL_IP)
768#define SOL_IP IPPROTO_IP
771#define COAP_SOL_IP IPPROTO_IP
773#define COAP_SOL_IP SOL_IP
778#if defined(__MINGW32__)
779static __thread LPFN_WSARECVMSG lpWSARecvMsg = NULL;
780#if(_WIN32_WINNT >= 0x0600)
781#define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
782#define CMSG_NXTHDR WSA_CMSG_NXTHDR
783#define CMSG_LEN WSA_CMSG_LEN
784#define CMSG_SPACE WSA_CMSG_SPACE
785#if(_WIN32_WINNT < 0x0603 || _WIN32_WINNT == 0x0a00)
786#define cmsghdr _WSACMSGHDR
790static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
793#define msghdr _WSAMSG
795#define msg_namelen namelen
796#define msg_iov lpBuffers
797#define msg_iovlen dwBufferCount
798#define msg_control Control.buf
799#define msg_controllen Control.len
803#define iov_len_t u_long
805#define CMSG_DATA WSA_CMSG_DATA
806#define ipi_spec_dst ipi_addr
807#if !defined(__MINGW32__)
808#pragma warning( disable : 4116 )
811#define iov_len_t size_t
814#if defined(_CYGWIN_ENV) || defined(__QNXNTO__)
815#define ipi_spec_dst ipi_addr
818#if !defined(RIOT_VERSION) && !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
819#if COAP_CLIENT_SUPPORT
820static uint32_t cid_track_counter;
843#ifdef COAP_EPOLL_SUPPORT
865 const uint8_t *data,
size_t datalen) {
866 ssize_t bytes_written = 0;
868#if COAP_CLIENT_SUPPORT
869 coap_test_cid_tuple_change(session);
873 bytes_written = (ssize_t)datalen;
876 bytes_written = send(sock->
fd, (
const char *)data, (
int)datalen, 0);
878 bytes_written = send(sock->
fd, data, datalen, 0);
882 DWORD dwNumberOfBytesSent = 0;
885#ifdef HAVE_STRUCT_CMSGHDR
894 memcpy(&iov[0].iov_base, &data,
sizeof(iov[0].iov_base));
897 memset(buf, 0,
sizeof(buf));
899 memset(&mhdr, 0,
sizeof(
struct msghdr));
900 memcpy(&mhdr.msg_name, &addr,
sizeof(mhdr.msg_name));
902 (socklen_t)
sizeof(
struct sockaddr_in) :
913 struct cmsghdr *cmsg;
917#if defined(IP_PKTINFO)
919 mhdr.msg_control = buf;
920 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
922 cmsg = CMSG_FIRSTHDR(&mhdr);
924 cmsg->cmsg_type = IP_PKTINFO;
925 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
927 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
933#elif defined(IP_SENDSRCADDR)
934 mhdr.msg_control = buf;
935 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
937 cmsg = CMSG_FIRSTHDR(&mhdr);
938 cmsg->cmsg_level = IPPROTO_IP;
939 cmsg->cmsg_type = IP_SENDSRCADDR;
940 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
942 memcpy(CMSG_DATA(cmsg),
944 sizeof(
struct in_addr));
949 mhdr.msg_control = buf;
950 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
952 cmsg = CMSG_FIRSTHDR(&mhdr);
953 cmsg->cmsg_level = IPPROTO_IPV6;
954 cmsg->cmsg_type = IPV6_PKTINFO;
955 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
975#if defined(IP_PKTINFO)
976 struct cmsghdr *cmsg;
979 mhdr.msg_control = buf;
980 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
982 cmsg = CMSG_FIRSTHDR(&mhdr);
984 cmsg->cmsg_type = IP_PKTINFO;
985 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
987 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
993#elif defined(IP_SENDSRCADDR)
994 struct cmsghdr *cmsg;
995 mhdr.msg_control = buf;
996 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
998 cmsg = CMSG_FIRSTHDR(&mhdr);
999 cmsg->cmsg_level = IPPROTO_IP;
1000 cmsg->cmsg_type = IP_SENDSRCADDR;
1001 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
1003 memcpy(CMSG_DATA(cmsg),
1005 sizeof(
struct in_addr));
1010#if COAP_AF_UNIX_SUPPORT
1023 r = WSASendMsg(sock->
fd, &mhdr, 0 , &dwNumberOfBytesSent, NULL ,
1026 bytes_written = (ssize_t)dwNumberOfBytesSent;
1029 coap_win_error_to_errno();
1032#ifdef HAVE_STRUCT_CMSGHDR
1033 bytes_written = sendmsg(sock->
fd, &mhdr, 0);
1035 bytes_written = sendto(sock->
fd, (
const void *)data, datalen, 0,
1042 if (bytes_written < 0)
1045 return bytes_written;
1049#define SIN6(A) ((struct sockaddr_in6 *)(A))
1054 *length = packet->
length;
1057#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
1075 sock->
flags &= ~COAP_SOCKET_CAN_READ;
1086 coap_win_error_to_errno();
1088 if (errno == ECONNREFUSED || errno == EHOSTUNREACH || errno == ECONNRESET) {
1096 if (errno != EAGAIN) {
1103 }
else if (len > 0) {
1104 packet->
length = (size_t)len;
1108 DWORD dwNumberOfBytesRecvd = 0;
1111#ifdef HAVE_STRUCT_CMSGHDR
1114 struct cmsghdr *cmsg;
1116 struct iovec iov[1];
1118#if defined(__MINGW32__)
1119 iov[0].iov_base = (
char *) packet->
payload;
1121 iov[0].iov_base = packet->
payload;
1125 memset(&mhdr, 0,
sizeof(
struct msghdr));
1131 mhdr.msg_iovlen = 1;
1133 mhdr.msg_control = buf;
1134 mhdr.msg_controllen =
sizeof(buf);
1137 cmsg = (
struct cmsghdr *)buf;
1138 cmsg->cmsg_len = CMSG_LEN(
sizeof(buf));
1139 cmsg->cmsg_level = -1;
1140 cmsg->cmsg_type = -1;
1143 if (!lpWSARecvMsg) {
1144 GUID wsaid = WSAID_WSARECVMSG;
1145 DWORD cbBytesReturned = 0;
1146 if (WSAIoctl(sock->
fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &wsaid,
sizeof(wsaid), &lpWSARecvMsg,
1147 sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
1152 r = lpWSARecvMsg(sock->
fd, &mhdr, &dwNumberOfBytesRecvd, NULL ,
1155 len = (ssize_t)dwNumberOfBytesRecvd;
1157 coap_win_error_to_errno();
1159 len = recvmsg(sock->
fd, &mhdr, 0);
1170 coap_win_error_to_errno();
1172 if (errno == ECONNREFUSED || errno == EHOSTUNREACH || errno == ECONNRESET) {
1180 if (errno != EAGAIN) {
1185#ifdef HAVE_STRUCT_CMSGHDR
1189 packet->
length = (size_t)len;
1193 for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
1195#if COAP_IPV6_SUPPORT
1197 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
1202 u.c = CMSG_DATA(cmsg);
1203 packet->
ifindex = (int)(u.p->ipi6_ifindex);
1205 &u.p->ipi6_addr,
sizeof(
struct in6_addr));
1211#if COAP_IPV4_SUPPORT
1213#if defined(IP_PKTINFO)
1214 if (cmsg->cmsg_level ==
COAP_SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
1219 u.c = CMSG_DATA(cmsg);
1220 packet->
ifindex = u.p->ipi_ifindex;
1221#if COAP_IPV6_SUPPORT
1227 &u.p->ipi_addr,
sizeof(
struct in_addr));
1232 &u.p->ipi_addr,
sizeof(
struct in_addr));
1238#if defined(IP_RECVDSTADDR)
1239 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
1242 CMSG_DATA(cmsg),
sizeof(
struct in_addr));
1251 if (cmsg->cmsg_level != -1 && cmsg->cmsg_type != -1) {
1252 coap_log_debug(
"cmsg_level = %d and cmsg_type = %d not supported - fix\n",
1253 cmsg->cmsg_level, cmsg->cmsg_type);
1267 packet->
length = (size_t)len;
1297#ifndef COAP_EPOLL_SUPPORT
1300 coap_log_emerg(
"coap_io_prepare_epoll() requires libcoap compiled for using epoll\n");
1304 unsigned int max_sockets =
sizeof(sockets)/
sizeof(sockets[0]);
1305 unsigned int num_sockets;
1306 unsigned int timeout;
1313 if (ctx->eptimerfd != -1) {
1314 struct itimerspec new_value;
1317 memset(&new_value, 0,
sizeof(new_value));
1326#ifdef COAP_DEBUG_WAKEUP_TIMES
1328 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
1331 ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
1334 "coap_io_prepare_epoll",
1349 unsigned int max_sockets,
1350 unsigned int *num_sockets,
1367 unsigned int max_sockets,
1368 unsigned int *num_sockets,
1374#if COAP_SERVER_SUPPORT
1375 int check_dtls_timeouts = 0;
1377#if defined(COAP_EPOLL_SUPPORT) || defined(WITH_LWIP) || defined(RIOT_VERSION)
1385#if COAP_SERVER_SUPPORT
1389#if COAP_ASYNC_SUPPORT
1391 if (coap_check_async(ctx, now, &s_timeout)) {
1392 if (s_timeout < timeout)
1393 timeout = s_timeout;
1413 if (tls_timeout > 0) {
1418 if (tls_timeout - now < timeout)
1419 timeout = tls_timeout - now;
1421#if COAP_SERVER_SUPPORT
1423 check_dtls_timeouts = 1;
1427#if COAP_PROXY_SUPPORT
1428 if (coap_proxy_check_timeouts(ctx, now, &s_timeout)) {
1429 if (s_timeout < timeout)
1430 timeout = s_timeout;
1433#if COAP_SERVER_SUPPORT
1443#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1445 if (*num_sockets < max_sockets)
1446 sockets[(*num_sockets)++] = &ep->
sock;
1462 s_timeout = (s->
last_rx_tx + session_timeout) - now;
1463 if (s_timeout < timeout)
1464 timeout = s_timeout;
1472 while (tls_timeout > 0 && tls_timeout <= now) {
1485 if (tls_timeout > 0 && tls_timeout - now < timeout)
1486 timeout = tls_timeout - now;
1491 if (s_timeout < timeout)
1492 timeout = s_timeout;
1498 if (s_timeout < timeout)
1499 timeout = s_timeout;
1502#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1505 sockets[(*num_sockets)++] = &s->
sock;
1508#if COAP_Q_BLOCK_SUPPORT
1514 if (coap_block_check_q_block2_xmit(s, now, &s_timeout)) {
1515 if (s_timeout < timeout)
1516 timeout = s_timeout;
1548 if (s_timeout < timeout)
1549 timeout = s_timeout;
1555#if COAP_CLIENT_SUPPORT
1579 if (s_timeout < timeout)
1580 timeout = s_timeout;
1590 if (timeout == 0 || s_timeout < timeout)
1591 timeout = s_timeout;
1596 if (s_timeout < timeout)
1597 timeout = s_timeout;
1601#if !COAP_DISABLE_TCP
1613 if (s_timeout < timeout)
1614 timeout = s_timeout;
1624 while (tls_timeout > 0 && tls_timeout <= now) {
1636 if (tls_timeout > 0 && tls_timeout - now < timeout)
1637 timeout = tls_timeout - now;
1643 if (s_timeout < timeout)
1644 timeout = s_timeout;
1650 if (s_timeout < timeout)
1651 timeout = s_timeout;
1654#if COAP_Q_BLOCK_SUPPORT
1660 if (coap_block_check_q_block1_xmit(s, now, &s_timeout)) {
1661 if (s_timeout < timeout)
1662 timeout = s_timeout;
1667#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1673 sockets[(*num_sockets)++] = &s->
sock;
1691 unsigned int *have_read_fds,
1692 unsigned int max_read_fds,
1694 unsigned int *have_write_fds,
1695 unsigned int max_write_fds,
1696 unsigned int *rem_timeout_ms) {
1701 have_write_fds, max_write_fds, rem_timeout_ms);
1706#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1709 unsigned int max_this_fds) {
1710 if (*have_this_fds < max_this_fds) {
1711 this_fds[(*have_this_fds)++] = fd;
1714 coap_log_warn(
"coap_io_get_fds: Insufficient space for new fd (%u >= %u)\n", *have_this_fds,
1726 unsigned int *have_read_fds,
1727 unsigned int max_read_fds,
1729 unsigned int *have_write_fds,
1730 unsigned int max_write_fds,
1731 unsigned int *rem_timeout_ms) {
1733 *have_write_fds = 0;
1735#ifdef COAP_EPOLL_SUPPORT
1737 (void)max_write_fds;;
1739 if (!
coap_add_fd(ctx->epfd, read_fds, have_read_fds, max_read_fds))
1742 *rem_timeout_ms = 0;
1747 unsigned int timeout_ms;
1748#if COAP_SERVER_SUPPORT
1773#if COAP_CLIENT_SUPPORT
1790 *rem_timeout_ms = timeout_ms;
1804 unsigned int *have_read_fds,
1805 unsigned int max_read_fds,
1807 unsigned int *have_write_fds,
1808 unsigned int max_write_fds,
1809 unsigned int *rem_timeout_ms) {
1814 (void)max_write_fds;
1817 *have_write_fds = 0;
1818 *rem_timeout_ms = 0;
1825#if !defined(WITH_LWIP) && !defined(CONTIKI) && !defined(RIOT_VERSION)
1843 int enfds, fd_set *ereadfds, fd_set *ewritefds,
1844 fd_set *eexceptfds) {
1854#if !defined(COAP_EPOLL_SUPPORT) && COAP_THREAD_SAFE
1857 int enfds, fd_set *ereadfds, fd_set *ewritefds,
1858 fd_set *eexceptfds) {
1860 unsigned int max_sockets =
sizeof(ctx->
sockets) /
sizeof(ctx->
sockets[0]);
1865#if COAP_SERVER_SUPPORT
1881#if COAP_CLIENT_SUPPORT
1916#if !COAP_DISABLE_TCP
1931 int enfds, fd_set *ereadfds, fd_set *ewritefds,
1932 fd_set *eexceptfds) {
1935 unsigned int timeout;
1936#ifndef COAP_EPOLL_SUPPORT
1945#ifndef COAP_EPOLL_SUPPORT
1977#if !COAP_DISABLE_TCP
1991 }
else if (timeout == 0 && timeout_ms ==
COAP_IO_WAIT) {
1994 if (timeout == 0 || (timeout_ms !=
COAP_IO_WAIT && timeout_ms < timeout))
1995 timeout = timeout_ms;
1996 tv.tv_usec = (timeout % 1000) * 1000;
1997 tv.tv_sec = (long)(timeout / 1000);
2006 timeout > 0 ? &tv : NULL);
2015 coap_win_error_to_errno();
2017 if (errno != EINTR) {
2019 if (errno == EBADF) {
2031 nfds = coap_io_prepare_fds(ctx, enfds, ereadfds, ewritefds, eexceptfds);
2037 coap_win_error_to_errno();
2039 if (errno != EINTR) {
2040 if (errno == EBADF) {
2065#if !COAP_DISABLE_TCP
2104 }
else if (timeout == 0 && timeout_ms ==
COAP_IO_WAIT) {
2112 if (timeout == 0 || (timeout_ms !=
COAP_IO_WAIT && timeout_ms < timeout))
2113 etimeout = timeout_ms;
2128 if (errno != EINTR) {
2129 coap_log_err(
"epoll_wait: unexpected error: %s (%d)\n",
2141 if (errno != EINTR) {
2142 coap_log_err(
"epoll_wait: unexpected error: %s (%d)\n",
2162#if COAP_SERVER_SUPPORT
2165#if COAP_ASYNC_SUPPORT
2169 coap_check_async(ctx, now, NULL);
2172#ifndef COAP_EPOLL_SUPPORT
2190 void *main_loop_code_arg, uint32_t timeout_ms,
2191 uint32_t thread_count) {
2198 main_loop_code_arg, timeout_ms,
2207 void *main_loop_code_arg, uint32_t timeout_ms,
2208 uint32_t thread_count) {
2212 if (thread_count > 1) {
2220 if (main_loop_code) {
2241 }
else if (timeout_ms > used_ms) {
2244 unsigned int next_sec_us;
2245 unsigned int next_sec_ms;
2249 next_sec_ms = (next_sec_us + 999) / 1000;
2250 if (next_sec_ms > timeout_ms && next_sec_ms > 1000)
2251 next_sec_ms -= 1000;
2258 if (thread_count == 1) {
2266 if (timeout_ms > 0 && timeout_ms < used_ms) {
2310#if COAP_SERVER_SUPPORT
2322#if COAP_SERVER_SUPPORT
2323 LL_FOREACH(context->
endpoint, ep) {
2334#if COAP_CLIENT_SUPPORT
2349 return strerror(error);
2354 coap_win_error_to_errno();
2366#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
2381 sock->
flags = flags;
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_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.
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
#define COAP_IPV4_SUPPORT
const char * coap_socket_format_errno(int error)
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Function interface for data stream receiving off a socket.
static int coap_add_fd(coap_fd_t fd, coap_fd_t this_fds[], unsigned int *have_this_fds, unsigned int max_this_fds)
void coap_socket_close(coap_socket_t *sock)
Function interface to close off a socket.
const char * coap_socket_strerror(void)
ssize_t coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for data transmission.
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
Given a packet, set msg and msg_len to an address and length of the packet's data in memory.
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Function interface for data stream sending off a socket.
volatile int coap_thread_quit
void coap_update_io_timer(coap_context_t *context, coap_tick_t delay)
Update when to continue with I/O processing, unless packets come in in the meantime.
#define COAP_MAX_EPOLL_EVENTS
uint16_t coap_socket_flags_t
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_ERROR
#define COAP_INVALID_SOCKET
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to add the state of events that epoll is to track for the appropriate file de...
int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_SLAVE
socket is a slave socket - do not close
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
coap_endpoint_t * coap_malloc_endpoint(void)
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to modify the state of events that epoll is tracking on the appropriate file ...
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
void coap_mfree_endpoint(coap_endpoint_t *ep)
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
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().
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define SESSIONS_ITER(e, el, rtmp)
#define COAP_DEFAULT_SESSION_TIMEOUT
void coap_io_do_epoll_lkd(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
int coap_io_process_loop_lkd(coap_context_t *context, coap_io_process_thread_t main_loop_code, void *main_loop_code_arg, uint32_t timeout_ms, uint32_t thread_count)
Do the coap_io_process() across thread_count threads.
int coap_io_pending_lkd(coap_context_t *context)
Check to see if there is any i/o pending for the context.
void coap_io_do_io_lkd(coap_context_t *ctx, coap_tick_t now)
Processes any outstanding read, write, accept or connect I/O as indicated in the coap_socket_t struct...
unsigned int coap_io_get_fds_lkd(coap_context_t *ctx, coap_fd_t read_fds[], unsigned int *have_read_fds, unsigned int max_read_fds, coap_fd_t write_fds[], unsigned int *have_write_fds, unsigned int max_write_fds, unsigned int *rem_timeout_ms)
int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
int coap_io_process_with_fds_lkd(coap_context_t *ctx, uint32_t timeout_ms, int enfds, fd_set *ereadfds, fd_set *ewritefds, fd_set *eexceptfds)
The main message processing loop with additional fds for internal select.
unsigned int coap_io_prepare_io_lkd(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
unsigned int coap_io_prepare_epoll_lkd(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
COAP_API int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
COAP_API int coap_io_pending(coap_context_t *context)
Check to see if there is any i/o pending for the context.
int coap_io_process_configure_threads(coap_context_t *context, uint32_t thread_count)
Configure a defined number of threads to do the alternate coap_io_process() work with traffic load ba...
void coap_io_process_terminate_loop(void)
Terminate all the additional threads created by coap_io_process_loop() and break out of the main thre...
void coap_io_process_remove_threads(coap_context_t *context)
Release the coap_io_process() worker threads.
COAP_API int coap_io_process_loop(coap_context_t *context, coap_io_process_thread_t main_loop_code, void *main_loop_code_arg, uint32_t timeout_ms, uint32_t thread_count)
Do the coap_io_process() across thread_count threads.
COAP_API unsigned int coap_io_get_fds(coap_context_t *ctx, coap_fd_t read_fds[], unsigned int *have_read_fds, unsigned int max_read_fds, coap_fd_t write_fds[], unsigned int *have_write_fds, unsigned int max_write_fds, unsigned int *rem_timeout_ms)
COAP_API unsigned int coap_io_prepare_io(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
COAP_API void coap_socket_set_flags(coap_socket_t *sock, coap_socket_flags_t flags)
Set the libcoap internal flags for a socket.
COAP_API unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
COAP_API coap_fd_t coap_socket_get_fd(coap_socket_t *sock)
Get the libcoap internal file descriptor for a socket.
COAP_API int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, int enfds, fd_set *ereadfds, fd_set *ewritefds, fd_set *eexceptfds)
The main message processing loop with additional fds for internal select.
void(* coap_io_process_thread_t)(void *arg)
Main thread coap_io_process_loop activity.
COAP_API coap_socket_flags_t coap_socket_get_flags(coap_socket_t *sock)
Get the libcoap internal flags for a socket.
int coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
void coap_expire_cache_entries(coap_context_t *context)
Expire coap_cache_entry_t entries.
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.
#define COAP_MAX_DELAY_TICKS
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
void coap_send_recv_terminate(void)
Terminate any active coap_send_recv() sessions.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
@ COAP_EVENT_KEEPALIVE_FAILURE
Triggered when no response to a keep alive (ping) packet.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_callback_release(func, failed)
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
#define coap_log_alert(...)
#define coap_log_emerg(...)
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define coap_log_crit(...)
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_INVALID_MID
Indicates an invalid message id.
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
void coap_session_server_keepalive_failed(coap_session_t *session)
Clear down a session following a keepalive failure.
void coap_session_failed(coap_session_t *session)
Session has failed due to a socket error.
coap_mid_t coap_session_send_ping_lkd(coap_session_t *session)
Send a ping message for the session.
void coap_session_free(coap_session_t *session)
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
#define COAP_PROTO_RELIABLE(p)
@ COAP_SESSION_TYPE_SERVER
server-side
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_NONE
void coap_check_notify_lkd(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
coap_address_t remote
remote address and port
coap_address_t local
local address and port
Multi-purpose address abstraction.
socklen_t size
size of addr
struct coap_sockaddr_un cun
union coap_address_t::@0 addr
The CoAP stack's global state is stored in a coap_context_t object.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
coap_socket_t * sockets[64]
Track different socket information in coap_io_process_with_fds_lkd()
unsigned int reconnect_time
Time to wait before reconnecting a failed client session.
unsigned int num_sockets
Number of sockets being tracked.
coap_session_t * sessions
client sessions
fd_set exceptfds
Used for select call in coap_io_process_with_fds_lkd()
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_endpoint_t * endpoint
the endpoints used for listening
uint32_t csm_timeout_ms
Timeout for waiting for a CSM from the remote side.
coap_tick_t next_timeout
When the next timeout is to occur.
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
unsigned ref_subscriptions
reference count of current subscriptions
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
unsigned ref_proxy_subs
reference count of current proxy subscriptions
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
unsigned ref
reference count from queues
uint8_t negotiated_cid
Set for a client if CID negotiated.
void * tls
security parameters
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
uint8_t session_failed
Set if session failed and can try re-connect.
int ifindex
interface index
char sun_path[COAP_UNIX_PATH_MAX]
coap_session_t * session
Used to determine session owner.
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
coap_address_t mcast_addr
remote address and port (multicast track)
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
struct in6_addr ipi6_addr
unsigned int ipi6_ifindex
struct in_addr ipi_spec_dst