scheme-utils: Introduce FpathWriter helper

This commit is contained in:
bjorn3
2026-04-19 14:35:33 +02:00
parent 648939b5b8
commit 64d23e9e05
24 changed files with 267 additions and 407 deletions
+12 -26
View File
@@ -1,3 +1,4 @@
use scheme_utils::FpathWriter;
use smoltcp::iface::SocketHandle;
use smoltcp::socket::icmp::{
Endpoint as IcmpEndpoint, PacketBuffer as IcmpSocketBuffer,
@@ -249,36 +250,21 @@ impl<'a> SchemeSocket for IcmpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
if let SchemeFile::Socket(ref socket_file) = *file {
match socket_file.data.socket_type {
IcmpSocketType::Echo => {
let path = format!("/scheme/icmp/echo/{}", socket_file.data.ip);
let path = path.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
FpathWriter::with(buf, |w| {
if let SchemeFile::Socket(ref socket_file) = *file {
match socket_file.data.socket_type {
IcmpSocketType::Echo => {
write!(w, "/scheme/icmp/echo/{}", socket_file.data.ip).unwrap();
}
Ok(i)
}
IcmpSocketType::Udp => {
let path = format!("/scheme/icmp/udp/{}", socket_file.data.ip);
let path = path.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
IcmpSocketType::Udp => {
write!(w, "/scheme/icmp/udp/{}", socket_file.data.ip).unwrap();
}
Ok(i)
}
Ok(())
} else {
Err(SyscallError::new(syscall::EBADF))
}
} else {
Err(SyscallError::new(syscall::EBADF))
}
})
}
fn handle_recvmsg(
+5 -10
View File
@@ -1,3 +1,4 @@
use scheme_utils::FpathWriter;
use smoltcp::iface::SocketHandle;
use smoltcp::socket::raw::{
PacketBuffer as RawSocketBuffer, PacketMetadata as RawPacketMetadata, Socket as RawSocket,
@@ -137,16 +138,10 @@ impl<'a> SchemeSocket for RawSocket<'a> {
}
fn fpath(&self, _file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
let path = format!("/scheme/ip/{}", self.ip_protocol());
let path = path.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
}
Ok(i)
FpathWriter::with(buf, |w| {
write!(w, "/scheme/ip/{}", self.ip_protocol()).unwrap();
Ok(())
})
}
fn handle_recvmsg(
+27 -33
View File
@@ -1,7 +1,7 @@
use scheme_utils::FpathWriter;
use smoltcp::iface::SocketHandle;
use smoltcp::socket::tcp::{Socket as TcpSocket, SocketBuffer as TcpSocketBuffer};
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
use std::fmt::Write;
use std::str;
use syscall;
use syscall::{Error as SyscallError, Result as SyscallResult};
@@ -273,40 +273,34 @@ impl<'a> SchemeSocket for TcpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
let unspecified = "0.0.0.0:0";
let mut path = String::from("/scheme/tcp/");
match self.remote_endpoint() {
Some(endpoint) => write!(&mut path, "{}", endpoint).unwrap(),
None => path.push_str(unspecified),
}
path.push('/');
match (self.local_endpoint(), file) {
(Some(endpoint), _) => write!(&mut path, "{}", endpoint).unwrap(),
(
None,
SchemeFile::Socket(SocketFile {
data: Some(endpoint),
..
}),
) => {
if endpoint.is_specified() {
write!(&mut path, "{}", endpoint).unwrap()
} else {
write!(&mut path, "0.0.0.0:{}", endpoint.port).unwrap()
}
FpathWriter::with(buf, |w| {
let unspecified = "0.0.0.0:0";
write!(w, "/scheme/tcp/").unwrap();
match self.remote_endpoint() {
Some(endpoint) => write!(w, "{}", endpoint).unwrap(),
None => w.push_str(unspecified),
}
w.push_str("/");
match (self.local_endpoint(), file) {
(Some(endpoint), _) => write!(w, "{}", endpoint).unwrap(),
(
None,
SchemeFile::Socket(SocketFile {
data: Some(endpoint),
..
}),
) => {
if endpoint.is_specified() {
write!(w, "{}", endpoint).unwrap()
} else {
write!(w, "0.0.0.0:{}", endpoint.port).unwrap()
}
}
_ => w.push_str(unspecified),
}
_ => path.push_str(unspecified),
}
trace!("fpath: {}", path);
let path = path.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
}
Ok(i)
Ok(())
})
}
fn handle_recvmsg(
+23 -28
View File
@@ -1,3 +1,4 @@
use scheme_utils::FpathWriter;
use smoltcp::iface::SocketHandle;
use smoltcp::socket::udp::{
PacketBuffer as UdpSocketBuffer, PacketMetadata as UdpPacketMetadata, Socket as UdpSocket,
@@ -12,7 +13,6 @@ use super::{parse_endpoint, SchemeWrapper, Smolnetd, SocketSet};
use crate::port_set::PortSet;
use crate::router::Router;
use libredox::flag;
use std::fmt::Write;
const SO_SNDBUF: usize = 7;
const SO_RCVBUF: usize = 8;
@@ -269,37 +269,32 @@ impl<'a> SchemeSocket for UdpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
let unspecified = "0.0.0.0:0";
let mut path = String::from("/scheme/udp/");
FpathWriter::with(buf, |w| {
let unspecified = "0.0.0.0:0";
w.push_str("/scheme/udp/");
// remote
match file {
SchemeFile::Socket(SocketFile { data: endpoint, .. }) => {
if endpoint.is_specified() {
write!(&mut path, "{}", endpoint).unwrap()
} else {
write!(&mut path, "0.0.0.0:{}", endpoint.port).unwrap()
// remote
match file {
SchemeFile::Socket(SocketFile { data: endpoint, .. }) => {
if endpoint.is_specified() {
write!(w, "{}", endpoint).unwrap()
} else {
write!(w, "0.0.0.0:{}", endpoint.port).unwrap()
}
}
_ => w.push_str(unspecified),
}
w.push_str("/");
// local
let endpoint = self.endpoint();
if endpoint.is_specified() {
write!(w, "{}", endpoint).unwrap()
} else {
write!(w, "0.0.0.0:{}", endpoint.port).unwrap()
}
_ => path.push_str(unspecified),
}
path.push('/');
// local
let endpoint = self.endpoint();
if endpoint.is_specified() {
write!(&mut path, "{}", endpoint).unwrap()
} else {
write!(&mut path, "0.0.0.0:{}", endpoint.port).unwrap()
}
let path = path.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
}
Ok(i)
Ok(())
})
}
fn handle_recvmsg(