diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 145dd359..28e567e9 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -11,11 +11,14 @@ #include "coap_io.h" +#include "coap_time.h" #include "pdu.h" struct coap_endpoint_t; struct coap_contex_t; +#define COAP_DEFAULT_SESSION_TIMEOUT 300 + typedef uint8_t coap_proto_t; /** * coap_proto_t values @@ -61,6 +64,7 @@ typedef struct coap_session_t { struct coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ coap_pdu_queue_t *sendqueue; /**< list of messages waiting to be sent */ + coap_tick_t last_rx_tx; uint8_t *psk_identity; size_t psk_identity_len; uint8_t *psk_key; @@ -73,7 +77,7 @@ typedef struct coap_session_t { * @param session The CoAP session. * @return same as session */ -coap_session_t *coap_session_reference( coap_session_t *session ); +coap_session_t *coap_session_reference(coap_session_t *session); /** * Decrement reference counter on a session. @@ -82,21 +86,21 @@ coap_session_t *coap_session_reference( coap_session_t *session ); * * @param session The CoAP session. */ -void coap_session_release( coap_session_t *session ); +void coap_session_release(coap_session_t *session); /** * Notify session that it has failed connecting or has been disconnected. * * @param session The CoAP session. */ -void coap_session_disconnected( coap_session_t *session ); +void coap_session_disconnected(coap_session_t *session); /** * Notify session that it has just connected or reconnected. * * @param session The CoAP session. */ -void coap_session_connected( coap_session_t *session ); +void coap_session_connected(coap_session_t *session); /** * Creates a new client session to the designated server. @@ -148,8 +152,8 @@ coap_session_t *coap_new_client_session_psk( * @return The number of bytes written on success, or a value * less than zero on error. */ -ssize_t coap_session_send( coap_session_t *session, - const uint8_t *data, size_t datalen ); +ssize_t coap_session_send(coap_session_t *session, + const uint8_t *data, size_t datalen); /** * Get session description. @@ -157,10 +161,10 @@ ssize_t coap_session_send( coap_session_t *session, * @param session The CoAP session. * @return description string */ -const char *coap_session_str( const coap_session_t *session ); +const char *coap_session_str(const coap_session_t *session); ssize_t -coap_session_delay_pdu( coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt ); +coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt); /** * Abstraction of virtual endpoint that can be attached to coap_context_t. The @@ -184,9 +188,9 @@ typedef struct coap_endpoint_t { * @param proto Protocol used on this endpoint */ -coap_endpoint_t *coap_new_endpoint( struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto ); +coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto); -void coap_free_endpoint( coap_endpoint_t *ep ); +void coap_free_endpoint(coap_endpoint_t *ep); /** * Get endpoint description. @@ -194,7 +198,7 @@ void coap_free_endpoint( coap_endpoint_t *ep ); * @param session The CoAP endpoint. * @return description string */ -const char *coap_endpoint_str( const coap_endpoint_t *endpoint ); +const char *coap_endpoint_str(const coap_endpoint_t *endpoint); /** * Lookup the server session for the packet received on an endpoint, or create @@ -204,10 +208,16 @@ const char *coap_endpoint_str( const coap_endpoint_t *endpoint ); * @param packet Received packet. * @return The CoAP session. */ -coap_session_t *coap_endpoint_get_session( coap_endpoint_t *endpoint, const struct coap_packet_t *packet ); +coap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint, + const struct coap_packet_t *packet, coap_tick_t now); -coap_session_t *coap_endpoint_new_dtls_session( coap_endpoint_t *endpoint, const struct coap_packet_t *packet ); +coap_session_t *coap_endpoint_new_dtls_session(coap_endpoint_t *endpoint, + const struct coap_packet_t *packet, coap_tick_t now); + +coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx, + const struct coap_address_t *remote_addr, int ifindex); + +void coap_session_free(coap_session_t *session); -coap_session_t *coap_session_get_by_peer( struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex ); #endif /* _SESSION_H */ diff --git a/include/coap/net.h b/include/coap/net.h index 45edfc44..f1acfee2 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -140,6 +140,11 @@ typedef struct coap_context_t { size_t psk_hint_len; uint8_t *psk_key; size_t psk_key_len; + + unsigned int session_timeout; /**< Number of seconds of inactivity after which an unused session will be closed. 0 means use default. */ + unsigned int max_idle_sessions; /**< Maximum number of simultaneous unused sessions per endpoint. 0 means no maximum. */ + unsigned int keepalive_interval; /**< Minimum interval before sending a keepalive message. 0 means disabled. */ + void *app; /**< application-specific data */ } coap_context_t; @@ -347,7 +352,7 @@ coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node); /** * Reads all data from the network. */ -void coap_read(coap_context_t *context); +void coap_read(coap_context_t *context, coap_tick_t now); /** * The main message processing loop. diff --git a/src/coap_io.c b/src/coap_io.c index bf53a312..6757a13a 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -49,7 +49,7 @@ #include "utlist.h" #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) -/* define generic PKTINFO for IPv4 */ + /* define generic PKTINFO for IPv4 */ #if defined(IP_PKTINFO) # define GEN_IP_PKTINFO IP_PKTINFO #elif defined(IP_RECVDSTADDR) @@ -68,13 +68,13 @@ #endif /* IPV6_RECVPKTINFO */ #endif -void coap_free_endpoint( coap_endpoint_t *ep ); +void coap_free_endpoint(coap_endpoint_t *ep); #ifdef WITH_CONTIKI static int ep_initialized = 0; struct coap_endpoint_t * -coap_malloc_endpoint() { + coap_malloc_endpoint() { static struct coap_endpoint_t ep; if (ep_initialized) { @@ -91,169 +91,166 @@ coap_mfree_endpoint(struct coap_endpoint_t *ep) { } 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 ); +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" ); + 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 ); + coap_address_init(&bound_addr); + uip_ipaddr_copy(&bound_addr->addr, &listen_addr->addr); bound_addr->addr.port = listen_addr->addr.port; - udp_bind( (struct uip_udp_conn *)sock->conn, bound_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 ) -{ +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; } -void coap_socket_close( coap_socket_t *sock ) { - if ( sock->conn ) - uip_udp_remove( (struct uip_udp_conn *)sock->conn ); +void coap_socket_close(coap_socket_t *sock) { + if (sock->conn) + uip_udp_remove((struct uip_udp_conn *)sock->conn); } #else struct coap_endpoint_t * -coap_malloc_endpoint( void ) { - return ( struct coap_endpoint_t * )coap_malloc( sizeof( struct coap_endpoint_t ) ); + coap_malloc_endpoint(void) { + return (struct coap_endpoint_t *)coap_malloc(sizeof(struct coap_endpoint_t)); } void -coap_mfree_endpoint( struct coap_endpoint_t *ep ) { - coap_free( ep ); +coap_mfree_endpoint(struct coap_endpoint_t *ep) { + coap_free(ep); } int -coap_socket_bind_udp( coap_socket_t *sock, +coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, - coap_address_t *bound_addr ) { + coap_address_t *bound_addr) { int on = 1, off = 0; - 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" ); + 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"); goto error; } - 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" ); + 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"); - switch ( listen_addr->addr.sa.sa_family ) { + 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\n" ); + 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\n"); 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\n" ); - 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\n" ); - 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 */ + 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\n"); + 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\n"); + 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" ); + coap_log(LOG_ALERT, "coap_socket_bind_udp: unsupported sa_family\n"); } - if ( bind( sock->fd, &listen_addr->addr.sa, listen_addr->size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_WARNING, "coap_socket_bind_udp: bind" ); + if (bind(sock->fd, &listen_addr->addr.sa, listen_addr->size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_socket_bind_udp: bind"); 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: cannot determine local address" ); + 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: cannot determine local address"); goto error; } return 1; error: - coap_socket_close( sock ); + coap_socket_close(sock); return 0; } int -coap_socket_connect_udp( coap_socket_t *sock, - const coap_address_t *local_if, - const coap_address_t *server, - int default_port, - coap_address_t *local_addr, - coap_address_t *remote_addr ) -{ +coap_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; coap_address_t connect_addr; - coap_address_copy( &connect_addr, server ); - - 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" ); + coap_address_copy(&connect_addr, server); + + 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"); goto error; } - switch ( connect_addr.addr.sa.sa_family ) { + 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 ); + 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 ); + 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\n" ); + 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\n"); break; default: - coap_log( LOG_ALERT, "coap_socket_connect_udp: unsupported sa_family\n" ); + coap_log(LOG_ALERT, "coap_socket_connect_udp: unsupported sa_family\n"); } - if ( 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_udp: setsockopt SO_REUSEADDR" ); - if ( bind( sock->fd, &local_if->addr.sa, local_if->size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_WARNING, "coap_socket_connect_udp: bind" ); + if (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_udp: setsockopt SO_REUSEADDR"); + if (bind(sock->fd, &local_if->addr.sa, local_if->size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_socket_connect_udp: bind"); goto error; } } - if ( connect( sock->fd, &connect_addr.addr.sa, connect_addr.size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_WARNING, "coap_socket_connect_udp: connect" ); + if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_socket_connect_udp: connect"); goto error; } - if ( getsockname( sock->fd, &local_addr->addr.sa, &local_addr->size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_WARNING, "coap_new_session: getsockname" ); + if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_new_session: getsockname"); } - if ( getpeername( sock->fd, &remote_addr->addr.sa, &remote_addr->size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_WARNING, "coap_new_session: getpeername" ); + if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_new_session: getpeername"); } return 1; error: - coap_socket_close( sock ); + coap_socket_close(sock); return 0; } -void coap_socket_close( coap_socket_t *sock ) { - if ( sock->fd != COAP_INVALID_SOCKET ) - coap_closesocket( sock->fd ); +void coap_socket_close(coap_socket_t *sock) { + if (sock->fd != COAP_INVALID_SOCKET) + coap_closesocket(sock->fd); } #endif /* WITH_CONTIKI */ @@ -287,7 +284,7 @@ struct in_pktinfo { #if defined(_WIN32) #include -static __declspec( thread ) LPFN_WSARECVMSG lpWSARecvMsg = NULL; +static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL; /* Map struct WSABUF fields to their posix counterpart */ #define msghdr _WSAMSG #define msg_name name @@ -308,21 +305,21 @@ static __declspec( thread ) LPFN_WSARECVMSG lpWSARecvMsg = NULL; #endif ssize_t -coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen ) { +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() ) { + if (!coap_debug_send_packet()) { bytes_written = (ssize_t)datalen; - } else if ( sock->flags & COAP_SOCKET_CONNECTED ) { + } else if (sock->flags & COAP_SOCKET_CONNECTED) { #ifdef _WIN32 - bytes_written = send( sock->fd, (const char *)data, (int)datalen, 0 ); + bytes_written = send(sock->fd, (const char *)data, (int)datalen, 0); #else - bytes_written = send( sock->fd, data, datalen, 0 ); + bytes_written = send(sock->fd, data, datalen, 0); #endif } else { #ifndef WITH_CONTIKI /* a buffer large enough to hold all packet info types, ipv6 is the largest */ - char buf[CMSG_SPACE( sizeof( struct in6_pktinfo ) )]; + char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; #ifdef _WIN32 DWORD dwNumberOfBytesSent = 0; int r; @@ -330,53 +327,53 @@ coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uin struct msghdr mhdr; struct iovec iov[1]; - assert( session ); + assert(session); iov[0].iov_base = (uint8_t*)data; iov[0].iov_len = (iov_len_t)datalen; - memset( &mhdr, 0, sizeof( struct msghdr ) ); + memset(&mhdr, 0, sizeof(struct msghdr)); mhdr.msg_name = (void *)&session->remote_addr.addr; mhdr.msg_namelen = session->remote_addr.size; mhdr.msg_iov = iov; mhdr.msg_iovlen = 1; - if ( !coap_address_isany( &session->local_addr ) && !coap_is_mcast( &session->local_addr ) ) switch ( session->local_addr.addr.sa.sa_family ) { + if (!coap_address_isany(&session->local_addr) && !coap_is_mcast(&session->local_addr)) switch (session->local_addr.addr.sa.sa_family) { case AF_INET6: { struct cmsghdr *cmsg; - if ( IN6_IS_ADDR_V4MAPPED( &session->local_addr.addr.sin6.sin6_addr ) ) { + if (IN6_IS_ADDR_V4MAPPED(&session->local_addr.addr.sin6.sin6_addr)) { struct in_pktinfo *pktinfo; mhdr.msg_control = buf; - mhdr.msg_controllen = CMSG_SPACE( sizeof( struct in_pktinfo ) ); + mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); - cmsg = CMSG_FIRSTHDR( &mhdr ); + cmsg = CMSG_FIRSTHDR(&mhdr); cmsg->cmsg_level = SOL_IP; cmsg->cmsg_type = IP_PKTINFO; - cmsg->cmsg_len = CMSG_LEN( sizeof( struct in_pktinfo ) ); + cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); - pktinfo = ( struct in_pktinfo * )CMSG_DATA( cmsg ); - memset( pktinfo, 0, sizeof( struct in_pktinfo ) ); + pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg); + memset(pktinfo, 0, sizeof(struct in_pktinfo)); pktinfo->ipi_ifindex = session->ifindex; - memcpy( &pktinfo->ipi_spec_dst, session->local_addr.addr.sin6.sin6_addr.s6_addr + 12, sizeof( pktinfo->ipi_spec_dst ) ); + memcpy(&pktinfo->ipi_spec_dst, session->local_addr.addr.sin6.sin6_addr.s6_addr + 12, sizeof(pktinfo->ipi_spec_dst)); } else { struct in6_pktinfo *pktinfo; mhdr.msg_control = buf; - mhdr.msg_controllen = CMSG_SPACE( sizeof( struct in6_pktinfo ) ); + mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); - cmsg = CMSG_FIRSTHDR( &mhdr ); + cmsg = CMSG_FIRSTHDR(&mhdr); cmsg->cmsg_level = IPPROTO_IPV6; cmsg->cmsg_type = IPV6_PKTINFO; - cmsg->cmsg_len = CMSG_LEN( sizeof( struct in6_pktinfo ) ); + cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); - pktinfo = ( struct in6_pktinfo * )CMSG_DATA( cmsg ); - memset( pktinfo, 0, sizeof( struct in6_pktinfo ) ); + pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg); + memset(pktinfo, 0, sizeof(struct in6_pktinfo)); pktinfo->ipi6_ifindex = session->ifindex; - memcpy( &pktinfo->ipi6_addr, &session->local_addr.addr.sin6.sin6_addr, sizeof( pktinfo->ipi6_addr ) ); + memcpy(&pktinfo->ipi6_addr, &session->local_addr.addr.sin6.sin6_addr, sizeof(pktinfo->ipi6_addr)); } break; } @@ -387,49 +384,49 @@ coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uin struct in_pktinfo *pktinfo; mhdr.msg_control = buf; - mhdr.msg_controllen = CMSG_SPACE( sizeof( struct in_pktinfo ) ); + mhdr.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); - cmsg = CMSG_FIRSTHDR( &mhdr ); + cmsg = CMSG_FIRSTHDR(&mhdr); cmsg->cmsg_level = SOL_IP; cmsg->cmsg_type = IP_PKTINFO; - cmsg->cmsg_len = CMSG_LEN( sizeof( struct in_pktinfo ) ); + cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); - pktinfo = ( struct in_pktinfo * )CMSG_DATA( cmsg ); - memset( pktinfo, 0, sizeof( struct in_pktinfo ) ); + pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg); + memset(pktinfo, 0, sizeof(struct in_pktinfo)); pktinfo->ipi_ifindex = session->ifindex; - memcpy( &pktinfo->ipi_spec_dst, &session->local_addr.addr.sin.sin_addr, sizeof( pktinfo->ipi_spec_dst ) ); + memcpy(&pktinfo->ipi_spec_dst, &session->local_addr.addr.sin.sin_addr, sizeof(pktinfo->ipi_spec_dst)); #endif /* IP_PKTINFO */ break; } default: /* error */ - coap_log( LOG_WARNING, "protocol not supported\n" ); + coap_log(LOG_WARNING, "protocol not supported\n"); bytes_written = -1; } #ifdef _WIN32 - r = WSASendMsg( sock->fd, &mhdr, 0 /*dwFlags*/, &dwNumberOfBytesSent, NULL /*lpOverlapped*/, NULL /*lpCompletionRoutine*/ ); - if ( r == 0 ) + 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 - bytes_written = sendmsg( sock->fd, &mhdr, 0 ); + bytes_written = sendmsg(sock->fd, &mhdr, 0); #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->remote_addr.addr, session->remote_addr.port ); + uip_udp_packet_sendto((struct uip_udp_conn *)sock->conn, data, datalen, + &session->remote_addr.addr, session->remote_addr.port); bytes_written = datalen; #endif /* WITH_CONTIKI */ } - if ( bytes_written < 0 ) - coap_log( LOG_CRIT, "coap_network_send: %s\n", coap_socket_strerror() ); + if (bytes_written < 0) + coap_log(LOG_CRIT, "coap_network_send: %s\n", coap_socket_strerror()); return bytes_written; } @@ -478,9 +475,9 @@ coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length = packet->length; } -void coap_packet_set_addr( coap_packet_t *packet, const coap_address_t *src, const coap_address_t *dst ) { - coap_address_copy( &packet->src, src ); - coap_address_copy( &packet->dst, dst ); +void coap_packet_set_addr(coap_packet_t *packet, const coap_address_t *src, const coap_address_t *dst) { + coap_address_copy(&packet->src, src); + coap_address_copy(&packet->dst, dst); } ssize_t @@ -507,14 +504,14 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { coap_address_init(&(*packet)->dst); /* the local interface address */ coap_address_init(&(*packet)->src); /* the remote peer */ - if ( sock->flags & COAP_SOCKET_CONNECTED ) { + if (sock->flags & COAP_SOCKET_CONNECTED) { #ifdef _WIN32 - len = recv( sock->fd, (char *)(*packet)->payload, (int)coap_get_max_packetlength( *packet ), 0 ); + len = recv(sock->fd, (char *)(*packet)->payload, (int)coap_get_max_packetlength(*packet), 0); #else - len = recv( sock->fd, (*packet)->payload, coap_get_max_packetlength( *packet ), 0 ); + len = recv(sock->fd, (*packet)->payload, coap_get_max_packetlength(*packet), 0); #endif - if ( len < 0 ) { - } else if ( len > 0 ) { + if (len < 0) { + } else if (len > 0) { (*packet)->length = (size_t)len; } } else { @@ -524,52 +521,52 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { #endif #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) /* a buffer large enough to hold all packet info types, ipv6 is the largest */ - char buf[CMSG_SPACE( sizeof( struct in6_pktinfo ) )]; + char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; struct msghdr mhdr; struct iovec iov[1]; - iov[0].iov_base = ( *packet )->payload; - iov[0].iov_len = (iov_len_t)coap_get_max_packetlength( *packet ); + iov[0].iov_base = (*packet)->payload; + iov[0].iov_len = (iov_len_t)coap_get_max_packetlength(*packet); - memset( &mhdr, 0, sizeof( struct msghdr ) ); + memset(&mhdr, 0, sizeof(struct msghdr)); - mhdr.msg_name = ( struct sockaddr* )&( *packet )->src.addr.st; - mhdr.msg_namelen = sizeof( ( *packet )->src.addr.st ); + mhdr.msg_name = (struct sockaddr*)&(*packet)->src.addr.st; + mhdr.msg_namelen = sizeof((*packet)->src.addr.st); mhdr.msg_iov = iov; mhdr.msg_iovlen = 1; mhdr.msg_control = buf; - mhdr.msg_controllen = sizeof( buf ); + mhdr.msg_controllen = sizeof(buf); #if defined(_WIN32) - if ( !lpWSARecvMsg ) { + 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" ); + 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 ) + 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 ); + len = recvmsg(sock->fd, &mhdr, 0); #endif - if ( len < 0 ) { - coap_log( LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror() ); + if (len < 0) { + coap_log(LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror()); goto error; } else { struct cmsghdr *cmsg; - ( *packet )->src.size = mhdr.msg_namelen; + (*packet)->src.size = mhdr.msg_namelen; /* use getsockname() to get the local port */ - ( *packet )->dst.size = sizeof( ( *packet )->dst.addr ); - if ( getsockname( sock->fd, &( *packet )->dst.addr.sa, &( *packet )->dst.size ) == COAP_SOCKET_ERROR ) { - coap_log( LOG_DEBUG, "cannot determine local port\n" ); + (*packet)->dst.size = sizeof((*packet)->dst.addr); + if (getsockname(sock->fd, &(*packet)->dst.addr.sa, &(*packet)->dst.size) == COAP_SOCKET_ERROR) { + coap_log(LOG_DEBUG, "cannot determine local port\n"); goto error; } @@ -577,43 +574,43 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { /* 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 ) ) { + 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 ) { + 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 )->dst.addr.sin6.sin6_addr, &u.p->ipi6_addr, sizeof( struct in6_addr ) ); + u.c = CMSG_DATA(cmsg); + (*packet)->ifindex = (int)(u.p->ipi6_ifindex); + memcpy(&(*packet)->dst.addr.sin6.sin6_addr, &u.p->ipi6_addr, sizeof(struct in6_addr)); break; } /* local interface for IPv4 */ #if defined(IP_PKTINFO) - if ( cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == 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 )->dst.addr.sa.sa_family == AF_INET6 ) { - memset( ( *packet )->dst.addr.sin6.sin6_addr.s6_addr, 0, 10 ); - ( *packet )->dst.addr.sin6.sin6_addr.s6_addr[10] = 0xff; - ( *packet )->dst.addr.sin6.sin6_addr.s6_addr[11] = 0xff; - memcpy( ( *packet )->dst.addr.sin6.sin6_addr.s6_addr + 12, &u.p->ipi_addr, sizeof( struct in_addr ) ); + u.c = CMSG_DATA(cmsg); + (*packet)->ifindex = u.p->ipi_ifindex; + if ((*packet)->dst.addr.sa.sa_family == AF_INET6) { + memset((*packet)->dst.addr.sin6.sin6_addr.s6_addr, 0, 10); + (*packet)->dst.addr.sin6.sin6_addr.s6_addr[10] = 0xff; + (*packet)->dst.addr.sin6.sin6_addr.s6_addr[11] = 0xff; + memcpy((*packet)->dst.addr.sin6.sin6_addr.s6_addr + 12, &u.p->ipi_addr, sizeof(struct in_addr)); } else { - memcpy( &( *packet )->dst.addr.sin.sin_addr, &u.p->ipi_addr, sizeof( struct in_addr ) ); + memcpy(&(*packet)->dst.addr.sin.sin_addr, &u.p->ipi_addr, sizeof(struct in_addr)); } break; } #elif defined(IP_RECVDSTADDR) - if ( cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR ) { - ( *packet )->ifindex = 0; - memcpy( &( *packet )->dst.addr.sin.sin_addr, CMSG_DATA( cmsg ), sizeof( struct in_addr ) ); + if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) { + (*packet)->ifindex = 0; + memcpy(&(*packet)->dst.addr.sin.sin_addr, CMSG_DATA(cmsg), sizeof(struct in_addr)); break; } #endif /* IP_PKTINFO */ @@ -621,41 +618,41 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { } #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */ #ifdef WITH_CONTIKI - /* FIXME: untested, make this work */ + /* 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)->src.addr, &UIP_IP_BUF->srcipaddr); - (*packet)->src.port = UIP_UDP_BUF->srcport; - uip_ipaddr_copy(&(*packet)->dst.addr, &UIP_IP_BUF->destipaddr); - (*packet)->dst.port = UIP_UDP_BUF->destport; + if (uip_newdata()) { + uip_ipaddr_copy(&(*packet)->src.addr, &UIP_IP_BUF->srcipaddr); + (*packet)->src.port = UIP_UDP_BUF->srcport; + uip_ipaddr_copy(&(*packet)->dst.addr, &UIP_IP_BUF->destipaddr); + (*packet)->dst.port = UIP_UDP_BUF->destport; - len = uip_datalen(); - - if (len > coap_get_max_packetlength(*packet)) { - /* FIXME: we might want to send back a response */ - warn("discarded oversized packet\n"); - return -1; - } + len = uip_datalen(); - ((char *)uip_appdata)[len] = 0; + if (len > coap_get_max_packetlength(*packet)) { + /* FIXME: we might want to send back a response */ + warn("discarded oversized packet\n"); + return -1; + } + + ((char *)uip_appdata)[len] = 0; #ifndef NDEBUG - if (LOG_DEBUG <= coap_get_log_level()) { + 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)->src, addr_str, INET6_ADDRSTRLEN+8)) { - debug("received %zd bytes from %s\n", len, addr_str); + unsigned char addr_str[INET6_ADDRSTRLEN + 8]; + + if (coap_print_addr(&(*packet)->src, addr_str, INET6_ADDRSTRLEN + 8)) { + debug("received %zd bytes from %s\n", len, addr_str); + } } - } #endif /* NDEBUG */ - (*packet)->length = len; - memcpy(&(*packet)->payload, uip_appdata, len); - } + (*packet)->length = len; + memcpy(&(*packet)->payload, uip_appdata, len); + } #undef UIP_IP_BUF #undef UIP_UDP_BUF @@ -665,208 +662,223 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { #endif } - if ( len >= 0 ) + if (len >= 0) return len; #if !defined(WITH_LWIP) - error: - coap_free_packet(*packet); - *packet = NULL; - return -1; + error: + coap_free_packet(*packet); + *packet = NULL; + return -1; #endif } #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) int -coap_run_once( coap_context_t *ctx, unsigned timeout_ms ) { +coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { fd_set readfds; struct timeval tv; coap_queue_t *nextpdu; - coap_tick_t now; + coap_tick_t before, now; coap_tick_t max_wait; - coap_tick_t dtls_timeout; - coap_session_t *dtls_timeout_session; int result; coap_endpoint_t *ep; coap_session_t *s; coap_fd_t nfds = 0; + coap_tick_t dtls_timeout = 0; + coap_session_t *dtls_timeout_session = NULL; + coap_tick_t expiration_timeout = 0; + coap_session_t *expiration_session = NULL; + coap_tick_t session_timeout; - coap_ticks( &now ); + coap_ticks(&now); - FD_ZERO( &readfds ); - LL_FOREACH( ctx->endpoint, ep ) { - if ( ep->sock.flags & COAP_SOCKET_WANT_DATA ) { - if ( ep->sock.fd + 1 > nfds ) + 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; + + FD_ZERO(&readfds); + LL_FOREACH(ctx->endpoint, ep) { + coap_session_t *tmp; + if (ep->sock.flags & COAP_SOCKET_WANT_DATA) { + if (ep->sock.fd + 1 > nfds) nfds = ep->sock.fd + 1; - FD_SET( ep->sock.fd, &readfds ); + FD_SET(ep->sock.fd, &readfds); } - LL_FOREACH( ctx->endpoint->sessions, s ) { - if ( s->sock.flags & COAP_SOCKET_WANT_DATA ) { - if ( s->sock.fd + 1 > nfds ) - nfds = s->sock.fd + 1; - FD_SET( s->sock.fd, &readfds ); + LL_FOREACH_SAFE(ctx->endpoint->sessions, s, tmp) { + if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 && s->sendqueue == 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->sendqueue == NULL) { + coap_tick_t s_timeout = now - (s->last_rx_tx + session_timeout); + if (expiration_timeout == 0 || s_timeout < expiration_timeout) { + expiration_timeout = s_timeout; + expiration_session = s; + } + } + if (s->sock.flags & COAP_SOCKET_WANT_DATA) { + if (s->sock.fd + 1 > nfds) + nfds = s->sock.fd + 1; + FD_SET(s->sock.fd, &readfds); + } } } } - LL_FOREACH( ctx->sessions, s ) { - if ( s->sock.flags & COAP_SOCKET_WANT_DATA ) { - if ( s->sock.fd + 1 > nfds ) + LL_FOREACH(ctx->sessions, s) { + if (s->sock.flags & COAP_SOCKET_WANT_DATA) { + if (s->sock.fd + 1 > nfds) nfds = s->sock.fd + 1; - FD_SET( s->sock.fd, &readfds ); + FD_SET(s->sock.fd, &readfds); } } max_wait = timeout_ms * COAP_TICKS_PER_SECOND / 1000; - while ( 1 ) { - dtls_timeout = 0; - dtls_timeout_session = NULL; + nextpdu = coap_peek_next(ctx); - 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); + } - 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 ( ctx->dtls_context ) { - int ctx_timeout = coap_dtls_get_context_timeout( ctx->dtls_context ); - if ( ctx_timeout < 0 ) { - ctx_timeout = 0; - LL_FOREACH( ctx->endpoint, ep ) { - if ( ep->proto == COAP_PROTO_DTLS ) { - LL_FOREACH( ep->sessions, s ) { - if ( s->proto == COAP_PROTO_DTLS && s->tls ) { - int s_timeout = coap_dtls_get_timeout( s ); - if ( s_timeout == 0 ) - s_timeout = 1; - if ( s_timeout > 0 ) { - if ( ctx_timeout == 0 || s_timeout < ctx_timeout ) { - ctx_timeout = s_timeout; - dtls_timeout_session = s; - } + if (ctx->dtls_context) { + int ctx_timeout = coap_dtls_get_context_timeout(ctx->dtls_context); + if (ctx_timeout < 0) { + ctx_timeout = 0; + LL_FOREACH(ctx->endpoint, ep) { + if (ep->proto == COAP_PROTO_DTLS) { + LL_FOREACH(ep->sessions, s) { + if (s->proto == COAP_PROTO_DTLS && s->tls) { + int s_timeout = coap_dtls_get_timeout(s); + if (s_timeout == 0) + s_timeout = 1; + if (s_timeout > 0) { + if (ctx_timeout == 0 || s_timeout < ctx_timeout) { + ctx_timeout = s_timeout; + dtls_timeout_session = s; } } } } } - LL_FOREACH( ctx->sessions, s ) { - if ( s->proto == COAP_PROTO_DTLS && s->tls ) { - int s_timeout = coap_dtls_get_timeout( s ); - if ( s_timeout == 0 ) - s_timeout = 1; - if ( s_timeout > 0 ) { - if ( ctx_timeout == 0 || s_timeout < ctx_timeout ) { - ctx_timeout = s_timeout; - dtls_timeout_session = s; - } + } + LL_FOREACH(ctx->sessions, s) { + if (s->proto == COAP_PROTO_DTLS && s->tls) { + int s_timeout = coap_dtls_get_timeout(s); + if (s_timeout == 0) + s_timeout = 1; + if (s_timeout > 0) { + if (ctx_timeout == 0 || s_timeout < ctx_timeout) { + ctx_timeout = s_timeout; + dtls_timeout_session = s; } } } - } else if ( ctx_timeout > 0 ) { - debug( "** DTLS global timeout set to %dms\n", ctx_timeout ); - } - if ( ctx_timeout > 0 ) - dtls_timeout = (coap_tick_t)ctx_timeout * COAP_TICKS_PER_SECOND / 1000; - } - - if ( nextpdu && ( max_wait == 0 || nextpdu->t < max_wait ) && ( dtls_timeout == 0 || nextpdu->t < dtls_timeout ) ) { - /* set timeout if there is a pdu to send */ - tv.tv_usec = ( ( nextpdu->t ) % COAP_TICKS_PER_SECOND ) * 1000000 / COAP_TICKS_PER_SECOND; - tv.tv_sec = (long)( nextpdu->t ) / COAP_TICKS_PER_SECOND; - } else if ( dtls_timeout > 0 && ( max_wait == 0 || dtls_timeout < max_wait ) ) { - tv.tv_usec = ( dtls_timeout % COAP_TICKS_PER_SECOND ) * 1000000 / COAP_TICKS_PER_SECOND; - tv.tv_sec = (long)dtls_timeout / COAP_TICKS_PER_SECOND; - } else if ( max_wait > 0 ) { - tv.tv_usec = ( max_wait % COAP_TICKS_PER_SECOND ) * 1000000 / COAP_TICKS_PER_SECOND; - tv.tv_sec = (long)max_wait / COAP_TICKS_PER_SECOND; - } - - result = select( nfds, &readfds, 0, 0, ( nextpdu || max_wait > 0 || dtls_timeout > 0 ) ? &tv : NULL ); - - if ( result < 0 ) { /* error */ -#ifdef _WIN32 - if ( WSAGetLastError() == WSAEINVAL ) { /* May happen because of ICMP */ - coap_tick_t past = now; - coap_ticks( &now ); - return (int)( ( ( now - past ) * 1000 ) / COAP_TICKS_PER_SECOND ); - } else { -#else - if ( errno != EINTR ) { -#endif - coap_log( LOG_DEBUG, coap_socket_strerror() ); - } - return -1; - } else if ( result > 0 ) { /* read from socket */ - coap_tick_t past = now; - LL_FOREACH( ctx->endpoint, ep ) { - if ( FD_ISSET( ep->sock.fd, &readfds ) ) - ep->sock.flags |= COAP_SOCKET_HAS_DATA; - LL_FOREACH( ctx->endpoint->sessions, s ) { - if ( FD_ISSET( s->sock.fd, &readfds ) ) - s->sock.flags |= COAP_SOCKET_HAS_DATA; - } - } - LL_FOREACH( ctx->sessions, s ) { - if ( FD_ISSET( s->sock.fd, &readfds ) ) - s->sock.flags |= COAP_SOCKET_HAS_DATA; - } - coap_read( ctx ); /* read received data */ - coap_ticks( &now ); - return (int)( ( ( now - past ) * 1000 ) / COAP_TICKS_PER_SECOND ); - } else { /* timeout */ - coap_tick_t past = now; - coap_ticks( &now ); - if ( dtls_timeout > 0 && dtls_timeout_session && past + dtls_timeout <= now ) { - debug( "** %s: DTLS retransmit timeout after %ums\n", coap_session_str( dtls_timeout_session ), (unsigned)( dtls_timeout * 1000 / COAP_TICKS_PER_SECOND ) ); - coap_dtls_handle_timeout( dtls_timeout_session ); - } - if ( max_wait > 0 && past + max_wait <= now ) { - return (int)( now - past ); - } else { - max_wait -= (unsigned)( now - past ); } + } else if (ctx_timeout > 0) { + debug("** DTLS global timeout set to %dms\n", ctx_timeout); } + if (ctx_timeout > 0) + dtls_timeout = (coap_tick_t)ctx_timeout * COAP_TICKS_PER_SECOND / 1000; } - /* never reached */ + if (nextpdu && (dtls_timeout == 0 || nextpdu->t < dtls_timeout) && (expiration_timeout == 0 || nextpdu->t < expiration_timeout) && (max_wait == 0 || nextpdu->t < max_wait)) { + /* set timeout if there is a pdu to send */ + tv.tv_usec = ((nextpdu->t) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = (long)(nextpdu->t) / COAP_TICKS_PER_SECOND; + } else if (dtls_timeout > 0 && (expiration_timeout == 0 || dtls_timeout < expiration_timeout) && (max_wait == 0 || dtls_timeout < max_wait)) { + tv.tv_usec = (dtls_timeout % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = (long)dtls_timeout / COAP_TICKS_PER_SECOND; + } else if (expiration_timeout > 0 && (max_wait == 0 || expiration_timeout < max_wait)) { + tv.tv_usec = (expiration_timeout % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = (long)expiration_timeout / COAP_TICKS_PER_SECOND; + } else if (max_wait > 0) { + tv.tv_usec = (max_wait % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = (long)max_wait / COAP_TICKS_PER_SECOND; + } + + result = select(nfds, &readfds, 0, 0, (nextpdu || max_wait > 0 || dtls_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, coap_socket_strerror()); + return -1; + } + } + if (result > 0) { /* read from socket */ + LL_FOREACH(ctx->endpoint, ep) { + if (FD_ISSET(ep->sock.fd, &readfds)) + ep->sock.flags |= COAP_SOCKET_HAS_DATA; + LL_FOREACH(ctx->endpoint->sessions, s) { + if (FD_ISSET(s->sock.fd, &readfds)) + s->sock.flags |= COAP_SOCKET_HAS_DATA; + } + } + LL_FOREACH(ctx->sessions, s) { + if (FD_ISSET(s->sock.fd, &readfds)) + s->sock.flags |= COAP_SOCKET_HAS_DATA; + } + coap_ticks(&now); + coap_read(ctx, now); /* read received data */ + } + + before = now; + coap_ticks(&now); + + if (expiration_timeout > 0 && expiration_session && expiration_session->ref == 0 && expiration_session->sendqueue == NULL && expiration_session->last_rx_tx + session_timeout <= now) { + if (dtls_timeout_session == expiration_session) + dtls_timeout_session = NULL; + coap_session_free(expiration_session); + } + + if (dtls_timeout > 0 && dtls_timeout_session && before + dtls_timeout <= now) { + debug("** %s: DTLS retransmit timeout after %ums\n", coap_session_str(dtls_timeout_session), (unsigned)(dtls_timeout * 1000 / COAP_TICKS_PER_SECOND)); + coap_dtls_handle_timeout(dtls_timeout_session); + } + + return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); } #else -int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms ) { +int coap_run_once(coap_context_t *ctx, unsigned int timeout_ms) { return -1; } #endif #ifdef _WIN32 -const char *coap_socket_strerror( void ) { +const char *coap_socket_strerror(void) { static char szError[256]; - if ( FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)WSAGetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)szError, ( DWORD )sizeof( szError ), NULL ) == 0 ) - strcpy( szError, "Unknown error" ); + if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szError, (DWORD)sizeof(szError), NULL) == 0) + strcpy(szError, "Unknown error"); return szError; } #else -const char *coap_socket_strerror( void ) { - return strerror( errno ); +const char *coap_socket_strerror(void) { + return strerror(errno); } #endif #if defined(WITH_LWIP) ssize_t -coap_socket_send_pdu( coap_socket_t *sock, coap_session_t *session, - coap_pdu_t *pdu ) { +coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session, + coap_pdu_t *pdu) { /* FIXME: we can't check this here with the existing infrastructure, but we * should actually check that the pdu is not held by anyone but us. the * respective pbuf is already exclusively owned by the pdu. */ - pbuf_realloc( pdu->pbuf, pdu->length ); - udp_sendto( sock->pcb, pdu->pbuf, &session->remote_addr.addr, - session->remote_addr.port ); + pbuf_realloc(pdu->pbuf, pdu->length); + udp_sendto(sock->pcb, pdu->pbuf, &session->remote_addr.addr, + session->remote_addr.port); return pdu->length; } ssize_t -coap_socket_send( coap_socket_t *sock, coap_session_t *session, +coap_socket_send(coap_socket_t *sock, coap_session_t *session, /* Not implemented, use coap_socket_send_pdu instead */ return -1; } @@ -874,10 +886,9 @@ coap_socket_send( coap_socket_t *sock, coap_session_t *session, #else /* defined(WITH_LWIP) */ 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 ); +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); } #endif /* defined(WITH_LWIP) */ diff --git a/src/coap_session.c b/src/coap_session.c index bbb3921b..734ad693 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -20,8 +20,6 @@ #include "utlist.h" #include -static void coap_free_session(coap_session_t *session); - coap_session_t * coap_session_reference(coap_session_t *session) { ++session->ref; @@ -35,7 +33,7 @@ coap_session_release(coap_session_t *session) { if (session->ref > 0) --session->ref; if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) - coap_free_session(session); + coap_session_free(session); } } @@ -61,8 +59,7 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addre return session; } -static void -coap_free_session(coap_session_t *session) { +void coap_session_free(coap_session_t *session) { coap_pdu_queue_t *q, *tmp; if (!session) @@ -107,10 +104,12 @@ ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t d } bytes_written = coap_socket_send(sock, session, data, datalen); - if (bytes_written == (ssize_t)datalen) + if (bytes_written == (ssize_t)datalen) { + coap_ticks(&session->last_rx_tx); debug("* %s: sent %zd bytes\n", coap_session_str(session), datalen); - else + } else { debug("* %s: failed to send %zd bytes\n", coap_session_str(session), datalen); + } return bytes_written; } @@ -173,24 +172,43 @@ void coap_session_disconnected(coap_session_t *session) { } coap_session_t * -coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet) { +coap_endpoint_get_session(coap_endpoint_t *endpoint, + const coap_packet_t *packet, coap_tick_t now) { coap_session_t *session = NULL; + unsigned int num_idle = 0; + coap_session_t *oldest = NULL; endpoint->hello.ifindex = -1; LL_FOREACH(endpoint->sessions, session) { - if (session->ifindex == packet->ifindex && coap_address_equals(&session->local_addr, &packet->dst) && coap_address_equals(&session->remote_addr, &packet->src)) + if (session->ifindex == packet->ifindex && + coap_address_equals(&session->local_addr, &packet->dst) && + coap_address_equals(&session->remote_addr, &packet->src)) + { + session->last_rx_tx = now; return session; + } + if (session->ref == 0 && session->sendqueue == NULL && session->type == COAP_SESSION_TYPE_SERVER) { + ++num_idle; + if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx) + oldest = session; + } } + if (endpoint->context->max_idle_sessions > 0 && num_idle >= endpoint->context->max_idle_sessions) + coap_session_free(oldest); + if (endpoint->proto == COAP_PROTO_DTLS) { session = &endpoint->hello; coap_address_copy(&session->local_addr, &packet->dst); coap_address_copy(&session->remote_addr, &packet->src); session->ifindex = packet->ifindex; } else { - session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER, &packet->dst, &packet->src, packet->ifindex, endpoint->context, endpoint); + session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER, + &packet->dst, &packet->src, packet->ifindex, endpoint->context, + endpoint); if (session) { + session->last_rx_tx = now; if (endpoint->proto == COAP_PROTO_UDP) session->state = COAP_SESSION_STATE_ESTABLISHED; LL_PREPEND(endpoint->sessions, session); @@ -203,9 +221,10 @@ coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet coap_session_t * coap_endpoint_new_dtls_session(coap_endpoint_t *endpoint, - const coap_packet_t *packet) { + const coap_packet_t *packet, coap_tick_t now) { coap_session_t *session = coap_make_session(COAP_PROTO_DTLS, COAP_SESSION_TYPE_SERVER, &packet->dst, &packet->src, packet->ifindex, endpoint->context, endpoint); if (session) { + session->last_rx_tx = now; session->state = COAP_SESSION_STATE_HANDSHAKE; session->tls = coap_dtls_new_server_session(session); if (session->tls) { @@ -213,7 +232,8 @@ coap_endpoint_new_dtls_session(coap_endpoint_t *endpoint, LL_PREPEND(endpoint->sessions, session); debug("*** %s: new incoming session\n", coap_session_str(session)); } else { - coap_free_session(session); + coap_session_free(session); + session = NULL; } } return session; @@ -252,7 +272,7 @@ coap_session_create_client( error: if (session) - coap_free_session(session); + coap_session_free(session); return NULL; } @@ -265,10 +285,11 @@ coap_session_connect(coap_session_t *session) { if (session->tls) { session->state = COAP_SESSION_STATE_HANDSHAKE; } else { - coap_free_session(session); + coap_session_free(session); return NULL; } } + coap_ticks(&session->last_rx_tx); return session; } diff --git a/src/net.c b/src/net.c index 665ef5b0..e4f3d7d0 100644 --- a/src/net.c +++ b/src/net.c @@ -650,6 +650,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt) { sock = &session->endpoint->sock; } bytes_written = coap_socket_send_pdu(sock, session, pdu); + coap_ticks(&session->last_rx_tx); #else @@ -939,7 +940,7 @@ coap_handle_message_for_proto(coap_context_t *ctx, coap_session_t *session, coap } static int -coap_read_session(coap_context_t *ctx, coap_session_t *session) { +coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) { ssize_t bytes_read = -1; coap_packet_t *packet; int result = -1; /* the value to be returned */ @@ -952,6 +953,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session) { warn("* %s: read error\n", coap_session_str(session)); } else if (bytes_read > 0) { debug("* %s: received %zd bytes\n", coap_session_str(session), bytes_read); + session->last_rx_tx = now; coap_packet_set_addr(packet, &session->remote_addr, &session->local_addr); result = coap_handle_message_for_proto(ctx, session, packet); } @@ -962,7 +964,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session) { } static int -coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint) { +coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) { ssize_t bytes_read = -1; coap_packet_t *packet; int result = -1; /* the value to be returned */ @@ -972,12 +974,12 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint) { if (bytes_read < 0) { warn("* %s: read failed\n", coap_endpoint_str(endpoint)); } else if (bytes_read > 0) { - coap_session_t *session = coap_endpoint_get_session(endpoint, packet); + coap_session_t *session = coap_endpoint_get_session(endpoint, packet, now); if (session) { debug("* %s: received %zd bytes\n", coap_session_str(session), bytes_read); result = coap_handle_message_for_proto(ctx, session, packet); if (endpoint->proto == COAP_PROTO_DTLS && session->type == COAP_SESSION_TYPE_HELLO && result == 1) - coap_endpoint_new_dtls_session(endpoint, packet); + coap_endpoint_new_dtls_session(endpoint, packet, now); } } @@ -987,18 +989,18 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint) { } void -coap_read(coap_context_t *ctx) { +coap_read(coap_context_t *ctx, coap_tick_t now) { coap_endpoint_t *ep, *tmp; coap_session_t *s, *tmp_s; LL_FOREACH_SAFE(ctx->endpoint, ep, tmp) { if ((ep->sock.flags & COAP_SOCKET_HAS_DATA) != 0) - coap_read_endpoint(ctx, ep); + coap_read_endpoint(ctx, ep, now); } LL_FOREACH_SAFE(ctx->sessions, s, tmp_s) { if ((s->sock.flags & COAP_SOCKET_HAS_DATA) != 0) - coap_read_session(ctx, s); + coap_read_session(ctx, s, now); } }