From 7f0b32874b56a1040e64fa1e47b25ed4ab79edc6 Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Tue, 21 May 2024 14:09:29 +0000 Subject: [PATCH] tcp: pair connection with the tls peer --- api/oc_client_api.c | 5 +-- api/oc_endpoint.c | 30 +++++++++++++++++ api/oc_tcp.c | 10 ++++++ api/oc_tcp_internal.h | 4 +++ include/oc_endpoint.h | 6 +++- port/android/ipadapter.c | 10 ++++-- port/android/tcpadapter.c | 8 ++++- port/android/tcpadapter.h | 3 +- port/esp32/adapter/src/ipadapter.c | 10 ++++-- port/esp32/adapter/src/tcpadapter.c | 8 ++++- port/esp32/adapter/src/tcpadapter.h | 3 +- port/linux/ipadapter.c | 9 +++-- port/linux/tcpsession.c | 36 +++++++++++++++++--- port/linux/tcpsession.h | 3 +- port/oc_connectivity_internal.h | 4 ++- port/windows/ipadapter.c | 9 +++-- port/windows/tcpadapter.c | 9 ++++- port/windows/tcpadapter.h | 3 +- security/oc_tls.c | 51 +++++++++++++++++++++++------ 19 files changed, 183 insertions(+), 38 deletions(-) diff --git a/api/oc_client_api.c b/api/oc_client_api.c index fbfe120532..7c805e9c45 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -967,8 +967,9 @@ oc_close_session(const oc_endpoint_t *endpoint) #endif /* OC_SECURITY */ } else if (endpoint->flags & TCP) { #ifdef OC_TCP - if (oc_connectivity_end_session_v1(endpoint, false)) { - oc_handle_session(endpoint, OC_SESSION_DISCONNECTED); + oc_endpoint_t session_endpoint; + while (oc_connectivity_end_session_v1(endpoint, false, &session_endpoint)) { + oc_handle_session(&session_endpoint, OC_SESSION_DISCONNECTED); } #endif /* OC_TCP */ } diff --git a/api/oc_endpoint.c b/api/oc_endpoint.c index 45a489f664..5a126231fb 100644 --- a/api/oc_endpoint.c +++ b/api/oc_endpoint.c @@ -675,6 +675,18 @@ oc_endpoint_compare_address(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2) return -1; } +#ifdef OC_TCP +static int +oc_endpoint_compare_session_ids(const oc_endpoint_t *ep1, + const oc_endpoint_t *ep2) +{ + if (ep1->session_id == 0 || ep2->session_id == 0) { + return 0; // session_id == 0 means any + } + return ep1->session_id == ep2->session_id ? 0 : -1; +} +#endif + int oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2) { @@ -690,7 +702,11 @@ oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2) if (ep1->flags & IPV6) { if (memcmp(ep1->addr.ipv6.address, ep2->addr.ipv6.address, 16) == 0 && ep1->addr.ipv6.port == ep2->addr.ipv6.port) { +#ifdef OC_TCP + return oc_endpoint_compare_session_ids(ep1, ep2); +#else /* OC_TCP */ return 0; +#endif /* !OC_TCP */ } return -1; } @@ -698,11 +714,25 @@ oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2) else if (ep1->flags & IPV4) { if (memcmp(ep1->addr.ipv4.address, ep2->addr.ipv4.address, 4) == 0 && ep1->addr.ipv4.port == ep2->addr.ipv4.port) { +#ifdef OC_TCP + return oc_endpoint_compare_session_ids(ep1, ep2); +#else /* OC_TCP */ return 0; +#endif /* !OC_TCP */ } return -1; } #endif /* OC_IPV4 */ + +#ifdef OC_TCP + else if (ep1->flags & TCP) { + if (memcmp(ep1->addr.ipv6.address, ep2->addr.ipv6.address, 16) == 0 && + ep1->addr.ipv6.port == ep2->addr.ipv6.port) { + return 0; + } + return -1; + } +#endif // TODO: Add support for other endpoint types return -1; } diff --git a/api/oc_tcp.c b/api/oc_tcp.c index 02c6f290ef..dc56566030 100644 --- a/api/oc_tcp.c +++ b/api/oc_tcp.c @@ -24,6 +24,7 @@ #include "oc_endpoint.h" #include "port/oc_connectivity.h" #include "oc_tcp_internal.h" +#include "util/oc_atomic.h" #ifdef OC_SECURITY #include #ifdef OC_OSCORE @@ -31,6 +32,15 @@ #endif /* OC_OSCORE */ #endif /* OC_SECURITY */ +static OC_ATOMIC_UINT32_T g_tcp_session_id = 0; + +uint32_t +oc_tcp_get_new_session_id(void) +{ + uint32_t v = OC_ATOMIC_INCREMENT32(g_tcp_session_id); + return (v == 0) ? OC_ATOMIC_INCREMENT32(g_tcp_session_id) : v; +} + #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT #include "port/oc_allocator_internal.h" diff --git a/api/oc_tcp_internal.h b/api/oc_tcp_internal.h index 26c05b812b..9248280f26 100644 --- a/api/oc_tcp_internal.h +++ b/api/oc_tcp_internal.h @@ -23,6 +23,7 @@ #ifdef OC_TCP +#include #include "messaging/coap/constants.h" #include "port/oc_connectivity.h" #include "oc_endpoint.h" @@ -34,6 +35,9 @@ extern "C" { #define OC_TCP_DEFAULT_RECEIVE_SIZE \ (COAP_TCP_DEFAULT_HEADER_LEN + COAP_TCP_MAX_EXTENDED_LENGTH_LEN) +/** @brief Get new tcp session ID */ +uint32_t oc_tcp_get_new_session_id(void); + #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT typedef struct oc_tcp_on_connect_event_s diff --git a/include/oc_endpoint.h b/include/oc_endpoint.h index d60dcbd07b..ca25526ec1 100644 --- a/include/oc_endpoint.h +++ b/include/oc_endpoint.h @@ -98,7 +98,7 @@ typedef struct oc_endpoint_t oc_ipv4_addr_t ipv4; ///< ipv4 address oc_le_addr_t bt; ///< blue tooth address } addr, addr_local; - unsigned interface_index; ///< interface index (valid intefaces are >0, 0 + unsigned interface_index; ///< interface index (valid interfaces are >0, 0 ///< means no index or error) uint8_t priority; ///< priority ocf_version_t version; ///< ocf version @@ -106,6 +106,10 @@ typedef struct oc_endpoint_t uint8_t piv[OSCORE_PIV_LEN]; uint8_t piv_len; #endif /* OC_OSCORE */ +#ifdef OC_TCP + uint32_t session_id; ///< session id for pairing tls peer with tcp session - 0 + ///< means any +#endif } oc_endpoint_t; #define oc_make_ipv4_endpoint(__name__, __flags__, __port__, ...) \ diff --git a/port/android/ipadapter.c b/port/android/ipadapter.c index e7d43bfd4c..9a769fef83 100644 --- a/port/android/ipadapter.c +++ b/port/android/ipadapter.c @@ -1630,17 +1630,21 @@ oc_connectivity_shutdown(size_t device) void oc_connectivity_end_session(const oc_endpoint_t *endpoint) { - oc_connectivity_end_session_v1(endpoint, true); + while (oc_connectivity_end_session_v1(endpoint, true, NULL)) { + // no-op + } } bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, + oc_endpoint_t *session_endpoint) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); if (dev) { - return oc_tcp_end_session(dev, endpoint, notify_session_end); + return oc_tcp_end_session(dev, endpoint, notify_session_end, + session_endpoint); } } return false; diff --git a/port/android/tcpadapter.c b/port/android/tcpadapter.c index 97ae5a5092..23cbb3da5c 100644 --- a/port/android/tcpadapter.c +++ b/port/android/tcpadapter.c @@ -235,6 +235,9 @@ add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->endpoint.next = NULL; session->sock = sock; session->csm_state = state; + if (session->endpoint.session_id == 0) { + session->endpoint.session_id = oc_tcp_get_new_session_id(); + } oc_list_add(session_list, session); @@ -463,11 +466,14 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, oc_endpoint_t *session_endpoint) { pthread_mutex_lock(&dev->tcp.mutex); tcp_session_t *session = find_session_by_endpoint(endpoint); if (session) { + if (session_endpoint) { + memcpy(session_endpoint, &session->endpoint, sizeof(oc_endpoint_t)); + } free_tcp_session(session, notify_session_end); } pthread_mutex_unlock(&dev->tcp.mutex); diff --git a/port/android/tcpadapter.h b/port/android/tcpadapter.h index ba30d698d3..a48c870b6c 100644 --- a/port/android/tcpadapter.h +++ b/port/android/tcpadapter.h @@ -40,7 +40,8 @@ adapter_receive_state_t oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message); bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, - bool notify_session_end); + bool notify_session_end, + oc_endpoint_t *session_endpoint); #ifdef __cplusplus } diff --git a/port/esp32/adapter/src/ipadapter.c b/port/esp32/adapter/src/ipadapter.c index a9d636ce05..3946e1cd12 100644 --- a/port/esp32/adapter/src/ipadapter.c +++ b/port/esp32/adapter/src/ipadapter.c @@ -1618,17 +1618,21 @@ oc_connectivity_shutdown(size_t device) void oc_connectivity_end_session(const oc_endpoint_t *endpoint) { - oc_connectivity_end_session_v1(endpoint, true); + while (oc_connectivity_end_session_v1(endpoint, true, NULL)) { + // no-op + } } bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, + oc_endpoint_t *session_endpoint) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); if (dev) { - return oc_tcp_end_session(dev, endpoint, notify_session_end); + return oc_tcp_end_session(dev, endpoint, notify_session_end, + session_endpoint); } } return false; diff --git a/port/esp32/adapter/src/tcpadapter.c b/port/esp32/adapter/src/tcpadapter.c index 27fa90d70b..49e8788a07 100644 --- a/port/esp32/adapter/src/tcpadapter.c +++ b/port/esp32/adapter/src/tcpadapter.c @@ -205,6 +205,9 @@ add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->endpoint.next = NULL; session->sock = sock; session->csm_state = state; + if (session->endpoint.session_id == 0) { + session->endpoint.session_id = oc_tcp_get_new_session_id(); + } oc_list_add(session_list, session); @@ -436,11 +439,14 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, oc_endpoint_t *session_endpoint) { pthread_mutex_lock(&dev->tcp.mutex); tcp_session_t *session = find_session_by_endpoint(endpoint); if (session) { + if (session_endpoint) { + memcpy(session_endpoint, &session->endpoint, sizeof(oc_endpoint_t)); + } free_tcp_session(session, notify_session_end); } pthread_mutex_unlock(&dev->tcp.mutex); diff --git a/port/esp32/adapter/src/tcpadapter.h b/port/esp32/adapter/src/tcpadapter.h index ba30d698d3..a48c870b6c 100644 --- a/port/esp32/adapter/src/tcpadapter.h +++ b/port/esp32/adapter/src/tcpadapter.h @@ -40,7 +40,8 @@ adapter_receive_state_t oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message); bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, - bool notify_session_end); + bool notify_session_end, + oc_endpoint_t *session_endpoint); #ifdef __cplusplus } diff --git a/port/linux/ipadapter.c b/port/linux/ipadapter.c index 60ca9290fc..646fdec057 100644 --- a/port/linux/ipadapter.c +++ b/port/linux/ipadapter.c @@ -1579,11 +1579,12 @@ oc_connectivity_shutdown(size_t device) #ifdef OC_TCP bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, + oc_endpoint_t *session_endpoint) { if ((endpoint->flags & TCP) != 0 && oc_get_ip_context_for_device(endpoint->device) != NULL) { - return tcp_end_session(endpoint, notify_session_end); + return tcp_end_session(endpoint, notify_session_end, session_endpoint); } return false; } @@ -1591,6 +1592,8 @@ oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, void oc_connectivity_end_session(const oc_endpoint_t *endpoint) { - oc_connectivity_end_session_v1(endpoint, true); + while (oc_connectivity_end_session_v1(endpoint, true, NULL)) { + // no-op + } } #endif /* OC_TCP */ diff --git a/port/linux/tcpsession.c b/port/linux/tcpsession.c index eeed5c9553..97c7f72c8b 100644 --- a/port/linux/tcpsession.c +++ b/port/linux/tcpsession.c @@ -211,9 +211,24 @@ log_new_session(oc_endpoint_t *endpoint, int sock, bool is_connected) addr = oc_string(ep); } OC_DBG("new TCP session endpoint: %s, endpoint interface: %d, sock: %d, " - "connected: %d", - addr, endpoint->interface_index, sock, (int)is_connected); + "connected: %d, session_id: %u", + addr, endpoint->interface_index, sock, (int)is_connected, + (unsigned)endpoint->session_id); } + +static void +log_free_session(oc_endpoint_t *endpoint, int sock) +{ + oc_string64_t ep; + const char *addr = ""; + if (oc_endpoint_to_string64(endpoint, &ep)) { + addr = oc_string(ep); + } + OC_DBG("free TCP session endpoint: %s, endpoint interface: %d, sock: %d, " + "session_id: %u", + addr, endpoint->interface_index, sock, (unsigned)endpoint->session_id); +} + #endif /* OC_DBG_IS_ENABLED */ static tcp_session_t * @@ -240,6 +255,9 @@ add_new_session_locked(int sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->sock = sock; session->csm_state = state; session->notify_session_end = true; + if (session->endpoint.session_id == 0) { + session->endpoint.session_id = oc_tcp_get_new_session_id(); + } oc_list_add(g_session_list, session); @@ -311,8 +329,9 @@ free_session_locked(tcp_session_t *session, bool signal) signal_network_thread(&session->dev->tcp); } close(session->sock); - - OC_DBG("free TCP session(%p, fd=%d)", (void *)session, session->sock); +#if OC_DBG_IS_ENABLED + log_free_session(&session->endpoint, session->sock); +#endif /* OC_DBG_IS_ENABLED */ oc_memb_free(&g_tcp_session_s, session); } @@ -713,12 +732,16 @@ free_waiting_session_async_locked(tcp_waiting_session_t *ws, #endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */ bool -tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end) +tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, + oc_endpoint_t *session_endpoint) { pthread_mutex_lock(&g_mutex); tcp_session_t *s = find_session_by_endpoint_locked(endpoint); if (s != NULL) { free_session_async_locked(s, notify_session_end); + if (session_endpoint) { + memcpy(session_endpoint, &s->endpoint, sizeof(oc_endpoint_t)); + } pthread_mutex_unlock(&g_mutex); return true; } @@ -727,6 +750,9 @@ tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end) tcp_waiting_session_t *ws = find_waiting_session_by_endpoint_locked(endpoint); if (ws != NULL) { free_waiting_session_async_locked(ws, notify_session_end); + if (session_endpoint) { + memcpy(session_endpoint, &ws->endpoint, sizeof(oc_endpoint_t)); + } pthread_mutex_unlock(&g_mutex); return true; } diff --git a/port/linux/tcpsession.h b/port/linux/tcpsession.h index f5211f60fd..50100b69b0 100644 --- a/port/linux/tcpsession.h +++ b/port/linux/tcpsession.h @@ -105,7 +105,8 @@ adapter_receive_state_t tcp_receive_message(ip_context_t *dev, fd_set *fds, * @brief Schedule the session associated with the endpoint to be stopped and * deallocated (if it exists). */ -bool tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end); +bool tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, + oc_endpoint_t *session_endpoint); /** * @brief Handle data received on the signal pipe. diff --git a/port/oc_connectivity_internal.h b/port/oc_connectivity_internal.h index fd7cbb7528..2f10d3159b 100644 --- a/port/oc_connectivity_internal.h +++ b/port/oc_connectivity_internal.h @@ -104,10 +104,12 @@ void handle_session_event_callback(const oc_endpoint_t *endpoint, * @param endpoint the endpoint to close the session for * @param notify_session_end send the notification about the disconnection * session. + * @param session_endpoint the endpoint of the session with session id * @return bool true if the session will be closed */ bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, - bool notify_session_end); + bool notify_session_end, + oc_endpoint_t *session_endpoint); #ifdef __cplusplus } diff --git a/port/windows/ipadapter.c b/port/windows/ipadapter.c index 0690f73c34..4bb322ef38 100644 --- a/port/windows/ipadapter.c +++ b/port/windows/ipadapter.c @@ -1685,17 +1685,20 @@ oc_connectivity_shutdown(size_t device) void oc_connectivity_end_session(const oc_endpoint_t *endpoint) { - oc_connectivity_end_session_v1(endpoint, true); + while (oc_connectivity_end_session_v1(endpoint, true, NULL)) { + // no-op + } } bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, - bool notify_session_end) + bool notify_session_end, + oc_endpoint_t *session_endpoint) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); if (dev) { - return oc_tcp_end_session(endpoint, notify_session_end); + return oc_tcp_end_session(endpoint, notify_session_end, session_endpoint); } } return false; diff --git a/port/windows/tcpadapter.c b/port/windows/tcpadapter.c index 1e94488407..1af90757db 100644 --- a/port/windows/tcpadapter.c +++ b/port/windows/tcpadapter.c @@ -238,6 +238,9 @@ add_new_session_locked(SOCKET sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->csm_state = state; session->sock_event = sock_event; session->notify_session_end = true; + if (session->endpoint.session_id == 0) { + session->endpoint.session_id = oc_tcp_get_new_session_id(); + } oc_list_add(session_list, session); @@ -318,11 +321,15 @@ find_session_by_endpoint_locked(const oc_endpoint_t *endpoint) } bool -oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end) +oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, + oc_endpoint_t *session_endpoint) { oc_tcp_adapter_mutex_lock(); tcp_session_t *session = find_session_by_endpoint_locked(endpoint); if (session) { + if (session_endpoint) { + memcpy(session_endpoint, &session->endpoint, sizeof(oc_endpoint_t)); + } free_tcp_session_async_locked(session, notify_session_end); } oc_tcp_adapter_mutex_unlock(); diff --git a/port/windows/tcpadapter.h b/port/windows/tcpadapter.h index 74f6962678..7425672224 100644 --- a/port/windows/tcpadapter.h +++ b/port/windows/tcpadapter.h @@ -33,7 +33,8 @@ void oc_tcp_connectivity_shutdown(ip_context_t *dev); int oc_tcp_send_buffer(ip_context_t *dev, oc_message_t *message, const struct sockaddr_storage *receiver); -bool oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end); +bool oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, + oc_endpoint_t *session_endpoint); void oc_tcp_adapter_mutex_init(void); diff --git a/security/oc_tls.c b/security/oc_tls.c index 18a033e949..9c8b7cde4f 100644 --- a/security/oc_tls.c +++ b/security/oc_tls.c @@ -540,7 +540,7 @@ oc_tls_free_peer(oc_tls_peer_t *peer, bool inactivity_cb, bool from_reset, #ifdef OC_TCP if (endpoint.flags & TCP) { - oc_connectivity_end_session_v1(&endpoint, false); + oc_connectivity_end_session_v1(&endpoint, false, NULL); } #endif /* OC_TCP */ if (notify_session_end) { @@ -570,14 +570,29 @@ oc_tls_peer_is_doc(const oc_endpoint_t *endpoint) } void -oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end) +oc_tls_remove_peer(const oc_endpoint_t *orig_endpoint, bool notify_session_end) { - oc_tls_peer_t *peer = oc_tls_get_peer(endpoint); - if (peer != NULL) { + oc_endpoint_t endpoint; + oc_endpoint_copy(&endpoint, orig_endpoint); + oc_tls_peer_t *peer = oc_tls_get_peer(&endpoint); + if (peer == NULL) { + tls_drop_endpoint_events(&endpoint); + return; + } + do { oc_tls_free_peer(peer, false, false, notify_session_end); - } else { - tls_drop_endpoint_events(endpoint); +#ifdef OC_TCP + if ((endpoint.flags & TCP) != 0 || endpoint.session_id != 0) { + break; + } +#endif /* OC_TCP */ + peer = oc_tls_get_peer(&endpoint); + } while (peer != NULL); +#ifdef OC_TCP + if ((endpoint.flags & TCP) == 0 && endpoint.session_id == 0) { + tls_drop_endpoint_events(&endpoint); } +#endif /* OC_TCP */ } static void @@ -2284,11 +2299,19 @@ oc_tls_init_context(void) } static void -tls_close_connection(const oc_endpoint_t *endpoint, bool from_reset) +tls_close_connection(const oc_endpoint_t *orig_endpoint, bool from_reset) { - oc_tls_peer_t *peer = oc_tls_get_peer(endpoint); - if (peer != NULL) { + oc_endpoint_t endpoint; + oc_endpoint_copy(&endpoint, orig_endpoint); + oc_tls_peer_t *peer = oc_tls_get_peer(&endpoint); + while (peer != NULL) { oc_tls_close_peer(peer, from_reset); +#ifdef OC_TCP + if ((endpoint.flags & TCP) != 0 || endpoint.session_id != 0) { + break; + } +#endif /* OC_TCP */ + peer = oc_tls_get_peer(&endpoint); } } @@ -2684,6 +2707,12 @@ oc_tls_init_connection(oc_message_t *message) return; } +#ifdef OC_TCP + if ((peer->endpoint.flags & TCP) != 0 && peer->endpoint.session_id == 0) { + peer->endpoint.session_id = oc_tcp_get_new_session_id(); + } +#endif + #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT if ((peer->endpoint.flags & TCP) != 0) { int state = oc_tcp_connect(&peer->endpoint, oc_tls_on_tcp_connect, NULL); @@ -3054,7 +3083,9 @@ close_all_tls_sessions_for_device_reset(size_t device) while (p != NULL) { oc_tls_peer_t *next = p->next; if (p->endpoint.device == device) { - tls_close_connection(&p->endpoint, true); + oc_endpoint_t endpoint; + oc_endpoint_copy(&endpoint, &p->endpoint); + tls_close_connection(&endpoint, true); } p = next; }