xref: /DragonOS/kernel/src/net/socket/unix.rs (revision 634349e0ebfca487e6aa2761a796f04895908718)
16046f775S裕依 use alloc::{boxed::Box, sync::Arc, vec::Vec};
26046f775S裕依 use system_error::SystemError;
36046f775S裕依 
46046f775S裕依 use crate::{libs::spinlock::SpinLock, net::Endpoint};
56046f775S裕依 
6d623e902SGnoCiYeH use super::{
7*634349e0SLoGin     handle::GlobalSocketHandle, PosixSocketHandleItem, Socket, SocketInode, SocketMetadata,
8*634349e0SLoGin     SocketOptions, SocketType,
9d623e902SGnoCiYeH };
106046f775S裕依 
116046f775S裕依 #[derive(Debug, Clone)]
126046f775S裕依 pub struct StreamSocket {
136046f775S裕依     metadata: SocketMetadata,
146046f775S裕依     buffer: Arc<SpinLock<Vec<u8>>>,
156046f775S裕依     peer_inode: Option<Arc<SocketInode>>,
16d623e902SGnoCiYeH     handle: GlobalSocketHandle,
17*634349e0SLoGin     posix_item: Arc<PosixSocketHandleItem>,
186046f775S裕依 }
196046f775S裕依 
206046f775S裕依 impl StreamSocket {
216046f775S裕依     /// 默认的元数据缓冲区大小
226046f775S裕依     pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024;
236046f775S裕依     /// 默认的缓冲区大小
246046f775S裕依     pub const DEFAULT_BUF_SIZE: usize = 64 * 1024;
256046f775S裕依 
266046f775S裕依     /// # 创建一个 Stream Socket
276046f775S裕依     ///
286046f775S裕依     /// ## 参数
296046f775S裕依     /// - `options`: socket选项
new(options: SocketOptions) -> Self306046f775S裕依     pub fn new(options: SocketOptions) -> Self {
316046f775S裕依         let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE)));
326046f775S裕依 
336046f775S裕依         let metadata = SocketMetadata::new(
346046f775S裕依             SocketType::Unix,
356046f775S裕依             Self::DEFAULT_BUF_SIZE,
366046f775S裕依             Self::DEFAULT_BUF_SIZE,
376046f775S裕依             Self::DEFAULT_METADATA_BUF_SIZE,
386046f775S裕依             options,
396046f775S裕依         );
406046f775S裕依 
41*634349e0SLoGin         let posix_item = Arc::new(PosixSocketHandleItem::new(None));
42*634349e0SLoGin 
436046f775S裕依         Self {
446046f775S裕依             metadata,
456046f775S裕依             buffer,
466046f775S裕依             peer_inode: None,
47d623e902SGnoCiYeH             handle: GlobalSocketHandle::new_kernel_handle(),
48*634349e0SLoGin             posix_item,
496046f775S裕依         }
506046f775S裕依     }
516046f775S裕依 }
526046f775S裕依 
536046f775S裕依 impl Socket for StreamSocket {
posix_item(&self) -> Arc<PosixSocketHandleItem>54*634349e0SLoGin     fn posix_item(&self) -> Arc<PosixSocketHandleItem> {
55*634349e0SLoGin         self.posix_item.clone()
56*634349e0SLoGin     }
socket_handle(&self) -> GlobalSocketHandle57d623e902SGnoCiYeH     fn socket_handle(&self) -> GlobalSocketHandle {
58d623e902SGnoCiYeH         self.handle
59d623e902SGnoCiYeH     }
60d623e902SGnoCiYeH 
close(&mut self)61d623e902SGnoCiYeH     fn close(&mut self) {}
62d623e902SGnoCiYeH 
read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint)636046f775S裕依     fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) {
646046f775S裕依         let mut buffer = self.buffer.lock_irqsave();
656046f775S裕依 
666046f775S裕依         let len = core::cmp::min(buf.len(), buffer.len());
676046f775S裕依         buf[..len].copy_from_slice(&buffer[..len]);
686046f775S裕依 
696046f775S裕依         let _ = buffer.split_off(len);
706046f775S裕依 
716046f775S裕依         (Ok(len), Endpoint::Inode(self.peer_inode.clone()))
726046f775S裕依     }
736046f775S裕依 
write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError>746046f775S裕依     fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> {
756046f775S裕依         if self.peer_inode.is_none() {
766046f775S裕依             return Err(SystemError::ENOTCONN);
776046f775S裕依         }
786046f775S裕依 
796046f775S裕依         let peer_inode = self.peer_inode.clone().unwrap();
806046f775S裕依         let len = peer_inode.inner().write_buffer(buf)?;
816046f775S裕依         Ok(len)
826046f775S裕依     }
836046f775S裕依 
connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError>846046f775S裕依     fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> {
856046f775S裕依         if self.peer_inode.is_some() {
866046f775S裕依             return Err(SystemError::EISCONN);
876046f775S裕依         }
886046f775S裕依 
896046f775S裕依         if let Endpoint::Inode(inode) = endpoint {
906046f775S裕依             self.peer_inode = inode;
916046f775S裕依             Ok(())
926046f775S裕依         } else {
936046f775S裕依             Err(SystemError::EINVAL)
946046f775S裕依         }
956046f775S裕依     }
966046f775S裕依 
write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError>976046f775S裕依     fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> {
986046f775S裕依         let mut buffer = self.buffer.lock_irqsave();
996046f775S裕依 
1006046f775S裕依         let len = buf.len();
1016046f775S裕依         if buffer.capacity() - buffer.len() < len {
1026046f775S裕依             return Err(SystemError::ENOBUFS);
1036046f775S裕依         }
1046046f775S裕依         buffer.extend_from_slice(buf);
1056046f775S裕依 
1066046f775S裕依         Ok(len)
1076046f775S裕依     }
1086046f775S裕依 
metadata(&self) -> SocketMetadata1096046f775S裕依     fn metadata(&self) -> SocketMetadata {
1106046f775S裕依         self.metadata.clone()
1116046f775S裕依     }
1126046f775S裕依 
box_clone(&self) -> Box<dyn Socket>1136046f775S裕依     fn box_clone(&self) -> Box<dyn Socket> {
1146046f775S裕依         Box::new(self.clone())
1156046f775S裕依     }
1166046f775S裕依 
as_any_ref(&self) -> &dyn core::any::Any1176046f775S裕依     fn as_any_ref(&self) -> &dyn core::any::Any {
1186046f775S裕依         self
1196046f775S裕依     }
1206046f775S裕依 
as_any_mut(&mut self) -> &mut dyn core::any::Any1216046f775S裕依     fn as_any_mut(&mut self) -> &mut dyn core::any::Any {
1226046f775S裕依         self
1236046f775S裕依     }
1246046f775S裕依 }
1256046f775S裕依 
1266046f775S裕依 #[derive(Debug, Clone)]
1276046f775S裕依 pub struct SeqpacketSocket {
1286046f775S裕依     metadata: SocketMetadata,
1296046f775S裕依     buffer: Arc<SpinLock<Vec<u8>>>,
1306046f775S裕依     peer_inode: Option<Arc<SocketInode>>,
131d623e902SGnoCiYeH     handle: GlobalSocketHandle,
132*634349e0SLoGin     posix_item: Arc<PosixSocketHandleItem>,
1336046f775S裕依 }
1346046f775S裕依 
1356046f775S裕依 impl SeqpacketSocket {
1366046f775S裕依     /// 默认的元数据缓冲区大小
1376046f775S裕依     pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024;
1386046f775S裕依     /// 默认的缓冲区大小
1396046f775S裕依     pub const DEFAULT_BUF_SIZE: usize = 64 * 1024;
1406046f775S裕依 
1416046f775S裕依     /// # 创建一个 Seqpacket Socket
1426046f775S裕依     ///
1436046f775S裕依     /// ## 参数
1446046f775S裕依     /// - `options`: socket选项
new(options: SocketOptions) -> Self1456046f775S裕依     pub fn new(options: SocketOptions) -> Self {
1466046f775S裕依         let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE)));
1476046f775S裕依 
1486046f775S裕依         let metadata = SocketMetadata::new(
1496046f775S裕依             SocketType::Unix,
1506046f775S裕依             Self::DEFAULT_BUF_SIZE,
1516046f775S裕依             Self::DEFAULT_BUF_SIZE,
1526046f775S裕依             Self::DEFAULT_METADATA_BUF_SIZE,
1536046f775S裕依             options,
1546046f775S裕依         );
1556046f775S裕依 
156*634349e0SLoGin         let posix_item = Arc::new(PosixSocketHandleItem::new(None));
157*634349e0SLoGin 
1586046f775S裕依         Self {
1596046f775S裕依             metadata,
1606046f775S裕依             buffer,
1616046f775S裕依             peer_inode: None,
162d623e902SGnoCiYeH             handle: GlobalSocketHandle::new_kernel_handle(),
163*634349e0SLoGin             posix_item,
1646046f775S裕依         }
1656046f775S裕依     }
1666046f775S裕依 }
1676046f775S裕依 
1686046f775S裕依 impl Socket for SeqpacketSocket {
posix_item(&self) -> Arc<PosixSocketHandleItem>169*634349e0SLoGin     fn posix_item(&self) -> Arc<PosixSocketHandleItem> {
170*634349e0SLoGin         self.posix_item.clone()
171*634349e0SLoGin     }
close(&mut self)172d623e902SGnoCiYeH     fn close(&mut self) {}
173d623e902SGnoCiYeH 
read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint)1746046f775S裕依     fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) {
1756046f775S裕依         let mut buffer = self.buffer.lock_irqsave();
1766046f775S裕依 
1776046f775S裕依         let len = core::cmp::min(buf.len(), buffer.len());
1786046f775S裕依         buf[..len].copy_from_slice(&buffer[..len]);
1796046f775S裕依 
1806046f775S裕依         let _ = buffer.split_off(len);
1816046f775S裕依 
1826046f775S裕依         (Ok(len), Endpoint::Inode(self.peer_inode.clone()))
1836046f775S裕依     }
1846046f775S裕依 
write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError>1856046f775S裕依     fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> {
1866046f775S裕依         if self.peer_inode.is_none() {
1876046f775S裕依             return Err(SystemError::ENOTCONN);
1886046f775S裕依         }
1896046f775S裕依 
1906046f775S裕依         let peer_inode = self.peer_inode.clone().unwrap();
1916046f775S裕依         let len = peer_inode.inner().write_buffer(buf)?;
1926046f775S裕依         Ok(len)
1936046f775S裕依     }
1946046f775S裕依 
connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError>1956046f775S裕依     fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> {
1966046f775S裕依         if self.peer_inode.is_some() {
1976046f775S裕依             return Err(SystemError::EISCONN);
1986046f775S裕依         }
1996046f775S裕依 
2006046f775S裕依         if let Endpoint::Inode(inode) = endpoint {
2016046f775S裕依             self.peer_inode = inode;
2026046f775S裕依             Ok(())
2036046f775S裕依         } else {
2046046f775S裕依             Err(SystemError::EINVAL)
2056046f775S裕依         }
2066046f775S裕依     }
2076046f775S裕依 
write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError>2086046f775S裕依     fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> {
2096046f775S裕依         let mut buffer = self.buffer.lock_irqsave();
2106046f775S裕依 
2116046f775S裕依         let len = buf.len();
2126046f775S裕依         if buffer.capacity() - buffer.len() < len {
2136046f775S裕依             return Err(SystemError::ENOBUFS);
2146046f775S裕依         }
2156046f775S裕依         buffer.extend_from_slice(buf);
2166046f775S裕依 
2176046f775S裕依         Ok(len)
2186046f775S裕依     }
2196046f775S裕依 
socket_handle(&self) -> GlobalSocketHandle220d623e902SGnoCiYeH     fn socket_handle(&self) -> GlobalSocketHandle {
221d623e902SGnoCiYeH         self.handle
222d623e902SGnoCiYeH     }
223d623e902SGnoCiYeH 
metadata(&self) -> SocketMetadata2246046f775S裕依     fn metadata(&self) -> SocketMetadata {
2256046f775S裕依         self.metadata.clone()
2266046f775S裕依     }
2276046f775S裕依 
box_clone(&self) -> Box<dyn Socket>2286046f775S裕依     fn box_clone(&self) -> Box<dyn Socket> {
2296046f775S裕依         Box::new(self.clone())
2306046f775S裕依     }
2316046f775S裕依 
as_any_ref(&self) -> &dyn core::any::Any2326046f775S裕依     fn as_any_ref(&self) -> &dyn core::any::Any {
2336046f775S裕依         self
2346046f775S裕依     }
2356046f775S裕依 
as_any_mut(&mut self) -> &mut dyn core::any::Any2366046f775S裕依     fn as_any_mut(&mut self) -> &mut dyn core::any::Any {
2376046f775S裕依         self
2386046f775S裕依     }
2396046f775S裕依 }
240