From 44e5c68c51f5318b0654b67cfee3b3d97b99b382 Mon Sep 17 00:00:00 2001 From: "Florek, Bartlomiej" Date: Thu, 19 Mar 2026 11:45:38 +0000 Subject: [PATCH] fix: OSCORE ACK retransmission handling --- src/coap_net.c | 8 +++----- tests/test_sendqueue.c | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/coap_net.c b/src/coap_net.c index a01eec4e..249a60f2 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -4618,13 +4618,11 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, switch (pdu->type) { case COAP_MESSAGE_ACK: - if (sent != NULL) { - coap_delete_node_lkd(sent); + if (NULL == sent) { + /* find message id in sendqueue to stop retransmission */ + coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent); } - /* find message id in sendqueue to stop retransmission */ - coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent); - if (sent && session->con_active) { session->con_active--; if (session->state == COAP_SESSION_STATE_ESTABLISHED) diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index 8d54810d..a6ccba00 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -276,6 +276,49 @@ t_sendqueue10(void) { CU_ASSERT(tmp_node->t == timestamp[2]); } +/* + * Regression guard for ACK handling: exercise the real coap_dispatch() + * ACK path and ensure the retransmission node is consumed exactly once. + */ +static void +t_sendqueue11(void) { + int result; + coap_queue_t *queued = NULL; + coap_pdu_t *sent_pdu = NULL; + uint8_t ack_data[] = { 0x60, 0x00, 0x12, 0x34 }; + + CU_ASSERT_PTR_NULL(ctx->sendqueue); + + queued = coap_new_node(); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(queued); + + sent_pdu = coap_pdu_init(COAP_MESSAGE_CON, + COAP_REQUEST_CODE_GET, + 0x1234, + 64); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(sent_pdu); + + queued->next = NULL; + queued->session = coap_session_reference(session); + queued->pdu = sent_pdu; + queued->id = 0x1234; + queued->t = timestamp[1]; + + result = coap_insert_node(&ctx->sendqueue, queued); + CU_ASSERT(result > 0); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(ctx->sendqueue); + + session->state = COAP_SESSION_STATE_ESTABLISHED; + coap_lock_lock(return); + coap_handle_dgram(ctx, session, ack_data, sizeof(ack_data)); + CU_ASSERT_PTR_NULL(ctx->sendqueue); + + /* Re-dispatch another ACK with the same mid to ensure idempotent behavior. */ + coap_handle_dgram(ctx, session, ack_data, sizeof(ack_data)); + coap_lock_unlock(); + CU_ASSERT_PTR_NULL(ctx->sendqueue); +} + /* This function creates a set of nodes for testing. These nodes * will exist for all tests and are modified by coap_insert_node() * and coap_remove_from_queue(). @@ -361,6 +404,7 @@ t_init_sendqueue_tests(void) { SENDQUEUE_TEST(suite, t_sendqueue8); SENDQUEUE_TEST(suite, t_sendqueue9); SENDQUEUE_TEST(suite, t_sendqueue10); + SENDQUEUE_TEST(suite, t_sendqueue11); return suite; }