dhcp_client.rs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #![allow(clippy::option_map_unit_fn)]
  2. mod utils;
  3. use log::*;
  4. use std::os::unix::io::AsRawFd;
  5. use smoltcp::iface::{Config, Interface, SocketSet};
  6. use smoltcp::socket::dhcpv4;
  7. use smoltcp::time::Instant;
  8. use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Cidr};
  9. use smoltcp::{
  10. phy::{wait as phy_wait, Device, Medium},
  11. time::Duration,
  12. };
  13. fn main() {
  14. #[cfg(feature = "log")]
  15. utils::setup_logging("");
  16. let (mut opts, mut free) = utils::create_options();
  17. utils::add_tuntap_options(&mut opts, &mut free);
  18. utils::add_middleware_options(&mut opts, &mut free);
  19. let mut matches = utils::parse_options(&opts, free);
  20. let device = utils::parse_tuntap_options(&mut matches);
  21. let fd = device.as_raw_fd();
  22. let mut device =
  23. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  24. // Create interface
  25. let mut config = match device.capabilities().medium {
  26. Medium::Ethernet => {
  27. Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
  28. }
  29. Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
  30. Medium::Ieee802154 => todo!(),
  31. };
  32. config.random_seed = rand::random();
  33. let mut iface = Interface::new(config, &mut device, Instant::now());
  34. // Create sockets
  35. let mut dhcp_socket = dhcpv4::Socket::new();
  36. // Set a ridiculously short max lease time to show DHCP renews work properly.
  37. // This will cause the DHCP client to start renewing after 5 seconds, and give up the
  38. // lease after 10 seconds if renew hasn't succeeded.
  39. // IMPORTANT: This should be removed in production.
  40. dhcp_socket.set_max_lease_duration(Some(Duration::from_secs(10)));
  41. let mut sockets = SocketSet::new(vec![]);
  42. let dhcp_handle = sockets.add(dhcp_socket);
  43. loop {
  44. let timestamp = Instant::now();
  45. iface.poll(timestamp, &mut device, &mut sockets);
  46. let event = sockets.get_mut::<dhcpv4::Socket>(dhcp_handle).poll();
  47. match event {
  48. None => {}
  49. Some(dhcpv4::Event::Configured(config)) => {
  50. debug!("DHCP config acquired!");
  51. debug!("IP address: {}", config.address);
  52. set_ipv4_addr(&mut iface, config.address);
  53. if let Some(router) = config.router {
  54. debug!("Default gateway: {}", router);
  55. iface.routes_mut().add_default_ipv4_route(router).unwrap();
  56. } else {
  57. debug!("Default gateway: None");
  58. iface.routes_mut().remove_default_ipv4_route();
  59. }
  60. for (i, s) in config.dns_servers.iter().enumerate() {
  61. debug!("DNS server {}: {}", i, s);
  62. }
  63. }
  64. Some(dhcpv4::Event::Deconfigured) => {
  65. debug!("DHCP lost config!");
  66. iface.update_ip_addrs(|addrs| addrs.clear());
  67. iface.routes_mut().remove_default_ipv4_route();
  68. }
  69. }
  70. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  71. }
  72. }
  73. /// Clear any existing IP addresses & add the new one
  74. fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
  75. iface.update_ip_addrs(|addrs| {
  76. addrs.clear();
  77. addrs.push(IpCidr::Ipv4(cidr)).unwrap();
  78. });
  79. }