[libc-commits] [libc] [libc] Add <netinet/ip.h> containing struct iphdr (PR #243768)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 01:29:07 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
This patch adds a generated <netinet/ip.h> containing the linux-style iphdr structure definition, its proxy header, and a layout unit test.
The ihl and version fields are declared as `unsigned int` bitfields, with their order depending on the byte order, matching the definitions in other C libraries (including the resulting 4-byte alignment of the structure).
The header is not complete yet. The BSD-style `struct ip`, `struct ip_timestamp`, and the various IP* macros (IPVERSION, IP_DF, IPTOS_*, IPOPT_*, etc.) will be added in follow-up patches. Unlike other implementations, the header also does not (yet) transitively include <netinet/in.h>.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/243768.diff
13 Files Affected:
- (modified) libc/config/linux/aarch64/headers.txt (+1)
- (modified) libc/config/linux/riscv/headers.txt (+1)
- (modified) libc/config/linux/x86_64/headers.txt (+1)
- (modified) libc/hdr/types/CMakeLists.txt (+8)
- (added) libc/hdr/types/struct_iphdr.h (+27)
- (modified) libc/include/CMakeLists.txt (+9)
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1)
- (added) libc/include/llvm-libc-types/struct_iphdr.h (+40)
- (added) libc/include/netinet/ip.yaml (+9)
- (modified) libc/test/src/netinet/CMakeLists.txt (+1)
- (added) libc/test/src/netinet/ip/CMakeLists.txt (+5)
- (added) libc/test/src/netinet/ip/linux/CMakeLists.txt (+11)
- (added) libc/test/src/netinet/ip/linux/ip_test.cpp (+43)
``````````diff
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 2136903109b514..20b182467fc6c0 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.net_if
libc.include.netinet_ether
libc.include.netinet_in
+ libc.include.netinet_ip
libc.include.netinet_tcp
libc.include.netinet_udp
libc.include.poll
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index b6a28c6226303b..b9e1373f11b80a 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.net_if
libc.include.netinet_ether
libc.include.netinet_in
+ libc.include.netinet_ip
libc.include.netinet_tcp
libc.include.netinet_udp
libc.include.nl_types
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index f7e5dd3bd6b80a..55c026ea34e672 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.net_if
libc.include.netinet_ether
libc.include.netinet_in
+ libc.include.netinet_ip
libc.include.netinet_tcp
libc.include.netinet_udp
libc.include.nl_types
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 2b834cd4f21e77..e920b0b138ea85 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -746,6 +746,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_sockaddr_in6
)
+add_proxy_header_library(
+ struct_iphdr
+ HDRS
+ struct_iphdr.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_iphdr
+)
+
add_proxy_header_library(
struct_udphdr
HDRS
diff --git a/libc/hdr/types/struct_iphdr.h b/libc/hdr/types/struct_iphdr.h
new file mode 100644
index 00000000000000..4f00010f6e7336
--- /dev/null
+++ b/libc/hdr/types/struct_iphdr.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 for struct iphdr.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_IPHDR_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_IPHDR_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_iphdr.h"
+
+#else
+
+#include <netinet/ip.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_IPHDR_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index ddf514cb287632..50b5d81f105111 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1243,6 +1243,15 @@ add_header_macro(
.sys_socket
)
+add_header_macro(
+ netinet_ip
+ ../libc/include/netinet/ip.yaml
+ netinet/ip.h
+ DEPENDS
+ .llvm-libc-types.struct_iphdr
+ .llvm_libc_common_h
+)
+
add_header_macro(
netinet_tcp
../libc/include/netinet/tcp.yaml
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 4ec5ee3c714653..545bd26cdb7189 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -325,6 +325,7 @@ add_header(struct_ucred HDR struct_ucred.h DEPENDS .pid_t .uid_t .gid_t)
add_header(struct_cmsghdr HDR struct_cmsghdr.h DEPENDS .size_t)
add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
add_header(struct_mmsghdr HDR struct_mmsghdr.h DEPENDS .struct_msghdr)
+add_header(struct_iphdr HDR struct_iphdr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(struct_udphdr HDR struct_udphdr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(struct_ether_addr HDR struct_ether_addr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(ACTION HDR ACTION.h)
diff --git a/libc/include/llvm-libc-types/struct_iphdr.h b/libc/include/llvm-libc-types/struct_iphdr.h
new file mode 100644
index 00000000000000..3ee6248a95703e
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_iphdr.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Definition of struct iphdr.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_IPHDR_H
+#define LLVM_LIBC_TYPES_STRUCT_IPHDR_H
+
+#include "../llvm-libc-macros/stdint-macros.h"
+
+struct iphdr {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ unsigned int ihl : 4;
+ unsigned int version : 4;
+#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ unsigned int version : 4;
+ unsigned int ihl : 4;
+#else
+#error "Unsupported byte order"
+#endif
+ uint8_t tos;
+ uint16_t tot_len;
+ uint16_t id;
+ uint16_t frag_off;
+ uint8_t ttl;
+ uint8_t protocol;
+ uint16_t check;
+ uint32_t saddr;
+ uint32_t daddr;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_IPHDR_H
diff --git a/libc/include/netinet/ip.yaml b/libc/include/netinet/ip.yaml
new file mode 100644
index 00000000000000..0b67194c2e9a4b
--- /dev/null
+++ b/libc/include/netinet/ip.yaml
@@ -0,0 +1,9 @@
+header: netinet/ip.h
+standards:
+ - linux
+macros: []
+types:
+ - type_name: struct_iphdr
+enums: []
+objects: []
+functions: []
diff --git a/libc/test/src/netinet/CMakeLists.txt b/libc/test/src/netinet/CMakeLists.txt
index 87c84b31bb1e32..a7a8329b238f87 100644
--- a/libc/test/src/netinet/CMakeLists.txt
+++ b/libc/test/src/netinet/CMakeLists.txt
@@ -1,6 +1,7 @@
add_libc_testsuite(libc-netinet-tests)
add_subdirectory(ether)
+add_subdirectory(ip)
add_libc_test(
in_test
diff --git a/libc/test/src/netinet/ip/CMakeLists.txt b/libc/test/src/netinet/ip/CMakeLists.txt
new file mode 100644
index 00000000000000..89abc92815166e
--- /dev/null
+++ b/libc/test/src/netinet/ip/CMakeLists.txt
@@ -0,0 +1,5 @@
+add_libc_testsuite(libc-netinet-ip-tests PARENT libc-netinet-tests)
+
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/netinet/ip/linux/CMakeLists.txt b/libc/test/src/netinet/ip/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..c74457a5072f85
--- /dev/null
+++ b/libc/test/src/netinet/ip/linux/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_libc_test(
+ ip_test
+ SUITE
+ libc-netinet-ip-tests
+ SRCS
+ ip_test.cpp
+ DEPENDS
+ libc.hdr.types.struct_iphdr
+ libc.src.__support.net.byte_order
+ libc.src.string.memcmp
+)
diff --git a/libc/test/src/netinet/ip/linux/ip_test.cpp b/libc/test/src/netinet/ip/linux/ip_test.cpp
new file mode 100644
index 00000000000000..a1d6e89729f485
--- /dev/null
+++ b/libc/test/src/netinet/ip/linux/ip_test.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/ip.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/struct_iphdr.h"
+#include "src/__support/net/byte_order.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcNetinetIpTest, StructIphdrLayout) {
+ ASSERT_EQ(sizeof(struct iphdr), static_cast<size_t>(20));
+
+ struct iphdr header;
+ header.version = 4;
+ header.ihl = 5;
+ header.tos = 0x10;
+ header.tot_len = LIBC_NAMESPACE::net::hton<uint16_t>(0x0028);
+ header.id = LIBC_NAMESPACE::net::hton<uint16_t>(0x1234);
+ header.frag_off = LIBC_NAMESPACE::net::hton<uint16_t>(0x4000);
+ header.ttl = 64;
+ header.protocol = 6;
+ header.check = LIBC_NAMESPACE::net::hton<uint16_t>(0xabcd);
+ header.saddr = LIBC_NAMESPACE::net::hton<uint32_t>(0x7f000001);
+ header.daddr = LIBC_NAMESPACE::net::hton<uint32_t>(0x08080808);
+
+ EXPECT_EQ(LIBC_NAMESPACE::memcmp(&header,
+ "\x45\x10\x00\x28"
+ "\x12\x34\x40\x00"
+ "\x40\x06\xab\xcd"
+ "\x7f\x00\x00\x01"
+ "\x08\x08\x08\x08",
+ 20),
+ 0);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/243768
More information about the libc-commits
mailing list