1 use core::{
2 fmt::{self, Debug},
3 sync::atomic::AtomicUsize,
4 };
5
6 use alloc::{collections::BTreeMap, sync::Arc};
7
8 use crate::{driver::net::NetDevice, libs::rwlock::RwLock};
9 use smoltcp::wire::IpEndpoint;
10
11 use self::socket::SocketInode;
12
13 pub mod event_poll;
14 pub mod net_core;
15 pub mod socket;
16 pub mod syscall;
17
18 lazy_static! {
19 /// # 所有网络接口的列表
20 /// 这个列表在中断上下文会使用到,因此需要irqsave
21 pub static ref NET_DEVICES: RwLock<BTreeMap<usize, Arc<dyn NetDevice>>> = RwLock::new(BTreeMap::new());
22 }
23
24 /// 生成网络接口的id (全局自增)
generate_iface_id() -> usize25 pub fn generate_iface_id() -> usize {
26 static IFACE_ID: AtomicUsize = AtomicUsize::new(0);
27 return IFACE_ID.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
28 }
29
30 bitflags! {
31 /// @brief 用于指定socket的关闭类型
32 /// 参考:https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573
33 pub struct ShutdownType: u8 {
34 const RCV_SHUTDOWN = 1;
35 const SEND_SHUTDOWN = 2;
36 const SHUTDOWN_MASK = 3;
37 }
38 }
39
40 #[derive(Debug, Clone)]
41 pub enum Endpoint {
42 /// 链路层端点
43 LinkLayer(LinkLayerEndpoint),
44 /// 网络层端点
45 Ip(Option<IpEndpoint>),
46 /// inode端点
47 Inode(Option<Arc<SocketInode>>),
48 // todo: 增加NetLink机制后,增加NetLink端点
49 }
50
51 /// @brief 链路层端点
52 #[derive(Debug, Clone)]
53 pub struct LinkLayerEndpoint {
54 /// 网卡的接口号
55 pub interface: usize,
56 }
57
58 impl LinkLayerEndpoint {
59 /// @brief 创建一个链路层端点
60 ///
61 /// @param interface 网卡的接口号
62 ///
63 /// @return 返回创建的链路层端点
new(interface: usize) -> Self64 pub fn new(interface: usize) -> Self {
65 Self { interface }
66 }
67 }
68
69 /// IP datagram encapsulated protocol.
70 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
71 #[repr(u8)]
72 pub enum Protocol {
73 HopByHop = 0x00,
74 Icmp = 0x01,
75 Igmp = 0x02,
76 Tcp = 0x06,
77 Udp = 0x11,
78 Ipv6Route = 0x2b,
79 Ipv6Frag = 0x2c,
80 Icmpv6 = 0x3a,
81 Ipv6NoNxt = 0x3b,
82 Ipv6Opts = 0x3c,
83 Unknown(u8),
84 }
85
86 impl fmt::Display for Protocol {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88 match *self {
89 Protocol::HopByHop => write!(f, "Hop-by-Hop"),
90 Protocol::Icmp => write!(f, "ICMP"),
91 Protocol::Igmp => write!(f, "IGMP"),
92 Protocol::Tcp => write!(f, "TCP"),
93 Protocol::Udp => write!(f, "UDP"),
94 Protocol::Ipv6Route => write!(f, "IPv6-Route"),
95 Protocol::Ipv6Frag => write!(f, "IPv6-Frag"),
96 Protocol::Icmpv6 => write!(f, "ICMPv6"),
97 Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"),
98 Protocol::Ipv6Opts => write!(f, "IPv6-Opts"),
99 Protocol::Unknown(id) => write!(f, "0x{id:02x}"),
100 }
101 }
102 }
103
104 impl From<smoltcp::wire::IpProtocol> for Protocol {
from(value: smoltcp::wire::IpProtocol) -> Self105 fn from(value: smoltcp::wire::IpProtocol) -> Self {
106 let x: u8 = value.into();
107 Protocol::from(x)
108 }
109 }
110
111 impl From<u8> for Protocol {
from(value: u8) -> Self112 fn from(value: u8) -> Self {
113 match value {
114 0x00 => Protocol::HopByHop,
115 0x01 => Protocol::Icmp,
116 0x02 => Protocol::Igmp,
117 0x06 => Protocol::Tcp,
118 0x11 => Protocol::Udp,
119 0x2b => Protocol::Ipv6Route,
120 0x2c => Protocol::Ipv6Frag,
121 0x3a => Protocol::Icmpv6,
122 0x3b => Protocol::Ipv6NoNxt,
123 0x3c => Protocol::Ipv6Opts,
124 _ => Protocol::Unknown(value),
125 }
126 }
127 }
128
129 impl From<Protocol> for u8 {
from(value: Protocol) -> Self130 fn from(value: Protocol) -> Self {
131 match value {
132 Protocol::HopByHop => 0x00,
133 Protocol::Icmp => 0x01,
134 Protocol::Igmp => 0x02,
135 Protocol::Tcp => 0x06,
136 Protocol::Udp => 0x11,
137 Protocol::Ipv6Route => 0x2b,
138 Protocol::Ipv6Frag => 0x2c,
139 Protocol::Icmpv6 => 0x3a,
140 Protocol::Ipv6NoNxt => 0x3b,
141 Protocol::Ipv6Opts => 0x3c,
142 Protocol::Unknown(id) => id,
143 }
144 }
145 }
146