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) {
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.
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
Pulls together all the internal only header files.
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)
void coap_free_endpoint(coap_endpoint_t *ep)
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
void coap_socket_close(coap_socket_t *sock)
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
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...
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)
int coap_run_once(coap_context_t *ctx, unsigned timeout_ms)
struct coap_endpoint_t * coap_malloc_endpoint(void)
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_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
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)
const char * coap_socket_strerror(void)
static const char * coap_socket_format_errno(int error)
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_MAX_EPOLL_EVENTS
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_ERROR
@ COAP_NACK_NOT_DELIVERABLE
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
#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
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#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
#define COAP_INVALID_SOCKET
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED, coap_tick_t now UNUSED)
void coap_dtls_handle_timeout(coap_session_t *session UNUSED)
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
void coap_session_free(coap_session_t *session)
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define COAP_SESSION_STATE_CSM
#define COAP_SESSION_TYPE_CLIENT
coap_session_type_t values
#define COAP_PROTO_RELIABLE(p)
#define COAP_SESSION_STATE_NONE
coap_session_state_t values
#define COAP_SESSION_STATE_ESTABLISHED
#define SESSIONS_ITER(e, el, rtmp)
#define COAP_SESSION_TYPE_SERVER
server-side
#define COAP_DEFAULT_SESSION_TIMEOUT
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
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_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
coap_log_t coap_get_log_level(void)
Get the current logging level.
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log(level,...)
Logging function.
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_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
void coap_read(coap_context_t *ctx, coap_tick_t now)
For applications with their own message loop, reads all data from the network.
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
void coap_io_do_events(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
#define COAP_RUN_NONBLOCK
#define COAP_INVALID_TID
Indicates an invalid transaction id.
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and 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
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.
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
coap_session_t * sessions
client sessions
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
coap_endpoint_t * endpoint
the endpoints used for listening
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.
struct coap_context_t * context
endpoint's context
struct coap_session_t * sessions
hash table or list of active sessions
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
int ifindex
the interface index
coap_tick_t t
when to send PDU for the next time
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationaship with peer
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
unsigned ref
reference count from queues
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
struct coap_context_t * context
session's context
void * tls
security parameters
coap_session_type_t type
client or server side socket
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
int ifindex
interface index
struct coap_endpoint_t * endpoint
struct coap_session_t * session
coap_socket_flags_t flags
struct in6_addr ipi6_addr
unsigned int ipi6_ifindex
struct in_addr ipi_spec_dst
#define LL_FOREACH(head, el)