mod common;
use common::{flag_present, print_help_if_requested, ExampleResult, EXAMPLE_IFACE};
use crafter::prelude::*;
use crafter::protocols::rip::{rip_v2_multicast_response, rip_v2_whole_table_request};
use std::net::Ipv4Addr;
const RIP_SOURCE: Ipv4Addr = Ipv4Addr::new(192, 0, 2, 10);
const ROUTE_ONE_NETWORK: Ipv4Addr = Ipv4Addr::new(198, 51, 100, 0);
const ROUTE_TWO_NETWORK: Ipv4Addr = Ipv4Addr::new(198, 51, 100, 128);
const MASK_24: Ipv4Addr = Ipv4Addr::new(255, 255, 255, 0);
const MASK_25: Ipv4Addr = Ipv4Addr::new(255, 255, 255, 128);
fn main() -> ExampleResult<()> {
if print_help_if_requested(
"usage: cargo run --example rip_request -- [--send]\n\nBuild a RIPv2 whole-table request and a RIPv2 multicast response over documentation address space and inspect their dry-run send plans. Offline by default; --send is reserved for an explicit, gated live path and is not enabled in this example.",
) {
return Ok(());
}
if flag_present("--send") {
eprintln!(
"refusing live send: rip_request is an offline stimulus driver; \
run RIP against a provider-backed routing daemon via the probe/lab runners instead"
);
return Err("live RIP send is not enabled in this example".into());
}
println!("example: rip_request");
println!("mode: dry-run");
println!("source: {RIP_SOURCE}");
let request = rip_v2_whole_table_request(RIP_SOURCE);
print_plan("request", "RIPv2 whole-table request", &request)?;
let entries = vec![
RipEntry::ipv2_route(ROUTE_ONE_NETWORK, MASK_24, 1),
RipEntry::ipv2_route(ROUTE_TWO_NETWORK, MASK_25, 2).with_route_tag(0),
];
let response = rip_v2_multicast_response(RIP_SOURCE, entries);
print_plan("response", "RIPv2 multicast response", &response)?;
Ok(())
}
fn print_plan(label: &str, description: &str, packet: &Packet) -> ExampleResult<()> {
let plan = packet.send_dry_run(SendOptions::new().iface(EXAMPLE_IFACE).network_layer())?;
println!();
println!("{label}: {description}");
println!("summary: {}", packet.summary());
println!("interface: {}", plan.interface());
println!("target: {:?}", plan.target());
println!("compiled bytes: {}", plan.len());
println!("show:\n{}", packet.show());
println!("hexdump:\n{}", plan.compiled_packet().hexdump());
Ok(())
}