renet/examples/dhcp_client.rs

103 lines
3.7 KiB
Rust
Raw Normal View History

#![allow(clippy::option_map_unit_fn)]
mod utils;
use std::collections::BTreeMap;
use std::os::unix::io::AsRawFd;
2021-04-07 07:31:53 +08:00
use log::*;
2021-04-14 02:23:28 +08:00
use smoltcp::{phy::{Device, Medium, wait as phy_wait}, time::Duration};
use smoltcp::wire::{EthernetAddress, Ipv4Address, IpCidr, Ipv4Cidr};
2021-04-07 07:31:53 +08:00
use smoltcp::iface::{NeighborCache, InterfaceBuilder, Interface, Routes};
use smoltcp::socket::{SocketSet, Dhcpv4Socket, Dhcpv4Event};
use smoltcp::time::Instant;
fn main() {
#[cfg(feature = "log")]
utils::setup_logging("");
let (mut opts, mut free) = utils::create_options();
2021-03-25 01:04:42 +08:00
utils::add_tuntap_options(&mut opts, &mut free);
utils::add_middleware_options(&mut opts, &mut free);
let mut matches = utils::parse_options(&opts, free);
2021-03-25 01:04:42 +08:00
let device = utils::parse_tuntap_options(&mut matches);
let fd = device.as_raw_fd();
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
let neighbor_cache = NeighborCache::new(BTreeMap::new());
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
let ip_addrs = [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0)];
let mut routes_storage = [None; 1];
let routes = Routes::new(&mut routes_storage[..]);
2021-03-25 01:04:42 +08:00
let medium = device.capabilities().medium;
let mut builder = InterfaceBuilder::new(device)
.ip_addrs(ip_addrs)
2021-03-25 01:04:42 +08:00
.routes(routes);
if medium == Medium::Ethernet {
builder = builder
.ethernet_addr(ethernet_addr)
.neighbor_cache(neighbor_cache);
}
let mut iface = builder.finalize();
let mut sockets = SocketSet::new(vec![]);
2021-04-14 02:23:28 +08:00
let mut dhcp_socket = Dhcpv4Socket::new();
// Set a ridiculously short max lease time to show DHCP renews work properly.
// This will cause the DHCP client to start renewing after 5 seconds, and give up the
// lease after 10 seconds if renew hasn't succeeded.
// IMPORTANT: This should be removed in production.
dhcp_socket.set_max_lease_duration(Some(Duration::from_secs(10)));
let dhcp_handle = sockets.add(dhcp_socket);
2021-04-07 07:31:53 +08:00
loop {
let timestamp = Instant::now();
2021-04-07 07:31:53 +08:00
if let Err(e) = iface.poll(&mut sockets, timestamp) {
debug!("poll error: {}", e);
}
match sockets.get::<Dhcpv4Socket>(dhcp_handle).poll() {
Dhcpv4Event::NoChange => {}
Dhcpv4Event::Configured(config) => {
debug!("DHCP config acquired!");
debug!("IP address: {}", config.address);
set_ipv4_addr(&mut iface, config.address);
if let Some(router) = config.router {
debug!("Default gateway: {}", router);
iface.routes_mut().add_default_ipv4_route(router).unwrap();
} else {
debug!("Default gateway: None");
iface.routes_mut().remove_default_ipv4_route();
}
2021-04-07 07:31:53 +08:00
for (i, s) in config.dns_servers.iter().enumerate() {
if let Some(s) = s {
debug!("DNS server {}: {}", i, s);
}
}
}
2021-04-07 07:31:53 +08:00
Dhcpv4Event::Deconfigured => {
debug!("DHCP lost config!");
set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
iface.routes_mut().remove_default_ipv4_route();
}
}
2021-04-07 07:31:53 +08:00
phy_wait(fd, iface.poll_delay(&sockets, timestamp)).expect("wait error");
}
}
2021-04-07 07:31:53 +08:00
fn set_ipv4_addr<DeviceT>(iface: &mut Interface<'_, DeviceT>, cidr: Ipv4Cidr)
where DeviceT: for<'d> Device<'d>
{
iface.update_ip_addrs(|addrs| {
let dest = addrs.iter_mut().next().unwrap();
*dest = IpCidr::Ipv4(cidr);
});
}