1 mod utils;
2
3 use log::debug;
4 use std::fmt::Write;
5 use std::os::unix::io::AsRawFd;
6
7 use smoltcp::iface::{Config, Interface, SocketSet};
8 use smoltcp::phy::{wait as phy_wait, Device, Medium};
9 use smoltcp::socket::{tcp, udp};
10 use smoltcp::time::{Duration, Instant};
11 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
12
main()13 fn main() {
14 utils::setup_logging("");
15
16 let (mut opts, mut free) = utils::create_options();
17 utils::add_tuntap_options(&mut opts, &mut free);
18 utils::add_middleware_options(&mut opts, &mut free);
19
20 let mut matches = utils::parse_options(&opts, free);
21 let device = utils::parse_tuntap_options(&mut matches);
22 let fd = device.as_raw_fd();
23 let mut device =
24 utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
25
26 // Create interface
27 let mut config = Config::new();
28 config.random_seed = rand::random();
29 if device.capabilities().medium == Medium::Ethernet {
30 config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
31 }
32
33 let mut iface = Interface::new(config, &mut device);
34 iface.update_ip_addrs(|ip_addrs| {
35 ip_addrs
36 .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
37 .unwrap();
38 ip_addrs
39 .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
40 .unwrap();
41 ip_addrs
42 .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
43 .unwrap();
44 });
45 iface
46 .routes_mut()
47 .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
48 .unwrap();
49 iface
50 .routes_mut()
51 .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
52 .unwrap();
53
54 // Create sockets
55 let udp_rx_buffer = udp::PacketBuffer::new(
56 vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
57 vec![0; 65535],
58 );
59 let udp_tx_buffer = udp::PacketBuffer::new(
60 vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
61 vec![0; 65535],
62 );
63 let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
64
65 let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
66 let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
67 let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
68
69 let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
70 let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
71 let tcp2_socket = tcp::Socket::new(tcp2_rx_buffer, tcp2_tx_buffer);
72
73 let tcp3_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
74 let tcp3_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
75 let tcp3_socket = tcp::Socket::new(tcp3_rx_buffer, tcp3_tx_buffer);
76
77 let tcp4_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
78 let tcp4_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
79 let tcp4_socket = tcp::Socket::new(tcp4_rx_buffer, tcp4_tx_buffer);
80
81 let mut sockets = SocketSet::new(vec![]);
82 let udp_handle = sockets.add(udp_socket);
83 let tcp1_handle = sockets.add(tcp1_socket);
84 let tcp2_handle = sockets.add(tcp2_socket);
85 let tcp3_handle = sockets.add(tcp3_socket);
86 let tcp4_handle = sockets.add(tcp4_socket);
87
88 let mut tcp_6970_active = false;
89 loop {
90 let timestamp = Instant::now();
91 iface.poll(timestamp, &mut device, &mut sockets);
92
93 // udp:6969: respond "hello"
94 let socket = sockets.get_mut::<udp::Socket>(udp_handle);
95 if !socket.is_open() {
96 socket.bind(6969).unwrap()
97 }
98
99 let client = match socket.recv() {
100 Ok((data, endpoint)) => {
101 debug!("udp:6969 recv data: {:?} from {}", data, endpoint);
102 let mut data = data.to_vec();
103 data.reverse();
104 Some((endpoint, data))
105 }
106 Err(_) => None,
107 };
108 if let Some((endpoint, data)) = client {
109 debug!("udp:6969 send data: {:?} to {}", data, endpoint,);
110 socket.send_slice(&data, endpoint).unwrap();
111 }
112
113 // tcp:6969: respond "hello"
114 let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
115 if !socket.is_open() {
116 socket.listen(6969).unwrap();
117 }
118
119 if socket.can_send() {
120 debug!("tcp:6969 send greeting");
121 writeln!(socket, "hello").unwrap();
122 debug!("tcp:6969 close");
123 socket.close();
124 }
125
126 // tcp:6970: echo with reverse
127 let socket = sockets.get_mut::<tcp::Socket>(tcp2_handle);
128 if !socket.is_open() {
129 socket.listen(6970).unwrap()
130 }
131
132 if socket.is_active() && !tcp_6970_active {
133 debug!("tcp:6970 connected");
134 } else if !socket.is_active() && tcp_6970_active {
135 debug!("tcp:6970 disconnected");
136 }
137 tcp_6970_active = socket.is_active();
138
139 if socket.may_recv() {
140 let data = socket
141 .recv(|buffer| {
142 let recvd_len = buffer.len();
143 let mut data = buffer.to_owned();
144 if !data.is_empty() {
145 debug!("tcp:6970 recv data: {:?}", data);
146 data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
147 data.reverse();
148 data.extend(b"\n");
149 }
150 (recvd_len, data)
151 })
152 .unwrap();
153 if socket.can_send() && !data.is_empty() {
154 debug!("tcp:6970 send data: {:?}", data);
155 socket.send_slice(&data[..]).unwrap();
156 }
157 } else if socket.may_send() {
158 debug!("tcp:6970 close");
159 socket.close();
160 }
161
162 // tcp:6971: sinkhole
163 let socket = sockets.get_mut::<tcp::Socket>(tcp3_handle);
164 if !socket.is_open() {
165 socket.listen(6971).unwrap();
166 socket.set_keep_alive(Some(Duration::from_millis(1000)));
167 socket.set_timeout(Some(Duration::from_millis(2000)));
168 }
169
170 if socket.may_recv() {
171 socket
172 .recv(|buffer| {
173 if !buffer.is_empty() {
174 debug!("tcp:6971 recv {:?} octets", buffer.len());
175 }
176 (buffer.len(), ())
177 })
178 .unwrap();
179 } else if socket.may_send() {
180 socket.close();
181 }
182
183 // tcp:6972: fountain
184 let socket = sockets.get_mut::<tcp::Socket>(tcp4_handle);
185 if !socket.is_open() {
186 socket.listen(6972).unwrap()
187 }
188
189 if socket.may_send() {
190 socket
191 .send(|data| {
192 if !data.is_empty() {
193 debug!("tcp:6972 send {:?} octets", data.len());
194 for (i, b) in data.iter_mut().enumerate() {
195 *b = (i % 256) as u8;
196 }
197 }
198 (data.len(), ())
199 })
200 .unwrap();
201 }
202
203 phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
204 }
205 }
206