[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.
This commit is contained in:
Abtin Keshavarzian
2023-10-09 21:28:09 -07:00
committed by GitHub
parent 652e3f20ca
commit 786bd7f2e9
5 changed files with 50 additions and 24 deletions
+4 -4
View File
@@ -211,7 +211,7 @@ otError otCoapSendRequestBlockWiseWithParameters(otInstance *aIn
Error error; Error error;
const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters); 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) if (aTxParameters != nullptr)
{ {
@@ -238,7 +238,7 @@ otError otCoapSendRequestWithParameters(otInstance *aInstance,
const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters); 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) if (aTxParameters != nullptr)
{ {
@@ -296,7 +296,7 @@ otError otCoapSendResponseBlockWiseWithParameters(otInstance *aI
{ {
otError error; 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), error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
Coap::TxParameters::From(aTxParameters), nullptr, Coap::TxParameters::From(aTxParameters), nullptr,
@@ -313,7 +313,7 @@ otError otCoapSendResponseWithParameters(otInstance *aInstance,
{ {
otError error; otError error;
VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
error = AsCoreType(aInstance).GetApplicationCoap().SendMessage( error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(
AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, nullptr); AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, nullptr);
+1 -1
View File
@@ -134,7 +134,7 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
{ {
otError error; otError error;
VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
error = AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage)); error = AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage));
+2 -2
View File
@@ -74,7 +74,7 @@ otError otUdpSend(otInstance *aInstance, otUdpSocket *aSocket, otMessage *aMessa
{ {
otError error; otError error;
VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
error = AsCoreType(aInstance).Get<Ip6::Udp>().SendTo(AsCoreType(aSocket), AsCoreType(aMessage), error = AsCoreType(aInstance).Get<Ip6::Udp>().SendTo(AsCoreType(aSocket), AsCoreType(aMessage),
AsCoreType(aMessageInfo)); AsCoreType(aMessageInfo));
@@ -124,7 +124,7 @@ otError otUdpSendDatagram(otInstance *aInstance, otMessage *aMessage, otMessageI
{ {
otError error; otError error;
VerifyOrExit(AsCoreType(aMessage).GetOrigin() != Message::kOriginThreadNetif, error = kErrorInvalidArgs); VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
return AsCoreType(aInstance).Get<Ip6::Udp>().SendDatagram(AsCoreType(aMessage), AsCoreType(aMessageInfo), return AsCoreType(aInstance).Get<Ip6::Udp>().SendDatagram(AsCoreType(aMessage), AsCoreType(aMessageInfo),
Ip6::kProtoUdp); Ip6::kProtoUdp);
+27
View File
@@ -1184,6 +1184,33 @@ public:
*/ */
void SetOrigin(Origin aOrigin) { GetMetadata().mOrigin = aOrigin; } 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. * Indicates whether or not link security is enabled for the message.
* *
+16 -17
View File
@@ -830,7 +830,7 @@ Error Ip6::HandleExtensionHeaders(Message &aMessage,
bool &aReceive) bool &aReceive)
{ {
Error error = kErrorNone; Error error = kErrorNone;
bool isOutbound = (aMessage.GetOrigin() != Message::kOriginThreadNetif); bool isOutbound = !aMessage.IsOriginThreadNetif();
ExtensionHeader extHeader; ExtensionHeader extHeader;
while (aReceive || aNextHeader == kProtoHopOpts) while (aReceive || aNextHeader == kProtoHopOpts)
@@ -1136,15 +1136,14 @@ exit:
Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool aIsReassembled) Error Ip6::HandleDatagram(Message &aMessage, const void *aLinkMessageInfo, bool aIsReassembled)
{ {
Error error; Error error;
MessageInfo messageInfo; MessageInfo messageInfo;
Header header; Header header;
bool receive; bool receive;
bool forwardThread; bool forwardThread;
bool forwardHost; bool forwardHost;
bool shouldFreeMessage; bool shouldFreeMessage;
uint8_t nextHeader; uint8_t nextHeader;
Message::Origin origin = aMessage.GetOrigin();
start: start:
receive = false; receive = false;
@@ -1168,10 +1167,10 @@ start:
{ {
// Destination is multicast // Destination is multicast
forwardThread = (origin != Message::kOriginThreadNetif); forwardThread = !aMessage.IsOriginThreadNetif();
#if OPENTHREAD_FTD #if OPENTHREAD_FTD
if ((origin == Message::kOriginThreadNetif) && header.GetDestination().IsMulticastLargerThanRealmLocal() && if (aMessage.IsOriginThreadNetif() && header.GetDestination().IsMulticastLargerThanRealmLocal() &&
Get<ChildTable>().HasSleepyChildWithAddress(header.GetDestination())) Get<ChildTable>().HasSleepyChildWithAddress(header.GetDestination()))
{ {
forwardThread = true; forwardThread = true;
@@ -1180,7 +1179,7 @@ start:
forwardHost = header.GetDestination().IsMulticastLargerThanRealmLocal(); forwardHost = header.GetDestination().IsMulticastLargerThanRealmLocal();
if (((origin == Message::kOriginThreadNetif) || aMessage.GetMulticastLoop()) && if ((aMessage.IsOriginThreadNetif() || aMessage.GetMulticastLoop()) &&
Get<ThreadNetif>().IsMulticastSubscribed(header.GetDestination())) Get<ThreadNetif>().IsMulticastSubscribed(header.GetDestination()))
{ {
receive = true; receive = true;
@@ -1198,7 +1197,7 @@ start:
{ {
receive = true; receive = true;
} }
else if ((origin != Message::kOriginThreadNetif) || !header.GetDestination().IsLinkLocal()) else if (!aMessage.IsOriginThreadNetif() || !header.GetDestination().IsLinkLocal())
{ {
if (header.GetDestination().IsLinkLocal()) if (header.GetDestination().IsLinkLocal())
{ {
@@ -1261,7 +1260,7 @@ start:
{ {
uint8_t hopLimit; uint8_t hopLimit;
if (origin == Message::kOriginThreadNetif) if (aMessage.IsOriginThreadNetif())
{ {
VerifyOrExit(Get<Mle::Mle>().IsRouterOrLeader()); VerifyOrExit(Get<Mle::Mle>().IsRouterOrLeader());
header.SetHopLimit(header.GetHopLimit() - 1); header.SetHopLimit(header.GetHopLimit() - 1);
@@ -1289,7 +1288,7 @@ start:
VerifyOrExit(isAllowedType, error = kErrorDrop); VerifyOrExit(isAllowedType, error = kErrorDrop);
} }
if (aMessage.GetOrigin() == Message::kOriginHostUntrusted && nextHeader == kProtoUdp) if (aMessage.IsOriginHostUntrusted() && (nextHeader == kProtoUdp))
{ {
uint16_t destPort; uint16_t destPort;
@@ -1304,7 +1303,7 @@ start:
} }
#if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE #if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
if ((origin == Message::kOriginHostTrusted && !aMessage.IsLoopbackToHostAllowed()) && (nextHeader == kProtoUdp)) if (aMessage.IsOriginHostTrusted() && !aMessage.IsLoopbackToHostAllowed() && (nextHeader == kProtoUdp))
{ {
uint16_t destPort; uint16_t destPort;