Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
superwhd committed Sep 25, 2023
1 parent f0530af commit d317c84
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 77 deletions.
2 changes: 1 addition & 1 deletion include/openthread/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (359)
#define OPENTHREAD_API_VERSION (360)

/**
* @addtogroup api-instance
Expand Down
32 changes: 32 additions & 0 deletions include/openthread/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ typedef enum otMessagePriority
OT_MESSAGE_PRIORITY_HIGH = 2, ///< High priority level.
} otMessagePriority;

/**
* Defines the OpenThread message origins.
*
*/
typedef enum otMessageOrigin
{
OT_MESSAGE_ORIGIN_THREAD_NETIF = 0, ///< Message originates from Thread Netif.
OT_MESSAGE_ORIGIN_HOST_DISALLOW_LOOPBACK =
1, ///< Message originates from host and should not be passed back to host.
OT_MESSAGE_ORIGIN_HOST_ALLOW_LOOPBACK = 2, ///< Message originates from host and can be passed back to host.
OT_MESSAGE_ORIGIN_HOST_UNTRUSTED = 3, ///< Message originates from an untrusted source on host.
} otMessageOrigin;

/**
* Represents a message settings.
*
Expand Down Expand Up @@ -180,6 +193,25 @@ void otMessageSetOffset(otMessage *aMessage, uint16_t aOffset);
*/
bool otMessageIsLinkSecurityEnabled(const otMessage *aMessage);

/**
* Gets the message origin.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @returns The message origin.
*
*/
otMessageOrigin otMessageGetOrigin(const otMessage *aMessage);

/**
* Sets the message origin.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aOrigin The message origin.
*
*/
void otMessageSetOrigin(otMessage *aMessage, otMessageOrigin aOrigin);

