Skip to content

std: net: uefi: Add support to query connection data #143838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions library/std/src/sys/net/connection/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ impl TcpStream {
}

pub fn peer_addr(&self) -> io::Result<SocketAddr> {
unsupported()
self.inner.peer_addr()
}

pub fn socket_addr(&self) -> io::Result<SocketAddr> {
unsupported()
self.inner.socket_addr()
}

pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
Expand All @@ -114,15 +114,15 @@ impl TcpStream {
}

pub fn nodelay(&self) -> io::Result<bool> {
unsupported()
self.inner.nodelay()
}

pub fn set_ttl(&self, _: u32) -> io::Result<()> {
unsupported()
}

pub fn ttl(&self) -> io::Result<u32> {
unsupported()
self.inner.ttl()
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
Expand All @@ -140,53 +140,55 @@ impl fmt::Debug for TcpStream {
}
}

pub struct TcpListener(!);
pub struct TcpListener {
inner: tcp::Tcp,
}

impl TcpListener {
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
unsupported()
}

pub fn socket_addr(&self) -> io::Result<SocketAddr> {
self.0
unsupported()
}

pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
self.0
unsupported()
}

pub fn duplicate(&self) -> io::Result<TcpListener> {
self.0
unsupported()
}

pub fn set_ttl(&self, _: u32) -> io::Result<()> {
self.0
unsupported()
}

pub fn ttl(&self) -> io::Result<u32> {
self.0
self.inner.ttl()
}

pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
self.0
unsupported()
}

pub fn only_v6(&self) -> io::Result<bool> {
self.0
unsupported()
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
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!()
}
}

Expand Down
42 changes: 42 additions & 0 deletions library/std/src/sys/net/connection/uefi/tcp.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -31,4 +33,44 @@ impl Tcp {
Self::V4(client) => client.read(buf, timeout),
}
}

pub(crate) fn ttl(&self) -> io::Result<u32> {
match self {
Self::V4(client) => client.get_mode_data().map(|x| x.time_to_live.into()),
}
}

pub(crate) fn nodelay(&self) -> io::Result<bool> {
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<SocketAddr> {
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<SocketAddr> {
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,
)
}),
}
}
}
24 changes: 24 additions & 0 deletions library/std/src/sys/net/connection/uefi/tcp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<tcp4::ConfigData> {
// Using MaybeUninit::uninit() generates a Page Fault Here
let mut config_data: MaybeUninit<tcp4::ConfigData> = 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<Duration>) -> io::Result<()> {
let evt = unsafe { self.create_evt() }?;
let completion_token =
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Loading