1 mod utils;
2 
3 use log::debug;
4 use std::os::unix::io::AsRawFd;
5 use std::str::{self, FromStr};
6 
7 use smoltcp::iface::{Config, Interface, SocketSet};
8 use smoltcp::phy::{wait as phy_wait, Device, Medium};
9 use smoltcp::socket::tcp;
10 use smoltcp::time::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     free.push("ADDRESS");
20     free.push("PORT");
21 
22     let mut matches = utils::parse_options(&opts, free);
23     let device = utils::parse_tuntap_options(&mut matches);
24 
25     let fd = device.as_raw_fd();
26     let mut device =
27         utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
28     let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
29     let port = u16::from_str(&matches.free[1]).expect("invalid port format");
30 
31     // Create interface
32     let mut config = Config::new();
33     config.random_seed = rand::random();
34     if device.capabilities().medium == Medium::Ethernet {
35         config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
36     }
37 
38     let mut iface = Interface::new(config, &mut device);
39     iface.update_ip_addrs(|ip_addrs| {
40         ip_addrs
41             .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
42             .unwrap();
43         ip_addrs
44             .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
45             .unwrap();
46         ip_addrs
47             .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
48             .unwrap();
49     });
50     iface
51         .routes_mut()
52         .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
53         .unwrap();
54     iface
55         .routes_mut()
56         .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
57         .unwrap();
58 
59     // Create sockets
60     let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
61     let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
62     let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
63     let mut sockets = SocketSet::new(vec![]);
64     let tcp_handle = sockets.add(tcp_socket);
65 
66     let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
67     socket
68         .connect(iface.context(), (address, port), 49500)
69         .unwrap();
70 
71     let mut tcp_active = false;
72     loop {
73         let timestamp = Instant::now();
74         iface.poll(timestamp, &mut device, &mut sockets);
75 
76         let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
77         if socket.is_active() && !tcp_active {
78             debug!("connected");
79         } else if !socket.is_active() && tcp_active {
80             debug!("disconnected");
81             break;
82         }
83         tcp_active = socket.is_active();
84 
85         if socket.may_recv() {
86             let data = socket
87                 .recv(|data| {
88                     let mut data = data.to_owned();
89                     if !data.is_empty() {
90                         debug!(
91                             "recv data: {:?}",
92                             str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
93                         );
94                         data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
95                         data.reverse();
96                         data.extend(b"\n");
97                     }
98                     (data.len(), data)
99                 })
100                 .unwrap();
101             if socket.can_send() && !data.is_empty() {
102                 debug!(
103                     "send data: {:?}",
104                     str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
105                 );
106                 socket.send_slice(&data[..]).unwrap();
107             }
108         } else if socket.may_send() {
109             debug!("close");
110             socket.close();
111         }
112 
113         phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
114     }
115 }
116