coap_io.c
Go to the documentation of this file.
/* coap_io.c -- Default network I/O functions for libcoap
*
* Copyright (C) 2012,2014,2016-2019 Olaf Bergmann <bergmann@tzi.org> and others
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_internal.h"
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
# define OPTVAL_T(t) (t)
# define OPTVAL_GT(t) (t)
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_WS2TCPIP_H
#include <ws2tcpip.h>
# define OPTVAL_T(t) (const char*)(t)
# define OPTVAL_GT(t) (char*)(t)
# undef CMSG_DATA
# define CMSG_DATA WSA_CMSG_DATA
#endif
#ifdef HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#ifdef COAP_EPOLL_SUPPORT
#include <sys/epoll.h>
#include <sys/timerfd.h>
#endif /* COAP_EPOLL_SUPPORT */
#ifdef WITH_CONTIKI
# include "uip.h"
#endif
#if !defined(WITH_CONTIKI)
/* define generic PKTINFO for IPv4 */
#if defined(IP_PKTINFO)
# define GEN_IP_PKTINFO IP_PKTINFO
#elif defined(IP_RECVDSTADDR)
# define GEN_IP_PKTINFO IP_RECVDSTADDR
#else
# error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS."
#endif /* IP_PKTINFO */
/* define generic KTINFO for IPv6 */
#ifdef IPV6_RECVPKTINFO
# define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO
#elif defined(IPV6_PKTINFO)
# define GEN_IPV6_PKTINFO IPV6_PKTINFO
#else
# error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS."
#endif /* IPV6_RECVPKTINFO */
#endif
void coap_free_endpoint(coap_endpoint_t *ep);
#ifdef WITH_CONTIKI
static int ep_initialized = 0;
struct coap_endpoint_t *
coap_malloc_endpoint() {
static struct coap_endpoint_t ep;
if (ep_initialized) {
return NULL;
} else {
ep_initialized = 1;
return &ep;
}
}
void
coap_mfree_endpoint(struct coap_endpoint_t *ep) {
ep_initialized = 0;
}
int
coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
sock->conn = udp_new(NULL, 0, NULL);
if (!sock->conn) {
coap_log(LOG_WARNING, "coap_socket_bind_udp");
return 0;
}
coap_address_init(bound_addr);
uip_ipaddr_copy(&bound_addr->addr, &listen_addr->addr);
bound_addr->port = listen_addr->port;
udp_bind((struct uip_udp_conn *)sock->conn, bound_addr->port);
return 1;
}
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) {
return 0;
}
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) {
return 0;
}
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
return 0;
}
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
return -1;
}
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
return -1;
}
void coap_socket_close(coap_socket_t *sock) {
if (sock->conn)
uip_udp_remove((struct uip_udp_conn *)sock->conn);
sock->flags = COAP_SOCKET_EMPTY;
}
#else
static const char *coap_socket_format_errno( int error );
struct coap_endpoint_t *
coap_malloc_endpoint(void) {
return (struct coap_endpoint_t *)coap_malloc_type(COAP_ENDPOINT, sizeof(struct coap_endpoint_t));
}
void
coap_mfree_endpoint(struct coap_endpoint_t *ep) {
coap_free_type(COAP_ENDPOINT, ep);
}
int
coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
int on = 1, off = 0;
#ifdef _WIN32
u_long u_on = 1;
#endif
sock->fd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
if (sock->fd == COAP_INVALID_SOCKET) {
coap_log(LOG_WARNING,
"coap_socket_bind_udp: socket: %s\n", coap_socket_strerror());
goto error;
}
#ifdef _WIN32
if (ioctlsocket(sock->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log(LOG_WARNING,
"coap_socket_bind_udp: ioctl FIONBIO: %s\n", coap_socket_strerror());
}
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_bind_udp: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
switch (listen_addr->addr.sa.sa_family) {
case AF_INET:
if (setsockopt(sock->fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_ALERT,
"coap_socket_bind_udp: setsockopt IP_PKTINFO: %s\n",
coap_socket_strerror());
break;
case AF_INET6:
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log(LOG_ALERT,
"coap_socket_bind_udp: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
if (setsockopt(sock->fd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_ALERT,
"coap_socket_bind_udp: setsockopt IPV6_PKTINFO: %s\n",
coap_socket_strerror());
setsockopt(sock->fd, IPPROTO_IP, GEN_IP_PKTINFO, OPTVAL_T(&on), sizeof(on)); /* ignore error, because the likely cause is that IPv4 is disabled at the os level */
break;
default:
coap_log(LOG_ALERT, "coap_socket_bind_udp: unsupported sa_family\n");
break;
}
if (bind(sock->fd, &listen_addr->addr.sa,
listen_addr->addr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
listen_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_bind_udp: bind: %s\n",
coap_socket_strerror());
goto error;
}
bound_addr->size = (socklen_t)sizeof(*bound_addr);
if (getsockname(sock->fd, &bound_addr->addr.sa, &bound_addr->size) < 0) {
coap_log(LOG_WARNING,
"coap_socket_bind_udp: getsockname: %s\n",
coap_socket_strerror());
goto error;
}
return 1;
error:
coap_socket_close(sock);
return 0;
}
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) {
int on = 1, off = 0;
#ifdef _WIN32
u_long u_on = 1;
#endif
coap_address_t connect_addr;
coap_address_copy( &connect_addr, server );
sock->flags &= ~COAP_SOCKET_CONNECTED;
sock->fd = socket(server->addr.sa.sa_family, SOCK_STREAM, 0);
if (sock->fd == COAP_INVALID_SOCKET) {
coap_log(LOG_WARNING,
"coap_socket_connect_tcp1: socket: %s\n",
coap_socket_strerror());
goto error;
}
#ifdef _WIN32
if (ioctlsocket(sock->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log(LOG_WARNING,
"coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
coap_socket_strerror());
}
switch (server->addr.sa.sa_family) {
case AF_INET:
if (connect_addr.addr.sin.sin_port == 0)
connect_addr.addr.sin.sin_port = htons(default_port);
break;
case AF_INET6:
if (connect_addr.addr.sin6.sin6_port == 0)
connect_addr.addr.sin6.sin6_port = htons(default_port);
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
break;
default:
coap_log(LOG_ALERT, "coap_socket_connect_tcp1: unsupported sa_family\n");
break;
}
if (local_if && local_if->addr.sa.sa_family) {
coap_address_copy(local_addr, local_if);
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
if (bind(sock->fd, &local_if->addr.sa,
local_if->addr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
local_if->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_tcp1: bind: %s\n",
coap_socket_strerror());
goto error;
}
} else {
local_addr->addr.sa.sa_family = server->addr.sa.sa_family;
}
if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK) {
#else
if (errno == EINPROGRESS) {
#endif
/*
* COAP_SOCKET_CONNECTED needs to be set here as there will be reads/writes
* by underlying TLS libraries during connect() and we do not want to
* assert() in coap_read_session() or coap_write_session() when called by coap_read()
*/
sock->flags |= COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CONNECTED;
return 1;
}
coap_log(LOG_WARNING, "coap_socket_connect_tcp1: connect: %s\n",
coap_socket_strerror());
goto error;
}
if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_tcp1: getsockname: %s\n",
coap_socket_strerror());
}
if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_tcp1: getpeername: %s\n",
coap_socket_strerror());
}
sock->flags |= COAP_SOCKET_CONNECTED;
return 1;
error:
coap_socket_close(sock);
return 0;
}
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
int error = 0;
#ifdef _WIN32
int optlen = (int)sizeof( error );
#else
socklen_t optlen = (socklen_t)sizeof( error );
#endif
sock->flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT);
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
&optlen) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_finish_connect_tcp: getsockopt: %s\n",
coap_socket_strerror());
}
if (error) {
coap_log(LOG_WARNING,
"coap_socket_finish_connect_tcp: connect failed: %s\n",
coap_socket_format_errno(error));
coap_socket_close(sock);
return 0;
}
if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_tcp: getsockname: %s\n",
coap_socket_strerror());
}
if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_tcp: getpeername: %s\n",
coap_socket_strerror());
}
return 1;
}
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
int on = 1, off = 0;
#ifdef _WIN32
u_long u_on = 1;
#endif
sock->fd = socket(listen_addr->addr.sa.sa_family, SOCK_STREAM, 0);
if (sock->fd == COAP_INVALID_SOCKET) {
coap_log(LOG_WARNING, "coap_socket_bind_tcp: socket: %s\n",
coap_socket_strerror());
goto error;
}
#ifdef _WIN32
if (ioctlsocket(sock->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log(LOG_WARNING, "coap_socket_bind_tcp: ioctl FIONBIO: %s\n",
coap_socket_strerror());
}
if (setsockopt (sock->fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
sizeof (on)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
coap_socket_strerror());
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
switch (listen_addr->addr.sa.sa_family) {
case AF_INET:
break;
case AF_INET6:
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log(LOG_ALERT,
"coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
break;
default:
coap_log(LOG_ALERT, "coap_socket_bind_tcp: unsupported sa_family\n");
}
if (bind(sock->fd, &listen_addr->addr.sa,
listen_addr->addr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
listen_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_ALERT, "coap_socket_bind_tcp: bind: %s\n",
coap_socket_strerror());
goto error;
}
bound_addr->size = (socklen_t)sizeof(*bound_addr);
if (getsockname(sock->fd, &bound_addr->addr.sa, &bound_addr->size) < 0) {
coap_log(LOG_WARNING, "coap_socket_bind_tcp: getsockname: %s\n",
coap_socket_strerror());
goto error;
}
if (listen(sock->fd, 5) == COAP_SOCKET_ERROR) {
coap_log(LOG_ALERT, "coap_socket_bind_tcp: listen: %s\n",
coap_socket_strerror());
goto error;
}
return 1;
error:
coap_socket_close(sock);
return 0;
}
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
#ifdef _WIN32
u_long u_on = 1;
#else
int on = 1;
#endif
server->flags &= ~COAP_SOCKET_CAN_ACCEPT;
new_client->fd = accept(server->fd, &remote_addr->addr.sa,
&remote_addr->size);
if (new_client->fd == COAP_INVALID_SOCKET) {
coap_log(LOG_WARNING, "coap_socket_accept_tcp: accept: %s\n",
coap_socket_strerror());
return 0;
}
if (getsockname( new_client->fd, &local_addr->addr.sa, &local_addr->size) < 0)
coap_log(LOG_WARNING, "coap_socket_accept_tcp: getsockname: %s\n",
coap_socket_strerror());
#ifdef _WIN32
if (ioctlsocket(new_client->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(new_client->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log(LOG_WARNING, "coap_socket_accept_tcp: ioctl FIONBIO: %s\n",
coap_socket_strerror());
}
return 1;
}
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) {
int on = 1, off = 0;
#ifdef _WIN32
u_long u_on = 1;
#endif
coap_address_t connect_addr;
int is_mcast = coap_is_mcast(server);
coap_address_copy(&connect_addr, server);
sock->flags &= ~(COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST);
sock->fd = socket(connect_addr.addr.sa.sa_family, SOCK_DGRAM, 0);
if (sock->fd == COAP_INVALID_SOCKET) {
coap_log(LOG_WARNING, "coap_socket_connect_udp: socket: %s\n",
coap_socket_strerror());
goto error;
}
#ifdef _WIN32
if (ioctlsocket(sock->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log(LOG_WARNING, "coap_socket_connect_udp: ioctl FIONBIO: %s\n",
coap_socket_strerror());
}
switch (connect_addr.addr.sa.sa_family) {
case AF_INET:
if (connect_addr.addr.sin.sin_port == 0)
connect_addr.addr.sin.sin_port = htons(default_port);
break;
case AF_INET6:
if (connect_addr.addr.sin6.sin6_port == 0)
connect_addr.addr.sin6.sin6_port = htons(default_port);
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_connect_udp: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
break;
default:
coap_log(LOG_ALERT, "coap_socket_connect_udp: unsupported sa_family\n");
break;
}
if (local_if && local_if->addr.sa.sa_family) {
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log(LOG_WARNING,
"coap_socket_connect_udp: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
if (bind(sock->fd, &local_if->addr.sa,
local_if->addr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
local_if->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_udp: bind: %s\n",
coap_socket_strerror());
goto error;
}
}
/* special treatment for sockets that are used for multicast communication */
if (is_mcast) {
if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING,
"coap_socket_connect_udp: getsockname for multicast socket: %s\n",
coap_socket_strerror());
}
coap_address_copy(remote_addr, &connect_addr);
sock->flags |= COAP_SOCKET_MULTICAST;
return 1;
}
if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_udp: connect: %s\n",
coap_socket_strerror());
goto error;
}
if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_udp: getsockname: %s\n",
coap_socket_strerror());
}
if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) {
coap_log(LOG_WARNING, "coap_socket_connect_udp: getpeername: %s\n",
coap_socket_strerror());
}
sock->flags |= COAP_SOCKET_CONNECTED;
return 1;
error:
coap_socket_close(sock);
return 0;
}
void coap_socket_close(coap_socket_t *sock) {
if (sock->fd != COAP_INVALID_SOCKET) {
#ifdef COAP_EPOLL_SUPPORT
coap_context_t *context = sock->session ? sock->session->context :
sock->endpoint ? sock->endpoint->context : NULL;
if (context != NULL) {
int ret;
struct epoll_event event;
/* Kernels prior to 2.6.9 expect non NULL event parameter */
ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, sock->fd, &event);
if (ret == -1) {
coap_log(LOG_ERR,
"%s: epoll_ctl DEL failed: %s (%d)\n",
"coap_socket_close",
coap_socket_strerror(), errno);
}
}
#endif /* COAP_EPOLL_SUPPORT */
sock->endpoint = NULL;
sock->session = NULL;
coap_closesocket(sock->fd);
sock->fd = COAP_INVALID_SOCKET;
}
sock->flags = COAP_SOCKET_EMPTY;
}
#ifdef COAP_EPOLL_SUPPORT
void
coap_epoll_ctl_mod(coap_socket_t *sock,
uint32_t events,
const char *func
) {
int ret;
struct epoll_event event;
coap_context_t *context;
if (sock == NULL)
return;
context = sock->session ? sock->session->context :
sock->endpoint ? sock->endpoint->context : NULL;
if (context == NULL)
return;
event.events = events;
event.data.ptr = sock;
ret = epoll_ctl(context->epfd, EPOLL_CTL_MOD, sock->fd, &event);
if (ret == -1) {
coap_log(LOG_ERR,
"%s: epoll_ctl MOD failed: %s (%d)\n",
func,
coap_socket_strerror(), errno);
}
}
#endif /* COAP_EPOLL_SUPPORT */
ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
ssize_t r;
sock->flags &= ~(COAP_SOCKET_WANT_WRITE | COAP_SOCKET_CAN_WRITE);
#ifdef _WIN32
r = send(sock->fd, (const char *)data, (int)data_len, 0);
#else
r = send(sock->fd, data, data_len, 0);
#endif
if (r == COAP_SOCKET_ERROR) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK) {
#elif EAGAIN != EWOULDBLOCK
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
#else
if (errno==EAGAIN || errno == EINTR) {
#endif
sock->flags |= COAP_SOCKET_WANT_WRITE;
#ifdef COAP_EPOLL_SUPPORT
coap_epoll_ctl_mod(sock,
EPOLLOUT |
((sock->flags & COAP_SOCKET_WANT_READ) ?
EPOLLIN : 0),
__func__);
#endif /* COAP_EPOLL_SUPPORT */
return 0;
}
coap_log(LOG_WARNING, "coap_socket_write: send: %s\n",
coap_socket_strerror());
return -1;
}
if (r < (ssize_t)data_len) {
sock->flags |= COAP_SOCKET_WANT_WRITE;
#ifdef COAP_EPOLL_SUPPORT
coap_epoll_ctl_mod(sock,
EPOLLOUT |
((sock->flags & COAP_SOCKET_WANT_READ) ?
EPOLLIN : 0),
__func__);
#endif /* COAP_EPOLL_SUPPORT */
}
return r;
}
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
ssize_t r;
#ifdef _WIN32
int error;
#endif
#ifdef _WIN32
r = recv(sock->fd, (char *)data, (int)data_len, 0);
#else
r = recv(sock->fd, data, data_len, 0);
#endif
if (r == 0) {
/* graceful shutdown */
sock->flags &= ~COAP_SOCKET_CAN_READ;
return -1;
} else if (r == COAP_SOCKET_ERROR) {
sock->flags &= ~COAP_SOCKET_CAN_READ;
#ifdef _WIN32
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
#elif EAGAIN != EWOULDBLOCK
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
#else
if (errno==EAGAIN || errno == EINTR) {
#endif
return 0;
}
#ifdef _WIN32
if (error != WSAECONNRESET)
#else
if (errno != ECONNRESET)
#endif
coap_log(LOG_WARNING, "coap_socket_read: recv: %s\n",
coap_socket_strerror());
return -1;
}
if (r < (ssize_t)data_len)
sock->flags &= ~COAP_SOCKET_CAN_READ;
return r;
}
#endif /* WITH_CONTIKI */
#if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
/* define struct in6_pktinfo and struct in_pktinfo if not available
FIXME: check with configure
*/
struct in6_pktinfo {
struct in6_addr ipi6_addr; /* src/dst IPv6 address */
unsigned int ipi6_ifindex; /* send/recv interface index */
};
struct in_pktinfo {
int ipi_ifindex;
struct in_addr ipi_spec_dst;
struct in_addr ipi_addr;
};
#endif
#if !defined(WITH_CONTIKI) && !defined(SOL_IP)
/* Solaris expects level IPPROTO_IP for ancillary data. */
#define SOL_IP IPPROTO_IP
#endif
#ifdef __GNUC__
#define UNUSED_PARAM __attribute__ ((unused))
#else /* not a GCC */
#define UNUSED_PARAM
#endif /* GCC */
#if defined(_WIN32)
#include <mswsock.h>
static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL;
/* Map struct WSABUF fields to their posix counterpart */
#define msghdr _WSAMSG
#define msg_name name
#define msg_namelen namelen
#define msg_iov lpBuffers
#define msg_iovlen dwBufferCount
#define msg_control Control.buf
#define msg_controllen Control.len
#define iovec _WSABUF
#define iov_base buf
#define iov_len len
#define iov_len_t u_long
#undef CMSG_DATA
#define CMSG_DATA WSA_CMSG_DATA
#define ipi_spec_dst ipi_addr
#else
#define iov_len_t size_t
#endif
ssize_t
coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen) {
ssize_t bytes_written = 0;
if (!coap_debug_send_packet()) {
bytes_written = (ssize_t)datalen;
#ifndef WITH_CONTIKI
} else if (sock->flags & COAP_SOCKET_CONNECTED) {
#ifdef _WIN32
bytes_written = send(sock->fd, (const char *)data, (int)datalen, 0);
#else
bytes_written = send(sock->fd, data, datalen, 0);
#endif
#endif
} else {
#ifndef WITH_CONTIKI
#ifdef _WIN32
DWORD dwNumberOfBytesSent = 0;
int r;
#endif
#ifdef HAVE_STRUCT_CMSGHDR
/* a buffer large enough to hold all packet info types, ipv6 is the largest */
char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
struct msghdr mhdr;
struct iovec iov[1];
const void *addr = &session->addr_info.remote.addr;
assert(session);
memcpy (&iov[0].iov_base, &data, sizeof (iov[0].iov_base));
iov[0].iov_len = (iov_len_t)datalen;
memset(buf, 0, sizeof (buf));
memset(&mhdr, 0, sizeof(struct msghdr));
memcpy (&mhdr.msg_name, &addr, sizeof (mhdr.msg_name));
mhdr.msg_namelen = session->addr_info.remote.size;
mhdr.msg_iov = iov;
mhdr.msg_iovlen = 1;
if (!coap_address_isany(&session->addr_info.local) &&
!coap_is_mcast(&session->addr_info.local))
switch (session->addr_info.local.addr.sa.sa_family) {
case AF_INET6:
{
struct cmsghdr *cmsg;
if (IN6_IS_ADDR_V4MAPPED(&session->addr_info.local.addr.sin6.sin6_addr)) {
#if defined(IP_PKTINFO)
struct in_pktinfo *pktinfo;
mhdr.msg_control = buf;
mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
cmsg = CMSG_FIRSTHDR(&mhdr);
cmsg->cmsg_level = SOL_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
pktinfo->ipi_ifindex = session->ifindex;
memcpy(&pktinfo->ipi_spec_dst,
session->addr_info.local.addr.sin6.sin6_addr.s6_addr + 12,
sizeof(pktinfo->ipi_spec_dst));
#elif defined(IP_SENDSRCADDR)
mhdr.msg_control = buf;
mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
cmsg = CMSG_FIRSTHDR(&mhdr);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_SENDSRCADDR;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
memcpy(CMSG_DATA(cmsg),
session->addr_info.local.addr.sin6.sin6_addr.s6_addr + 12,
sizeof(struct in_addr));
#endif /* IP_PKTINFO */
} else {
struct in6_pktinfo *pktinfo;
mhdr.msg_control = buf;
mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
cmsg = CMSG_FIRSTHDR(&mhdr);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
pktinfo->ipi6_ifindex = session->ifindex;
memcpy(&pktinfo->ipi6_addr,
&session->addr_info.local.addr.sin6.sin6_addr,
sizeof(pktinfo->ipi6_addr));
}
break;
}
case AF_INET:
{
#if defined(IP_PKTINFO)
struct cmsghdr *cmsg;
struct in_pktinfo *pktinfo;
mhdr.msg_control = buf;
mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
cmsg = CMSG_FIRSTHDR(&mhdr);
cmsg->cmsg_level = SOL_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
pktinfo->ipi_ifindex = session->ifindex;
memcpy(&pktinfo->ipi_spec_dst,
&session->addr_info.local.addr.sin.sin_addr,
sizeof(pktinfo->ipi_spec_dst));
#elif defined(IP_SENDSRCADDR)
struct cmsghdr *cmsg;
mhdr.msg_control = buf;
mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
cmsg = CMSG_FIRSTHDR(&mhdr);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_SENDSRCADDR;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
memcpy(CMSG_DATA(cmsg),
&session->addr_info.local.addr.sin.sin_addr,
sizeof(struct in_addr));
#endif /* IP_PKTINFO */
break;
}
default:
/* error */
coap_log(LOG_WARNING, "protocol not supported\n");
bytes_written = -1;
}
#endif /* HAVE_STRUCT_CMSGHDR */
#ifdef _WIN32
r = WSASendMsg(sock->fd, &mhdr, 0 /*dwFlags*/, &dwNumberOfBytesSent, NULL /*lpOverlapped*/, NULL /*lpCompletionRoutine*/);
if (r == 0)
bytes_written = (ssize_t)dwNumberOfBytesSent;
else
bytes_written = -1;
#else
#ifdef HAVE_STRUCT_CMSGHDR
bytes_written = sendmsg(sock->fd, &mhdr, 0);
#else /* ! HAVE_STRUCT_CMSGHDR */
bytes_written = sendto(sock->fd, data, datalen, 0,
&session->addr_info.remote.addr.sa,
session->addr_info.remote.size);
#endif /* ! HAVE_STRUCT_CMSGHDR */
#endif
#else /* WITH_CONTIKI */
/* FIXME: untested */
/* FIXME: is there a way to check if send was successful? */
(void)datalen;
(void)data;
uip_udp_packet_sendto((struct uip_udp_conn *)sock->conn, data, datalen,
&session->addr_info.remote.addr, session->addr_info.remote.port);
bytes_written = datalen;
#endif /* WITH_CONTIKI */
}
if (bytes_written < 0)
coap_log(LOG_CRIT, "coap_network_send: %s\n", coap_socket_strerror());
return bytes_written;
}
#define SIN6(A) ((struct sockaddr_in6 *)(A))
void
coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length) {
*address = packet->payload;
*length = packet->length;
}
ssize_t
coap_network_read(coap_socket_t *sock, coap_packet_t *packet) {
ssize_t len = -1;
assert(sock);
assert(packet);
if ((sock->flags & COAP_SOCKET_CAN_READ) == 0) {
return -1;
} else {
/* clear has-data flag */
sock->flags &= ~COAP_SOCKET_CAN_READ;
}
#ifndef WITH_CONTIKI
if (sock->flags & COAP_SOCKET_CONNECTED) {
#ifdef _WIN32
len = recv(sock->fd, (char *)packet->payload, COAP_RXBUFFER_SIZE, 0);
#else
len = recv(sock->fd, packet->payload, COAP_RXBUFFER_SIZE, 0);
#endif
if (len < 0) {
#ifdef _WIN32
if (WSAGetLastError() == WSAECONNRESET) {
#else
if (errno == ECONNREFUSED) {
#endif
/* client-side ICMP destination unreachable, ignore it */
coap_log(LOG_WARNING, "coap_network_read: unreachable\n");
return -2;
}
coap_log(LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror());
goto error;
} else if (len > 0) {
packet->length = (size_t)len;
}
} else {
#endif /* WITH_CONTIKI */
#if defined(_WIN32)
DWORD dwNumberOfBytesRecvd = 0;
int r;
#endif
#if !defined(WITH_CONTIKI)
#ifdef HAVE_STRUCT_CMSGHDR
/* a buffer large enough to hold all packet info types, ipv6 is the largest */
char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
struct cmsghdr *cmsg;
struct msghdr mhdr;
struct iovec iov[1];
iov[0].iov_base = packet->payload;
iov[0].iov_len = (iov_len_t)COAP_RXBUFFER_SIZE;
memset(&mhdr, 0, sizeof(struct msghdr));
mhdr.msg_name = (struct sockaddr*)&packet->addr_info.remote.addr;
mhdr.msg_namelen = sizeof(packet->addr_info.remote.addr);
mhdr.msg_iov = iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = buf;
mhdr.msg_controllen = sizeof(buf);
/* set a big first length incase recvmsg() does not implement updating
msg_control as well as preset the first cmsg with bad data */
cmsg = (struct cmsghdr *)buf;
cmsg->cmsg_len = CMSG_LEN(sizeof(buf));
cmsg->cmsg_level = -1;
cmsg->cmsg_type = -1;
#if defined(_WIN32)
if (!lpWSARecvMsg) {
GUID wsaid = WSAID_WSARECVMSG;
DWORD cbBytesReturned = 0;
if (WSAIoctl(sock->fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &wsaid, sizeof(wsaid), &lpWSARecvMsg, sizeof(lpWSARecvMsg), &cbBytesReturned, NULL, NULL) != 0) {
coap_log(LOG_WARNING, "coap_network_read: no WSARecvMsg\n");
return -1;
}
}
r = lpWSARecvMsg(sock->fd, &mhdr, &dwNumberOfBytesRecvd, NULL /* LPWSAOVERLAPPED */, NULL /* LPWSAOVERLAPPED_COMPLETION_ROUTINE */);
if (r == 0)
len = (ssize_t)dwNumberOfBytesRecvd;
#else
len = recvmsg(sock->fd, &mhdr, 0);
#endif
#else /* ! HAVE_STRUCT_CMSGHDR */
len = recvfrom(sock->fd, packet->payload, COAP_RXBUFFER_SIZE, 0,
&packet->addr_info.remote.addr.sa,
&packet->addr_info.remote.size);
#endif /* ! HAVE_STRUCT_CMSGHDR */
if (len < 0) {
#ifdef _WIN32
if (WSAGetLastError() == WSAECONNRESET) {
#else
if (errno == ECONNREFUSED) {
#endif
/* server-side ICMP destination unreachable, ignore it. The destination address is in msg_name. */
return 0;
}
coap_log(LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror());
goto error;
} else {
#ifdef HAVE_STRUCT_CMSGHDR
int dst_found = 0;
packet->addr_info.remote.size = mhdr.msg_namelen;
packet->length = (size_t)len;
/* Walk through ancillary data records until the local interface
* is found where the data was received. */
for (cmsg = CMSG_FIRSTHDR(&mhdr); cmsg; cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
/* get the local interface for IPv6 */
if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
union {
uint8_t *c;
struct in6_pktinfo *p;
} u;
u.c = CMSG_DATA(cmsg);
packet->ifindex = (int)(u.p->ipi6_ifindex);
memcpy(&packet->addr_info.local.addr.sin6.sin6_addr,
&u.p->ipi6_addr, sizeof(struct in6_addr));
dst_found = 1;
break;
}
/* local interface for IPv4 */
#if defined(IP_PKTINFO)
if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
union {
uint8_t *c;
struct in_pktinfo *p;
} u;
u.c = CMSG_DATA(cmsg);
packet->ifindex = u.p->ipi_ifindex;
if (packet->addr_info.local.addr.sa.sa_family == AF_INET6) {
memset(packet->addr_info.local.addr.sin6.sin6_addr.s6_addr, 0, 10);
packet->addr_info.local.addr.sin6.sin6_addr.s6_addr[10] = 0xff;
packet->addr_info.local.addr.sin6.sin6_addr.s6_addr[11] = 0xff;
memcpy(packet->addr_info.local.addr.sin6.sin6_addr.s6_addr + 12,
&u.p->ipi_addr, sizeof(struct in_addr));
} else {
memcpy(&packet->addr_info.local.addr.sin.sin_addr,
&u.p->ipi_addr, sizeof(struct in_addr));
}
dst_found = 1;
break;
}
#elif defined(IP_RECVDSTADDR)
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
packet->ifindex = sock->fd;
memcpy(&packet->addr_info.local.addr.sin.sin_addr,
CMSG_DATA(cmsg), sizeof(struct in_addr));
dst_found = 1;
break;
}
#endif /* IP_PKTINFO */
if (!dst_found) {
/* cmsg_level / cmsg_type combination we do not understand
(ignore preset case for bad recvmsg() not updating cmsg) */
if (cmsg->cmsg_level != -1 && cmsg->cmsg_type != -1) {
coap_log(LOG_DEBUG,
"cmsg_level = %d and cmsg_type = %d not supported - fix\n",
cmsg->cmsg_level, cmsg->cmsg_type);
}
}
}
if (!dst_found) {
/* Not expected, but cmsg_level and cmsg_type don't match above and
may need a new case */
packet->ifindex = sock->fd;
if (getsockname(sock->fd, &packet->addr_info.local.addr.sa,
&packet->addr_info.local.size) < 0) {
coap_log(LOG_DEBUG, "Cannot determine local port\n");
}
}
#else /* ! HAVE_STRUCT_CMSGHDR */
packet->length = (size_t)len;
packet->ifindex = 0;
if (getsockname(sock->fd, &packet->addr_info.local.addr.sa,
&packet->addr_info.local.size) < 0) {
coap_log(LOG_DEBUG, "Cannot determine local port\n");
goto error;
}
#endif /* ! HAVE_STRUCT_CMSGHDR */
}
#endif /* !defined(WITH_CONTIKI) */
#ifdef WITH_CONTIKI
/* FIXME: untested, make this work */
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
if (uip_newdata()) {
uip_ipaddr_copy(&packet->addr_info.remote.addr, &UIP_IP_BUF->srcipaddr);
packet->addr_info.remote.port = UIP_UDP_BUF->srcport;
uip_ipaddr_copy(&(packet)->addr_info.local.addr, &UIP_IP_BUF->destipaddr);
packet->addr_info.local.port = UIP_UDP_BUF->destport;
len = uip_datalen();
if (len > COAP_RXBUFFER_SIZE) {
/* FIXME: we might want to send back a response */
coap_log(LOG_WARNING, "discarded oversized packet\n");
return -1;
}
((char *)uip_appdata)[len] = 0;
if (LOG_DEBUG <= coap_get_log_level()) {
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 40
#endif
unsigned char addr_str[INET6_ADDRSTRLEN + 8];
if (coap_print_addr(&packet->addr_info.remote, addr_str,
INET6_ADDRSTRLEN + 8)) {
coap_log(LOG_DEBUG, "received %zd bytes from %s\n", len, addr_str);
}
}
packet->length = len;
memcpy(&packet->payload, uip_appdata, len);
}
#undef UIP_IP_BUF
#undef UIP_UDP_BUF
#endif /* WITH_CONTIKI */
#ifndef WITH_CONTIKI
}
#endif /* WITH_CONTIKI */
if (len >= 0)
return len;
#if !defined(WITH_CONTIKI)
error:
#endif
return -1;
}
unsigned int
coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now) {
#ifndef COAP_EPOLL_SUPPORT
(void)ctx;
(void)now;
coap_log(LOG_EMERG,
"coap_io_prepare_epoll() requires libcoap compiled for using epoll\n");
return 0;
#else /* COAP_EPOLL_SUPPORT */
coap_socket_t *sockets[1];
unsigned int max_sockets = sizeof(sockets)/sizeof(sockets[0]);
unsigned int num_sockets;
unsigned int timeout;
/* Use the common logic */
timeout = coap_write(ctx, sockets, max_sockets, &num_sockets, now);
/* Save when the next expected I/O is to take place */
ctx->next_timeout = timeout ? now + timeout : 0;
if (ctx->eptimerfd != -1) {
struct itimerspec new_value;
int ret;
memset(&new_value, 0, sizeof(new_value));
coap_ticks(&now);
if (ctx->next_timeout != 0 && ctx->next_timeout > now) {
coap_tick_t rem_timeout = ctx->next_timeout - now;
/* Need to trigger an event on ctx->epfd in the future */
new_value.it_value.tv_sec = rem_timeout / COAP_TICKS_PER_SECOND;
new_value.it_value.tv_nsec = (rem_timeout % COAP_TICKS_PER_SECOND) *
1000000;
}
/* reset, or specify a future time for eptimerfd to trigger */
ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
if (ret == -1) {
coap_log(LOG_ERR,
"%s: timerfd_settime failed: %s (%d)\n",
"coap_io_prepare_epoll",
coap_socket_strerror(), errno);
}
}
return timeout;
#endif /* COAP_EPOLL_SUPPORT */
}
#if !defined(WITH_CONTIKI)
unsigned int
coap_write(coap_context_t *ctx,
coap_socket_t *sockets[],
unsigned int max_sockets,
unsigned int *num_sockets,
coap_tick_t now)
{
coap_queue_t *nextpdu;
coap_endpoint_t *ep;
coap_session_t *s, *rtmp;
coap_tick_t session_timeout;
coap_tick_t timeout = 0;
#ifdef COAP_EPOLL_SUPPORT
(void)sockets;
(void)max_sockets;
#endif /* COAP_EPOLL_SUPPORT */
*num_sockets = 0;
/* Check to see if we need to send off any Observe requests */
coap_check_notify(ctx);
if (ctx->session_timeout > 0)
session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND;
else
session_timeout = COAP_DEFAULT_SESSION_TIMEOUT * COAP_TICKS_PER_SECOND;
LL_FOREACH(ctx->endpoint, ep) {
#ifndef COAP_EPOLL_SUPPORT
if (ep->sock.flags & (COAP_SOCKET_WANT_READ | COAP_SOCKET_WANT_WRITE | COAP_SOCKET_WANT_ACCEPT)) {
if (*num_sockets < max_sockets)
sockets[(*num_sockets)++] = &ep->sock;
}
#endif /* ! COAP_EPOLL_SUPPORT */
SESSIONS_ITER_SAFE(ep->sessions, s, rtmp) {
if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
s->delayqueue == NULL &&
(s->last_rx_tx + session_timeout <= now ||
s->state == COAP_SESSION_STATE_NONE)) {
coap_session_free(s);
} else {
if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 && s->delayqueue == NULL) {
coap_tick_t s_timeout = (s->last_rx_tx + session_timeout) - now;
if (timeout == 0 || s_timeout < timeout)
timeout = s_timeout;
}
#ifndef COAP_EPOLL_SUPPORT
if (s->sock.flags & (COAP_SOCKET_WANT_READ | COAP_SOCKET_WANT_WRITE)) {
if (*num_sockets < max_sockets)
sockets[(*num_sockets)++] = &s->sock;
}
#endif /* ! COAP_EPOLL_SUPPORT */
}
}
}
SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
if (
s->type == COAP_SESSION_TYPE_CLIENT
&& s->state == COAP_SESSION_STATE_ESTABLISHED
&& ctx->ping_timeout > 0
) {
coap_tick_t s_timeout;
if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
if ((s->last_ping > 0 && s->last_pong < s->last_ping)
|| ((s->last_ping_mid = coap_session_send_ping(s)) == COAP_INVALID_TID))
{
/* Make sure the session object is not deleted in the callback */
coap_session_reference(s);
coap_session_disconnected(s, COAP_NACK_NOT_DELIVERABLE);
coap_session_release(s);
continue;
}
s->last_rx_tx = now;
s->last_ping = now;
}
s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
if (timeout == 0 || s_timeout < timeout)
timeout = s_timeout;
}
if (
s->type == COAP_SESSION_TYPE_CLIENT
&& COAP_PROTO_RELIABLE(s->proto)
&& s->state == COAP_SESSION_STATE_CSM
&& ctx->csm_timeout > 0
) {
coap_tick_t s_timeout;
if (s->csm_tx == 0) {
s->csm_tx = now;
} else if (s->csm_tx + ctx->csm_timeout * COAP_TICKS_PER_SECOND <= now) {
/* Make sure the session object is not deleted in the callback */
coap_session_reference(s);
coap_session_disconnected(s, COAP_NACK_NOT_DELIVERABLE);
coap_session_release(s);
continue;
}
s_timeout = (s->csm_tx + ctx->csm_timeout * COAP_TICKS_PER_SECOND) - now;
if (timeout == 0 || s_timeout < timeout)
timeout = s_timeout;
}
#ifndef COAP_EPOLL_SUPPORT
if (s->sock.flags & (COAP_SOCKET_WANT_READ | COAP_SOCKET_WANT_WRITE | COAP_SOCKET_WANT_CONNECT)) {
if (*num_sockets < max_sockets)
sockets[(*num_sockets)++] = &s->sock;
}
#endif /* ! COAP_EPOLL_SUPPORT */
}
nextpdu = coap_peek_next(ctx);
while (nextpdu && now >= ctx->sendqueue_basetime && nextpdu->t <= now - ctx->sendqueue_basetime) {
coap_retransmit(ctx, coap_pop_next(ctx));
nextpdu = coap_peek_next(ctx);
}
if (nextpdu && (timeout == 0 || nextpdu->t - ( now - ctx->sendqueue_basetime ) < timeout))
timeout = nextpdu->t - (now - ctx->sendqueue_basetime);
if (ctx->dtls_context) {
if (coap_dtls_is_context_timeout()) {
coap_tick_t tls_timeout = coap_dtls_get_context_timeout(ctx->dtls_context);
if (tls_timeout > 0) {
if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
coap_log(LOG_DEBUG, "** DTLS global timeout set to %dms\n",
(int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
if (timeout == 0 || tls_timeout - now < timeout)
timeout = tls_timeout - now;
}
} else {
LL_FOREACH(ctx->endpoint, ep) {
if (ep->proto == COAP_PROTO_DTLS) {
SESSIONS_ITER(ep->sessions, s, rtmp) {
if (s->proto == COAP_PROTO_DTLS && s->tls) {
coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
while (tls_timeout > 0 && tls_timeout <= now) {
coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n",
coap_session_str(s));
coap_dtls_handle_timeout(s);
if (s->tls)
tls_timeout = coap_dtls_get_timeout(s, now);
else {
tls_timeout = 0;
timeout = 1;
}
}
if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
timeout = tls_timeout - now;
}
}
}
}
SESSIONS_ITER(ctx->sessions, s, rtmp) {
if (s->proto == COAP_PROTO_DTLS && s->tls) {
coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
while (tls_timeout > 0 && tls_timeout <= now) {
coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s));
coap_dtls_handle_timeout(s);
if (s->tls)
tls_timeout = coap_dtls_get_timeout(s, now);
else {
tls_timeout = 0;
timeout = 1;
}
}
if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout))
timeout = tls_timeout - now;
}
}
}
}
return (unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
}
int
coap_run_once(coap_context_t *ctx, unsigned timeout_ms) {
#if COAP_CONSTRAINED_STACK
static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER;
# ifndef COAP_EPOLL_SUPPORT
static fd_set readfds, writefds, exceptfds;
static coap_socket_t *sockets[64];
unsigned int num_sockets = 0;
# endif /* ! COAP_EPOLL_SUPPORT */
#else /* ! COAP_CONSTRAINED_STACK */
# ifndef COAP_EPOLL_SUPPORT
fd_set readfds, writefds, exceptfds;
coap_socket_t *sockets[64];
unsigned int num_sockets = 0;
# endif /* ! COAP_EPOLL_SUPPORT */
#endif /* ! COAP_CONSTRAINED_STACK */
coap_fd_t nfds = 0;
coap_tick_t before, now;
unsigned int timeout;
#ifndef COAP_EPOLL_SUPPORT
struct timeval tv;
int result;
unsigned int i;
#endif /* ! COAP_EPOLL_SUPPORT */
#if COAP_CONSTRAINED_STACK
coap_mutex_lock(&static_mutex);
#endif /* COAP_CONSTRAINED_STACK */
coap_ticks(&before);
#ifndef COAP_EPOLL_SUPPORT
timeout = coap_write(ctx, sockets, (unsigned int)(sizeof(sockets) / sizeof(sockets[0])), &num_sockets, before);
if (timeout == 0 || timeout_ms < timeout)
timeout = timeout_ms;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
for (i = 0; i < num_sockets; i++) {
if (sockets[i]->fd + 1 > nfds)
nfds = sockets[i]->fd + 1;
if (sockets[i]->flags & COAP_SOCKET_WANT_READ)
FD_SET(sockets[i]->fd, &readfds);
if (sockets[i]->flags & COAP_SOCKET_WANT_WRITE)
FD_SET(sockets[i]->fd, &writefds);
if (sockets[i]->flags & COAP_SOCKET_WANT_ACCEPT)
FD_SET(sockets[i]->fd, &readfds);
if (sockets[i]->flags & COAP_SOCKET_WANT_CONNECT) {
FD_SET(sockets[i]->fd, &writefds);
FD_SET(sockets[i]->fd, &exceptfds);
}
}
if ( timeout > 0 ) {
if (timeout == COAP_RUN_NONBLOCK)
timeout = 0;
tv.tv_usec = (timeout % 1000) * 1000;
tv.tv_sec = (long)(timeout / 1000);
}
result = select(nfds, &readfds, &writefds, &exceptfds, timeout > 0 ? &tv : NULL);
if (result < 0) { /* error */
#ifdef _WIN32
if (WSAGetLastError() != WSAEINVAL) { /* May happen because of ICMP */
#else
if (errno != EINTR) {
#endif
coap_log(LOG_DEBUG, "%s", coap_socket_strerror());
#if COAP_CONSTRAINED_STACK
coap_mutex_unlock(&static_mutex);
#endif /* COAP_CONSTRAINED_STACK */
return -1;
}
}
if (result > 0) {
for (i = 0; i < num_sockets; i++) {
if ((sockets[i]->flags & COAP_SOCKET_WANT_READ) && FD_ISSET(sockets[i]->fd, &readfds))
sockets[i]->flags |= COAP_SOCKET_CAN_READ;
if ((sockets[i]->flags & COAP_SOCKET_WANT_ACCEPT) && FD_ISSET(sockets[i]->fd, &readfds))
sockets[i]->flags |= COAP_SOCKET_CAN_ACCEPT;
if ((sockets[i]->flags & COAP_SOCKET_WANT_WRITE) && FD_ISSET(sockets[i]->fd, &writefds))
sockets[i]->flags |= COAP_SOCKET_CAN_WRITE;
if ((sockets[i]->flags & COAP_SOCKET_WANT_CONNECT) && (FD_ISSET(sockets[i]->fd, &writefds) || FD_ISSET(sockets[i]->fd, &exceptfds)))
sockets[i]->flags |= COAP_SOCKET_CAN_CONNECT;
}
}
coap_ticks(&now);
coap_read(ctx, now);
#else /* COAP_EPOLL_SUPPORT */
timeout = coap_io_prepare_epoll(ctx, before);
if (timeout == 0 || timeout_ms < timeout)
timeout = timeout_ms;
do {
struct epoll_event events[COAP_MAX_EPOLL_EVENTS];
int etimeout = timeout;
/* Potentially adjust based on what the caller wants */
if (timeout_ms == COAP_RUN_BLOCK)
etimeout = -1;
else if (timeout_ms == COAP_RUN_NONBLOCK)
etimeout = 0;
nfds = epoll_wait(ctx->epfd, events, COAP_MAX_EPOLL_EVENTS, etimeout);
if (nfds < 0) {
if (errno != EINTR) {
coap_log (LOG_ERR, "epoll_wait: unexpected error: %s (%d)\n",
coap_socket_strerror(), nfds);
}
break;
}
coap_io_do_events(ctx, events, nfds);
/*
* reset to COAP_RUN_NONBLOCK (which causes etimeout to become 0)
* incase we have to do another iteration
* (COAP_MAX_EPOLL_EVENTS insufficient)
*/
timeout_ms = COAP_RUN_NONBLOCK;
/* Keep retrying until less than COAP_MAX_EPOLL_EVENTS are returned */
} while (nfds == COAP_MAX_EPOLL_EVENTS);
coap_ticks(&now);
#endif /* COAP_EPOLL_SUPPORT */
#if COAP_CONSTRAINED_STACK
coap_mutex_unlock(&static_mutex);
#endif /* COAP_CONSTRAINED_STACK */
return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND);
}
#else /* WITH_CONTIKI */
int coap_run_once(coap_context_t *ctx, unsigned int timeout_ms) {
coap_tick_t now;
coap_ticks(&now);
/* There is something to read on the endpoint */
ctx->endpoint->sock.flags |= COAP_SOCKET_CAN_READ;
/* read in, and send off any responses */
coap_read(ctx, now); /* read received data */
return -1;
}
unsigned int
coap_write(coap_context_t *ctx,
coap_socket_t *sockets[],
unsigned int max_sockets,
unsigned int *num_sockets,
coap_tick_t now)
{
*num_sockets = 0;
return 0;
}
#endif /* WITH_CONTIKI */
#ifdef _WIN32
static const char *coap_socket_format_errno(int error) {
static char szError[256];
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)
strcpy(szError, "Unknown error");
return szError;
}
const char *coap_socket_strerror(void) {
return coap_socket_format_errno(WSAGetLastError());
}
#else
#ifndef WITH_CONTIKI
static const char *coap_socket_format_errno(int error) {
return strerror(error);
}
#endif /* WITH_CONTIKI */
const char *coap_socket_strerror(void) {
return strerror(errno);
}
#endif
ssize_t
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
const uint8_t *data, size_t data_len) {
return session->context->network_send(sock, session, data, data_len);
}
#undef SIN6
coap_address_init
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:71
coap_is_mcast
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:54
coap_address_isany
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
Definition: address.h:146
coap_address_copy
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:108
coap_debug_send_packet
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
Definition: coap_debug.c:852
coap_internal.h
Pulls together all the internal only header files.
coap_socket_connect_udp
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)
Definition: coap_io.c:529
coap_free_endpoint
void coap_free_endpoint(coap_endpoint_t *ep)
Definition: coap_session.c:1006
coap_socket_bind_tcp
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:412
coap_socket_read
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:735
coap_socket_close
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:631
coap_socket_send
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
Definition: coap_io.c:1657
coap_mfree_endpoint
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
Definition: coap_io.c:180
coap_io_prepare_epoll
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...
Definition: coap_io.c:1248
coap_packet_get_memmapped
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.
Definition: coap_io.c:1002
coap_socket_write
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:690
SOL_IP
#define SOL_IP
Definition: coap_io.c:796
coap_run_once
int coap_run_once(coap_context_t *ctx, unsigned timeout_ms)
Definition: coap_io.c:1470
coap_malloc_endpoint
struct coap_endpoint_t * coap_malloc_endpoint(void)
Definition: coap_io.c:175
coap_socket_connect_tcp1
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)
Definition: coap_io.c:264
coap_network_read
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:1008
coap_socket_bind_udp
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:185
coap_network_send
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Definition: coap_io.c:828
coap_write
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...
Definition: coap_io.c:1294
coap_socket_accept_tcp
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:492
iov_len_t
#define iov_len_t
Definition: coap_io.c:824
coap_socket_strerror
const char * coap_socket_strerror(void)
Definition: coap_io.c:1651
coap_socket_format_errno
static const char * coap_socket_format_errno(int error)
Definition: coap_io.c:1646
coap_socket_connect_tcp2
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:372
COAP_SOCKET_MULTICAST
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Definition: coap_io.h:88
COAP_SOCKET_WANT_ACCEPT
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
Definition: coap_io.h:82
coap_closesocket
#define coap_closesocket
Definition: coap_io.h:37
COAP_MAX_EPOLL_EVENTS
#define COAP_MAX_EPOLL_EVENTS
Definition: coap_io.h:27
COAP_SOCKET_CAN_WRITE
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
Definition: coap_io.h:85
COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE
Definition: coap_io.h:18
COAP_SOCKET_WANT_READ
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
Definition: coap_io.h:80
COAP_SOCKET_ERROR
#define COAP_SOCKET_ERROR
Definition: coap_io.h:38
COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:228
COAP_SOCKET_CAN_ACCEPT
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
Definition: coap_io.h:86
COAP_SOCKET_WANT_WRITE
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
Definition: coap_io.h:81
COAP_SOCKET_CAN_CONNECT
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
Definition: coap_io.h:87
coap_fd_t
int coap_fd_t
Definition: coap_io.h:36
coap_epoll_ctl_mod
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
COAP_SOCKET_WANT_CONNECT
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
Definition: coap_io.h:83
COAP_SOCKET_CAN_READ
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
Definition: coap_io.h:84
COAP_SOCKET_CONNECTED
#define COAP_SOCKET_CONNECTED
the socket is connected
Definition: coap_io.h:79
COAP_SOCKET_EMPTY
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
Definition: coap_io.h:76
COAP_INVALID_SOCKET
#define COAP_INVALID_SOCKET
Definition: coap_io.h:39
coap_dtls_get_context_timeout
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED)
Definition: coap_notls.c:118
coap_dtls_get_timeout
coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED, coap_tick_t now UNUSED)
Definition: coap_notls.c:123
coap_dtls_handle_timeout
void coap_dtls_handle_timeout(coap_session_t *session UNUSED)
Definition: coap_notls.c:127
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
coap_session_free
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:168
coap_session_send_ping
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:314
coap_session_disconnected
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:387
INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN
SESSIONS_ITER_SAFE
#define SESSIONS_ITER_SAFE(e, el, rtmp)
Definition: coap_session.h:489
COAP_SESSION_STATE_CSM
#define COAP_SESSION_STATE_CSM
Definition: coap_session.h:56
COAP_SESSION_TYPE_CLIENT
#define COAP_SESSION_TYPE_CLIENT
coap_session_type_t values
Definition: coap_session.h:45
COAP_PROTO_RELIABLE
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:39
COAP_SESSION_STATE_NONE
#define COAP_SESSION_STATE_NONE
coap_session_state_t values
Definition: coap_session.h:53
COAP_SESSION_STATE_ESTABLISHED
#define COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:57
SESSIONS_ITER
#define SESSIONS_ITER(e, el, rtmp)
Definition: coap_session.h:486
COAP_SESSION_TYPE_SERVER
#define COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:46
COAP_DEFAULT_SESSION_TIMEOUT
#define COAP_DEFAULT_SESSION_TIMEOUT
Definition: coap_session.h:34
coap_ticks
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:93
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:108
coap_dtls_is_context_timeout
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition: coap_notls.c:114
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:61
coap_print_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.
Definition: coap_debug.c:161
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:1049
coap_log
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
LOG_ERR
@ LOG_ERR
Error.
Definition: coap_debug.h:51
LOG_CRIT
@ LOG_CRIT
Critical.
Definition: coap_debug.h:50
LOG_ALERT
@ LOG_ALERT
Alert.
Definition: coap_debug.h:49
LOG_WARNING
@ LOG_WARNING
Warning.
Definition: coap_debug.h:52
LOG_DEBUG
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:55
LOG_EMERG
@ LOG_EMERG
Emergency.
Definition: coap_debug.h:48
coap_malloc_type
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.
COAP_ENDPOINT
@ COAP_ENDPOINT
Definition: mem.h:36
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_peek_next
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:262
coap_read
void coap_read(coap_context_t *ctx, coap_tick_t now)
For applications with their own message loop, reads all data from the network.
Definition: net.c:1397
coap_pop_next
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition: net.c:270
coap_io_do_events
void coap_io_do_events(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: net.c:1458
coap_retransmit
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:1021
COAP_RUN_BLOCK
#define COAP_RUN_BLOCK
Definition: net.h:570
COAP_RUN_NONBLOCK
#define COAP_RUN_NONBLOCK
Definition: net.h:571
COAP_PROTO_DTLS
#define COAP_PROTO_DTLS
Definition: pdu.h:343
COAP_INVALID_TID
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:241
coap_check_notify
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:864
coap_addr_tuple_t::remote
coap_address_t remote
remote address and port
Definition: coap_io.h:50
coap_addr_tuple_t::local
coap_address_t local
local address and port
Definition: coap_io.h:51
coap_address_t
multi-purpose address abstraction
Definition: address.h:62
coap_address_t::size
socklen_t size
size of addr
Definition: address.h:63
coap_address_t::sin
struct sockaddr_in sin
Definition: address.h:66
coap_address_t::sin6
struct sockaddr_in6 sin6
Definition: address.h:67
coap_address_t::sa
struct sockaddr sa
Definition: address.h:65
coap_address_t::addr
union coap_address_t::@0 addr
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:147
coap_context_t::sendqueue_basetime
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:163
coap_context_t::csm_timeout
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
Definition: net.h:209
coap_context_t::sessions
coap_session_t * sessions
client sessions
Definition: net.h:166
coap_context_t::ping_timeout
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
Definition: net.h:208
coap_context_t::network_send
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Definition: net.h:191
coap_context_t::dtls_context
void * dtls_context
Definition: net.h:199
coap_context_t::endpoint
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:165
coap_context_t::session_timeout
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Definition: net.h:205
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session_internal.h:29
coap_endpoint_t::context
struct coap_context_t * context
endpoint's context
Definition: coap_session_internal.h:31
coap_endpoint_t::sessions
struct coap_session_t * sessions
hash table or list of active sessions
Definition: coap_session_internal.h:36
coap_endpoint_t::sock
coap_socket_t sock
socket object for the interface, if any
Definition: coap_session_internal.h:34
coap_endpoint_t::proto
coap_proto_t proto
protocol used on this interface
Definition: coap_session_internal.h:32
coap_packet_t
Definition: coap_io.h:217
coap_packet_t::length
size_t length
length of payload
Definition: coap_io.h:220
coap_packet_t::addr_info
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:218
coap_packet_t::payload
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
Definition: coap_io.h:221
coap_packet_t::ifindex
int ifindex
the interface index
Definition: coap_io.h:219
coap_queue_t
Queue entry.
Definition: net.h:38
coap_queue_t::t
coap_tick_t t
when to send PDU for the next time
Definition: net.h:40
coap_session_t
Definition: coap_session.h:59
coap_session_t::last_pong
coap_tick_t last_pong
Definition: coap_session.h:85
coap_session_t::last_rx_tx
coap_tick_t last_rx_tx
Definition: coap_session.h:82
coap_session_t::sock
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:70
coap_session_t::state
coap_session_state_t state
current state of relationaship with peer
Definition: coap_session.h:62
coap_session_t::addr_info
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:68
coap_session_t::proto
coap_proto_t proto
protocol used
Definition: coap_session.h:60
coap_session_t::ref
unsigned ref
reference count from queues
Definition: coap_session.h:63
coap_session_t::delayqueue
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition: coap_session.h:77
coap_session_t::context
struct coap_context_t * context
session's context
Definition: coap_session.h:72
coap_session_t::tls
void * tls
security parameters
Definition: coap_session.h:73
coap_session_t::last_ping
coap_tick_t last_ping
Definition: coap_session.h:84
coap_session_t::type
coap_session_type_t type
client or server side socket
Definition: coap_session.h:61
coap_session_t::last_ping_mid
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
Definition: coap_session.h:76
coap_session_t::csm_tx
coap_tick_t csm_tx
Definition: coap_session.h:86
coap_session_t::ifindex
int ifindex
interface index
Definition: coap_session.h:69
coap_socket_t
Definition: coap_io.h:54
coap_socket_t::fd
coap_fd_t fd
Definition: coap_io.h:60
coap_socket_t::endpoint
struct coap_endpoint_t * endpoint
Definition: coap_io.h:67
coap_socket_t::session
struct coap_session_t * session
Definition: coap_io.h:63
coap_socket_t::flags
coap_socket_flags_t flags
Definition: coap_io.h:62
in6_pktinfo
Definition: coap_io.c:782
in6_pktinfo::ipi6_addr
struct in6_addr ipi6_addr
Definition: coap_io.c:783
in6_pktinfo::ipi6_ifindex
unsigned int ipi6_ifindex
Definition: coap_io.c:784
in_pktinfo
Definition: coap_io.c:787
in_pktinfo::ipi_spec_dst
struct in_addr ipi_spec_dst
Definition: coap_io.c:789
in_pktinfo::ipi_addr
struct in_addr ipi_addr
Definition: coap_io.c:790
in_pktinfo::ipi_ifindex
int ipi_ifindex
Definition: coap_io.c:788
uint32_t
unsigned int uint32_t
Definition: uthash.h:78
uint8_t
unsigned char uint8_t
Definition: uthash.h:79
LL_FOREACH
#define LL_FOREACH(head, el)
Definition: utlist.h:413