xref: /DragonOS/kernel/src/net/syscall.rs (revision f049d1af01da7b92f312245ed411b22475b76065) !
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 file = fd_table_guard.get_file_by_fd(fd as i32).unwrap();
607                     if file.file_type() != FileType::Socket {
608                         return Err(SystemError::ENOTSOCK);
609                     }
610                     let inode = file.inode();
611                     let socketinode = inode.as_any_ref().downcast_ref::<Arc<SocketInode>>();
612 
613                     return Ok(Endpoint::Inode(socketinode.cloned()));
614                 }
615                 AddressFamily::Packet => {
616                     // TODO: support packet socket
617                     return Err(SystemError::EINVAL);
618                 }
619                 AddressFamily::Netlink => {
620                     // TODO: support netlink socket
621                     return Err(SystemError::EINVAL);
622                 }
623                 _ => {
624                     return Err(SystemError::EINVAL);
625                 }
626             }
627         }
628     }
629 
630     /// @brief 获取地址长度
631     pub fn len(&self) -> Result<usize, SystemError> {
632         let ret = match AddressFamily::try_from(unsafe { self.family })? {
633             AddressFamily::INet => Ok(core::mem::size_of::<SockAddrIn>()),
634             AddressFamily::Packet => Ok(core::mem::size_of::<SockAddrLl>()),
635             AddressFamily::Netlink => Ok(core::mem::size_of::<SockAddrNl>()),
636             AddressFamily::Unix => Err(SystemError::EINVAL),
637             _ => Err(SystemError::EINVAL),
638         };
639 
640         return ret;
641     }
642 
643     /// @brief 把SockAddr的数据写入用户空间
644     ///
645     /// @param addr 用户空间的SockAddr的地址
646     /// @param len 要写入的长度
647     ///
648     /// @return 成功返回写入的长度,失败返回错误码
649     pub unsafe fn write_to_user(
650         &self,
651         addr: *mut SockAddr,
652         addr_len: *mut u32,
653     ) -> Result<usize, SystemError> {
654         // 当用户传入的地址或者长度为空时,直接返回0
655         if addr.is_null() || addr_len.is_null() {
656             return Ok(0);
657         }
658 
659         // 检查用户传入的地址是否合法
660         verify_area(
661             VirtAddr::new(addr as usize),
662             core::mem::size_of::<SockAddr>(),
663         )
664         .map_err(|_| SystemError::EFAULT)?;
665 
666         verify_area(
667             VirtAddr::new(addr_len as usize),
668             core::mem::size_of::<u32>(),
669         )
670         .map_err(|_| SystemError::EFAULT)?;
671 
672         let to_write = min(self.len()?, *addr_len as usize);
673         if to_write > 0 {
674             let buf = core::slice::from_raw_parts_mut(addr as *mut u8, to_write);
675             buf.copy_from_slice(core::slice::from_raw_parts(
676                 self as *const SockAddr as *const u8,
677                 to_write,
678             ));
679         }
680         *addr_len = self.len()? as u32;
681         return Ok(to_write);
682     }
683 }
684 
685 impl From<Endpoint> for SockAddr {
686     fn from(value: Endpoint) -> Self {
687         match value {
688             Endpoint::Ip(ip_endpoint) => {
689                 // 未指定地址
690                 if ip_endpoint.is_none() {
691                     return SockAddr {
692                         addr_ph: SockAddrPlaceholder {
693                             family: AddressFamily::Unspecified as u16,
694                             data: [0; 14],
695                         },
696                     };
697                 }
698                 // 指定了地址
699                 let ip_endpoint = ip_endpoint.unwrap();
700                 match ip_endpoint.addr {
701                     wire::IpAddress::Ipv4(ipv4_addr) => {
702                         let addr_in = SockAddrIn {
703                             sin_family: AddressFamily::INet as u16,
704                             sin_port: ip_endpoint.port.to_be(),
705                             sin_addr: u32::from_be_bytes(ipv4_addr.0).to_be(),
706                             sin_zero: [0; 8],
707                         };
708 
709                         return SockAddr { addr_in };
710                     }
711                     _ => {
712                         unimplemented!("not support ipv6");
713                     }
714                 }
715             }
716 
717             Endpoint::LinkLayer(link_endpoint) => {
718                 let addr_ll = SockAddrLl {
719                     sll_family: AddressFamily::Packet as u16,
720                     sll_protocol: 0,
721                     sll_ifindex: link_endpoint.interface as u32,
722                     sll_hatype: 0,
723                     sll_pkttype: 0,
724                     sll_halen: 0,
725                     sll_addr: [0; 8],
726                 };
727 
728                 return SockAddr { addr_ll };
729             }
730 
731             _ => {
732                 // todo: support other endpoint, like Netlink...
733                 unimplemented!("not support {value:?}");
734             }
735         }
736     }
737 }
738 
739 #[repr(C)]
740 #[derive(Debug, Clone, Copy)]
741 pub struct MsgHdr {
742     /// 指向一个SockAddr结构体的指针
743     pub msg_name: *mut SockAddr,
744     /// SockAddr结构体的大小
745     pub msg_namelen: u32,
746     /// scatter/gather array
747     pub msg_iov: *mut IoVec,
748     /// elements in msg_iov
749     pub msg_iovlen: usize,
750     /// 辅助数据
751     pub msg_control: *mut u8,
752     /// 辅助数据长度
753     pub msg_controllen: usize,
754     /// 接收到的消息的标志
755     pub msg_flags: u32,
756 }
757 
758 #[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, PartialEq, Eq)]
759 pub enum PosixIpProtocol {
760     /// Dummy protocol for TCP.
761     IP = 0,
762     /// Internet Control Message Protocol.
763     ICMP = 1,
764     /// Internet Group Management Protocol.
765     IGMP = 2,
766     /// IPIP tunnels (older KA9Q tunnels use 94).
767     IPIP = 4,
768     /// Transmission Control Protocol.
769     TCP = 6,
770     /// Exterior Gateway Protocol.
771     EGP = 8,
772     /// PUP protocol.
773     PUP = 12,
774     /// User Datagram Protocol.
775     UDP = 17,
776     /// XNS IDP protocol.
777     IDP = 22,
778     /// SO Transport Protocol Class 4.
779     TP = 29,
780     /// Datagram Congestion Control Protocol.
781     DCCP = 33,
782     /// IPv6-in-IPv4 tunnelling.
783     IPv6 = 41,
784     /// RSVP Protocol.
785     RSVP = 46,
786     /// Generic Routing Encapsulation. (Cisco GRE) (rfc 1701, 1702)
787     GRE = 47,
788     /// Encapsulation Security Payload protocol
789     ESP = 50,
790     /// Authentication Header protocol
791     AH = 51,
792     /// Multicast Transport Protocol.
793     MTP = 92,
794     /// IP option pseudo header for BEET
795     BEETPH = 94,
796     /// Encapsulation Header.
797     ENCAP = 98,
798     /// Protocol Independent Multicast.
799     PIM = 103,
800     /// Compression Header Protocol.
801     COMP = 108,
802     /// Stream Control Transport Protocol
803     SCTP = 132,
804     /// UDP-Lite protocol (RFC 3828)
805     UDPLITE = 136,
806     /// MPLS in IP (RFC 4023)
807     MPLSINIP = 137,
808     /// Ethernet-within-IPv6 Encapsulation
809     ETHERNET = 143,
810     /// Raw IP packets
811     RAW = 255,
812     /// Multipath TCP connection
813     MPTCP = 262,
814 }
815 
816 impl TryFrom<u16> for PosixIpProtocol {
817     type Error = SystemError;
818 
819     fn try_from(value: u16) -> Result<Self, Self::Error> {
820         match <Self as FromPrimitive>::from_u16(value) {
821             Some(p) => Ok(p),
822             None => Err(SystemError::EPROTONOSUPPORT),
823         }
824     }
825 }
826 
827 impl From<PosixIpProtocol> for u16 {
828     fn from(value: PosixIpProtocol) -> Self {
829         <PosixIpProtocol as ToPrimitive>::to_u16(&value).unwrap()
830     }
831 }
832 
833 #[allow(non_camel_case_types)]
834 #[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, PartialEq, Eq)]
835 pub enum PosixSocketOption {
836     SO_DEBUG = 1,
837     SO_REUSEADDR = 2,
838     SO_TYPE = 3,
839     SO_ERROR = 4,
840     SO_DONTROUTE = 5,
841     SO_BROADCAST = 6,
842     SO_SNDBUF = 7,
843     SO_RCVBUF = 8,
844     SO_SNDBUFFORCE = 32,
845     SO_RCVBUFFORCE = 33,
846     SO_KEEPALIVE = 9,
847     SO_OOBINLINE = 10,
848     SO_NO_CHECK = 11,
849     SO_PRIORITY = 12,
850     SO_LINGER = 13,
851     SO_BSDCOMPAT = 14,
852     SO_REUSEPORT = 15,
853     SO_PASSCRED = 16,
854     SO_PEERCRED = 17,
855     SO_RCVLOWAT = 18,
856     SO_SNDLOWAT = 19,
857     SO_RCVTIMEO_OLD = 20,
858     SO_SNDTIMEO_OLD = 21,
859 
860     SO_SECURITY_AUTHENTICATION = 22,
861     SO_SECURITY_ENCRYPTION_TRANSPORT = 23,
862     SO_SECURITY_ENCRYPTION_NETWORK = 24,
863 
864     SO_BINDTODEVICE = 25,
865 
866     /// 与SO_GET_FILTER相同
867     SO_ATTACH_FILTER = 26,
868     SO_DETACH_FILTER = 27,
869 
870     SO_PEERNAME = 28,
871 
872     SO_ACCEPTCONN = 30,
873 
874     SO_PEERSEC = 31,
875     SO_PASSSEC = 34,
876 
877     SO_MARK = 36,
878 
879     SO_PROTOCOL = 38,
880     SO_DOMAIN = 39,
881 
882     SO_RXQ_OVFL = 40,
883 
884     /// 与SCM_WIFI_STATUS相同
885     SO_WIFI_STATUS = 41,
886     SO_PEEK_OFF = 42,
887 
888     /* Instruct lower device to use last 4-bytes of skb data as FCS */
889     SO_NOFCS = 43,
890 
891     SO_LOCK_FILTER = 44,
892     SO_SELECT_ERR_QUEUE = 45,
893     SO_BUSY_POLL = 46,
894     SO_MAX_PACING_RATE = 47,
895     SO_BPF_EXTENSIONS = 48,
896     SO_INCOMING_CPU = 49,
897     SO_ATTACH_BPF = 50,
898     // SO_DETACH_BPF = SO_DETACH_FILTER,
899     SO_ATTACH_REUSEPORT_CBPF = 51,
900     SO_ATTACH_REUSEPORT_EBPF = 52,
901 
902     SO_CNX_ADVICE = 53,
903     SCM_TIMESTAMPING_OPT_STATS = 54,
904     SO_MEMINFO = 55,
905     SO_INCOMING_NAPI_ID = 56,
906     SO_COOKIE = 57,
907     SCM_TIMESTAMPING_PKTINFO = 58,
908     SO_PEERGROUPS = 59,
909     SO_ZEROCOPY = 60,
910     /// 与SCM_TXTIME相同
911     SO_TXTIME = 61,
912 
913     SO_BINDTOIFINDEX = 62,
914 
915     SO_TIMESTAMP_OLD = 29,
916     SO_TIMESTAMPNS_OLD = 35,
917     SO_TIMESTAMPING_OLD = 37,
918     SO_TIMESTAMP_NEW = 63,
919     SO_TIMESTAMPNS_NEW = 64,
920     SO_TIMESTAMPING_NEW = 65,
921 
922     SO_RCVTIMEO_NEW = 66,
923     SO_SNDTIMEO_NEW = 67,
924 
925     SO_DETACH_REUSEPORT_BPF = 68,
926 
927     SO_PREFER_BUSY_POLL = 69,
928     SO_BUSY_POLL_BUDGET = 70,
929 
930     SO_NETNS_COOKIE = 71,
931     SO_BUF_LOCK = 72,
932     SO_RESERVE_MEM = 73,
933     SO_TXREHASH = 74,
934     SO_RCVMARK = 75,
935 }
936 
937 impl TryFrom<i32> for PosixSocketOption {
938     type Error = SystemError;
939 
940     fn try_from(value: i32) -> Result<Self, Self::Error> {
941         match <Self as FromPrimitive>::from_i32(value) {
942             Some(p) => Ok(p),
943             None => Err(SystemError::EINVAL),
944         }
945     }
946 }
947 
948 impl From<PosixSocketOption> for i32 {
949     fn from(value: PosixSocketOption) -> Self {
950         <PosixSocketOption as ToPrimitive>::to_i32(&value).unwrap()
951     }
952 }
953 
954 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
955 pub enum PosixTcpSocketOptions {
956     /// Turn off Nagle's algorithm.
957     NoDelay = 1,
958     /// Limit MSS.
959     MaxSegment = 2,
960     /// Never send partially complete segments.
961     Cork = 3,
962     /// Start keeplives after this period.
963     KeepIdle = 4,
964     /// Interval between keepalives.
965     KeepIntvl = 5,
966     /// Number of keepalives before death.
967     KeepCnt = 6,
968     /// Number of SYN retransmits.
969     Syncnt = 7,
970     /// Lifetime for orphaned FIN-WAIT-2 state.
971     Linger2 = 8,
972     /// Wake up listener only when data arrive.
973     DeferAccept = 9,
974     /// Bound advertised window
975     WindowClamp = 10,
976     /// Information about this connection.
977     Info = 11,
978     /// Block/reenable quick acks.
979     QuickAck = 12,
980     /// Congestion control algorithm.
981     Congestion = 13,
982     /// TCP MD5 Signature (RFC2385).
983     Md5Sig = 14,
984     /// Use linear timeouts for thin streams
985     ThinLinearTimeouts = 16,
986     /// Fast retrans. after 1 dupack.
987     ThinDupack = 17,
988     /// How long for loss retry before timeout.
989     UserTimeout = 18,
990     /// TCP sock is under repair right now.
991     Repair = 19,
992     RepairQueue = 20,
993     QueueSeq = 21,
994     RepairOptions = 22,
995     /// Enable FastOpen on listeners
996     FastOpen = 23,
997     Timestamp = 24,
998     /// Limit number of unsent bytes in write queue.
999     NotSentLowat = 25,
1000     /// Get Congestion Control (optional) info.
1001     CCInfo = 26,
1002     /// Record SYN headers for new connections.
1003     SaveSyn = 27,
1004     /// Get SYN headers recorded for connection.
1005     SavedSyn = 28,
1006     /// Get/set window parameters.
1007     RepairWindow = 29,
1008     /// Attempt FastOpen with connect.
1009     FastOpenConnect = 30,
1010     /// Attach a ULP to a TCP connection.
1011     ULP = 31,
1012     /// TCP MD5 Signature with extensions.
1013     Md5SigExt = 32,
1014     /// Set the key for Fast Open(cookie).
1015     FastOpenKey = 33,
1016     /// Enable TFO without a TFO cookie.
1017     FastOpenNoCookie = 34,
1018     ZeroCopyReceive = 35,
1019     /// Notify bytes available to read as a cmsg on read.
1020     /// 与TCP_CM_INQ相同
1021     INQ = 36,
1022     /// delay outgoing packets by XX usec
1023     TxDelay = 37,
1024 }
1025 
1026 impl TryFrom<i32> for PosixTcpSocketOptions {
1027     type Error = SystemError;
1028 
1029     fn try_from(value: i32) -> Result<Self, Self::Error> {
1030         match <Self as FromPrimitive>::from_i32(value) {
1031             Some(p) => Ok(p),
1032             None => Err(SystemError::EINVAL),
1033         }
1034     }
1035 }
1036 
1037 impl From<PosixTcpSocketOptions> for i32 {
1038     fn from(val: PosixTcpSocketOptions) -> Self {
1039         <PosixTcpSocketOptions as ToPrimitive>::to_i32(&val).unwrap()
1040     }
1041 }
1042