[libc-commits] [libc] [libc] Add netinet/udp.h containing struct udphdr (PR #200839)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Mon Jun 1 07:52:52 PDT 2026
https://github.com/labath created https://github.com/llvm/llvm-project/pull/200839
This patch adds a generated <netinet/udp.h> containing the `udphdr` structure definition.
There are two styles ("linux" and "BSD") of udphdr field names (and both of them can be found in the wild), so I follow the glibc and bionic approach of using an anonymous union. (musl uses a #define on the field names, which doesn't seem that great).
I've added the target to `include/CMakeLists.txt` and registered it under target lists in `headers.txt` for the supported Linux platforms (x86_64, aarch64, and riscv).
To verify layout and alignment correctness, I've added a layout and field compatibility unit test under `test/src/netinet/udp_test.cpp`.
Assisted by Gemini.
>From a04f89a6ff754a3f478875d9120b5805429c842e Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 1 Jun 2026 14:00:40 +0000
Subject: [PATCH] [libc] Add netinet/udp.h containing struct udphdr
This patch adds a generated <netinet/udp.h> containing the `udphdr`
structure definition.
There are two styles ("linux" and "BSD") of udphdr field names (and both
of them can be found in the wild), so I follow the glibc and bionic
approach of using an anonymous union. (musl uses a #define on the field
names, which doesn't seem that great).
I've added the target to `include/CMakeLists.txt` and registered it
under target lists in `headers.txt` for the supported Linux platforms
(x86_64, aarch64, and riscv).
To verify layout and alignment correctness, I've added a layout and
field compatibility unit test under `test/src/netinet/udp_test.cpp`.
Assisted by Gemini.
---
libc/config/linux/aarch64/headers.txt | 1 +
libc/config/linux/riscv/headers.txt | 1 +
libc/config/linux/x86_64/headers.txt | 1 +
libc/hdr/types/CMakeLists.txt | 10 +++++
libc/hdr/types/struct_udphdr.h | 27 ++++++++++++
libc/include/CMakeLists.txt | 9 ++++
libc/include/llvm-libc-types/CMakeLists.txt | 1 +
libc/include/llvm-libc-types/struct_udphdr.h | 37 +++++++++++++++++
libc/include/netinet/udp.yaml | 9 ++++
libc/test/src/CMakeLists.txt | 1 +
libc/test/src/netinet/CMakeLists.txt | 13 ++++++
libc/test/src/netinet/udp_test.cpp | 43 ++++++++++++++++++++
12 files changed, 153 insertions(+)
create mode 100644 libc/hdr/types/struct_udphdr.h
create mode 100644 libc/include/llvm-libc-types/struct_udphdr.h
create mode 100644 libc/include/netinet/udp.yaml
create mode 100644 libc/test/src/netinet/CMakeLists.txt
create mode 100644 libc/test/src/netinet/udp_test.cpp
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 37359e6064959..d09d307565bea 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -22,6 +22,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.math
libc.include.netinet_in
libc.include.netinet_tcp
+ libc.include.netinet_udp
libc.include.poll
libc.include.pthread
libc.include.sched
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index 04d251926b8d1..c7ea504def1d8 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -22,6 +22,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.math
libc.include.netinet_in
libc.include.netinet_tcp
+ libc.include.netinet_udp
libc.include.poll
libc.include.pthread
libc.include.sched
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index cf76db06cb62c..e1e5564ca0840 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -22,6 +22,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.math
libc.include.netinet_in
libc.include.netinet_tcp
+ libc.include.netinet_udp
libc.include.nl_types
libc.include.poll
libc.include.pthread
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 09b6cc5b7fd9a..d8fdadcf9d1d0 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -483,6 +483,16 @@ add_proxy_header_library(
libc.include.netinet_in
)
+add_proxy_header_library(
+ struct_udphdr
+ HDRS
+ struct_udphdr.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_udphdr
+ libc.include.netinet_udp
+)
+
+
add_proxy_header_library(
socklen_t
HDRS
diff --git a/libc/hdr/types/struct_udphdr.h b/libc/hdr/types/struct_udphdr.h
new file mode 100644
index 0000000000000..c0b59bc8178f8
--- /dev/null
+++ b/libc/hdr/types/struct_udphdr.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 udphdr.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_UDPHDR_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_UDPHDR_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_udphdr.h"
+
+#else
+
+#include <netinet/udp.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_UDPHDR_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 0fbe37efbbf8e..a83c6bfc79537 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -233,6 +233,15 @@ add_header_macro(
.llvm_libc_common_h
)
+add_header_macro(
+ netinet_udp
+ ../libc/include/netinet/udp.yaml
+ netinet/udp.h
+ DEPENDS
+ .llvm-libc-types.struct_udphdr
+ .llvm_libc_common_h
+)
+
add_header_macro(
assert
../libc/include/assert.yaml
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 7c88e6e538879..c1b35fce166a7 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -206,6 +206,7 @@ add_header(struct_linger HDR struct_linger.h)
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_udphdr HDR struct_udphdr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(ACTION HDR ACTION.h)
add_header(ENTRY HDR ENTRY.h)
add_header(VISIT HDR VISIT.h)
diff --git a/libc/include/llvm-libc-types/struct_udphdr.h b/libc/include/llvm-libc-types/struct_udphdr.h
new file mode 100644
index 0000000000000..e821cf79018c8
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_udphdr.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 udphdr. Fields are accessible using both linux-style
+/// and BSD-style names.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_UDPHDR_H
+#define LLVM_LIBC_TYPES_STRUCT_UDPHDR_H
+
+#include "../llvm-libc-macros/stdint-macros.h"
+
+struct udphdr {
+ __extension__ union {
+ struct {
+ uint16_t uh_sport;
+ uint16_t uh_dport;
+ uint16_t uh_ulen;
+ uint16_t uh_sum;
+ };
+ struct {
+ uint16_t source;
+ uint16_t dest;
+ uint16_t len;
+ uint16_t check;
+ };
+ };
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_UDPHDR_H
diff --git a/libc/include/netinet/udp.yaml b/libc/include/netinet/udp.yaml
new file mode 100644
index 0000000000000..336525f87a52f
--- /dev/null
+++ b/libc/include/netinet/udp.yaml
@@ -0,0 +1,9 @@
+header: netinet/udp.h
+standards:
+ - linux
+macros: []
+types:
+ - type_name: struct_udphdr
+enums: []
+objects: []
+functions: []
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 7ee978f209388..dd232b29a7a7b 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -65,6 +65,7 @@ add_subdirectory(errno)
add_subdirectory(fenv)
add_subdirectory(link)
add_subdirectory(math)
+add_subdirectory(netinet)
add_subdirectory(search)
add_subdirectory(setjmp)
add_subdirectory(stdbit)
diff --git a/libc/test/src/netinet/CMakeLists.txt b/libc/test/src/netinet/CMakeLists.txt
new file mode 100644
index 0000000000000..f8e90b37288ab
--- /dev/null
+++ b/libc/test/src/netinet/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_custom_target(libc_netinet_unittests)
+
+add_libc_unittest(
+ udp_test
+ SUITE
+ libc_netinet_unittests
+ SRCS
+ udp_test.cpp
+ DEPENDS
+ libc.hdr.types.struct_udphdr
+ libc.src.arpa.inet.htons
+ libc.src.string.memcmp
+)
diff --git a/libc/test/src/netinet/udp_test.cpp b/libc/test/src/netinet/udp_test.cpp
new file mode 100644
index 0000000000000..003c993e95666
--- /dev/null
+++ b/libc/test/src/netinet/udp_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/udp.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/struct_udphdr.h"
+#include "src/arpa/inet/htons.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcNetinetUdpTest, StructUdphdrLayout) {
+ EXPECT_EQ(sizeof(struct udphdr), size_t(8));
+
+ struct udphdr header;
+
+ // 1. Set fields using BSD-style names and read via Linux-style names
+ header.uh_sport = LIBC_NAMESPACE::htons(0x1234);
+ header.uh_dport = LIBC_NAMESPACE::htons(0x5678);
+ header.uh_ulen = LIBC_NAMESPACE::htons(0x0010);
+ header.uh_sum = LIBC_NAMESPACE::htons(0xABCD);
+
+ EXPECT_EQ(
+ LIBC_NAMESPACE::memcmp(&header, "\x12\x34\x56\x78\x00\x10\xAB\xCD", 8),
+ 0);
+
+ // 2. Set fields using Linux-style names and read via BSD-style names
+ header.source = LIBC_NAMESPACE::htons(0x4321);
+ header.dest = LIBC_NAMESPACE::htons(0x8765);
+ header.len = LIBC_NAMESPACE::htons(0x2000);
+ header.check = LIBC_NAMESPACE::htons(0xDCBA);
+
+ EXPECT_EQ(
+ LIBC_NAMESPACE::memcmp(&header, "\x43\x21\x87\x65\x20\x00\xDC\xBA", 8),
+ 0);
+}
More information about the libc-commits
mailing list