Skip to content

Commit a651c9a

Browse files
committed
examples/slaac: add SLAAC example
1 parent 01e7775 commit a651c9a

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,9 @@ required-features = ["std", "medium-ieee802154", "phy-raw_socket", "proto-sixlow
337337
name = "dns"
338338
required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interface", "proto-ipv4", "socket-dns"]
339339

340+
[[example]]
341+
name = "slaac"
342+
required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interface", "proto-ipv6", "socket-udp"]
343+
340344
[profile.release]
341345
debug = 2

examples/slaac.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
mod utils;
2+
3+
use std::os::unix::io::AsRawFd;
4+
5+
use smoltcp::iface::{Config, Interface, SocketSet};
6+
use smoltcp::phy::{wait as phy_wait, Device, Medium};
7+
use smoltcp::socket::udp;
8+
use smoltcp::time::{Duration, Instant};
9+
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};
10+
11+
const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0x0, 0, 0, 0x01);
12+
13+
fn main() {
14+
utils::setup_logging("warn");
15+
16+
let (mut opts, mut free) = utils::create_options();
17+
utils::add_tuntap_options(&mut opts, &mut free);
18+
utils::add_middleware_options(&mut opts, &mut free);
19+
20+
let mut matches = utils::parse_options(&opts, free);
21+
let device = utils::parse_tuntap_options(&mut matches);
22+
let fd = device.as_raw_fd();
23+
let mut device =
24+
utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
25+
26+
// Create interface
27+
let mut config = match device.capabilities().medium {
28+
Medium::Ethernet => {
29+
Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
30+
}
31+
Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
32+
Medium::Ieee802154 => todo!(),
33+
};
34+
config.slaac = true;
35+
36+
let mut iface = Interface::new(config, &mut device, Instant::now());
37+
iface.update_ip_addrs(|ip_addrs| {
38+
ip_addrs
39+
.push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
40+
.unwrap();
41+
});
42+
43+
let mut sockets = SocketSet::new(vec![]);
44+
let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
45+
let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
46+
let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
47+
let _udp_handle = sockets.add(udp_socket);
48+
49+
let mut last_print = Instant::now();
50+
loop {
51+
let timestamp = Instant::now();
52+
iface.poll(timestamp, &mut device, &mut sockets);
53+
let mut delay = iface.poll_delay(timestamp, &sockets);
54+
if delay.is_none() || delay.is_some_and(|d| d > Duration::from_millis(1000)) {
55+
delay = Some(Duration::from_millis(1000));
56+
}
57+
58+
phy_wait(fd, delay).expect("wait error");
59+
60+
let timestamp = Instant::now();
61+
if timestamp > last_print + Duration::from_secs(1) {
62+
last_print = timestamp;
63+
println!();
64+
println!("Addresses:");
65+
for addr in iface.ip_addrs() {
66+
println!(" - {addr}");
67+
}
68+
println!("Routes:");
69+
iface.routes_mut().update(|routes| {
70+
for route in routes {
71+
println!(" - {} via {}", route.cidr, route.via_router);
72+
}
73+
});
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)