1 mod utils;
2
3 use log::debug;
4 use std::os::unix::io::AsRawFd;
5 use std::str::{self, FromStr};
6 use url::Url;
7
8 use smoltcp::iface::{Config, Interface, SocketSet};
9 use smoltcp::phy::{wait as phy_wait, Device, Medium};
10 use smoltcp::socket::tcp;
11 use smoltcp::time::Instant;
12 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
13
main()14 fn main() {
15 utils::setup_logging("");
16
17 let (mut opts, mut free) = utils::create_options();
18 utils::add_tuntap_options(&mut opts, &mut free);
19 utils::add_middleware_options(&mut opts, &mut free);
20 free.push("ADDRESS");
21 free.push("URL");
22
23 let mut matches = utils::parse_options(&opts, free);
24 let device = utils::parse_tuntap_options(&mut matches);
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 url = Url::parse(&matches.free[1]).expect("invalid url 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; 1024]);
61 let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 1024]);
62 let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
63
64 let mut sockets = SocketSet::new(vec![]);
65 let tcp_handle = sockets.add(tcp_socket);
66
67 enum State {
68 Connect,
69 Request,
70 Response,
71 }
72 let mut state = State::Connect;
73
74 loop {
75 let timestamp = Instant::now();
76 iface.poll(timestamp, &mut device, &mut sockets);
77
78 let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
79 let cx = iface.context();
80
81 state = match state {
82 State::Connect if !socket.is_active() => {
83 debug!("connecting");
84 let local_port = 49152 + rand::random::<u16>() % 16384;
85 socket
86 .connect(cx, (address, url.port().unwrap_or(80)), local_port)
87 .unwrap();
88 State::Request
89 }
90 State::Request if socket.may_send() => {
91 debug!("sending request");
92 let http_get = "GET ".to_owned() + url.path() + " HTTP/1.1\r\n";
93 socket.send_slice(http_get.as_ref()).expect("cannot send");
94 let http_host = "Host: ".to_owned() + url.host_str().unwrap() + "\r\n";
95 socket.send_slice(http_host.as_ref()).expect("cannot send");
96 socket
97 .send_slice(b"Connection: close\r\n")
98 .expect("cannot send");
99 socket.send_slice(b"\r\n").expect("cannot send");
100 State::Response
101 }
102 State::Response if socket.can_recv() => {
103 socket
104 .recv(|data| {
105 println!("{}", str::from_utf8(data).unwrap_or("(invalid utf8)"));
106 (data.len(), ())
107 })
108 .unwrap();
109 State::Response
110 }
111 State::Response if !socket.may_recv() => {
112 debug!("received complete response");
113 break;
114 }
115 _ => state,
116 };
117
118 phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
119 }
120 }
121