22#ifdef HAVE_SYS_SELECT_H
23# include <sys/select.h>
25#ifdef HAVE_SYS_SOCKET_H
26# include <sys/socket.h>
27# define OPTVAL_T(t) (t)
28# define OPTVAL_GT(t) (t)
30#ifdef HAVE_SYS_IOCTL_H
33#ifdef HAVE_NETINET_IN_H
34# include <netinet/in.h>
38# define OPTVAL_T(t) (const char*)(t)
39# define OPTVAL_GT(t) (char*)(t)
41# define CMSG_DATA WSA_CMSG_DATA
49#ifdef COAP_EPOLL_SUPPORT
51#include <sys/timerfd.h>
57#if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) && !defined(WITH_LWIP)
59#if defined(IP_PKTINFO)
60# define GEN_IP_PKTINFO IP_PKTINFO
61#elif defined(IP_RECVDSTADDR)
62# define GEN_IP_PKTINFO IP_RECVDSTADDR
64# error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS."
68#ifdef IPV6_RECVPKTINFO
69# define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO
70#elif defined(IPV6_PKTINFO)
71# define GEN_IPV6_PKTINFO IPV6_PKTINFO
73# error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS."
77#if COAP_SERVER_SUPPORT
89#if defined(__MINGW32__)
90#if(_WIN32_WINNT >= 0x0600)
91#define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
92#define CMSG_NXTHDR WSA_CMSG_NXTHDR
93#define CMSG_LEN WSA_CMSG_LEN
94#define CMSG_SPACE WSA_CMSG_SPACE
95#define cmsghdr _WSACMSGHDR
99#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
101#if COAP_SERVER_SUPPORT
112 struct timeval timeout = {0, 0};
118 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_DGRAM, 0);
134 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
135 coap_log_warn(
"coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
138 switch (listen_addr->
addr.
sa.sa_family) {
141 if (setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
143 coap_log_alert(
"coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
150 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
152 coap_log_alert(
"coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
154#if !defined(ESPIDF_VERSION)
155 if (setsockopt(sock->
fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on),
157 coap_log_alert(
"coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
161 setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
165#if COAP_AF_UNIX_SUPPORT
174 if (setsockopt(sock->
fd, SOL_SOCKET, SO_RCVTIMEO, OPTVAL_T(&timeout),
176 coap_log_alert(
"coap_socket_bind_udp: setsockopt SO_RCVTIMEO: %s\n",
180 if (bind(sock->
fd, &listen_addr->
addr.
sa,
182 listen_addr->
addr.
sa.sa_family == AF_INET ?
183 (socklen_t)
sizeof(
struct sockaddr_in) :
191 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
192 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
197#if defined(RIOT_VERSION) && defined(COAP_SERVER_SUPPORT)
199 bound_addr->
addr.
sa.sa_family == AF_INET6) {
200 bound_addr->
addr.
sin6.sin6_scope_id =
201 listen_addr->
addr.
sin6.sin6_scope_id;
202 bound_addr->
addr.
sin6.sin6_flowinfo = 0;
214#if COAP_CLIENT_SUPPORT
234 sock->
fd = socket(connect_addr.
addr.
sa.sa_family, SOCK_DGRAM, 0);
248 coap_log_warn(
"coap_socket_connect_udp: ioctl FIONBIO: %s\n",
252 switch (connect_addr.
addr.
sa.sa_family) {
255 if (connect_addr.
addr.
sin.sin_port == 0)
256 connect_addr.
addr.
sin.sin_port = htons(default_port);
261 if (connect_addr.
addr.
sin6.sin6_port == 0)
262 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
264 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
266 coap_log_warn(
"coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
270#if COAP_AF_UNIX_SUPPORT
275 coap_log_alert(
"coap_socket_connect_udp: unsupported sa_family %d\n",
276 connect_addr.
addr.
sa.sa_family);
280 if (local_if && local_if->
addr.
sa.sa_family) {
281 if (local_if->
addr.
sa.sa_family != connect_addr.
addr.
sa.sa_family) {
282 coap_log_warn(
"coap_socket_connect_udp: local address family != "
283 "remote address family\n");
286 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
287 coap_log_warn(
"coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
289 if (bind(sock->
fd, &local_if->
addr.
sa,
291 local_if->
addr.
sa.sa_family == AF_INET ?
292 (socklen_t)
sizeof(
struct sockaddr_in) :
299#if COAP_AF_UNIX_SUPPORT
300 }
else if (connect_addr.
addr.
sa.sa_family == AF_UNIX) {
302 coap_log_warn(
"coap_socket_connect_udp: local address required\n");
309 if (!(local_if && local_if->
addr.
sa.sa_family)) {
314 bind_addr.
addr.
sa.sa_family = connect_addr.
addr.
sa.sa_family;
315 if (bind(sock->
fd, &bind_addr.
addr.
sa,
317 bind_addr.
addr.
sa.sa_family == AF_INET ?
318 (socklen_t)
sizeof(
struct sockaddr_in) :
327 coap_log_warn(
"coap_socket_connect_udp: getsockname for multicast socket: %s\n",
334 setsockopt(sock->
fd, SOL_SOCKET, SO_BROADCAST, OPTVAL_T(&on),
336 coap_log_warn(
"coap_socket_connect_udp: setsockopt SO_BROADCAST: %s\n",
342#if COAP_AF_UNIX_SUPPORT
343 if (connect_addr.
addr.
sa.sa_family == AF_UNIX) {
377#ifdef COAP_EPOLL_SUPPORT
378#if COAP_SERVER_SUPPORT
384 if (context != NULL) {
386 struct epoll_event event;
389 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, sock->
fd, &event);
390 if (ret == -1 && errno != ENOENT) {
396#if COAP_SERVER_SUPPORT
397#if COAP_AF_UNIX_SUPPORT
406#if COAP_CLIENT_SUPPORT
407#if COAP_AF_UNIX_SUPPORT
423#ifdef COAP_EPOLL_SUPPORT
429 struct epoll_event event;
432#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_ERR
439#if COAP_SERVER_SUPPORT
449 memset(&event, 0,
sizeof(event));
450 event.events = events;
451 event.data.ptr = sock;
453 ret = epoll_ctl(context->epfd, EPOLL_CTL_ADD, sock->
fd, &event);
466 struct epoll_event event;
469#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_ERR
476#if COAP_SERVER_SUPPORT
485 event.events = events;
486 event.data.ptr = sock;
488 ret = epoll_ctl(context->epfd, EPOLL_CTL_MOD, sock->
fd, &event);
490#if (COAP_MAX_LOGGING_LEVEL < COAP_LOG_ERR)
505#if COAP_EPOLL_SUPPORT
506 if (context->eptimerfd != -1) {
511 struct itimerspec new_value;
515 memset(&new_value, 0,
sizeof(new_value));
517 new_value.it_value.tv_nsec = 1;
523 ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
526 "coap_update_io_timer",
529#ifdef COAP_DEBUG_WAKEUP_TIMES
532 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
548#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
552coap_win_error_to_errno(
void) {
553 int w_error = WSAGetLastError();
555 case WSA_NOT_ENOUGH_MEMORY:
558 case WSA_INVALID_PARAMETER:
591 case WSAECONNABORTED:
592 errno = ECONNABORTED;
603 case WSAECONNREFUSED:
604 errno = ECONNREFUSED;
607 coap_log_err(
"WSAGetLastError: %d mapping to errno failed - please fix\n",
627 r = send(sock->
fd, (
const char *)data, (
int)data_len, 0);
630#define MSG_NOSIGNAL 0
636 coap_win_error_to_errno();
639#
if EAGAIN != EWOULDBLOCK
640 errno == EWOULDBLOCK ||
644#ifdef COAP_EPOLL_SUPPORT
653 if (errno == EPIPE || errno == ECONNRESET) {
662 if (r < (ssize_t)data_len) {
664#ifdef COAP_EPOLL_SUPPORT
685 r = recv(sock->
fd, (
char *)data, (
int)data_len, 0);
687 r = recv(sock->
fd, data, data_len, 0);
691 sock->
flags &= ~COAP_SOCKET_CAN_READ;
695 sock->
flags &= ~COAP_SOCKET_CAN_READ;
697 coap_win_error_to_errno();
700#
if EAGAIN != EWOULDBLOCK
701 errno == EWOULDBLOCK ||
706 if (errno != ECONNRESET) {
712 if (r < (ssize_t)data_len)
713 sock->
flags &= ~COAP_SOCKET_CAN_READ;
719#if !defined(WITH_LWIP)
720#if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
724#if !defined(__MINGW32__) && !defined(RIOT_VERSION)
739#if !defined(WITH_CONTIKI) && !defined(SOL_IP)
741#define SOL_IP IPPROTO_IP
744#define COAP_SOL_IP IPPROTO_IP
746#define COAP_SOL_IP SOL_IP
751#if defined(__MINGW32__)
752static __thread LPFN_WSARECVMSG lpWSARecvMsg = NULL;
754static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
757#define msghdr _WSAMSG
759#define msg_namelen namelen
760#define msg_iov lpBuffers
761#define msg_iovlen dwBufferCount
762#define msg_control Control.buf
763#define msg_controllen Control.len
767#define iov_len_t u_long
769#define CMSG_DATA WSA_CMSG_DATA
770#define ipi_spec_dst ipi_addr
771#if !defined(__MINGW32__)
772#pragma warning( disable : 4116 )
775#define iov_len_t size_t
778#if defined(_CYGWIN_ENV) || defined(__QNXNTO__)
779#define ipi_spec_dst ipi_addr
782#if !defined(RIOT_VERSION) && !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
783#if COAP_CLIENT_SUPPORT
784static uint32_t cid_track_counter;
807#ifdef COAP_EPOLL_SUPPORT
829 const uint8_t *data,
size_t datalen) {
830 ssize_t bytes_written = 0;
832#if COAP_CLIENT_SUPPORT
833 coap_test_cid_tuple_change(session);
837 bytes_written = (ssize_t)datalen;
840 bytes_written = send(sock->
fd, (
const char *)data, (
int)datalen, 0);
842 bytes_written = send(sock->
fd, data, datalen, 0);
846 DWORD dwNumberOfBytesSent = 0;
849#ifdef HAVE_STRUCT_CMSGHDR
858 memcpy(&iov[0].iov_base, &data,
sizeof(iov[0].iov_base));
861 memset(buf, 0,
sizeof(buf));
863 memset(&mhdr, 0,
sizeof(
struct msghdr));
864 memcpy(&mhdr.msg_name, &addr,
sizeof(mhdr.msg_name));
866 (socklen_t)
sizeof(
struct sockaddr_in) :
877 struct cmsghdr *cmsg;
881#if defined(IP_PKTINFO)
883 mhdr.msg_control = buf;
884 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
886 cmsg = CMSG_FIRSTHDR(&mhdr);
888 cmsg->cmsg_type = IP_PKTINFO;
889 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
891 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
897#elif defined(IP_SENDSRCADDR)
898 mhdr.msg_control = buf;
899 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
901 cmsg = CMSG_FIRSTHDR(&mhdr);
902 cmsg->cmsg_level = IPPROTO_IP;
903 cmsg->cmsg_type = IP_SENDSRCADDR;
904 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
906 memcpy(CMSG_DATA(cmsg),
908 sizeof(
struct in_addr));
913 mhdr.msg_control = buf;
914 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
916 cmsg = CMSG_FIRSTHDR(&mhdr);
917 cmsg->cmsg_level = IPPROTO_IPV6;
918 cmsg->cmsg_type = IPV6_PKTINFO;
919 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
935#if defined(IP_PKTINFO)
936 struct cmsghdr *cmsg;
939 mhdr.msg_control = buf;
940 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
942 cmsg = CMSG_FIRSTHDR(&mhdr);
944 cmsg->cmsg_type = IP_PKTINFO;
945 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
947 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
953#elif defined(IP_SENDSRCADDR)
954 struct cmsghdr *cmsg;
955 mhdr.msg_control = buf;
956 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
958 cmsg = CMSG_FIRSTHDR(&mhdr);
959 cmsg->cmsg_level = IPPROTO_IP;
960 cmsg->cmsg_type = IP_SENDSRCADDR;
961 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
963 memcpy(CMSG_DATA(cmsg),
965 sizeof(
struct in_addr));
970#if COAP_AF_UNIX_SUPPORT
983 r = WSASendMsg(sock->
fd, &mhdr, 0 , &dwNumberOfBytesSent, NULL ,
986 bytes_written = (ssize_t)dwNumberOfBytesSent;
989 coap_win_error_to_errno();
992#ifdef HAVE_STRUCT_CMSGHDR
993 bytes_written = sendmsg(sock->
fd, &mhdr, 0);
995 bytes_written = sendto(sock->
fd, (
const void *)data, datalen, 0,
1002 if (bytes_written < 0)
1005 return bytes_written;
1009#define SIN6(A) ((struct sockaddr_in6 *)(A))
1014 *length = packet->
length;
1017#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
1035 sock->
flags &= ~COAP_SOCKET_CAN_READ;
1046 coap_win_error_to_errno();
1048 if (errno == ECONNREFUSED || errno == EHOSTUNREACH || errno == ECONNRESET) {
1056 if (errno != EAGAIN) {
1063 }
else if (len > 0) {
1064 packet->
length = (size_t)len;
1068 DWORD dwNumberOfBytesRecvd = 0;
1071#ifdef HAVE_STRUCT_CMSGHDR
1074 struct cmsghdr *cmsg;
1076 struct iovec iov[1];
1078#if defined(__MINGW32__)
1079 iov[0].iov_base = (
char *) packet->
payload;
1081 iov[0].iov_base = packet->
payload;
1085 memset(&mhdr, 0,
sizeof(
struct msghdr));
1091 mhdr.msg_iovlen = 1;
1093 mhdr.msg_control = buf;
1094 mhdr.msg_controllen =
sizeof(buf);
1097 cmsg = (
struct cmsghdr *)buf;
1098 cmsg->cmsg_len = CMSG_LEN(
sizeof(buf));
1099 cmsg->cmsg_level = -1;
1100 cmsg->cmsg_type = -1;
1103 if (!lpWSARecvMsg) {
1104 GUID wsaid = WSAID_WSARECVMSG;
1105 DWORD cbBytesReturned = 0;
1106 if (WSAIoctl(sock->
fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &wsaid,
sizeof(wsaid), &lpWSARecvMsg,
1107 sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
1112 r = lpWSARecvMsg(sock->
fd, &mhdr, &dwNumberOfBytesRecvd, NULL ,
1115 len = (ssize_t)dwNumberOfBytesRecvd;
1117 coap_win_error_to_errno();
1119 len = recvmsg(sock->
fd, &mhdr, 0);
1130 coap_win_error_to_errno();
1132 if (errno == ECONNREFUSED || errno == EHOSTUNREACH || errno == ECONNRESET) {
1140 if (errno != EAGAIN) {
1145#ifdef HAVE_STRUCT_CMSGHDR
1149 packet->
length = (size_t)len;
1153 for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
1155#if COAP_IPV6_SUPPORT
1157 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
1162 u.c = CMSG_DATA(cmsg);
1163 packet->
ifindex = (int)(u.p->ipi6_ifindex);
1165 &u.p->ipi6_addr,
sizeof(
struct in6_addr));
1171#if COAP_IPV4_SUPPORT
1173#if defined(IP_PKTINFO)
1174 if (cmsg->cmsg_level ==
COAP_SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
1179 u.c = CMSG_DATA(cmsg);
1180 packet->
ifindex = u.p->ipi_ifindex;
1181#if COAP_IPV6_SUPPORT
1187 &u.p->ipi_addr,
sizeof(
struct in_addr));
1192 &u.p->ipi_addr,
sizeof(
struct in_addr));
1198#if defined(IP_RECVDSTADDR)
1199 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
1202 CMSG_DATA(cmsg),
sizeof(
struct in_addr));
1211 if (cmsg->cmsg_level != -1 && cmsg->cmsg_type != -1) {
1212 coap_log_debug(
"cmsg_level = %d and cmsg_type = %d not supported - fix\n",
1213 cmsg->cmsg_level, cmsg->cmsg_type);
1227 packet->
length = (size_t)len;
1257#ifndef COAP_EPOLL_SUPPORT
1260 coap_log_emerg(
"coap_io_prepare_epoll() requires libcoap compiled for using epoll\n");
1264 unsigned int max_sockets =
sizeof(sockets)/
sizeof(sockets[0]);
1265 unsigned int num_sockets;
1266 unsigned int timeout;
1273 if (ctx->eptimerfd != -1) {
1274 struct itimerspec new_value;
1277 memset(&new_value, 0,
sizeof(new_value));
1286#ifdef COAP_DEBUG_WAKEUP_TIMES
1288 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
1291 ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
1294 "coap_io_prepare_epoll",
1309 unsigned int max_sockets,
1310 unsigned int *num_sockets,
1327 unsigned int max_sockets,
1328 unsigned int *num_sockets,
1334#if COAP_SERVER_SUPPORT
1335 int check_dtls_timeouts = 0;
1337#if defined(COAP_EPOLL_SUPPORT) || defined(WITH_LWIP) || defined(RIOT_VERSION)
1345#if COAP_SERVER_SUPPORT
1349#if COAP_ASYNC_SUPPORT
1351 timeout = coap_check_async(ctx, now);
1362 if (nextpdu && (timeout == 0 ||
1370 if (tls_timeout > 0) {
1375 if (timeout == 0 || tls_timeout - now < timeout)
1376 timeout = tls_timeout - now;
1378#if COAP_SERVER_SUPPORT
1380 check_dtls_timeouts = 1;
1384#if COAP_PROXY_SUPPORT
1386 if (timeout == 0 || s_timeout < timeout)
1387 timeout = s_timeout;
1390#if COAP_SERVER_SUPPORT
1400#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1402 if (*num_sockets < max_sockets)
1403 sockets[(*num_sockets)++] = &ep->
sock;
1417 s_timeout = (s->
last_rx_tx + session_timeout) - now;
1418 if (timeout == 0 || s_timeout < timeout)
1419 timeout = s_timeout;
1427 while (tls_timeout > 0 && tls_timeout <= now) {
1440 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1441 timeout = tls_timeout - now;
1446 if (timeout == 0 || s_timeout < timeout)
1447 timeout = s_timeout;
1453 if (timeout == 0 || s_timeout < timeout)
1454 timeout = s_timeout;
1457#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1459 if (*num_sockets < max_sockets)
1460 sockets[(*num_sockets)++] = &s->
sock;
1463#if COAP_Q_BLOCK_SUPPORT
1469 s_timeout = coap_block_check_q_block2_xmit(s, now);
1470 if (timeout == 0 || s_timeout < timeout)
1471 timeout = s_timeout;
1480#if COAP_CLIENT_SUPPORT
1497 if (timeout == 0 || s_timeout < timeout)
1498 timeout = s_timeout;
1501#if !COAP_DISABLE_TCP
1513 if ((timeout == 0 || s_timeout < timeout) && s_timeout != 0)
1514 timeout = s_timeout;
1524 while (tls_timeout > 0 && tls_timeout <= now) {
1536 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1537 timeout = tls_timeout - now;
1543 if (timeout == 0 || s_timeout < timeout)
1544 timeout = s_timeout;
1550 if (timeout == 0 || s_timeout < timeout)
1551 timeout = s_timeout;
1554#if COAP_Q_BLOCK_SUPPORT
1560 s_timeout = coap_block_check_q_block1_xmit(s, now);
1561 if (timeout == 0 || s_timeout < timeout)
1562 timeout = s_timeout;
1566#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
1571 if (*num_sockets < max_sockets)
1572 sockets[(*num_sockets)++] = &s->
sock;
1590 unsigned int *have_read_fds,
1591 unsigned int max_read_fds,
1593 unsigned int *have_write_fds,
1594 unsigned int max_write_fds,
1595 unsigned int *rem_timeout_ms) {
1600 have_write_fds, max_write_fds, rem_timeout_ms);
1605#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1608 unsigned int max_this_fds) {
1609 if (*have_this_fds < max_this_fds) {
1610 this_fds[(*have_this_fds)++] = fd;
1613 coap_log_warn(
"coap_io_get_fds: Insufficient space for new fd (%u >= %u)\n", *have_this_fds,
1625 unsigned int *have_read_fds,
1626 unsigned int max_read_fds,
1628 unsigned int *have_write_fds,
1629 unsigned int max_write_fds,
1630 unsigned int *rem_timeout_ms) {
1632 *have_write_fds = 0;
1634#ifdef COAP_EPOLL_SUPPORT
1636 (void)max_write_fds;;
1638 if (!
coap_add_fd(ctx->epfd, read_fds, have_read_fds, max_read_fds))
1641 *rem_timeout_ms = 0;
1646 unsigned int timeout_ms;
1647#if COAP_SERVER_SUPPORT
1672#if COAP_CLIENT_SUPPORT
1689 *rem_timeout_ms = timeout_ms;
1703 unsigned int *have_read_fds,
1704 unsigned int max_read_fds,
1706 unsigned int *have_write_fds,
1707 unsigned int max_write_fds,
1708 unsigned int *rem_timeout_ms) {
1713 (void)max_write_fds;
1716 *have_write_fds = 0;
1717 *rem_timeout_ms = 0;
1724#if !defined(WITH_LWIP) && !defined(CONTIKI) && !defined(RIOT_VERSION)
1742 int enfds, fd_set *ereadfds, fd_set *ewritefds,
1743 fd_set *eexceptfds) {
1755 int enfds, fd_set *ereadfds, fd_set *ewritefds,
1756 fd_set *eexceptfds) {
1759 unsigned int timeout;
1760#ifndef COAP_EPOLL_SUPPORT
1769#ifndef COAP_EPOLL_SUPPORT
1801#if !COAP_DISABLE_TCP
1815 }
else if (timeout == 0 && timeout_ms ==
COAP_IO_WAIT) {
1818 if (timeout == 0 || (timeout_ms !=
COAP_IO_WAIT && timeout_ms < timeout))
1819 timeout = timeout_ms;
1820 tv.tv_usec = (timeout % 1000) * 1000;
1821 tv.tv_sec = (long)(timeout / 1000);
1828 timeout > 0 ? &tv : NULL);
1834 coap_win_error_to_errno();
1836 if (errno != EINTR) {
1862#if !COAP_DISABLE_TCP
1901 }
else if (timeout == 0 && timeout_ms ==
COAP_IO_WAIT) {
1909 if (timeout == 0 || (timeout_ms !=
COAP_IO_WAIT && timeout_ms < timeout))
1910 etimeout = timeout_ms;
1925 if (errno != EINTR) {
1926 coap_log_err(
"epoll_wait: unexpected error: %s (%d)\n",
1937 if (errno != EINTR) {
1938 coap_log_err(
"epoll_wait: unexpected error: %s (%d)\n",
1960#if COAP_SERVER_SUPPORT
1964#if COAP_ASYNC_SUPPORT
1967 coap_check_async(ctx, now);
1992#if COAP_SERVER_SUPPORT
2004#if COAP_SERVER_SUPPORT
2005 LL_FOREACH(context->
endpoint, ep) {
2016#if COAP_CLIENT_SUPPORT
2031 return strerror(error);
2047#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
2057 return socket->
flags;
2062 socket->
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.
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_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
int coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now, coap_tick_t *tim_rem)
Idle timeout inactive proxy sessions as well as return in tim_rem the time to remaining to timeout th...
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_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.
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 *socket, 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 *socket)
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.
COAP_API coap_socket_flags_t coap_socket_get_flags(coap_socket_t *socket)
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.
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_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(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_lock_check_locked(c)
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(...)
#define COAP_INVALID_MID
Indicates an invalid message id.
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 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
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
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
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
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