Get a packet's destination IP address whilst using Tokio on Linux and macOS
- UDP destination IP address from incoming packets (Linux + macOS)
- TCP destination IP address for accepted connections (Linux + macOS)
- Native
tokio::netcompatibility - TLS-friendly: works with
tokio_rustlsand other wrappers
[dependencies]
tokio-dstip = "0.1"use tokio_dstip::TcpListenerWithDst;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let listener = TcpListenerWithDst::bind("127.0.0.1:8080".parse().unwrap()).await?;
let (stream, peer, dst_ip) = listener.accept_with_dst().await?;
println!("Received from {peer}, destined to {dst_ip}");
Ok(())
}use tokio_dstip::UdpSocketWithDst;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let sock = UdpSocketWithDst::bind("0.0.0.0:8080".parse().unwrap())?;
let (data, source, dst_ip) = sock.recv_from().await?;
println!("UDP from {source}, destined to {dst_ip}: {:?}", data);
Ok(())
}cargo run --example udp
cargo run --example tcpMIT