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