[coaps] refine definition of CoapSecure::Connect (#3178)

This commit is contained in:
Yakun Xu
2018-10-17 23:21:53 +02:00
committed by Jonathan Hui
parent b0f84aa210
commit 72fa647805
7 changed files with 40 additions and 34 deletions
+2 -2
View File
@@ -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);
+13 -19
View File
@@ -35,17 +35,13 @@
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
#include <ctype.h>
#include <mbedtls/debug.h>
#include <openthread/ip6.h>
#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 <mbedtls/debug.h>
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<uint16_t>(value);
sockaddr.mPort = static_cast<uint16_t>(value);
}
SuccessOrExit(error = otCoapSecureConnect(mInterpreter.mInstance, &messageInfo,
SuccessOrExit(error = otCoapSecureConnect(mInterpreter.mInstance, &sockaddr,
&CoapsSecure::HandleClientConnect, this));
mInterpreter.mServer->OutputFormat("Coap Secure connect: ");
}
+3 -2
View File
@@ -34,6 +34,7 @@
#include "openthread-core-config.h"
#include <openthread/coap_secure.h>
#include <openthread/ip6.h>
#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<Instance *>(aInstance);
return instance.GetApplicationCoapSecure().Connect(*static_cast<const Ip6::MessageInfo *>(aMessageInfo), aHandler,
return instance.GetApplicationCoapSecure().Connect(*static_cast<const Ip6::SockAddr *>(aSockAddr), aHandler,
aContext);
}
+13 -2
View File
@@ -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;
+2 -2
View File
@@ -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.
+6 -6
View File
@@ -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;
}
+1 -1
View File
@@ -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.