diff --git a/library/std/src/sys/net/connection/uefi/mod.rs b/library/std/src/sys/net/connection/uefi/mod.rs index 884cbd4ac1dc7..16e3487a1747f 100644 --- a/library/std/src/sys/net/connection/uefi/mod.rs +++ b/library/std/src/sys/net/connection/uefi/mod.rs @@ -86,11 +86,11 @@ impl TcpStream { } pub fn peer_addr(&self) -> io::Result { - unsupported() + self.inner.peer_addr() } pub fn socket_addr(&self) -> io::Result { - unsupported() + self.inner.socket_addr() } pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { @@ -114,7 +114,7 @@ impl TcpStream { } pub fn nodelay(&self) -> io::Result { - unsupported() + self.inner.nodelay() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { @@ -122,7 +122,7 @@ impl TcpStream { } pub fn ttl(&self) -> io::Result { - unsupported() + self.inner.ttl() } pub fn take_error(&self) -> io::Result> { @@ -140,7 +140,9 @@ impl fmt::Debug for TcpStream { } } -pub struct TcpListener(!); +pub struct TcpListener { + inner: tcp::Tcp, +} impl TcpListener { pub fn bind(_: io::Result<&SocketAddr>) -> io::Result { @@ -148,45 +150,45 @@ impl TcpListener { } pub fn socket_addr(&self) -> io::Result { - self.0 + unsupported() } pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { - self.0 + unsupported() } pub fn duplicate(&self) -> io::Result { - self.0 + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - self.0 + unsupported() } pub fn ttl(&self) -> io::Result { - self.0 + self.inner.ttl() } pub fn set_only_v6(&self, _: bool) -> io::Result<()> { - self.0 + unsupported() } pub fn only_v6(&self) -> io::Result { - self.0 + unsupported() } pub fn take_error(&self) -> io::Result> { - self.0 + unsupported() } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - self.0 + unsupported() } } impl fmt::Debug for TcpListener { fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0 + todo!() } } diff --git a/library/std/src/sys/net/connection/uefi/tcp.rs b/library/std/src/sys/net/connection/uefi/tcp.rs index 1152f69446e42..aac97007bbfe5 100644 --- a/library/std/src/sys/net/connection/uefi/tcp.rs +++ b/library/std/src/sys/net/connection/uefi/tcp.rs @@ -1,6 +1,8 @@ use super::tcp4; use crate::io; use crate::net::SocketAddr; +use crate::ptr::NonNull; +use crate::sys::{helpers, unsupported}; use crate::time::Duration; pub(crate) enum Tcp { @@ -31,4 +33,44 @@ impl Tcp { Self::V4(client) => client.read(buf, timeout), } } + + pub(crate) fn ttl(&self) -> io::Result { + match self { + Self::V4(client) => client.get_mode_data().map(|x| x.time_to_live.into()), + } + } + + pub(crate) fn nodelay(&self) -> io::Result { + match self { + Self::V4(client) => { + let temp = client.get_mode_data()?; + match NonNull::new(temp.control_option) { + Some(x) => unsafe { Ok(x.as_ref().enable_nagle.into()) }, + None => unsupported(), + } + } + } + } + + pub fn peer_addr(&self) -> io::Result { + match self { + Self::V4(client) => client.get_mode_data().map(|x| { + SocketAddr::new( + helpers::ipv4_from_r_efi(x.access_point.remote_address).into(), + x.access_point.remote_port, + ) + }), + } + } + + pub fn socket_addr(&self) -> io::Result { + match self { + Self::V4(client) => client.get_mode_data().map(|x| { + SocketAddr::new( + helpers::ipv4_from_r_efi(x.access_point.station_address).into(), + x.access_point.station_port, + ) + }), + } + } } diff --git a/library/std/src/sys/net/connection/uefi/tcp4.rs b/library/std/src/sys/net/connection/uefi/tcp4.rs index 6342718929a7d..f7b84ab4e4983 100644 --- a/library/std/src/sys/net/connection/uefi/tcp4.rs +++ b/library/std/src/sys/net/connection/uefi/tcp4.rs @@ -2,6 +2,7 @@ use r_efi::efi::{self, Status}; use r_efi::protocols::tcp4; use crate::io; +use crate::mem::MaybeUninit; use crate::net::SocketAddrV4; use crate::ptr::NonNull; use crate::sync::atomic::{AtomicBool, Ordering}; @@ -67,6 +68,29 @@ impl Tcp4 { if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) } } + pub(crate) fn get_mode_data(&self) -> io::Result { + // Using MaybeUninit::uninit() generates a Page Fault Here + let mut config_data: MaybeUninit = MaybeUninit::zeroed(); + let protocol = self.protocol.as_ptr(); + + let r = unsafe { + ((*protocol).get_mode_data)( + protocol, + crate::ptr::null_mut(), + config_data.as_mut_ptr(), + crate::ptr::null_mut(), + crate::ptr::null_mut(), + crate::ptr::null_mut(), + ) + }; + + if r.is_error() { + return Err(io::Error::from_raw_os_error(r.as_usize())); + } else { + Ok(unsafe { config_data.assume_init() }) + } + } + pub(crate) fn connect(&self, timeout: Option) -> io::Result<()> { let evt = unsafe { self.create_evt() }?; let completion_token = diff --git a/library/std/src/sys/pal/uefi/helpers.rs b/library/std/src/sys/pal/uefi/helpers.rs index 420481648a7d5..271dc4d11de75 100644 --- a/library/std/src/sys/pal/uefi/helpers.rs +++ b/library/std/src/sys/pal/uefi/helpers.rs @@ -761,3 +761,7 @@ impl Drop for OwnedEvent { pub(crate) const fn ipv4_to_r_efi(addr: crate::net::Ipv4Addr) -> efi::Ipv4Address { efi::Ipv4Address { addr: addr.octets() } } + +pub(crate) const fn ipv4_from_r_efi(ip: efi::Ipv4Address) -> crate::net::Ipv4Addr { + crate::net::Ipv4Addr::new(ip.addr[0], ip.addr[1], ip.addr[2], ip.addr[3]) +}