[libc] Add IN6_IS_ADDR_V4COMPAT (#172646)

This patch adds the `IN6_IS_ADDR_V4COMPAT` macro, which checks whether
an address is IPv4-compatible.
This commit is contained in:
Connector Switch
2025-12-18 23:13:54 +08:00
committed by GitHub
parent e071e43589
commit 12e5bd1e30
2 changed files with 17 additions and 0 deletions

View File

@@ -72,6 +72,12 @@
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[10]) == 0xff && \
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[11]) == 0xff)
#define IN6_IS_ADDR_V4COMPAT(a) \
((__LLVM_LIBC_CAST(reinterpret_cast, uint32_t *, a)[0]) == 0 && \
(__LLVM_LIBC_CAST(reinterpret_cast, uint32_t *, a)[1]) == 0 && \
(__LLVM_LIBC_CAST(reinterpret_cast, uint32_t *, a)[2]) == 0 && \
!IN6_IS_ADDR_UNSPECIFIED(a) && !IN6_IS_ADDR_LOOPBACK(a))
#define IN6_IS_ADDR_MC_NODELOCAL(a) \
(IN6_IS_ADDR_MULTICAST(a) && \
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0x1)

View File

@@ -63,4 +63,15 @@ TEST(LlvmLibcNetinetInTest, IN6Macro) {
EXPECT_TRUE(IN6_IS_ADDR_V4MAPPED(buff));
buff[10] = 0;
buff[11] = 0;
for (int i = 12; i < 16; ++i) {
buff[i] ^= 42;
EXPECT_TRUE(IN6_IS_ADDR_V4COMPAT(buff));
buff[i] ^= 42;
}
for (int i = 0; i < 12; ++i) {
buff[i] ^= 42;
EXPECT_FALSE(IN6_IS_ADDR_V4COMPAT(buff));
buff[i] ^= 42;
}
}