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