diff --git a/src/header/arpa_inet/cbindgen.toml b/src/header/arpa_inet/cbindgen.toml index a6b683dde2..e6205e3805 100644 --- a/src/header/arpa_inet/cbindgen.toml +++ b/src/header/arpa_inet/cbindgen.toml @@ -3,18 +3,20 @@ # Spec quotations relating to includes: # - "The header shall define the in_port_t and in_addr_t types as described in and the socklen_t type as defined in ." # - "The header shall define the in_addr structure as described in ." -# - "The header shall define the INET_ADDRSTRLEN [IP6] and INET6_ADDRSTRLEN macros as described in ." +# - "The header shall define the INET_ADDRSTRLEN and INET6_ADDRSTRLEN macros as described in ." # - "The header shall define the uint32_t and uint16_t types as described in ." # - "Inclusion of the header may also make visible all symbols from and ." # -# features.h included for #[deprecated] annotation -sys_includes = [ - "inttypes.h", - "netinet/in.h", - "features.h" -] +# Possible cycle between arpa/inet.h and netinet/in.h solved by: +# - including netinet/in.h in arpa/inet.h +# - splitting out functions (htonl, htons, ntohl, ntohs) into bits/arpainet.h +# +# netinet/in.h brings in sys/types.h which brings in features.h (needed for #[deprecated] annotation) +# bits/arpainet.h brings in inttypes.h +sys_includes = ["netinet/in.h"] include_guard = "_ARPA_INET_H" after_includes = """ +#include // for htonl, htons, ntohl, ntohs #include // for socklen_t """ language = "C" diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index abab965675..838f71f2ff 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -10,6 +10,7 @@ use core::{ use crate::{ c_str::CStr, header::{ + bits_arpainet::ntohl, bits_socklen_t::socklen_t, errno::{EAFNOSUPPORT, ENOSPC}, netinet_in::{INADDR_NONE, in_addr, in_addr_t}, @@ -17,7 +18,7 @@ use crate::{ }, platform::{ self, - types::{c_char, c_int, c_void, uint16_t, uint32_t}, + types::{c_char, c_int, c_void}, }, raw_cell::RawCell, }; @@ -231,27 +232,3 @@ pub unsafe extern "C" fn inet_pton(af: c_int, src: *const c_char, dst: *mut c_vo } } } - -/// See . -#[unsafe(no_mangle)] -pub extern "C" fn htonl(hostlong: uint32_t) -> uint32_t { - hostlong.to_be() -} - -/// See . -#[unsafe(no_mangle)] -pub extern "C" fn htons(hostshort: uint16_t) -> uint16_t { - hostshort.to_be() -} - -/// See . -#[unsafe(no_mangle)] -pub extern "C" fn ntohl(netlong: uint32_t) -> uint32_t { - u32::from_be(netlong) -} - -/// See . -#[unsafe(no_mangle)] -pub extern "C" fn ntohs(netshort: uint16_t) -> uint16_t { - u16::from_be(netshort) -} diff --git a/src/header/bits_arpainet/cbindgen.toml b/src/header/bits_arpainet/cbindgen.toml new file mode 100644 index 0000000000..2db885b93e --- /dev/null +++ b/src/header/bits_arpainet/cbindgen.toml @@ -0,0 +1,19 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/arpa_inet.h.html +# +# The arpa/inet header should define the following functions: +# - htonl +# - htons +# - ntohl +# - ntohs +# +# They are also meant to be available in the netinet/in header. +# The arpa/inet and netinet/in headers both say that they may include each other which creates a cycle. +# To break the cycle we include netinet/in in the arpa/inet header and split out the above functions. +# +# include inttypes.h to get uint16_t and uint32_t +sys_includes = ["inttypes.h"] +include_guard = "_RELIBC_BITS_ARPAINET_H" +language = "C" +style = "Tag" +no_includes = true +cpp_compat = true diff --git a/src/header/bits_arpainet/mod.rs b/src/header/bits_arpainet/mod.rs new file mode 100644 index 0000000000..176b4d7958 --- /dev/null +++ b/src/header/bits_arpainet/mod.rs @@ -0,0 +1,25 @@ +use crate::platform::types::{uint16_t, uint32_t}; + +/// See . +#[unsafe(no_mangle)] +pub extern "C" fn htonl(hostlong: uint32_t) -> uint32_t { + hostlong.to_be() +} + +/// See . +#[unsafe(no_mangle)] +pub extern "C" fn htons(hostshort: uint16_t) -> uint16_t { + hostshort.to_be() +} + +/// See . +#[unsafe(no_mangle)] +pub extern "C" fn ntohl(netlong: uint32_t) -> uint32_t { + u32::from_be(netlong) +} + +/// See . +#[unsafe(no_mangle)] +pub extern "C" fn ntohs(netshort: uint16_t) -> uint16_t { + u16::from_be(netshort) +} diff --git a/src/header/mod.rs b/src/header/mod.rs index 5cfbdd3268..37b6104264 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -4,6 +4,7 @@ pub mod _aio; pub mod _fenv; pub mod arpa_inet; pub mod assert; +pub mod bits_arpainet; #[path = "bits_locale-t/mod.rs"] pub mod bits_locale_t; pub mod bits_pthread; diff --git a/src/header/netdb/lookup.rs b/src/header/netdb/lookup.rs index 627d39c7b6..e0358c6b22 100644 --- a/src/header/netdb/lookup.rs +++ b/src/header/netdb/lookup.rs @@ -10,7 +10,7 @@ use crate::{ }; use crate::header::{ - arpa_inet::htons, + bits_arpainet::htons, bits_socklen_t::socklen_t, bits_time::timespec, errno::*, diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs index 4e7b30f7b8..2d3778df16 100644 --- a/src/header/netdb/mod.rs +++ b/src/header/netdb/mod.rs @@ -12,7 +12,8 @@ use crate::{ c_str::{CStr, CString}, error::ResultExt, header::{ - arpa_inet::{htons, inet_aton, ntohl}, + arpa_inet::inet_aton, + bits_arpainet::{htons, ntohl}, bits_socklen_t::socklen_t, errno::*, fcntl::O_RDONLY, diff --git a/src/header/netinet_in/cbindgen.toml b/src/header/netinet_in/cbindgen.toml index 651700f05d..af2b99e690 100644 --- a/src/header/netinet_in/cbindgen.toml +++ b/src/header/netinet_in/cbindgen.toml @@ -1,4 +1,24 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/netinet_in.h.html +# +# Spec quotations relating to includes: +# - "The header shall define in_port_t Equivalent to the type uint16_t as described in ." +# - "The header shall define in_addr_t Equivalent to the type uint32_t as described in ." +# - "The header shall define the sa_family_t type as described in ." +# - "The header shall define the uint8_t and uint32_t types as described in ." +# - "Inclusion of the header may also make visible all symbols from and ." +# - "The htonl(), htons(), ntohl(), and ntohs() functions shall be available as described in ." +# - "Inclusion of the header may also make visible all symbols from ." +# +# Possible cycle between arpa/inet.h and netinet/in.h solved by: +# - including netinet/in.h in arpa/inet.h +# - splitting out functions (htonl, htons, ntohl, ntohs) into bits/arpainet.h +# +# sys/types.h needed for c_int and c_char +# bits/arpainet.h brings in inttypes.h sys_includes = ["sys/types.h", "sys/socket.h"] +after_includes = """ +#include // for htonl, htons, ntohl, ntohs +""" include_guard = "_NETINET_IN_H" trailer = "#include " language = "C" diff --git a/tests/sys_socket/recvfrom.c b/tests/sys_socket/recvfrom.c index f4975aeafd..2b57597280 100644 --- a/tests/sys_socket/recvfrom.c +++ b/tests/sys_socket/recvfrom.c @@ -1,6 +1,5 @@ #include -#include #include #include