From 33d4f08385484a999f3f51a7dff19b1a50d1fd9a Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 29 Aug 2025 12:49:48 -0700 Subject: [PATCH] [nat64] simplify `SendMessage()` using `OwnedPtr` (#11877) This commit simplifies the implementation of `SendMessage()` by changing its parameter from a `Message` reference to an `OwnedPtr`. The `OwnedPtr` now manages the lifetime of the message, ensuring it is always freed, whether the translation and send operation succeed or fail. This change removes the need for a manual tracking flag and an explicit `Free()` call in the error path, resulting in cleaner and more robust code. --- src/core/api/nat64_api.cpp | 2 +- src/core/net/nat64_translator.cpp | 17 ++++------------- src/core/net/nat64_translator.hpp | 7 ++----- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/core/api/nat64_api.cpp b/src/core/api/nat64_api.cpp index 6ceab789e..32f1bf6cf 100644 --- a/src/core/api/nat64_api.cpp +++ b/src/core/api/nat64_api.cpp @@ -58,7 +58,7 @@ otMessage *otIp4NewMessage(otInstance *aInstance, const otMessageSettings *aSett otError otNat64Send(otInstance *aInstance, otMessage *aMessage) { - return AsCoreType(aInstance).Get().SendMessage(AsCoreType(aMessage)); + return AsCoreType(aInstance).Get().SendMessage(OwnedPtr(AsCoreTypePtr(aMessage))); } void otNat64SetReceiveIp4Callback(otInstance *aInstance, otNat64ReceiveIp4Callback aCallback, void *aContext) diff --git a/src/core/net/nat64_translator.cpp b/src/core/net/nat64_translator.cpp index d5b94082c..4e6170c30 100644 --- a/src/core/net/nat64_translator.cpp +++ b/src/core/net/nat64_translator.cpp @@ -90,23 +90,14 @@ Message *Translator::NewIp4Message(const Message::Settings &aSettings) return message; } -Error Translator::SendMessage(Message &aMessage) +Error Translator::SendMessage(OwnedPtr aMessagePtr) { - bool freed = false; - Error error = kErrorDrop; - Result result = TranslateToIp6(aMessage); + Error error; - VerifyOrExit(result == kForward); - - error = Get().SendRaw(OwnedPtr(&aMessage).PassOwnership()); - freed = true; + VerifyOrExit(TranslateToIp6(*aMessagePtr) == kForward, error = kErrorDrop); + error = Get().SendRaw(aMessagePtr.PassOwnership()); exit: - if (!freed) - { - aMessage.Free(); - } - return error; } diff --git a/src/core/net/nat64_translator.hpp b/src/core/net/nat64_translator.hpp index d37f45267..c0f08f697 100644 --- a/src/core/net/nat64_translator.hpp +++ b/src/core/net/nat64_translator.hpp @@ -168,10 +168,7 @@ public: /** * Translates an IPv4 datagram to an IPv6 datagram and sends it via Thread interface. * - * The caller transfers ownership of @p aMessage when making this call. OpenThread will free @p aMessage when - * processing is complete, including when a value other than `kErrorNone` is returned. - * - * @param[in] aMessage A reference to the message. + * @param[in] aMessagePtr An owned pointer to a message (ownership is transferred to the method). * * @retval kErrorNone Successfully processed the message. * @retval kErrorDrop Message was well-formed but not fully processed due to datagram processing rules. @@ -179,7 +176,7 @@ public: * @retval kErrorNoRoute No route to host. * @retval kErrorParse Encountered a malformed header when processing the message. */ - Error SendMessage(Message &aMessage); + Error SendMessage(OwnedPtr aMessagePtr); /** * Allocate a new message buffer for sending an IPv4 message (which will be translated into an IPv6 datagram by