forked from smoltcp-rs/smoltcp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.rs
209 lines (182 loc) · 7.19 KB
/
server.rs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
mod utils;
use log::debug;
use std::fmt::Write;
use std::os::unix::io::AsRawFd;
use smoltcp::iface::{Config, Interface, SocketSet};
use smoltcp::phy::{wait as phy_wait, Device, Medium};
use smoltcp::socket::{tcp, udp};
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
fn main() {
utils::setup_logging("");
let (mut opts, mut free) = utils::create_options();
utils::add_tuntap_options(&mut opts, &mut free);
utils::add_middleware_options(&mut opts, &mut free);
let mut matches = utils::parse_options(&opts, free);
let device = utils::parse_tuntap_options(&mut matches);
let fd = device.as_raw_fd();
let mut device =
utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
// Create interface
let mut config = match device.capabilities().medium {
Medium::Ethernet => {
Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
}
Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
Medium::Ieee802154 => todo!(),
};
config.random_seed = rand::random();
let mut iface = Interface::new(config, &mut device, Instant::now());
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
.unwrap();
ip_addrs
.push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
.unwrap();
ip_addrs
.push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
.unwrap();
});
iface
.routes_mut()
.add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
.unwrap();
iface
.routes_mut()
.add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
.unwrap();
// Create sockets
let udp_rx_buffer = udp::PacketBuffer::new(
vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
vec![0; 65535],
);
let udp_tx_buffer = udp::PacketBuffer::new(
vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
vec![0; 65535],
);
let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
let tcp2_socket = tcp::Socket::new(tcp2_rx_buffer, tcp2_tx_buffer);
let tcp3_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
let tcp3_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
let tcp3_socket = tcp::Socket::new(tcp3_rx_buffer, tcp3_tx_buffer);
let tcp4_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
let tcp4_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
let tcp4_socket = tcp::Socket::new(tcp4_rx_buffer, tcp4_tx_buffer);
let mut sockets = SocketSet::new(vec![]);
let udp_handle = sockets.add(udp_socket);
let tcp1_handle = sockets.add(tcp1_socket);
let tcp2_handle = sockets.add(tcp2_socket);
let tcp3_handle = sockets.add(tcp3_socket);
let tcp4_handle = sockets.add(tcp4_socket);
let mut tcp_6970_active = false;
loop {
let timestamp = Instant::now();
iface.poll(timestamp, &mut device, &mut sockets);
// udp:6969: respond "hello"
let socket = sockets.get_mut::<udp::Socket>(udp_handle);
if !socket.is_open() {
socket.bind(6969).unwrap()
}
let client = match socket.recv() {
Ok((data, endpoint)) => {
debug!("udp:6969 recv data: {:?} from {}", data, endpoint);
let mut data = data.to_vec();
data.reverse();
Some((endpoint, data))
}
Err(_) => None,
};
if let Some((endpoint, data)) = client {
debug!("udp:6969 send data: {:?} to {}", data, endpoint,);
socket.send_slice(&data, endpoint).unwrap();
}
// tcp:6969: respond "hello"
let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
if !socket.is_open() {
socket.listen(6969).unwrap();
}
if socket.can_send() {
debug!("tcp:6969 send greeting");
writeln!(socket, "hello").unwrap();
debug!("tcp:6969 close");
socket.close();
}
// tcp:6970: echo with reverse
let socket = sockets.get_mut::<tcp::Socket>(tcp2_handle);
if !socket.is_open() {
socket.listen(6970).unwrap()
}
if socket.is_active() && !tcp_6970_active {
debug!("tcp:6970 connected");
} else if !socket.is_active() && tcp_6970_active {
debug!("tcp:6970 disconnected");
}
tcp_6970_active = socket.is_active();
if socket.may_recv() {
let data = socket
.recv(|buffer| {
let recvd_len = buffer.len();
let mut data = buffer.to_owned();
if !data.is_empty() {
debug!("tcp:6970 recv data: {:?}", data);
data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
data.reverse();
data.extend(b"\n");
}
(recvd_len, data)
})
.unwrap();
if socket.can_send() && !data.is_empty() {
debug!("tcp:6970 send data: {:?}", data);
socket.send_slice(&data[..]).unwrap();
}
} else if socket.may_send() {
debug!("tcp:6970 close");
socket.close();
}
// tcp:6971: sinkhole
let socket = sockets.get_mut::<tcp::Socket>(tcp3_handle);
if !socket.is_open() {
socket.listen(6971).unwrap();
socket.set_keep_alive(Some(Duration::from_millis(1000)));
socket.set_timeout(Some(Duration::from_millis(2000)));
}
if socket.may_recv() {
socket
.recv(|buffer| {
if !buffer.is_empty() {
debug!("tcp:6971 recv {:?} octets", buffer.len());
}
(buffer.len(), ())
})
.unwrap();
} else if socket.may_send() {
socket.close();
}
// tcp:6972: fountain
let socket = sockets.get_mut::<tcp::Socket>(tcp4_handle);
if !socket.is_open() {
socket.listen(6972).unwrap()
}
if socket.may_send() {
socket
.send(|data| {
if !data.is_empty() {
debug!("tcp:6972 send {:?} octets", data.len());
for (i, b) in data.iter_mut().enumerate() {
*b = (i % 256) as u8;
}
}
(data.len(), ())
})
.unwrap();
}
phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
}
}