#![allow(dead_code)]
use std::net::UdpSocket;
use std::process::Command;
use std::sync::atomic::{AtomicU16, Ordering};
pub const LOOPBACK: &str = "lo";
static PORT_COUNTER: AtomicU16 = AtomicU16::new(30_000);
pub fn unique_port() -> u16 {
PORT_COUNTER.fetch_add(1, Ordering::Relaxed)
}
pub fn send_udp_to_loopback(port: u16, payload: &[u8], count: usize) {
let sock = UdpSocket::bind("127.0.0.1:0").expect("bind sender");
let dst = format!("127.0.0.1:{port}");
for _ in 0..count {
sock.send_to(payload, &dst).expect("send_to");
}
}
pub struct VethPair {
pub a: String,
pub b: String,
}
impl VethPair {
pub fn create(a: &str, b: &str) -> Option<Self> {
let _ = Command::new("ip").args(["link", "delete", a]).output();
let status = Command::new("ip")
.args(["link", "add", a, "type", "veth", "peer", "name", b])
.status()
.ok()?;
if !status.success() {
return None;
}
let up_a = Command::new("ip")
.args(["link", "set", a, "up"])
.status()
.ok()?;
if !up_a.success() {
let _ = Command::new("ip").args(["link", "delete", a]).status();
return None;
}
let up_b = Command::new("ip")
.args(["link", "set", b, "up"])
.status()
.ok()?;
if !up_b.success() {
let _ = Command::new("ip").args(["link", "delete", a]).status();
return None;
}
Some(Self {
a: a.to_string(),
b: b.to_string(),
})
}
}
impl Drop for VethPair {
fn drop(&mut self) {
let _ = Command::new("ip")
.args(["link", "delete", &self.a])
.output();
}
}