[ipv4] smaller improvements in Address, Cidr, and Header (#11863)

This commit introduces several cleanups and smaller improvements to
the IPv4 types.

- Optimizes `Cidr::ToString(StringWriter&)` by writing the address
  directly to the provided writer, avoiding the allocation of a
  temporary `String` object.
- Moves the IPv4 header field offset constants from the public
  `ip4_types.hpp` header into the unit test file, as this was their
  only place of use. This cleans up the `Ip4::Header` public API.
- Replaces hardcoded values for address and string sizes with the
  corresponding `OT_IP4_*` definitions for consistency.
- Corrects the format specifier in `Address::ToString()` from `%d` to
  `%u` to properly print unsigned octet values.
This commit is contained in:
Abtin Keshavarzian
2025-08-26 19:52:33 -07:00
committed by GitHub
parent 7bb1ed0a7f
commit d2d2f0092f
3 changed files with 31 additions and 28 deletions
+21 -10
View File
@@ -47,6 +47,17 @@ void VerifyEcnDscp(const Header &aHeader, uint8_t aDscp, Ecn aEcn)
void TestIp4Header(void)
{
static constexpr uint8_t kHeaderVersionIhlOffset = 0;
static constexpr uint8_t kHeaderTrafficClassOffset = 1;
static constexpr uint8_t kHeaderTotalLengthOffset = 2;
static constexpr uint8_t kHeaderIdentificationOffset = 4;
static constexpr uint8_t kHeaderFlagsFragmentOffset = 6;
static constexpr uint8_t kHeaderTtlOffset = 8;
static constexpr uint8_t kHeaderProtocolOffset = 9;
static constexpr uint8_t kHeaderHeaderChecksumOffset = 10;
static constexpr uint8_t kHeaderSourceAddressOffset = 12;
static constexpr uint8_t kHeaderDestinationAddressOffset = 16;
static constexpr uint16_t kTotalLength = 84;
static constexpr uint8_t kTtl = 64;
@@ -89,13 +100,13 @@ void TestIp4Header(void)
// Verify the offsets to different fields.
VerifyOrQuit(BigEndian::ReadUint16(headerBytes + Header::kTotalLengthOffset) == kTotalLength,
VerifyOrQuit(BigEndian::ReadUint16(headerBytes + kHeaderTotalLengthOffset) == kTotalLength,
"kTotalLength is incorrect");
VerifyOrQuit(headerBytes[Header::kProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
VerifyOrQuit(headerBytes[Header::kTtlOffset] == kTtl, "kTtl is incorrect");
VerifyOrQuit(memcmp(&headerBytes[Header::kSourceAddressOffset], &source, sizeof(source)) == 0,
VerifyOrQuit(headerBytes[kHeaderProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
VerifyOrQuit(headerBytes[kHeaderTtlOffset] == kTtl, "kTtl is incorrect");
VerifyOrQuit(memcmp(&headerBytes[kHeaderSourceAddressOffset], &source, sizeof(source)) == 0,
"Source address is incorrect");
VerifyOrQuit(memcmp(&headerBytes[Header::kDestinationAddressOffset], &destination, sizeof(destination)) == 0,
VerifyOrQuit(memcmp(&headerBytes[kHeaderDestinationAddressOffset], &destination, sizeof(destination)) == 0,
"Destination address is incorrect");
for (uint8_t dscp : kDscps)
@@ -111,14 +122,14 @@ void TestIp4Header(void)
memcpy(&header, kExampleIp4Header, sizeof(header));
VerifyOrQuit(header.IsValid());
VerifyOrQuit(memcmp(&headerBytes[Header::kSourceAddressOffset], &source, sizeof(source)) == 0,
VerifyOrQuit(memcmp(&headerBytes[kHeaderSourceAddressOffset], &source, sizeof(source)) == 0,
"Source address is incorrect");
VerifyOrQuit(memcmp(&headerBytes[Header::kDestinationAddressOffset], &destination, sizeof(destination)) == 0,
VerifyOrQuit(memcmp(&headerBytes[kHeaderDestinationAddressOffset], &destination, sizeof(destination)) == 0,
"Destination address is incorrect");
VerifyOrQuit(BigEndian::ReadUint16(headerBytes + Header::kTotalLengthOffset) == kTotalLength,
VerifyOrQuit(BigEndian::ReadUint16(headerBytes + kHeaderTotalLengthOffset) == kTotalLength,
"kTotalLength is incorrect");
VerifyOrQuit(headerBytes[Header::kProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
VerifyOrQuit(headerBytes[Header::kTtlOffset] == kTtl, "kTtl is incorrect");
VerifyOrQuit(headerBytes[kHeaderProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
VerifyOrQuit(headerBytes[kHeaderTtlOffset] == kTtl, "kTtl is incorrect");
}
} // namespace Ip4