From 72fa647805be4a3679f786153fe5438761fb61b8 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Thu, 18 Oct 2018 05:21:53 +0800 Subject: [PATCH] [coaps] refine definition of CoapSecure::Connect (#3178) --- include/openthread/coap_secure.h | 4 ++-- src/cli/cli_coap_secure.cpp | 32 +++++++++++++------------------- src/core/api/coap_secure_api.cpp | 5 +++-- src/core/coap/coap_secure.cpp | 15 +++++++++++++-- src/core/coap/coap_secure.hpp | 4 ++-- src/core/meshcop/joiner.cpp | 12 ++++++------ src/core/net/socket.hpp | 2 +- 7 files changed, 40 insertions(+), 34 deletions(-) diff --git a/include/openthread/coap_secure.h b/include/openthread/coap_secure.h index bf6f0aefb..6ee6c6a98 100644 --- a/include/openthread/coap_secure.h +++ b/include/openthread/coap_secure.h @@ -188,7 +188,7 @@ otError otCoapSecureSetCaCertificateChain(otInstance * aInstance, * This method initializes DTLS session with a peer. * * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aMessageInfo A pointer to a message info structure. + * @param[in] aSockAddr A pointer to the remote sockaddr. * @param[in] aCallback A pointer to a function that will be called when the DTLS connection * state changes. * @param[in] aContext A pointer to arbitrary context information. @@ -197,7 +197,7 @@ otError otCoapSecureSetCaCertificateChain(otInstance * aInstance, * */ otError otCoapSecureConnect(otInstance * aInstance, - const otMessageInfo * aMessageInfo, + const otSockAddr * aSockAddr, otHandleCoapSecureClientConnect aHandler, void * aContext); diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index 558b04723..de139d2bb 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -35,17 +35,13 @@ #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE -#include +#include +#include #include "cli/cli.hpp" -#include "coap/coap_header.hpp" -#include "coap/coap_secure.hpp" - // header for place your x509 certificate and private key #include "x509_cert_key.hpp" -#include - namespace ot { namespace Cli { @@ -130,11 +126,9 @@ void CoapsSecure::PrintPayload(otMessage *aMessage) const otError CoapsSecure::Process(int argc, char *argv[]) { - otError error = OT_ERROR_NONE; - otIp6Address coapDestinationIp; - otMessageInfo messageInfo; - bool mVerifyPeerCert = true; - long value; + otError error = OT_ERROR_NONE; + bool mVerifyPeerCert = true; + long value; VerifyOrExit(argc > 0, error = OT_ERROR_INVALID_ARGS); @@ -222,22 +216,22 @@ otError CoapsSecure::Process(int argc, char *argv[]) // Destination IPv6 address if (argc > 1) { - // parse ipAddr - SuccessOrExit(error = otIp6AddressFromString(argv[1], &coapDestinationIp)); - memset(&messageInfo, 0, sizeof(messageInfo)); - messageInfo.mPeerAddr = coapDestinationIp; - messageInfo.mPeerPort = OT_DEFAULT_COAP_SECURE_PORT; - messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; + otSockAddr sockaddr; + + memset(&sockaddr, 0, sizeof(sockaddr)); + SuccessOrExit(error = otIp6AddressFromString(argv[1], &sockaddr.mAddress)); + sockaddr.mPort = OT_DEFAULT_COAP_SECURE_PORT; + sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD; // check for port specification if (argc > 2) { error = Interpreter::ParseLong(argv[2], value); SuccessOrExit(error); - messageInfo.mPeerPort = static_cast(value); + sockaddr.mPort = static_cast(value); } - SuccessOrExit(error = otCoapSecureConnect(mInterpreter.mInstance, &messageInfo, + SuccessOrExit(error = otCoapSecureConnect(mInterpreter.mInstance, &sockaddr, &CoapsSecure::HandleClientConnect, this)); mInterpreter.mServer->OutputFormat("Coap Secure connect: "); } diff --git a/src/core/api/coap_secure_api.cpp b/src/core/api/coap_secure_api.cpp index 154deff78..53e14f49d 100644 --- a/src/core/api/coap_secure_api.cpp +++ b/src/core/api/coap_secure_api.cpp @@ -34,6 +34,7 @@ #include "openthread-core-config.h" #include +#include #include "coap/coap_header.hpp" #include "coap/coap_secure.hpp" @@ -152,13 +153,13 @@ void otCoapSecureSetSslAuthMode(otInstance *aInstance, bool aVerifyPeerCertifica } otError otCoapSecureConnect(otInstance * aInstance, - const otMessageInfo * aMessageInfo, + const otSockAddr * aSockAddr, otHandleCoapSecureClientConnect aHandler, void * aContext) { Instance &instance = *static_cast(aInstance); - return instance.GetApplicationCoapSecure().Connect(*static_cast(aMessageInfo), aHandler, + return instance.GetApplicationCoapSecure().Connect(*static_cast(aSockAddr), aHandler, aContext); } diff --git a/src/core/coap/coap_secure.cpp b/src/core/coap/coap_secure.cpp index c21334c8a..25458a197 100644 --- a/src/core/coap/coap_secure.cpp +++ b/src/core/coap/coap_secure.cpp @@ -118,9 +118,20 @@ otError CoapSecure::Stop(void) return CoapBase::Stop(); } -otError CoapSecure::Connect(const Ip6::MessageInfo &aMessageInfo, ConnectedCallback aCallback, void *aContext) +otError CoapSecure::Connect(const Ip6::SockAddr &aSockAddr, ConnectedCallback aCallback, void *aContext) { - mPeerAddress = aMessageInfo; + memcpy(&mPeerAddress.mPeerAddr, &aSockAddr.mAddress, sizeof(mPeerAddress.mPeerAddr)); + mPeerAddress.mPeerPort = aSockAddr.mPort; + + if (aSockAddr.GetAddress().IsLinkLocal() || aSockAddr.GetAddress().IsMulticast()) + { + mPeerAddress.mInterfaceId = aSockAddr.mScopeId; + } + else + { + mPeerAddress.mInterfaceId = 0; + } + mConnectedCallback = aCallback; mConnectedContext = aContext; diff --git a/src/core/coap/coap_secure.hpp b/src/core/coap/coap_secure.hpp index 9b19affa1..ce2f08c5c 100644 --- a/src/core/coap/coap_secure.hpp +++ b/src/core/coap/coap_secure.hpp @@ -125,14 +125,14 @@ public: /** * This method initializes DTLS session with a peer. * - * @param[in] aMessageInfo A pointer to a message info structure. + * @param[in] aSockAddr A reference to the remote sockaddr. * @param[in] aCallback A pointer to a function that will be called once DTLS connection is * established. * * @retval OT_ERROR_NONE Successfully started DTLS connection. * */ - otError Connect(const Ip6::MessageInfo &aMessageInfo, ConnectedCallback aCallback, void *aContext); + otError Connect(const Ip6::SockAddr &aSockAddr, ConnectedCallback aCallback, void *aContext); /** * This method indicates whether or not the DTLS session is active. diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 7b959d8cd..82206c4ac 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -284,7 +284,7 @@ otError Joiner::TryNextJoin() if (joinerRouter->mPriority > 0) { - Ip6::MessageInfo messageInfo; + Ip6::SockAddr sockaddr; joinerRouter->mPriority = 0; @@ -292,12 +292,12 @@ otError Joiner::TryNextJoin() netif.GetMac().SetPanChannel(joinerRouter->mChannel); netif.GetIp6Filter().AddUnsecurePort(OPENTHREAD_CONFIG_JOINER_UDP_PORT); - messageInfo.GetPeerAddr().mFields.m16[0] = HostSwap16(0xfe80); - messageInfo.GetPeerAddr().SetIid(joinerRouter->mExtAddr); - messageInfo.mPeerPort = joinerRouter->mJoinerUdpPort; - messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; + sockaddr.GetAddress().mFields.m16[0] = HostSwap16(0xfe80); + sockaddr.GetAddress().SetIid(joinerRouter->mExtAddr); + sockaddr.mPort = joinerRouter->mJoinerUdpPort; + sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD; - netif.GetCoapSecure().Connect(messageInfo, Joiner::HandleSecureCoapClientConnect, this); + netif.GetCoapSecure().Connect(sockaddr, Joiner::HandleSecureCoapClientConnect, this); mState = OT_JOINER_STATE_CONNECT; error = OT_ERROR_NONE; } diff --git a/src/core/net/socket.hpp b/src/core/net/socket.hpp index 7f365ca6b..bff304796 100644 --- a/src/core/net/socket.hpp +++ b/src/core/net/socket.hpp @@ -193,7 +193,7 @@ public: * This constructor initializes the object. * */ - SockAddr(void) { memset(&mAddress, 0, sizeof(mAddress)), mPort = 0, mScopeId = 0; } + SockAddr(void) { memset(&mAddress, 0, sizeof(*this)); } /** * This method returns a reference to the IPv6 address.