Skip to content

Commit

Permalink
fix: ruxnet
Browse files Browse the repository at this point in the history
  • Loading branch information
PKTH-Jx committed Feb 17, 2025
1 parent 635a746 commit 80d61bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 16 additions & 13 deletions api/ruxos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::{sync::Arc, vec, vec::Vec};
use core::ffi::{c_char, c_int, c_void};
use core::mem::size_of;
use core::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
use ruxfs::fops;
use ruxfs::AbsPath;

use axerrno::{LinuxError, LinuxResult};
use axio::PollState;
Expand All @@ -20,7 +20,6 @@ use ruxfdtable::{FileLike, RuxStat};
use ruxnet::{SocketAddrUnix, TcpSocket, UdpSocket, UnixSocket, UnixSocketType};

use crate::ctypes;
use crate::imp::fs::flags_to_options;
use crate::utils::char_ptr_to_str;

fn addrun_convert(addr: *const ctypes::sockaddr_un) -> SocketAddrUnix {
Expand Down Expand Up @@ -62,8 +61,8 @@ pub enum Socket {
}

impl Socket {
fn add_to_fd_table(self, flags: fops::OpenOptions) -> LinuxResult<c_int> {
ruxtask::fs::add_file_like(Arc::new(self), flags)
fn add_to_fd_table(self, cloexec: bool) -> LinuxResult<c_int> {
ruxtask::fs::add_file_like(Arc::new(self), cloexec)
}

fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> {
Expand Down Expand Up @@ -266,6 +265,10 @@ impl Socket {
}

impl FileLike for Socket {
fn path(&self) -> AbsPath {
AbsPath::new("/dev/socket")
}

fn read(&self, buf: &mut [u8]) -> LinuxResult<usize> {
self.recv(buf, 0)
}
Expand Down Expand Up @@ -390,7 +393,7 @@ fn from_sockaddr(
pub fn sys_socket(domain: c_int, socktype: c_int, protocol: c_int) -> c_int {
debug!("sys_socket <= {} {} {}", domain, socktype, protocol);
let (domain, socktype, protocol) = (domain as u32, socktype as u32, protocol as u32);
let fdflags = flags_to_options((socktype & ctypes::SOCK_CLOEXEC) as c_int, 0);
let cloexec = (socktype & ctypes::SOCK_CLOEXEC) != 0;
let nonblock = (socktype & ctypes::SOCK_NONBLOCK) != 0;
let socktype = socktype & !ctypes::SOCK_CLOEXEC & !ctypes::SOCK_NONBLOCK;
syscall_body!(sys_socket, {
Expand All @@ -401,21 +404,21 @@ pub fn sys_socket(domain: c_int, socktype: c_int, protocol: c_int) -> c_int {
if nonblock {
tcp_socket.set_nonblocking(true);
}
Socket::Tcp(Mutex::new(tcp_socket)).add_to_fd_table(fdflags)
Socket::Tcp(Mutex::new(tcp_socket)).add_to_fd_table(cloexec)
}
(ctypes::SOCK_DGRAM, ctypes::IPPROTO_UDP) | (ctypes::SOCK_DGRAM, 0) => {
Socket::Udp(Mutex::new(UdpSocket::new())).add_to_fd_table(fdflags)
Socket::Udp(Mutex::new(UdpSocket::new())).add_to_fd_table(cloexec)
}
_ => Err(LinuxError::EINVAL),
},
ctypes::AF_UNIX => match (socktype, protocol) {
(ctypes::SOCK_STREAM, 0) => {
Socket::Unix(Mutex::new(UnixSocket::new(UnixSocketType::SockStream)))
.add_to_fd_table(fdflags)
.add_to_fd_table(cloexec)
}
(ctypes::SOCK_DGRAM, 0) => {
Socket::Unix(Mutex::new(UnixSocket::new(UnixSocketType::SockDgram)))
.add_to_fd_table(fdflags)
.add_to_fd_table(cloexec)
}
_ => Err(LinuxError::EINVAL),
},
Expand Down Expand Up @@ -645,7 +648,7 @@ pub unsafe fn sys_accept(
let socket = Socket::from_fd(socket_fd)?;
let new_socket = socket.accept()?;
let addr = new_socket.peer_addr()?;
let new_fd = Socket::add_to_fd_table(new_socket, fops::OpenOptions::new())?;
let new_fd = Socket::add_to_fd_table(new_socket, false)?;
match addr {
UnifiedSocketAddress::Net(addr) => unsafe {
(*socket_addr, *socket_len) = into_sockaddr(addr);
Expand Down Expand Up @@ -951,7 +954,7 @@ pub fn sys_socketpair(domain: c_int, socktype: c_int, protocol: c_int, sv: &mut
info!("sys_socketpair <= domain: {domain}, socktype: {socktype}, protocol: {protocol}, sv pointer: {:#x}", sv.as_ptr() as usize);
syscall_body!(sys_socketpair, {
let (domain, socktype, _protocol) = (domain as u32, socktype as u32, protocol as u32);
let fdflags = flags_to_options((socktype & ctypes::SOCK_CLOEXEC) as c_int, 0);
let cloexec = socktype & ctypes::SOCK_CLOEXEC != 0;
let socktype = socktype & !ctypes::SOCK_CLOEXEC & !ctypes::SOCK_NONBLOCK;
match domain {
ctypes::AF_UNIX => {
Expand All @@ -964,8 +967,8 @@ pub fn sys_socketpair(domain: c_int, socktype: c_int, protocol: c_int, sv: &mut
}
_ => return Err(LinuxError::EAFNOSUPPORT),
};
sv[0] = Socket::Unix(Mutex::new(sk1)).add_to_fd_table(fdflags.clone())?;
sv[1] = Socket::Unix(Mutex::new(sk2)).add_to_fd_table(fdflags)?;
sv[0] = Socket::Unix(Mutex::new(sk1)).add_to_fd_table(cloexec)?;
sv[1] = Socket::Unix(Mutex::new(sk2)).add_to_fd_table(cloexec)?;
info!("create sv[0] {}, sv[1] {}", sv[0], sv[1]);
Ok(0)
}
Expand Down
6 changes: 3 additions & 3 deletions modules/ruxnet/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hashbrown::HashMap;
use lazy_init::LazyInit;
use smoltcp::socket::tcp::SocketBuffer;

use ruxfs::root::{create_file, lookup};
use ruxfs::{AbsPath, fops::{create_file, lookup}};
use ruxtask::yield_now;

const SOCK_ADDR_UN_PATH_LEN: usize = 108;
Expand Down Expand Up @@ -165,7 +165,7 @@ fn get_inode(addr: SocketAddrUnix) -> AxResult<usize> {
.to_str()
.expect("Invalid UTF-8 string")
};
let _vfsnode = match lookup(None, socket_path) {
let _vfsnode = match lookup(&AbsPath::new_canonicalized(socket_path)) {
Ok(node) => node,
Err(_) => {
return Err(AxError::NotFound);
Expand All @@ -183,7 +183,7 @@ fn create_socket_file(addr: SocketAddrUnix) -> AxResult<usize> {
.to_str()
.expect("Invalid UTF-8 string")
};
let _vfsnode = create_file(None, socket_path)?;
let _vfsnode = create_file(&AbsPath::new_canonicalized(socket_path))?;
Err(AxError::Unsupported)
}

Expand Down

0 comments on commit 80d61bd

Please sign in to comment.