diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index 8ef45c49f..f270508d9 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -139,13 +139,16 @@ typedef struct otSockAddr */ typedef struct otMessageInfo { - otIp6Address mSockAddr; ///< The local IPv6 address. - otIp6Address mPeerAddr; ///< The peer IPv6 address. - uint16_t mSockPort; ///< The local transport-layer port. - uint16_t mPeerPort; ///< The peer transport-layer port. - const void * mLinkInfo; ///< A pointer to link-specific information. - uint8_t mHopLimit; ///< The IPv6 hop limit. - bool mIsHostInterface : 1; ///< TRUE if packets sent/received via host interface, FALSE otherwise. + otIp6Address mSockAddr; ///< The local IPv6 address. + otIp6Address mPeerAddr; ///< The peer IPv6 address. + uint16_t mSockPort; ///< The local transport-layer port. + uint16_t mPeerPort; ///< The peer transport-layer port. + const void * mLinkInfo; ///< A pointer to link-specific information. + uint8_t mHopLimit; ///< The IPv6 Hop Limit value. Only applies if `mAllowZeroHopLimit` is FALSE. + ///< If `0`, IPv6 Hop Limit is default value `OPENTHREAD_CONFIG_IP6_HOP_LIMIT_DEFAULT`. + ///< Otherwise, specifies the IPv6 Hop Limit. + bool mIsHostInterface : 1; ///< TRUE if packets sent/received via host interface, FALSE otherwise. + bool mAllowZeroHopLimit : 1; ///< TRUE to allow IPv6 Hop Limit 0 in `mHopLimit`, FALSE otherwise. } otMessageInfo; /** diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index ed18974eb..09fa915d9 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -460,7 +460,15 @@ otError Ip6::SendDatagram(Message &aMessage, MessageInfo &aMessageInfo, IpProto header.SetDscp(PriorityToDscp(aMessage.GetPriority())); header.SetPayloadLength(payloadLength); header.SetNextHeader(aIpProto); - header.SetHopLimit(aMessageInfo.mHopLimit ? aMessageInfo.mHopLimit : static_cast(kDefaultHopLimit)); + + if (aMessageInfo.GetHopLimit() != 0 || aMessageInfo.ShouldAllowZeroHopLimit()) + { + header.SetHopLimit(aMessageInfo.GetHopLimit()); + } + else + { + header.SetHopLimit(static_cast(kDefaultHopLimit)); + } if (aMessageInfo.GetSockAddr().IsUnspecified() || aMessageInfo.GetSockAddr().IsMulticast()) { diff --git a/src/core/net/socket.hpp b/src/core/net/socket.hpp index f59619d53..bbe0a82b3 100644 --- a/src/core/net/socket.hpp +++ b/src/core/net/socket.hpp @@ -182,6 +182,15 @@ public: */ bool IsHostInterface(void) const { return mIsHostInterface; } + /** + * This method indicates whether or not to apply hop limit 0. + * + * @retval TRUE if applying hop limit 0 when `mHopLimit` field is 0. + * @retval FALSE if applying default `OPENTHREAD_CONFIG_IP6_HOP_LIMIT_DEFAULT` when `mHopLimit` field is 0. + * + */ + bool ShouldAllowZeroHopLimit(void) const { return mAllowZeroHopLimit; } + /** * This method sets whether the peer is via the host interface. *