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