15 #ifdef HAVE_SYS_SELECT_H 16 # include <sys/select.h> 18 #ifdef HAVE_SYS_SOCKET_H 19 # include <sys/socket.h> 20 # define OPTVAL_T(t) (t) 21 # define OPTVAL_GT(t) (t) 23 #ifdef HAVE_SYS_IOCTL_H 24 #include <sys/ioctl.h> 26 #ifdef HAVE_NETINET_IN_H 27 # include <netinet/in.h> 29 #ifdef HAVE_WS2TCPIP_H 31 # define OPTVAL_T(t) (const char*)(t) 32 # define OPTVAL_GT(t) (char*)(t) 34 # define CMSG_DATA WSA_CMSG_DATA 43 #ifdef COAP_EPOLL_SUPPORT 44 #include <sys/epoll.h> 45 #include <sys/timerfd.h> 52 #if !defined(WITH_CONTIKI) 54 #if defined(IP_PKTINFO) 55 # define GEN_IP_PKTINFO IP_PKTINFO 56 #elif defined(IP_RECVDSTADDR) 57 # define GEN_IP_PKTINFO IP_RECVDSTADDR 59 # error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS." 63 #ifdef IPV6_RECVPKTINFO 64 # define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO 65 #elif defined(IPV6_PKTINFO) 66 # define GEN_IPV6_PKTINFO IPV6_PKTINFO 68 # error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS." 75 static int ep_initialized = 0;
98 sock->conn = udp_new(NULL, 0, NULL);
106 uip_ipaddr_copy(&bound_addr->
addr, &listen_addr->
addr);
107 bound_addr->port = listen_addr->port;
108 udp_bind((
struct uip_udp_conn *)sock->conn, bound_addr->port);
166 uip_udp_remove((
struct uip_udp_conn *)sock->conn);
193 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_DGRAM, 0);
210 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
212 "coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
215 switch (listen_addr->
addr.
sa.sa_family) {
217 if (setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
219 "coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
224 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
226 "coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
228 if (setsockopt(sock->
fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
230 "coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
232 setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
239 if (bind(sock->
fd, &listen_addr->
addr.
sa,
240 listen_addr->
addr.
sa.sa_family == AF_INET ?
241 sizeof(
struct sockaddr_in) :
248 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
249 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
251 "coap_socket_bind_udp: getsockname: %s\n",
278 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
282 "coap_socket_connect_tcp1: socket: %s\n",
293 "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
297 switch (server->
addr.
sa.sa_family) {
299 if (connect_addr.
addr.
sin.sin_port == 0)
300 connect_addr.
addr.
sin.sin_port = htons(default_port);
303 if (connect_addr.
addr.
sin6.sin6_port == 0)
304 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
306 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
308 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
316 if (local_if && local_if->
addr.
sa.sa_family) {
318 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
320 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
322 if (bind(sock->
fd, &local_if->
addr.
sa,
323 local_if->
addr.
sa.sa_family == AF_INET ?
324 sizeof(
struct sockaddr_in) :
331 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
336 if (WSAGetLastError() == WSAEWOULDBLOCK) {
338 if (errno == EINPROGRESS) {
377 int optlen = (int)
sizeof( error );
379 socklen_t optlen = (socklen_t)
sizeof( error );
384 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
392 "coap_socket_finish_connect_tcp: connect failed: %s\n",
420 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
436 if (setsockopt (sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
439 "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
442 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
445 "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
448 switch (listen_addr->
addr.
sa.sa_family) {
453 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
455 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
462 if (bind(sock->
fd, &listen_addr->
addr.
sa,
463 listen_addr->
addr.
sa.sa_family == AF_INET ?
464 sizeof(
struct sockaddr_in) :
471 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
472 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
504 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
512 if (getsockname( new_client->
fd, &local_addr->
addr.
sa, &local_addr->
size) < 0)
544 sock->
fd = socket(connect_addr.
addr.
sa.sa_family, SOCK_DGRAM, 0);
561 switch (connect_addr.
addr.
sa.sa_family) {
563 if (connect_addr.
addr.
sin.sin_port == 0)
564 connect_addr.
addr.
sin.sin_port = htons(default_port);
567 if (connect_addr.
addr.
sin6.sin6_port == 0)
568 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
570 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
572 "coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
580 if (local_if && local_if->
addr.
sa.sa_family) {
581 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
583 "coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
585 if (bind(sock->
fd, &local_if->
addr.
sa,
586 local_if->
addr.
sa.sa_family == AF_INET ?
587 sizeof(
struct sockaddr_in) :
599 "coap_socket_connect_udp: getsockname for multicast socket: %s\n",
633 #ifdef COAP_EPOLL_SUPPORT 636 if (context != NULL) {
638 struct epoll_event event;
641 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, sock->
fd, &event);
644 "%s: epoll_ctl DEL failed: %s (%d)\n",
658 #ifdef COAP_EPOLL_SUPPORT 665 struct epoll_event event;
676 event.events = events;
677 event.data.ptr = sock;
679 ret = epoll_ctl(context->epfd, EPOLL_CTL_MOD, sock->
fd, &event);
682 "%s: epoll_ctl MOD failed: %s (%d)\n",
695 r = send(sock->
fd, (
const char *)data, (
int)data_len, 0);
697 r = send(sock->
fd, data, data_len, 0);
701 if (WSAGetLastError() == WSAEWOULDBLOCK) {
702 #elif EAGAIN != EWOULDBLOCK 703 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
705 if (errno==EAGAIN || errno == EINTR) {
708 #ifdef COAP_EPOLL_SUPPORT 721 if (r < (ssize_t)data_len) {
723 #ifdef COAP_EPOLL_SUPPORT 742 r = recv(sock->
fd, (
char *)data, (
int)data_len, 0);
744 r = recv(sock->
fd, data, data_len, 0);
753 error = WSAGetLastError();
754 if (error == WSAEWOULDBLOCK) {
755 #elif EAGAIN != EWOULDBLOCK 756 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
758 if (errno==EAGAIN || errno == EINTR) {
763 if (error != WSAECONNRESET)
765 if (errno != ECONNRESET)
771 if (r < (ssize_t)data_len)
778 #if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) ) 789 struct in_addr ipi_spec_dst;
790 struct in_addr ipi_addr;
794 #if !defined(WITH_CONTIKI) && !defined(SOL_IP) 796 #define SOL_IP IPPROTO_IP 800 #define UNUSED_PARAM __attribute__ ((unused)) 807 static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
809 #define msghdr _WSAMSG 810 #define msg_name name 811 #define msg_namelen namelen 812 #define msg_iov lpBuffers 813 #define msg_iovlen dwBufferCount 814 #define msg_control Control.buf 815 #define msg_controllen Control.len 816 #define iovec _WSABUF 819 #define iov_len_t u_long 821 #define CMSG_DATA WSA_CMSG_DATA 822 #define ipi_spec_dst ipi_addr 824 #define iov_len_t size_t 829 ssize_t bytes_written = 0;
832 bytes_written = (ssize_t)datalen;
836 bytes_written = send(sock->
fd, (
const char *)data, (
int)datalen, 0);
838 bytes_written = send(sock->
fd, data, datalen, 0);
844 DWORD dwNumberOfBytesSent = 0;
847 #ifdef HAVE_STRUCT_CMSGHDR 856 memcpy (&iov[0].iov_base, &data,
sizeof (iov[0].iov_base));
859 memset(buf, 0,
sizeof (buf));
861 memset(&mhdr, 0,
sizeof(
struct msghdr));
862 memcpy (&mhdr.msg_name, &addr, sizeof (mhdr.msg_name));
873 struct cmsghdr *cmsg;
876 #if defined(IP_PKTINFO) 878 mhdr.msg_control = buf;
879 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
881 cmsg = CMSG_FIRSTHDR(&mhdr);
882 cmsg->cmsg_level =
SOL_IP;
883 cmsg->cmsg_type = IP_PKTINFO;
884 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
886 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
892 #elif defined(IP_SENDSRCADDR) 893 mhdr.msg_control = buf;
894 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
896 cmsg = CMSG_FIRSTHDR(&mhdr);
897 cmsg->cmsg_level = IPPROTO_IP;
898 cmsg->cmsg_type = IP_SENDSRCADDR;
899 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
901 memcpy(CMSG_DATA(cmsg),
903 sizeof(
struct in_addr));
907 mhdr.msg_control = buf;
908 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
910 cmsg = CMSG_FIRSTHDR(&mhdr);
911 cmsg->cmsg_level = IPPROTO_IPV6;
912 cmsg->cmsg_type = IPV6_PKTINFO;
913 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
926 #if defined(IP_PKTINFO) 927 struct cmsghdr *cmsg;
930 mhdr.msg_control = buf;
931 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
933 cmsg = CMSG_FIRSTHDR(&mhdr);
934 cmsg->cmsg_level =
SOL_IP;
935 cmsg->cmsg_type = IP_PKTINFO;
936 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
938 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
944 #elif defined(IP_SENDSRCADDR) 945 struct cmsghdr *cmsg;
946 mhdr.msg_control = buf;
947 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
949 cmsg = CMSG_FIRSTHDR(&mhdr);
950 cmsg->cmsg_level = IPPROTO_IP;
951 cmsg->cmsg_type = IP_SENDSRCADDR;
952 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
954 memcpy(CMSG_DATA(cmsg),
956 sizeof(
struct in_addr));
968 r = WSASendMsg(sock->
fd, &mhdr, 0 , &dwNumberOfBytesSent, NULL , NULL );
970 bytes_written = (ssize_t)dwNumberOfBytesSent;
974 #ifdef HAVE_STRUCT_CMSGHDR 975 bytes_written = sendmsg(sock->
fd, &mhdr, 0);
977 bytes_written = sendto(sock->
fd, data, datalen, 0,
987 uip_udp_packet_sendto((
struct uip_udp_conn *)sock->conn, data, datalen,
989 bytes_written = datalen;
993 if (bytes_written < 0)
996 return bytes_written;
999 #define SIN6(A) ((struct sockaddr_in6 *)(A)) 1004 *length = packet->
length;
1021 #ifndef WITH_CONTIKI 1030 if (WSAGetLastError() == WSAECONNRESET) {
1032 if (errno == ECONNREFUSED) {
1040 }
else if (len > 0) {
1041 packet->
length = (size_t)len;
1046 DWORD dwNumberOfBytesRecvd = 0;
1049 #if !defined(WITH_CONTIKI) 1050 #ifdef HAVE_STRUCT_CMSGHDR 1053 struct cmsghdr *cmsg;
1055 struct iovec iov[1];
1057 iov[0].iov_base = packet->
payload;
1060 memset(&mhdr, 0,
sizeof(
struct msghdr));
1066 mhdr.msg_iovlen = 1;
1068 mhdr.msg_control = buf;
1069 mhdr.msg_controllen =
sizeof(buf);
1072 cmsg = (
struct cmsghdr *)buf;
1073 cmsg->cmsg_len = CMSG_LEN(
sizeof(buf));
1074 cmsg->cmsg_level = -1;
1075 cmsg->cmsg_type = -1;
1078 if (!lpWSARecvMsg) {
1079 GUID wsaid = WSAID_WSARECVMSG;
1080 DWORD cbBytesReturned = 0;
1081 if (WSAIoctl(sock->
fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &wsaid,
sizeof(wsaid), &lpWSARecvMsg,
sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
1086 r = lpWSARecvMsg(sock->
fd, &mhdr, &dwNumberOfBytesRecvd, NULL , NULL );
1088 len = (ssize_t)dwNumberOfBytesRecvd;
1090 len = recvmsg(sock->
fd, &mhdr, 0);
1101 if (WSAGetLastError() == WSAECONNRESET) {
1103 if (errno == ECONNREFUSED) {
1111 #ifdef HAVE_STRUCT_CMSGHDR 1115 packet->
length = (size_t)len;
1119 for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
1122 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
1127 u.c = CMSG_DATA(cmsg);
1128 packet->
ifindex = (int)(u.p->ipi6_ifindex);
1130 &u.p->ipi6_addr,
sizeof(
struct in6_addr));
1136 #if defined(IP_PKTINFO) 1137 if (cmsg->cmsg_level ==
SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
1142 u.c = CMSG_DATA(cmsg);
1143 packet->
ifindex = u.p->ipi_ifindex;
1149 &u.p->ipi_addr,
sizeof(
struct in_addr));
1152 &u.p->ipi_addr,
sizeof(
struct in_addr));
1157 #elif defined(IP_RECVDSTADDR) 1158 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
1161 CMSG_DATA(cmsg),
sizeof(
struct in_addr));
1169 if (cmsg->cmsg_level != -1 && cmsg->cmsg_type != -1) {
1171 "cmsg_level = %d and cmsg_type = %d not supported - fix\n",
1172 cmsg->cmsg_level, cmsg->cmsg_type);
1186 packet->
length = (size_t)len;
1198 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) 1199 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN]) 1201 if (uip_newdata()) {
1204 uip_ipaddr_copy(&(packet)->addr_info.local.addr, &UIP_IP_BUF->destipaddr);
1207 len = uip_datalen();
1215 ((
char *)uip_appdata)[len] = 0;
1217 #ifndef INET6_ADDRSTRLEN 1218 #define INET6_ADDRSTRLEN 40 1229 memcpy(&packet->
payload, uip_appdata, len);
1235 #ifndef WITH_CONTIKI 1241 #if !defined(WITH_CONTIKI) 1249 #ifndef COAP_EPOLL_SUPPORT 1253 "coap_io_prepare_epoll() requires libcoap compiled for using epoll\n");
1257 unsigned int max_sockets =
sizeof(sockets)/
sizeof(sockets[0]);
1258 unsigned int num_sockets;
1259 unsigned int timeout;
1262 timeout =
coap_write(ctx, sockets, max_sockets, &num_sockets, now);
1264 ctx->next_timeout = timeout ? now + timeout : 0;
1265 if (ctx->eptimerfd != -1) {
1266 struct itimerspec new_value;
1269 memset(&new_value, 0,
sizeof(new_value));
1271 if (ctx->next_timeout != 0 && ctx->next_timeout > now) {
1272 coap_tick_t rem_timeout = ctx->next_timeout - now;
1279 ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
1282 "%s: timerfd_settime failed: %s (%d)\n",
1283 "coap_io_prepare_epoll",
1291 #if !defined(WITH_CONTIKI) 1296 unsigned int max_sockets,
1297 unsigned int *num_sockets,
1305 #ifdef COAP_EPOLL_SUPPORT 1321 #ifndef COAP_EPOLL_SUPPORT 1323 if (*num_sockets < max_sockets)
1324 sockets[(*num_sockets)++] = &ep->
sock;
1336 if (timeout == 0 || s_timeout < timeout)
1337 timeout = s_timeout;
1339 #ifndef COAP_EPOLL_SUPPORT 1341 if (*num_sockets < max_sockets)
1342 sockets[(*num_sockets)++] = &s->
sock;
1369 if (timeout == 0 || s_timeout < timeout)
1370 timeout = s_timeout;
1390 if (timeout == 0 || s_timeout < timeout)
1391 timeout = s_timeout;
1394 #ifndef COAP_EPOLL_SUPPORT 1396 if (*num_sockets < max_sockets)
1397 sockets[(*num_sockets)++] = &s->
sock;
1409 if (nextpdu && (timeout == 0 || nextpdu->
t - ( now - ctx->
sendqueue_basetime ) < timeout))
1415 if (tls_timeout > 0) {
1416 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
1417 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
1419 (
int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
1420 if (timeout == 0 || tls_timeout - now < timeout)
1421 timeout = tls_timeout - now;
1429 while (tls_timeout > 0 && tls_timeout <= now) {
1440 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1441 timeout = tls_timeout - now;
1449 while (tls_timeout > 0 && tls_timeout <= now) {
1459 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1460 timeout = tls_timeout - now;
1466 return (
unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
1471 #if COAP_CONSTRAINED_STACK 1472 static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER;
1473 # ifndef COAP_EPOLL_SUPPORT 1474 static fd_set readfds, writefds, exceptfds;
1476 unsigned int num_sockets = 0;
1479 # ifndef COAP_EPOLL_SUPPORT 1480 fd_set readfds, writefds, exceptfds;
1482 unsigned int num_sockets = 0;
1487 unsigned int timeout;
1488 #ifndef COAP_EPOLL_SUPPORT 1494 #if COAP_CONSTRAINED_STACK 1495 coap_mutex_lock(&static_mutex);
1500 #ifndef COAP_EPOLL_SUPPORT 1501 timeout =
coap_write(ctx, sockets, (
unsigned int)(
sizeof(sockets) /
sizeof(sockets[0])), &num_sockets, before);
1502 if (timeout == 0 || timeout_ms < timeout)
1503 timeout = timeout_ms;
1507 FD_ZERO(&exceptfds);
1508 for (i = 0; i < num_sockets; i++) {
1509 if (sockets[i]->fd + 1 > nfds)
1510 nfds = sockets[i]->
fd + 1;
1512 FD_SET(sockets[i]->fd, &readfds);
1514 FD_SET(sockets[i]->fd, &writefds);
1516 FD_SET(sockets[i]->fd, &readfds);
1518 FD_SET(sockets[i]->fd, &writefds);
1519 FD_SET(sockets[i]->fd, &exceptfds);
1523 if ( timeout > 0 ) {
1526 tv.tv_usec = (timeout % 1000) * 1000;
1527 tv.tv_sec = (long)(timeout / 1000);
1530 result = select(nfds, &readfds, &writefds, &exceptfds, timeout > 0 ? &tv : NULL);
1534 if (WSAGetLastError() != WSAEINVAL) {
1536 if (errno != EINTR) {
1539 #if COAP_CONSTRAINED_STACK 1540 coap_mutex_unlock(&static_mutex);
1547 for (i = 0; i < num_sockets; i++) {
1554 if ((sockets[i]->flags &
COAP_SOCKET_WANT_CONNECT) && (FD_ISSET(sockets[i]->fd, &writefds) || FD_ISSET(sockets[i]->fd, &exceptfds)))
1565 if (timeout == 0 || timeout_ms < timeout)
1566 timeout = timeout_ms;
1569 struct epoll_event events[COAP_MAX_EPOLL_EVENTS];
1570 int etimeout = timeout;
1578 nfds = epoll_wait(ctx->epfd, events, COAP_MAX_EPOLL_EVENTS, etimeout);
1580 if (errno != EINTR) {
1602 #if COAP_CONSTRAINED_STACK 1603 coap_mutex_unlock(&static_mutex);
1624 unsigned int max_sockets,
1625 unsigned int *num_sockets,
1635 static char szError[256];
1636 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szError, (DWORD)
sizeof(szError), NULL) == 0)
1637 strcpy(szError,
"Unknown error");
1645 #ifndef WITH_CONTIKI 1647 return strerror(error);
1652 return strerror(errno);
1658 const uint8_t *data,
size_t data_len) {
#define LL_FOREACH(head, el)
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
#define COAP_RXBUFFER_SIZE
coap_address_t remote
remote address and port
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
#define COAP_SESSION_TYPE_CLIENT
coap_session_type_t values
void coap_socket_close(coap_socket_t *sock)
struct coap_context_t * context
session's context
void * tls
security parameters
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...
coap_endpoint_t * endpoint
the endpoints used for listening
#define COAP_SOCKET_CONNECTED
the socket is connected
multi-purpose address abstraction
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
unsigned ref
reference count from queues
void coap_free_endpoint(coap_endpoint_t *ep)
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
int ifindex
the interface index
coap_address_t local
local address and port
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
#define COAP_SOCKET_ERROR
struct in_addr ipi_spec_dst
static const char * coap_socket_format_errno(int error)
int coap_socket_connect_tcp1(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)
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
#define COAP_INVALID_SOCKET
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
struct coap_context_t * context
endpoint's context
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Abstraction of virtual endpoint that can be attached to coap_context_t.
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_socket_strerror(void)
struct coap_endpoint_t * endpoint
#define COAP_SESSION_STATE_ESTABLISHED
#define SESSIONS_ITER_SAFE(e, el, rtmp)
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
int coap_run_once(coap_context_t *ctx, unsigned timeout_ms)
struct coap_endpoint_t * coap_malloc_endpoint(void)
coap_tick_t t
when to send PDU for the next time
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
struct in6_addr ipi6_addr
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
#define COAP_INVALID_TID
Indicates an invalid transaction id.
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
coap_proto_t proto
protocol used
coap_addr_tuple_t addr_info
key: remote/local address info
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
void coap_session_free(coap_session_t *session)
coap_socket_t sock
socket object for the session, if any
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
#define COAP_RUN_NONBLOCK
coap_proto_t proto
protocol used on this interface
coap_session_type_t type
client or server side socket
coap_session_state_t state
current state of relationaship with peer
#define COAP_SESSION_TYPE_SERVER
server-side
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
#define COAP_DEFAULT_SESSION_TIMEOUT
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...
int ifindex
interface index
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
void coap_io_do_events(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
size_t length
length of payload
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_PROTO_RELIABLE(p)
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
void coap_read(coap_context_t *ctx, coap_tick_t now)
For applications with their own message loop, reads all data from the network.
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
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_dtls_handle_timeout(coap_session_t *session UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED, coap_tick_t now UNUSED)
coap_log_t coap_get_log_level(void)
Get the current logging level.
struct coap_session_t * sessions
hash table or list of active sessions
coap_socket_flags_t flags
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
coap_session_t * sessions
client sessions
#define coap_log(level,...)
Logging function.
coap_addr_tuple_t addr_info
local and remote addresses
union coap_address_t::@0 addr
coap_socket_t sock
socket object for the interface, if any
unsigned int coap_write(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
For applications with their own message loop, send all pending retransmits and return the list of soc...
unsigned int ipi6_ifindex
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)
socklen_t size
size of addr
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED)
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
#define COAP_SESSION_STATE_NONE
coap_session_state_t values
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
#define SESSIONS_ITER(e, el, rtmp)
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
The CoAP stack's global state is stored in a coap_context_t object.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
struct coap_session_t * session
#define COAP_MAX_EPOLL_EVENTS
Pulls together all the internal only header files.
#define COAP_SESSION_STATE_CSM
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.