Skip to content

Commit

Permalink
coap: Cleanup transactions when client is unregistered
Browse files Browse the repository at this point in the history
All pending transactions of a just unregistered client must be removed
  • Loading branch information
mlasch authored and LukasWoodtli committed Jan 22, 2025
1 parent 8493582 commit 7a963dd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
19 changes: 19 additions & 0 deletions coap/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions core/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions include/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/helper/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 7a963dd

Please sign in to comment.