[libc-commits] [libc] 05d152b - [libc] Add definition of struct in6_addr to netinet/in.h (#201057)
via libc-commits
libc-commits at lists.llvm.org
Tue Jun 2 10:49:08 PDT 2026
Author: Pavel Labath
Date: 2026-06-02T17:49:02Z
New Revision: 05d152bd59372aa3e10bbf06deae5795a81052d9
URL: https://github.com/llvm/llvm-project/commit/05d152bd59372aa3e10bbf06deae5795a81052d9
DIFF: https://github.com/llvm/llvm-project/commit/05d152bd59372aa3e10bbf06deae5795a81052d9.diff
LOG: [libc] Add definition of struct in6_addr to netinet/in.h (#201057)
This patch implements the struct in6_addr definition for netinet/in.h,
mapping it from netinet/in.yaml to the generated public header.
I've defined struct in6_addr under include/llvm-libc-types/ using an
anonymous union containing s6_addr, s6_addr16, and s6_addr32 (only the
first one is mandated by POSIX). Other implementations achieve this by
using a named union and defining the members as macros. This approach is
cleaner, but could potentially run into compatibility problems with code
which expects s6_addr is a macro. If we do, we may have to re-evaluate
this approach, but I'd like to avoid uglyfying this preemptively.
I added a simple test to check the memory layout of the address.
Assisted by Gemini.
Added:
libc/hdr/types/struct_in6_addr.h
libc/include/llvm-libc-types/struct_in6_addr.h
libc/test/src/netinet/in_test.cpp
Modified:
libc/hdr/types/CMakeLists.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-types/CMakeLists.txt
libc/include/netinet/in.yaml
libc/test/src/netinet/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index d8fdadcf9d1d0..47d47cc884e1b 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -438,6 +438,15 @@ add_proxy_header_library(
libc.include.arpa_inet
)
+add_proxy_header_library(
+ struct_in6_addr
+ HDRS
+ struct_in6_addr.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_in6_addr
+ libc.include.netinet_in
+)
+
add_proxy_header_library(
in_addr_t
HDRS
diff --git a/libc/hdr/types/struct_in6_addr.h b/libc/hdr/types/struct_in6_addr.h
new file mode 100644
index 0000000000000..65fa009935ec9
--- /dev/null
+++ b/libc/hdr/types/struct_in6_addr.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for struct in6_addr.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_IN6_ADDR_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_IN6_ADDR_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_in6_addr.h"
+
+#else
+
+#include <netinet/in.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_IN6_ADDR_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index a83c6bfc79537..e97859a782e91 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -222,6 +222,7 @@ add_header_macro(
.llvm-libc-types.sa_family_t
.llvm-libc-types.struct_sockaddr_in
.llvm-libc-types.struct_in_addr
+ .llvm-libc-types.struct_in6_addr
.llvm_libc_common_h
)
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index c1b35fce166a7..5586eff88ef6d 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -59,6 +59,7 @@ add_header(imaxdiv_t HDR imaxdiv_t.h)
add_header(in_addr_t HDR in_addr_t.h)
add_header(in_port_t HDR in_port_t.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(struct_in_addr HDR struct_in_addr.h DEPENDS .in_addr_t)
+add_header(struct_in6_addr HDR struct_in6_addr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(ino_t HDR ino_t.h)
add_header(key_t HDR key_t.h)
add_header(mbstate_t HDR mbstate_t.h)
diff --git a/libc/include/llvm-libc-types/struct_in6_addr.h b/libc/include/llvm-libc-types/struct_in6_addr.h
new file mode 100644
index 0000000000000..58e9b6a1009d4
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_in6_addr.h
@@ -0,0 +1,22 @@
+//===-- Definition of struct in6_addr -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_IN6_ADDR_H
+#define LLVM_LIBC_TYPES_STRUCT_IN6_ADDR_H
+
+#include "../llvm-libc-macros/stdint-macros.h"
+
+struct in6_addr {
+ __extension__ union {
+ uint8_t s6_addr[16];
+ uint16_t s6_addr16[8];
+ uint32_t s6_addr32[4];
+ };
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_IN6_ADDR_H
diff --git a/libc/include/netinet/in.yaml b/libc/include/netinet/in.yaml
index 2354e656c4bfc..9c2ef92590f9c 100644
--- a/libc/include/netinet/in.yaml
+++ b/libc/include/netinet/in.yaml
@@ -20,6 +20,7 @@ types:
- type_name: sa_family_t
- type_name: struct_sockaddr_in
- type_name: struct_in_addr
+ - type_name: struct_in6_addr
enums: []
objects: []
functions: []
diff --git a/libc/test/src/netinet/CMakeLists.txt b/libc/test/src/netinet/CMakeLists.txt
index f8e90b37288ab..36d37f2dbdf72 100644
--- a/libc/test/src/netinet/CMakeLists.txt
+++ b/libc/test/src/netinet/CMakeLists.txt
@@ -1,5 +1,18 @@
add_custom_target(libc_netinet_unittests)
+add_libc_unittest(
+ in_test
+ SUITE
+ libc_netinet_unittests
+ SRCS
+ in_test.cpp
+ DEPENDS
+ libc.hdr.types.struct_in6_addr
+ libc.src.arpa.inet.htons
+ libc.src.arpa.inet.htonl
+ libc.src.string.memcmp
+)
+
add_libc_unittest(
udp_test
SUITE
diff --git a/libc/test/src/netinet/in_test.cpp b/libc/test/src/netinet/in_test.cpp
new file mode 100644
index 0000000000000..ee3a4af0fa53f
--- /dev/null
+++ b/libc/test/src/netinet/in_test.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for netinet/in.h
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/arpa/inet/htonl.h"
+#include "src/arpa/inet/htons.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/Test.h"
+
+#include "hdr/types/struct_in6_addr.h"
+
+TEST(LlvmLibcNetinetInTest, In6AddrLayout) {
+ EXPECT_EQ(sizeof(struct in6_addr), size_t(16));
+
+ struct in6_addr addr = {};
+
+ // Using parenthesis to suppress htons-as-a-macro in overlay mode.
+ addr.s6_addr16[0] = (LIBC_NAMESPACE::htons)(0x1122);
+ addr.s6_addr16[1] = (LIBC_NAMESPACE::htons)(0x3344);
+ addr.s6_addr16[2] = (LIBC_NAMESPACE::htons)(0x5566);
+ addr.s6_addr16[3] = (LIBC_NAMESPACE::htons)(0x7788);
+ addr.s6_addr16[4] = (LIBC_NAMESPACE::htons)(0x99aa);
+ addr.s6_addr16[5] = (LIBC_NAMESPACE::htons)(0xbbcc);
+ addr.s6_addr16[6] = (LIBC_NAMESPACE::htons)(0xddee);
+ addr.s6_addr16[7] = (LIBC_NAMESPACE::htons)(0xff00);
+
+ EXPECT_EQ(
+ LIBC_NAMESPACE::memcmp(
+ &addr,
+ "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x00",
+ 16),
+ 0);
+
+ addr.s6_addr32[0] = (LIBC_NAMESPACE::htonl)(0x12345678);
+ addr.s6_addr32[1] = (LIBC_NAMESPACE::htonl)(0x9abcdef0);
+ addr.s6_addr32[2] = (LIBC_NAMESPACE::htonl)(0x12345678);
+ addr.s6_addr32[3] = (LIBC_NAMESPACE::htonl)(0x9abcdef0);
+
+ EXPECT_EQ(
+ LIBC_NAMESPACE::memcmp(
+ &addr,
+ "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56\x78\x9a\xbc\xde\xf0",
+ 16),
+ 0);
+}
More information about the libc-commits
mailing list