Skip to content

Commit b4114e2

Browse files
mlaschLukasWoodtli
authored andcommitted
coap: Cleanup transactions when client is unregistered
All pending transactions of a just unregistered client must be removed
1 parent 8493582 commit b4114e2

File tree

8 files changed

+44
-9
lines changed

8 files changed

+44
-9
lines changed

coap/transaction.c

+19
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,22 @@ bool transaction_free_userData(lwm2m_context_t * context, lwm2m_transaction_t *
527527
transaction->userData = NULL;
528528
return true;
529529
}
530+
531+
/*
532+
* Remove transactions from a specific client.
533+
*/
534+
void transaction_remove_client(lwm2m_context_t *contextP, lwm2m_client_t *clientP) {
535+
lwm2m_transaction_t *transacP;
536+
537+
LOG_DBG("Entering");
538+
transacP = contextP->transactionList;
539+
while (transacP != NULL) {
540+
lwm2m_transaction_t *nextP = transacP->next;
541+
542+
if (lwm2m_session_is_equal(transacP->peerH, clientP->sessionH, contextP->userData)) {
543+
LOG_DBG("Found session to remove");
544+
transaction_remove(contextP, transacP);
545+
}
546+
transacP = nextP;
547+
}
548+
}

core/internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ void transaction_remove(lwm2m_context_t * contextP, lwm2m_transaction_t * transa
263263
bool transaction_handleResponse(lwm2m_context_t * contextP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response);
264264
void transaction_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP);
265265
bool transaction_free_userData(lwm2m_context_t * context, lwm2m_transaction_t * transaction);
266+
void transaction_remove_client(lwm2m_context_t *contextP, lwm2m_client_t *clientP);
266267
bool transaction_set_payload(lwm2m_transaction_t *transaction, uint8_t *buffer, size_t length);
267268

268269
#ifdef LWM2M_SUPPORT_TLV

core/liblwm2m.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void lwm2m_close(lwm2m_context_t * contextP)
218218
clientP = contextP->clientList;
219219
contextP->clientList = contextP->clientList->next;
220220

221-
registration_freeClient(clientP);
221+
registration_freeClient(contextP, clientP);
222222
}
223223
#endif
224224

core/registration.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,7 @@ static lwm2m_client_t * prv_getClientByName(lwm2m_context_t * contextP,
17211721
return targetP;
17221722
}
17231723

1724-
void registration_freeClient(lwm2m_client_t * clientP)
1725-
{
1724+
void registration_freeClient(lwm2m_context_t *const context, lwm2m_client_t *clientP) {
17261725
LOG_DBG("Entering");
17271726
if (clientP->name != NULL) lwm2m_free(clientP->name);
17281727
if (clientP->msisdn != NULL) lwm2m_free(clientP->msisdn);
@@ -1744,6 +1743,8 @@ void registration_freeClient(lwm2m_client_t * clientP)
17441743

17451744
free_block_data(targetP);
17461745
}
1746+
transaction_remove_client(context, clientP);
1747+
lwm2m_session_remove(clientP->sessionH);
17471748
lwm2m_free(clientP);
17481749
}
17491750

@@ -1858,7 +1859,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
18581859
{
18591860
lwm2m_client_t * tmpClientP = utils_findClient(contextP, fromSessionH);
18601861
contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_RM(contextP->clientList, tmpClientP->internalID, &tmpClientP);
1861-
registration_freeClient(tmpClientP);
1862+
registration_freeClient(contextP, tmpClientP);
18621863
}
18631864
}
18641865
if (clientP != NULL)
@@ -1898,12 +1899,12 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
18981899

18991900
if (prv_getLocationString(clientP->internalID, location) == 0)
19001901
{
1901-
registration_freeClient(clientP);
1902+
registration_freeClient(contextP, clientP);
19021903
return COAP_500_INTERNAL_SERVER_ERROR;
19031904
}
19041905
if (coap_set_header_location_path(response, location) == 0)
19051906
{
1906-
registration_freeClient(clientP);
1907+
registration_freeClient(contextP, clientP);
19071908
return COAP_500_INTERNAL_SERVER_ERROR;
19081909
}
19091910

@@ -2023,7 +2024,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
20232024
{
20242025
contextP->monitorCallback(contextP, clientP->internalID, NULL, COAP_202_DELETED, NULL, LWM2M_CONTENT_TEXT, NULL, 0, contextP->monitorUserData);
20252026
}
2026-
registration_freeClient(clientP);
2027+
registration_freeClient(contextP, clientP);
20272028
result = COAP_202_DELETED;
20282029
}
20292030
break;
@@ -2143,7 +2144,7 @@ void registration_step(lwm2m_context_t * contextP,
21432144
{
21442145
contextP->monitorCallback(contextP, clientP->internalID, NULL, COAP_202_DELETED, NULL, LWM2M_CONTENT_TEXT, NULL, 0, contextP->monitorUserData);
21452146
}
2146-
registration_freeClient(clientP);
2147+
registration_freeClient(contextP, clientP);
21472148
}
21482149
else
21492150
{

core/registration.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
uint8_t registration_handleRequest(lwm2m_context_t *contextP, lwm2m_uri_t *uriP, void *fromSessionH,
2424
coap_packet_t *message, coap_packet_t *response);
2525
void registration_deregister(lwm2m_context_t *contextP, lwm2m_server_t *serverP);
26-
void registration_freeClient(lwm2m_client_t *clientP);
26+
void registration_freeClient(lwm2m_context_t *contextP, lwm2m_client_t *clientP);
2727
uint8_t registration_start(lwm2m_context_t *contextP, bool restartFailed);
2828
void registration_step(lwm2m_context_t *contextP, time_t currentTime, time_t *timeoutP);
2929
lwm2m_status_t registration_getStatus(lwm2m_context_t *contextP);

include/liblwm2m.h

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void
157157
// userData: parameter to lwm2m_init()
158158
bool lwm2m_session_is_equal(void * session1, void * session2, void * userData);
159159

160+
/*
161+
* Remove session from list
162+
*/
163+
void lwm2m_session_remove(void *sessionH);
164+
160165
/*
161166
* Error code
162167
*/

tests/helper/connection.c

+7
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ uint8_t *test_get_response_buffer(size_t *len) {
5050
*len = response_len;
5151
return response_buffer;
5252
}
53+
54+
void lwm2m_session_remove(void *sessionH) {
55+
(void)sessionH;
56+
57+
// Currently the tests do not use a session structure, assume a NULL pointer here.
58+
assert(sessionH == NULL);
59+
}

transport/udp/connection.c

+2
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,5 @@ bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) {
213213

214214
return (session1 == session2);
215215
}
216+
217+
void lwm2m_session_remove(void *session_h) { (void)session_h; /* unused */ }

0 commit comments

Comments
 (0)