Skip to content

Commit 62ee2cf

Browse files
authored
[libc] Add IN6_IS_ADDR_{LINK, SITE}LOCAL (#168207)
This patch introduces two macros in `netinet/in.h`. The redundant tests for macro values in the testcases have been removed.
1 parent b3bc005 commit 62ee2cf

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@
3434
#define INET_ADDRSTRLEN 16
3535
#define INET6_ADDRSTRLEN 46
3636

37+
// The following macros test for special IPv6 addresses. Each macro is of type
38+
// int and takes a single argument of type const struct in6_addr *:
39+
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/netinet_in.h.html
40+
41+
#define IN6_IS_ADDR_LINKLOCAL(a) \
42+
((__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[0]) == 0xfe && \
43+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xc0) == 0x80)
44+
45+
#define IN6_IS_ADDR_SITELOCAL(a) \
46+
((__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[0]) == 0xfe && \
47+
(__LLVM_LIBC_CAST(reinterpret_cast, uint8_t *, a)[1] & 0xc0) == 0xc0)
48+
3749
#endif // LLVM_LIBC_MACROS_NETINET_IN_MACROS_H

libc/test/include/netinet_in_test.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
#include "include/llvm-libc-macros/netinet-in-macros.h"
1010
#include "test/UnitTest/Test.h"
1111

12-
TEST(LlvmLibcNetinetInTest, IPPROTOMacro) {
13-
EXPECT_EQ(IPPROTO_IP, 0);
14-
EXPECT_EQ(IPPROTO_ICMP, 1);
15-
EXPECT_EQ(IPPROTO_TCP, 6);
16-
EXPECT_EQ(IPPROTO_UDP, 17);
17-
EXPECT_EQ(IPPROTO_IPV6, 41);
18-
EXPECT_EQ(IPPROTO_RAW, 255);
19-
}
12+
TEST(LlvmLibcNetinetInTest, IN6Macro) {
13+
char buff[16] = {};
14+
15+
buff[0] = 0xfe;
16+
buff[1] = 0x80;
17+
EXPECT_TRUE(IN6_IS_ADDR_LINKLOCAL(buff));
18+
buff[0] = 0xff;
19+
buff[1] = 0x80;
20+
EXPECT_FALSE(IN6_IS_ADDR_LINKLOCAL(buff));
2021

21-
TEST(LlvmLibcNetinetInTest, IPV6Macro) {
22-
EXPECT_EQ(IPV6_UNICAST_HOPS, 16);
23-
EXPECT_EQ(IPV6_MULTICAST_IF, 17);
24-
EXPECT_EQ(IPV6_MULTICAST_HOPS, 18);
25-
EXPECT_EQ(IPV6_MULTICAST_LOOP, 19);
26-
EXPECT_EQ(IPV6_JOIN_GROUP, 20);
27-
EXPECT_EQ(IPV6_LEAVE_GROUP, 21);
28-
EXPECT_EQ(IPV6_V6ONLY, 26);
22+
buff[0] = 0xfe;
23+
buff[1] = 0xc0;
24+
EXPECT_TRUE(IN6_IS_ADDR_SITELOCAL(buff));
25+
buff[0] = 0xff;
26+
buff[1] = 0x80;
27+
EXPECT_FALSE(IN6_IS_ADDR_SITELOCAL(buff));
2928
}

0 commit comments

Comments
 (0)