9 #include "coap_config.h" 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 57 #if !defined(WITH_CONTIKI) 59 #if defined(IP_PKTINFO) 60 # define GEN_IP_PKTINFO IP_PKTINFO 61 #elif defined(IP_RECVDSTADDR) 62 # define GEN_IP_PKTINFO IP_RECVDSTADDR 64 # error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS." 68 #ifdef IPV6_RECVPKTINFO 69 # define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO 70 #elif defined(IPV6_PKTINFO) 71 # define GEN_IPV6_PKTINFO IPV6_PKTINFO 73 # error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS." 80 static int ep_initialized = 0;
104 sock->conn = udp_new(NULL, 0, NULL);
112 uip_ipaddr_copy(&bound_addr->
addr, &listen_addr->
addr);
113 bound_addr->port = listen_addr->port;
114 udp_bind((
struct uip_udp_conn *)sock->conn, bound_addr->port);
172 uip_udp_remove((
struct uip_udp_conn *)sock->conn);
200 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_DGRAM, 0);
217 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
219 "coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
222 switch (listen_addr->
addr.
sa.sa_family) {
224 if (setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
226 "coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
231 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
233 "coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
235 if (setsockopt(sock->
fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
237 "coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
239 setsockopt(sock->
fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on),
sizeof(on));
252 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
253 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
255 "coap_socket_bind_udp: getsockname: %s\n",
282 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
286 "coap_socket_connect_tcp1: socket: %s\n",
297 "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
301 switch (server->
addr.
sa.sa_family) {
303 if (connect_addr.
addr.
sin.sin_port == 0)
304 connect_addr.
addr.
sin.sin_port = htons(default_port);
307 if (connect_addr.
addr.
sin6.sin6_port == 0)
308 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
310 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
312 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
320 if (local_if && local_if->
addr.
sa.sa_family) {
322 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
324 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
332 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
337 if (WSAGetLastError() == WSAEWOULDBLOCK) {
339 if (errno == EINPROGRESS) {
378 int optlen = (int)
sizeof( error );
380 socklen_t optlen = (socklen_t)
sizeof( error );
385 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
393 "coap_socket_finish_connect_tcp: connect failed: %s\n",
421 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
437 if (setsockopt (sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
440 "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
443 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
446 "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
449 switch (listen_addr->
addr.
sa.sa_family) {
454 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
456 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
469 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
470 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
502 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
510 if (getsockname( new_client->
fd, &local_addr->
addr.
sa, &local_addr->
size) < 0)
542 sock->
fd = socket(connect_addr.
addr.
sa.sa_family, SOCK_DGRAM, 0);
559 switch (connect_addr.
addr.
sa.sa_family) {
561 if (connect_addr.
addr.
sin.sin_port == 0)
562 connect_addr.
addr.
sin.sin_port = htons(default_port);
565 if (connect_addr.
addr.
sin6.sin6_port == 0)
566 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
568 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
570 "coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
578 if (local_if && local_if->
addr.
sa.sa_family) {
579 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
581 "coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
594 "coap_socket_connect_udp: getsockname for multicast socket: %s\n",
640 r = send(sock->
fd, (
const char *)data, (
int)data_len, 0);
642 r = send(sock->
fd, data, data_len, 0);
646 if (WSAGetLastError() == WSAEWOULDBLOCK) {
647 #elif EAGAIN != EWOULDBLOCK 648 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
650 if (errno==EAGAIN || errno == EINTR) {
659 if (r < (ssize_t)data_len)
672 r = recv(sock->
fd, (
char *)data, (
int)data_len, 0);
674 r = recv(sock->
fd, data, data_len, 0);
683 error = WSAGetLastError();
684 if (error == WSAEWOULDBLOCK) {
685 #elif EAGAIN != EWOULDBLOCK 686 if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
688 if (errno==EAGAIN || errno == EINTR) {
693 if (error != WSAECONNRESET)
695 if (errno != ECONNRESET)
701 if (r < (ssize_t)data_len)
708 #if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) ) 719 struct in_addr ipi_spec_dst;
720 struct in_addr ipi_addr;
724 #if !defined(WITH_CONTIKI) && !defined(SOL_IP) 726 #define SOL_IP IPPROTO_IP 730 #define UNUSED_PARAM __attribute__ ((unused)) 737 static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
739 #define msghdr _WSAMSG 740 #define msg_name name 741 #define msg_namelen namelen 742 #define msg_iov lpBuffers 743 #define msg_iovlen dwBufferCount 744 #define msg_control Control.buf 745 #define msg_controllen Control.len 746 #define iovec _WSABUF 749 #define iov_len_t u_long 751 #define CMSG_DATA WSA_CMSG_DATA 752 #define ipi_spec_dst ipi_addr 754 #define iov_len_t size_t 759 ssize_t bytes_written = 0;
762 bytes_written = (ssize_t)datalen;
766 bytes_written = send(sock->
fd, (
const char *)data, (
int)datalen, 0);
768 bytes_written = send(sock->
fd, data, datalen, 0);
776 DWORD dwNumberOfBytesSent = 0;
785 memcpy (&iov[0].iov_base, &data,
sizeof (iov[0].iov_base));
788 memset( buf, 0,
sizeof(buf));
790 memset(&mhdr, 0,
sizeof(
struct msghdr));
791 memcpy (&mhdr.msg_name, &addr, sizeof (mhdr.msg_name));
800 struct cmsghdr *cmsg;
803 #if defined(IP_PKTINFO) 805 mhdr.msg_control = buf;
806 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
808 cmsg = CMSG_FIRSTHDR(&mhdr);
809 cmsg->cmsg_level =
SOL_IP;
810 cmsg->cmsg_type = IP_PKTINFO;
811 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
813 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
817 #elif defined(IP_SENDSRCADDR) 818 mhdr.msg_control = buf;
819 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
821 cmsg = CMSG_FIRSTHDR(&mhdr);
822 cmsg->cmsg_level = IPPROTO_IP;
823 cmsg->cmsg_type = IP_SENDSRCADDR;
824 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
826 memcpy(CMSG_DATA(cmsg), session->
local_addr.
addr.
sin6.sin6_addr.s6_addr + 12,
sizeof(
struct in_addr));
830 mhdr.msg_control = buf;
831 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
833 cmsg = CMSG_FIRSTHDR(&mhdr);
834 cmsg->cmsg_level = IPPROTO_IPV6;
835 cmsg->cmsg_type = IPV6_PKTINFO;
836 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
847 #if defined(IP_PKTINFO) 848 struct cmsghdr *cmsg;
851 mhdr.msg_control = buf;
852 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
854 cmsg = CMSG_FIRSTHDR(&mhdr);
855 cmsg->cmsg_level =
SOL_IP;
856 cmsg->cmsg_type = IP_PKTINFO;
857 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
859 pktinfo = (
struct in_pktinfo *)CMSG_DATA(cmsg);
863 #elif defined(IP_SENDSRCADDR) 864 struct cmsghdr *cmsg;
865 mhdr.msg_control = buf;
866 mhdr.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
868 cmsg = CMSG_FIRSTHDR(&mhdr);
869 cmsg->cmsg_level = IPPROTO_IP;
870 cmsg->cmsg_type = IP_SENDSRCADDR;
871 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
873 memcpy(CMSG_DATA(cmsg), &session->
local_addr.
addr.
sin.sin_addr,
sizeof(
struct in_addr));
884 r = WSASendMsg(sock->
fd, &mhdr, 0 , &dwNumberOfBytesSent, NULL , NULL );
886 bytes_written = (ssize_t)dwNumberOfBytesSent;
890 bytes_written = sendmsg(sock->
fd, &mhdr, 0);
897 uip_udp_packet_sendto((
struct uip_udp_conn *)sock->conn, data, datalen,
899 bytes_written = datalen;
903 if (bytes_written < 0)
906 return bytes_written;
909 #define SIN6(A) ((struct sockaddr_in6 *)(A)) 945 if (WSAGetLastError() == WSAECONNRESET) {
947 if (errno == ECONNREFUSED) {
955 }
else if (len > 0) {
956 packet->
length = (size_t)len;
961 DWORD dwNumberOfBytesRecvd = 0;
964 #if !defined(WITH_CONTIKI) 970 iov[0].iov_base = packet->
payload;
973 memset(&mhdr, 0,
sizeof(
struct msghdr));
975 mhdr.msg_name = (
struct sockaddr*)&packet->
src.
addr;
976 mhdr.msg_namelen =
sizeof(packet->
src.
addr);
981 mhdr.msg_control = buf;
982 mhdr.msg_controllen =
sizeof(buf);
986 GUID wsaid = WSAID_WSARECVMSG;
987 DWORD cbBytesReturned = 0;
988 if (WSAIoctl(sock->
fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &wsaid,
sizeof(wsaid), &lpWSARecvMsg,
sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
993 r = lpWSARecvMsg(sock->
fd, &mhdr, &dwNumberOfBytesRecvd, NULL , NULL );
995 len = (ssize_t)dwNumberOfBytesRecvd;
997 len = recvmsg(sock->
fd, &mhdr, 0);
1002 if (WSAGetLastError() == WSAECONNRESET) {
1004 if (errno == ECONNREFUSED) {
1012 struct cmsghdr *cmsg;
1014 packet->
src.
size = mhdr.msg_namelen;
1015 packet->
length = (size_t)len;
1019 for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
1022 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
1027 u.c = CMSG_DATA(cmsg);
1028 packet->
ifindex = (int)(u.p->ipi6_ifindex);
1029 memcpy(&packet->
dst.
addr.
sin6.sin6_addr, &u.p->ipi6_addr,
sizeof(
struct in6_addr));
1034 #if defined(IP_PKTINFO) 1035 if (cmsg->cmsg_level ==
SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
1040 u.c = CMSG_DATA(cmsg);
1041 packet->
ifindex = u.p->ipi_ifindex;
1042 if (packet->
dst.
addr.
sa.sa_family == AF_INET6) {
1043 memset(packet->
dst.
addr.
sin6.sin6_addr.s6_addr, 0, 10);
1044 packet->
dst.
addr.
sin6.sin6_addr.s6_addr[10] = 0xff;
1045 packet->
dst.
addr.
sin6.sin6_addr.s6_addr[11] = 0xff;
1046 memcpy(packet->
dst.
addr.
sin6.sin6_addr.s6_addr + 12, &u.p->ipi_addr,
sizeof(
struct in_addr));
1048 memcpy(&packet->
dst.
addr.
sin.sin_addr, &u.p->ipi_addr,
sizeof(
struct in_addr));
1052 #elif defined(IP_RECVDSTADDR) 1053 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
1055 memcpy(&packet->
dst.
addr.
sin.sin_addr, CMSG_DATA(cmsg),
sizeof(
struct in_addr));
1064 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) 1065 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN]) 1067 if (uip_newdata()) {
1068 uip_ipaddr_copy(&packet->
src.
addr, &UIP_IP_BUF->srcipaddr);
1069 packet->
src.port = UIP_UDP_BUF->srcport;
1070 uip_ipaddr_copy(&(packet)->dst.addr, &UIP_IP_BUF->destipaddr);
1071 packet->
dst.port = UIP_UDP_BUF->destport;
1073 len = uip_datalen();
1081 ((
char *)uip_appdata)[len] = 0;
1084 #ifndef INET6_ADDRSTRLEN 1085 #define INET6_ADDRSTRLEN 40 1096 memcpy(&packet->
payload, uip_appdata, len);
1102 #ifndef WITH_CONTIKI 1108 #if !defined(WITH_CONTIKI) 1114 #if !defined(WITH_CONTIKI) 1119 unsigned int max_sockets,
1120 unsigned int *num_sockets,
1142 if (*num_sockets < max_sockets)
1143 sockets[(*num_sockets)++] = &ep->
sock;
1154 if (timeout == 0 || s_timeout < timeout)
1155 timeout = s_timeout;
1158 if (*num_sockets < max_sockets)
1159 sockets[(*num_sockets)++] = &s->
sock;
1186 if (timeout == 0 || s_timeout < timeout)
1187 timeout = s_timeout;
1207 if (timeout == 0 || s_timeout < timeout)
1208 timeout = s_timeout;
1212 if (*num_sockets < max_sockets)
1213 sockets[(*num_sockets)++] = &s->
sock;
1224 if (nextpdu && (timeout == 0 || nextpdu->
t - ( now - ctx->
sendqueue_basetime ) < timeout))
1230 if (tls_timeout > 0) {
1231 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
1232 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
1234 (
int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
1235 if (timeout == 0 || tls_timeout - now < timeout)
1236 timeout = tls_timeout - now;
1244 while (tls_timeout > 0 && tls_timeout <= now) {
1255 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1256 timeout = tls_timeout - now;
1264 while (tls_timeout > 0 && tls_timeout <= now) {
1274 if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
1275 timeout = tls_timeout - now;
1281 return (
unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
1286 fd_set readfds, writefds, exceptfds;
1292 unsigned int num_sockets = 0, i, timeout;
1296 timeout =
coap_write(ctx, sockets, (
unsigned int)(
sizeof(sockets) /
sizeof(sockets[0])), &num_sockets, before);
1297 if (timeout == 0 || timeout_ms < timeout)
1298 timeout = timeout_ms;
1302 FD_ZERO(&exceptfds);
1303 for (i = 0; i < num_sockets; i++) {
1304 if (sockets[i]->fd + 1 > nfds)
1305 nfds = sockets[i]->
fd + 1;
1307 FD_SET(sockets[i]->fd, &readfds);
1309 FD_SET(sockets[i]->fd, &writefds);
1311 FD_SET(sockets[i]->fd, &readfds);
1313 FD_SET(sockets[i]->fd, &writefds);
1314 FD_SET(sockets[i]->fd, &exceptfds);
1318 if ( timeout > 0 ) {
1319 tv.tv_usec = (timeout % 1000) * 1000;
1320 tv.tv_sec = (long)(timeout / 1000);
1323 result = select(nfds, &readfds, &writefds, &exceptfds, timeout > 0 ? &tv : NULL);
1327 if (WSAGetLastError() != WSAEINVAL) {
1329 if (errno != EINTR) {
1337 for (i = 0; i < num_sockets; i++) {
1344 if ((sockets[i]->flags &
COAP_SOCKET_WANT_CONNECT) && (FD_ISSET(sockets[i]->fd, &writefds) || FD_ISSET(sockets[i]->fd, &exceptfds)))
1363 unsigned int max_sockets,
1364 unsigned int *num_sockets,
1374 static char szError[256];
1375 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)
1376 strcpy(szError,
"Unknown error");
1384 #ifndef WITH_CONTIKI 1386 return strerror(error);
1391 return strerror(errno);
1397 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
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_session_t * sessions
list of active sessions
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.
int ifindex
the interface index
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.
COAP_STATIC_INLINE void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
#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)
#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.
coap_address_t local_addr
local address and port
const char * coap_socket_strerror(void)
#define COAP_SESSION_STATE_ESTABLISHED
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
void coap_session_mfree(coap_session_t *session)
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.
coap_address_t remote_addr
remote address and port
coap_address_t src
the packet's source address
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
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
void coap_packet_set_addr(coap_packet_t *packet, const coap_address_t *src, const coap_address_t *dst)
coap_session_t hello
special session of DTLS hello messages
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.
coap_proto_t proto
protocol used on this interface
Generic resource handling.
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
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.
Pre-defined constants that reflect defaults for CoAP.
coap_address_t dst
the packet's destination address
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 LL_FOREACH_SAFE(head, el, tmp)
#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
coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED)
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_log_t coap_get_log_level(void)
Get the current logging level.
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.
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.
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.
#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.