Skip to content

Commit 46e3178

Browse files
authored
Merge pull request #3669 from safinaskar/htons
Add htonl, htons, ntohl, ntohs
2 parents 8583c71 + 1feb354 commit 46e3178

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

libc-test/semver/unix.txt

+4
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ grantpt
585585
group
586586
hostent
587587
hstrerror
588+
htonl
589+
htons
588590
if_indextoname
589591
if_nametoindex
590592
in6_addr
@@ -655,6 +657,8 @@ munmap
655657
nanosleep
656658
nfds_t
657659
nlink_t
660+
ntohl
661+
ntohs
658662
off_t
659663
open
660664
opendir

src/unix/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,23 @@ extern "C" {
13981398

13991399
}
14001400

1401+
safe_f! {
1402+
// It seems htonl, etc are macros on macOS. So we have to reimplement them. So let's
1403+
// reimplement them for all UNIX platforms
1404+
pub {const} fn htonl(hostlong: u32) -> u32 {
1405+
u32::to_be(hostlong)
1406+
}
1407+
pub {const} fn htons(hostshort: u16) -> u16 {
1408+
u16::to_be(hostshort)
1409+
}
1410+
pub {const} fn ntohl(netlong: u32) -> u32 {
1411+
u32::from_be(netlong)
1412+
}
1413+
pub {const} fn ntohs(netshort: u16) -> u16 {
1414+
u16::from_be(netshort)
1415+
}
1416+
}
1417+
14011418
cfg_if! {
14021419
if #[cfg(not(any(target_os = "emscripten",
14031420
target_os = "android",

0 commit comments

Comments
 (0)