1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use alloc::collections::VecDeque;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use lose_net_stack::IPv4;
use crate::sync::UPIntrFreeCell;
pub struct Socket {
pub raddr: IPv4,
pub lport: u16,
pub rport: u16,
pub buffers: VecDeque<Vec<u8>>,
pub seq: u32,
pub ack: u32
}
lazy_static! {
static ref SOCKET_TABLE: UPIntrFreeCell<Vec<Option<Socket>>> =
unsafe { UPIntrFreeCell::new(Vec::new()) };
}
pub fn get_s_a_by_index(index: usize) -> Option<(u32, u32)> {
let socket_table = SOCKET_TABLE.exclusive_access();
assert!(index < socket_table.len());
socket_table.get(index).map_or(None, |x| {
match x {
Some(x) => Some((x.seq, x.ack)),
None => None,
}
})
}
pub fn set_s_a_by_index(index: usize, seq: u32, ack: u32) {
let mut socket_table = SOCKET_TABLE.exclusive_access();
assert!(socket_table.len() > index);
assert!(socket_table[index].is_some());
let sock = socket_table[index]
.as_mut()
.unwrap();
sock.ack = ack;
sock.seq = seq;
}
pub fn get_socket(raddr: IPv4, lport: u16, rport: u16) -> Option<usize> {
let socket_table = SOCKET_TABLE.exclusive_access();
for i in 0..socket_table.len() {
let sock = &socket_table[i];
if sock.is_none() {
continue;
}
let sock = sock.as_ref().unwrap();
if sock.raddr == raddr && sock.lport == lport && sock.rport == rport {
return Some(i);
}
}
None
}
pub fn add_socket(raddr: IPv4, lport: u16, rport: u16) -> Option<usize> {
if get_socket(raddr, lport, rport).is_some() {
return None;
}
let mut socket_table = SOCKET_TABLE.exclusive_access();
let mut index = usize::MAX;
for i in 0..socket_table.len() {
if socket_table[i].is_none() {
index = i;
break;
}
}
let socket = Socket {
raddr,
lport,
rport,
buffers: VecDeque::new(),
seq: 0,
ack: 0
};
if index == usize::MAX {
socket_table.push(Some(socket));
Some(socket_table.len() - 1)
} else {
socket_table[index] = Some(socket);
Some(index)
}
}
pub fn remove_socket(index: usize) {
let mut socket_table = SOCKET_TABLE.exclusive_access();
assert!(socket_table.len() > index);
socket_table[index] = None;
}
pub fn push_data(index: usize, data: Vec<u8>) {
let mut socket_table = SOCKET_TABLE.exclusive_access();
assert!(socket_table.len() > index);
assert!(socket_table[index].is_some());
socket_table[index]
.as_mut()
.unwrap()
.buffers
.push_back(data);
}
pub fn pop_data(index: usize) -> Option<Vec<u8>> {
let mut socket_table = SOCKET_TABLE.exclusive_access();
assert!(socket_table.len() > index);
assert!(socket_table[index].is_some());
socket_table[index].as_mut().unwrap().buffers.pop_front()
}