From cc37bd1dfb706c96f017baa0cf24dda54c09f297 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 27 Feb 2019 07:41:59 +0800 Subject: [PATCH] [coaps] fix disconnected (#3623) When DTLS is disconnected, the socket should also be disconnected from the previous peer. --- include/openthread/coap_secure.h | 4 +--- src/cli/cli_coap_secure.cpp | 4 ++-- src/core/api/coap_secure_api.cpp | 4 ++-- src/core/coap/coap_secure.cpp | 20 +++++++++++++------- src/core/coap/coap_secure.hpp | 4 +--- src/core/meshcop/border_agent.cpp | 7 ++----- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/include/openthread/coap_secure.h b/include/openthread/coap_secure.h index c73729554..66d734c38 100644 --- a/include/openthread/coap_secure.h +++ b/include/openthread/coap_secure.h @@ -206,10 +206,8 @@ otError otCoapSecureConnect(otInstance * aInstance, * * @param[in] aInstance A pointer to an OpenThread instance. * - * @retval OT_ERROR_NONE Successfully stopped the DTLS connection. - * */ -otError otCoapSecureDisconnect(otInstance *aInstance); +void otCoapSecureDisconnect(otInstance *aInstance); /** * This method indicates whether or not the DTLS session is connected. diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index 1f74de593..e98921b1a 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -263,13 +263,13 @@ otError CoapSecure::Process(int argc, char *argv[]) } else if (strcmp(argv[0], "disconnect") == 0) { - SuccessOrExit(error = otCoapSecureDisconnect(mInterpreter.mInstance)); + otCoapSecureDisconnect(mInterpreter.mInstance); } else if (strcmp(argv[0], "stop") == 0) { if (otCoapSecureIsConnectionActive(mInterpreter.mInstance)) { - error = otCoapSecureDisconnect(mInterpreter.mInstance); + otCoapSecureDisconnect(mInterpreter.mInstance); mShutdownFlag = true; } else diff --git a/src/core/api/coap_secure_api.cpp b/src/core/api/coap_secure_api.cpp index 092a18d0c..8b30cb552 100644 --- a/src/core/api/coap_secure_api.cpp +++ b/src/core/api/coap_secure_api.cpp @@ -163,11 +163,11 @@ otError otCoapSecureConnect(otInstance * aInstance, aContext); } -otError otCoapSecureDisconnect(otInstance *aInstance) +void otCoapSecureDisconnect(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); - return instance.GetApplicationCoapSecure().Disconnect(); + instance.GetApplicationCoapSecure().Disconnect(); } bool otCoapSecureIsConnected(otInstance *aInstance) diff --git a/src/core/coap/coap_secure.cpp b/src/core/coap/coap_secure.cpp index f3d5c8727..fb24a3fc6 100644 --- a/src/core/coap/coap_secure.cpp +++ b/src/core/coap/coap_secure.cpp @@ -146,18 +146,16 @@ bool CoapSecure::IsConnected(void) return GetNetif().GetDtls().GetState() == MeshCoP::Dtls::kStateConnected; } -otError CoapSecure::Disconnect(void) +void CoapSecure::Disconnect(void) { - Ip6::SockAddr sockAddr; - otError error = OT_ERROR_NONE; - GetNetif().GetDtls().Stop(); // Disconnect from previous peer by connecting to any address - SuccessOrExit(error = mSocket.Connect(sockAddr)); + { + otError error = mSocket.Connect(Ip6::SockAddr()); -exit: - return error; + assert(error == OT_ERROR_NONE); + } } MeshCoP::Dtls &CoapSecure::GetDtls(void) @@ -307,6 +305,14 @@ void CoapSecure::HandleDtlsConnected(void *aContext, bool aConnected) void CoapSecure::HandleDtlsConnected(bool aConnected) { + if (!aConnected) + { + // Disconnect from previous peer by connecting to any address + otError error = mSocket.Connect(Ip6::SockAddr()); + + assert(error == OT_ERROR_NONE); + } + if (mConnectedCallback != NULL) { mConnectedCallback(aConnected, mConnectedContext); diff --git a/src/core/coap/coap_secure.hpp b/src/core/coap/coap_secure.hpp index bde27c7e1..84ec4a37e 100644 --- a/src/core/coap/coap_secure.hpp +++ b/src/core/coap/coap_secure.hpp @@ -140,10 +140,8 @@ public: /** * This method stops the DTLS connection. * - * @retval OT_ERROR_NONE Successfully stopped the DTLS connection. - * */ - otError Disconnect(void); + void Disconnect(void); /** * This method returns a reference to the DTLS object. diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 817fa2ed6..e184d5650 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -691,11 +691,8 @@ void BorderAgent::HandleTimeout(void) if (coaps.IsConnected()) { - otError error; - - error = coaps.Disconnect(); - otLogWarnMeshCoP("Reset commissioner session: %s", otThreadErrorToString(error)); - OT_UNUSED_VARIABLE(error); + coaps.Disconnect(); + otLogWarnMeshCoP("Reset commissioner session"); } }