I have tried to do some experiments with rust-packet crate in order to parse inbound data from a tun interface (using tokio_tun crate), as seen in the code below:
#[tokio::main]
async fn main() -> Result<()> {
let tun = TunBuilder::new()
.name("") // if name is empty, then it is set by kernel.
.tap(false) // false (default): TUN, true: TAP.
.packet_info(false) // false: IFF_NO_PI, default is true.
.up() // or set it up manually using `sudo ip link set <tun-name> up`.
.try_build()?; // or `.try_build_mq(queues)` for multi-queue support.
println!("tun created, name: {}, fd: {}", tun.name(), tun.as_raw_fd());
let (mut reader, mut _writer) = tokio::io::split(tun);
let mut buf = [0u8; 1024];
loop {
let n = reader.read(&mut buf).await?;
// println!("reading {} bytes: {:?}", n, &buf[..n]);
let ether = ether::Packet::new(&mut buf[..]).unwrap();
println!("ETHER SRC: {} DST: {} PROTOCOL: {:?}", ether.source(), ether.destination(), ether.protocol());
if(ether.protocol() == Protocol::Ipv4) {
let ip = ip::v4::Packet::new(ether.payload()).unwrap();
println!("IP SRC: {} DST: {}", ip.source(), ip.destination());
}
}
}
The above code seems not working as expected, since while performing a simple PING command on that tun interface, I got the following output:
I have tried to do some experiments with rust-packet crate in order to parse inbound data from a tun interface (using tokio_tun crate), as seen in the code below:
The above code seems not working as expected, since while performing a simple PING command on that tun interface, I got the following output:
Possibly I did something wrong and that is not an issue at all, but I would appreciate it if anybody can help me on this.
Thank you in advance!