Skip to content

Commit

Permalink
Merge pull request #3669 from safinaskar/htons
Browse files Browse the repository at this point in the history
Add htonl, htons, ntohl, ntohs
  • Loading branch information
tgross35 authored Aug 16, 2024
2 parents 8583c71 + 1feb354 commit 46e3178
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libc-test/semver/unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ grantpt
group
hostent
hstrerror
htonl
htons
if_indextoname
if_nametoindex
in6_addr
Expand Down Expand Up @@ -655,6 +657,8 @@ munmap
nanosleep
nfds_t
nlink_t
ntohl
ntohs
off_t
open
opendir
Expand Down
17 changes: 17 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,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",
Expand Down

0 comments on commit 46e3178

Please sign in to comment.