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