diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 92c0e903..8f37f0f8 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -704,6 +704,7 @@ main(int argc, char **argv) { } coap_startup(); + coap_dtls_set_log_level(log_level); coap_set_log_level(log_level); ctx = get_context(addr_str, port_str); diff --git a/examples/coap-server.c b/examples/coap-server.c index cd5d2342..172de5f5 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -519,6 +519,7 @@ main(int argc, char **argv) { } coap_startup(); + coap_dtls_set_log_level(log_level); coap_set_log_level(log_level); ctx = get_context(addr_str, port_str); diff --git a/include/coap/address.h b/include/coap/address.h index 5a32694a..fb6188da 100644 --- a/include/coap/address.h +++ b/include/coap/address.h @@ -61,7 +61,6 @@ typedef struct coap_address_t { socklen_t size; /**< size of addr */ union { struct sockaddr sa; - struct sockaddr_storage st; struct sockaddr_in sin; struct sockaddr_in6 sin6; } addr; @@ -124,7 +123,7 @@ coap_address_copy( coap_address_t *dst, const coap_address_t *src ) { dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port; dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id; } else if ( src->addr.sa.sa_family == AF_INET ) { - dst->addr.st = src->addr.st; + dst->addr.sin = src->addr.sin; } else { memcpy( &dst->addr, &src->addr, src->size ); } diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 65858e86..13d54fca 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -67,6 +67,13 @@ void *coap_dtls_new_server_session(coap_session_t *session); */ void coap_dtls_free_session(coap_session_t *session); +/** + * Notify of a change in the session's MTU, e.g. after a PMTU update. + * + * @param session The CoAP session + */ +void coap_dtls_session_update_mtu(coap_session_t *session); + /** * Send data to a DTLS peer. * @@ -78,21 +85,29 @@ int coap_dtls_send(coap_session_t *session, const uint8_t *data, size_t data_len); +/** +* Check if timeout is handled per session or per context. +* +* @param dtls_context The DTLS context +* @return 1 of timeout and retransmit is per context, 0 if it is per session. +*/ +int coap_dtls_is_context_timeout(void); + /** * Do all pending retransmits and get next timeout * * @param dtls_context The DTLS context - * @return <0 If not implemented, i.e. each session has its own timeout, 0 if no timeout, >0 Number of milliseconds until the next timeout. + * @return 0 If no event is pending or date of the next retransmit. */ -int coap_dtls_get_context_timeout(void *dtls_context); +coap_tick_t coap_dtls_get_context_timeout(void *dtls_context); /** * Get next timeout for this session. * * @param session The CoAP session - * @return <0 If no event is pending, >=0 Number of milliseconds until the next timeout. + * @return 0 If no event is pending or date of the next retransmit. */ -int coap_dtls_get_timeout(coap_session_t *session); +coap_tick_t coap_dtls_get_timeout(coap_session_t *session); /** * Handle a DTLS timeout expiration. diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 83df0f2f..405b6aee 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -16,6 +16,7 @@ struct coap_endpoint_t; struct coap_contex_t; +struct coap_queue_t; #define COAP_DEFAULT_SESSION_TIMEOUT 300 @@ -44,12 +45,6 @@ typedef uint8_t coap_session_state_t; #define COAP_SESSION_STATE_HANDSHAKE 2 #define COAP_SESSION_STATE_ESTABLISHED 3 -typedef struct coap_pdu_queue_t { - struct coap_pdu_queue_t *next; - coap_pdu_t *pdu; - int retransmit_cnt; -} coap_pdu_queue_t; - typedef struct coap_session_t { struct coap_session_t *next; coap_proto_t proto; /**< protocol used */ @@ -65,7 +60,7 @@ typedef struct coap_session_t { struct coap_endpoint_t *endpoint; /**< session's endpoint */ struct coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ - coap_pdu_queue_t *sendqueue; /**< list of messages waiting to be sent */ + struct coap_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; @@ -190,8 +185,8 @@ ssize_t coap_session_send(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, + struct coap_queue_t *node); /** * Abstraction of virtual endpoint that can be attached to coap_context_t. The * tuple (handle, addr) must uniquely identify this endpoint. diff --git a/include/coap/coap_time.h b/include/coap/coap_time.h index 52cc098a..9585c1f8 100644 --- a/include/coap/coap_time.h +++ b/include/coap/coap_time.h @@ -120,6 +120,25 @@ void coap_ticks(coap_tick_t *t); * point (seconds since epoch on POSIX). */ coap_time_t coap_ticks_to_rt(coap_tick_t t); + +/** +* Helper function that converts coap ticks to POSIX wallclock time in us. +* +* @param t Internal system ticks. +* +* @return The number of seconds that has passed since a specific reference +* point (seconds since epoch on POSIX). +*/ +uint64_t coap_ticks_to_rt_us(coap_tick_t t); + +/** +* Helper function that converts POSIX wallclock time in us to coap ticks. +* +* @param t POSIX time is us +* +* @return coap ticks +*/ +coap_tick_t coap_ticks_from_rt_us(uint64_t t); #endif /** diff --git a/include/coap/debug.h b/include/coap/debug.h index e7a6822c..64e965be 100644 --- a/include/coap/debug.h +++ b/include/coap/debug.h @@ -55,7 +55,7 @@ const char *coap_package_version(void); void coap_log_impl(coap_log_t level, const char *format, ...); #ifndef coap_log -#define coap_log(...) coap_log_impl(__VA_ARGS__) +#define coap_log(level, ...) do { if ((level)<=coap_get_log_level()) coap_log_impl((level), __VA_ARGS__); } while(0) #endif /* A set of convenience macros for common log levels. */ diff --git a/include/coap/net.h b/include/coap/net.h index c16ccc0a..a62583a9 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -350,12 +350,41 @@ coap_tid_t coap_send( coap_session_t *session, coap_pdu_t *pdu ); coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node); /** - * Reads all data from the network. +* For applications with their own message loop, send all pending retransmits and +* return the list of sockets with events to wait for and the next timeout +* The application should call coap_read, then coap_write again when any condition below is true: +* - data is available on any of the sockets with the COAP_SOCKET_WANT_DATA flag set +* - at least some data can be written without blocking on any of the sockets with the COAP_SOCKET_WANT_WRITE flag set +* - the timeout has expired +* Before calling coap_read or coap_write again, the application should position COAP_SOCKET_HAS_DATA and COAP_SOCKET_CAN_WRITE flags as applicable. +* +* @param ctx The CoAP context +* @param sockets array of socket descriptors, filled on output +* @param max_sockets size of socket array. +* @param num_sockets pointer to the number of valid entries in the socket arrays on output +* @param now Current time. +* @return timeout as maxmimum number of milliseconds that the application should wait for network events or 0 if the application should wait forever. +*/ + +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, reads all data from the network. + * + * @param ctx The CoAP context + * @param now Current time */ -void coap_read(coap_context_t *context, coap_tick_t now); +void coap_read(coap_context_t *ctx, coap_tick_t now); /** * The main message processing loop. + * * @param ctx The CoAP context * @param timeout_ms Minimum number of milliseconds to wait for new messages before returning. If zero the call will block until at least one packet is sent or received. * @return number of milliseconds spent or -1 if there was an error @@ -433,7 +462,7 @@ coap_remove_transaction(coap_queue_t **queue, coap_session_t *session, coap_tid_ coap_tid_t coap_wait_ack( coap_context_t *context, coap_session_t *session, - coap_pdu_t *pdu, int retransmit_cnt ); + coap_queue_t *node); /** * Retrieves transaction from the queue. diff --git a/src/coap_io.c b/src/coap_io.c index 3bc6f93c..c9fa340a 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -533,8 +533,8 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) { 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; + mhdr.msg_namelen = sizeof(packet->src.addr); mhdr.msg_iov = iov; mhdr.msg_iovlen = 1; @@ -674,67 +674,56 @@ error: } #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) -int -coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { - fd_set readfds; - struct timeval tv; + +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_tick_t before, now; - coap_tick_t max_wait; - 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_tick_t timeout = 0; - coap_ticks(&now); + *num_sockets = 0; 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); + if (ep->sock.flags & (COAP_SOCKET_WANT_DATA|COAP_SOCKET_WANT_WRITE)) { + if (*num_sockets < max_sockets) + sockets[(*num_sockets)++] = &ep->sock; } 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); + 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); - } + 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 (timeout == 0 || s_timeout < timeout) + timeout = s_timeout; + } + if (s->sock.flags & (COAP_SOCKET_WANT_DATA | COAP_SOCKET_WANT_WRITE)) { + if (*num_sockets < max_sockets) + sockets[(*num_sockets)++] = &s->sock; + } } } } 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); + if (s->sock.flags & (COAP_SOCKET_WANT_DATA | COAP_SOCKET_WANT_WRITE)) { + if (*num_sockets < max_sockets) + sockets[(*num_sockets)++] = &s->sock; } } - max_wait = timeout_ms * COAP_TICKS_PER_SECOND / 1000; - nextpdu = coap_peek_next(ctx); while (nextpdu && now >= ctx->sendqueue_basetime && nextpdu->t <= now - ctx->sendqueue_basetime) { @@ -742,63 +731,87 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { 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) { - int ctx_timeout = coap_dtls_get_context_timeout(ctx->dtls_context); - if (ctx_timeout < 0) { - ctx_timeout = 0; + 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) + tls_timeout = now + 1; + 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) { - 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 (ep->proto == COAP_PROTO_DTLS) { + LL_FOREACH(ep->sessions, s) { + if (s->proto == COAP_PROTO_DTLS && s->tls) { + coap_tick_t tls_timeout = coap_dtls_get_timeout(s); + while (tls_timeout > 0 && tls_timeout <= now) { + debug("** %s: DTLS retransmit timeout\n", coap_session_str(s)); + coap_dtls_handle_timeout(s); + tls_timeout = coap_dtls_get_timeout(s); + } + if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) + timeout = tls_timeout - now; + } + } + } } 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; - } - } - } + if (s->proto == COAP_PROTO_DTLS && s->tls) { + coap_tick_t tls_timeout = coap_dtls_get_timeout(s); + while (tls_timeout > 0 && tls_timeout <= now) { + debug("** %s: DTLS retransmit timeout\n", coap_session_str(s)); + coap_dtls_handle_timeout(s); + tls_timeout = coap_dtls_get_timeout(s); + } + if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) + timeout = tls_timeout - now; + } } - } 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 && (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; + 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) { + fd_set readfds, writefds; + coap_fd_t nfds = 0; + struct timeval tv; + coap_tick_t before, now; + int result; + coap_socket_t *sockets[64]; + unsigned int num_sockets = 0, i, timeout; + + coap_ticks(&before); + + 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); + 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_DATA) + FD_SET(sockets[i]->fd, &readfds); + if (sockets[i]->flags & COAP_SOCKET_WANT_WRITE) + FD_SET(sockets[i]->fd, &writefds); } - result = select(nfds, &readfds, 0, 0, (nextpdu || max_wait > 0 || dtls_timeout > 0) ? &tv : NULL); + if ( timeout > 0 ) { + tv.tv_usec = (timeout % 1000) * 1000; + tv.tv_sec = (long)(timeout / 1000); + } + + result = select(nfds, &readfds, &writefds, 0, timeout > 0 ? &tv : NULL); if (result < 0) { /* error */ #ifdef _WIN32 @@ -810,36 +823,18 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { 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; - } + + if (result > 0) { + for (i = 0; i < num_sockets; i++) { + if ((sockets[i]->flags & COAP_SOCKET_WANT_DATA) && FD_ISSET(sockets[i]->fd, &readfds)) + sockets[i]->flags |= COAP_SOCKET_HAS_DATA; + if ((sockets[i]->flags & COAP_SOCKET_WANT_WRITE) && FD_ISSET(sockets[i]->fd, &writefds)) + sockets[i]->flags |= COAP_SOCKET_CAN_WRITE; } - 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 && dtls_timeout_session->tls && 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); - } + coap_read(ctx, now); return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); } @@ -848,6 +843,17 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { int coap_run_once(coap_context_t *ctx, unsigned int timeout_ms) { 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 #ifdef _WIN32 diff --git a/src/coap_notls.c b/src/coap_notls.c index a85c4489..07734542 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -20,68 +20,74 @@ #endif /* __GNUC__ */ int -coap_dtls_is_supported( void ) { +coap_dtls_is_supported(void) { return 0; } static int dtls_log_level = 0; -void coap_dtls_startup( void ) { +void coap_dtls_startup(void) { } void -coap_dtls_set_log_level( int level ) { +coap_dtls_set_log_level(int level) { dtls_log_level = level; } int -coap_dtls_get_log_level( void ) { +coap_dtls_get_log_level(void) { return dtls_log_level; } void * -coap_dtls_new_context( struct coap_context_t *coap_context UNUSED ) { +coap_dtls_new_context(struct coap_context_t *coap_context UNUSED) { return NULL; } void -coap_dtls_free_context( void *handle UNUSED ) { +coap_dtls_free_context(void *handle UNUSED) { } -void *coap_dtls_new_server_session( coap_session_t *session UNUSED ) { +void *coap_dtls_new_server_session(coap_session_t *session UNUSED) { return NULL; } -void *coap_dtls_new_client_session( coap_session_t *session UNUSED ) { +void *coap_dtls_new_client_session(coap_session_t *session UNUSED) { return NULL; } -void -coap_dtls_free_session( coap_session_t *coap_session UNUSED ) { +void coap_dtls_free_session(coap_session_t *coap_session UNUSED) { +} + +void coap_dtls_session_update_mtu(coap_session_t *session UNUSED) { } int -coap_dtls_send( coap_session_t *session UNUSED, +coap_dtls_send(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED ) { return -1; } -int coap_dtls_get_context_timeout( void *dtls_context UNUSED ) { - return -1; +int coap_dtls_is_context_timeout(void) { + return 1; } -int coap_dtls_get_timeout( coap_session_t *session UNUSED ) { - return -1; +coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED) { + return 0; } -void coap_dtls_handle_timeout( coap_session_t *session UNUSED ) { +coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED) { + return 0; +} + +void coap_dtls_handle_timeout(coap_session_t *session UNUSED) { return 0; } int -coap_dtls_receive( coap_session_t *session UNUSED, +coap_dtls_receive(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED ) { @@ -89,7 +95,7 @@ coap_dtls_receive( coap_session_t *session UNUSED, } int -coap_dtls_hello( coap_session_t *session UNUSED, +coap_dtls_hello(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED ) { diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 34052a59..9fd22ff7 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -52,8 +52,8 @@ typedef struct coap_ssl_st { coap_session_t *session; const void *pdu; unsigned pdu_len; - unsigned mtu; unsigned peekmode; + coap_tick_t timeout; } coap_ssl_data; static int coap_dgram_create(BIO *a) { @@ -64,7 +64,6 @@ static int coap_dgram_create(BIO *a) { BIO_set_init(a, 1); BIO_set_data(a, data); memset(data, 0x00, sizeof(coap_ssl_data)); - data->mtu = 1280 - 40 - 8; return 1; } @@ -125,14 +124,6 @@ static int coap_dgram_puts(BIO *a, const char *pstr) { return coap_dgram_write(a, pstr, (int)strlen(pstr)); } -static const uint8_t in6_v4mapped[12] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }; - -static long dgram_get_mtu_overhead(const coap_ssl_data *data) { - if (data->session && data->session->remote_addr.addr.sa.sa_family == AF_INET6 && memcmp(&data->session->remote_addr.addr.sin6.sin6_addr, in6_v4mapped, sizeof(in6_v4mapped)) != 0) - return 48; - return 28; -} - static long coap_dgram_ctrl(BIO *a, int cmd, long num, void *ptr) { long ret = 1; coap_ssl_data *data = BIO_get_data(a); @@ -148,23 +139,6 @@ static long coap_dgram_ctrl(BIO *a, int cmd, long num, void *ptr) { break; ret = 0; break; - case BIO_CTRL_DGRAM_QUERY_MTU: - case BIO_CTRL_DGRAM_GET_FALLBACK_MTU: - ret = -dgram_get_mtu_overhead(data); - if (data->session && data->session->remote_addr.addr.sa.sa_family == AF_INET6 && memcmp(&data->session->remote_addr.addr.sin6.sin6_addr, in6_v4mapped, sizeof(in6_v4mapped)) != 0) - ret += 1280; - else - ret += 576; - break; - case BIO_CTRL_DGRAM_GET_MTU: - return (long)data->mtu; - case BIO_CTRL_DGRAM_SET_MTU: - data->mtu = (unsigned)num; - ret = num; - break; - case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD: - ret = dgram_get_mtu_overhead(data); - break; case BIO_CTRL_DGRAM_SET_PEEK_MODE: data->peekmode = (unsigned)num; break; @@ -172,13 +146,20 @@ static long coap_dgram_ctrl(BIO *a, int cmd, long num, void *ptr) { case BIO_C_SET_FD: case BIO_C_GET_FD: case BIO_CTRL_DGRAM_SET_DONT_FRAG: + case BIO_CTRL_DGRAM_GET_MTU: + case BIO_CTRL_DGRAM_SET_MTU: + case BIO_CTRL_DGRAM_QUERY_MTU: + case BIO_CTRL_DGRAM_GET_FALLBACK_MTU: ret = -1; break; case BIO_CTRL_DUP: case BIO_CTRL_FLUSH: case BIO_CTRL_DGRAM_MTU_DISCOVER: case BIO_CTRL_DGRAM_SET_CONNECTED: + ret = 1; + break; case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT: + data->timeout = coap_ticks_from_rt_us((uint64_t)((struct timeval*)ptr)->tv_sec * 1000000 + ((struct timeval*)ptr)->tv_usec); ret = 1; break; case BIO_CTRL_RESET: @@ -196,6 +177,7 @@ static long coap_dgram_ctrl(BIO *a, int cmd, long num, void *ptr) { case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: case BIO_CTRL_DGRAM_MTU_EXCEEDED: + case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD: default: ret = 0; break; @@ -207,7 +189,7 @@ static int coap_dtls_verify_cert(int ok, X509_STORE_CTX *ctx) { (void)ok; (void)ctx; - if (dtls_log_level <= LOG_WARNING) + if (dtls_log_level >= LOG_WARNING) coap_log(LOG_WARNING, "cannot accept DTLS connection with certificate.\n"); return 0; /* For now, trust no one */ } @@ -245,7 +227,7 @@ static unsigned coap_dtls_psk_client_callback(SSL *ssl, const char *hint, char * if (data->session == NULL || data->session->context == NULL || data->session->context->get_client_psk == NULL) return 0; - psk_len = data->session->context->get_client_psk(data->session, hint, hint_len, (uint8_t*)identity, &identity_len, max_identity_len - 1, (uint8_t*)buf, max_len); + psk_len = data->session->context->get_client_psk(data->session, (const uint8_t*)hint, hint_len, (uint8_t*)identity, &identity_len, max_identity_len - 1, (uint8_t*)buf, max_len); if (identity_len < max_identity_len) identity[identity_len] = 0; return (unsigned)psk_len; @@ -282,24 +264,24 @@ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { pstr = "undefined"; if (where & SSL_CB_LOOP) { - if (dtls_log_level <= LOG_INFO) - coap_log(LOG_INFO, "%s:%s\n", pstr, SSL_state_string_long(ssl)); + if (dtls_log_level >= LOG_DEBUG) + coap_log(LOG_DEBUG, "%s:%s\n", pstr, SSL_state_string_long(ssl)); } else if (where & SSL_CB_ALERT) { pstr = (where & SSL_CB_READ) ? "read" : "write"; - if (dtls_log_level <= LOG_WARNING) - coap_log(LOG_WARNING, "SSL3 alert %s:%s:%s\n", pstr, SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret)); + if (dtls_log_level >= LOG_INFO) + coap_log(LOG_INFO, "SSL3 alert %s:%s:%s\n", pstr, SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret)); if ((where & SSL_CB_WRITE) && (ret >> 8) == SSL3_AL_FATAL) dtls_event = COAP_EVENT_DTLS_ERROR; } else if (where & SSL_CB_EXIT) { if (ret == 0) { - if (dtls_log_level <= LOG_WARNING) { + if (dtls_log_level >= LOG_WARNING) { unsigned long e; coap_log(LOG_WARNING, "%s:failed in %s\n", pstr, SSL_state_string_long(ssl)); while ((e = ERR_get_error())) coap_log(LOG_WARNING, " %s at %s:%s\n", ERR_reason_error_string(e), ERR_lib_error_string(e), ERR_func_error_string(e)); } } else if (ret < 0) { - if (dtls_log_level <= LOG_WARNING) { + if (dtls_log_level >= LOG_WARNING) { int err = SSL_get_error(ssl, ret); if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT && err != SSL_ERROR_WANT_X509_LOOKUP) { long e; @@ -323,7 +305,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { BIO *bio; uint8_t cookie_secret[32]; memset(context, 0, sizeof(coap_dtls_context_t)); - context->ctx = SSL_CTX_new(DTLSv1_2_method()); + context->ctx = SSL_CTX_new(DTLS_method()); if (!context->ctx) goto error; SSL_CTX_set_min_proto_version(context->ctx, DTLS1_2_VERSION); @@ -331,7 +313,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { SSL_CTX_set_read_ahead(context->ctx, 1); SSL_CTX_set_cipher_list(context->ctx, "TLSv1.2:TLSv1.0"); if (!RAND_bytes(cookie_secret, (int)sizeof(cookie_secret))) { - if (dtls_log_level <= LOG_WARNING) + if (dtls_log_level >= LOG_WARNING) coap_log(LOG_WARNING, "Insufficient entropy for random cookie generation"); prng(cookie_secret, sizeof(cookie_secret)); } @@ -345,6 +327,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { SSL_CTX_set_psk_client_callback(context->ctx, coap_dtls_psk_client_callback); SSL_CTX_set_psk_server_callback(context->ctx, coap_dtls_psk_server_callback); SSL_CTX_use_psk_identity_hint(context->ctx, ""); + SSL_CTX_set_options(context->ctx, SSL_OP_NO_QUERY_MTU); context->meth = BIO_meth_new(BIO_TYPE_DGRAM, "coapdgram"); if (!context->meth) goto error; @@ -366,6 +349,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { SSL_set_bio(context->ssl, bio, bio); SSL_set_app_data(context->ssl, NULL); SSL_set_options(context->ssl, SSL_OP_COOKIE_EXCHANGE); + SSL_set_mtu(context->ssl, COAP_DEFAULT_PDU_SIZE); } return context; @@ -406,6 +390,7 @@ void * coap_dtls_new_server_session(coap_session_t *session) { SSL_set_bio(nssl, nbio, nbio); SSL_set_app_data(nssl, NULL); SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE); + SSL_set_mtu(nssl, session->mtu); ssl = dtls->ssl; dtls->ssl = nssl; nssl = NULL; @@ -461,6 +446,7 @@ void *coap_dtls_new_client_session(coap_session_t *session) { SSL_set_bio(ssl, bio, bio); SSL_set_app_data(ssl, session); SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); + SSL_set_mtu(ssl, session->mtu); r = SSL_connect(ssl); if (r == -1) { @@ -480,6 +466,12 @@ error: return NULL; } +void coap_dtls_session_update_mtu(coap_session_t *session) { + SSL *ssl = (SSL *)session->tls; + if (ssl) + SSL_set_mtu(ssl, session->mtu); +} + void coap_dtls_free_session(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; if (ssl) { @@ -524,19 +516,22 @@ int coap_dtls_send(coap_session_t *session, return r; } -int coap_dtls_get_context_timeout(void *dtls_context) { - (void)dtls_context; - return -1; +int coap_dtls_is_context_timeout(void) { + return 0; } -int coap_dtls_get_timeout(coap_session_t *session) { +coap_tick_t coap_dtls_get_context_timeout(void *dtls_context) { + (void)dtls_context; + return 0; +} + +coap_tick_t coap_dtls_get_timeout(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; - struct timeval timeout; + coap_ssl_data *ssl_data; assert(ssl != NULL); - if (DTLSv1_get_timeout(ssl, &timeout) > 0) - return (int)(timeout.tv_sec * 1000 + timeout.tv_usec / 1000); - return -1; + ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl)); + return ssl_data->timeout; } void coap_dtls_handle_timeout(coap_session_t *session) { @@ -555,6 +550,7 @@ int coap_dtls_hello(coap_session_t *session, coap_ssl_data *ssl_data; int r; + SSL_set_mtu(ctx->ssl, session->mtu); ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ctx->ssl)); ssl_data->session = session; ssl_data->pdu = data; diff --git a/src/coap_session.c b/src/coap_session.c index c6277bec..4d045d2d 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -72,7 +72,7 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addre } void coap_session_free(coap_session_t *session) { - coap_pdu_queue_t *q, *tmp; + coap_queue_t *q, *tmp; if (!session) return; @@ -95,11 +95,8 @@ void coap_session_free(coap_session_t *session) { if (session->psk_key) coap_free(session->psk_key); - LL_FOREACH_SAFE(session->sendqueue, q, tmp) { - if (q->pdu) - coap_delete_pdu(q->pdu); - coap_free(q); - } + LL_FOREACH_SAFE(session->sendqueue, q, tmp) + coap_delete_node(q); debug("*** %s: session closed\n", coap_session_str(session)); @@ -137,16 +134,34 @@ ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t d return bytes_written; } +unsigned int calc_timeout(unsigned char r); + ssize_t -coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt) { - coap_pdu_queue_t *q = (coap_pdu_queue_t*)coap_malloc(sizeof(coap_pdu_queue_t)); - if (q == NULL) - return COAP_INVALID_TID; - q->next = NULL; - q->pdu = pdu; - q->retransmit_cnt = retransmit_cnt; - LL_APPEND(session->sendqueue, q); - debug("** %s tid=%d: delayed\n", coap_session_str(session), (int)ntohs(q->pdu->hdr->id)); +coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, + coap_queue_t *node) +{ + if ( node ) { + coap_queue_t *removed = NULL; + coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed); + assert(removed == node); + coap_session_release(node->session); + node->session = NULL; + node->t = 0; + } else { + node = coap_new_node(); + if (node == NULL) + return COAP_INVALID_TID; + node->id = ntohs(pdu->hdr->id); + node->pdu = pdu; + if (pdu->hdr->type == COAP_MESSAGE_CON) { + uint8_t r; + prng(&r, sizeof(r)); + /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ + node->timeout = calc_timeout(r); + } + } + LL_APPEND(session->sendqueue, node); + debug("** %s tid=%d: delayed\n", coap_session_str(session), node->id); return COAP_PDU_DELAYED; } @@ -165,20 +180,20 @@ void coap_session_connected(coap_session_t *session) { while (session->sendqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) { ssize_t bytes_written; - coap_pdu_queue_t *q = session->sendqueue; + coap_queue_t *q = session->sendqueue; session->sendqueue = q->next; + q->next = NULL; debug("** %s tid=%d: transmitted after delay\n", coap_session_str(session), (int)ntohs(q->pdu->hdr->id)); if (session->proto == COAP_PROTO_DTLS) bytes_written = coap_dtls_send(session, (const uint8_t*)q->pdu->hdr, q->pdu->length); else bytes_written = coap_session_send(session, (const uint8_t*)q->pdu->hdr, q->pdu->length); if (bytes_written > 0 && q->pdu->hdr->type == COAP_MESSAGE_CON) { - if (coap_wait_ack(session->context, session, q->pdu, q->retransmit_cnt) >= 0) - q->pdu = NULL; + if (coap_wait_ack(session->context, session, q) >= 0) + q = NULL; } - if (q->pdu) - coap_delete_pdu(q->pdu); - coap_free(q); + if ( q ) + coap_delete_node(q); if (bytes_written < 0) break; } @@ -192,16 +207,16 @@ void coap_session_disconnected(coap_session_t *session) { } session->state = COAP_SESSION_STATE_NONE; while (session->sendqueue) { - coap_pdu_queue_t *q = session->sendqueue; + coap_queue_t *q = session->sendqueue; session->sendqueue = q->next; - debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), (int)ntohs(q->pdu->hdr->id)); + q->next = NULL; + debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), q->id); if (q->pdu->hdr->type == COAP_MESSAGE_CON) { - if (coap_wait_ack(session->context, session, q->pdu, q->retransmit_cnt) >= 0) - q->pdu = NULL; + if (coap_wait_ack(session->context, session, q) >= 0) + q = NULL; } - if (q->pdu) - coap_delete_pdu(q->pdu); - coap_free(q); + if (q) + coap_delete_node(q); } } @@ -217,11 +232,11 @@ void coap_session_reset(coap_session_t *session) { } session->state = COAP_SESSION_STATE_NONE; while (session->sendqueue) { - coap_pdu_queue_t *q = session->sendqueue; + coap_queue_t *q = session->sendqueue; session->sendqueue = q->next; - debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), (int)ntohs(q->pdu->hdr->id)); - coap_delete_pdu(q->pdu); - coap_free(q); + q->next = NULL; + debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), (int)q->id); + coap_delete_node(q); } } @@ -446,6 +461,7 @@ coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, co if (proto == COAP_PROTO_DTLS) { ep->hello.proto = proto; ep->hello.type = COAP_SESSION_TYPE_HELLO; + ep->hello.mtu = ep->default_mtu; ep->hello.context = context; ep->hello.endpoint = ep; } diff --git a/src/coap_time.c b/src/coap_time.c index e80178ed..df8316f5 100644 --- a/src/coap_time.c +++ b/src/coap_time.c @@ -114,6 +114,14 @@ coap_ticks_to_rt(coap_tick_t t) { return coap_clock_offset + (t / COAP_TICKS_PER_SECOND); } +uint64_t coap_ticks_to_rt_us(coap_tick_t t) { + return (uint64_t)coap_clock_offset * 1000000 + (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; +} + +coap_tick_t coap_ticks_from_rt_us(uint64_t t) { + return (coap_tick_t)((t - (uint64_t)coap_clock_offset * 1000000) * COAP_TICKS_PER_SECOND / 1000000); +} + #undef Q #undef FRAC #undef SHR_FP diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 64d6673f..2502204e 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -19,6 +19,9 @@ #include #include +static coap_time_t dtls_tick_0 = 0; +static coap_tick_t coap_tick_0 = 0; + /* Prototypes from dtls_debug.h as including that header will conflict * with coap_config.h. */ void dtls_set_log_level(int); @@ -31,6 +34,8 @@ coap_dtls_is_supported(void) { void coap_dtls_startup(void) { dtls_init(); + dtls_ticks(&dtls_tick_0); + coap_ticks(&coap_tick_0); } void @@ -271,6 +276,11 @@ void *coap_dtls_new_client_session(coap_session_t *session) { return dtls_session; } +void +coap_dtls_session_update_mtu(coap_session_t *session) { + (void)session; +} + void coap_dtls_free_session(coap_session_t *coap_session) { if (coap_session->tls) { @@ -299,21 +309,21 @@ coap_dtls_send(coap_session_t *session, return res; } -int coap_dtls_get_context_timeout(void *dtls_context) { - clock_time_t next = 0; - dtls_check_retransmit((struct dtls_context_t *)dtls_context, &next); - if (next > 0) { - clock_time_t now; - dtls_ticks(&now); - if (next >= now) - return (int)(((next - now) * 1000 + DTLS_TICKS_PER_SECOND - 1) / DTLS_TICKS_PER_SECOND); - } - return -1; +int coap_dtls_is_context_timeout(void) { + return 1; } -int coap_dtls_get_timeout(coap_session_t *session) { +coap_tick_t coap_dtls_get_context_timeout(void *dtls_context) { + clock_time_t next = 0; + dtls_check_retransmit((struct dtls_context_t *)dtls_context, &next); + if (next > 0) + return ((coap_tick_t)(next - dtls_tick_0)) * COAP_TICKS_PER_SECOND / DTLS_TICKS_PER_SECOND + coap_tick_0; + return 0; +} + +coap_tick_t coap_dtls_get_timeout(coap_session_t *session) { (void)session; - return -1; + return 0; } void coap_dtls_handle_timeout(coap_session_t *session) { diff --git a/src/net.c b/src/net.c index 1b3bbb98..ba734f2e 100644 --- a/src/net.c +++ b/src/net.c @@ -124,8 +124,6 @@ #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) -time_t clock_offset = 0; - COAP_STATIC_INLINE coap_queue_t * coap_malloc_node(void) { return (coap_queue_t *)coap_malloc_type(COAP_NODE, sizeof(coap_queue_t)); @@ -165,8 +163,6 @@ coap_free_node(coap_queue_t *node) { #include "mem.h" #include "net/ip/uip-debug.h" -clock_time_t clock_offset = 0; - #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]) @@ -268,7 +264,8 @@ coap_delete_node(coap_queue_t *node) { return 0; coap_delete_pdu(node->pdu); - coap_session_release(node->session); + if ( node->session ) + coap_session_release(node->session); coap_free_node(node); return 1; @@ -444,19 +441,13 @@ coap_new_context( if (initialized) return NULL; #endif /* WITH_CONTIKI */ + + coap_startup(); + #ifndef WITH_CONTIKI c = coap_malloc_type(COAP_CONTEXT, sizeof(coap_context_t)); #endif /* not WITH_CONTIKI */ - coap_clock_init(); -#if defined(WITH_LWIP) - prng_init(LWIP_RAND()); -#elif defined(WITH_CONTIKI) - prng_init((ptrdiff_t)listen_addr ^ clock_offset); -#elif !defined(_WIN32) - prng_init((unsigned long)listen_addr ^ clock_offset); -#endif - #ifndef WITH_CONTIKI if (!c) { #ifndef NDEBUG @@ -639,7 +630,7 @@ coap_send_ack(coap_session_t *session, coap_pdu_t *request) { } static ssize_t -coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt) { +coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { ssize_t bytes_written; #ifdef LWIP @@ -668,14 +659,14 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_cnt) { session->tls = coap_dtls_new_client_session(session); if (session->tls) { session->state = COAP_SESSION_STATE_HANDSHAKE; - return coap_session_delay_pdu(session, pdu, retransmit_cnt); + return coap_session_delay_pdu(session, pdu, node); } } return -1; } if (session->state != COAP_SESSION_STATE_ESTABLISHED) - return coap_session_delay_pdu(session, pdu, retransmit_cnt); + return coap_session_delay_pdu(session, pdu, node); /* Pass to DTLS module for sending if local_interface is for secure * communication. */ @@ -731,7 +722,7 @@ coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned ch * value * @return COAP_TICKS_PER_SECOND * ACK_TIMEOUT * (1 + (ACK_RANDOM_FACTOR - 1) * r) */ -COAP_STATIC_INLINE unsigned int +unsigned int calc_timeout(unsigned char r) { unsigned int result; @@ -759,25 +750,10 @@ calc_timeout(unsigned char r) { coap_tid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, - coap_pdu_t *pdu, int retransmit_cnt) { + coap_queue_t *node) { coap_tick_t now; - unsigned char r; - coap_queue_t *node = coap_new_node(); - if (!node) { - debug("coap_wait_ack: insufficient memory\n"); - return COAP_INVALID_TID; - } - - node->id = ntohs(pdu->hdr->id); - node->retransmit_cnt = retransmit_cnt; - - prng((unsigned char *)&r, sizeof(r)); - - /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ - node->timeout = calc_timeout(r) << retransmit_cnt; node->session = coap_session_reference(session); - node->pdu = pdu; /* Set timer for pdu retransmission. If this is the first element in * the retransmission queue, the base time is set to the current @@ -817,17 +793,17 @@ coap_wait_ack(coap_context_t *context, coap_session_t *session, } #endif /* WITH_CONTIKI */ - debug("** %s tid=%d added to retransmit queue (%us)\n", + debug("** %s tid=%d added to retransmit queue (%ums)\n", coap_session_str(node->session), node->id, - (unsigned)(node->t / COAP_TICKS_PER_SECOND)); + (unsigned)(node->t * 1000 / COAP_TICKS_PER_SECOND)); return node->id; } coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu) { - - ssize_t bytes_written = coap_send_pdu(session, pdu, 0); + uint8_t r; + ssize_t bytes_written = coap_send_pdu(session, pdu, NULL); if (bytes_written == COAP_PDU_DELAYED) return ntohs(pdu->hdr->id); @@ -843,7 +819,18 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { return id; } - return coap_wait_ack(session->context, session, pdu, 0); + coap_queue_t *node = coap_new_node(); + if (!node) { + debug("coap_wait_ack: insufficient memory\n"); + return COAP_INVALID_TID; + } + + node->id = ntohs(pdu->hdr->id); + node->pdu = pdu; + prng(&r, sizeof(r)); + /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ + node->timeout = calc_timeout(r); + return coap_wait_ack(session->context, session, node); } coap_tid_t @@ -854,9 +841,17 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { /* re-initialize timeout when maximum number of retransmissions are not reached yet */ if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT) { ssize_t bytes_written; + coap_tick_t now; node->retransmit_cnt++; - node->t = node->timeout << node->retransmit_cnt; + coap_ticks(&now); + if (context->sendqueue == NULL) { + node->t = node->timeout << node->retransmit_cnt; + context->sendqueue_basetime = now; + } else { + /* make node->t relative to context->sendqueue_basetime */ + node->t = (now - context->sendqueue_basetime) + (node->timeout << node->retransmit_cnt); + } coap_insert_node(&context->sendqueue, node); #ifdef WITH_LWIP if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */ @@ -866,16 +861,12 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { debug("** %s tid=%d: retransmission #%d\n", coap_session_str(node->session), node->id, node->retransmit_cnt); - bytes_written = coap_send_pdu(node->session, node->pdu, node->retransmit_cnt); + bytes_written = coap_send_pdu(node->session, node->pdu, node); if (bytes_written == COAP_PDU_DELAYED) { /* PDU was not retransmitted immediately because a new handshake is - in progress. Must be remove from the retransmit list as it now - belongs to the delayed list of the session. */ - coap_tid_t id = node->id; - node->pdu = NULL; - coap_remove_transaction(&context->sendqueue, node->session, node->id); - return id; + in progress. node was moved to the send queue of the session. */ + return node->id; } if (bytes_written < 0) @@ -1128,7 +1119,6 @@ coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_tid_t (*queue)->t += (*node)->t; } (*node)->next = NULL; - /* coap_delete_node( q ); */ debug("** %s tid=%d: removed\n", coap_session_str(session), id); return 1; } @@ -1147,7 +1137,6 @@ coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_tid_t } q->next = NULL; *node = q; - /* coap_delete_node( q ); */ debug("** %s tid=%d: removed\n", coap_session_str(session), id); return 1; } @@ -1884,14 +1873,43 @@ coap_handle_event(coap_context_t *context, coap_event_t event, void *data) { int coap_can_exit(coap_context_t *context) { - return !context || (context->sendqueue == NULL); + coap_endpoint_t *ep; + coap_session_t *s; + if (!context) + return 1; + if (context->sendqueue) + return 0; + LL_FOREACH(context->endpoint, ep) { + LL_FOREACH(ep->sessions, s) { + if (s->sendqueue) + return 0; + } + } + LL_FOREACH(context->sessions, s) { + if (s->sendqueue) + return 0; + } + return 1; } +static int coap_started = 0; + void coap_startup(void) { + if (coap_started) + return; + coap_started = 1; #if defined(HAVE_WINSOCK2_H) WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); +#endif + coap_clock_init(); +#if defined(WITH_LWIP) + prng_init(LWIP_RAND()); +#elif defined(WITH_CONTIKI) + prng_init(0); +#elif !defined(_WIN32) + prng_init(0); #endif coap_dtls_startup(); }