From 7a963ddc4f6484dcfcb97f99263e7d9fef7ac781 Mon Sep 17 00:00:00 2001 From: Marc Lasch Date: Tue, 30 Aug 2022 08:32:23 +0200 Subject: [PATCH] coap: Cleanup transactions when client is unregistered All pending transactions of a just unregistered client must be removed --- coap/transaction.c | 19 +++++++++++++++++++ core/liblwm2m.c | 2 +- core/registration.c | 8 +++++--- include/liblwm2m.h | 5 +++++ tests/helper/connection.c | 7 +++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/coap/transaction.c b/coap/transaction.c index 01a5bf092..7aa9518fa 100644 --- a/coap/transaction.c +++ b/coap/transaction.c @@ -527,3 +527,22 @@ bool transaction_free_userData(lwm2m_context_t * context, lwm2m_transaction_t * transaction->userData = NULL; return true; } + +/* + * Remove transactions from a specific client. + */ +void transaction_remove_client(lwm2m_context_t *contextP, lwm2m_client_t *clientP) { + lwm2m_transaction_t *transacP; + + LOG("Entering"); + transacP = contextP->transactionList; + while (transacP != NULL) { + lwm2m_transaction_t *nextP = transacP->next; + + if (lwm2m_session_is_equal(transacP->peerH, clientP->sessionH, contextP->userData)) { + LOG("Found session to remove"); + transaction_remove(contextP, transacP); + } + transacP = nextP; + } +} diff --git a/core/liblwm2m.c b/core/liblwm2m.c index 2bff2833e..515df5d48 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -218,7 +218,7 @@ void lwm2m_close(lwm2m_context_t * contextP) clientP = contextP->clientList; contextP->clientList = contextP->clientList->next; - registration_freeClient(clientP); + registration_freeClient(contextP, clientP); } #endif diff --git a/core/registration.c b/core/registration.c index 5bafc6679..04c416449 100644 --- a/core/registration.c +++ b/core/registration.c @@ -1744,6 +1744,8 @@ void registration_freeClient(lwm2m_client_t * clientP) free_block_data(targetP); } + transaction_remove_client(contextP, clientP); + lwm2m_session_remove(clientP->sessionH); lwm2m_free(clientP); } @@ -1858,7 +1860,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP, { lwm2m_client_t * tmpClientP = utils_findClient(contextP, fromSessionH); contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_RM(contextP->clientList, tmpClientP->internalID, &tmpClientP); - registration_freeClient(tmpClientP); + registration_freeClient(contextP, tmpClientP); } } if (clientP != NULL) @@ -1898,12 +1900,12 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP, if (prv_getLocationString(clientP->internalID, location) == 0) { - registration_freeClient(clientP); + registration_freeClient(contextP, clientP); return COAP_500_INTERNAL_SERVER_ERROR; } if (coap_set_header_location_path(response, location) == 0) { - registration_freeClient(clientP); + registration_freeClient(contextP, clientP); return COAP_500_INTERNAL_SERVER_ERROR; } diff --git a/include/liblwm2m.h b/include/liblwm2m.h index 16039418c..a77010d4f 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -157,6 +157,11 @@ uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void // userData: parameter to lwm2m_init() bool lwm2m_session_is_equal(void * session1, void * session2, void * userData); +/* + * Remove session from list + */ +void lwm2m_session_remove(void *sessionH); + /* * Error code */ diff --git a/tests/helper/connection.c b/tests/helper/connection.c index d7b2d91a7..2a9a48521 100644 --- a/tests/helper/connection.c +++ b/tests/helper/connection.c @@ -50,3 +50,10 @@ uint8_t *test_get_response_buffer(size_t *len) { *len = response_len; return response_buffer; } + +void lwm2m_session_remove(void *sessionH) { + (void)sessionH; + + // Currently the tests do not use a session structure, assume a NULL pointer here. + assert(sessionH == NULL); +}