[nat64] use IP headers for Mapping::Matches (#11856)

This change updates the `Mapping::Matches` methods to accept
`Ip4::Headers` and `Ip6::Headers` objects instead of separate IP
address and port arguments.

This simplifies the callers `FindOrAllocateMapping` and `FindMapping`
by encapsulating the conditional logic for port translation within
the `Mapping::Matches` methods. This allows for a single, unified
`FindMatching` method on the active mappings list.
This commit is contained in:
Abtin Keshavarzian
2025-08-26 10:36:11 -07:00
committed by GitHub
parent cc44220c85
commit e0b6bbeccd
2 changed files with 30 additions and 21 deletions
+27 -16
View File
@@ -495,12 +495,7 @@ exit:
Translator::Mapping *Translator::FindOrAllocateMapping(const Ip6::Headers &aIp6Headers)
{
#if OPENTHREAD_CONFIG_NAT64_PORT_TRANSLATION_ENABLE
uint16_t srcPortOrId = GetSourcePortOrIcmp6Id(aIp6Headers);
Mapping *mapping = mActiveMappings.FindMatching(aIp6Headers.GetSourceAddress(), srcPortOrId);
#else
Mapping *mapping = mActiveMappings.FindMatching(aIp6Headers.GetSourceAddress());
#endif
Mapping *mapping = mActiveMappings.FindMatching(aIp6Headers);
// Exit if we found a valid mapping.
VerifyOrExit(mapping == nullptr);
@@ -513,17 +508,13 @@ exit:
Translator::Mapping *Translator::FindMapping(const Ip4::Headers &aIp4Headers)
{
#if OPENTHREAD_CONFIG_NAT64_PORT_TRANSLATION_ENABLE
uint16_t dstPortOrId = GetDestinationPortOrIcmp4Id(aIp4Headers);
Mapping *mapping = mActiveMappings.FindMatching(aIp4Headers.GetDestinationAddress(), dstPortOrId);
#else
Mapping *mapping = mActiveMappings.FindMatching(aIp4Headers.GetDestinationAddress());
#endif
Mapping *mapping = mActiveMappings.FindMatching(aIp4Headers);
if (mapping != nullptr)
{
mapping->Touch(TimerMilli::GetNow(), aIp4Headers.GetIpProto());
}
return mapping;
}
@@ -539,14 +530,34 @@ void Translator::Mapping::Touch(TimeMilli aNow, uint8_t aProtocol)
}
}
bool Translator::Mapping::Matches(const Ip6::Address &aIp6Address, const uint16_t aPort) const
bool Translator::Mapping::Matches(const Ip6::Headers &aIp6Headers) const
{
return ((mIp6Address == aIp6Address) && (mSrcPortOrId == aPort));
bool matches = false;
#if OPENTHREAD_CONFIG_NAT64_PORT_TRANSLATION_ENABLE
VerifyOrExit(mSrcPortOrId == GetSourcePortOrIcmp6Id(aIp6Headers));
#endif
VerifyOrExit(mIp6Address == aIp6Headers.GetSourceAddress());
matches = true;
exit:
return matches;
}
bool Translator::Mapping::Matches(const Ip4::Address &aIp4Address, const uint16_t aPort) const
bool Translator::Mapping::Matches(const Ip4::Headers &aIp4Headers) const
{
return ((mIp4Address == aIp4Address) && (mTranslatedPortOrId == aPort));
bool matches = false;
#if OPENTHREAD_CONFIG_NAT64_PORT_TRANSLATION_ENABLE
VerifyOrExit(mTranslatedPortOrId == GetDestinationPortOrIcmp4Id(aIp4Headers));
#endif
VerifyOrExit(mIp4Address == aIp4Headers.GetDestinationAddress());
matches = true;
exit:
return matches;
}
Error Translator::TranslateIcmp4(Message &aMessage, uint16_t aOriginalId)
+3 -5
View File
@@ -307,12 +307,10 @@ private:
void Touch(TimeMilli aNow, uint8_t aProtocol);
InfoString ToString(void) const;
void CopyTo(AddressMapping &aMapping, TimeMilli aNow) const;
bool Matches(const Ip4::Address &aIp4Address) const { return mIp4Address == aIp4Address; }
bool Matches(const Ip6::Address &aIp6Address) const { return mIp6Address == aIp6Address; }
bool Matches(const uint16_t aPort) const { return mTranslatedPortOrId == aPort; }
bool Matches(const Ip6::Headers &aIp6Headers) const;
bool Matches(const Ip4::Headers &aIp4Headers) const;
bool Matches(const TimeMilli aNow) const { return mExpiry < aNow; }
bool Matches(const Ip6::Address &aIp6Address, const uint16_t aPort) const;
bool Matches(const Ip4::Address &aIp4Address, const uint16_t aPort) const;
bool Matches(const uint16_t aPort) const { return mTranslatedPortOrId == aPort; }
Mapping *mNext;
uint64_t mId;