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

Merged
merged 1 commit into from
Jul 24, 2025
Merged
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,
)
}),
}
}
}
18 changes: 18 additions & 0 deletions library/std/src/sys/net/connection/uefi/tcp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ 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> {
let mut config_data = tcp4::ConfigData::default();
let protocol = self.protocol.as_ptr();

let r = unsafe {
((*protocol).get_mode_data)(
protocol,
crate::ptr::null_mut(),
&mut config_data,
crate::ptr::null_mut(),
crate::ptr::null_mut(),
crate::ptr::null_mut(),
)
};

if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(config_data) }
}

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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this could be simplified to crate::net::Ipv4Addr::from(ip.addr) (see https://doc.rust-lang.org/std/net/struct.Ipv4Addr.html#impl-From%3C%5Bu8;+4%5D%3E-for-Ipv4Addr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So From is not constant. I would prefer to keep this function constant.
There does exist from_octets, but that's experimental right now. I can enable the feature, but well I just thought this way might be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep fine to leave as-is, I missed that it's constant.

}
Loading