forked from smoltcp-rs/smoltcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rs
204 lines (177 loc) · 7.31 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
mod utils;
use std::str;
use std::collections::BTreeMap;
use std::fmt::Write;
use std::os::unix::io::AsRawFd;
use log::debug;
use smoltcp::phy::{Device, Medium, wait as phy_wait};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
use smoltcp::iface::{NeighborCache, InterfaceBuilder};
use smoltcp::socket::SocketSet;
use smoltcp::socket::{UdpSocket, UdpSocketBuffer, UdpPacketMetadata};
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
use smoltcp::time::{Duration, Instant};
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 device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
let neighbor_cache = NeighborCache::new(BTreeMap::new());
let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 64]);
let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 128]);
let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
let tcp3_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp3_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp3_socket = TcpSocket::new(tcp3_rx_buffer, tcp3_tx_buffer);
let tcp4_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp4_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp4_socket = TcpSocket::new(tcp4_rx_buffer, tcp4_tx_buffer);
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
let ip_addrs = [
IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24),
IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64),
IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)
];
let medium = device.capabilities().medium;
let mut builder = InterfaceBuilder::new(device)
.ip_addrs(ip_addrs);
if medium == Medium::Ethernet {
builder = builder
.ethernet_addr(ethernet_addr)
.neighbor_cache(neighbor_cache);
}
let mut iface = builder.finalize();
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();
match iface.poll(&mut sockets, timestamp) {
Ok(_) => {},
Err(e) => {
debug!("poll error: {}", e);
}
}
// udp:6969: respond "hello"
{
let mut socket = sockets.get::<UdpSocket>(udp_handle);
if !socket.is_open() {
socket.bind(6969).unwrap()
}
let client = match socket.recv() {
Ok((data, endpoint)) => {
debug!("udp:6969 recv data: {:?} from {}",
str::from_utf8(data).unwrap(), endpoint);
Some(endpoint)
}
Err(_) => None
};
if let Some(endpoint) = client {
let data = b"hello\n";
debug!("udp:6969 send data: {:?}",
str::from_utf8(data.as_ref()).unwrap());
socket.send_slice(data, endpoint).unwrap();
}
}
// tcp:6969: respond "hello"
{
let mut socket = sockets.get::<TcpSocket>(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 mut socket = sockets.get::<TcpSocket>(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: {:?}",
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
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: {:?}",
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
socket.send_slice(&data[..]).unwrap();
}
} else if socket.may_send() {
debug!("tcp:6970 close");
socket.close();
}
}
// tcp:6971: sinkhole
{
let mut socket = sockets.get::<TcpSocket>(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 mut socket = sockets.get::<TcpSocket>(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(&sockets, timestamp)).expect("wait error");
}
}