From 7011a12e9a9471796e0b562b5a3fa80431fdb27e Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 19 Oct 2020 10:15:12 -0700 Subject: [PATCH] [coap] add helper method 'Message::SetTokenFromMessage()' (#5661) This commit adds a new helper method to set the token on a CoAP `Message` by copying it from another `Message`. It also renames the method which generates a random token of a given length to `GenerateRandomToken()`. --- src/core/api/coap_api.cpp | 4 ++-- src/core/coap/coap.cpp | 2 +- src/core/coap/coap_message.cpp | 11 ++++++++--- src/core/coap/coap_message.hpp | 13 ++++++++++++- src/core/meshcop/border_agent.cpp | 2 +- src/core/thread/mlr_manager.cpp | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index 2d795070f..721e4cd95 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -63,7 +63,7 @@ otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aReques response.Init(static_cast(aType), static_cast(aCode)); response.SetMessageId(request.GetMessageId()); - return response.SetToken(request.GetToken(), request.GetTokenLength()); + return response.SetTokenFromMessage(request); } otError otCoapMessageSetToken(otMessage *aMessage, const uint8_t *aToken, uint8_t aTokenLength) @@ -73,7 +73,7 @@ otError otCoapMessageSetToken(otMessage *aMessage, const uint8_t *aToken, uint8_ void otCoapMessageGenerateToken(otMessage *aMessage, uint8_t aTokenLength) { - IgnoreError(static_cast(aMessage)->SetToken(aTokenLength)); + IgnoreError(static_cast(aMessage)->GenerateRandomToken(aTokenLength)); } otError otCoapMessageAppendContentFormatOption(otMessage *aMessage, otCoapOptionContentFormat aContentFormat) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 3bb4a324a..1ea33864f 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -320,7 +320,7 @@ otError CoapBase::SendHeaderResponse(Message::Code aCode, const Message &aReques OT_UNREACHABLE_CODE(break); } - SuccessOrExit(error = message->SetToken(aRequest.GetToken(), aRequest.GetTokenLength())); + SuccessOrExit(error = message->SetTokenFromMessage(aRequest)); SuccessOrExit(error = SendMessage(*message, aMessageInfo)); diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index ef8dfc6d8..5ca789ea0 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -65,7 +65,7 @@ otError Message::Init(Type aType, Code aCode, const char *aUriPath) otError error; Init(aType, aCode); - SuccessOrExit(error = SetToken(kDefaultTokenLength)); + SuccessOrExit(error = GenerateRandomToken(kDefaultTokenLength)); SuccessOrExit(error = AppendUriPathOptions(aUriPath)); exit: @@ -333,7 +333,7 @@ otError Message::SetToken(const uint8_t *aToken, uint8_t aTokenLength) return SetLength(GetHelpData().mHeaderLength); } -otError Message::SetToken(uint8_t aTokenLength) +otError Message::GenerateRandomToken(uint8_t aTokenLength) { uint8_t token[kMaxTokenLength]; @@ -344,6 +344,11 @@ otError Message::SetToken(uint8_t aTokenLength) return SetToken(token, aTokenLength); } +otError Message::SetTokenFromMessage(const Message &aMessage) +{ + return SetToken(aMessage.GetToken(), aMessage.GetTokenLength()); +} + bool Message::IsTokenEqual(const Message &aMessage) const { uint8_t tokenLength = GetTokenLength(); @@ -357,7 +362,7 @@ otError Message::SetDefaultResponseHeader(const Message &aRequest) SetMessageId(aRequest.GetMessageId()); - return SetToken(aRequest.GetToken(), aRequest.GetTokenLength()); + return SetTokenFromMessage(aRequest); } Message *Message::Clone(uint16_t aLength) const diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index 445941ace..bf155e10a 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -386,6 +386,17 @@ public: */ otError SetToken(const uint8_t *aToken, uint8_t aTokenLength); + /** + * This method sets the Token value and length by copying it from another given message. + * + * @param[in] aMessage The message to copy the Token from. + * + * @retval OT_ERROR_NONE Successfully set the token value. + * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to set the token value. + * + */ + otError SetTokenFromMessage(const Message &aMessage); + /** * This method sets the Token length and randomizes its value. * @@ -395,7 +406,7 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to set the token value. * */ - otError SetToken(uint8_t aTokenLength); + otError GenerateRandomToken(uint8_t aTokenLength); /** * This method checks if Tokens in two CoAP headers are equal. diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 7d5dbf7b1..af632fa5e 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -202,7 +202,7 @@ static void SendErrorMessage(Coap::CoapSecure & aCoapSecure, message->SetMessageId(aRequest.GetMessageId()); } - SuccessOrExit(error = message->SetToken(aRequest.GetToken(), aRequest.GetTokenLength())); + SuccessOrExit(error = message->SetTokenFromMessage(aRequest)); SuccessOrExit(error = aCoapSecure.SendMessage(*message, aCoapSecure.GetMessageInfo())); diff --git a/src/core/thread/mlr_manager.cpp b/src/core/thread/mlr_manager.cpp index 7c64ddf19..5508a7a77 100644 --- a/src/core/thread/mlr_manager.cpp +++ b/src/core/thread/mlr_manager.cpp @@ -395,7 +395,7 @@ otError MlrManager::SendMulticastListenerRegistrationMessage(const otIp6Address VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); message->InitAsConfirmablePost(); - SuccessOrExit(message->SetToken(Coap::Message::kDefaultTokenLength)); + SuccessOrExit(message->GenerateRandomToken(Coap::Message::kDefaultTokenLength)); SuccessOrExit(message->AppendUriPathOptions(UriPath::kMlr)); SuccessOrExit(message->SetPayloadMarker());