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