/**
* Sets/forces the message to be forwarded using direct transmission.
* Default setting for a new message is `false`.
Expand Down
9 changes: 8 additions & 1 deletion src/core/api/coap_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ using namespace ot;

otMessage *otCoapNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
return AsCoreType(aInstance).GetApplicationCoap().NewMessage(Message::Settings::From(aSettings));
Message *message = AsCoreType(aInstance).GetApplicationCoap().NewMessage(Message::Settings::From(aSettings));

if (message != nullptr)
{
message->SetDefaultHostOrigin();
}

return message;
}

void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode)
Expand Down
23 changes: 18 additions & 5 deletions src/core/api/ip6_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,35 @@ void otIp6SetReceiveFilterEnabled(otInstance *aInstance, bool aEnabled)

otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
{
return AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage),
OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS);
return AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage));
}

otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
return AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(0, Message::Settings::From(aSettings));
Message *message = AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(0, Message::Settings::From(aSettings));

if (message != nullptr)
{
message->SetDefaultHostOrigin();
}

return message;
}

otMessage *otIp6NewMessageFromBuffer(otInstance *aInstance,
const uint8_t *aData,
uint16_t aDataLength,
const otMessageSettings *aSettings)
{
return AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessageFromData(aData, aDataLength,
Message::Settings::From(aSettings));
Message *message = AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessageFromData(aData, aDataLength,
Message::Settings::From(aSettings));

if (message != nullptr)
{
message->SetDefaultHostOrigin();
}

return message;
}

otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort)
Expand Down
7 changes: 7 additions & 0 deletions src/core/api/message_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ void otMessageSetOffset(otMessage *aMessage, uint16_t aOffset) { AsCoreType(aMes

bool otMessageIsLinkSecurityEnabled(const otMessage *aMessage) { return AsCoreType(aMessage).IsLinkSecurityEnabled(); }

otMessageOrigin otMessageGetOrigin(const otMessage *aMessage) { return MapEnum(AsCoreType(aMessage).GetOrigin()); }

void otMessageSetOrigin(otMessage *aMessage, otMessageOrigin aOrigin)
{
AsCoreType(aMessage).SetOrigin(MapEnum(aOrigin));
}

void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled)
{
if (aEnabled)
Expand Down
9 changes: 8 additions & 1 deletion src/core/api/nat64_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ otError otNat64SetIp4Cidr(otInstance *aInstance, const otIp4Cidr *aCidr)

otMessage *otIp4NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
return AsCoreType(aInstance).Get<Nat64::Translator>().NewIp4Message(Message::Settings::From(aSettings));
Message *message = AsCoreType(aInstance).Get<Nat64::Translator>().NewIp4Message(Message::Settings::From(aSettings));

if (message != nullptr)
{
message->SetDefaultHostOrigin();
}

return message;
}

otError otNat64Send(otInstance *aInstance, otMessage *aMessage)
Expand Down
1 change: 1 addition & 0 deletions src/core/common/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ Message *Message::Clone(uint16_t aLength) const
messageCopy->SetOffset(offset);

messageCopy->SetSubType(GetSubType());
messageCopy->SetOrigin(GetOrigin());
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
messageCopy->SetTimeSync(IsTimeSync());
#endif
Expand Down
51 changes: 51 additions & 0 deletions src/core/common/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class Buffer : public otMessageBuffer, public LinkedListEntry<Buffer>
bool mDoNotEvict : 1; // Whether this message may be evicted.
bool mMulticastLoop : 1; // Whether this multicast message may be looped back.
bool mResolvingAddress : 1; // Whether the message is pending an address query resolution.
uint8_t mOrigin : 2; // The origin of the message.
#if OPENTHREAD_CONFIG_MULTI_RADIO
uint8_t mRadioType : 2; // The radio link type the message was received on, or should be sent on.
bool mIsRadioTypeSet : 1; // Whether the radio type is set.
Expand Down Expand Up @@ -349,6 +350,25 @@ class Message : public otMessage, public Buffer, public GetProvider<Message>
kCopyToUse,
};

/**
* Represents an IPv6 message origin.
*
* In case the message is originating from host, it may also indicate whether or not it is allowed to passed back
* the message to the host.
*
*/
enum Origin : uint8_t
{
// Message originates from Thread Netif.
kOriginThreadNetif = OT_MESSAGE_ORIGIN_THREAD_NETIF,
// Message originates from host and should not be passed back to host.
kOriginHostDisallowLoopBack = OT_MESSAGE_ORIGIN_HOST_DISALLOW_LOOPBACK,
// Message originates from host and can be passed back to host.
kOriginHostAllowLoopBack = OT_MESSAGE_ORIGIN_HOST_ALLOW_LOOPBACK,
// Message originates from an untrusted source on host.
kOriginHostUntrusted = OT_MESSAGE_ORIGIN_HOST_UNTRUSTED,
};

/**
* Represents settings used for creating a new message.
*
Expand Down Expand Up @@ -1135,6 +1155,35 @@ class Message : public otMessage, public Buffer, public GetProvider<Message>
*/
void SetResolvingAddress(bool aResolvingAddress) { GetMetadata().mResolvingAddress = aResolvingAddress; }

/**
* Gets the message origin.
*
* @returns An enum representing the origin of the message.
*
*/
Origin GetOrigin(void) const { return static_cast<Origin>(GetMetadata().mOrigin); }

/**
* Sets the message origin.
*
* @param aOrigin An enum representing the origin of the message.
*
*/
void SetOrigin(Origin aOrigin) { GetMetadata().mOrigin = aOrigin; }

/**
* Sets the message origin as the default host origin.
*
* This method sets the message origin as `kOriginHostAllowLoopBack` or `kOriginHostDisallowLoopBack`, depending on
* the value of macro `OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS`.
*
*/
void SetDefaultHostOrigin(void)
{
SetOrigin(OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS ? Message::kOriginHostAllowLoopBack
: Message::kOriginHostDisallowLoopBack);
}

/**
* Indicates whether or not link security is enabled for the message.
*
Expand Down Expand Up @@ -1798,6 +1847,8 @@ DefineCoreType(otMessageSettings, Message::Settings);
DefineCoreType(otMessage, Message);
DefineCoreType(otMessageQueue, MessageQueue);

DefineMapEnum(otMessageOrigin, Message::Origin);

} // namespace ot

#endif // MESSAGE_HPP_
Loading

0 comments on commit d317c84

Please sign in to comment.