Skip to content

Commit 0a0f27c

Browse files
Askar Safintgross35
authored andcommitted
Add htonl, htons, ntohl, ntohs
(backport <#3669>) (cherry picked from commit 1feb354)
1 parent 65c9072 commit 0a0f27c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

libc-test/semver/unix.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ grantpt
588588
group
589589
hostent
590590
hstrerror
591+
htonl
592+
htons
591593
if_indextoname
592594
if_nametoindex
593595
in6_addr
@@ -658,6 +660,8 @@ munmap
658660
nanosleep
659661
nfds_t
660662
nlink_t
663+
ntohl
664+
ntohs
661665
off_t
662666
open
663667
opendir

src/unix/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,23 @@ extern "C" {
14461446

14471447
}
14481448

1449+
safe_f! {
1450+
// It seems htonl, etc are macros on macOS. So we have to reimplement them. So let's
1451+
// reimplement them for all UNIX platforms
1452+
pub {const} fn htonl(hostlong: u32) -> u32 {
1453+
u32::to_be(hostlong)
1454+
}
1455+
pub {const} fn htons(hostshort: u16) -> u16 {
1456+
u16::to_be(hostshort)
1457+
}
1458+
pub {const} fn ntohl(netlong: u32) -> u32 {
1459+
u32::from_be(netlong)
1460+
}
1461+
pub {const} fn ntohs(netshort: u16) -> u16 {
1462+
u16::from_be(netshort)
1463+
}
1464+
}
1465+
14491466
cfg_if! {
14501467
if #[cfg(not(any(target_os = "emscripten",
14511468
target_os = "android",

0 commit comments

Comments
 (0)