Skip to content

Commit 12d4889

Browse files
authored
[libc] Add IN6_IS_ADDR_MC* (#172643)
This patch adds the `IN6_IS_ADDR_MC*` macro, which checks whether an address is multicast node-local address, multicast link-local address, multicast site-local address, multicast organization-local address and multicast global address.
1 parent a256c03 commit 12d4889

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

libc/include/llvm-libc-macros/netinet-in-macros.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@
6464
((__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[0]) == 0xfe && \
6565
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xc0) == 0xc0)
6666

67+
#define IN6_IS_ADDR_MC_NODELOCAL(a) \
68+
(IN6_IS_ADDR_MULTICAST(a) && \
69+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0x1)
70+
71+
#define IN6_IS_ADDR_MC_LINKLOCAL(a) \
72+
(IN6_IS_ADDR_MULTICAST(a) && \
73+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0x2)
74+
75+
#define IN6_IS_ADDR_MC_SITELOCAL(a) \
76+
(IN6_IS_ADDR_MULTICAST(a) && \
77+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0x5)
78+
79+
#define IN6_IS_ADDR_MC_ORGLOCAL(a) \
80+
(IN6_IS_ADDR_MULTICAST(a) && \
81+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0x8)
82+
83+
#define IN6_IS_ADDR_MC_GLOBAL(a) \
84+
(IN6_IS_ADDR_MULTICAST(a) && \
85+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xf) == 0xe)
86+
6787
#endif // LLVM_LIBC_MACROS_NETINET_IN_MACROS_H

libc/test/include/netinet_in_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,18 @@ TEST(LlvmLibcNetinetInTest, IN6Macro) {
4242
buff[0] = 0xff;
4343
buff[1] = 0x80;
4444
EXPECT_FALSE(IN6_IS_ADDR_SITELOCAL(buff));
45+
46+
buff[0] = 0xff;
47+
buff[1] = 0x1;
48+
EXPECT_TRUE(IN6_IS_ADDR_MC_NODELOCAL(buff));
49+
buff[1] = 0x2;
50+
EXPECT_TRUE(IN6_IS_ADDR_MC_LINKLOCAL(buff));
51+
buff[1] = 0x5;
52+
EXPECT_TRUE(IN6_IS_ADDR_MC_SITELOCAL(buff));
53+
buff[1] = 0x8;
54+
EXPECT_TRUE(IN6_IS_ADDR_MC_ORGLOCAL(buff));
55+
buff[1] = 0xe;
56+
EXPECT_TRUE(IN6_IS_ADDR_MC_GLOBAL(buff));
57+
buff[1] = 0;
58+
buff[0] = 0;
4559
}

0 commit comments

Comments
 (0)