From adf1238eaec04244ff4bc1155d484c388a932624 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Wed, 21 Jun 2017 20:40:15 +0200 Subject: [PATCH] Add DTLS retransmit and recovery. Add lossy networks debug option in examples. --- Makefile.am | 1 + examples/client.c | 7 +++- examples/coap-server.c | 11 ++++-- include/coap/coap_dtls.h | 35 +++++++++++++++---- include/coap/debug.h | 3 ++ libcoap-1.map | 1 + libcoap-1.sym | 1 + src/coap_io.c | 74 +++++++++++++++++++++++++++++++++++----- src/coap_openssl.c | 24 +++++++++++++ src/coap_tinydtls.c | 21 ++++++++++++ src/debug.c | 31 +++++++++++++++++ src/session.c | 4 +++ 12 files changed, 194 insertions(+), 19 deletions(-) diff --git a/Makefile.am b/Makefile.am index 10b0500d..2410ccf7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,6 +52,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/async.c \ src/block.c \ src/coap_io.c \ + src/coap_notls.c \ src/coap_tinydtls.c \ src/coap_event.c \ src/coap_openssl.c \ diff --git a/examples/client.c b/examples/client.c index 038c1750..ce3ee878 100644 --- a/examples/client.c +++ b/examples/client.c @@ -574,6 +574,8 @@ usage( const char *program, const char *version) { "\t\t\trequest)\n" "\t-T token\tinclude specified token\n" "\t-U\t\tnever include Uri-Host or Uri-Port options\n" + "\t-l num\t\tFail to send the first num datagrams (for debugging only)\n" + "\t-l loss%%\t\tRandmoly fail to send datagrams with the specified probability(for debugging only)\n" "\n" "examples:\n" "\tcoap-client -m get coap://[::1]/\n" @@ -1109,7 +1111,7 @@ main(int argc, char **argv) { ssize_t user_length = 0, key_length = 0; int create_uri_opts = 1; - while ((opt = getopt(argc, argv, "Na:b:e:f:g:k:m:p:s:t:o:v:A:B:O:P:T:u:U")) != -1) { + while ((opt = getopt(argc, argv, "Na:b:e:f:g:k:m:p:s:t:o:v:A:B:O:P:T:u:U:l:")) != -1) { switch (opt) { case 'a' : strncpy(node_str, optarg, NI_MAXHOST-1); @@ -1186,6 +1188,9 @@ main(int argc, char **argv) { case 'v' : log_level = strtol(optarg, NULL, 10); break; + case 'l': + coap_debug_set_packet_loss( optarg ); + break; default: usage( argv[0], LIBCOAP_PACKAGE_VERSION ); exit( 1 ); diff --git a/examples/coap-server.c b/examples/coap-server.c index 70114db5..317b858c 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -342,8 +342,10 @@ usage( const char *program, const char *version) { "\t-A address\tinterface address to bind to\n" "\t-g group\tjoin the given multicast group\n" "\t-p port\t\tlisten on specified port\n" - "\t-v num\t\tverbosity level (default: 3)\n", - program, version, program ); + "\t-v num\t\tverbosity level (default: 3)\n" + "\t-l num\t\tFail to send the first num datagrams (for debugging only)\n" + "\t-l loss%%\t\tRandmoly fail to send datagrams with the specified probability(for debugging only)\n", + program, version, program ); } static coap_context_t * @@ -488,7 +490,7 @@ main(int argc, char **argv) { clock_offset = time(NULL); - while ((opt = getopt(argc, argv, "A:g:p:v:")) != -1) { + while ((opt = getopt(argc, argv, "A:g:p:v:l:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -504,6 +506,9 @@ main(int argc, char **argv) { case 'v' : log_level = strtol(optarg, NULL, 10); break; + case 'l': + coap_debug_set_packet_loss( optarg ); + break; default: usage( argv[0], LIBCOAP_PACKAGE_VERSION ); exit( 1 ); diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 7e10749f..054cbc98 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -75,16 +75,39 @@ void *coap_dtls_new_server_session( coap_session_t *session ); void coap_dtls_free_session( coap_session_t *session ); /** -* Send data to a DTLS peer. -* -* @param session The CoAP session -* @param pdu The CoAP PDU -* @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent -*/ + * Send data to a DTLS peer. + * + * @param session The CoAP session + * @param pdu The CoAP PDU + * @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent + */ int coap_dtls_send( coap_session_t *session, const uint8_t *data, size_t data_len ); +/** + * 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. + */ +int 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. + */ +int coap_dtls_get_timeout( coap_session_t *session ); + +/** + * Handle a DTLS timeout expiration. + * + * @param session The CoAP session + */ +void coap_dtls_handle_timeout( coap_session_t *session ); + /** * Handling incoming data from a DTLS peer. * diff --git a/include/coap/debug.h b/include/coap/debug.h index 356f401e..893f29e2 100644 --- a/include/coap/debug.h +++ b/include/coap/debug.h @@ -69,4 +69,7 @@ void coap_show_pdu(const coap_pdu_t *); struct coap_address_t; size_t coap_print_addr(const struct coap_address_t *, unsigned char *, size_t); +void coap_debug_set_packet_loss( const char *loss_level ); +int coap_debug_send_packet(); + #endif /* _COAP_DEBUG_H_ */ diff --git a/libcoap-1.map b/libcoap-1.map index 4a54a537..7b9c2517 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -19,6 +19,7 @@ global: coap_clock_init; coap_clone_uri; coap_context_set_psk; + coap_debug_set_packet_loss; coap_decode_var_bytes; coap_delete_all; coap_delete_all_resources; diff --git a/libcoap-1.sym b/libcoap-1.sym index 57b5ae02..d0f6dccd 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -17,6 +17,7 @@ coap_clear_event_handler coap_clock_init coap_clone_uri coap_context_set_psk +coap_debug_set_packet_loss coap_decode_var_bytes coap_delete_all coap_delete_all_resources diff --git a/src/coap_io.c b/src/coap_io.c index fcc58d88..bf53a312 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -311,7 +311,9 @@ ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen ) { ssize_t bytes_written = 0; - if ( sock->flags & COAP_SOCKET_CONNECTED ) { + if ( !coap_debug_send_packet() ) { + bytes_written = (ssize_t)datalen; + } else if ( sock->flags & COAP_SOCKET_CONNECTED ) { #ifdef _WIN32 bytes_written = send( sock->fd, (const char *)data, (int)datalen, 0 ); #else @@ -675,12 +677,14 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) int -coap_run_once( coap_context_t *ctx, unsigned int 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; - unsigned long max_wait; + 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; @@ -712,24 +716,72 @@ coap_run_once( coap_context_t *ctx, unsigned int timeout_ms ) { } max_wait = timeout_ms * COAP_TICKS_PER_SECOND / 1000; + while ( 1 ) { + dtls_timeout = 0; + dtls_timeout_session = NULL; + nextpdu = coap_peek_next( ctx ); - while ( nextpdu && nextpdu->t <= now - ctx->sendqueue_basetime ) { + while ( nextpdu && now >= ctx->sendqueue_basetime && nextpdu->t <= now - ctx->sendqueue_basetime ) { coap_retransmit( ctx, coap_pop_next( ctx ) ); nextpdu = coap_peek_next( ctx ); } - if ( nextpdu && ( ( max_wait == 0 ) || nextpdu->t < max_wait ) ) { + 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; + } + } + } + } + } 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 { + } 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 ) ) ? &tv : NULL ); + result = select( nfds, &readfds, 0, 0, ( nextpdu || max_wait > 0 || dtls_timeout > 0 ) ? &tv : NULL ); if ( result < 0 ) { /* error */ #ifdef _WIN32 @@ -764,10 +816,14 @@ coap_run_once( coap_context_t *ctx, unsigned int timeout_ms ) { } else { /* timeout */ coap_tick_t past = now; coap_ticks( &now ); - if ( past + max_wait <= 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 long)( now - past ); + max_wait -= (unsigned)( now - past ); } } } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index d3fa2bd2..21fe964d 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -503,6 +503,30 @@ int coap_dtls_send( coap_session_t *session, return r; } +int coap_dtls_get_context_timeout( void *dtls_context ) { + return -1; +} + +int coap_dtls_get_timeout( coap_session_t *session ) { + SSL *ssl = (SSL *)session->tls; + struct timeval timeout; + + assert( ssl != NULL ); + if ( DTLSv1_get_timeout( ssl, &timeout ) > 0 ) + return (int)( timeout.tv_sec * 1000 + timeout.tv_usec / 1000 ); + return -1; +} + +void coap_dtls_handle_timeout( coap_session_t *session ) { + SSL *ssl = (SSL *)session->tls; + + assert( ssl != NULL ); + if ( DTLSv1_handle_timeout( ssl ) < 0 ) { + /* Too may retries */ + coap_session_disconnected( session ); + } +} + int coap_dtls_hello( coap_session_t *session, const uint8_t *data, size_t data_len ) { diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index ec4d5872..c8e6da49 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -312,6 +312,27 @@ 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 ) { +#if DTLS_TICKS_PER_SECOND != 1000 + return (int)(( next * 1000 + DTLS_TICKS_PER_SECOND - 1 ) / DTLS_TICKS_PER_SECOND); +#else + return (int)next; +#endif + } + return -1; +} + +int coap_dtls_get_timeout( coap_session_t *session ) { + return -1; +} + +void coap_dtls_handle_timeout( coap_session_t *session ) { + return 0; +} + int coap_dtls_receive( coap_session_t *session, const uint8_t *data, diff --git a/src/debug.c b/src/debug.c index fca920d6..d8ac4209 100644 --- a/src/debug.c +++ b/src/debug.c @@ -506,3 +506,34 @@ coap_log_impl(coap_log_t level, const char *format, ...) { fflush(log_fd); } +static int num_initial_packets_lost = 0; +static int packet_loss_level = 0; +static int send_packet_count = 0; + +void coap_debug_set_packet_loss( const char *loss_level ) { + char *end = NULL; + int level = (int)strtol( loss_level, &end, 10 ); + if ( end == loss_level || level <= 0 ) + return; + if ( *end == '%' ) { + if ( level > 100 ) + level = 100; + packet_loss_level = level * 65536 / 100; + } else { + num_initial_packets_lost = (int)level; + } + send_packet_count = 0; +} + +int coap_debug_send_packet() { + ++send_packet_count; + if ( num_initial_packets_lost > 0 && send_packet_count <= num_initial_packets_lost ) + return 0; + if ( packet_loss_level > 0 ) { + uint16_t r = 0; + prng( (uint8_t*)&r, 2 ); + if ( r < packet_loss_level ) + return 0; + } + return 1; +} diff --git a/src/session.c b/src/session.c index 363c30b7..477a4f1a 100644 --- a/src/session.c +++ b/src/session.c @@ -153,6 +153,10 @@ void coap_session_connected( coap_session_t *session ) { void coap_session_disconnected( coap_session_t *session ) { debug( "*** %s: session disconnected\n", coap_session_str( session ) ); + if ( session->proto == COAP_PROTO_DTLS && session->tls ) { + coap_dtls_free_session( session ); + session->tls = NULL; + } session->state = COAP_SESSION_STATE_NONE; while ( session->sendqueue ) { coap_pdu_queue_t *q = session->sendqueue;