12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #![allow(clippy::option_map_unit_fn)]
- mod utils;
- use log::*;
- use std::os::unix::io::AsRawFd;
- use smoltcp::iface::{Config, Interface, SocketSet};
- use smoltcp::socket::dhcpv4;
- use smoltcp::time::Instant;
- use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Address, Ipv4Cidr};
- use smoltcp::{
- phy::{wait as phy_wait, Device, Medium},
- time::Duration,
- };
- fn main() {
- #[cfg(feature = "log")]
- utils::setup_logging("");
- let (mut opts, mut free) = utils::create_options();
- utils::add_tuntap_options(&mut opts, &mut free);
- utils::add_middleware_options(&mut opts, &mut free);
- let mut matches = utils::parse_options(&opts, free);
- let device = utils::parse_tuntap_options(&mut matches);
- let fd = device.as_raw_fd();
- let mut device =
- utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
- // Create interface
- let mut config = Config::new();
- config.random_seed = rand::random();
- if device.capabilities().medium == Medium::Ethernet {
- config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
- }
- let mut iface = Interface::new(config, &mut device);
- // Create sockets
- let mut dhcp_socket = dhcpv4::Socket::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 mut sockets = SocketSet::new(vec![]);
- let dhcp_handle = sockets.add(dhcp_socket);
- loop {
- let timestamp = Instant::now();
- iface.poll(timestamp, &mut device, &mut sockets);
- let event = sockets.get_mut::<dhcpv4::Socket>(dhcp_handle).poll();
- match event {
- None => {}
- Some(dhcpv4::Event::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();
- }
- for (i, s) in config.dns_servers.iter().enumerate() {
- debug!("DNS server {}: {}", i, s);
- }
- }
- Some(dhcpv4::Event::Deconfigured) => {
- debug!("DHCP lost config!");
- set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
- iface.routes_mut().remove_default_ipv4_route();
- }
- }
- phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
- }
- }
- fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
- iface.update_ip_addrs(|addrs| {
- let dest = addrs.iter_mut().next().unwrap();
- *dest = IpCidr::Ipv4(cidr);
- });
- }
|