dns.rs 3.0 KB

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