diff --git a/src/core/coap/coap_secure.cpp b/src/core/coap/coap_secure.cpp index e345c8925..48db9ac85 100644 --- a/src/core/coap/coap_secure.cpp +++ b/src/core/coap/coap_secure.cpp @@ -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: diff --git a/src/core/coap/coap_secure.hpp b/src/core/coap/coap_secure.hpp index bb3fc56e9..b8edb9440 100644 --- a/src/core/coap/coap_secure.hpp +++ b/src/core/coap/coap_secure.hpp @@ -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) diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 6661e7196..6f84e27db 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -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().SendMessage(*message, Get().GetPeerAddress())); + SuccessOrExit(error = Get().SendMessage(*message, Get().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().SendMessage(aForwardMessage, Get().GetPeerAddress())); + Get().SendMessage(aForwardMessage, Get().GetMessageInfo())); otLogInfoMeshCoP("Sent to commissioner"); diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index 2569ed91a..9364f1529 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -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().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: diff --git a/src/core/meshcop/dtls.hpp b/src/core/meshcop/dtls.hpp index e5b1c03c6..8aebbc216 100644 --- a/src/core/meshcop/dtls.hpp +++ b/src/core/meshcop/dtls.hpp @@ -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; diff --git a/src/core/net/socket.hpp b/src/core/net/socket.hpp index 869406a36..a47cde413 100644 --- a/src/core/net/socket.hpp +++ b/src/core/net/socket.hpp @@ -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 { 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.