dhcp_client.rs 3.9 KB

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