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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Nov 17 01:11:06 PST 2026


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

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

>From 8b4f3f238565cd70229e197a87a97ed749acc222 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 17 Nov 2026 08:40:30 +0000
Subject: [PATCH] Reapply "[libc] Add network byte-order helpers to
 __support/net"

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
---
 libc/src/__support/endian_internal.h          | 43 ++++++++-------
 libc/src/__support/net/CMakeLists.txt         | 10 ++++
 libc/src/__support/net/address.cpp            |  8 +--
 libc/src/__support/net/byte_order.h           | 35 ++++++++++++
 libc/src/arpa/inet/CMakeLists.txt             |  4 ++
 libc/src/arpa/inet/htonl.cpp                  |  4 +-
 libc/src/arpa/inet/htons.cpp                  |  4 +-
 libc/src/arpa/inet/ntohl.cpp                  |  4 +-
 libc/src/arpa/inet/ntohs.cpp                  |  4 +-
 libc/test/src/__support/net/CMakeLists.txt    | 13 +++++
 .../src/__support/net/byte_order_test.cpp     | 53 +++++++++++++++++++
 libc/test/src/arpa/inet/CMakeLists.txt        |  9 ++--
 libc/test/src/arpa/inet/inet_addr_test.cpp    | 20 +++----
 libc/test/src/arpa/inet/inet_aton_test.cpp    | 26 ++++-----
 libc/test/src/arpa/inet/inet_ntoa_test.cpp    |  4 +-
 libc/test/src/arpa/inet/inet_ntop_test.cpp    | 20 +++----
 libc/test/src/arpa/inet/inet_pton_test.cpp    |  6 +--
 libc/test/src/netinet/CMakeLists.txt          | 17 ++----
 libc/test/src/netinet/in_test.cpp             | 33 ++++++------
 libc/test/src/netinet/linux/CMakeLists.txt    | 11 ++++
 .../test/src/netinet/{ => linux}/udp_test.cpp | 18 +++----
 libc/test/src/sys/socket/linux/CMakeLists.txt |  2 +-
 libc/test/src/sys/socket/linux/bind_test.cpp  | 21 ++++----
 .../libc/src/__support/net/BUILD.bazel        | 10 ++++
 .../libc/src/arpa/inet/BUILD.bazel            |  4 ++
 .../libc/test/src/__support/net/BUILD.bazel   |  9 ++++
 .../libc/test/src/arpa/inet/BUILD.bazel       | 10 ++--
 .../libc/test/src/sys/socket/BUILD.bazel      |  2 +-
 28 files changed, 272 insertions(+), 132 deletions(-)
 create mode 100644 libc/src/__support/net/byte_order.h
 create mode 100644 libc/test/src/__support/net/byte_order_test.cpp
 create mode 100644 libc/test/src/netinet/linux/CMakeLists.txt
 rename libc/test/src/netinet/{ => linux}/udp_test.cpp (66%)

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/arpa/inet/inet_ntoa.h"
 #include "test/UnitTest/Test.h"
 
 static struct in_addr make_addr(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
   struct in_addr addr;
-  addr.s_addr = LIBC_NAMESPACE::Endian::to_big_endian(
+  addr.s_addr = LIBC_NAMESPACE::net::hton(
       static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
       static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d));
   return addr;
diff --git a/libc/test/src/arpa/inet/inet_ntop_test.cpp b/libc/test/src/arpa/inet/inet_ntop_test.cpp
index c51c79f2d01a56..c2fe7d2e6af5ee 100644
--- a/libc/test/src/arpa/inet/inet_ntop_test.cpp
+++ b/libc/test/src/arpa/inet/inet_ntop_test.cpp
@@ -15,7 +15,7 @@
 #include "hdr/sys_socket_macros.h"
 #include "hdr/types/struct_in6_addr.h"
 #include "hdr/types/struct_in_addr.h"
-#include "src/__support/endian_internal.h"
+#include "src/__support/net/byte_order.h"
 #include "src/arpa/inet/inet_ntop.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/Test.h"
@@ -33,7 +33,7 @@ TEST_F(LlvmLibcInetNtopTest, InvalidFamily) {
 
 static void *ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
   static struct in_addr addr;
-  addr.s_addr = LIBC_NAMESPACE::Endian::to_big_endian(
+  addr.s_addr = LIBC_NAMESPACE::net::hton(
       static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
       static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d));
   return &addr;
@@ -42,14 +42,14 @@ static void *ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
 static void *ipv6(uint16_t a, uint16_t b, uint16_t c, uint16_t d, uint16_t e,
                   uint16_t f, uint16_t g, uint16_t h) {
   static struct in6_addr addr;
-  addr.s6_addr16[0] = LIBC_NAMESPACE::Endian::to_big_endian(a);
-  addr.s6_addr16[1] = LIBC_NAMESPACE::Endian::to_big_endian(b);
-  addr.s6_addr16[2] = LIBC_NAMESPACE::Endian::to_big_endian(c);
-  addr.s6_addr16[3] = LIBC_NAMESPACE::Endian::to_big_endian(d);
-  addr.s6_addr16[4] = LIBC_NAMESPACE::Endian::to_big_endian(e);
-  addr.s6_addr16[5] = LIBC_NAMESPACE::Endian::to_big_endian(f);
-  addr.s6_addr16[6] = LIBC_NAMESPACE::Endian::to_big_endian(g);
-  addr.s6_addr16[7] = LIBC_NAMESPACE::Endian::to_big_endian(h);
+  addr.s6_addr16[0] = LIBC_NAMESPACE::net::hton(a);
+  addr.s6_addr16[1] = LIBC_NAMESPACE::net::hton(b);
+  addr.s6_addr16[2] = LIBC_NAMESPACE::net::hton(c);
+  addr.s6_addr16[3] = LIBC_NAMESPACE::net::hton(d);
+  addr.s6_addr16[4] = LIBC_NAMESPACE::net::hton(e);
+  addr.s6_addr16[5] = LIBC_NAMESPACE::net::hton(f);
+  addr.s6_addr16[6] = LIBC_NAMESPACE::net::hton(g);
+  addr.s6_addr16[7] = LIBC_NAMESPACE::net::hton(h);
   return &addr;
 }
 
diff --git a/libc/test/src/arpa/inet/inet_pton_test.cpp b/libc/test/src/arpa/inet/inet_pton_test.cpp
index f54b6f51790dc6..1c92e62dc36ada 100644
--- a/libc/test/src/arpa/inet/inet_pton_test.cpp
+++ b/libc/test/src/arpa/inet/inet_pton_test.cpp
@@ -15,7 +15,7 @@
 #include "hdr/sys_socket_macros.h"
 #include "hdr/types/struct_in6_addr.h"
 #include "hdr/types/struct_in_addr.h"
-#include "src/__support/endian_internal.h"
+#include "src/__support/net/byte_order.h"
 #include "src/arpa/inet/inet_pton.h"
 #include "src/string/memory_utils/inline_memcmp.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
@@ -24,7 +24,7 @@
 using LlvmLibcInetPtonTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 static uint32_t ipv4_bits(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
-  return LIBC_NAMESPACE::Endian::to_big_endian(
+  return LIBC_NAMESPACE::net::hton(
       static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
       static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d));
 }
@@ -97,7 +97,7 @@ static bool check_ipv6(const struct in6_addr &addr, uint16_t a, uint16_t b,
   uint16_t words[8] = {a, b, c, d, e, f, g, h};
   struct in6_addr expected;
   for (size_t i = 0; i < 8; ++i)
-    expected.s6_addr16[i] = LIBC_NAMESPACE::Endian::to_big_endian(words[i]);
+    expected.s6_addr16[i] = LIBC_NAMESPACE::net::hton(words[i]);
   return LIBC_NAMESPACE::inline_memcmp(&addr, &expected,
                                        sizeof(struct in6_addr)) == 0;
 }
diff --git a/libc/test/src/netinet/CMakeLists.txt b/libc/test/src/netinet/CMakeLists.txt
index 57fd067d80c046..87c84b31bb1e32 100644
--- a/libc/test/src/netinet/CMakeLists.txt
+++ b/libc/test/src/netinet/CMakeLists.txt
@@ -20,21 +20,12 @@ add_libc_test(
     libc.hdr.types.struct_ip_opts
     libc.hdr.types.struct_ipv6_mreq
     libc.hdr.types.struct_sockaddr_in6
-    libc.src.arpa.inet.htons
-    libc.src.arpa.inet.htonl
+    libc.src.__support.net.byte_order
     libc.src.netinet.in6addr_any
     libc.src.netinet.in6addr_loopback
     libc.src.string.memcmp
 )
 
-add_libc_test(
-  udp_test
-  SUITE
-    libc-netinet-tests
-  SRCS
-    udp_test.cpp
-  DEPENDS
-    libc.hdr.types.struct_udphdr
-    libc.src.arpa.inet.htons
-    libc.src.string.memcmp
-)
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/netinet/in_test.cpp b/libc/test/src/netinet/in_test.cpp
index 9c599b5f53432a..6357385a5a158b 100644
--- a/libc/test/src/netinet/in_test.cpp
+++ b/libc/test/src/netinet/in_test.cpp
@@ -11,11 +11,6 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "src/arpa/inet/htonl.h"
-#include "src/arpa/inet/htons.h"
-#include "src/string/memcmp.h"
-#include "test/UnitTest/Test.h"
-
 #include "hdr/netinet_in_macros.h"
 #include "hdr/types/struct_group_req.h"
 #include "hdr/types/struct_group_source_req.h"
@@ -27,23 +22,25 @@
 #include "hdr/types/struct_ip_opts.h"
 #include "hdr/types/struct_ipv6_mreq.h"
 #include "hdr/types/struct_sockaddr_in6.h"
+#include "src/__support/net/byte_order.h"
 #include "src/netinet/in6addr_any.h"
 #include "src/netinet/in6addr_loopback.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcNetinetInTest, In6AddrLayout) {
   EXPECT_EQ(sizeof(struct in6_addr), static_cast<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);
+  addr.s6_addr16[0] = LIBC_NAMESPACE::net::hton<uint16_t>(0x1122);
+  addr.s6_addr16[1] = LIBC_NAMESPACE::net::hton<uint16_t>(0x3344);
+  addr.s6_addr16[2] = LIBC_NAMESPACE::net::hton<uint16_t>(0x5566);
+  addr.s6_addr16[3] = LIBC_NAMESPACE::net::hton<uint16_t>(0x7788);
+  addr.s6_addr16[4] = LIBC_NAMESPACE::net::hton<uint16_t>(0x99aa);
+  addr.s6_addr16[5] = LIBC_NAMESPACE::net::hton<uint16_t>(0xbbcc);
+  addr.s6_addr16[6] = LIBC_NAMESPACE::net::hton<uint16_t>(0xddee);
+  addr.s6_addr16[7] = LIBC_NAMESPACE::net::hton<uint16_t>(0xff00);
 
   EXPECT_EQ(
       LIBC_NAMESPACE::memcmp(
@@ -52,10 +49,10 @@ TEST(LlvmLibcNetinetInTest, In6AddrLayout) {
           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);
+  addr.s6_addr32[0] = LIBC_NAMESPACE::net::hton<uint32_t>(0x12345678);
+  addr.s6_addr32[1] = LIBC_NAMESPACE::net::hton<uint32_t>(0x9abcdef0);
+  addr.s6_addr32[2] = LIBC_NAMESPACE::net::hton<uint32_t>(0x12345678);
+  addr.s6_addr32[3] = LIBC_NAMESPACE::net::hton<uint32_t>(0x9abcdef0);
 
   EXPECT_EQ(
       LIBC_NAMESPACE::memcmp(
diff --git a/libc/test/src/netinet/linux/CMakeLists.txt b/libc/test/src/netinet/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..4316564b9a7f1e
--- /dev/null
+++ b/libc/test/src/netinet/linux/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_libc_test(
+  udp_test
+  SUITE
+    libc-netinet-tests
+  SRCS
+    udp_test.cpp
+  DEPENDS
+    libc.hdr.types.struct_udphdr
+    libc.src.__support.net.byte_order
+    libc.src.string.memcmp
+)
diff --git a/libc/test/src/netinet/udp_test.cpp b/libc/test/src/netinet/linux/udp_test.cpp
similarity index 66%
rename from libc/test/src/netinet/udp_test.cpp
rename to libc/test/src/netinet/linux/udp_test.cpp
index 9c906cd036c7c0..6524ea2ca5778f 100644
--- a/libc/test/src/netinet/udp_test.cpp
+++ b/libc/test/src/netinet/linux/udp_test.cpp
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/types/struct_udphdr.h"
-#include "src/arpa/inet/htons.h"
+#include "src/__support/net/byte_order.h"
 #include "src/string/memcmp.h"
 #include "test/UnitTest/Test.h"
 
@@ -22,20 +22,20 @@ TEST(LlvmLibcNetinetUdpTest, StructUdphdrLayout) {
   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);
+  header.uh_sport = LIBC_NAMESPACE::net::hton<uint16_t>(0x1234);
+  header.uh_dport = LIBC_NAMESPACE::net::hton<uint16_t>(0x5678);
+  header.uh_ulen = LIBC_NAMESPACE::net::hton<uint16_t>(0x0010);
+  header.uh_sum = LIBC_NAMESPACE::net::hton<uint16_t>(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);
+  header.source = LIBC_NAMESPACE::net::hton<uint16_t>(0x4321);
+  header.dest = LIBC_NAMESPACE::net::hton<uint16_t>(0x8765);
+  header.len = LIBC_NAMESPACE::net::hton<uint16_t>(0x2000);
+  header.check = LIBC_NAMESPACE::net::hton<uint16_t>(0xDCBA);
 
   EXPECT_EQ(
       LIBC_NAMESPACE::memcmp(&header, "\x43\x21\x87\x65\x20\x00\xDC\xBA", 8),
diff --git a/libc/test/src/sys/socket/linux/CMakeLists.txt b/libc/test/src/sys/socket/linux/CMakeLists.txt
index adbde8e38cb514..ca49e10b5f6887 100644
--- a/libc/test/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/test/src/sys/socket/linux/CMakeLists.txt
@@ -49,7 +49,7 @@ add_libc_test(
     libc.hdr.types.struct_sockaddr_in
     libc.hdr.types.struct_sockaddr_in6
     libc.hdr.types.struct_sockaddr_un
-    libc.src.arpa.inet.htonl
+    libc.src.__support.net.byte_order
     libc.src.sys.socket.socket
     libc.src.sys.socket.bind
     libc.src.sys.socket.getsockname
diff --git a/libc/test/src/sys/socket/linux/bind_test.cpp b/libc/test/src/sys/socket/linux/bind_test.cpp
index fea268bef74c5b..31bad885c446dd 100644
--- a/libc/test/src/sys/socket/linux/bind_test.cpp
+++ b/libc/test/src/sys/socket/linux/bind_test.cpp
@@ -6,8 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/netinet_in_macros.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/sa_family_t.h"
+#include "hdr/types/struct_sockaddr_in.h"
+#include "hdr/types/struct_sockaddr_in6.h"
+#include "hdr/types/struct_sockaddr_un.h"
 #include "src/__support/CPP/scope.h"
-#include "src/arpa/inet/htonl.h"
+#include "src/__support/net/byte_order.h"
 #include "src/stdio/remove.h"
 #include "src/sys/socket/bind.h"
 #include "src/sys/socket/getsockname.h"
@@ -18,13 +24,6 @@
 #include "test/UnitTest/Test.h"
 #include "test/src/sys/socket/linux/socket_test_support.h"
 
-#include "hdr/netinet_in_macros.h"
-#include "hdr/sys_socket_macros.h"
-#include "hdr/types/sa_family_t.h"
-#include "hdr/types/struct_sockaddr_in.h"
-#include "hdr/types/struct_sockaddr_in6.h"
-#include "hdr/types/struct_sockaddr_un.h"
-
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 using LlvmLibcBindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
@@ -58,8 +57,7 @@ TEST_F(LlvmLibcBindTest, BindInetSocket) {
   struct sockaddr_in my_addr;
   my_addr.sin_family = AF_INET;
   my_addr.sin_port = 0;
-  // Avoid expanding the htonl macro in overlay mode.
-  my_addr.sin_addr.s_addr = (LIBC_NAMESPACE::htonl)(INADDR_LOOPBACK);
+  my_addr.sin_addr.s_addr = LIBC_NAMESPACE::net::hton(INADDR_LOOPBACK);
 
   ASSERT_THAT(
       LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr),
@@ -74,7 +72,8 @@ TEST_F(LlvmLibcBindTest, BindInetSocket) {
   ASSERT_EQ(len, static_cast<socklen_t>(sizeof(struct sockaddr_in)));
   EXPECT_EQ(my_addr.sin_family, static_cast<sa_family_t>(AF_INET));
   EXPECT_NE(my_addr.sin_port, static_cast<in_port_t>(0));
-  EXPECT_EQ(my_addr.sin_addr.s_addr, (LIBC_NAMESPACE::htonl)(INADDR_LOOPBACK));
+  EXPECT_EQ(my_addr.sin_addr.s_addr,
+            LIBC_NAMESPACE::net::hton(INADDR_LOOPBACK));
 }
 
 TEST_F(LlvmLibcBindTest, BindInet6Socket) {
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/net/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/net/BUILD.bazel
index 0c00521225abd9..831d0f467d3b7d 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/net/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/net/BUILD.bazel
@@ -15,6 +15,15 @@ package(
 
 licenses(["notice"])
 
+libc_support_library(
+    name = "byte_order",
+    hdrs = ["byte_order.h"],
+    deps = [
+        "//libc/src/__support:common",
+        "//libc/src/__support/macros:config",
+    ],
+)
+
 libc_support_library(
     name = "hex",
     hdrs = ["hex.h"],
@@ -34,6 +43,7 @@ libc_support_library(
     srcs = ["address.cpp"],
     hdrs = ["address.h"],
     deps = [
+        ":byte_order",
         ":hex",
         "//libc/hdr:inet_address_macros",
         "//libc/hdr:stdint_proxy",
diff --git a/utils/bazel/llvm-project-overlay/libc/src/arpa/inet/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/arpa/inet/BUILD.bazel
index 57271b67e0d1df..f1abedd04e324b 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/arpa/inet/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/arpa/inet/BUILD.bazel
@@ -23,6 +23,7 @@ libc_function(
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support:common",
         "//libc/src/__support/macros:config",
+        "//libc/src/__support/net:byte_order",
     ],
 )
 
@@ -34,6 +35,7 @@ libc_function(
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support:common",
         "//libc/src/__support/macros:config",
+        "//libc/src/__support/net:byte_order",
     ],
 )
 
@@ -125,6 +127,7 @@ libc_function(
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support:common",
         "//libc/src/__support/macros:config",
+        "//libc/src/__support/net:byte_order",
     ],
 )
 
@@ -136,5 +139,6 @@ libc_function(
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support:common",
         "//libc/src/__support/macros:config",
+        "//libc/src/__support/net:byte_order",
     ],
 )
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/__support/net/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/__support/net/BUILD.bazel
index 68e5d9356f718f..727468cf3564c2 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/__support/net/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/__support/net/BUILD.bazel
@@ -10,6 +10,15 @@ package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])
 
+libc_test(
+    name = "byte_order_test",
+    srcs = ["byte_order_test.cpp"],
+    deps = [
+        "//libc/src/__support:common",
+        "//libc/src/__support/net:byte_order",
+    ],
+)
+
 libc_test(
     name = "hex_test",
     srcs = ["hex_test.cpp"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/arpa/inet/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/arpa/inet/BUILD.bazel
index 39011fd234fcc3..cce78a55fc397a 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/arpa/inet/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/arpa/inet/BUILD.bazel
@@ -35,7 +35,7 @@ libc_test(
     srcs = ["inet_addr_test.cpp"],
     deps = [
         "//libc/src/__support:common",
-        "//libc/src/arpa/inet:htonl",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/arpa/inet:inet_addr",
     ],
 )
@@ -45,7 +45,7 @@ libc_test(
     srcs = ["inet_aton_test.cpp"],
     deps = [
         "//libc/src/__support:common",
-        "//libc/src/arpa/inet:htonl",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/arpa/inet:inet_aton",
     ],
 )
@@ -55,7 +55,7 @@ libc_test(
     srcs = ["inet_ntoa_test.cpp"],
     deps = [
         "//libc/hdr/types:struct_in_addr",
-        "//libc/src/__support:common",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/arpa/inet:inet_ntoa",
     ],
 )
@@ -68,7 +68,7 @@ libc_test(
         "//libc/hdr:sys_socket_macros",
         "//libc/hdr/types:struct_in6_addr",
         "//libc/hdr/types:struct_in_addr",
-        "//libc/src/__support:common",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/arpa/inet:inet_ntop",
     ],
 )
@@ -81,7 +81,7 @@ libc_test(
         "//libc/hdr:sys_socket_macros",
         "//libc/hdr/types:struct_in6_addr",
         "//libc/hdr/types:struct_in_addr",
-        "//libc/src/__support:common",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/arpa/inet:inet_pton",
         "//libc/src/string/memory_utils",
     ],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
index 837b795ff1fdd3..b70b02eb300131 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
@@ -43,7 +43,7 @@ libc_test(
         "//libc/hdr/types:struct_sockaddr_in6",
         "//libc/hdr/types:struct_sockaddr_un",
         "//libc/src/__support/CPP:scope",
-        "//libc/src/arpa/inet:htonl",
+        "//libc/src/__support/net:byte_order",
         "//libc/src/stdio:remove",
         "//libc/src/sys/socket",
         "//libc/src/sys/socket:bind",



More information about the libc-commits mailing list