dns.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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::{InterfaceBuilder, NeighborCache, Routes, 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::{
  14. DnsQueryType, EthernetAddress, HardwareAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address,
  15. };
  16. use std::os::unix::io::AsRawFd;
  17. fn main() {
  18. utils::setup_logging("warn");
  19. let (mut opts, mut free) = utils::create_options();
  20. utils::add_tuntap_options(&mut opts, &mut free);
  21. utils::add_middleware_options(&mut opts, &mut free);
  22. free.push("ADDRESS");
  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 name = &matches.free[0];
  29. let neighbor_cache = NeighborCache::new();
  30. let servers = &[
  31. Ipv4Address::new(8, 8, 4, 4).into(),
  32. Ipv4Address::new(8, 8, 8, 8).into(),
  33. ];
  34. let dns_socket = dns::Socket::new(servers, vec![]);
  35. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  36. let src_ipv6 = IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1);
  37. let mut ip_addrs = heapless::Vec::<IpCidr, 5>::new();
  38. ip_addrs
  39. .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
  40. .unwrap();
  41. ip_addrs.push(IpCidr::new(src_ipv6, 64)).unwrap();
  42. ip_addrs
  43. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  44. .unwrap();
  45. let default_v4_gw = Ipv4Address::new(192, 168, 69, 100);
  46. let default_v6_gw = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);
  47. let mut routes = Routes::new();
  48. routes.add_default_ipv4_route(default_v4_gw).unwrap();
  49. routes.add_default_ipv6_route(default_v6_gw).unwrap();
  50. let medium = device.capabilities().medium;
  51. let mut builder = InterfaceBuilder::new().ip_addrs(ip_addrs).routes(routes);
  52. if medium == Medium::Ethernet {
  53. builder = builder
  54. .hardware_addr(HardwareAddress::Ethernet(ethernet_addr))
  55. .neighbor_cache(neighbor_cache);
  56. }
  57. let mut iface = builder.finalize(&mut device);
  58. let mut sockets = SocketSet::new(vec![]);
  59. let dns_handle = sockets.add(dns_socket);
  60. let socket = sockets.get_mut::<dns::Socket>(dns_handle);
  61. let query = socket
  62. .start_query(iface.context(), name, DnsQueryType::A)
  63. .unwrap();
  64. loop {
  65. let timestamp = Instant::now();
  66. debug!("timestamp {:?}", timestamp);
  67. match iface.poll(timestamp, &mut device, &mut sockets) {
  68. Ok(_) => {}
  69. Err(e) => {
  70. debug!("poll error: {}", e);
  71. }
  72. }
  73. match sockets
  74. .get_mut::<dns::Socket>(dns_handle)
  75. .get_query_result(query)
  76. {
  77. Ok(addrs) => {
  78. println!("Query done: {addrs:?}");
  79. break;
  80. }
  81. Err(GetQueryResultError::Pending) => {} // not done yet
  82. Err(e) => panic!("query failed: {e:?}"),
  83. }
  84. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  85. }
  86. }