From 1feb3542a7263fe4e0f80b1a7f3e29ef781e75ae 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 --- 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 5e84434b46bf4..d09dd1d83f1b8 100644 --- a/libc-test/semver/unix.txt +++ b/libc-test/semver/unix.txt @@ -583,6 +583,8 @@ grantpt group hostent hstrerror +htonl +htons if_indextoname if_nametoindex in6_addr @@ -651,6 +653,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 51984bc2c42c2..9aebe7217d917 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -1367,6 +1367,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",