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);
212 "coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
215 switch (listen_addr->
addr.
sa.sa_family) {
219 "coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
226 "coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
230 "coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
232 setsockopt(
sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
240 listen_addr->
addr.
sa.sa_family == AF_INET ?
241 sizeof(
struct sockaddr_in) :
248 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
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);
308 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
316 if (local_if && local_if->
addr.
sa.sa_family) {
320 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
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) {
455 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
463 listen_addr->
addr.
sa.sa_family == AF_INET ?
464 sizeof(
struct sockaddr_in) :
471 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
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);
572 "coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
580 if (local_if && local_if->
addr.
sa.sa_family) {
583 "coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
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
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) )
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) {
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;
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;
1570 int etimeout = timeout;
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) {