[libc-commits] [libc] [llvm] Reapply "[libc] Add network byte-order helpers to __support/net" (PR #243396)

via libc-commits libc-commits at lists.llvm.org
Tue Nov 17 02:03:03 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Pavel Labath (labath)

<details>
<summary>Changes</summary>

Reverts llvm/llvm-project#<!-- -->243187, re-applying #<!-- -->243122.

Dropping libc.src.arpa.inet.htons from udp_test removed the entrypoint gating that had been keeping it disabled on non-Linux targets. Move udp_test into test/src/netinet/linux/ so it only builds on Linux.

The original commit message was:

In overlay mode, system headers (<netinet/in.h>, <arpa/inet.h>) may define htons/htonl/ntohs/ntohl as macros. Including our entrypoint headers after those system headers or calling LIBC_NAMESPACE::htons(...) without extra parentheses runs into macro expansion issues.

Add templated net::hton<T> and net::ntoh<T> helpers in __support/net/byte_order.h (along with making Endian conversions constexpr), and use them across internal networking code, the hton*/ntoh* entrypoints, and unit tests instead of the htons-family entrypoints or direct Endian::{to,from}_big_endian calls.

Assisted-by: Gemini

---

Patch is 37.91 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243396.diff


28 Files Affected:

- (modified) libc/src/__support/endian_internal.h (+23-20) 
- (modified) libc/src/__support/net/CMakeLists.txt (+10) 
- (modified) libc/src/__support/net/address.cpp (+4-4) 
- (added) libc/src/__support/net/byte_order.h (+35) 
- (modified) libc/src/arpa/inet/CMakeLists.txt (+4) 
- (modified) libc/src/arpa/inet/htonl.cpp (+2-2) 
- (modified) libc/src/arpa/inet/htons.cpp (+2-2) 
- (modified) libc/src/arpa/inet/ntohl.cpp (+2-2) 
- (modified) libc/src/arpa/inet/ntohs.cpp (+2-2) 
- (modified) libc/test/src/__support/net/CMakeLists.txt (+13) 
- (added) libc/test/src/__support/net/byte_order_test.cpp (+53) 
- (modified) libc/test/src/arpa/inet/CMakeLists.txt (+5-4) 
- (modified) libc/test/src/arpa/inet/inet_addr_test.cpp (+10-10) 
- (modified) libc/test/src/arpa/inet/inet_aton_test.cpp (+13-13) 
- (modified) libc/test/src/arpa/inet/inet_ntoa_test.cpp (+2-2) 
- (modified) libc/test/src/arpa/inet/inet_ntop_test.cpp (+10-10) 
- (modified) libc/test/src/arpa/inet/inet_pton_test.cpp (+3-3) 
- (modified) libc/test/src/netinet/CMakeLists.txt (+4-13) 
- (modified) libc/test/src/netinet/in_test.cpp (+15-18) 
- (added) libc/test/src/netinet/linux/CMakeLists.txt (+11) 
- (renamed) libc/test/src/netinet/linux/udp_test.cpp (+9-9) 
- (modified) libc/test/src/sys/socket/linux/CMakeLists.txt (+1-1) 
- (modified) libc/test/src/sys/socket/linux/bind_test.cpp (+10-11) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/net/BUILD.bazel (+10) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/arpa/inet/BUILD.bazel (+4) 
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/__support/net/BUILD.bazel (+9) 
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/arpa/inet/BUILD.bazel (+5-5) 
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel (+1-1) 


``````````diff
diff --git a/libc/src/__support/endian_internal.h b/libc/src/__support/endian_internal.h
index 0173cb5748b58b..6078a5d23639c7 100644
--- a/libc/src/__support/endian_internal.h
+++ b/libc/src/__support/endian_internal.h
@@ -39,16 +39,19 @@ namespace internal {
 template <unsigned ORDER> struct Endian {
   static constexpr const bool IS_LITTLE = ORDER == __ORDER_LITTLE_ENDIAN__;
   static constexpr const bool IS_BIG = ORDER == __ORDER_BIG_ENDIAN__;
-  template <typename T> LIBC_INLINE static T to_big_endian(T value);
-  template <typename T> LIBC_INLINE static T to_little_endian(T value);
+  template <typename T> LIBC_INLINE static constexpr T to_big_endian(T value);
+  template <typename T>
+  LIBC_INLINE static constexpr T to_little_endian(T value);
 
   // Converting "to" and "from" a given endianness is actually the same
   // operation, but we provide dedicated functions to let the user express their
   // intent clearly.
-  template <typename T> LIBC_INLINE static T from_big_endian(T value) {
+  template <typename T>
+  LIBC_INLINE static constexpr T from_big_endian(T value) {
     return to_big_endian(value);
   }
-  template <typename T> LIBC_INLINE static T from_little_endian(T value) {
+  template <typename T>
+  LIBC_INLINE static constexpr T from_little_endian(T value) {
     return to_little_endian(value);
   }
 };
@@ -56,49 +59,49 @@ template <unsigned ORDER> struct Endian {
 // Little Endian specializations
 template <>
 template <>
-LIBC_INLINE uint8_t
+LIBC_INLINE constexpr uint8_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint8_t>(uint8_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint8_t
+LIBC_INLINE constexpr uint8_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian<uint8_t>(uint8_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint16_t
+LIBC_INLINE constexpr uint16_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint16_t>(uint16_t v) {
   return cpp::byteswap(v);
 }
 template <>
 template <>
-LIBC_INLINE uint16_t
+LIBC_INLINE constexpr uint16_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian<uint16_t>(uint16_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint32_t
+LIBC_INLINE constexpr uint32_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint32_t>(uint32_t v) {
   return cpp::byteswap(v);
 }
 template <>
 template <>
-LIBC_INLINE uint32_t
+LIBC_INLINE constexpr uint32_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian<uint32_t>(uint32_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint64_t
+LIBC_INLINE constexpr uint64_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint64_t>(uint64_t v) {
   return cpp::byteswap(v);
 }
 template <>
 template <>
-LIBC_INLINE uint64_t
+LIBC_INLINE constexpr uint64_t
 Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian<uint64_t>(uint64_t v) {
   return v;
 }
@@ -106,49 +109,49 @@ Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian<uint64_t>(uint64_t v) {
 // Big Endian specializations
 template <>
 template <>
-LIBC_INLINE uint8_t
+LIBC_INLINE constexpr uint8_t
 Endian<__ORDER_BIG_ENDIAN__>::to_big_endian<uint8_t>(uint8_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint8_t
+LIBC_INLINE constexpr uint8_t
 Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint8_t>(uint8_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint16_t
+LIBC_INLINE constexpr uint16_t
 Endian<__ORDER_BIG_ENDIAN__>::to_big_endian<uint16_t>(uint16_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint16_t
+LIBC_INLINE constexpr uint16_t
 Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint16_t>(uint16_t v) {
   return cpp::byteswap(v);
 }
 template <>
 template <>
-LIBC_INLINE uint32_t
+LIBC_INLINE constexpr uint32_t
 Endian<__ORDER_BIG_ENDIAN__>::to_big_endian<uint32_t>(uint32_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint32_t
+LIBC_INLINE constexpr uint32_t
 Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint32_t>(uint32_t v) {
   return cpp::byteswap(v);
 }
 template <>
 template <>
-LIBC_INLINE uint64_t
+LIBC_INLINE constexpr uint64_t
 Endian<__ORDER_BIG_ENDIAN__>::to_big_endian<uint64_t>(uint64_t v) {
   return v;
 }
 template <>
 template <>
-LIBC_INLINE uint64_t
+LIBC_INLINE constexpr uint64_t
 Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint64_t>(uint64_t v) {
   return cpp::byteswap(v);
 }
diff --git a/libc/src/__support/net/CMakeLists.txt b/libc/src/__support/net/CMakeLists.txt
index fd7fbe0dba952f..20f373c71cc0d9 100644
--- a/libc/src/__support/net/CMakeLists.txt
+++ b/libc/src/__support/net/CMakeLists.txt
@@ -1,3 +1,12 @@
+add_header_library(
+  byte_order
+  HDRS
+    byte_order.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   hex
   HDRS
@@ -19,6 +28,7 @@ add_object_library(
   SRCS
     address.cpp
   DEPENDS
+    .byte_order
     .hex
     libc.hdr.types.in_addr_t
     libc.hdr.types.struct_in_addr
diff --git a/libc/src/__support/net/address.cpp b/libc/src/__support/net/address.cpp
index 64954c08eea300..203592e2098ade 100644
--- a/libc/src/__support/net/address.cpp
+++ b/libc/src/__support/net/address.cpp
@@ -21,9 +21,9 @@
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/common.h"
 #include "src/__support/ctype_utils.h"
-#include "src/__support/endian_internal.h"
 #include "src/__support/fixedvector.h"
 #include "src/__support/libc_assert.h"
+#include "src/__support/net/byte_order.h"
 #include "src/__support/net/hex.h"
 #include "src/__support/str_to_integer.h"
 #include "src/string/memory_utils/inline_bzero.h"
@@ -124,7 +124,7 @@ namespace net {
     }
 
     cpp::optional<uint16_t> word = consume_hex<uint16_t>(src);
-    if (!word || !parts[part_idx].push_back(Endian::to_big_endian(*word)))
+    if (!word || !parts[part_idx].push_back(hton(*word)))
       return false;
   }
 
@@ -191,7 +191,7 @@ cpp::optional<in_addr_t> inet_addr(cpp::string_view src) {
     result |= parts[i] << shift;
   }
 
-  return Endian::to_big_endian(result);
+  return hton(result);
 }
 
 namespace {
@@ -253,7 +253,7 @@ size_t ipv6_to_str_unchecked(const struct in6_addr &src, cpp::span<char> dst) {
 
   size_t pos = 0;
   auto append_word = [&](unsigned i) {
-    uint16_t word = Endian::from_big_endian(src.s6_addr16[i]);
+    uint16_t word = ntoh(src.s6_addr16[i]);
     // This isn't using int_to_b36_char because it's large intermediate
     // representation prevents append_word from being inlined.
     static constexpr char DIGITS[] = "0123456789abcdef";
diff --git a/libc/src/__support/net/byte_order.h b/libc/src/__support/net/byte_order.h
new file mode 100644
index 00000000000000..ea218f6ec485d0
--- /dev/null
+++ b/libc/src/__support/net/byte_order.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Helpers for converting between host and network byte order.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_NET_BYTE_ORDER_H
+#define LLVM_LIBC_SRC___SUPPORT_NET_BYTE_ORDER_H
+
+#include "src/__support/common.h"
+#include "src/__support/endian_internal.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace net {
+
+template <typename T> LIBC_INLINE constexpr T hton(T v) {
+  return Endian::to_big_endian(v);
+}
+
+template <typename T> LIBC_INLINE constexpr T ntoh(T v) {
+  return Endian::from_big_endian(v);
+}
+
+} // namespace net
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_NET_BYTE_ORDER_H
diff --git a/libc/src/arpa/inet/CMakeLists.txt b/libc/src/arpa/inet/CMakeLists.txt
index 1096d8654511fa..8ae33d16fd8038 100644
--- a/libc/src/arpa/inet/CMakeLists.txt
+++ b/libc/src/arpa/inet/CMakeLists.txt
@@ -8,6 +8,7 @@ add_entrypoint_object(
     libc.hdr.stdint_proxy
     libc.include.arpa_inet
     libc.src.__support.common
+    libc.src.__support.net.byte_order
 )
 
 add_entrypoint_object(
@@ -20,6 +21,7 @@ add_entrypoint_object(
     libc.hdr.stdint_proxy
     libc.include.arpa_inet
     libc.src.__support.common
+    libc.src.__support.net.byte_order
 )
 
 add_entrypoint_object(
@@ -119,6 +121,7 @@ add_entrypoint_object(
     libc.hdr.stdint_proxy
     libc.include.arpa_inet
     libc.src.__support.common
+    libc.src.__support.net.byte_order
 )
 
 add_entrypoint_object(
@@ -131,4 +134,5 @@ add_entrypoint_object(
     libc.hdr.stdint_proxy
     libc.include.arpa_inet
     libc.src.__support.common
+    libc.src.__support.net.byte_order
 )
diff --git a/libc/src/arpa/inet/htonl.cpp b/libc/src/arpa/inet/htonl.cpp
index fb0404a26a2446..5fd31210529174 100644
--- a/libc/src/arpa/inet/htonl.cpp
+++ b/libc/src/arpa/inet/htonl.cpp
@@ -8,13 +8,13 @@
 
 #include "src/arpa/inet/htonl.h"
 #include "src/__support/common.h"
-#include "src/__support/endian_internal.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/net/byte_order.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint32_t, htonl, (uint32_t hostlong)) {
-  return Endian::to_big_endian(hostlong);
+  return net::hton(hostlong);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/arpa/inet/htons.cpp b/libc/src/arpa/inet/htons.cpp
index 1fcbbdf67deb01..659356a8053f85 100644
--- a/libc/src/arpa/inet/htons.cpp
+++ b/libc/src/arpa/inet/htons.cpp
@@ -8,13 +8,13 @@
 
 #include "src/arpa/inet/htons.h"
 #include "src/__support/common.h"
-#include "src/__support/endian_internal.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/net/byte_order.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint16_t, htons, (uint16_t hostshort)) {
-  return Endian::to_big_endian(hostshort);
+  return net::hton(hostshort);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/arpa/inet/ntohl.cpp b/libc/src/arpa/inet/ntohl.cpp
index 255c24dcff81a2..2f5ba82805e024 100644
--- a/libc/src/arpa/inet/ntohl.cpp
+++ b/libc/src/arpa/inet/ntohl.cpp
@@ -8,13 +8,13 @@
 
 #include "src/arpa/inet/ntohl.h"
 #include "src/__support/common.h"
-#include "src/__support/endian_internal.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/net/byte_order.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) {
-  return Endian::from_big_endian(netlong);
+  return net::ntoh(netlong);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/arpa/inet/ntohs.cpp b/libc/src/arpa/inet/ntohs.cpp
index 79c9fb9e436dab..fb87d810953e2b 100644
--- a/libc/src/arpa/inet/ntohs.cpp
+++ b/libc/src/arpa/inet/ntohs.cpp
@@ -8,13 +8,13 @@
 
 #include "src/arpa/inet/ntohs.h"
 #include "src/__support/common.h"
-#include "src/__support/endian_internal.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/net/byte_order.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint16_t, ntohs, (uint16_t netshort)) {
-  return Endian::from_big_endian(netshort);
+  return net::ntoh(netshort);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/__support/net/CMakeLists.txt b/libc/test/src/__support/net/CMakeLists.txt
index 81ec1b64b244fd..5cade8d940e6f1 100644
--- a/libc/test/src/__support/net/CMakeLists.txt
+++ b/libc/test/src/__support/net/CMakeLists.txt
@@ -1,5 +1,18 @@
 add_libc_testsuite(libc-support-net-tests PARENT libc-support-tests)
 
+add_libc_test(
+  byte_order_test
+  SUITE
+    libc-support-net-tests
+  SRCS
+    byte_order_test.cpp
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.net.byte_order
+)
+
 add_libc_test(
   hex_test
   SUITE
diff --git a/libc/test/src/__support/net/byte_order_test.cpp b/libc/test/src/__support/net/byte_order_test.cpp
new file mode 100644
index 00000000000000..6b822b99586879
--- /dev/null
+++ b/libc/test/src/__support/net/byte_order_test.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 the network byte order helpers.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/endian_internal.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/net/byte_order.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace net {
+
+static_assert(ntoh(hton<uint8_t>(0x12)) == 0x12);
+static_assert(hton(ntoh<uint8_t>(0x12)) == 0x12);
+static_assert(ntoh(hton<uint16_t>(0x0123)) == 0x0123);
+static_assert(hton(ntoh<uint16_t>(0x0123)) == 0x0123);
+static_assert(ntoh(hton<uint32_t>(0x01234567)) == 0x01234567);
+static_assert(hton(ntoh<uint32_t>(0x01234567)) == 0x01234567);
+static_assert(ntoh(hton<uint64_t>(0x0123456789abcdef)) == 0x0123456789abcdef);
+static_assert(hton(ntoh<uint64_t>(0x0123456789abcdef)) == 0x0123456789abcdef);
+
+template <typename T> void check_conversion(T original, T swapped) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+  EXPECT_EQ(hton(original), swapped);
+  EXPECT_EQ(ntoh(swapped), original);
+#endif
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+  EXPECT_EQ(hton(original), original);
+  EXPECT_EQ(ntoh(original), original);
+#endif
+  EXPECT_EQ(ntoh(hton(original)), original);
+  EXPECT_EQ(hton(ntoh(original)), original);
+}
+
+TEST(LlvmLibcNetByteOrderTest, Conversion) {
+  check_conversion<uint8_t>(0x12, 0x12);
+  check_conversion<uint16_t>(0x2301, 0x0123);
+  check_conversion<uint32_t>(0x67452301, 0x01234567);
+  check_conversion<uint64_t>(0xefcdab8967452301, 0x0123456789abcdef);
+}
+
+} // namespace net
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/arpa/inet/CMakeLists.txt b/libc/test/src/arpa/inet/CMakeLists.txt
index 79acb1a522340f..cd9223abe1c599 100644
--- a/libc/test/src/arpa/inet/CMakeLists.txt
+++ b/libc/test/src/arpa/inet/CMakeLists.txt
@@ -29,7 +29,7 @@ add_libc_test(
   SRCS
     inet_addr_test.cpp
   DEPENDS
-    libc.src.arpa.inet.htonl
+    libc.src.__support.net.byte_order
     libc.src.arpa.inet.inet_addr
 )
 
@@ -40,7 +40,7 @@ add_libc_test(
   SRCS
     inet_aton_test.cpp
   DEPENDS
-    libc.src.arpa.inet.htonl
+    libc.src.__support.net.byte_order
     libc.src.arpa.inet.inet_aton
 )
 
@@ -53,7 +53,7 @@ add_libc_test(
   DEPENDS
     libc.src.arpa.inet.inet_ntoa
     libc.hdr.types.struct_in_addr
-    libc.src.__support.endian_internal
+    libc.src.__support.net.byte_order
 )
 
 add_libc_test(
@@ -68,7 +68,7 @@ add_libc_test(
     libc.hdr.sys_socket_macros
     libc.hdr.types.struct_in_addr
     libc.hdr.types.struct_in6_addr
-    libc.src.__support.common
+    libc.src.__support.net.byte_order
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
@@ -82,6 +82,7 @@ add_libc_test(
     libc.hdr.errno_macros
     libc.hdr.types.struct_in_addr
     libc.hdr.types.struct_in6_addr
+    libc.src.__support.net.byte_order
     libc.src.arpa.inet.inet_pton
     libc.hdr.sys_socket_macros
     libc.src.string.memory_utils.inline_memcmp
diff --git a/libc/test/src/arpa/inet/inet_addr_test.cpp b/libc/test/src/arpa/inet/inet_addr_test.cpp
index bc6d07a2d186a2..17bc15431620b5 100644
--- a/libc/test/src/arpa/inet/inet_addr_test.cpp
+++ b/libc/test/src/arpa/inet/inet_addr_test.cpp
@@ -6,25 +6,25 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/arpa/inet/htonl.h"
+#include "src/__support/net/byte_order.h"
 #include "src/arpa/inet/inet_addr.h"
 #include "test/UnitTest/Test.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 TEST(LlvmLibcInetAddr, ValidTest) {
-  EXPECT_EQ(htonl(0x7f010204), inet_addr("127.1.2.4"));
-  EXPECT_EQ(htonl(0x7f010004), inet_addr("127.1.4"));
-  EXPECT_EQ(htonl(0x00000000), inet_addr("0"));
-  EXPECT_EQ(htonl(0x00000000), inet_addr("00"));
-  EXPECT_EQ(htonl(0x00000000), inet_addr("0x0"));
+  EXPECT_EQ(net::hton<uint32_t>(0x7f010204), inet_addr("127.1.2.4"));
+  EXPECT_EQ(net::hton<uint32_t>(0x7f010004), inet_addr("127.1.4"));
+  EXPECT_EQ(net::hton<uint32_t>(0x00000000), inet_addr("0"));
+  EXPECT_EQ(net::hton<uint32_t>(0x00000000), inet_addr("00"));
+  EXPECT_EQ(net::hton<uint32_t>(0x00000000), inet_addr("0x0"));
 }
 
 TEST(LlvmLibcInetAddr, InvalidTest) {
-  EXPECT_EQ(htonl(0xffffffff), inet_addr(""));
-  EXPECT_EQ(htonl(0xffffffff), inet_addr("x"));
-  EXPECT_EQ(htonl(0xffffffff), inet_addr("0b0"));
-  EXPECT_EQ(htonl(0xffffffff), inet_addr("0B0"));
+  EXPECT_EQ(net::hton<uint32_t>(0xffffffff), inet_addr(""));
+  EXPECT_EQ(net::hton<uint32_t>(0xffffffff), inet_addr("x"));
+  EXPECT_EQ(net::hton<uint32_t>(0xffffffff), inet_addr("0b0"));
+  EXPECT_EQ(net::hton<uint32_t>(0xffffffff), inet_addr("0B0"));
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/arpa/inet/inet_aton_test.cpp b/libc/test/src/arpa/inet/inet_aton_test.cpp
index dd7ea26374584a..de486a953b5827 100644
--- a/libc/test/src/arpa/inet/inet_aton_test.cpp
+++ b/libc/test/src/arpa/inet/inet_aton_test.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/arpa/inet/htonl.h"
+#include "src/__support/net/byte_order.h"
 #include "src/arpa/inet/inet_aton.h"
 #include "test/UnitTest/Test.h"
 
@@ -18,55 +18,55 @@ TEST(LlvmLibcInetAton, ValidTest) {
   // a.b.c.d
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("127.1.2.4", &a));
-  ASSERT_EQ(htonl(0x7f010204), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x7f010204), a.s_addr);
 
   // a.b.c
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("127.1.4", &a));
-  ASSERT_EQ(htonl(0x7f010004), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x7f010004), a.s_addr);
 
   // a.b
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("127.1", &a));
-  ASSERT_EQ(htonl(0x7f000001), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x7f000001), a.s_addr);
 
   // a
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("0x7f000001", &a));
-  ASSERT_EQ(htonl(0x7f000001), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x7f000001), a.s_addr);
 
   // Hex (0x) and mixed-case hex digits.
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("0xFf.0.0.1", &a));
-  ASSERT_EQ(htonl(0xff000001), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0xff000001), a.s_addr);
 
   // Hex (0X) and mixed-case hex digits.
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("0XfF.0.0.1", &a));
-  ASSERT_EQ(htonl(0xff000001), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0xff000001), a.s_addr);
 
   // Octal.
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("0177.0.0.1", &a));
-  ASSERT_EQ(htonl(0x7f000001), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x7f000001), a.s_addr);
 
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("036", &a));
-  ASSERT_EQ(htonl(036U), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(036), a.s_addr);
 
   // Trailing space.
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("1 ", &a));
-  ASSERT_EQ(htonl(1), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(1), a.s_addr);
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("1.2 ", &a));
-  ASSERT_EQ(htonl(0x01000002), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x01000002), a.s_addr);
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("1.2.3 ", &a));
-  ASSERT_EQ(htonl(0x01020003), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x01020003), a.s_addr);
   a.s_addr = 0;
   ASSERT_EQ(1, inet_aton("1.2.3.4 ", &a));
-  ASSERT_EQ(htonl(0x01020304), a.s_addr);
+  ASSERT_EQ(net::hton<uint32_t>(0x01020304), a.s_addr);
 
   // We can check for validity with a nullptr argument.
   ASSERT_EQ(1, inet_aton("127.1.2.4", nullptr));
diff --git a/libc/test/src/arpa/inet/inet_ntoa_test.cpp b/libc/test/src/arpa/inet/inet_ntoa_test.cpp
index bdc78644513b07..980ac48bee2ca0 100644
--- a/libc/test/src/arpa/inet/inet_ntoa_test.cpp
+++ b/libc/test/src/arpa/inet/inet_ntoa_test.cpp
@@ -12,13 +12,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/types/struct_in_addr.h"
-#include "src/__support/endian_internal.h"
+#include "src/__support/net/byte_order.h"
 #include "src/arp...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/243396


More information about the libc-commits mailing list