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