dhcp_client.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. mod utils;
  2. use std::collections::BTreeMap;
  3. use std::os::unix::io::AsRawFd;
  4. use smoltcp::phy::wait as phy_wait;
  5. use smoltcp::wire::{EthernetAddress, Ipv4Address, IpCidr, Ipv4Cidr};
  6. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, Routes};
  7. use smoltcp::socket::{SocketSet, RawSocketBuffer, RawPacketMetadata};
  8. use smoltcp::time::Instant;
  9. use smoltcp::dhcp::Dhcpv4Client;
  10. fn main() {
  11. #[cfg(feature = "log")]
  12. utils::setup_logging("");
  13. let (mut opts, mut free) = utils::create_options();
  14. utils::add_tap_options(&mut opts, &mut free);
  15. utils::add_middleware_options(&mut opts, &mut free);
  16. let mut matches = utils::parse_options(&opts, free);
  17. let device = utils::parse_tap_options(&mut matches);
  18. let fd = device.as_raw_fd();
  19. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  20. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  21. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  22. let ip_addrs = [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0)];
  23. let mut routes_storage = [None; 1];
  24. let routes = Routes::new(&mut routes_storage[..]);
  25. let mut iface = EthernetInterfaceBuilder::new(device)
  26. .ethernet_addr(ethernet_addr)
  27. .neighbor_cache(neighbor_cache)
  28. .ip_addrs(ip_addrs)
  29. .routes(routes)
  30. .finalize();
  31. let mut sockets = SocketSet::new(vec![]);
  32. let dhcp_rx_buffer = RawSocketBuffer::new(
  33. [RawPacketMetadata::EMPTY; 1],
  34. vec![0; 900]
  35. );
  36. let dhcp_tx_buffer = RawSocketBuffer::new(
  37. [RawPacketMetadata::EMPTY; 1],
  38. vec![0; 600]
  39. );
  40. let mut dhcp = Dhcpv4Client::new(&mut sockets, dhcp_rx_buffer, dhcp_tx_buffer, Instant::now());
  41. let mut prev_cidr = Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0);
  42. loop {
  43. let timestamp = Instant::now();
  44. iface.poll(&mut sockets, timestamp)
  45. .map(|_| ())
  46. .unwrap_or_else(|e| println!("Poll: {:?}", e));
  47. let config = dhcp.poll(&mut iface, &mut sockets, timestamp)
  48. .unwrap_or_else(|e| {
  49. println!("DHCP: {:?}", e);
  50. None
  51. });
  52. config.map(|config| {
  53. println!("DHCP config: {:?}", config);
  54. match config.address {
  55. Some(cidr) => if cidr != prev_cidr {
  56. iface.update_ip_addrs(|addrs| {
  57. addrs.iter_mut().nth(0)
  58. .map(|addr| {
  59. *addr = IpCidr::Ipv4(cidr);
  60. });
  61. });
  62. prev_cidr = cidr;
  63. println!("Assigned a new IPv4 address: {}", cidr);
  64. }
  65. _ => {}
  66. }
  67. config.router.map(|router| iface.routes_mut()
  68. .add_default_ipv4_route(router.into())
  69. .unwrap()
  70. );
  71. iface.routes_mut()
  72. .update(|routes_map| {
  73. routes_map.get(&IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0))
  74. .map(|default_route| {
  75. println!("Default gateway: {}", default_route.via_router);
  76. });
  77. });
  78. if config.dns_servers.iter().any(|s| s.is_some()) {
  79. println!("DNS servers:");
  80. for dns_server in config.dns_servers.iter().filter_map(|s| *s) {
  81. println!("- {}", dns_server);
  82. }
  83. }
  84. });
  85. let mut timeout = dhcp.next_poll(timestamp);
  86. iface.poll_delay(&sockets, timestamp)
  87. .map(|sockets_timeout| timeout = sockets_timeout);
  88. phy_wait(fd, Some(timeout))
  89. .unwrap_or_else(|e| println!("Wait: {:?}", e));
  90. }
  91. }