1 use core::{ 2 fmt::{self, Debug}, 3 sync::atomic::AtomicUsize, 4 }; 5 6 use alloc::{boxed::Box, collections::BTreeMap, sync::Arc}; 7 8 use crate::{driver::net::NetDriver, libs::rwlock::RwLock, syscall::SystemError}; 9 use smoltcp::wire::IpEndpoint; 10 11 use self::socket::SocketMetadata; 12 13 pub mod endpoints; 14 pub mod net_core; 15 pub mod socket; 16 17 lazy_static! { 18 /// @brief 所有网络接口的列表 19 pub static ref NET_DRIVERS: RwLock<BTreeMap<usize, Arc<dyn NetDriver>>> = RwLock::new(BTreeMap::new()); 20 } 21 22 /// @brief 生成网络接口的id (全局自增) 23 pub fn generate_iface_id() -> usize { 24 static IFACE_ID: AtomicUsize = AtomicUsize::new(0); 25 return IFACE_ID 26 .fetch_add(1, core::sync::atomic::Ordering::SeqCst) 27 .into(); 28 } 29 30 /// @brief 用于指定socket的关闭类型 31 #[derive(Debug, Clone)] 32 pub enum ShutdownType { 33 ShutRd, // Disables further receive operations. 34 ShutWr, // Disables further send operations. 35 ShutRdwr, // Disables further send and receive operations. 36 } 37 38 #[derive(Debug, Clone)] 39 pub enum Endpoint { 40 /// 链路层端点 41 LinkLayer(endpoints::LinkLayerEndpoint), 42 /// 网络层端点 43 Ip(IpEndpoint), 44 // todo: 增加NetLink机制后,增加NetLink端点 45 } 46 47 pub trait Socket: Sync + Send + Debug { 48 /// @brief 从socket中读取数据,如果socket是阻塞的,那么直到读取到数据才返回 49 /// 50 /// @param buf 读取到的数据存放的缓冲区 51 /// 52 /// @return - 成功:(返回读取的数据的长度,读取数据的端点). 53 /// - 失败:错误码 54 fn read(&self, buf: &mut [u8]) -> Result<(usize, Endpoint), SystemError>; 55 56 /// @brief 向socket中写入数据。如果socket是阻塞的,那么直到写入的数据全部写入socket中才返回 57 /// 58 /// @param buf 要写入的数据 59 /// @param to 要写入的目的端点,如果是None,那么写入的数据将会被丢弃 60 /// 61 /// @return 返回写入的数据的长度 62 fn write(&self, buf: &[u8], to: Option<Endpoint>) -> Result<usize, SystemError>; 63 64 /// @brief 对应于POSIX的connect函数,用于连接到指定的远程服务器端点 65 /// 66 /// It is used to establish a connection to a remote server. 67 /// When a socket is connected to a remote server, 68 /// the operating system will establish a network connection with the server 69 /// and allow data to be sent and received between the local socket and the remote server. 70 /// 71 /// @param endpoint 要连接的端点 72 /// 73 /// @return 返回连接是否成功 74 fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError>; 75 76 /// @brief 对应于POSIX的bind函数,用于绑定到本机指定的端点 77 /// 78 /// The bind() function is used to associate a socket with a particular IP address and port number on the local machine. 79 /// 80 /// @param endpoint 要绑定的端点 81 /// 82 /// @return 返回绑定是否成功 83 fn bind(&self, _endpoint: Endpoint) -> Result<(), SystemError> { 84 return Err(SystemError::ENOSYS); 85 } 86 87 /// @brief 对应于 POSIX 的 shutdown 函数,用于关闭socket。 88 /// 89 /// shutdown() 函数用于启动网络连接的正常关闭。 90 /// 当在两个端点之间建立网络连接时,任一端点都可以通过调用其端点对象上的 shutdown() 函数来启动关闭序列。 91 /// 此函数向远程端点发送关闭消息以指示本地端点不再接受新数据。 92 /// 93 /// @return 返回是否成功关闭 94 fn shutdown(&self, _type: ShutdownType) -> Result<(), SystemError> { 95 return Err(SystemError::ENOSYS); 96 } 97 98 /// @brief 对应于POSIX的listen函数,用于监听端点 99 /// 100 /// @param backlog 最大的等待连接数 101 /// 102 /// @return 返回监听是否成功 103 fn listen(&mut self, _backlog: usize) -> Result<(), SystemError> { 104 return Err(SystemError::ENOSYS); 105 } 106 107 /// @brief 对应于POSIX的accept函数,用于接受连接 108 /// 109 /// @param endpoint 用于返回连接的端点 110 /// 111 /// @return 返回接受连接是否成功 112 fn accept(&mut self) -> Result<(Box<dyn Socket>, Endpoint), SystemError> { 113 return Err(SystemError::ENOSYS); 114 } 115 116 /// @brief 获取socket的端点 117 /// 118 /// @return 返回socket的端点 119 fn endpoint(&self) -> Option<Endpoint> { 120 return None; 121 } 122 123 /// @brief 获取socket的对端端点 124 /// 125 /// @return 返回socket的对端端点 126 fn peer_endpoint(&self) -> Option<Endpoint> { 127 return None; 128 } 129 130 /// @brief 131 /// The purpose of the poll function is to provide 132 /// a non-blocking way to check if a socket is ready for reading or writing, 133 /// so that you can efficiently handle multiple sockets in a single thread or event loop. 134 /// 135 /// @return (in, out, err) 136 /// 137 /// The first boolean value indicates whether the socket is ready for reading. If it is true, then there is data available to be read from the socket without blocking. 138 /// The second boolean value indicates whether the socket is ready for writing. If it is true, then data can be written to the socket without blocking. 139 /// The third boolean value indicates whether the socket has encountered an error condition. If it is true, then the socket is in an error state and should be closed or reset 140 /// 141 fn poll(&self) -> (bool, bool, bool) { 142 return (false, false, false); 143 } 144 145 /// @brief socket的ioctl函数 146 /// 147 /// @param cmd ioctl命令 148 /// @param arg0 ioctl命令的第一个参数 149 /// @param arg1 ioctl命令的第二个参数 150 /// @param arg2 ioctl命令的第三个参数 151 /// 152 /// @return 返回ioctl命令的返回值 153 fn ioctl( 154 &self, 155 _cmd: usize, 156 _arg0: usize, 157 _arg1: usize, 158 _arg2: usize, 159 ) -> Result<usize, SystemError> { 160 return Ok(0); 161 } 162 163 /// @brief 获取socket的元数据 164 fn metadata(&self) -> Result<SocketMetadata, SystemError>; 165 166 fn box_clone(&self) -> Box<dyn Socket>; 167 } 168 169 impl Clone for Box<dyn Socket> { 170 fn clone(&self) -> Box<dyn Socket> { 171 self.box_clone() 172 } 173 } 174 175 /// IP datagram encapsulated protocol. 176 #[derive(Debug, PartialEq, Eq, Clone, Copy)] 177 #[repr(u8)] 178 pub enum Protocol { 179 HopByHop = 0x00, 180 Icmp = 0x01, 181 Igmp = 0x02, 182 Tcp = 0x06, 183 Udp = 0x11, 184 Ipv6Route = 0x2b, 185 Ipv6Frag = 0x2c, 186 Icmpv6 = 0x3a, 187 Ipv6NoNxt = 0x3b, 188 Ipv6Opts = 0x3c, 189 Unknown(u8), 190 } 191 192 impl fmt::Display for Protocol { 193 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 194 match *self { 195 Protocol::HopByHop => write!(f, "Hop-by-Hop"), 196 Protocol::Icmp => write!(f, "ICMP"), 197 Protocol::Igmp => write!(f, "IGMP"), 198 Protocol::Tcp => write!(f, "TCP"), 199 Protocol::Udp => write!(f, "UDP"), 200 Protocol::Ipv6Route => write!(f, "IPv6-Route"), 201 Protocol::Ipv6Frag => write!(f, "IPv6-Frag"), 202 Protocol::Icmpv6 => write!(f, "ICMPv6"), 203 Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"), 204 Protocol::Ipv6Opts => write!(f, "IPv6-Opts"), 205 Protocol::Unknown(id) => write!(f, "0x{id:02x}"), 206 } 207 } 208 } 209 210 impl From<smoltcp::wire::IpProtocol> for Protocol { 211 fn from(value: smoltcp::wire::IpProtocol) -> Self { 212 let x: u8 = value.into(); 213 Protocol::from(x) 214 } 215 } 216 217 impl From<u8> for Protocol { 218 fn from(value: u8) -> Self { 219 match value { 220 0x00 => Protocol::HopByHop, 221 0x01 => Protocol::Icmp, 222 0x02 => Protocol::Igmp, 223 0x06 => Protocol::Tcp, 224 0x11 => Protocol::Udp, 225 0x2b => Protocol::Ipv6Route, 226 0x2c => Protocol::Ipv6Frag, 227 0x3a => Protocol::Icmpv6, 228 0x3b => Protocol::Ipv6NoNxt, 229 0x3c => Protocol::Ipv6Opts, 230 _ => Protocol::Unknown(value), 231 } 232 } 233 } 234 235 impl Into<u8> for Protocol { 236 fn into(self) -> u8 { 237 match self { 238 Protocol::HopByHop => 0x00, 239 Protocol::Icmp => 0x01, 240 Protocol::Igmp => 0x02, 241 Protocol::Tcp => 0x06, 242 Protocol::Udp => 0x11, 243 Protocol::Ipv6Route => 0x2b, 244 Protocol::Ipv6Frag => 0x2c, 245 Protocol::Icmpv6 => 0x3a, 246 Protocol::Ipv6NoNxt => 0x3b, 247 Protocol::Ipv6Opts => 0x3c, 248 Protocol::Unknown(id) => id, 249 } 250 } 251 } 252