dns.rs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 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.start_query(&mut iface, name).unwrap();
  61. loop {
  62. let timestamp = Instant::now();
  63. debug!("timestamp {:?}", timestamp);
  64. match iface.poll(timestamp, &mut device, &mut sockets) {
  65. Ok(_) => {}
  66. Err(e) => {
  67. debug!("poll error: {}", e);
  68. }
  69. }
  70. match sockets
  71. .get_mut::<dns::Socket>(dns_handle)
  72. .get_query_result(query)
  73. {
  74. Ok(addrs) => {
  75. println!("Query done: {:?}", addrs);
  76. break;
  77. }
  78. Err(GetQueryResultError::Pending) => {} // not done yet
  79. Err(e) => panic!("query failed: {:?}", e),
  80. }
  81. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  82. }
  83. }