diff --git a/cykusz-rs/src/kernel/net/socket.rs b/cykusz-rs/src/kernel/net/socket.rs index 4647b614..79af13cb 100644 --- a/cykusz-rs/src/kernel/net/socket.rs +++ b/cykusz-rs/src/kernel/net/socket.rs @@ -1,6 +1,8 @@ use alloc::sync::Arc; -use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockFlags, SockOption, SockType, SockTypeFlags}; +use syscall_defs::net::{ + MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockFlags, SockOption, SockType, SockTypeFlags, +}; use syscall_defs::SyscallError::ENOTSUP; use syscall_defs::{SyscallError, SyscallResult}; @@ -9,7 +11,12 @@ use crate::kernel::net::ip::{Ip, Ip4}; use crate::kernel::net::Packet; pub fn new(domain: SockDomain, typ: SockTypeFlags) -> Result, SyscallError> { - logln!("new socket: {:?} {:?} {:?}", domain, SockType::from(typ), SockFlags::from(typ)); + logln!( + "new socket: {:?} {:?} {:?}", + domain, + SockType::from(typ), + SockFlags::from(typ) + ); match (domain, SockType::from(typ)) { (SockDomain::AfInet, SockType::Stream) => { diff --git a/cykusz-rs/src/kernel/net/unix/socket.rs b/cykusz-rs/src/kernel/net/unix/socket.rs index 298cd173..7d82d5d5 100644 --- a/cykusz-rs/src/kernel/net/unix/socket.rs +++ b/cykusz-rs/src/kernel/net/unix/socket.rs @@ -11,7 +11,7 @@ use crate::kernel::utils::wait_queue::{WaitQueue, WaitQueueFlags}; use alloc::sync::{Arc, Weak}; use alloc::vec::Vec; use core::sync::atomic::{AtomicUsize, Ordering}; -use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockAddrUn}; +use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockAddrUn, SockTypeFlags}; use syscall_defs::poll::PollEventFlags; use syscall_defs::stat::{Mode, Stat}; use syscall_defs::{OpenFlags, SyscallError, SyscallResult}; @@ -23,7 +23,10 @@ struct ConnectionQueue { impl ConnectionQueue { fn new(addr: SockAddrUn) -> ConnectionQueue { - ConnectionQueue { queue: Vec::new(), addr } + ConnectionQueue { + queue: Vec::new(), + addr, + } } fn addr(&self) -> &SockAddrUn { @@ -63,6 +66,24 @@ impl Socket { }) } + pub fn new_connected(_flags: SockTypeFlags) -> (Arc, Arc) { + let s1 = Socket::new_unbound(None); + let s2 = Socket::new_unbound(None); + + s1.buffer.init_size(4096 * 4); + s2.buffer.init_size(4096 * 4); + + *s1.data.lock() = SocketState::Connected(s2.clone()); + *s2.data.lock() = SocketState::Connected(s1.clone()); + + s1.inc_readers(); + s1.inc_writers(); + s2.inc_readers(); + s2.inc_writers(); + + (s1, s2) + } + fn target(&self) -> Arc { if let SocketState::Connected(s) = &*self.data.lock() { s.clone() diff --git a/cykusz-rs/src/kernel/syscall/mod.rs b/cykusz-rs/src/kernel/syscall/mod.rs index d1c420aa..c8a062b4 100644 --- a/cykusz-rs/src/kernel/syscall/mod.rs +++ b/cykusz-rs/src/kernel/syscall/mod.rs @@ -42,6 +42,7 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) SYS_POWEROFF => sys::sys_poweroff(), SYS_REBOOT => sys::sys_reboot(), SYS_SOCKET => sys::sys_socket(a, b, c), + SYS_SOCKETPAIR => sys::sys_socketpair(a, b, c, d), SYS_BIND => sys::sys_bind(a, b, c), SYS_CONNECT => sys::sys_connect(a, b, c).maybe_into_erestartsys(), SYS_ACCEPT => sys::sys_accept(a, b, c, d).maybe_into_erestartsys(), diff --git a/cykusz-rs/src/kernel/syscall/sys.rs b/cykusz-rs/src/kernel/syscall/sys.rs index b92ae222..a4d2bfa2 100644 --- a/cykusz-rs/src/kernel/syscall/sys.rs +++ b/cykusz-rs/src/kernel/syscall/sys.rs @@ -783,6 +783,29 @@ pub fn sys_socket(domain: u64, typ: u64, _protocol: u64) -> SyscallResult { res } +pub fn sys_socketpair(domain: u64, typ: u64, _protocol: u64, fds: u64) -> SyscallResult { + let sock_domain = SockDomain::try_from(domain)?; + let typ = SockTypeFlags::new(typ); + + if sock_domain != SockDomain::AfUnix { + return Err(SyscallError::ENOTSUP); + } + + let (s1, s2) = crate::kernel::net::unix::socket::Socket::new_connected(typ); + + let task = current_task_ref(); + + let fd1 = task.open_file(DirEntry::inode_wrap(s1.clone()), OpenFlags::RDWR)?; + let fd2 = task.open_file(DirEntry::inode_wrap(s2.clone()), OpenFlags::RDWR)?; + + let fds = unsafe { VirtAddr(fds as usize).read_mut::<[i32; 2]>() }; + + fds[0] = fd1 as i32; + fds[1] = fd2 as i32; + + Ok(0) +} + fn get_socket(fd: usize) -> Result, SyscallError> { let task = current_task_ref(); @@ -868,7 +891,11 @@ pub fn sys_msg_send(sockfd: u64, hdr: u64, flags: u64) -> SyscallResult { let hdr = unsafe { VirtAddr(hdr as usize).read_ref::() }; - logln!("hdr name len {} name addr {:#X}", hdr.msg_namelen, hdr.msg_name.addr()); + logln!( + "hdr name len {} name addr {:#X}", + hdr.msg_namelen, + hdr.msg_name.addr() + ); let sock = get_socket(sockfd as usize)?; diff --git a/cykusz-rs/src/kernel/utils/buffer.rs b/cykusz-rs/src/kernel/utils/buffer.rs index 57c962d6..557b19e1 100644 --- a/cykusz-rs/src/kernel/utils/buffer.rs +++ b/cykusz-rs/src/kernel/utils/buffer.rs @@ -212,7 +212,6 @@ impl BufferQueue { })? .unwrap(); - logln!("READ DATA STARTING"); let read = if transient { buffer.read_data_transient_from(offset, buf) diff --git a/syscall-defs/src/lib.rs b/syscall-defs/src/lib.rs index 685760cb..38c4e647 100644 --- a/syscall-defs/src/lib.rs +++ b/syscall-defs/src/lib.rs @@ -34,6 +34,8 @@ pub const SYS_SLEEP: usize = 9; pub const SYS_POWEROFF: usize = 10; pub const SYS_REBOOT: usize = 11; pub const SYS_GETADDRINFO: usize = 12; +pub const SYS_BIND: usize = 13; +pub const SYS_CONNECT: usize = 14; pub const SYS_SELECT: usize = 15; pub const SYS_MOUNT: usize = 16; pub const SYS_UMOUNT: usize = 17; @@ -91,8 +93,6 @@ pub const SYS_TRUNCATE: usize = 61; pub const SYS_POLL: usize = 62; pub const SYS_SOCKET: usize = 63; -pub const SYS_BIND: usize = 13; -pub const SYS_CONNECT: usize = 14; pub const SYS_ACCEPT: usize = 64; pub const SYS_LISTEN: usize = 65; pub const SYS_MSGRECV: usize = 66; @@ -103,6 +103,7 @@ pub const SYS_YIELD: usize = 70; pub const SYS_CHMOD: usize = 71; pub const SYS_UTIME: usize = 72; pub const SYS_MKNODE: usize = 73; +pub const SYS_SOCKETPAIR: usize = 74; #[derive(Copy, Clone, PartialEq, Debug)] #[repr(u64)] diff --git a/syscall-defs/src/poll.rs b/syscall-defs/src/poll.rs index 76be52c6..4af39577 100644 --- a/syscall-defs/src/poll.rs +++ b/syscall-defs/src/poll.rs @@ -47,7 +47,9 @@ impl FdSet { impl PollFd { pub fn new(fd: i32, events: PollEventFlags) -> PollFd { PollFd { - fd, events, revents: PollEventFlags::empty() + fd, + events, + revents: PollEventFlags::empty(), } } }