dns.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. mod utils;
  8. use smoltcp::iface::{Config, Interface, SocketSet};
  9. use smoltcp::phy::Device;
  10. use smoltcp::phy::{wait as phy_wait, Medium};
  11. use smoltcp::socket::dns::{self, GetQueryResultError};
  12. use smoltcp::time::Instant;
  13. use smoltcp::wire::{DnsQueryType, EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
  14. use std::os::unix::io::AsRawFd;
  15. fn main() {
  16. utils::setup_logging("warn");
  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. let mut matches = utils::parse_options(&opts, free);
  22. let device = utils::parse_tuntap_options(&mut matches);
  23. let fd = device.as_raw_fd();
  24. let mut device =
  25. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  26. let name = &matches.free[0];
  27. // Create interface
  28. let mut config = Config::new();
  29. config.random_seed = rand::random();
  30. if device.capabilities().medium == Medium::Ethernet {
  31. config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
  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. // Create sockets
  54. let servers = &[
  55. Ipv4Address::new(8, 8, 4, 4).into(),
  56. Ipv4Address::new(8, 8, 8, 8).into(),
  57. ];
  58. let dns_socket = dns::Socket::new(servers, vec![]);
  59. let mut sockets = SocketSet::new(vec![]);
  60. let dns_handle = sockets.add(dns_socket);
  61. let socket = sockets.get_mut::<dns::Socket>(dns_handle);
  62. let query = socket
  63. .start_query(iface.context(), name, DnsQueryType::A)
  64. .unwrap();
  65. loop {
  66. let timestamp = Instant::now();
  67. debug!("timestamp {:?}", timestamp);
  68. iface.poll(timestamp, &mut device, &mut sockets);
  69. match sockets
  70. .get_mut::<dns::Socket>(dns_handle)
  71. .get_query_result(query)
  72. {
  73. Ok(addrs) => {
  74. println!("Query done: {addrs:?}");
  75. break;
  76. }
  77. Err(GetQueryResultError::Pending) => {} // not done yet
  78. Err(e) => panic!("query failed: {e:?}"),
  79. }
  80. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  81. }
  82. }