From 0a0f27ced8b182b7356c924ab2a86c43bcc4025c Mon Sep 17 00:00:00 2001 From: Askar Safin Date: Sun, 28 Apr 2024 23:10:36 +0300 Subject: [PATCH] Add htonl, htons, ntohl, ntohs (backport ) (cherry picked from commit 1feb3542a7263fe4e0f80b1a7f3e29ef781e75ae) --- libc-test/semver/unix.txt | 4 ++++ src/unix/mod.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/libc-test/semver/unix.txt b/libc-test/semver/unix.txt index 7f750ecae3a19..6a18038c48ae3 100644 --- a/libc-test/semver/unix.txt +++ b/libc-test/semver/unix.txt @@ -588,6 +588,8 @@ grantpt group hostent hstrerror +htonl +htons if_indextoname if_nametoindex in6_addr @@ -658,6 +660,8 @@ munmap nanosleep nfds_t nlink_t +ntohl +ntohs off_t open opendir diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 35194ac0ebc51..0a290b734a3d1 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -1446,6 +1446,23 @@ extern "C" { } +safe_f! { + // It seems htonl, etc are macros on macOS. So we have to reimplement them. So let's + // reimplement them for all UNIX platforms + pub {const} fn htonl(hostlong: u32) -> u32 { + u32::to_be(hostlong) + } + pub {const} fn htons(hostshort: u16) -> u16 { + u16::to_be(hostshort) + } + pub {const} fn ntohl(netlong: u32) -> u32 { + u32::from_be(netlong) + } + pub {const} fn ntohs(netshort: u16) -> u16 { + u16::from_be(netshort) + } +} + cfg_if! { if #[cfg(not(any(target_os = "emscripten", target_os = "android",