mirror of
https://github.com/espressif/openthread.git
synced 2026-08-11 13:17:48 +00:00
[dtls] small enhancements (#5220)
This commit contains few small enhancements in `Dtls`: - It renames the `GetPeerAddress()` to `GetMessageInof()` (to indicate that it returns `Ip6::MessageInfo` and not just peer socket address). (it also renames the variable to `mMessageInfo` from `mPeerAddress`). - Declares `Ip6::MessageInfo` to be `Clearable` and uses the `Clear() method instead of `new` operator to clear the `mMessageInfo`. - Uses helper methods to set the peer socket address (remove `memcpy`).
This commit is contained in:
@@ -173,7 +173,7 @@ otError CoapSecure::SendMessage(Message &aMessage, ResponseHandler aHandler, voi
|
||||
|
||||
VerifyOrExit(IsConnected(), error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
error = CoapBase::SendMessage(aMessage, mDtls.GetPeerAddress(), aHandler, aContext);
|
||||
error = CoapBase::SendMessage(aMessage, mDtls.GetMessageInfo(), aHandler, aContext);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -223,7 +223,7 @@ void CoapSecure::HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength)
|
||||
OT_NOOP);
|
||||
SuccessOrExit(message->Append(aBuf, aLength));
|
||||
|
||||
CoapBase::Receive(*message, mDtls.GetPeerAddress());
|
||||
CoapBase::Receive(*message, mDtls.GetMessageInfo());
|
||||
|
||||
exit:
|
||||
|
||||
|
||||
@@ -313,7 +313,7 @@ public:
|
||||
* @return DTLS session's message info.
|
||||
*
|
||||
*/
|
||||
const Ip6::MessageInfo &GetPeerAddress(void) const { return mDtls.GetPeerAddress(); }
|
||||
const Ip6::MessageInfo &GetMessageInfo(void) const { return mDtls.GetMessageInfo(); }
|
||||
|
||||
private:
|
||||
static otError Send(CoapBase &aCoapBase, ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
|
||||
|
||||
@@ -169,7 +169,7 @@ static void SendErrorMessage(Coap::CoapSecure &aCoapSecure, ForwardContext &aFor
|
||||
|
||||
VerifyOrExit((message = NewMeshCoPMessage(aCoapSecure)) != nullptr, error = OT_ERROR_NO_BUFS);
|
||||
SuccessOrExit(error = aForwardContext.ToHeader(*message, CoapCodeFromError(aError)));
|
||||
SuccessOrExit(error = aCoapSecure.SendMessage(*message, aCoapSecure.GetPeerAddress()));
|
||||
SuccessOrExit(error = aCoapSecure.SendMessage(*message, aCoapSecure.GetMessageInfo()));
|
||||
|
||||
exit:
|
||||
if (error != OT_ERROR_NONE)
|
||||
@@ -209,7 +209,7 @@ static void SendErrorMessage(Coap::CoapSecure & aCoapSecure,
|
||||
|
||||
SuccessOrExit(error = message->SetToken(aRequest.GetToken(), aRequest.GetTokenLength()));
|
||||
|
||||
SuccessOrExit(error = aCoapSecure.SendMessage(*message, aCoapSecure.GetPeerAddress()));
|
||||
SuccessOrExit(error = aCoapSecure.SendMessage(*message, aCoapSecure.GetMessageInfo()));
|
||||
|
||||
exit:
|
||||
if (error != OT_ERROR_NONE)
|
||||
@@ -463,7 +463,7 @@ bool BorderAgent::HandleUdpReceive(const Message &aMessage, const Ip6::MessageIn
|
||||
SuccessOrExit(error =
|
||||
Tlv::AppendTlv(*message, Tlv::kIPv6Address, &aMessageInfo.GetPeerAddr(), sizeof(Ip6::Address)));
|
||||
|
||||
SuccessOrExit(error = Get<Coap::CoapSecure>().SendMessage(*message, Get<Coap::CoapSecure>().GetPeerAddress()));
|
||||
SuccessOrExit(error = Get<Coap::CoapSecure>().SendMessage(*message, Get<Coap::CoapSecure>().GetMessageInfo()));
|
||||
|
||||
otLogInfoMeshCoP("Sent to commissioner on %s", OT_URI_PATH_PROXY_RX);
|
||||
|
||||
@@ -513,7 +513,7 @@ otError BorderAgent::ForwardToCommissioner(Coap::Message &aForwardMessage, const
|
||||
aMessage.CopyTo(aMessage.GetOffset(), offset, aMessage.GetLength() - aMessage.GetOffset(), aForwardMessage);
|
||||
|
||||
SuccessOrExit(error =
|
||||
Get<Coap::CoapSecure>().SendMessage(aForwardMessage, Get<Coap::CoapSecure>().GetPeerAddress()));
|
||||
Get<Coap::CoapSecure>().SendMessage(aForwardMessage, Get<Coap::CoapSecure>().GetMessageInfo()));
|
||||
|
||||
otLogInfoMeshCoP("Sent to commissioner");
|
||||
|
||||
|
||||
+13
-14
@@ -45,7 +45,6 @@
|
||||
#include "common/instance.hpp"
|
||||
#include "common/locator-getters.hpp"
|
||||
#include "common/logging.hpp"
|
||||
#include "common/new.hpp"
|
||||
#include "common/timer.hpp"
|
||||
#include "crypto/mbedtls.hpp"
|
||||
#include "crypto/sha256.hpp"
|
||||
@@ -150,8 +149,8 @@ otError Dtls::Connect(const Ip6::SockAddr &aSockAddr)
|
||||
|
||||
VerifyOrExit(mState == kStateOpen, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
memcpy(&mPeerAddress.mPeerAddr, &aSockAddr.mAddress, sizeof(mPeerAddress.mPeerAddr));
|
||||
mPeerAddress.mPeerPort = aSockAddr.mPort;
|
||||
mMessageInfo.SetPeerAddr(aSockAddr.GetAddress());
|
||||
mMessageInfo.SetPeerPort(aSockAddr.mPort);
|
||||
|
||||
error = Setup(true);
|
||||
|
||||
@@ -180,16 +179,16 @@ void Dtls::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageI
|
||||
sockAddr.mPort = aMessageInfo.GetPeerPort();
|
||||
IgnoreError(mSocket.Connect(sockAddr));
|
||||
|
||||
mPeerAddress.SetPeerAddr(aMessageInfo.GetPeerAddr());
|
||||
mPeerAddress.SetPeerPort(aMessageInfo.GetPeerPort());
|
||||
mPeerAddress.SetIsHostInterface(aMessageInfo.IsHostInterface());
|
||||
mMessageInfo.SetPeerAddr(aMessageInfo.GetPeerAddr());
|
||||
mMessageInfo.SetPeerPort(aMessageInfo.GetPeerPort());
|
||||
mMessageInfo.SetIsHostInterface(aMessageInfo.IsHostInterface());
|
||||
|
||||
if (Get<ThreadNetif>().HasUnicastAddress(aMessageInfo.GetSockAddr()))
|
||||
{
|
||||
mPeerAddress.SetSockAddr(aMessageInfo.GetSockAddr());
|
||||
mMessageInfo.SetSockAddr(aMessageInfo.GetSockAddr());
|
||||
}
|
||||
|
||||
mPeerAddress.SetSockPort(aMessageInfo.GetSockPort());
|
||||
mMessageInfo.SetSockPort(aMessageInfo.GetSockPort());
|
||||
|
||||
SuccessOrExit(Setup(false));
|
||||
break;
|
||||
@@ -197,8 +196,8 @@ void Dtls::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageI
|
||||
|
||||
default:
|
||||
// Once DTLS session is started, communicate only with a peer.
|
||||
VerifyOrExit((mPeerAddress.GetPeerAddr() == aMessageInfo.GetPeerAddr()) &&
|
||||
(mPeerAddress.GetPeerPort() == aMessageInfo.GetPeerPort()),
|
||||
VerifyOrExit((mMessageInfo.GetPeerAddr() == aMessageInfo.GetPeerAddr()) &&
|
||||
(mMessageInfo.GetPeerPort() == aMessageInfo.GetPeerPort()),
|
||||
OT_NOOP);
|
||||
break;
|
||||
}
|
||||
@@ -206,7 +205,7 @@ void Dtls::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageI
|
||||
#ifdef MBEDTLS_SSL_SRV_C
|
||||
if (mState == MeshCoP::Dtls::kStateConnecting)
|
||||
{
|
||||
IgnoreError(SetClientId(mPeerAddress.GetPeerAddr().mFields.m8, sizeof(mPeerAddress.GetPeerAddr().mFields)));
|
||||
IgnoreError(SetClientId(mMessageInfo.GetPeerAddr().mFields.m8, sizeof(mMessageInfo.GetPeerAddr().mFields)));
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -432,7 +431,7 @@ void Dtls::Disconnect(void)
|
||||
mState = kStateCloseNotify;
|
||||
mTimer.Start(kGuardTimeNewConnectionMilli);
|
||||
|
||||
new (&mPeerAddress) Ip6::MessageInfo();
|
||||
mMessageInfo.Clear();
|
||||
IgnoreError(mSocket.Connect(Ip6::SockAddr()));
|
||||
|
||||
FreeMbedtls();
|
||||
@@ -933,11 +932,11 @@ otError Dtls::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, Message::Sub
|
||||
|
||||
if (mTransportCallback)
|
||||
{
|
||||
SuccessOrExit(error = mTransportCallback(mTransportContext, *message, mPeerAddress));
|
||||
SuccessOrExit(error = mTransportCallback(mTransportContext, *message, mMessageInfo));
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = mSocket.SendTo(*message, mPeerAddress));
|
||||
SuccessOrExit(error = mSocket.SendTo(*message, mMessageInfo));
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
@@ -332,7 +332,7 @@ public:
|
||||
* @return DTLS session's message info.
|
||||
*
|
||||
*/
|
||||
const Ip6::MessageInfo &GetPeerAddress(void) const { return mPeerAddress; }
|
||||
const Ip6::MessageInfo &GetMessageInfo(void) const { return mMessageInfo; }
|
||||
|
||||
void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
|
||||
|
||||
@@ -460,7 +460,7 @@ private:
|
||||
ReceiveHandler mReceiveHandler;
|
||||
void * mContext;
|
||||
|
||||
Ip6::MessageInfo mPeerAddress;
|
||||
Ip6::MessageInfo mMessageInfo;
|
||||
Ip6::Udp::Socket mSocket;
|
||||
|
||||
TransportCallback mTransportCallback;
|
||||
|
||||
@@ -53,14 +53,14 @@ namespace Ip6 {
|
||||
* This class implements message information for an IPv6 message.
|
||||
*
|
||||
*/
|
||||
class MessageInfo : public otMessageInfo
|
||||
class MessageInfo : public otMessageInfo, public Clearable<MessageInfo>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This constructor initializes the object.
|
||||
*
|
||||
*/
|
||||
MessageInfo(void) { memset(this, 0, sizeof(*this)); }
|
||||
MessageInfo(void) { Clear(); }
|
||||
|
||||
/**
|
||||
* This method returns a reference to the local socket address.
|
||||
|
||||
Reference in New Issue
Block a user