Skip to content

Commit

Permalink
[ip6] simplify Ip6::InsertMplOption() (openthread#10814)
Browse files Browse the repository at this point in the history
This commit refactors the `InsertMplOption()` method for improved
efficiency and readability. Cascading `if`/`else` blocks have been
eliminated by handling the simpler `IsMulticastLargerThanRealmLocal()`
case first. Redundant checks for multicast destination and scope
have been removed, as these conditions are already covered by
`IsRealmLocalMulticast()` and `IsMulticastLargerThanRealmLocal()`.
  • Loading branch information
abtink authored Oct 11, 2024
1 parent ad29f8e commit 8fddb4c
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/core/net/ip6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,60 +231,58 @@ Error Ip6::InsertMplOption(Message &aMessage, Header &aHeader)
{
Error error = kErrorNone;

VerifyOrExit(aHeader.GetDestination().IsMulticast() &&
aHeader.GetDestination().GetScope() >= Address::kRealmLocalScope);

if (aHeader.GetDestination().IsRealmLocalMulticast())
if (aHeader.GetDestination().IsMulticastLargerThanRealmLocal())
{
aMessage.RemoveHeader(sizeof(aHeader));
error = PrepareMulticastToLargerThanRealmLocal(aMessage, aHeader);
ExitNow();
}

if (aHeader.GetNextHeader() == kProtoHopOpts)
{
HopByHopHeader hbh;
uint16_t hbhSize;
MplOption mplOption;
PadOption padOption;
VerifyOrExit(aHeader.GetDestination().IsRealmLocalMulticast());

// Read existing hop-by-hop option header
SuccessOrExit(error = aMessage.Read(0, hbh));
hbhSize = hbh.GetSize();
aMessage.RemoveHeader(sizeof(aHeader));

VerifyOrExit(hbhSize <= aHeader.GetPayloadLength(), error = kErrorParse);
if (aHeader.GetNextHeader() == kProtoHopOpts)
{
HopByHopHeader hbh;
uint16_t hbhSize;
MplOption mplOption;
PadOption padOption;

// Increment hop-by-hop option header length by one which
// increases its total size by 8 bytes.
hbh.SetLength(hbh.GetLength() + 1);
aMessage.Write(0, hbh);
// Read existing hop-by-hop option header
SuccessOrExit(error = aMessage.Read(0, hbh));
hbhSize = hbh.GetSize();

// Make space for MPL Option + padding (8 bytes) at the end
// of hop-by-hop header
SuccessOrExit(error = aMessage.InsertHeader(hbhSize, ExtensionHeader::kLengthUnitSize));
VerifyOrExit(hbhSize <= aHeader.GetPayloadLength(), error = kErrorParse);

// Insert MPL Option
mMpl.InitOption(mplOption, aHeader.GetSource());
aMessage.WriteBytes(hbhSize, &mplOption, mplOption.GetSize());
// Increment hop-by-hop option header length by one which
// increases its total size by 8 bytes.
hbh.SetLength(hbh.GetLength() + 1);
aMessage.Write(0, hbh);

// Insert Pad Option (if needed)
if (padOption.InitToPadHeaderWithSize(mplOption.GetSize()) == kErrorNone)
{
aMessage.WriteBytes(hbhSize + mplOption.GetSize(), &padOption, padOption.GetSize());
}
// Make space for MPL Option + padding (8 bytes) at the end
// of hop-by-hop header
SuccessOrExit(error = aMessage.InsertHeader(hbhSize, ExtensionHeader::kLengthUnitSize));

// Update IPv6 Payload Length
aHeader.SetPayloadLength(aHeader.GetPayloadLength() + ExtensionHeader::kLengthUnitSize);
}
else
// Insert MPL Option
mMpl.InitOption(mplOption, aHeader.GetSource());
aMessage.WriteBytes(hbhSize, &mplOption, mplOption.GetSize());

// Insert Pad Option (if needed)
if (padOption.InitToPadHeaderWithSize(mplOption.GetSize()) == kErrorNone)
{
SuccessOrExit(error = AddMplOption(aMessage, aHeader));
aMessage.WriteBytes(hbhSize + mplOption.GetSize(), &padOption, padOption.GetSize());
}

SuccessOrExit(error = aMessage.Prepend(aHeader));
// Update IPv6 Payload Length
aHeader.SetPayloadLength(aHeader.GetPayloadLength() + ExtensionHeader::kLengthUnitSize);
}
else
{
SuccessOrExit(error = PrepareMulticastToLargerThanRealmLocal(aMessage, aHeader));
SuccessOrExit(error = AddMplOption(aMessage, aHeader));
}

SuccessOrExit(error = aMessage.Prepend(aHeader));

exit:
return error;
}
Expand Down

0 comments on commit 8fddb4c

Please sign in to comment.