dns.rs 3.3 KB

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