From ec7715e8041bf530a3b26e8f2b3b0e2897fc9841 Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Tue, 21 May 2024 07:48:52 +0000 Subject: [PATCH] ports: fix TCP client disconnect notifications --- api/cloud/oc_cloud.c | 15 ++------------ api/oc_client_api.c | 22 ++++++++++++++------- messaging/coap/engine.c | 3 ++- messaging/coap/signal.c | 3 ++- port/android/ipadapter.c | 10 +++++++++- port/android/tcpadapter.c | 24 ++++++++++++----------- port/android/tcpadapter.h | 3 ++- port/esp32/adapter/src/ipadapter.c | 10 +++++++++- port/esp32/adapter/src/tcpadapter.c | 24 ++++++++++++----------- port/esp32/adapter/src/tcpadapter.h | 3 ++- port/linux/ipadapter.c | 14 ++++++++++--- port/linux/tcpsession.c | 27 +++++++++++++++++--------- port/linux/tcpsession.h | 2 +- port/oc_connectivity.h | 15 +++++++++++++- port/windows/ipadapter.c | 12 ++++++++++-- port/windows/tcpadapter.c | 15 +++++++++----- port/windows/tcpadapter.h | 2 +- security/oc_tls.c | 7 ++++--- swig/swig_interfaces/oc_connectivity.i | 1 + 19 files changed, 139 insertions(+), 73 deletions(-) diff --git a/api/cloud/oc_cloud.c b/api/cloud/oc_cloud.c index 7db7f44c04..e67f7ce0f3 100644 --- a/api/cloud/oc_cloud.c +++ b/api/cloud/oc_cloud.c @@ -21,6 +21,7 @@ #ifdef OC_CLOUD +#include "api/oc_client_api_internal.h" #include "api/cloud/oc_cloud_context_internal.h" #include "api/cloud/oc_cloud_deregister_internal.h" #include "api/cloud/oc_cloud_internal.h" @@ -132,19 +133,7 @@ void oc_cloud_close_endpoint(const oc_endpoint_t *ep) { OC_CLOUD_DBG("oc_cloud_close_endpoint"); -#ifdef OC_SECURITY - const oc_tls_peer_t *peer = oc_tls_get_peer(ep); - if (peer != NULL) { - OC_CLOUD_DBG("oc_cloud_close_endpoint: oc_tls_close_connection"); - oc_tls_close_connection(ep); - } else -#endif /* OC_SECURITY */ - { -#ifdef OC_TCP - OC_CLOUD_DBG("oc_cloud_close_endpoint: oc_connectivity_end_session"); - oc_connectivity_end_session(ep); -#endif /* OC_TCP */ - } + oc_close_session(ep); } void diff --git a/api/oc_client_api.c b/api/oc_client_api.c index c01aceae71..2e22df902f 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -18,6 +18,16 @@ #include "oc_config.h" +#include "oc_endpoint.h" + +#ifdef OC_TCP +#include "port/oc_connectivity.h" +#endif /* OC_TCP */ + +#ifdef OC_SECURITY +#include "security/oc_tls_internal.h" +#endif /* OC_SECURITY */ + #ifdef OC_CLIENT #include "api/client/oc_client_cb_internal.h" @@ -38,10 +48,6 @@ #include "messaging/coap/signal_internal.h" #endif /* OC_TCP */ -#ifdef OC_SECURITY -#include "security/oc_tls_internal.h" -#endif /* OC_SECURITY */ - #include typedef struct oc_dispatch_context_t @@ -948,6 +954,8 @@ oc_do_ip_discovery_at_endpoint(const char *rt, oc_discovery_handler_t handler, return status; } +#endif /* OC_CLIENT */ + void oc_close_session(const oc_endpoint_t *endpoint) { @@ -957,9 +965,9 @@ oc_close_session(const oc_endpoint_t *endpoint) #endif /* OC_SECURITY */ } else if (endpoint->flags & TCP) { #ifdef OC_TCP - oc_connectivity_end_session(endpoint); + if (oc_connectivity_end_session_v1(endpoint, false)) { + oc_handle_session(endpoint, OC_SESSION_DISCONNECTED); + } #endif /* OC_TCP */ } } - -#endif /* OC_CLIENT */ diff --git a/messaging/coap/engine.c b/messaging/coap/engine.c index aa0352097c..3f398941ac 100644 --- a/messaging/coap/engine.c +++ b/messaging/coap/engine.c @@ -47,6 +47,7 @@ * This file is part of the Contiki operating system. */ +#include "api/oc_client_api_internal.h" #include "api/oc_helpers_internal.h" #include "api/oc_events_internal.h" #include "api/oc_main_internal.h" @@ -1100,7 +1101,7 @@ coap_process_invalid_inbound_message(const coap_packet_t *packet, #endif /* OC_SECURITY */ #ifdef OC_TCP if ((msg->endpoint.flags & TCP) != 0) { - oc_connectivity_end_session(&msg->endpoint); + oc_close_session(&msg->endpoint); return; } #endif /* OC_TCP */ diff --git a/messaging/coap/signal.c b/messaging/coap/signal.c index 7995b7550f..303dbbc240 100644 --- a/messaging/coap/signal.c +++ b/messaging/coap/signal.c @@ -16,6 +16,7 @@ * ****************************************************************************/ +#include "api/oc_client_api_internal.h" #include "api/oc_helpers_internal.h" #include "api/oc_message_internal.h" #include "log_internal.h" @@ -223,7 +224,7 @@ coap_signal_handle_message(const coap_packet_t *packet, if (packet->code == RELEASE_7_04) { // alternative address // hold off - oc_connectivity_end_session(endpoint); + oc_close_session(endpoint); return COAP_SIGNAL_DONE; } diff --git a/port/android/ipadapter.c b/port/android/ipadapter.c index dc228895c4..e7d43bfd4c 100644 --- a/port/android/ipadapter.c +++ b/port/android/ipadapter.c @@ -1629,13 +1629,21 @@ oc_connectivity_shutdown(size_t device) #ifdef OC_TCP void oc_connectivity_end_session(const oc_endpoint_t *endpoint) +{ + oc_connectivity_end_session_v1(endpoint, true); +} + +bool +oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, + bool notify_session_end) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); if (dev) { - oc_tcp_end_session(dev, endpoint); + return oc_tcp_end_session(dev, endpoint, notify_session_end); } } + return false; } #endif /* OC_TCP */ diff --git a/port/android/tcpadapter.c b/port/android/tcpadapter.c index b811f560ff..97ae5a5092 100644 --- a/port/android/tcpadapter.c +++ b/port/android/tcpadapter.c @@ -189,11 +189,11 @@ oc_tcp_add_socks_to_fd_set(ip_context_t *dev) } static void -free_tcp_session(tcp_session_t *session) +free_tcp_session(tcp_session_t *session, bool notify_session_end) { oc_list_remove(session_list, session); - if (!oc_session_events_disconnect_is_ongoing()) { + if (!oc_session_events_disconnect_is_ongoing() && notify_session_end) { oc_session_end_event(&session->endpoint); } @@ -401,13 +401,13 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) if (count < 0) { OC_ERR("recv error! %d", errno); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } else if (count == 0) { OC_DBG("peer closed TCP session\n"); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_NONE); } @@ -428,7 +428,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) oc_tcp_get_total_length_from_message_header(message); if (length_from_header < 0) { OC_ERR("invalid message size in header"); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } @@ -438,7 +438,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) OC_ERR( "total receive length(%zu) is bigger than message buffer size(%zu)", total_length, oc_message_buffer_size(message)); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } OC_DBG("tcp packet total length : %zu bytes.", total_length); @@ -448,7 +448,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) } while (total_length > message->length); if (!oc_tcp_is_valid_message(message)) { - free_tcp_session(session); + free_tcp_session(session, true); return ADAPTER_STATUS_ERROR; } @@ -461,15 +461,17 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) return ret; } -void -oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint) +bool +oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, + bool notify_session_end) { pthread_mutex_lock(&dev->tcp.mutex); tcp_session_t *session = find_session_by_endpoint(endpoint); if (session) { - free_tcp_session(session); + free_tcp_session(session, notify_session_end); } pthread_mutex_unlock(&dev->tcp.mutex); + return session != NULL; } static int @@ -764,7 +766,7 @@ oc_tcp_connectivity_shutdown(ip_context_t *dev) while (session != NULL) { next = session->next; if (session->endpoint.device == dev->device) { - free_tcp_session(session); + free_tcp_session(session, true); } session = next; } diff --git a/port/android/tcpadapter.h b/port/android/tcpadapter.h index 3d400e875d..ba30d698d3 100644 --- a/port/android/tcpadapter.h +++ b/port/android/tcpadapter.h @@ -39,7 +39,8 @@ void oc_tcp_add_socks_to_fd_set(ip_context_t *dev); adapter_receive_state_t oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message); -void oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint); +bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, + bool notify_session_end); #ifdef __cplusplus } diff --git a/port/esp32/adapter/src/ipadapter.c b/port/esp32/adapter/src/ipadapter.c index 1f2dac88fc..a9d636ce05 100644 --- a/port/esp32/adapter/src/ipadapter.c +++ b/port/esp32/adapter/src/ipadapter.c @@ -1617,13 +1617,21 @@ oc_connectivity_shutdown(size_t device) #ifdef OC_TCP void oc_connectivity_end_session(const oc_endpoint_t *endpoint) +{ + oc_connectivity_end_session_v1(endpoint, true); +} + +bool +oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, + bool notify_session_end) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); if (dev) { - oc_tcp_end_session(dev, endpoint); + return oc_tcp_end_session(dev, endpoint, notify_session_end); } } + return false; } #endif /* OC_TCP */ diff --git a/port/esp32/adapter/src/tcpadapter.c b/port/esp32/adapter/src/tcpadapter.c index 45acfc8a67..27fa90d70b 100644 --- a/port/esp32/adapter/src/tcpadapter.c +++ b/port/esp32/adapter/src/tcpadapter.c @@ -159,11 +159,11 @@ oc_tcp_add_socks_to_fd_set(ip_context_t *dev) } static void -free_tcp_session(tcp_session_t *session) +free_tcp_session(tcp_session_t *session, bool notify_session_end) { oc_list_remove(session_list, session); - if (!oc_session_events_disconnect_is_ongoing()) { + if (!oc_session_events_disconnect_is_ongoing() && notify_session_end) { oc_session_end_event(&session->endpoint); } @@ -374,13 +374,13 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) if (count < 0) { OC_ERR("recv error! %d", errno); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } else if (count == 0) { OC_DBG("peer closed TCP session\n"); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_NONE); } @@ -401,7 +401,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) oc_tcp_get_total_length_from_message_header(message); if (length_from_header < 0) { OC_ERR("invalid message size in header"); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } @@ -411,7 +411,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) OC_ERR( "total receive length(%zu) is bigger than message buffer size(%zu)", total_length, oc_message_buffer_size(message)); - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } OC_DBG("tcp packet total length : %zu bytes.", total_length); @@ -421,7 +421,7 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) } while (total_length > message->length); if (!oc_tcp_is_valid_message(message)) { - free_tcp_session(session); + free_tcp_session(session, true); ret_with_code(ADAPTER_STATUS_ERROR); } @@ -434,15 +434,17 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) return ret; } -void -oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint) +bool +oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, + bool notify_session_end) { pthread_mutex_lock(&dev->tcp.mutex); tcp_session_t *session = find_session_by_endpoint(endpoint); if (session) { - free_tcp_session(session); + free_tcp_session(session, notify_session_end); } pthread_mutex_unlock(&dev->tcp.mutex); + return session != NULL; } static int @@ -724,7 +726,7 @@ oc_tcp_connectivity_shutdown(ip_context_t *dev) while (session != NULL) { next = session->next; if (session->endpoint.device == dev->device) { - free_tcp_session(session); + free_tcp_session(session, true); } session = next; } diff --git a/port/esp32/adapter/src/tcpadapter.h b/port/esp32/adapter/src/tcpadapter.h index 3d400e875d..ba30d698d3 100644 --- a/port/esp32/adapter/src/tcpadapter.h +++ b/port/esp32/adapter/src/tcpadapter.h @@ -39,7 +39,8 @@ void oc_tcp_add_socks_to_fd_set(ip_context_t *dev); adapter_receive_state_t oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message); -void oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint); +bool oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, + bool notify_session_end); #ifdef __cplusplus } diff --git a/port/linux/ipadapter.c b/port/linux/ipadapter.c index 7a15a0e6b9..60ca9290fc 100644 --- a/port/linux/ipadapter.c +++ b/port/linux/ipadapter.c @@ -1577,12 +1577,20 @@ oc_connectivity_shutdown(size_t device) } #ifdef OC_TCP -void -oc_connectivity_end_session(const oc_endpoint_t *endpoint) +bool +oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, + bool notify_session_end) { if ((endpoint->flags & TCP) != 0 && oc_get_ip_context_for_device(endpoint->device) != NULL) { - tcp_end_session(endpoint); + return tcp_end_session(endpoint, notify_session_end); } + return false; +} + +void +oc_connectivity_end_session(const oc_endpoint_t *endpoint) +{ + oc_connectivity_end_session_v1(endpoint, true); } #endif /* OC_TCP */ diff --git a/port/linux/tcpsession.c b/port/linux/tcpsession.c index 344583bb94..eeed5c9553 100644 --- a/port/linux/tcpsession.c +++ b/port/linux/tcpsession.c @@ -60,6 +60,7 @@ typedef struct tcp_session_t oc_endpoint_t endpoint; int sock; tcp_csm_state_t csm_state; + bool notify_session_end; } tcp_session_t; static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -95,6 +96,7 @@ typedef struct tcp_waiting_session_t OC_LIST_STRUCT(messages); on_tcp_connect_t on_tcp_connect; void *on_tcp_connect_data; + bool notify_session_end; } tcp_waiting_session_t; OC_LIST(g_waiting_session_list); ///< sessions waiting to open a connection, @@ -237,6 +239,7 @@ add_new_session_locked(int sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->endpoint.next = NULL; session->sock = sock; session->csm_state = state; + session->notify_session_end = true; oc_list_add(g_session_list, session); @@ -297,7 +300,8 @@ free_session_locked(tcp_session_t *session, bool signal) oc_list_remove(g_session_list, session); oc_list_remove(g_free_session_list_async, session); - if (!oc_session_events_disconnect_is_ongoing()) { + if (!oc_session_events_disconnect_is_ongoing() && + session->notify_session_end) { oc_session_end_event(&session->endpoint); } @@ -582,6 +586,7 @@ add_new_waiting_session_locked(int sock, ip_context_t *dev, ws->retry.count = 0; ws->on_tcp_connect = on_tcp_connect; ws->on_tcp_connect_data = on_tcp_connect_data; + ws->notify_session_end = true; #if OC_DBG_IS_ENABLED log_new_session(&ws->endpoint, sock, false); @@ -679,10 +684,11 @@ tcp_connect_locked(ip_context_t *dev, oc_endpoint_t *endpoint, #endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */ static void -free_session_async_locked(tcp_session_t *s) +free_session_async_locked(tcp_session_t *s, bool notify_session_end) { oc_list_remove(g_session_list, s); oc_list_add(g_free_session_list_async, s); + s->notify_session_end = notify_session_end; signal_network_thread(&s->dev->tcp); OC_DBG("signaled network event thread to monitor that the session needs to " @@ -692,10 +698,12 @@ free_session_async_locked(tcp_session_t *s) #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT static void -free_waiting_session_async_locked(tcp_waiting_session_t *ws) +free_waiting_session_async_locked(tcp_waiting_session_t *ws, + bool notify_session_end) { oc_list_remove(g_waiting_session_list, ws); oc_list_add(g_free_waiting_session_list_async, ws); + ws->notify_session_end = notify_session_end; signal_network_thread(&ws->dev->tcp); OC_DBG("signaled network event thread to monitor that the session needs to " @@ -704,27 +712,28 @@ free_waiting_session_async_locked(tcp_waiting_session_t *ws) } #endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */ -void -tcp_end_session(const oc_endpoint_t *endpoint) +bool +tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end) { pthread_mutex_lock(&g_mutex); tcp_session_t *s = find_session_by_endpoint_locked(endpoint); if (s != NULL) { - free_session_async_locked(s); + free_session_async_locked(s, notify_session_end); pthread_mutex_unlock(&g_mutex); - return; + return true; } #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT tcp_waiting_session_t *ws = find_waiting_session_by_endpoint_locked(endpoint); if (ws != NULL) { - free_waiting_session_async_locked(ws); + free_waiting_session_async_locked(ws, notify_session_end); pthread_mutex_unlock(&g_mutex); - return; + return true; } #endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */ pthread_mutex_unlock(&g_mutex); + return false; } #ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT diff --git a/port/linux/tcpsession.h b/port/linux/tcpsession.h index cd4488e794..f5211f60fd 100644 --- a/port/linux/tcpsession.h +++ b/port/linux/tcpsession.h @@ -105,7 +105,7 @@ 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). */ -void tcp_end_session(const oc_endpoint_t *endpoint); +bool tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end); /** * @brief Handle data received on the signal pipe. diff --git a/port/oc_connectivity.h b/port/oc_connectivity.h index 0b6e2b15fa..fa70f82f6e 100644 --- a/port/oc_connectivity.h +++ b/port/oc_connectivity.h @@ -158,12 +158,25 @@ int oc_send_buffer(oc_message_t *message); void oc_send_discovery_request(oc_message_t *message); /** - * @brief end session for the specific endpoint + * @brief end TCP session for the specific endpoint. * * @param endpoint the endpoint to close the session for + * + * @deprecated replaced by oc_connectivity_end_session_v1 in v2.2.5.14 */ void oc_connectivity_end_session(const oc_endpoint_t *endpoint); +/** + * @brief end TCP session for the specific endpoint. + * + * @param endpoint the endpoint to close the session for + * @param notify_session_end send the notification about the disconnection + * session. + * @return bool true if the session will be closed + */ +bool oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, + bool notify_session_end); + #ifdef OC_DNS_LOOKUP /** * @brief dns look up diff --git a/port/windows/ipadapter.c b/port/windows/ipadapter.c index 34589fb061..af46cb6c79 100644 --- a/port/windows/ipadapter.c +++ b/port/windows/ipadapter.c @@ -1684,13 +1684,21 @@ oc_connectivity_shutdown(size_t device) #ifdef OC_TCP void oc_connectivity_end_session(const oc_endpoint_t *endpoint) +{ + oc_connectivity_end_session_v1(endpoint, true); +} + +bool +oc_connectivity_end_session_v1(const oc_endpoint_t *endpoint, + bool notify_session_end) { if (endpoint->flags & TCP) { ip_context_t *dev = get_ip_context_for_device(endpoint->device); - if (dev != NULL) { - oc_tcp_end_session(endpoint); + if (dev) { + return oc_tcp_end_session(dev, endpoint, notify_session_end); } } + return false; } #endif /* OC_TCP */ diff --git a/port/windows/tcpadapter.c b/port/windows/tcpadapter.c index 0eea6b9f6c..881bca5cc2 100644 --- a/port/windows/tcpadapter.c +++ b/port/windows/tcpadapter.c @@ -54,6 +54,7 @@ typedef struct tcp_session SOCKET sock; HANDLE sock_event; tcp_csm_state_t csm_state; + bool notify_session_end; } tcp_session_t; OC_LIST(session_list); @@ -184,7 +185,8 @@ free_tcp_session(tcp_session_t *session) free_tcp_session_locked(session, &endpoint, &sock, &sock_event); WSACloseEvent(sock_event); closesocket(sock); - if (!oc_session_events_disconnect_is_ongoing()) { + if (!oc_session_events_disconnect_is_ongoing() && + session->notify_session_end) { oc_session_end_event(&endpoint); } @@ -192,10 +194,11 @@ free_tcp_session(tcp_session_t *session) } static void -free_tcp_session_async_locked(tcp_session_t *session) +free_tcp_session_async_locked(tcp_session_t *session, bool notify_session_end) { oc_list_remove(session_list, session); oc_list_add(free_session_list_async, session); + session->session_end = notify_session_end; if (!SetEvent(session->dev->tcp.signal_event)) { OC_ERR("could not trigger signal event (%ld)\n", GetLastError()); @@ -234,6 +237,7 @@ add_new_session_locked(SOCKET sock, ip_context_t *dev, oc_endpoint_t *endpoint, session->sock = sock; session->csm_state = state; session->sock_event = sock_event; + session->notify_session_end = true; oc_list_add(session_list, session); @@ -313,15 +317,16 @@ find_session_by_endpoint_locked(const oc_endpoint_t *endpoint) return session; } -void -oc_tcp_end_session(const oc_endpoint_t *endpoint) +bool +oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end) { oc_tcp_adapter_mutex_lock(); tcp_session_t *session = find_session_by_endpoint_locked(endpoint); if (session) { - free_tcp_session_async_locked(session); + free_tcp_session_async_locked(session, notify_session_end); } oc_tcp_adapter_mutex_unlock(); + return session != NULL; } static SOCKET diff --git a/port/windows/tcpadapter.h b/port/windows/tcpadapter.h index 23be79cd5e..74f6962678 100644 --- a/port/windows/tcpadapter.h +++ b/port/windows/tcpadapter.h @@ -33,7 +33,7 @@ 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); -void oc_tcp_end_session(const oc_endpoint_t *endpoint); +bool oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end); void oc_tcp_adapter_mutex_init(void); diff --git a/security/oc_tls.c b/security/oc_tls.c index 92949f3d88..45c610f7c3 100644 --- a/security/oc_tls.c +++ b/security/oc_tls.c @@ -537,12 +537,13 @@ oc_tls_free_peer(oc_tls_peer_t *peer, bool inactivity_cb, bool from_reset) oc_ri_free_client_cbs_by_endpoint_v1(&endpoint, OC_CONNECTION_CLOSED); #endif /* OC_CLIENT */ + bool disconnected = true; #ifdef OC_TCP if (endpoint.flags & TCP) { - oc_connectivity_end_session(&endpoint); - } else + disconnected = oc_connectivity_end_session_v1(&endpoint, false); + } #endif /* OC_TCP */ - { + if (disconnected) { oc_handle_session(&endpoint, OC_SESSION_DISCONNECTED); } } diff --git a/swig/swig_interfaces/oc_connectivity.i b/swig/swig_interfaces/oc_connectivity.i index 91d90e6254..f55bc60a7f 100644 --- a/swig/swig_interfaces/oc_connectivity.i +++ b/swig/swig_interfaces/oc_connectivity.i @@ -58,6 +58,7 @@ void jni_connectivity_shutdown(size_t device) %} %ignore oc_send_discovery_request; %ignore oc_connectivity_end_session; +%ignore oc_connectivity_end_session_v1; %ignore oc_dns_lookup; %ignore oc_connectivity_get_endpoints; %ignore handle_network_interface_event_callback;