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) && !defined(RIOT_VERSION) 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);
101 coap_log(COAP_LOG_WARNING,
"coap_socket_bind_udp");
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);
195 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_DGRAM, 0);
213 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
215 "coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
219 switch (listen_addr->
addr.
sa.sa_family) {
221 if (setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
223 "coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
228 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
230 "coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
232 if (setsockopt(sock->
fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
234 "coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
236 setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
239 coap_log(COAP_LOG_ALERT,
"coap_socket_bind_udp: unsupported sa_family\n");
245 coap_log(COAP_LOG_WARNING,
"coap_socket_bind_udp: bind: %s\n",
250 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
251 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
253 "coap_socket_bind_udp: getsockname: %s\n",
283 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
287 "coap_socket_connect_tcp1: socket: %s\n",
299 "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
304 switch (server->
addr.
sa.sa_family) {
306 if (connect_addr.
addr.
sin.sin_port == 0)
307 connect_addr.
addr.
sin.sin_port = htons(default_port);
310 if (connect_addr.
addr.
sin6.sin6_port == 0)
311 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
314 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
316 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
321 coap_log(COAP_LOG_ALERT,
"coap_socket_connect_tcp1: unsupported sa_family\n");
325 if (local_if && local_if->
addr.
sa.sa_family) {
327 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
329 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
332 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp1: bind: %s\n",
337 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
342 if (WSAGetLastError() == WSAEWOULDBLOCK) {
344 if (errno == EINPROGRESS) {
354 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp1: connect: %s\n",
360 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp1: getsockname: %s\n",
365 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp1: getpeername: %s\n",
383 int optlen = (int)
sizeof( error );
385 socklen_t optlen = (socklen_t)
sizeof( error );
390 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
392 coap_log(COAP_LOG_WARNING,
"coap_socket_finish_connect_tcp: getsockopt: %s\n",
398 "coap_socket_finish_connect_tcp: connect failed: %s\n",
405 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp: getsockname: %s\n",
410 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_tcp: getpeername: %s\n",
429 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
432 coap_log(COAP_LOG_WARNING,
"coap_socket_bind_tcp: socket: %s\n",
443 coap_log(COAP_LOG_WARNING,
"coap_socket_bind_tcp: ioctl FIONBIO: %s\n",
447 if (setsockopt (sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
450 "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
453 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
456 "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
459 switch (listen_addr->
addr.
sa.sa_family) {
465 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
467 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
472 coap_log(COAP_LOG_ALERT,
"coap_socket_bind_tcp: unsupported sa_family\n");
476 coap_log(COAP_LOG_ALERT,
"coap_socket_bind_tcp: bind: %s\n",
481 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
482 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
483 coap_log(COAP_LOG_WARNING,
"coap_socket_bind_tcp: getsockname: %s\n",
489 coap_log(COAP_LOG_ALERT,
"coap_socket_bind_tcp: listen: %s\n",
516 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
519 coap_log(COAP_LOG_WARNING,
"coap_socket_accept_tcp: accept: %s\n",
524 if (getsockname( new_client->
fd, &local_addr->
addr.
sa, &local_addr->
size) < 0)
525 coap_log(COAP_LOG_WARNING,
"coap_socket_accept_tcp: getsockname: %s\n",
534 coap_log(COAP_LOG_WARNING,
"coap_socket_accept_tcp: ioctl FIONBIO: %s\n",
560 sock->
fd = socket(connect_addr.
addr.
sa.sa_family, SOCK_DGRAM, 0);
563 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: socket: %s\n",
574 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: ioctl FIONBIO: %s\n",
579 switch (connect_addr.
addr.
sa.sa_family) {
581 if (connect_addr.
addr.
sin.sin_port == 0)
582 connect_addr.
addr.
sin.sin_port = htons(default_port);
585 if (connect_addr.
addr.
sin6.sin6_port == 0)
586 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
589 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
591 "coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
596 coap_log(COAP_LOG_ALERT,
"coap_socket_connect_udp: unsupported sa_family\n");
600 if (local_if && local_if->
addr.
sa.sa_family) {
602 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
604 "coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
608 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: bind: %s\n",
618 "coap_socket_connect_udp: getsockname for multicast socket: %s\n",
627 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: connect: %s\n",
633 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: getsockname: %s\n",
638 coap_log(COAP_LOG_WARNING,
"coap_socket_connect_udp: getpeername: %s\n",
652 #ifdef COAP_EPOLL_SUPPORT 655 if (context != NULL) {
657 struct epoll_event event;
660 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, sock->
fd, &event);
663 "%s: epoll_ctl DEL failed: %s (%d)\n",
677 #ifdef COAP_EPOLL_SUPPORT 684 struct epoll_event event;
695 event.events = events;
696 event.data.ptr = sock;
698 ret = epoll_ctl(context->epfd, EPOLL_CTL_MOD, sock->
fd, &event);
701 "%s: epoll_ctl MOD failed: %s (%d)\n",
714 r = send(sock->
fd, (
const char *)data, (
int)data_len, 0);
716 r = send(sock->
fd, data, data_len, 0);
720 if (WSAGetLastError() == WSAEWOULDBLOCK) {
721 #elif EAGAIN != EWOULDBLOCK 722 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
724 if (errno==EAGAIN || errno == EINTR) {
727 #ifdef COAP_EPOLL_SUPPORT 736 coap_log(COAP_LOG_WARNING,
"coap_socket_write: send: %s\n",
740 if (r < (ssize_t)data_len) {
742 #ifdef COAP_EPOLL_SUPPORT 761 r = recv(sock->
fd, (
char *)data, (
int)data_len, 0);
763 r = recv(sock->
fd, data, data_len, 0);
772 error = WSAGetLastError();
773 if (error == WSAEWOULDBLOCK) {
774 #elif EAGAIN != EWOULDBLOCK 775 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
777 if (errno==EAGAIN || errno == EINTR) {
782 if (error != WSAECONNRESET)
784 if (errno != ECONNRESET)
786 coap_log(COAP_LOG_WARNING,
"coap_socket_read: recv: %s\n",
790 if (r < (ssize_t)data_len)
797 #if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) ) 802 struct in6_addr ipi6_addr;
808 struct in_addr ipi_spec_dst;
809 struct in_addr ipi_addr;
813 #if !defined(WITH_CONTIKI) && !defined(SOL_IP) 815 #define SOL_IP IPPROTO_IP 819 #define UNUSED_PARAM __attribute__ ((unused)) 826 static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
828 #define msghdr _WSAMSG 829 #define msg_name name 830 #define msg_namelen namelen 831 #define msg_iov lpBuffers 832 #define msg_iovlen dwBufferCount 833 #define msg_control Control.buf 834 #define msg_controllen Control.len 835 #define iovec _WSABUF 838 #define iov_len_t u_long 840 #define CMSG_DATA WSA_CMSG_DATA 841 #define ipi_spec_dst ipi_addr 843 #define iov_len_t size_t 849 ssize_t bytes_written = 0;
852 bytes_written = (ssize_t)datalen;
856 bytes_written = send(sock->
fd, (
const char *)data, (
int)datalen, 0);
858 bytes_written = send(sock->
fd, data, datalen, 0);
863 DWORD dwNumberOfBytesSent = 0;
866 #ifdef HAVE_STRUCT_CMSGHDR 875 memcpy (&iov[0].iov_base, &data,
sizeof (iov[0].iov_base));
878 memset(buf, 0,
sizeof (buf));
880 memset(&mhdr, 0,
sizeof(
struct msghdr));
881 memcpy (&mhdr.msg_name, &addr, sizeof (mhdr.msg_name));
892 struct cmsghdr *cmsg;
895 #if defined(IP_PKTINFO) 897 mhdr.msg_control = buf;
898 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
900 cmsg = CMSG_FIRSTHDR(&mhdr);
901 cmsg->cmsg_level =
SOL_IP;
902 cmsg->cmsg_type = IP_PKTINFO;
903 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
905 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
911 #elif defined(IP_SENDSRCADDR) 912 mhdr.msg_control = buf;
913 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
915 cmsg = CMSG_FIRSTHDR(&mhdr);
916 cmsg->cmsg_level = IPPROTO_IP;
917 cmsg->cmsg_type = IP_SENDSRCADDR;
918 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
920 memcpy(CMSG_DATA(cmsg),
922 sizeof(
struct in_addr));
926 mhdr.msg_control = buf;
927 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
929 cmsg = CMSG_FIRSTHDR(&mhdr);
930 cmsg->cmsg_level = IPPROTO_IPV6;
931 cmsg->cmsg_type = IPV6_PKTINFO;
932 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
945 #if defined(IP_PKTINFO) 946 struct cmsghdr *cmsg;
949 mhdr.msg_control = buf;
950 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
952 cmsg = CMSG_FIRSTHDR(&mhdr);
953 cmsg->cmsg_level =
SOL_IP;
954 cmsg->cmsg_type = IP_PKTINFO;
955 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
957 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
963 #elif defined(IP_SENDSRCADDR) 964 struct cmsghdr *cmsg;
965 mhdr.msg_control = buf;
966 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
968 cmsg = CMSG_FIRSTHDR(&mhdr);
969 cmsg->cmsg_level = IPPROTO_IP;
970 cmsg->cmsg_type = IP_SENDSRCADDR;
971 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
973 memcpy(CMSG_DATA(cmsg),
975 sizeof(
struct in_addr));
981 coap_log(COAP_LOG_WARNING,
"protocol not supported\n");
987 r = WSASendMsg(sock->
fd, &mhdr, 0 , &dwNumberOfBytesSent, NULL , NULL );
989 bytes_written = (ssize_t)dwNumberOfBytesSent;
993 #ifdef HAVE_STRUCT_CMSGHDR 994 bytes_written = sendmsg(sock->
fd, &mhdr, 0);
996 bytes_written = sendto(sock->
fd, data, datalen, 0,
1001 #if defined(WITH_CONTIKI) 1006 uip_udp_packet_sendto((
struct uip_udp_conn *)sock->conn, data, datalen,
1008 bytes_written = datalen;
1009 #elif defined(RIOT_VERSION) 1010 bytes_written = sendto(sock->
fd, data, datalen, 0,
1011 &session->remote_addr.addr.sa,
1012 session->remote_addr.size);
1016 if (bytes_written < 0)
1019 return bytes_written;
1023 #define SIN6(A) ((struct sockaddr_in6 *)(A)) 1028 *length = packet->
length;
1031 #ifndef RIOT_VERSION 1046 #if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) 1055 if (WSAGetLastError() == WSAECONNRESET) {
1057 if (errno == ECONNREFUSED) {
1060 coap_log(COAP_LOG_WARNING,
"coap_network_read: unreachable\n");
1065 }
else if (len > 0) {
1066 packet->
length = (size_t)len;
1071 DWORD dwNumberOfBytesRecvd = 0;
1074 #if !defined(WITH_CONTIKI) 1075 #ifdef HAVE_STRUCT_CMSGHDR 1078 struct cmsghdr *cmsg;
1080 struct iovec iov[1];
1082 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,
sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
1107 coap_log(COAP_LOG_WARNING,
"coap_network_read: no WSARecvMsg\n");
1111 r = lpWSARecvMsg(sock->
fd, &mhdr, &dwNumberOfBytesRecvd, NULL , NULL );
1113 len = (ssize_t)dwNumberOfBytesRecvd;
1115 len = recvmsg(sock->
fd, &mhdr, 0);
1126 if (WSAGetLastError() == WSAECONNRESET) {
1128 if (errno == ECONNREFUSED) {
1136 #ifdef HAVE_STRUCT_CMSGHDR 1140 packet->
length = (size_t)len;
1144 for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
1147 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
1152 u.c = CMSG_DATA(cmsg);
1153 packet->
ifindex = (int)(u.p->ipi6_ifindex);
1155 &u.p->ipi6_addr,
sizeof(
struct in6_addr));
1161 #if defined(IP_PKTINFO) 1162 if (cmsg->cmsg_level ==
SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
1167 u.c = CMSG_DATA(cmsg);
1168 packet->
ifindex = u.p->ipi_ifindex;
1174 &u.p->ipi_addr,
sizeof(
struct in_addr));
1177 &u.p->ipi_addr,
sizeof(
struct in_addr));
1182 #elif defined(IP_RECVDSTADDR) 1183 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
1186 CMSG_DATA(cmsg),
sizeof(
struct in_addr));
1194 if (cmsg->cmsg_level != -1 && cmsg->cmsg_type != -1) {
1196 "cmsg_level = %d and cmsg_type = %d not supported - fix\n",
1197 cmsg->cmsg_level, cmsg->cmsg_type);
1207 coap_log(COAP_LOG_DEBUG,
"Cannot determine local port\n");
1211 packet->
length = (size_t)len;
1215 coap_log(COAP_LOG_DEBUG,
"Cannot determine local port\n");
1223 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) 1224 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN]) 1226 if (uip_newdata()) {
1229 uip_ipaddr_copy(&(packet)->addr_info.local.addr, &UIP_IP_BUF->destipaddr);
1232 len = uip_datalen();
1236 coap_log(COAP_LOG_WARNING,
"discarded oversized packet\n");
1240 ((
char *)uip_appdata)[len] = 0;
1243 #ifndef INET6_ADDRSTRLEN 1244 #define INET6_ADDRSTRLEN 40 1250 coap_log(COAP_LOG_DEBUG,
"received %zd bytes from %s\n", len, addr_str);
1256 memcpy(&packet->
payload, uip_appdata, len);
1263 packet->src.size =
sizeof(packet->src.addr);
1265 0, &packet->src.addr.sa, &packet->src.size);
1270 coap_log(COAP_LOG_DEBUG,
"received %zd bytes from %s\n", len, addr_str);
1274 #if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) 1280 #if !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) 1287 #if !defined(WITH_CONTIKI) 1292 unsigned int max_sockets,
1293 unsigned int *num_sockets,
1301 #ifdef COAP_EPOLL_SUPPORT 1317 #ifndef COAP_EPOLL_SUPPORT 1319 if (*num_sockets < max_sockets)
1320 sockets[(*num_sockets)++] = &ep->
sock;
1332 if (timeout == 0 || s_timeout < timeout)
1333 timeout = s_timeout;
1335 #ifndef COAP_EPOLL_SUPPORT 1337 if (*num_sockets < max_sockets)
1338 sockets[(*num_sockets)++] = &s->
sock;
1365 if (timeout == 0 || s_timeout < timeout)
1366 timeout = s_timeout;
1386 if (timeout == 0 || s_timeout < timeout)
1387 timeout = s_timeout;
1390 #ifndef COAP_EPOLL_SUPPORT 1392 if (*num_sockets < max_sockets)
1393 sockets[(*num_sockets)++] = &s->
sock;
1405 if (nextpdu && (timeout == 0 || nextpdu->
t - ( now - ctx->
sendqueue_basetime ) < timeout))
1411 if (tls_timeout > 0) {
1412 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
1413 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
1414 coap_log(COAP_LOG_DEBUG,
"** DTLS global timeout set to %dms\n",
1415 (
int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
1416 if (timeout == 0 || tls_timeout - now < timeout)
1417 timeout = tls_timeout - now;
1425 while (tls_timeout > 0 && tls_timeout <= now) {
1426 coap_log(COAP_LOG_DEBUG,
"** %s: DTLS retransmit timeout\n",
1436 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1437 timeout = tls_timeout - now;
1445 while (tls_timeout > 0 && tls_timeout <= now) {
1455 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1456 timeout = tls_timeout - now;
1462 return (
unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
1465 #ifndef RIOT_VERSION 1468 #if COAP_CONSTRAINED_STACK 1469 static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER;
1470 # ifndef COAP_EPOLL_SUPPORT 1471 static fd_set readfds, writefds, exceptfds;
1475 # ifndef COAP_EPOLL_SUPPORT 1476 fd_set readfds, writefds, exceptfds;
1482 unsigned int num_sockets = 0, timeout;
1483 #ifndef COAP_EPOLL_SUPPORT 1489 #if COAP_CONSTRAINED_STACK 1490 coap_mutex_lock(&static_mutex);
1495 timeout =
coap_write(ctx, sockets, (
unsigned int)(
sizeof(sockets) /
sizeof(sockets[0])), &num_sockets, before);
1496 #ifdef COAP_EPOLL_SUPPORT 1498 ctx->next_timeout = timeout ? before + timeout : 0;
1500 if (timeout == 0 || timeout_ms < timeout)
1501 timeout = timeout_ms;
1503 #ifndef COAP_EPOLL_SUPPORT 1506 FD_ZERO(&exceptfds);
1507 for (i = 0; i < num_sockets; i++) {
1508 if (sockets[i]->fd + 1 > nfds)
1509 nfds = sockets[i]->
fd + 1;
1511 FD_SET(sockets[i]->fd, &readfds);
1513 FD_SET(sockets[i]->fd, &writefds);
1515 FD_SET(sockets[i]->fd, &readfds);
1517 FD_SET(sockets[i]->fd, &writefds);
1518 FD_SET(sockets[i]->fd, &exceptfds);
1522 if ( timeout > 0 ) {
1525 tv.tv_usec = (timeout % 1000) * 1000;
1526 tv.tv_sec = (long)(timeout / 1000);
1529 result = select(nfds, &readfds, &writefds, &exceptfds, timeout > 0 ? &tv : NULL);
1533 if (WSAGetLastError() != WSAEINVAL) {
1535 if (errno != EINTR) {
1538 #if COAP_CONSTRAINED_STACK 1539 coap_mutex_unlock(&static_mutex);
1546 for (i = 0; i < num_sockets; i++) {
1553 if ((sockets[i]->flags &
COAP_SOCKET_WANT_CONNECT) && (FD_ISSET(sockets[i]->fd, &writefds) || FD_ISSET(sockets[i]->fd, &exceptfds)))
1563 struct epoll_event events[COAP_MAX_EPOLL_EVENTS];
1564 int etimeout = timeout;
1572 nfds = epoll_wait(ctx->epfd, events, COAP_MAX_EPOLL_EVENTS, etimeout);
1574 if (errno != EINTR) {
1575 coap_log (COAP_LOG_ERR,
"epoll_wait: unexpected error: %s (%d)\n",
1585 (
unsigned int)(
sizeof(sockets) /
sizeof(sockets[0])),
1599 if (ctx->eptimerfd != -1) {
1600 struct itimerspec new_value;
1603 memset(&new_value, 0,
sizeof(new_value));
1605 if (ctx->next_timeout != 0 && ctx->next_timeout > now) {
1606 coap_tick_t rem_timeout = ctx->next_timeout - now;
1613 ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
1616 "%s: timerfd_settime failed: %s (%d)\n",
1624 #if COAP_CONSTRAINED_STACK 1625 coap_mutex_unlock(&static_mutex);
1647 unsigned int max_sockets,
1648 unsigned int *num_sockets,
1658 static char szError[256];
1659 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)
1660 strcpy(szError,
"Unknown error");
1668 #ifndef WITH_CONTIKI 1670 return strerror(error);
1675 return strerror(errno);
1681 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_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...
int coap_run_once(coap_context_t *ctx, unsigned timeout_ms)
struct coap_context_t * context
session's context
void coap_socket_close(coap_socket_t *sock)
void * tls
security parameters
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
coap_endpoint_t * endpoint
the endpoints used for listening
#define COAP_SOCKET_CONNECTED
the socket is connected
multi-purpose address abstraction
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
unsigned ref
reference count from queues
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)
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)
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
int ifindex
the interface index
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
coap_address_t local
local address and port
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
#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
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.
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.
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
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
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.
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...
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, 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
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
#define COAP_RUN_NONBLOCK
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)
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
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
#define COAP_SESSION_TYPE_SERVER
server-side
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
#define COAP_DEFAULT_SESSION_TIMEOUT
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.
const char * coap_socket_strerror(void)
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
struct coap_endpoint_t * coap_malloc_endpoint(void)
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
void coap_free_endpoint(coap_endpoint_t *ep)
#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 ipi6_ifindex
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().
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
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)
#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.
static const char * coap_socket_format_errno(int error)
#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.
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.