diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -321,6 +321,8 @@ # network.h entrypoints libc.src.network.htonl libc.src.network.htons + libc.src.network.ntohl + libc.src.network.ntohs # pthread.h entrypoints libc.src.pthread.pthread_atfork diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -334,6 +334,8 @@ # network.h entrypoints libc.src.network.htonl libc.src.network.htons + libc.src.network.ntohl + libc.src.network.ntohs # pthread.h entrypoints libc.src.pthread.pthread_atfork diff --git a/libc/spec/posix.td b/libc/spec/posix.td --- a/libc/spec/posix.td +++ b/libc/spec/posix.td @@ -814,6 +814,16 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "ntohl", + RetValSpec, + [ArgSpec] + >, + FunctionSpec< + "ntohs", + RetValSpec, + [ArgSpec] + >, ] >; diff --git a/libc/src/network/CMakeLists.txt b/libc/src/network/CMakeLists.txt --- a/libc/src/network/CMakeLists.txt +++ b/libc/src/network/CMakeLists.txt @@ -19,3 +19,25 @@ libc.include.arpa_inet libc.src.__support.common ) + +add_entrypoint_object( + ntohl + SRCS + ntohl.cpp + HDRS + ntohl.h + DEPENDS + libc.include.arpa_inet + libc.src.__support.common +) + +add_entrypoint_object( + ntohs + SRCS + ntohs.cpp + HDRS + ntohs.h + DEPENDS + libc.include.arpa_inet + libc.src.__support.common +) diff --git a/libc/src/network/htons.h b/libc/src/network/htons.h --- a/libc/src/network/htons.h +++ b/libc/src/network/htons.h @@ -13,7 +13,7 @@ namespace __llvm_libc { -uint16_t htons(uint16_t hostlong); +uint16_t htons(uint16_t hostshort); } // namespace __llvm_libc diff --git a/libc/src/network/htons.h b/libc/src/network/ntohl.h copy from libc/src/network/htons.h copy to libc/src/network/ntohl.h --- a/libc/src/network/htons.h +++ b/libc/src/network/ntohl.h @@ -1,4 +1,4 @@ -//===-- Implementation header of htons --------------------------*- C++ -*-===// +//===-- Implementation header of ntohl --------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,15 +6,15 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_NETWORK_HTONS_H -#define LLVM_LIBC_SRC_NETWORK_HTONS_H +#ifndef LLVM_LIBC_SRC_NETWORK_NTOHL_H +#define LLVM_LIBC_SRC_NETWORK_NTOHL_H #include namespace __llvm_libc { -uint16_t htons(uint16_t hostlong); +uint32_t ntohl(uint32_t netlong); } // namespace __llvm_libc -#endif // LLVM_LIBC_SRC_NETWORK_HTONS_H +#endif // LLVM_LIBC_SRC_NETWORK_NTOHL_H diff --git a/libc/src/network/htons.h b/libc/src/network/ntohl.cpp copy from libc/src/network/htons.h copy to libc/src/network/ntohl.cpp --- a/libc/src/network/htons.h +++ b/libc/src/network/ntohl.cpp @@ -1,4 +1,4 @@ -//===-- Implementation header of htons --------------------------*- C++ -*-===// +//===-- Implementation of ntohl function ----------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,15 +6,17 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_NETWORK_HTONS_H -#define LLVM_LIBC_SRC_NETWORK_HTONS_H - -#include +#include "src/network/ntohl.h" +#include "src/__support/common.h" +#include "src/__support/endian.h" namespace __llvm_libc { -uint16_t htons(uint16_t hostlong); +LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) { + if constexpr (Endian::IS_LITTLE) + return __builtin_bswap32(netlong); + else + return netlong; +} } // namespace __llvm_libc - -#endif // LLVM_LIBC_SRC_NETWORK_HTONS_H diff --git a/libc/src/network/htons.h b/libc/src/network/ntohs.h copy from libc/src/network/htons.h copy to libc/src/network/ntohs.h --- a/libc/src/network/htons.h +++ b/libc/src/network/ntohs.h @@ -1,4 +1,4 @@ -//===-- Implementation header of htons --------------------------*- C++ -*-===// +//===-- Implementation header of ntohs --------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,15 +6,15 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_NETWORK_HTONS_H -#define LLVM_LIBC_SRC_NETWORK_HTONS_H +#ifndef LLVM_LIBC_SRC_NETWORK_NTOHS_H +#define LLVM_LIBC_SRC_NETWORK_NTOHS_H #include namespace __llvm_libc { -uint16_t htons(uint16_t hostlong); +uint16_t ntohs(uint16_t netshort); } // namespace __llvm_libc -#endif // LLVM_LIBC_SRC_NETWORK_HTONS_H +#endif // LLVM_LIBC_SRC_NETWORK_NTOHS_H diff --git a/libc/src/network/ntohs.cpp b/libc/src/network/ntohs.cpp new file mode 100644 --- /dev/null +++ b/libc/src/network/ntohs.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of ntohs function ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/network/ntohs.h" +#include "src/__support/common.h" +#include "src/__support/endian.h" + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(uint16_t, ntohs, (uint16_t netshort)) { + if constexpr (Endian::IS_LITTLE) + return __builtin_bswap16(netshort); + else + return netshort; +} + +} // namespace __llvm_libc diff --git a/libc/test/src/network/CMakeLists.txt b/libc/test/src/network/CMakeLists.txt --- a/libc/test/src/network/CMakeLists.txt +++ b/libc/test/src/network/CMakeLists.txt @@ -10,6 +10,7 @@ 20 DEPENDS libc.src.network.htonl + libc.src.network.ntohl ) add_libc_unittest( @@ -22,4 +23,31 @@ 20 DEPENDS libc.src.network.htons + libc.src.network.ntohs +) + +add_libc_unittest( + ntohl + SUITE + libc_network_unittests + SRCS + ntohl_test.cpp + CXX_STANDARD + 20 + DEPENDS + libc.src.network.htonl + libc.src.network.ntohl +) + +add_libc_unittest( + ntohs + SUITE + libc_network_unittests + SRCS + ntohs_test.cpp + CXX_STANDARD + 20 + DEPENDS + libc.src.network.htons + libc.src.network.ntohs ) diff --git a/libc/test/src/network/htonl_test.cpp b/libc/test/src/network/htonl_test.cpp --- a/libc/test/src/network/htonl_test.cpp +++ b/libc/test/src/network/htonl_test.cpp @@ -8,6 +8,7 @@ #include "src/__support/endian.h" #include "src/network/htonl.h" +#include "src/network/ntohl.h" #include "test/UnitTest/Test.h" TEST(LlvmLibcHtonl, SmokeTest) { @@ -20,3 +21,8 @@ EXPECT_EQ(__llvm_libc::htonl(original), original); #endif } + +TEST(LlvmLibcHtonl, CompleteTest) { + uint32_t original = 0x01234567; + EXPECT_EQ(__llvm_libc::htonl(__llvm_libc::ntohl(original)), original); +} diff --git a/libc/test/src/network/htons_test.cpp b/libc/test/src/network/htons_test.cpp --- a/libc/test/src/network/htons_test.cpp +++ b/libc/test/src/network/htons_test.cpp @@ -8,6 +8,7 @@ #include "src/__support/endian.h" #include "src/network/htons.h" +#include "src/network/ntohs.h" #include "test/UnitTest/Test.h" TEST(LlvmLibcHtons, SmokeTest) { @@ -20,3 +21,8 @@ EXPECT_EQ(__llvm_libc::htons(original), original); #endif } + +TEST(LlvmLibcHtons, CompleteTest) { + uint16_t original = 0x0123; + EXPECT_EQ(__llvm_libc::htons(__llvm_libc::ntohs(original)), original); +} diff --git a/libc/test/src/network/htonl_test.cpp b/libc/test/src/network/ntohl_test.cpp copy from libc/test/src/network/htonl_test.cpp copy to libc/test/src/network/ntohl_test.cpp --- a/libc/test/src/network/htonl_test.cpp +++ b/libc/test/src/network/ntohl_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for htonl -----------------------------------------------===// +//===-- Unittests for ntohl -----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -8,15 +8,21 @@ #include "src/__support/endian.h" #include "src/network/htonl.h" +#include "src/network/ntohl.h" #include "test/UnitTest/Test.h" -TEST(LlvmLibcHtonl, SmokeTest) { +TEST(LlvmLibcNtohl, SmokeTest) { uint32_t original = 0x67452301; uint32_t swapped = 0x01234567; #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - EXPECT_EQ(__llvm_libc::htonl(original), swapped); + EXPECT_EQ(__llvm_libc::ntohl(original), swapped); #endif #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - EXPECT_EQ(__llvm_libc::htonl(original), original); + EXPECT_EQ(__llvm_libc::ntohl(original), original); #endif } + +TEST(LlvmLibcNtohl, CompleteTest) { + uint32_t original = 0x01234567; + EXPECT_EQ(__llvm_libc::ntohl(__llvm_libc::htonl(original)), original); +} diff --git a/libc/test/src/network/htons_test.cpp b/libc/test/src/network/ntohs_test.cpp copy from libc/test/src/network/htons_test.cpp copy to libc/test/src/network/ntohs_test.cpp --- a/libc/test/src/network/htons_test.cpp +++ b/libc/test/src/network/ntohs_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for htons -----------------------------------------------===// +//===-- Unittests for ntohs -----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -8,15 +8,21 @@ #include "src/__support/endian.h" #include "src/network/htons.h" +#include "src/network/ntohs.h" #include "test/UnitTest/Test.h" -TEST(LlvmLibcHtons, SmokeTest) { +TEST(LlvmLibcNtohs, SmokeTest) { uint16_t original = 0x2301; uint16_t swapped = 0x0123; #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - EXPECT_EQ(__llvm_libc::htons(original), swapped); + EXPECT_EQ(__llvm_libc::ntohs(original), swapped); #endif #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - EXPECT_EQ(__llvm_libc::htons(original), original); + EXPECT_EQ(__llvm_libc::ntohs(original), original); #endif } + +TEST(LlvmLibcNtohs, CompleteTest) { + uint16_t original = 0x0123; + EXPECT_EQ(__llvm_libc::ntohs(__llvm_libc::htons(original)), original); +}