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