From 6e97dbafcf24713a64ed81f6571200d7de04bb75 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Sat, 24 Jun 2017 17:10:50 +0200 Subject: [PATCH] Add facility for the removal of all queued messages and observations of a session. Apply it to client-side destination unreachable notification. --- include/coap/coap_session.h | 7 +++++++ include/coap/net.h | 14 ++++++++++++-- include/coap/resource.h | 8 ++++++++ src/coap_io.c | 4 ++-- src/coap_session.c | 21 +++++++++++++++++++++ src/net.c | 35 ++++++++++++++++++++++++++++++++++- src/resource.c | 14 ++++++++++++++ 7 files changed, 98 insertions(+), 5 deletions(-) diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 28e567e9..b0237f0d 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -95,6 +95,13 @@ void coap_session_release(coap_session_t *session); */ void coap_session_disconnected(coap_session_t *session); +/** +* Notify session that the remote peer is no longer listening. +* +* @param session The CoAP session. +*/ +void coap_session_reset(coap_session_t *session); + /** * Notify session that it has just connected or reconnected. * diff --git a/include/coap/net.h b/include/coap/net.h index f1acfee2..a512b8e2 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -447,11 +447,11 @@ coap_wait_ack( coap_context_t *context, coap_session_t *session, coap_queue_t *coap_find_transaction(coap_queue_t *queue, coap_session_t *session, coap_tid_t id); /** - * Cancels all outstanding messages for peer @p dst that have the specified + * Cancels all outstanding messages for session @p session that have the specified * token. * * @param context The context in use. - * @param dst Session of the messages to remove. + * @param session Session of the messages to remove. * @param token Message token. * @param token_length Actual length of @p token. */ @@ -460,6 +460,16 @@ void coap_cancel_all_messages(coap_context_t *context, const unsigned char *token, size_t token_length); +/** +* Cancels all outstanding messages for session @p session. +* +* @param context The context in use. +* @param session Session of the messages to remove. +*/ +void +coap_cancel_session_messages(coap_context_t *context, + coap_session_t *session); + /** * Dispatches the PDUs from the receive queue in given context. */ diff --git a/include/coap/resource.h b/include/coap/resource.h index 88127f78..b0573a39 100644 --- a/include/coap/resource.h +++ b/include/coap/resource.h @@ -344,6 +344,14 @@ int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const str *token); +/** + * Removes any subscription for @p session and releases the allocated storage. + * + * @param context The CoAP context to use. + * @param session The observer's session. + */ +void coap_delete_observers(coap_context_t *context, coap_session_t *session); + /** * Checks for all known resources, if they are dirty and notifies subscribed * observers. diff --git a/src/coap_io.c b/src/coap_io.c index fa8d3157..ab8e775d 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -546,7 +546,7 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { coap_log(LOG_WARNING, "coap_network_read: unreachable\n"); coap_free_packet(*packet); *packet = NULL; - return 0; + return -2; } coap_log(LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror()); goto error; @@ -884,7 +884,7 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { coap_session_free(expiration_session); } - if (dtls_timeout > 0 && dtls_timeout_session && before + dtls_timeout <= now) { + 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); } diff --git a/src/coap_session.c b/src/coap_session.c index 734ad693..91a706f8 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -17,6 +17,7 @@ #include "net.h" #include "debug.h" #include "mem.h" +#include "resource.h" #include "utlist.h" #include @@ -171,6 +172,26 @@ void coap_session_disconnected(coap_session_t *session) { } } +void coap_session_reset(coap_session_t *session) { + debug("*** %s: session reset\n", coap_session_str(session)); +#ifndef WITHOUT_OBSERVE + coap_delete_observers(session->context, session); +#endif + coap_cancel_session_messages(session->context, 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; + 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); + } +} + coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now) { diff --git a/src/net.c b/src/net.c index 58d5fba4..b0cde5e1 100644 --- a/src/net.c +++ b/src/net.c @@ -950,7 +950,10 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) bytes_read = ctx->network_read(&session->sock, &packet); if (bytes_read < 0) { - warn("* %s: read error\n", coap_session_str(session)); + if (bytes_read == -2) + coap_session_reset(session); + else + 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; @@ -1135,6 +1138,36 @@ token_match(const unsigned char *a, size_t alen, return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0); } +void +coap_cancel_session_messages(coap_context_t *context, coap_session_t *session) { + coap_queue_t *p, *q; + + while (context->sendqueue && context->sendqueue->session == session) { + q = context->sendqueue; + context->sendqueue = q->next; + debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); + coap_delete_node(q); + } + + if (!context->sendqueue) + return; + + p = context->sendqueue; + q = p->next; + + while (q) { + if (q->session == session) { + p->next = q->next; + debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); + coap_delete_node(q); + q = p->next; + } else { + p = q; + q = q->next; + } + } +} + void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, const unsigned char *token, size_t token_length) { diff --git a/src/resource.c b/src/resource.c index cc327adb..bac05d58 100644 --- a/src/resource.c +++ b/src/resource.c @@ -623,6 +623,20 @@ coap_delete_observer(coap_resource_t *resource, coap_session_t *session, return s != NULL; } +void +coap_delete_observers(coap_context_t *context, coap_session_t *session) { + RESOURCES_ITER(context->resources, resource) { + coap_subscription_t *s, *tmp; + LL_FOREACH_SAFE(resource->subscribers, s, tmp) { + if (s->session == session) { + LL_DELETE(resource->subscribers, s); + coap_session_release(session); + COAP_FREE_TYPE(subscription, s); + } + } + } +} + static void coap_notify_observers(coap_context_t *context, coap_resource_t *r) { coap_method_handler_t h;