Skip to content

Commit

Permalink
examples: Update connection handling
Browse files Browse the repository at this point in the history
Add function for removing connection.
  • Loading branch information
LukasWoodtli committed Jan 23, 2025
1 parent b4114e2 commit 29435ee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
24 changes: 24 additions & 0 deletions transport/tinydtls/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,27 @@ uint8_t lwm2m_buffer_send(void *sessionH, uint8_t *buffer, size_t length, void *
}

bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) { return (session1 == session2); }

void lwm2m_session_remove(void *sessionH) {
lwm2m_dtls_connection_t *connP = (lwm2m_dtls_connection_t *)sessionH;
dtls_app_context_t *appContext = (dtls_app_context_t *)connP->dtlsContext;
lwm2m_dtls_connection_t *connIter;

connIter = appContext->connList;

if (connIter == NULL) {
return;
}
if (connIter == sessionH) {
/* First element */
free(sessionH);
appContext->connList = NULL;
return;
}

while (connIter->next != connP)
;
connIter->next = connP->next;

free(connP);
}
37 changes: 31 additions & 6 deletions transport/udp/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <sys/socket.h>
#include <sys/types.h>

static lwm2m_connection_t *connectionList = NULL;

static int find_and_bind_to_address(struct addrinfo *res) {
int s = -1;
for (struct addrinfo *p = res; p != NULL && s == -1; p = p->ai_next) {
Expand Down Expand Up @@ -80,7 +82,7 @@ int lwm2m_create_socket(const char *portStr, int addressFamily) {
lwm2m_connection_t *lwm2m_connection_find(lwm2m_connection_t *connList, struct sockaddr_storage *addr, size_t addrLen) {
lwm2m_connection_t *connP;

connP = connList;
connP = connectionList;
while (connP != NULL) {
if ((connP->addrLen == addrLen) && (memcmp(&(connP->addr), addr, addrLen) == 0)) { // NOSONAR
return connP;
Expand All @@ -100,7 +102,9 @@ lwm2m_connection_t *lwm2m_connection_new_incoming(lwm2m_connection_t *connList,
connP->sock = sock;
memcpy(&(connP->addr), addr, addrLen);
connP->addrLen = addrLen;
connP->next = connList;
connP->next = connectionList;

connectionList = connP;
}

return connP;
Expand Down Expand Up @@ -139,10 +143,10 @@ void lwm2m_connection_free(lwm2m_connection_t *connList) {
while (connList != NULL) {
lwm2m_connection_t *nextP;

nextP = connList->next;
lwm2m_free(connList);
nextP = connectionList->next;
lwm2m_free(connectionList);

connList = nextP;
connectionList = nextP;
}
}

Expand Down Expand Up @@ -214,4 +218,25 @@ bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) {
return (session1 == session2);
}

void lwm2m_session_remove(void *session_h) { (void)session_h; /* unused */ }
void lwm2m_session_remove(void *sessionH) {
lwm2m_connection_t *connP = (lwm2m_connection_t *)sessionH;
lwm2m_connection_t *connIter;

connIter = connectionList;

if (connIter == NULL) {
return;
}
if (connIter == sessionH) {
/* First element */
free(sessionH);
connectionList = NULL;
return;
}

while (connIter->next != connP)
;
connIter->next = connP->next;

free(connP);
}

0 comments on commit 29435ee

Please sign in to comment.