xref: /DragonOS/kernel/src/net/syscall.rs (revision 56cc4dbe27e132aac5c61b8bd4f4ec9a223b49ee)
1 use core::{cmp::min, ffi::CStr};
2 
3 use alloc::{boxed::Box, sync::Arc};
4 use num_traits::{FromPrimitive, ToPrimitive};
5 use smoltcp::wire;
6 use system_error::SystemError;
7 
8 use crate::{
9     filesystem::vfs::{
10         file::{File, FileMode},
11         syscall::{IoVec, IoVecs},
12         FileType,
13     },
14     libs::spinlock::SpinLockGuard,
15     mm::{verify_area, VirtAddr},
16     net::socket::{AddressFamily, SOL_SOCKET},
17     process::ProcessManager,
18     syscall::Syscall,
19 };
20 
21 use super::{
22     socket::{new_socket, PosixSocketType, Socket, SocketHandleItem, SocketInode, HANDLE_MAP},
23     Endpoint, Protocol, ShutdownType,
24 };
25 
26 /// Flags for socket, socketpair, accept4
27 const SOCK_CLOEXEC: FileMode = FileMode::O_CLOEXEC;
28 const SOCK_NONBLOCK: FileMode = FileMode::O_NONBLOCK;
29 
30 impl Syscall {
31     /// @brief sys_socket系统调用的实际执行函数
32     ///
33     /// @param address_family 地址族
34     /// @param socket_type socket类型
35     /// @param protocol 传输协议
36     pub fn socket(
37         address_family: usize,
38         socket_type: usize,
39         protocol: usize,
40     ) -> Result<usize, SystemError> {
41         let address_family = AddressFamily::try_from(address_family as u16)?;
42         let socket_type = PosixSocketType::try_from((socket_type & 0xf) as u8)?;
43         let protocol = Protocol::from(protocol as u8);
44 
45         let socket = new_socket(address_family, socket_type, protocol)?;
46 
47         if address_family != AddressFamily::Unix {
48             let handle_item = SocketHandleItem::new();
49             HANDLE_MAP
50                 .write_irqsave()
51                 .insert(socket.socket_handle(), handle_item);
52         }
53 
54         let socketinode: Arc<SocketInode> = SocketInode::new(socket);
55         let f = File::new(socketinode, FileMode::O_RDWR)?;
56         // 把socket添加到当前进程的文件描述符表中
57         let binding = ProcessManager::current_pcb().fd_table();
58         let mut fd_table_guard = binding.write();
59         let fd = fd_table_guard.alloc_fd(f, None).map(|x| x as usize);
60         drop(fd_table_guard);
61         return fd;
62     }
63 
64     /// # sys_socketpair系统调用的实际执行函数
65     ///
66     /// ## 参数
67     /// - `address_family`: 地址族
68     /// - `socket_type`: socket类型
69     /// - `protocol`: 传输协议
70     /// - `fds`: 用于返回文件描述符的数组
71     pub fn socketpair(
72         address_family: usize,
73         socket_type: usize,
74         protocol: usize,
75         fds: &mut [i32],
76     ) -> Result<usize, SystemError> {
77         let address_family = AddressFamily::try_from(address_family as u16)?;
78         let socket_type = PosixSocketType::try_from((socket_type & 0xf) as u8)?;
79         let protocol = Protocol::from(protocol as u8);
80 
81         let binding = ProcessManager::current_pcb().fd_table();
82         let mut fd_table_guard = binding.write();
83 
84         // 创建一对socket
85         let inode0 = SocketInode::new(new_socket(address_family, socket_type, protocol)?);
86         let inode1 = SocketInode::new(new_socket(address_family, socket_type, protocol)?);
87 
88         // 进行pair
89         unsafe {
90             inode0
91                 .inner_no_preempt()
92                 .connect(Endpoint::Inode(Some(inode1.clone())))?;
93             inode1
94                 .inner_no_preempt()
95                 .connect(Endpoint::Inode(Some(inode0.clone())))?;
96         }
97 
98         fds[0] = fd_table_guard.alloc_fd(File::new(inode0, FileMode::O_RDWR)?, None)?;
99         fds[1] = fd_table_guard.alloc_fd(File::new(inode1, FileMode::O_RDWR)?, None)?;
100 
101         drop(fd_table_guard);
102         Ok(0)
103     }
104 
105     /// @brief sys_setsockopt系统调用的实际执行函数
106     ///
107     /// @param fd 文件描述符
108     /// @param level 选项级别
109     /// @param optname 选项名称
110     /// @param optval 选项值
111     /// @param optlen optval缓冲区长度
112     pub fn setsockopt(
113         fd: usize,
114         level: usize,
115         optname: usize,
116         optval: &[u8],
117     ) -> Result<usize, SystemError> {
118         let socket_inode: Arc<SocketInode> = ProcessManager::current_pcb()
119             .get_socket(fd as i32)
120             .ok_or(SystemError::EBADF)?;
121         // 获取内层的socket(真正的数据)
122         let socket: SpinLockGuard<Box<dyn Socket>> = socket_inode.inner();
123         return socket.setsockopt(level, optname, optval).map(|_| 0);
124     }
125 
126     /// @brief sys_getsockopt系统调用的实际执行函数
127     ///
128     /// 参考:https://man7.org/linux/man-pages/man2/setsockopt.2.html
129     ///
130     /// @param fd 文件描述符
131     /// @param level 选项级别
132     /// @param optname 选项名称
133     /// @param optval 返回的选项值
134     /// @param optlen 返回的optval缓冲区长度
135     pub fn getsockopt(
136         fd: usize,
137         level: usize,
138         optname: usize,
139         optval: *mut u8,
140         optlen: *mut u32,
141     ) -> Result<usize, SystemError> {
142         // 获取socket
143         let optval = optval as *mut u32;
144         let binding: Arc<SocketInode> = ProcessManager::current_pcb()
145             .get_socket(fd as i32)
146             .ok_or(SystemError::EBADF)?;
147         let socket = binding.inner();
148 
149         if level as u8 == SOL_SOCKET {
150             let optname = PosixSocketOption::try_from(optname as i32)
151                 .map_err(|_| SystemError::ENOPROTOOPT)?;
152             match optname {
153                 PosixSocketOption::SO_SNDBUF => {
154                     // 返回发送缓冲区大小
155                     unsafe {
156                         *optval = socket.metadata().tx_buf_size as u32;
157                         *optlen = core::mem::size_of::<u32>() as u32;
158                     }
159                     return Ok(0);
160                 }
161                 PosixSocketOption::SO_RCVBUF => {
162                     // 返回默认的接收缓冲区大小
163                     unsafe {
164                         *optval = socket.metadata().rx_buf_size as u32;
165                         *optlen = core::mem::size_of::<u32>() as u32;
166                     }
167                     return Ok(0);
168                 }
169                 _ => {
170                     return Err(SystemError::ENOPROTOOPT);
171                 }
172             }
173         }
174         drop(socket);
175 
176         // To manipulate options at any other level the
177         // protocol number of the appropriate protocol controlling the
178         // option is supplied.  For example, to indicate that an option is
179         // to be interpreted by the TCP protocol, level should be set to the
180         // protocol number of TCP.
181 
182         let posix_protocol =
183             PosixIpProtocol::try_from(level as u16).map_err(|_| SystemError::ENOPROTOOPT)?;
184         if posix_protocol == PosixIpProtocol::TCP {
185             let optname = PosixTcpSocketOptions::try_from(optname as i32)
186                 .map_err(|_| SystemError::ENOPROTOOPT)?;
187             match optname {
188                 PosixTcpSocketOptions::Congestion => return Ok(0),
189                 _ => {
190                     return Err(SystemError::ENOPROTOOPT);
191                 }
192             }
193         }
194         return Err(SystemError::ENOPROTOOPT);
195     }
196 
197     /// @brief sys_connect系统调用的实际执行函数
198     ///
199     /// @param fd 文件描述符
200     /// @param addr SockAddr
201     /// @param addrlen 地址长度
202     ///
203     /// @return 成功返回0,失败返回错误码
204     pub fn connect(fd: usize, addr: *const SockAddr, addrlen: usize) -> Result<usize, SystemError> {
205         let endpoint: Endpoint = SockAddr::to_endpoint(addr, addrlen)?;
206         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
207             .get_socket(fd as i32)
208             .ok_or(SystemError::EBADF)?;
209         let mut socket = unsafe { socket.inner_no_preempt() };
210         socket.connect(endpoint)?;
211         Ok(0)
212     }
213 
214     /// @brief sys_bind系统调用的实际执行函数
215     ///
216     /// @param fd 文件描述符
217     /// @param addr SockAddr
218     /// @param addrlen 地址长度
219     ///
220     /// @return 成功返回0,失败返回错误码
221     pub fn bind(fd: usize, addr: *const SockAddr, addrlen: usize) -> Result<usize, SystemError> {
222         let endpoint: Endpoint = SockAddr::to_endpoint(addr, addrlen)?;
223         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
224             .get_socket(fd as i32)
225             .ok_or(SystemError::EBADF)?;
226         let mut socket = unsafe { socket.inner_no_preempt() };
227         socket.bind(endpoint)?;
228         Ok(0)
229     }
230 
231     /// @brief sys_sendto系统调用的实际执行函数
232     ///
233     /// @param fd 文件描述符
234     /// @param buf 发送缓冲区
235     /// @param flags 标志
236     /// @param addr SockAddr
237     /// @param addrlen 地址长度
238     ///
239     /// @return 成功返回发送的字节数,失败返回错误码
240     pub fn sendto(
241         fd: usize,
242         buf: &[u8],
243         _flags: u32,
244         addr: *const SockAddr,
245         addrlen: usize,
246     ) -> Result<usize, SystemError> {
247         let endpoint = if addr.is_null() {
248             None
249         } else {
250             Some(SockAddr::to_endpoint(addr, addrlen)?)
251         };
252 
253         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
254             .get_socket(fd as i32)
255             .ok_or(SystemError::EBADF)?;
256         let socket = unsafe { socket.inner_no_preempt() };
257         return socket.write(buf, endpoint);
258     }
259 
260     /// @brief sys_recvfrom系统调用的实际执行函数
261     ///
262     /// @param fd 文件描述符
263     /// @param buf 接收缓冲区
264     /// @param flags 标志
265     /// @param addr SockAddr
266     /// @param addrlen 地址长度
267     ///
268     /// @return 成功返回接收的字节数,失败返回错误码
269     pub fn recvfrom(
270         fd: usize,
271         buf: &mut [u8],
272         _flags: u32,
273         addr: *mut SockAddr,
274         addrlen: *mut u32,
275     ) -> Result<usize, SystemError> {
276         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
277             .get_socket(fd as i32)
278             .ok_or(SystemError::EBADF)?;
279         let socket = unsafe { socket.inner_no_preempt() };
280 
281         let (n, endpoint) = socket.read(buf);
282         drop(socket);
283 
284         let n: usize = n?;
285 
286         // 如果有地址信息,将地址信息写入用户空间
287         if !addr.is_null() {
288             let sockaddr_in = SockAddr::from(endpoint);
289             unsafe {
290                 sockaddr_in.write_to_user(addr, addrlen)?;
291             }
292         }
293         return Ok(n);
294     }
295 
296     /// @brief sys_recvmsg系统调用的实际执行函数
297     ///
298     /// @param fd 文件描述符
299     /// @param msg MsgHdr
300     /// @param flags 标志,暂时未使用
301     ///
302     /// @return 成功返回接收的字节数,失败返回错误码
303     pub fn recvmsg(fd: usize, msg: &mut MsgHdr, _flags: u32) -> Result<usize, SystemError> {
304         // 检查每个缓冲区地址是否合法,生成iovecs
305         let mut iovs = unsafe { IoVecs::from_user(msg.msg_iov, msg.msg_iovlen, true)? };
306 
307         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
308             .get_socket(fd as i32)
309             .ok_or(SystemError::EBADF)?;
310         let socket = unsafe { socket.inner_no_preempt() };
311 
312         let mut buf = iovs.new_buf(true);
313         // 从socket中读取数据
314         let (n, endpoint) = socket.read(&mut buf);
315         drop(socket);
316 
317         let n: usize = n?;
318 
319         // 将数据写入用户空间的iovecs
320         iovs.scatter(&buf[..n]);
321 
322         let sockaddr_in = SockAddr::from(endpoint);
323         unsafe {
324             sockaddr_in.write_to_user(msg.msg_name, &mut msg.msg_namelen)?;
325         }
326         return Ok(n);
327     }
328 
329     /// @brief sys_listen系统调用的实际执行函数
330     ///
331     /// @param fd 文件描述符
332     /// @param backlog 队列最大连接数
333     ///
334     /// @return 成功返回0,失败返回错误码
335     pub fn listen(fd: usize, backlog: usize) -> Result<usize, SystemError> {
336         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
337             .get_socket(fd as i32)
338             .ok_or(SystemError::EBADF)?;
339         let mut socket = unsafe { socket.inner_no_preempt() };
340         socket.listen(backlog)?;
341         return Ok(0);
342     }
343 
344     /// @brief sys_shutdown系统调用的实际执行函数
345     ///
346     /// @param fd 文件描述符
347     /// @param how 关闭方式
348     ///
349     /// @return 成功返回0,失败返回错误码
350     pub fn shutdown(fd: usize, how: usize) -> Result<usize, SystemError> {
351         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
352             .get_socket(fd as i32)
353             .ok_or(SystemError::EBADF)?;
354         let mut socket = unsafe { socket.inner_no_preempt() };
355         socket.shutdown(ShutdownType::from_bits_truncate(how as u8))?;
356         return Ok(0);
357     }
358 
359     /// @brief sys_accept系统调用的实际执行函数
360     ///
361     /// @param fd 文件描述符
362     /// @param addr SockAddr
363     /// @param addrlen 地址长度
364     ///
365     /// @return 成功返回新的文件描述符,失败返回错误码
366     pub fn accept(fd: usize, addr: *mut SockAddr, addrlen: *mut u32) -> Result<usize, SystemError> {
367         return Self::do_accept(fd, addr, addrlen, 0);
368     }
369 
370     /// sys_accept4 - accept a connection on a socket
371     ///
372     ///
373     /// If flags is 0, then accept4() is the same as accept().  The
374     ///    following values can be bitwise ORed in flags to obtain different
375     ///    behavior:
376     ///
377     /// - SOCK_NONBLOCK
378     ///     Set the O_NONBLOCK file status flag on the open file
379     ///     description (see open(2)) referred to by the new file
380     ///     descriptor.  Using this flag saves extra calls to fcntl(2)
381     ///     to achieve the same result.
382     ///
383     /// - SOCK_CLOEXEC
384     ///     Set the close-on-exec (FD_CLOEXEC) flag on the new file
385     ///     descriptor.  See the description of the O_CLOEXEC flag in
386     ///     open(2) for reasons why this may be useful.
387     pub fn accept4(
388         fd: usize,
389         addr: *mut SockAddr,
390         addrlen: *mut u32,
391         mut flags: u32,
392     ) -> Result<usize, SystemError> {
393         // 如果flags不合法,返回错误
394         if (flags & (!(SOCK_CLOEXEC | SOCK_NONBLOCK)).bits()) != 0 {
395             return Err(SystemError::EINVAL);
396         }
397 
398         if SOCK_NONBLOCK != FileMode::O_NONBLOCK && ((flags & SOCK_NONBLOCK.bits()) != 0) {
399             flags = (flags & !FileMode::O_NONBLOCK.bits()) | FileMode::O_NONBLOCK.bits();
400         }
401 
402         return Self::do_accept(fd, addr, addrlen, flags);
403     }
404 
405     fn do_accept(
406         fd: usize,
407         addr: *mut SockAddr,
408         addrlen: *mut u32,
409         flags: u32,
410     ) -> Result<usize, SystemError> {
411         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
412             .get_socket(fd as i32)
413             .ok_or(SystemError::EBADF)?;
414         // kdebug!("accept: socket={:?}", socket);
415         let mut socket = unsafe { socket.inner_no_preempt() };
416         // 从socket中接收连接
417         let (new_socket, remote_endpoint) = socket.accept()?;
418         drop(socket);
419 
420         // kdebug!("accept: new_socket={:?}", new_socket);
421         // Insert the new socket into the file descriptor vector
422         let new_socket: Arc<SocketInode> = SocketInode::new(new_socket);
423 
424         let mut file_mode = FileMode::O_RDWR;
425         if flags & SOCK_NONBLOCK.bits() != 0 {
426             file_mode |= FileMode::O_NONBLOCK;
427         }
428         if flags & SOCK_CLOEXEC.bits() != 0 {
429             file_mode |= FileMode::O_CLOEXEC;
430         }
431 
432         let new_fd = ProcessManager::current_pcb()
433             .fd_table()
434             .write()
435             .alloc_fd(File::new(new_socket, file_mode)?, None)?;
436         // kdebug!("accept: new_fd={}", new_fd);
437         if !addr.is_null() {
438             // kdebug!("accept: write remote_endpoint to user");
439             // 将对端地址写入用户空间
440             let sockaddr_in = SockAddr::from(remote_endpoint);
441             unsafe {
442                 sockaddr_in.write_to_user(addr, addrlen)?;
443             }
444         }
445         return Ok(new_fd as usize);
446     }
447 
448     /// @brief sys_getsockname系统调用的实际执行函数
449     ///
450     ///  Returns the current address to which the socket
451     ///     sockfd is bound, in the buffer pointed to by addr.
452     ///
453     /// @param fd 文件描述符
454     /// @param addr SockAddr
455     /// @param addrlen 地址长度
456     ///
457     /// @return 成功返回0,失败返回错误码
458     pub fn getsockname(
459         fd: usize,
460         addr: *mut SockAddr,
461         addrlen: *mut u32,
462     ) -> Result<usize, SystemError> {
463         if addr.is_null() {
464             return Err(SystemError::EINVAL);
465         }
466         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
467             .get_socket(fd as i32)
468             .ok_or(SystemError::EBADF)?;
469         let socket = socket.inner();
470         let endpoint: Endpoint = socket.endpoint().ok_or(SystemError::EINVAL)?;
471         drop(socket);
472 
473         let sockaddr_in = SockAddr::from(endpoint);
474         unsafe {
475             sockaddr_in.write_to_user(addr, addrlen)?;
476         }
477         return Ok(0);
478     }
479 
480     /// @brief sys_getpeername系统调用的实际执行函数
481     ///
482     /// @param fd 文件描述符
483     /// @param addr SockAddr
484     /// @param addrlen 地址长度
485     ///
486     /// @return 成功返回0,失败返回错误码
487     pub fn getpeername(
488         fd: usize,
489         addr: *mut SockAddr,
490         addrlen: *mut u32,
491     ) -> Result<usize, SystemError> {
492         if addr.is_null() {
493             return Err(SystemError::EINVAL);
494         }
495 
496         let socket: Arc<SocketInode> = ProcessManager::current_pcb()
497             .get_socket(fd as i32)
498             .ok_or(SystemError::EBADF)?;
499         let socket = socket.inner();
500         let endpoint: Endpoint = socket.peer_endpoint().ok_or(SystemError::EINVAL)?;
501         drop(socket);
502 
503         let sockaddr_in = SockAddr::from(endpoint);
504         unsafe {
505             sockaddr_in.write_to_user(addr, addrlen)?;
506         }
507         return Ok(0);
508     }
509 }
510 
511 // 参考资料: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html#tag_13_32
512 #[repr(C)]
513 #[derive(Debug, Clone, Copy)]
514 pub struct SockAddrIn {
515     pub sin_family: u16,
516     pub sin_port: u16,
517     pub sin_addr: u32,
518     pub sin_zero: [u8; 8],
519 }
520 
521 #[repr(C)]
522 #[derive(Debug, Clone, Copy)]
523 pub struct SockAddrUn {
524     pub sun_family: u16,
525     pub sun_path: [u8; 108],
526 }
527 
528 #[repr(C)]
529 #[derive(Debug, Clone, Copy)]
530 pub struct SockAddrLl {
531     pub sll_family: u16,
532     pub sll_protocol: u16,
533     pub sll_ifindex: u32,
534     pub sll_hatype: u16,
535     pub sll_pkttype: u8,
536     pub sll_halen: u8,
537     pub sll_addr: [u8; 8],
538 }
539 
540 #[repr(C)]
541 #[derive(Debug, Clone, Copy)]
542 pub struct SockAddrNl {
543     nl_family: u16,
544     nl_pad: u16,
545     nl_pid: u32,
546     nl_groups: u32,
547 }
548 
549 #[repr(C)]
550 #[derive(Debug, Clone, Copy)]
551 pub struct SockAddrPlaceholder {
552     pub family: u16,
553     pub data: [u8; 14],
554 }
555 
556 #[repr(C)]
557 #[derive(Clone, Copy)]
558 pub union SockAddr {
559     pub family: u16,
560     pub addr_in: SockAddrIn,
561     pub addr_un: SockAddrUn,
562     pub addr_ll: SockAddrLl,
563     pub addr_nl: SockAddrNl,
564     pub addr_ph: SockAddrPlaceholder,
565 }
566 
567 impl SockAddr {
568     /// @brief 把用户传入的SockAddr转换为Endpoint结构体
569     pub fn to_endpoint(addr: *const SockAddr, len: usize) -> Result<Endpoint, SystemError> {
570         verify_area(
571             VirtAddr::new(addr as usize),
572             core::mem::size_of::<SockAddr>(),
573         )
574         .map_err(|_| SystemError::EFAULT)?;
575 
576         let addr = unsafe { addr.as_ref() }.ok_or(SystemError::EFAULT)?;
577         unsafe {
578             match AddressFamily::try_from(addr.family)? {
579                 AddressFamily::INet => {
580                     if len < addr.len()? {
581                         return Err(SystemError::EINVAL);
582                     }
583 
584                     let addr_in: SockAddrIn = addr.addr_in;
585 
586                     let ip: wire::IpAddress = wire::IpAddress::from(wire::Ipv4Address::from_bytes(
587                         &u32::from_be(addr_in.sin_addr).to_be_bytes()[..],
588                     ));
589                     let port = u16::from_be(addr_in.sin_port);
590 
591                     return Ok(Endpoint::Ip(Some(wire::IpEndpoint::new(ip, port))));
592                 }
593                 AddressFamily::Unix => {
594                     let addr_un: SockAddrUn = addr.addr_un;
595 
596                     let path = CStr::from_bytes_until_nul(&addr_un.sun_path)
597                         .map_err(|_| SystemError::EINVAL)?
598                         .to_str()
599                         .map_err(|_| SystemError::EINVAL)?;
600 
601                     let fd = Syscall::open(path.as_ptr(), FileMode::O_RDWR.bits(), 0o755, true)?;
602 
603                     let binding = ProcessManager::current_pcb().fd_table();
604                     let fd_table_guard = binding.read();
605 
606                     let binding = fd_table_guard.get_file_by_fd(fd as i32).unwrap();
607                     let file = binding.lock();
608                     if file.file_type() != FileType::Socket {
609                         return Err(SystemError::ENOTSOCK);
610                     }
611                     let inode = file.inode();
612                     let socketinode = inode.as_any_ref().downcast_ref::<Arc<SocketInode>>();
613 
614                     return Ok(Endpoint::Inode(socketinode.cloned()));
615                 }
616                 AddressFamily::Packet => {
617                     // TODO: support packet socket
618                     return Err(SystemError::EINVAL);
619                 }
620                 AddressFamily::Netlink => {
621                     // TODO: support netlink socket
622                     return Err(SystemError::EINVAL);
623                 }
624                 _ => {
625                     return Err(SystemError::EINVAL);
626                 }
627             }
628         }
629     }
630 
631     /// @brief 获取地址长度
632     pub fn len(&self) -> Result<usize, SystemError> {
633         let ret = match AddressFamily::try_from(unsafe { self.family })? {
634             AddressFamily::INet => Ok(core::mem::size_of::<SockAddrIn>()),
635             AddressFamily::Packet => Ok(core::mem::size_of::<SockAddrLl>()),
636             AddressFamily::Netlink => Ok(core::mem::size_of::<SockAddrNl>()),
637             AddressFamily::Unix => Err(SystemError::EINVAL),
638             _ => Err(SystemError::EINVAL),
639         };
640 
641         return ret;
642     }
643 
644     /// @brief 把SockAddr的数据写入用户空间
645     ///
646     /// @param addr 用户空间的SockAddr的地址
647     /// @param len 要写入的长度
648     ///
649     /// @return 成功返回写入的长度,失败返回错误码
650     pub unsafe fn write_to_user(
651         &self,
652         addr: *mut SockAddr,
653         addr_len: *mut u32,
654     ) -> Result<usize, SystemError> {
655         // 当用户传入的地址或者长度为空时,直接返回0
656         if addr.is_null() || addr_len.is_null() {
657             return Ok(0);
658         }
659 
660         // 检查用户传入的地址是否合法
661         verify_area(
662             VirtAddr::new(addr as usize),
663             core::mem::size_of::<SockAddr>(),
664         )
665         .map_err(|_| SystemError::EFAULT)?;
666 
667         verify_area(
668             VirtAddr::new(addr_len as usize),
669             core::mem::size_of::<u32>(),
670         )
671         .map_err(|_| SystemError::EFAULT)?;
672 
673         let to_write = min(self.len()?, *addr_len as usize);
674         if to_write > 0 {
675             let buf = core::slice::from_raw_parts_mut(addr as *mut u8, to_write);
676             buf.copy_from_slice(core::slice::from_raw_parts(
677                 self as *const SockAddr as *const u8,
678                 to_write,
679             ));
680         }
681         *addr_len = self.len()? as u32;
682         return Ok(to_write);
683     }
684 }
685 
686 impl From<Endpoint> for SockAddr {
687     fn from(value: Endpoint) -> Self {
688         match value {
689             Endpoint::Ip(ip_endpoint) => {
690                 // 未指定地址
691                 if ip_endpoint.is_none() {
692                     return SockAddr {
693                         addr_ph: SockAddrPlaceholder {
694                             family: AddressFamily::Unspecified as u16,
695                             data: [0; 14],
696                         },
697                     };
698                 }
699                 // 指定了地址
700                 let ip_endpoint = ip_endpoint.unwrap();
701                 match ip_endpoint.addr {
702                     wire::IpAddress::Ipv4(ipv4_addr) => {
703                         let addr_in = SockAddrIn {
704                             sin_family: AddressFamily::INet as u16,
705                             sin_port: ip_endpoint.port.to_be(),
706                             sin_addr: u32::from_be_bytes(ipv4_addr.0).to_be(),
707                             sin_zero: [0; 8],
708                         };
709 
710                         return SockAddr { addr_in };
711                     }
712                     _ => {
713                         unimplemented!("not support ipv6");
714                     }
715                 }
716             }
717 
718             Endpoint::LinkLayer(link_endpoint) => {
719                 let addr_ll = SockAddrLl {
720                     sll_family: AddressFamily::Packet as u16,
721                     sll_protocol: 0,
722                     sll_ifindex: link_endpoint.interface as u32,
723                     sll_hatype: 0,
724                     sll_pkttype: 0,
725                     sll_halen: 0,
726                     sll_addr: [0; 8],
727                 };
728 
729                 return SockAddr { addr_ll };
730             }
731 
732             _ => {
733                 // todo: support other endpoint, like Netlink...
734                 unimplemented!("not support {value:?}");
735             }
736         }
737     }
738 }
739 
740 #[repr(C)]
741 #[derive(Debug, Clone, Copy)]
742 pub struct MsgHdr {
743     /// 指向一个SockAddr结构体的指针
744     pub msg_name: *mut SockAddr,
745     /// SockAddr结构体的大小
746     pub msg_namelen: u32,
747     /// scatter/gather array
748     pub msg_iov: *mut IoVec,
749     /// elements in msg_iov
750     pub msg_iovlen: usize,
751     /// 辅助数据
752     pub msg_control: *mut u8,
753     /// 辅助数据长度
754     pub msg_controllen: usize,
755     /// 接收到的消息的标志
756     pub msg_flags: u32,
757 }
758 
759 #[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, PartialEq, Eq)]
760 pub enum PosixIpProtocol {
761     /// Dummy protocol for TCP.
762     IP = 0,
763     /// Internet Control Message Protocol.
764     ICMP = 1,
765     /// Internet Group Management Protocol.
766     IGMP = 2,
767     /// IPIP tunnels (older KA9Q tunnels use 94).
768     IPIP = 4,
769     /// Transmission Control Protocol.
770     TCP = 6,
771     /// Exterior Gateway Protocol.
772     EGP = 8,
773     /// PUP protocol.
774     PUP = 12,
775     /// User Datagram Protocol.
776     UDP = 17,
777     /// XNS IDP protocol.
778     IDP = 22,
779     /// SO Transport Protocol Class 4.
780     TP = 29,
781     /// Datagram Congestion Control Protocol.
782     DCCP = 33,
783     /// IPv6-in-IPv4 tunnelling.
784     IPv6 = 41,
785     /// RSVP Protocol.
786     RSVP = 46,
787     /// Generic Routing Encapsulation. (Cisco GRE) (rfc 1701, 1702)
788     GRE = 47,
789     /// Encapsulation Security Payload protocol
790     ESP = 50,
791     /// Authentication Header protocol
792     AH = 51,
793     /// Multicast Transport Protocol.
794     MTP = 92,
795     /// IP option pseudo header for BEET
796     BEETPH = 94,
797     /// Encapsulation Header.
798     ENCAP = 98,
799     /// Protocol Independent Multicast.
800     PIM = 103,
801     /// Compression Header Protocol.
802     COMP = 108,
803     /// Stream Control Transport Protocol
804     SCTP = 132,
805     /// UDP-Lite protocol (RFC 3828)
806     UDPLITE = 136,
807     /// MPLS in IP (RFC 4023)
808     MPLSINIP = 137,
809     /// Ethernet-within-IPv6 Encapsulation
810     ETHERNET = 143,
811     /// Raw IP packets
812     RAW = 255,
813     /// Multipath TCP connection
814     MPTCP = 262,
815 }
816 
817 impl TryFrom<u16> for PosixIpProtocol {
818     type Error = SystemError;
819 
820     fn try_from(value: u16) -> Result<Self, Self::Error> {
821         match <Self as FromPrimitive>::from_u16(value) {
822             Some(p) => Ok(p),
823             None => Err(SystemError::EPROTONOSUPPORT),
824         }
825     }
826 }
827 
828 impl From<PosixIpProtocol> for u16 {
829     fn from(value: PosixIpProtocol) -> Self {
830         <PosixIpProtocol as ToPrimitive>::to_u16(&value).unwrap()
831     }
832 }
833 
834 #[allow(non_camel_case_types)]
835 #[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, PartialEq, Eq)]
836 pub enum PosixSocketOption {
837     SO_DEBUG = 1,
838     SO_REUSEADDR = 2,
839     SO_TYPE = 3,
840     SO_ERROR = 4,
841     SO_DONTROUTE = 5,
842     SO_BROADCAST = 6,
843     SO_SNDBUF = 7,
844     SO_RCVBUF = 8,
845     SO_SNDBUFFORCE = 32,
846     SO_RCVBUFFORCE = 33,
847     SO_KEEPALIVE = 9,
848     SO_OOBINLINE = 10,
849     SO_NO_CHECK = 11,
850     SO_PRIORITY = 12,
851     SO_LINGER = 13,
852     SO_BSDCOMPAT = 14,
853     SO_REUSEPORT = 15,
854     SO_PASSCRED = 16,
855     SO_PEERCRED = 17,
856     SO_RCVLOWAT = 18,
857     SO_SNDLOWAT = 19,
858     SO_RCVTIMEO_OLD = 20,
859     SO_SNDTIMEO_OLD = 21,
860 
861     SO_SECURITY_AUTHENTICATION = 22,
862     SO_SECURITY_ENCRYPTION_TRANSPORT = 23,
863     SO_SECURITY_ENCRYPTION_NETWORK = 24,
864 
865     SO_BINDTODEVICE = 25,
866 
867     /// 与SO_GET_FILTER相同
868     SO_ATTACH_FILTER = 26,
869     SO_DETACH_FILTER = 27,
870 
871     SO_PEERNAME = 28,
872 
873     SO_ACCEPTCONN = 30,
874 
875     SO_PEERSEC = 31,
876     SO_PASSSEC = 34,
877 
878     SO_MARK = 36,
879 
880     SO_PROTOCOL = 38,
881     SO_DOMAIN = 39,
882 
883     SO_RXQ_OVFL = 40,
884 
885     /// 与SCM_WIFI_STATUS相同
886     SO_WIFI_STATUS = 41,
887     SO_PEEK_OFF = 42,
888 
889     /* Instruct lower device to use last 4-bytes of skb data as FCS */
890     SO_NOFCS = 43,
891 
892     SO_LOCK_FILTER = 44,
893     SO_SELECT_ERR_QUEUE = 45,
894     SO_BUSY_POLL = 46,
895     SO_MAX_PACING_RATE = 47,
896     SO_BPF_EXTENSIONS = 48,
897     SO_INCOMING_CPU = 49,
898     SO_ATTACH_BPF = 50,
899     // SO_DETACH_BPF = SO_DETACH_FILTER,
900     SO_ATTACH_REUSEPORT_CBPF = 51,
901     SO_ATTACH_REUSEPORT_EBPF = 52,
902 
903     SO_CNX_ADVICE = 53,
904     SCM_TIMESTAMPING_OPT_STATS = 54,
905     SO_MEMINFO = 55,
906     SO_INCOMING_NAPI_ID = 56,
907     SO_COOKIE = 57,
908     SCM_TIMESTAMPING_PKTINFO = 58,
909     SO_PEERGROUPS = 59,
910     SO_ZEROCOPY = 60,
911     /// 与SCM_TXTIME相同
912     SO_TXTIME = 61,
913 
914     SO_BINDTOIFINDEX = 62,
915 
916     SO_TIMESTAMP_OLD = 29,
917     SO_TIMESTAMPNS_OLD = 35,
918     SO_TIMESTAMPING_OLD = 37,
919     SO_TIMESTAMP_NEW = 63,
920     SO_TIMESTAMPNS_NEW = 64,
921     SO_TIMESTAMPING_NEW = 65,
922 
923     SO_RCVTIMEO_NEW = 66,
924     SO_SNDTIMEO_NEW = 67,
925 
926     SO_DETACH_REUSEPORT_BPF = 68,
927 
928     SO_PREFER_BUSY_POLL = 69,
929     SO_BUSY_POLL_BUDGET = 70,
930 
931     SO_NETNS_COOKIE = 71,
932     SO_BUF_LOCK = 72,
933     SO_RESERVE_MEM = 73,
934     SO_TXREHASH = 74,
935     SO_RCVMARK = 75,
936 }
937 
938 impl TryFrom<i32> for PosixSocketOption {
939     type Error = SystemError;
940 
941     fn try_from(value: i32) -> Result<Self, Self::Error> {
942         match <Self as FromPrimitive>::from_i32(value) {
943             Some(p) => Ok(p),
944             None => Err(SystemError::EINVAL),
945         }
946     }
947 }
948 
949 impl From<PosixSocketOption> for i32 {
950     fn from(value: PosixSocketOption) -> Self {
951         <PosixSocketOption as ToPrimitive>::to_i32(&value).unwrap()
952     }
953 }
954 
955 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
956 pub enum PosixTcpSocketOptions {
957     /// Turn off Nagle's algorithm.
958     NoDelay = 1,
959     /// Limit MSS.
960     MaxSegment = 2,
961     /// Never send partially complete segments.
962     Cork = 3,
963     /// Start keeplives after this period.
964     KeepIdle = 4,
965     /// Interval between keepalives.
966     KeepIntvl = 5,
967     /// Number of keepalives before death.
968     KeepCnt = 6,
969     /// Number of SYN retransmits.
970     Syncnt = 7,
971     /// Lifetime for orphaned FIN-WAIT-2 state.
972     Linger2 = 8,
973     /// Wake up listener only when data arrive.
974     DeferAccept = 9,
975     /// Bound advertised window
976     WindowClamp = 10,
977     /// Information about this connection.
978     Info = 11,
979     /// Block/reenable quick acks.
980     QuickAck = 12,
981     /// Congestion control algorithm.
982     Congestion = 13,
983     /// TCP MD5 Signature (RFC2385).
984     Md5Sig = 14,
985     /// Use linear timeouts for thin streams
986     ThinLinearTimeouts = 16,
987     /// Fast retrans. after 1 dupack.
988     ThinDupack = 17,
989     /// How long for loss retry before timeout.
990     UserTimeout = 18,
991     /// TCP sock is under repair right now.
992     Repair = 19,
993     RepairQueue = 20,
994     QueueSeq = 21,
995     RepairOptions = 22,
996     /// Enable FastOpen on listeners
997     FastOpen = 23,
998     Timestamp = 24,
999     /// Limit number of unsent bytes in write queue.
1000     NotSentLowat = 25,
1001     /// Get Congestion Control (optional) info.
1002     CCInfo = 26,
1003     /// Record SYN headers for new connections.
1004     SaveSyn = 27,
1005     /// Get SYN headers recorded for connection.
1006     SavedSyn = 28,
1007     /// Get/set window parameters.
1008     RepairWindow = 29,
1009     /// Attempt FastOpen with connect.
1010     FastOpenConnect = 30,
1011     /// Attach a ULP to a TCP connection.
1012     ULP = 31,
1013     /// TCP MD5 Signature with extensions.
1014     Md5SigExt = 32,
1015     /// Set the key for Fast Open(cookie).
1016     FastOpenKey = 33,
1017     /// Enable TFO without a TFO cookie.
1018     FastOpenNoCookie = 34,
1019     ZeroCopyReceive = 35,
1020     /// Notify bytes available to read as a cmsg on read.
1021     /// 与TCP_CM_INQ相同
1022     INQ = 36,
1023     /// delay outgoing packets by XX usec
1024     TxDelay = 37,
1025 }
1026 
1027 impl TryFrom<i32> for PosixTcpSocketOptions {
1028     type Error = SystemError;
1029 
1030     fn try_from(value: i32) -> Result<Self, Self::Error> {
1031         match <Self as FromPrimitive>::from_i32(value) {
1032             Some(p) => Ok(p),
1033             None => Err(SystemError::EINVAL),
1034         }
1035     }
1036 }
1037 
1038 impl From<PosixTcpSocketOptions> for i32 {
1039     fn from(val: PosixTcpSocketOptions) -> Self {
1040         <PosixTcpSocketOptions as ToPrimitive>::to_i32(&val).unwrap()
1041     }
1042 }
1043