dhcp_client.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, Ipv4Address, 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. // Initialize with an unspecified address
  35. set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
  36. // Create sockets
  37. let mut dhcp_socket = dhcpv4::Socket::new();
  38. // Set a ridiculously short max lease time to show DHCP renews work properly.
  39. // This will cause the DHCP client to start renewing after 5 seconds, and give up the
  40. // lease after 10 seconds if renew hasn't succeeded.
  41. // IMPORTANT: This should be removed in production.
  42. dhcp_socket.set_max_lease_duration(Some(Duration::from_secs(10)));
  43. let mut sockets = SocketSet::new(vec![]);
  44. let dhcp_handle = sockets.add(dhcp_socket);
  45. loop {
  46. let timestamp = Instant::now();
  47. iface.poll(timestamp, &mut device, &mut sockets);
  48. let event = sockets.get_mut::<dhcpv4::Socket>(dhcp_handle).poll();
  49. match event {
  50. None => {}
  51. Some(dhcpv4::Event::Configured(config)) => {
  52. debug!("DHCP config acquired!");
  53. debug!("IP address: {}", config.address);
  54. set_ipv4_addr(&mut iface, config.address);
  55. if let Some(router) = config.router {
  56. debug!("Default gateway: {}", router);
  57. iface.routes_mut().add_default_ipv4_route(router).unwrap();
  58. } else {
  59. debug!("Default gateway: None");
  60. iface.routes_mut().remove_default_ipv4_route();
  61. }
  62. for (i, s) in config.dns_servers.iter().enumerate() {
  63. debug!("DNS server {}: {}", i, s);
  64. }
  65. }
  66. Some(dhcpv4::Event::Deconfigured) => {
  67. debug!("DHCP lost config!");
  68. set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
  69. iface.routes_mut().remove_default_ipv4_route();
  70. }
  71. }
  72. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  73. }
  74. }
  75. /// Clear any existing IP addresses & add the new one
  76. fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
  77. iface.update_ip_addrs(|addrs| {
  78. addrs.clear();
  79. addrs.push(IpCidr::Ipv4(cidr)).unwrap();
  80. });
  81. }