From 5ec734a67269ff935d384627f16f831d7ca49cc2 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 30 Jan 2025 21:19:20 +0100 Subject: [PATCH 1/5] Add docstrings and deprecations for arpa/inet.h --- src/header/arpa_inet/mod.rs | 47 ++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index 2af943a2fe..d59430db74 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -1,4 +1,6 @@ -//! arpa/inet implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xns/arpainet.h.html +//! `arpa/inet.h` implementation. +//! +//! See . // TODO: set this for entire crate when possible #![deny(unsafe_op_in_unsafe_fn)] @@ -18,32 +20,43 @@ use crate::{ platform::{self, types::*}, }; +/// See . #[no_mangle] pub extern "C" fn htonl(hostlong: uint32_t) -> uint32_t { hostlong.to_be() } +/// See . #[no_mangle] pub extern "C" fn htons(hostshort: uint16_t) -> uint16_t { hostshort.to_be() } +/// See . #[no_mangle] pub extern "C" fn ntohl(netlong: uint32_t) -> uint32_t { u32::from_be(netlong) } +/// See . #[no_mangle] pub extern "C" fn ntohs(netshort: uint16_t) -> uint16_t { u16::from_be(netshort) } +/// Non-POSIX, see . #[no_mangle] pub unsafe extern "C" fn inet_aton(cp: *const c_char, inp: *mut in_addr) -> c_int { // TODO: octal/hex unsafe { inet_pton(AF_INET, cp, inp as *mut c_void) } } +/// See . +/// +/// # Deprecation +/// The `inet_ntoa()` function was marked obsolescent in the Open Group Base +/// Specifications Issue 8. +#[deprecated] #[no_mangle] pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { static mut NTOA_ADDR: [c_char; 16] = [0; 16]; @@ -58,6 +71,7 @@ pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { } } +/// See . #[no_mangle] pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut c_void) -> c_int { if domain != AF_INET { @@ -87,6 +101,7 @@ pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut } } +/// See . #[no_mangle] pub unsafe extern "C" fn inet_ntop( domain: c_int, @@ -115,6 +130,12 @@ pub unsafe extern "C" fn inet_ntop( } } +/// See . +/// +/// # Deprecated +/// The `inet_addr()` function was marked obsolescent in the Open Group Base +/// Specifications Issue 8. +#[deprecated] #[no_mangle] pub unsafe extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { let mut val: in_addr = in_addr { s_addr: 0 }; @@ -126,6 +147,12 @@ pub unsafe extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { } } +/// See . +/// +/// # Deprecation +/// The `inet_lnaof()` function was specified in Networking Services Issue 5, +/// but not in the Open Group Base Specifications Issue 6 and later. +#[deprecated] #[no_mangle] pub extern "C" fn inet_lnaof(input: in_addr) -> in_addr_t { if input.s_addr >> 24 < 128 { @@ -137,6 +164,12 @@ pub extern "C" fn inet_lnaof(input: in_addr) -> in_addr_t { } } +/// See . +/// +/// # Deprecation +/// The `inet_makeaddr()` function was specified in Networking Services Issue +/// 5, but not in the Open Group Base Specifications Issue 6 and later. +#[deprecated] #[no_mangle] pub extern "C" fn inet_makeaddr(net: in_addr_t, host: in_addr_t) -> in_addr { let mut output: in_addr = in_addr { s_addr: 0 }; @@ -152,6 +185,12 @@ pub extern "C" fn inet_makeaddr(net: in_addr_t, host: in_addr_t) -> in_addr { output } +/// See . +/// +/// # Deprecation +/// The `inet_netof()` function was specified in Networking Services Issue 5, +/// but not in the Open Group Base Specifications Issue 6 and later. +#[deprecated] #[no_mangle] pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { if input.s_addr >> 24 < 128 { @@ -163,6 +202,12 @@ pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { } } +/// See . +/// +/// # Deprecation +/// The `inet_network()` function was specified in Networking Services Issue 5, +/// but not in the Open Group Base Specifications Issue 6 and later. +#[deprecated] #[no_mangle] pub unsafe extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { ntohl(unsafe { inet_addr(cp) }) From b36ae4cab588f0a02831542920e80195f9429178 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 30 Jan 2025 21:23:54 +0100 Subject: [PATCH 2/5] Reorder functions alphabetically --- src/header/arpa_inet/mod.rs | 196 ++++++++++++++++++------------------ 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index d59430db74..066c7fd3e1 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -32,104 +32,6 @@ pub extern "C" fn htons(hostshort: uint16_t) -> uint16_t { hostshort.to_be() } -/// See . -#[no_mangle] -pub extern "C" fn ntohl(netlong: uint32_t) -> uint32_t { - u32::from_be(netlong) -} - -/// See . -#[no_mangle] -pub extern "C" fn ntohs(netshort: uint16_t) -> uint16_t { - u16::from_be(netshort) -} - -/// Non-POSIX, see . -#[no_mangle] -pub unsafe extern "C" fn inet_aton(cp: *const c_char, inp: *mut in_addr) -> c_int { - // TODO: octal/hex - unsafe { inet_pton(AF_INET, cp, inp as *mut c_void) } -} - -/// See . -/// -/// # Deprecation -/// The `inet_ntoa()` function was marked obsolescent in the Open Group Base -/// Specifications Issue 8. -#[deprecated] -#[no_mangle] -pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { - static mut NTOA_ADDR: [c_char; 16] = [0; 16]; - - unsafe { - inet_ntop( - AF_INET, - &addr as *const in_addr as *const c_void, - NTOA_ADDR.as_mut_ptr(), - 16, - ) - } -} - -/// See . -#[no_mangle] -pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut c_void) -> c_int { - if domain != AF_INET { - platform::ERRNO.set(EAFNOSUPPORT); - -1 - } else { - let s_addr = unsafe { - slice::from_raw_parts_mut( - &mut (*(dest as *mut in_addr)).s_addr as *mut _ as *mut u8, - 4, - ) - }; - let src_cstr = unsafe { CStr::from_ptr(src) }; - let mut octets = unsafe { str::from_utf8_unchecked(src_cstr.to_bytes()).split('.') }; - for i in 0..4 { - if let Some(n) = octets.next().and_then(|x| u8::from_str(x).ok()) { - s_addr[i] = n; - } else { - return 0; - } - } - if octets.next() == None { - 1 // Success - } else { - 0 - } - } -} - -/// See . -#[no_mangle] -pub unsafe extern "C" fn inet_ntop( - domain: c_int, - src: *const c_void, - dest: *mut c_char, - size: socklen_t, -) -> *const c_char { - if domain != AF_INET { - platform::ERRNO.set(EAFNOSUPPORT); - ptr::null() - } else if size < 16 { - platform::ERRNO.set(ENOSPC); - ptr::null() - } else { - let s_addr = unsafe { - slice::from_raw_parts( - &(*(src as *const in_addr)).s_addr as *const _ as *const u8, - 4, - ) - }; - let addr = format!("{}.{}.{}.{}\0", s_addr[0], s_addr[1], s_addr[2], s_addr[3]); - unsafe { - ptr::copy(addr.as_ptr() as *const c_char, dest, addr.len()); - } - dest - } -} - /// See . /// /// # Deprecated @@ -147,6 +49,13 @@ pub unsafe extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { } } +/// Non-POSIX, see . +#[no_mangle] +pub unsafe extern "C" fn inet_aton(cp: *const c_char, inp: *mut in_addr) -> c_int { + // TODO: octal/hex + unsafe { inet_pton(AF_INET, cp, inp as *mut c_void) } +} + /// See . /// /// # Deprecation @@ -212,3 +121,94 @@ pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { pub unsafe extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { ntohl(unsafe { inet_addr(cp) }) } + +/// See . +/// +/// # Deprecation +/// The `inet_ntoa()` function was marked obsolescent in the Open Group Base +/// Specifications Issue 8. +#[deprecated] +#[no_mangle] +pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { + static mut NTOA_ADDR: [c_char; 16] = [0; 16]; + + unsafe { + inet_ntop( + AF_INET, + &addr as *const in_addr as *const c_void, + NTOA_ADDR.as_mut_ptr(), + 16, + ) + } +} + +/// See . +#[no_mangle] +pub unsafe extern "C" fn inet_ntop( + domain: c_int, + src: *const c_void, + dest: *mut c_char, + size: socklen_t, +) -> *const c_char { + if domain != AF_INET { + platform::ERRNO.set(EAFNOSUPPORT); + ptr::null() + } else if size < 16 { + platform::ERRNO.set(ENOSPC); + ptr::null() + } else { + let s_addr = unsafe { + slice::from_raw_parts( + &(*(src as *const in_addr)).s_addr as *const _ as *const u8, + 4, + ) + }; + let addr = format!("{}.{}.{}.{}\0", s_addr[0], s_addr[1], s_addr[2], s_addr[3]); + unsafe { + ptr::copy(addr.as_ptr() as *const c_char, dest, addr.len()); + } + dest + } +} + +/// See . +#[no_mangle] +pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut c_void) -> c_int { + if domain != AF_INET { + platform::ERRNO.set(EAFNOSUPPORT); + -1 + } else { + let s_addr = unsafe { + slice::from_raw_parts_mut( + &mut (*(dest as *mut in_addr)).s_addr as *mut _ as *mut u8, + 4, + ) + }; + let src_cstr = unsafe { CStr::from_ptr(src) }; + let mut octets = unsafe { str::from_utf8_unchecked(src_cstr.to_bytes()).split('.') }; + for i in 0..4 { + if let Some(n) = octets.next().and_then(|x| u8::from_str(x).ok()) { + s_addr[i] = n; + } else { + return 0; + } + } + if octets.next() == None { + 1 // Success + } else { + 0 + } + } +} + +/// See . +#[no_mangle] +pub extern "C" fn ntohl(netlong: uint32_t) -> uint32_t { + u32::from_be(netlong) +} + +/// See . +#[no_mangle] +pub extern "C" fn ntohs(netshort: uint16_t) -> uint16_t { + u16::from_be(netshort) +} From f4c75e99cdde7afacedde2439105dab7d445cf23 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 30 Jan 2025 21:35:11 +0100 Subject: [PATCH 3/5] Align function parameter names and types with POSIX --- src/header/arpa_inet/mod.rs | 54 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index 066c7fd3e1..57f4a8ef63 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -63,13 +63,13 @@ pub unsafe extern "C" fn inet_aton(cp: *const c_char, inp: *mut in_addr) -> c_in /// but not in the Open Group Base Specifications Issue 6 and later. #[deprecated] #[no_mangle] -pub extern "C" fn inet_lnaof(input: in_addr) -> in_addr_t { - if input.s_addr >> 24 < 128 { - input.s_addr & 0xff_ffff - } else if input.s_addr >> 24 < 192 { - input.s_addr & 0xffff +pub extern "C" fn inet_lnaof(r#in: in_addr) -> in_addr_t { + if r#in.s_addr >> 24 < 128 { + r#in.s_addr & 0xff_ffff + } else if r#in.s_addr >> 24 < 192 { + r#in.s_addr & 0xffff } else { - input.s_addr & 0xff + r#in.s_addr & 0xff } } @@ -80,15 +80,15 @@ pub extern "C" fn inet_lnaof(input: in_addr) -> in_addr_t { /// 5, but not in the Open Group Base Specifications Issue 6 and later. #[deprecated] #[no_mangle] -pub extern "C" fn inet_makeaddr(net: in_addr_t, host: in_addr_t) -> in_addr { +pub extern "C" fn inet_makeaddr(net: in_addr_t, lna: in_addr_t) -> in_addr { let mut output: in_addr = in_addr { s_addr: 0 }; if net < 256 { - output.s_addr = host | net << 24; + output.s_addr = lna | net << 24; } else if net < 65536 { - output.s_addr = host | net << 16; + output.s_addr = lna | net << 16; } else { - output.s_addr = host | net << 8; + output.s_addr = lna | net << 8; } output @@ -101,13 +101,13 @@ pub extern "C" fn inet_makeaddr(net: in_addr_t, host: in_addr_t) -> in_addr { /// but not in the Open Group Base Specifications Issue 6 and later. #[deprecated] #[no_mangle] -pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { - if input.s_addr >> 24 < 128 { - input.s_addr & 0xff_ffff - } else if input.s_addr >> 24 < 192 { - input.s_addr & 0xffff +pub extern "C" fn inet_netof(r#in: in_addr) -> in_addr_t { + if r#in.s_addr >> 24 < 128 { + r#in.s_addr & 0xff_ffff + } else if r#in.s_addr >> 24 < 192 { + r#in.s_addr & 0xffff } else { - input.s_addr & 0xff + r#in.s_addr & 0xff } } @@ -118,7 +118,7 @@ pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { /// but not in the Open Group Base Specifications Issue 6 and later. #[deprecated] #[no_mangle] -pub unsafe extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { +pub unsafe extern "C" fn inet_network(cp: *const c_char) -> in_addr_t { ntohl(unsafe { inet_addr(cp) }) } @@ -129,13 +129,13 @@ pub unsafe extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { /// Specifications Issue 8. #[deprecated] #[no_mangle] -pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { +pub unsafe extern "C" fn inet_ntoa(r#in: in_addr) -> *const c_char { static mut NTOA_ADDR: [c_char; 16] = [0; 16]; unsafe { inet_ntop( AF_INET, - &addr as *const in_addr as *const c_void, + &r#in as *const in_addr as *const c_void, NTOA_ADDR.as_mut_ptr(), 16, ) @@ -145,12 +145,12 @@ pub unsafe extern "C" fn inet_ntoa(addr: in_addr) -> *const c_char { /// See . #[no_mangle] pub unsafe extern "C" fn inet_ntop( - domain: c_int, + af: c_int, src: *const c_void, - dest: *mut c_char, + dst: *mut c_char, size: socklen_t, ) -> *const c_char { - if domain != AF_INET { + if af != AF_INET { platform::ERRNO.set(EAFNOSUPPORT); ptr::null() } else if size < 16 { @@ -165,22 +165,22 @@ pub unsafe extern "C" fn inet_ntop( }; let addr = format!("{}.{}.{}.{}\0", s_addr[0], s_addr[1], s_addr[2], s_addr[3]); unsafe { - ptr::copy(addr.as_ptr() as *const c_char, dest, addr.len()); + ptr::copy(addr.as_ptr() as *const c_char, dst, addr.len()); } - dest + dst } } /// See . #[no_mangle] -pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut c_void) -> c_int { - if domain != AF_INET { +pub unsafe extern "C" fn inet_pton(af: c_int, src: *const c_char, dst: *mut c_void) -> c_int { + if af != AF_INET { platform::ERRNO.set(EAFNOSUPPORT); -1 } else { let s_addr = unsafe { slice::from_raw_parts_mut( - &mut (*(dest as *mut in_addr)).s_addr as *mut _ as *mut u8, + &mut (*(dst as *mut in_addr)).s_addr as *mut _ as *mut u8, 4, ) }; From a2857286101b7d9a4279cc0ac57e8ab069b759a6 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 30 Jan 2025 21:35:43 +0100 Subject: [PATCH 4/5] Formatting --- src/header/arpa_inet/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index 57f4a8ef63..848b8022ed 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -179,10 +179,7 @@ pub unsafe extern "C" fn inet_pton(af: c_int, src: *const c_char, dst: *mut c_vo -1 } else { let s_addr = unsafe { - slice::from_raw_parts_mut( - &mut (*(dst as *mut in_addr)).s_addr as *mut _ as *mut u8, - 4, - ) + slice::from_raw_parts_mut(&mut (*(dst as *mut in_addr)).s_addr as *mut _ as *mut u8, 4) }; let src_cstr = unsafe { CStr::from_ptr(src) }; let mut octets = unsafe { str::from_utf8_unchecked(src_cstr.to_bytes()).split('.') }; From 38d22bd7359c56fa8a33d41ff14d6cc10b42ec7a Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 30 Jan 2025 21:41:52 +0100 Subject: [PATCH 5/5] Suppress warnings about deprecated declarations in C --- tests/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Makefile b/tests/Makefile index a1fcd5b78f..d711f049d5 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -275,6 +275,7 @@ FLAGS=\ -Wall \ -Wextra \ -Werror \ + -Wno-deprecated-declarations \ -pedantic \ -g \ -I .