Skip to content

Commit

Permalink
[message] add IsOrigin{}() helper methods (openthread#9506)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abtink authored Oct 10, 2023
1 parent 652e3f2 commit 786bd7f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/core/api/coap_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/api/ip6_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ip6::Ip6>().SendRaw(AsCoreType(aMessage));

Expand Down
4 changes: 2 additions & 2 deletions src/core/api/udp_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ip6::Udp>().SendTo(AsCoreType(aSocket), AsCoreType(aMessage),
AsCoreType(aMessageInfo));
Expand Down Expand Up @@ -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<Ip6::Udp>().SendDatagram(AsCoreType(aMessage), AsCoreType(aMessageInfo),
Ip6::kProtoUdp);
Expand Down
27 changes: 27 additions & 0 deletions src/core/common/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,33 @@ class Message : public otMessage, public Buffer, public GetProvider<Message>
*/
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.
*
Expand Down
33 changes: 16 additions & 17 deletions src/core/net/ip6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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<ChildTable>().HasSleepyChildWithAddress(header.GetDestination()))
{
forwardThread = true;
Expand All @@ -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<ThreadNetif>().IsMulticastSubscribed(header.GetDestination()))
{
receive = true;
Expand All @@ -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())
{
Expand Down Expand Up @@ -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<Mle::Mle>().IsRouterOrLeader());
header.SetHopLimit(header.GetHopLimit() - 1);
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down

0 comments on commit 786bd7f

Please sign in to comment.