xref: /DragonOS/kernel/src/net/socket/mod.rs (revision 7c958c9ef0cd25eb15abb21d0d3420aac1c67c88)
1 use core::{any::Any, fmt::Debug, sync::atomic::AtomicUsize};
2 
3 use alloc::{
4     boxed::Box,
5     collections::LinkedList,
6     string::String,
7     sync::{Arc, Weak},
8     vec::Vec,
9 };
10 use hashbrown::HashMap;
11 use smoltcp::{
12     iface::{SocketHandle, SocketSet},
13     socket::{self, tcp, udp},
14 };
15 use system_error::SystemError;
16 
17 use crate::{
18     arch::{rand::rand, sched::sched},
19     filesystem::vfs::{
20         file::FileMode, syscall::ModeType, FilePrivateData, FileSystem, FileType, IndexNode,
21         Metadata,
22     },
23     libs::{
24         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
25         spinlock::{SpinLock, SpinLockGuard},
26         wait_queue::EventWaitQueue,
27     },
28 };
29 
30 use self::{
31     inet::{RawSocket, TcpSocket, UdpSocket},
32     unix::{SeqpacketSocket, StreamSocket},
33 };
34 
35 use super::{
36     event_poll::{EPollEventType, EPollItem, EventPoll},
37     net_core::poll_ifaces,
38     Endpoint, Protocol, ShutdownType,
39 };
40 
41 pub mod inet;
42 pub mod unix;
43 
44 lazy_static! {
45     /// 所有socket的集合
46     /// TODO: 优化这里,自己实现SocketSet!!!现在这样的话,不管全局有多少个网卡,每个时间点都只会有1个进程能够访问socket
47     pub static ref SOCKET_SET: SpinLock<SocketSet<'static >> = SpinLock::new(SocketSet::new(vec![]));
48     /// SocketHandle表,每个SocketHandle对应一个SocketHandleItem,
49     /// 注意!:在网卡中断中需要拿到这张表的��,在获取读锁时应该确保关中断避免死锁
50     pub static ref HANDLE_MAP: RwLock<HashMap<SocketHandle, SocketHandleItem>> = RwLock::new(HashMap::new());
51     /// 端口管理器
52     pub static ref PORT_MANAGER: PortManager = PortManager::new();
53 }
54 
55 /* For setsockopt(2) */
56 // See: linux-5.19.10/include/uapi/asm-generic/socket.h#9
57 pub const SOL_SOCKET: u8 = 1;
58 
59 /// 根据地址族、socket类型和协议创建socket
60 pub(super) fn new_socket(
61     address_family: AddressFamily,
62     socket_type: PosixSocketType,
63     protocol: Protocol,
64 ) -> Result<Box<dyn Socket>, SystemError> {
65     let socket: Box<dyn Socket> = match address_family {
66         AddressFamily::Unix => match socket_type {
67             PosixSocketType::Stream => Box::new(StreamSocket::new(SocketOptions::default())),
68             PosixSocketType::SeqPacket => Box::new(SeqpacketSocket::new(SocketOptions::default())),
69             _ => {
70                 return Err(SystemError::EINVAL);
71             }
72         },
73         AddressFamily::INet => match socket_type {
74             PosixSocketType::Stream => Box::new(TcpSocket::new(SocketOptions::default())),
75             PosixSocketType::Datagram => Box::new(UdpSocket::new(SocketOptions::default())),
76             PosixSocketType::Raw => Box::new(RawSocket::new(protocol, SocketOptions::default())),
77             _ => {
78                 return Err(SystemError::EINVAL);
79             }
80         },
81         _ => {
82             return Err(SystemError::EAFNOSUPPORT);
83         }
84     };
85     Ok(socket)
86 }
87 
88 pub trait Socket: Sync + Send + Debug + Any {
89     /// @brief 从socket中读取数据,如果socket是阻塞的,那么直到读取到数据才返回
90     ///
91     /// @param buf 读取到的数据存放的缓冲区
92     ///
93     /// @return - 成功:(返回读取的数据的长度,读取数据的端点).
94     ///         - 失败:错误码
95     fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint);
96 
97     /// @brief 向socket中写入数据。如果socket是阻塞的,那么直到写入的数据全部写入socket中才返回
98     ///
99     /// @param buf 要写入的数据
100     /// @param to 要写入的目的端点,如果是None,那么写入的数据将会被丢弃
101     ///
102     /// @return 返回写入的数据的长度
103     fn write(&self, buf: &[u8], to: Option<Endpoint>) -> Result<usize, SystemError>;
104 
105     /// @brief 对应于POSIX的connect函数,用于连接到指定的远程服务器端点
106     ///
107     /// It is used to establish a connection to a remote server.
108     /// When a socket is connected to a remote server,
109     /// the operating system will establish a network connection with the server
110     /// and allow data to be sent and received between the local socket and the remote server.
111     ///
112     /// @param endpoint 要连接的端点
113     ///
114     /// @return 返回连接是否成功
115     fn connect(&mut self, _endpoint: Endpoint) -> Result<(), SystemError>;
116 
117     /// @brief 对应于POSIX的bind函数,用于绑定到本机指定的端点
118     ///
119     /// The bind() function is used to associate a socket with a particular IP address and port number on the local machine.
120     ///
121     /// @param endpoint 要绑定的端点
122     ///
123     /// @return 返回绑定是否成功
124     fn bind(&mut self, _endpoint: Endpoint) -> Result<(), SystemError> {
125         Err(SystemError::ENOSYS)
126     }
127 
128     /// @brief 对应于 POSIX 的 shutdown 函数,用于关闭socket。
129     ///
130     /// shutdown() 函数用于启动网络连接的正常关闭。
131     /// 当在两个端点之间建立网络连接时,任一端点都可以通过调用其端点对象上的 shutdown() 函数来启动关闭序列。
132     /// 此函数向远程端点发送关闭消息以指示本地端点不再接受新数据。
133     ///
134     /// @return 返回是否成功关闭
135     fn shutdown(&mut self, _type: ShutdownType) -> Result<(), SystemError> {
136         Err(SystemError::ENOSYS)
137     }
138 
139     /// @brief 对应于POSIX的listen函数,用于监听端点
140     ///
141     /// @param backlog 最大的等待连接数
142     ///
143     /// @return 返回监听是否成功
144     fn listen(&mut self, _backlog: usize) -> Result<(), SystemError> {
145         Err(SystemError::ENOSYS)
146     }
147 
148     /// @brief 对应于POSIX的accept函数,用于接受连接
149     ///
150     /// @param endpoint 对端的端点
151     ///
152     /// @return 返回接受连接是否成功
153     fn accept(&mut self) -> Result<(Box<dyn Socket>, Endpoint), SystemError> {
154         Err(SystemError::ENOSYS)
155     }
156 
157     /// @brief 获取socket的端点
158     ///
159     /// @return 返回socket的端点
160     fn endpoint(&self) -> Option<Endpoint> {
161         None
162     }
163 
164     /// @brief 获取socket的对端端点
165     ///
166     /// @return 返回socket的对端端点
167     fn peer_endpoint(&self) -> Option<Endpoint> {
168         None
169     }
170 
171     /// @brief
172     ///     The purpose of the poll function is to provide
173     ///     a non-blocking way to check if a socket is ready for reading or writing,
174     ///     so that you can efficiently handle multiple sockets in a single thread or event loop.
175     ///
176     /// @return (in, out, err)
177     ///
178     ///     The first boolean value indicates whether the socket is ready for reading. If it is true, then there is data available to be read from the socket without blocking.
179     ///     The second boolean value indicates whether the socket is ready for writing. If it is true, then data can be written to the socket without blocking.
180     ///     The third boolean value indicates whether the socket has encountered an error condition. If it is true, then the socket is in an error state and should be closed or reset
181     ///
182     fn poll(&self) -> EPollEventType {
183         EPollEventType::empty()
184     }
185 
186     /// @brief socket的ioctl函数
187     ///
188     /// @param cmd ioctl命令
189     /// @param arg0 ioctl命令的第一个参数
190     /// @param arg1 ioctl命令的第二个参数
191     /// @param arg2 ioctl命令的第三个参数
192     ///
193     /// @return 返回ioctl命令的返回值
194     fn ioctl(
195         &self,
196         _cmd: usize,
197         _arg0: usize,
198         _arg1: usize,
199         _arg2: usize,
200     ) -> Result<usize, SystemError> {
201         Ok(0)
202     }
203 
204     /// @brief 获取socket的元数据
205     fn metadata(&self) -> SocketMetadata;
206 
207     fn box_clone(&self) -> Box<dyn Socket>;
208 
209     /// @brief 设置socket的选项
210     ///
211     /// @param level 选项的层次
212     /// @param optname 选项的名称
213     /// @param optval 选项的值
214     ///
215     /// @return 返回设置是否成功, 如果不支持该选项,返回ENOSYS
216     fn setsockopt(
217         &self,
218         _level: usize,
219         _optname: usize,
220         _optval: &[u8],
221     ) -> Result<(), SystemError> {
222         kwarn!("setsockopt is not implemented");
223         Ok(())
224     }
225 
226     fn socket_handle(&self) -> SocketHandle {
227         todo!()
228     }
229 
230     fn write_buffer(&self, _buf: &[u8]) -> Result<usize, SystemError> {
231         todo!()
232     }
233 
234     fn as_any_ref(&self) -> &dyn Any;
235 
236     fn as_any_mut(&mut self) -> &mut dyn Any;
237 
238     fn add_epoll(&mut self, epitem: Arc<EPollItem>) -> Result<(), SystemError> {
239         HANDLE_MAP
240             .write_irqsave()
241             .get_mut(&self.socket_handle())
242             .unwrap()
243             .add_epoll(epitem);
244         Ok(())
245     }
246 
247     fn remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError> {
248         HANDLE_MAP
249             .write_irqsave()
250             .get_mut(&self.socket_handle())
251             .unwrap()
252             .remove_epoll(epoll)?;
253 
254         Ok(())
255     }
256 
257     fn clear_epoll(&mut self) -> Result<(), SystemError> {
258         let mut handle_map_guard = HANDLE_MAP.write_irqsave();
259         let handle_item = handle_map_guard.get_mut(&self.socket_handle()).unwrap();
260 
261         for epitem in handle_item.epitems.lock_irqsave().iter() {
262             let epoll = epitem.epoll();
263             if epoll.upgrade().is_some() {
264                 EventPoll::ep_remove(
265                     &mut epoll.upgrade().unwrap().lock_irqsave(),
266                     epitem.fd(),
267                     None,
268                 )?;
269             }
270         }
271 
272         Ok(())
273     }
274 }
275 
276 impl Clone for Box<dyn Socket> {
277     fn clone(&self) -> Box<dyn Socket> {
278         self.box_clone()
279     }
280 }
281 
282 /// # Socket在文件系统中的inode封装
283 #[derive(Debug)]
284 pub struct SocketInode(SpinLock<Box<dyn Socket>>, AtomicUsize);
285 
286 impl SocketInode {
287     pub fn new(socket: Box<dyn Socket>) -> Arc<Self> {
288         Arc::new(Self(SpinLock::new(socket), AtomicUsize::new(0)))
289     }
290 
291     #[inline]
292     pub fn inner(&self) -> SpinLockGuard<Box<dyn Socket>> {
293         self.0.lock()
294     }
295 
296     pub unsafe fn inner_no_preempt(&self) -> SpinLockGuard<Box<dyn Socket>> {
297         self.0.lock_no_preempt()
298     }
299 }
300 
301 impl IndexNode for SocketInode {
302     fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> {
303         self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
304         Ok(())
305     }
306 
307     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
308         let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst);
309         if prev_ref_count == 1 {
310             // 最后一次关闭,需要释放
311             let mut socket = self.0.lock_irqsave();
312 
313             if socket.metadata().socket_type == SocketType::Unix {
314                 return Ok(());
315             }
316 
317             if let Some(Endpoint::Ip(Some(ip))) = socket.endpoint() {
318                 PORT_MANAGER.unbind_port(socket.metadata().socket_type, ip.port)?;
319             }
320 
321             socket.clear_epoll()?;
322 
323             HANDLE_MAP
324                 .write_irqsave()
325                 .remove(&socket.socket_handle())
326                 .unwrap();
327         }
328         Ok(())
329     }
330 
331     fn read_at(
332         &self,
333         _offset: usize,
334         len: usize,
335         buf: &mut [u8],
336         _data: &mut FilePrivateData,
337     ) -> Result<usize, SystemError> {
338         self.0.lock_no_preempt().read(&mut buf[0..len]).0
339     }
340 
341     fn write_at(
342         &self,
343         _offset: usize,
344         len: usize,
345         buf: &[u8],
346         _data: &mut FilePrivateData,
347     ) -> Result<usize, SystemError> {
348         self.0.lock_no_preempt().write(&buf[0..len], None)
349     }
350 
351     fn poll(&self, _private_data: &FilePrivateData) -> Result<usize, SystemError> {
352         let events = self.0.lock_irqsave().poll();
353         return Ok(events.bits() as usize);
354     }
355 
356     fn fs(&self) -> Arc<dyn FileSystem> {
357         todo!()
358     }
359 
360     fn as_any_ref(&self) -> &dyn Any {
361         self
362     }
363 
364     fn list(&self) -> Result<Vec<String>, SystemError> {
365         return Err(SystemError::ENOTDIR);
366     }
367 
368     fn metadata(&self) -> Result<Metadata, SystemError> {
369         let meta = Metadata {
370             mode: ModeType::from_bits_truncate(0o755),
371             file_type: FileType::Socket,
372             ..Default::default()
373         };
374 
375         return Ok(meta);
376     }
377 
378     fn resize(&self, _len: usize) -> Result<(), SystemError> {
379         return Ok(());
380     }
381 }
382 
383 #[derive(Debug)]
384 pub struct SocketHandleItem {
385     /// shutdown状态
386     pub shutdown_type: RwLock<ShutdownType>,
387     /// socket的waitqueue
388     pub wait_queue: EventWaitQueue,
389     /// epitems,考虑写在这是否是最优解?
390     pub epitems: SpinLock<LinkedList<Arc<EPollItem>>>,
391 }
392 
393 impl SocketHandleItem {
394     pub fn new() -> Self {
395         Self {
396             shutdown_type: RwLock::new(ShutdownType::empty()),
397             wait_queue: EventWaitQueue::new(),
398             epitems: SpinLock::new(LinkedList::new()),
399         }
400     }
401 
402     /// ## 在socket的等待队列上睡眠
403     pub fn sleep(
404         socket_handle: SocketHandle,
405         events: u64,
406         handle_map_guard: RwLockReadGuard<'_, HashMap<SocketHandle, SocketHandleItem>>,
407     ) {
408         unsafe {
409             handle_map_guard
410                 .get(&socket_handle)
411                 .unwrap()
412                 .wait_queue
413                 .sleep_without_schedule(events)
414         };
415         drop(handle_map_guard);
416         sched();
417     }
418 
419     pub fn shutdown_type(&self) -> ShutdownType {
420         *self.shutdown_type.read()
421     }
422 
423     pub fn shutdown_type_writer(&mut self) -> RwLockWriteGuard<ShutdownType> {
424         self.shutdown_type.write_irqsave()
425     }
426 
427     pub fn add_epoll(&mut self, epitem: Arc<EPollItem>) {
428         self.epitems.lock_irqsave().push_back(epitem)
429     }
430 
431     pub fn remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError> {
432         let is_remove = !self
433             .epitems
434             .lock_irqsave()
435             .extract_if(|x| x.epoll().ptr_eq(epoll))
436             .collect::<Vec<_>>()
437             .is_empty();
438 
439         if is_remove {
440             return Ok(());
441         }
442 
443         Err(SystemError::ENOENT)
444     }
445 }
446 
447 /// # TCP 和 UDP 的端口管理器。
448 /// 如果 TCP/UDP 的 socket 绑定了某个端口,它会在对应的表中记录,以检测端口冲突。
449 pub struct PortManager {
450     // TCP 端口记录表
451     tcp_port_table: SpinLock<HashMap<u16, Arc<GlobalSocketHandle>>>,
452     // UDP 端口记录表
453     udp_port_table: SpinLock<HashMap<u16, Arc<GlobalSocketHandle>>>,
454 }
455 
456 impl PortManager {
457     pub fn new() -> Self {
458         return Self {
459             tcp_port_table: SpinLock::new(HashMap::new()),
460             udp_port_table: SpinLock::new(HashMap::new()),
461         };
462     }
463 
464     /// @brief 自动分配一个相对应协议中未被使用的PORT,如果动态端口均已被占用,返回错误码 EADDRINUSE
465     pub fn get_ephemeral_port(&self, socket_type: SocketType) -> Result<u16, SystemError> {
466         // TODO: selects non-conflict high port
467 
468         static mut EPHEMERAL_PORT: u16 = 0;
469         unsafe {
470             if EPHEMERAL_PORT == 0 {
471                 EPHEMERAL_PORT = (49152 + rand() % (65536 - 49152)) as u16;
472             }
473         }
474 
475         let mut remaining = 65536 - 49152; // 剩余尝试分配端口次数
476         let mut port: u16;
477         while remaining > 0 {
478             unsafe {
479                 if EPHEMERAL_PORT == 65535 {
480                     EPHEMERAL_PORT = 49152;
481                 } else {
482                     EPHEMERAL_PORT += 1;
483                 }
484                 port = EPHEMERAL_PORT;
485             }
486 
487             // 使用 ListenTable 检查端口是否被占用
488             let listen_table_guard = match socket_type {
489                 SocketType::Udp => self.udp_port_table.lock(),
490                 SocketType::Tcp => self.tcp_port_table.lock(),
491                 _ => panic!("{:?} cann't get a port", socket_type),
492             };
493             if listen_table_guard.get(&port).is_none() {
494                 drop(listen_table_guard);
495                 return Ok(port);
496             }
497             remaining -= 1;
498         }
499         return Err(SystemError::EADDRINUSE);
500     }
501 
502     /// @brief 检测给定端口是否已被占用,如果未被占用则在 TCP/UDP 对应的表中记录
503     ///
504     /// TODO: 增加支持端口复用的逻辑
505     pub fn bind_port(
506         &self,
507         socket_type: SocketType,
508         port: u16,
509         handle: Arc<GlobalSocketHandle>,
510     ) -> Result<(), SystemError> {
511         if port > 0 {
512             let mut listen_table_guard = match socket_type {
513                 SocketType::Udp => self.udp_port_table.lock(),
514                 SocketType::Tcp => self.tcp_port_table.lock(),
515                 _ => panic!("{:?} cann't bind a port", socket_type),
516             };
517             match listen_table_guard.get(&port) {
518                 Some(_) => return Err(SystemError::EADDRINUSE),
519                 None => listen_table_guard.insert(port, handle),
520             };
521             drop(listen_table_guard);
522         }
523         return Ok(());
524     }
525 
526     /// @brief 在对应的端口记录表中将端口和 socket 解绑
527     pub fn unbind_port(&self, socket_type: SocketType, port: u16) -> Result<(), SystemError> {
528         let mut listen_table_guard = match socket_type {
529             SocketType::Udp => self.udp_port_table.lock(),
530             SocketType::Tcp => self.tcp_port_table.lock(),
531             _ => return Ok(()),
532         };
533         listen_table_guard.remove(&port);
534         drop(listen_table_guard);
535         return Ok(());
536     }
537 }
538 
539 /// # socket的句柄管理组件
540 /// 它在smoltcp的SocketHandle上封装了一层,增加更多的功能。
541 /// 比如,在socket被关闭时,自动释放socket的资源,通知系统的其他组件。
542 #[derive(Debug)]
543 pub struct GlobalSocketHandle(SocketHandle);
544 
545 impl GlobalSocketHandle {
546     pub fn new(handle: SocketHandle) -> Arc<Self> {
547         return Arc::new(Self(handle));
548     }
549 }
550 
551 impl Clone for GlobalSocketHandle {
552     fn clone(&self) -> Self {
553         Self(self.0)
554     }
555 }
556 
557 impl Drop for GlobalSocketHandle {
558     fn drop(&mut self) {
559         let mut socket_set_guard = SOCKET_SET.lock_irqsave();
560         socket_set_guard.remove(self.0); // 删除的时候,会发送一条FINISH的信息?
561         drop(socket_set_guard);
562         poll_ifaces();
563     }
564 }
565 
566 /// @brief socket的类型
567 #[derive(Debug, Clone, Copy, PartialEq)]
568 pub enum SocketType {
569     /// 原始的socket
570     Raw,
571     /// 用于Tcp通信的 Socket
572     Tcp,
573     /// 用于Udp通信的 Socket
574     Udp,
575     /// unix域的 Socket
576     Unix,
577 }
578 
579 bitflags! {
580     /// @brief socket的选项
581     #[derive(Default)]
582     pub struct SocketOptions: u32 {
583         /// 是否阻塞
584         const BLOCK = 1 << 0;
585         /// 是否允许广播
586         const BROADCAST = 1 << 1;
587         /// 是否允许多播
588         const MULTICAST = 1 << 2;
589         /// 是否允许重用地址
590         const REUSEADDR = 1 << 3;
591         /// 是否允许重用端口
592         const REUSEPORT = 1 << 4;
593     }
594 }
595 
596 #[derive(Debug, Clone)]
597 /// @brief 在trait Socket的metadata函数中返回该结构体供外部使用
598 pub struct SocketMetadata {
599     /// socket的类型
600     pub socket_type: SocketType,
601     /// 接收缓冲区的大小
602     pub rx_buf_size: usize,
603     /// 发送缓冲区的大小
604     pub tx_buf_size: usize,
605     /// 元数据的缓冲区的大小
606     pub metadata_buf_size: usize,
607     /// socket的选项
608     pub options: SocketOptions,
609 }
610 
611 impl SocketMetadata {
612     fn new(
613         socket_type: SocketType,
614         rx_buf_size: usize,
615         tx_buf_size: usize,
616         metadata_buf_size: usize,
617         options: SocketOptions,
618     ) -> Self {
619         Self {
620             socket_type,
621             rx_buf_size,
622             tx_buf_size,
623             metadata_buf_size,
624             options,
625         }
626     }
627 }
628 
629 /// @brief 地址族的枚举
630 ///
631 /// 参考:https://code.dragonos.org.cn/xref/linux-5.19.10/include/linux/socket.h#180
632 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
633 pub enum AddressFamily {
634     /// AF_UNSPEC 表示地址族未指定
635     Unspecified = 0,
636     /// AF_UNIX 表示Unix域的socket (与AF_LOCAL相同)
637     Unix = 1,
638     ///  AF_INET 表示IPv4的socket
639     INet = 2,
640     /// AF_AX25 表示AMPR AX.25的socket
641     AX25 = 3,
642     /// AF_IPX 表示IPX的socket
643     IPX = 4,
644     /// AF_APPLETALK 表示Appletalk的socket
645     Appletalk = 5,
646     /// AF_NETROM 表示AMPR NET/ROM的socket
647     Netrom = 6,
648     /// AF_BRIDGE 表示多协议桥接的socket
649     Bridge = 7,
650     /// AF_ATMPVC 表示ATM PVCs的socket
651     Atmpvc = 8,
652     /// AF_X25 表示X.25的socket
653     X25 = 9,
654     /// AF_INET6 表示IPv6的socket
655     INet6 = 10,
656     /// AF_ROSE 表示AMPR ROSE的socket
657     Rose = 11,
658     /// AF_DECnet Reserved for DECnet project
659     Decnet = 12,
660     /// AF_NETBEUI Reserved for 802.2LLC project
661     Netbeui = 13,
662     /// AF_SECURITY 表示Security callback的伪AF
663     Security = 14,
664     /// AF_KEY 表示Key management API
665     Key = 15,
666     /// AF_NETLINK 表示Netlink的socket
667     Netlink = 16,
668     /// AF_PACKET 表示Low level packet interface
669     Packet = 17,
670     /// AF_ASH 表示Ash
671     Ash = 18,
672     /// AF_ECONET 表示Acorn Econet
673     Econet = 19,
674     /// AF_ATMSVC 表示ATM SVCs
675     Atmsvc = 20,
676     /// AF_RDS 表示Reliable Datagram Sockets
677     Rds = 21,
678     /// AF_SNA 表示Linux SNA Project
679     Sna = 22,
680     /// AF_IRDA 表示IRDA sockets
681     Irda = 23,
682     /// AF_PPPOX 表示PPPoX sockets
683     Pppox = 24,
684     /// AF_WANPIPE 表示WANPIPE API sockets
685     WanPipe = 25,
686     /// AF_LLC 表示Linux LLC
687     Llc = 26,
688     /// AF_IB 表示Native InfiniBand address
689     /// 介绍:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_infiniband_and_rdma_networks/index#understanding-infiniband-and-rdma_configuring-infiniband-and-rdma-networks
690     Ib = 27,
691     /// AF_MPLS 表示MPLS
692     Mpls = 28,
693     /// AF_CAN 表示Controller Area Network
694     Can = 29,
695     /// AF_TIPC 表示TIPC sockets
696     Tipc = 30,
697     /// AF_BLUETOOTH 表示Bluetooth sockets
698     Bluetooth = 31,
699     /// AF_IUCV 表示IUCV sockets
700     Iucv = 32,
701     /// AF_RXRPC 表示RxRPC sockets
702     Rxrpc = 33,
703     /// AF_ISDN 表示mISDN sockets
704     Isdn = 34,
705     /// AF_PHONET 表示Phonet sockets
706     Phonet = 35,
707     /// AF_IEEE802154 表示IEEE 802.15.4 sockets
708     Ieee802154 = 36,
709     /// AF_CAIF 表示CAIF sockets
710     Caif = 37,
711     /// AF_ALG 表示Algorithm sockets
712     Alg = 38,
713     /// AF_NFC 表示NFC sockets
714     Nfc = 39,
715     /// AF_VSOCK 表示vSockets
716     Vsock = 40,
717     /// AF_KCM 表示Kernel Connection Multiplexor
718     Kcm = 41,
719     /// AF_QIPCRTR 表示Qualcomm IPC Router
720     Qipcrtr = 42,
721     /// AF_SMC 表示SMC-R sockets.
722     /// reserve number for PF_SMC protocol family that reuses AF_INET address family
723     Smc = 43,
724     /// AF_XDP 表示XDP sockets
725     Xdp = 44,
726     /// AF_MCTP 表示Management Component Transport Protocol
727     Mctp = 45,
728     /// AF_MAX 表示最大的地址族
729     Max = 46,
730 }
731 
732 impl TryFrom<u16> for AddressFamily {
733     type Error = SystemError;
734     fn try_from(x: u16) -> Result<Self, Self::Error> {
735         use num_traits::FromPrimitive;
736         return <Self as FromPrimitive>::from_u16(x).ok_or(SystemError::EINVAL);
737     }
738 }
739 
740 /// @brief posix套接字类型的枚举(这些值与linux内核中的值一致)
741 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
742 pub enum PosixSocketType {
743     Stream = 1,
744     Datagram = 2,
745     Raw = 3,
746     Rdm = 4,
747     SeqPacket = 5,
748     Dccp = 6,
749     Packet = 10,
750 }
751 
752 impl TryFrom<u8> for PosixSocketType {
753     type Error = SystemError;
754     fn try_from(x: u8) -> Result<Self, Self::Error> {
755         use num_traits::FromPrimitive;
756         return <Self as FromPrimitive>::from_u8(x).ok_or(SystemError::EINVAL);
757     }
758 }
759 
760 /// ### 为socket提供无锁的poll方法
761 ///
762 /// 因为在网卡中断中,需要轮询socket的状态,如果使用socket文件或者其inode来poll
763 /// 在当前的设计,会必然死锁,所以引用这一个设计来解决,提供无��的poll
764 pub struct SocketPollMethod;
765 
766 impl SocketPollMethod {
767     pub fn poll(socket: &socket::Socket, shutdown: ShutdownType) -> EPollEventType {
768         match socket {
769             socket::Socket::Udp(udp) => Self::udp_poll(udp, shutdown),
770             socket::Socket::Tcp(tcp) => Self::tcp_poll(tcp, shutdown),
771             _ => todo!(),
772         }
773     }
774 
775     pub fn tcp_poll(socket: &tcp::Socket, shutdown: ShutdownType) -> EPollEventType {
776         let mut events = EPollEventType::empty();
777         if socket.is_listening() && socket.is_active() {
778             events.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
779             return events;
780         }
781 
782         // socket已经关闭
783         if !socket.is_open() {
784             events.insert(EPollEventType::EPOLLHUP)
785         }
786         if shutdown.contains(ShutdownType::RCV_SHUTDOWN) {
787             events.insert(
788                 EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM | EPollEventType::EPOLLRDHUP,
789             );
790         }
791 
792         let state = socket.state();
793         if state != tcp::State::SynSent && state != tcp::State::SynReceived {
794             // socket有可读数据
795             if socket.can_recv() {
796                 events.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
797             }
798 
799             if !(shutdown.contains(ShutdownType::SEND_SHUTDOWN)) {
800                 // 缓冲区可写
801                 if socket.send_queue() < socket.send_capacity() {
802                     events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
803                 } else {
804                     // TODO:触发缓冲区已满的信号
805                     todo!("A signal that the buffer is full needs to be sent");
806                 }
807             } else {
808                 // 如果我们的socket关闭了SEND_SHUTDOWN,epoll事件就是EPOLLOUT
809                 events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
810             }
811         } else if state == tcp::State::SynSent {
812             events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
813         }
814 
815         // socket发生错误
816         if !socket.is_active() {
817             events.insert(EPollEventType::EPOLLERR);
818         }
819 
820         events
821     }
822 
823     pub fn udp_poll(socket: &udp::Socket, shutdown: ShutdownType) -> EPollEventType {
824         let mut event = EPollEventType::empty();
825 
826         if shutdown.contains(ShutdownType::RCV_SHUTDOWN) {
827             event.insert(
828                 EPollEventType::EPOLLRDHUP | EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM,
829             );
830         }
831         if shutdown.contains(ShutdownType::SHUTDOWN_MASK) {
832             event.insert(EPollEventType::EPOLLHUP);
833         }
834 
835         if socket.can_recv() {
836             event.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
837         }
838 
839         if socket.can_send() {
840             event.insert(
841                 EPollEventType::EPOLLOUT
842                     | EPollEventType::EPOLLWRNORM
843                     | EPollEventType::EPOLLWRBAND,
844             );
845         } else {
846             // TODO: 缓冲区空间不够,需要使用信号处理
847             todo!()
848         }
849 
850         return event;
851     }
852 }
853