From 786bd7f2e93b12ed809df09e91fb02afca2dc48d Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 9 Oct 2023 21:28:09 -0700 Subject: [PATCH] [message] add `IsOrigin{}()` helper methods (#9506) This commit adds helper methods to the `Message` class to check if the message origin matches a specific origin. These methods are used in the code as syntactic sugar to simplify the code. --- src/core/api/coap_api.cpp | 8 ++++---- src/core/api/ip6_api.cpp | 2 +- src/core/api/udp_api.cpp | 4 ++-- src/core/common/message.hpp | 27 +++++++++++++++++++++++++++ src/core/net/ip6.cpp | 33 ++++++++++++++++----------------- 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index b218d29e787..25f3208e775 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -211,7 +211,7 @@ otError otCoapSendRequestBlockWiseWithParameters(otInstance *aIn Error error; const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters); - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); if (aTxParameters != nullptr) { @@ -238,7 +238,7 @@ otError otCoapSendRequestWithParameters(otInstance *aInstance, const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters); - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); if (aTxParameters != nullptr) { @@ -296,7 +296,7 @@ otError otCoapSendResponseBlockWiseWithParameters(otInstance *aI { otError error; - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, @@ -313,7 +313,7 @@ otError otCoapSendResponseWithParameters(otInstance *aInstance, { otError error; - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); error = AsCoreType(aInstance).GetApplicationCoap().SendMessage( AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, nullptr); diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 710001919e1..58f5524144a 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -134,7 +134,7 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage) { otError error; - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); error = AsCoreType(aInstance).Get().SendRaw(AsCoreType(aMessage)); diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index 48dc5845f9c..739fc4c36dc 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -74,7 +74,7 @@ otError otUdpSend(otInstance *aInstance, otUdpSocket *aSocket, otMessage *aMessa { otError error; - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); error = AsCoreType(aInstance).Get().SendTo(AsCoreType(aSocket), AsCoreType(aMessage), AsCoreType(aMessageInfo)); @@ -124,7 +124,7 @@ otError otUdpSendDatagram(otInstance *aInstance, otMessage *aMessage, otMessageI { otError error; - VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); + VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs); return AsCoreType(aInstance).Get().SendDatagram(AsCoreType(aMessage), AsCoreType(aMessageInfo), Ip6::kProtoUdp); diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 5edb3344e1d..66153e240e8 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -1184,6 +1184,33 @@ class Message : public otMessage, public Buffer, public GetProvider */ void SetOrigin(Origin aOrigin) { GetMetadata().mOrigin = aOrigin; } + /** + * Indicates whether or not the message origin is Thread Netif. + * + * @retval TRUE If the message origin is Thread Netif. + * @retval FALSE If the message origin is not Thread Netif. + * + */ + bool IsOriginThreadNetif(void) const { return GetOrigin() == kOriginThreadNetif; } + + /** + * Indicates whether or not the message origin is a trusted source on host. + * + * @retval TRUE If the message origin is a trusted source on host. + * @retval FALSE If the message origin is not a trusted source on host. + * + */ + bool IsOriginHostTrusted(void) const { return GetOrigin() == kOriginHostTrusted; } + + /** + * Indicates whether or not the message origin is an untrusted source on host. + * + * @retval TRUE If the message origin is an untrusted source on host. + * @retval FALSE If the message origin is not an untrusted source on host. + * + */ + bool IsOriginHostUntrusted(void) const { return GetOrigin() == kOriginHostUntrusted; } + /** * Indicates whether or not link security is enabled for the message. * diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index d55713ec6ed..b97995cf834 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -830,7 +830,7 @@ Error Ip6::HandleExtensionHeaders(Message &aMessage, bool &aReceive) { Error error = kErrorNone; - bool isOutbound = (aMessage.GetOrigin() != Message::kOriginThreadNetif); + bool isOutbound = !aMessage.IsOriginThreadNetif(); ExtensionHeader extHeader; while (aReceive || aNextHeader == kProtoHopOpts) @@ -1136,15 +1136,14 @@ Error Ip6::SendRaw(Message &aMessage) Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool aIsReassembled) { - Error error; - MessageInfo messageInfo; - Header header; - bool receive; - bool forwardThread; - bool forwardHost; - bool shouldFreeMessage; - uint8_t nextHeader; - Message::Origin origin = aMessage.GetOrigin(); + Error error; + MessageInfo messageInfo; + Header header; + bool receive; + bool forwardThread; + bool forwardHost; + bool shouldFreeMessage; + uint8_t nextHeader; start: receive = false; @@ -1168,10 +1167,10 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool { // Destination is multicast - forwardThread = (origin != Message::kOriginThreadNetif); + forwardThread = !aMessage.IsOriginThreadNetif(); #if OPENTHREAD_FTD - if ((origin == Message::kOriginThreadNetif) && header.GetDestination().IsMulticastLargerThanRealmLocal() && + if (aMessage.IsOriginThreadNetif() && header.GetDestination().IsMulticastLargerThanRealmLocal() && Get().HasSleepyChildWithAddress(header.GetDestination())) { forwardThread = true; @@ -1180,7 +1179,7 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool forwardHost = header.GetDestination().IsMulticastLargerThanRealmLocal(); - if (((origin == Message::kOriginThreadNetif) || aMessage.GetMulticastLoop()) && + if ((aMessage.IsOriginThreadNetif() || aMessage.GetMulticastLoop()) && Get().IsMulticastSubscribed(header.GetDestination())) { receive = true; @@ -1198,7 +1197,7 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool { receive = true; } - else if ((origin != Message::kOriginThreadNetif) || !header.GetDestination().IsLinkLocal()) + else if (!aMessage.IsOriginThreadNetif() || !header.GetDestination().IsLinkLocal()) { if (header.GetDestination().IsLinkLocal()) { @@ -1261,7 +1260,7 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool { uint8_t hopLimit; - if (origin == Message::kOriginThreadNetif) + if (aMessage.IsOriginThreadNetif()) { VerifyOrExit(Get().IsRouterOrLeader()); header.SetHopLimit(header.GetHopLimit() - 1); @@ -1289,7 +1288,7 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool VerifyOrExit(isAllowedType, error = kErrorDrop); } - if (aMessage.GetOrigin() == Message::kOriginHostUntrusted && nextHeader == kProtoUdp) + if (aMessage.IsOriginHostUntrusted() && (nextHeader == kProtoUdp)) { uint16_t destPort; @@ -1304,7 +1303,7 @@ Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool } #if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - if ((origin == Message::kOriginHostTrusted && !aMessage.IsLoopbackToHostAllowed()) && (nextHeader == kProtoUdp)) + if (aMessage.IsOriginHostTrusted() && !aMessage.IsLoopbackToHostAllowed() && (nextHeader == kProtoUdp)) { uint16_t destPort;