Skip to content

Commit

Permalink
examples/slaac: add SLAAC example
Browse files Browse the repository at this point in the history
  • Loading branch information
bergzand committed Feb 5, 2025
1 parent 01e7775 commit a651c9a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,9 @@ required-features = ["std", "medium-ieee802154", "phy-raw_socket", "proto-sixlow
name = "dns"
required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interface", "proto-ipv4", "socket-dns"]

[[example]]
name = "slaac"
required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interface", "proto-ipv6", "socket-udp"]

[profile.release]
debug = 2
76 changes: 76 additions & 0 deletions examples/slaac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
mod utils;

use std::os::unix::io::AsRawFd;

use smoltcp::iface::{Config, Interface, SocketSet};
use smoltcp::phy::{wait as phy_wait, Device, Medium};
use smoltcp::socket::udp;
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};

const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0x0, 0, 0, 0x01);

fn main() {
utils::setup_logging("warn");

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 = match device.capabilities().medium {
Medium::Ethernet => {
Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
}
Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
Medium::Ieee802154 => todo!(),
};
config.slaac = true;

let mut iface = Interface::new(config, &mut device, Instant::now());
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
.unwrap();
});

let mut sockets = SocketSet::new(vec![]);
let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
let _udp_handle = sockets.add(udp_socket);

let mut last_print = Instant::now();
loop {
let timestamp = Instant::now();
iface.poll(timestamp, &mut device, &mut sockets);
let mut delay = iface.poll_delay(timestamp, &sockets);
if delay.is_none() || delay.is_some_and(|d| d > Duration::from_millis(1000)) {
delay = Some(Duration::from_millis(1000));
}

phy_wait(fd, delay).expect("wait error");

let timestamp = Instant::now();
if timestamp > last_print + Duration::from_secs(1) {
last_print = timestamp;
println!();
println!("Addresses:");
for addr in iface.ip_addrs() {
println!(" - {addr}");
}
println!("Routes:");
iface.routes_mut().update(|routes| {
for route in routes {
println!(" - {} via {}", route.cidr, route.via_router);
}
});
}
}
}

0 comments on commit a651c9a

Please sign in to comment.