1 #[macro_use]
2 extern crate log;
3 extern crate byteorder;
4 extern crate env_logger;
5 extern crate getopts;
6 extern crate smoltcp;
7 
8 mod utils;
9 
10 use smoltcp::iface::{Config, Interface, SocketSet};
11 use smoltcp::phy::Device;
12 use smoltcp::phy::{wait as phy_wait, Medium};
13 use smoltcp::socket::dns::{self, GetQueryResultError};
14 use smoltcp::time::Instant;
15 use smoltcp::wire::{DnsQueryType, EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
16 use std::os::unix::io::AsRawFd;
17 
main()18 fn main() {
19     utils::setup_logging("warn");
20 
21     let (mut opts, mut free) = utils::create_options();
22     utils::add_tuntap_options(&mut opts, &mut free);
23     utils::add_middleware_options(&mut opts, &mut free);
24     free.push("ADDRESS");
25 
26     let mut matches = utils::parse_options(&opts, free);
27     let device = utils::parse_tuntap_options(&mut matches);
28     let fd = device.as_raw_fd();
29     let mut device =
30         utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
31     let name = &matches.free[0];
32 
33     // Create interface
34     let mut config = Config::new();
35     config.random_seed = rand::random();
36     if device.capabilities().medium == Medium::Ethernet {
37         config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
38     }
39 
40     let mut iface = Interface::new(config, &mut device);
41     iface.update_ip_addrs(|ip_addrs| {
42         ip_addrs
43             .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
44             .unwrap();
45         ip_addrs
46             .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
47             .unwrap();
48         ip_addrs
49             .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
50             .unwrap();
51     });
52     iface
53         .routes_mut()
54         .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
55         .unwrap();
56     iface
57         .routes_mut()
58         .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
59         .unwrap();
60 
61     // Create sockets
62     let servers = &[
63         Ipv4Address::new(8, 8, 4, 4).into(),
64         Ipv4Address::new(8, 8, 8, 8).into(),
65     ];
66     let dns_socket = dns::Socket::new(servers, vec![]);
67 
68     let mut sockets = SocketSet::new(vec![]);
69     let dns_handle = sockets.add(dns_socket);
70 
71     let socket = sockets.get_mut::<dns::Socket>(dns_handle);
72     let query = socket
73         .start_query(iface.context(), name, DnsQueryType::A)
74         .unwrap();
75 
76     loop {
77         let timestamp = Instant::now();
78         debug!("timestamp {:?}", timestamp);
79 
80         iface.poll(timestamp, &mut device, &mut sockets);
81 
82         match sockets
83             .get_mut::<dns::Socket>(dns_handle)
84             .get_query_result(query)
85         {
86             Ok(addrs) => {
87                 println!("Query done: {addrs:?}");
88                 break;
89             }
90             Err(GetQueryResultError::Pending) => {} // not done yet
91             Err(e) => panic!("query failed: {e:?}"),
92         }
93 
94         phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
95     }
96 }
97