#macos #api-bindings #udp #tcp #compatibility #wrapper #tokio-dstip

tokio-dstip

Get a packet's destination IP address whilst using Tokio on Linux and macOS

2 releases

Uses new Rust 2024

new 0.1.1 Apr 30, 2025
0.1.0 Apr 27, 2025

#38 in #compatibility

Download history 131/week @ 2025-04-23

131 downloads per month

MIT license

15KB
255 lines

tokio-dstip

Get a packet's destination IP address whilst using Tokio on Linux and macOS

Features

  • UDP destination IP address from incoming packets (Linux + macOS)
  • TCP destination IP address for accepted connections (Linux + macOS)
  • Native tokio::net compatibility
  • TLS-friendly: works with tokio_rustls and other wrappers

Crates.io MIT licensed CI

Install

[dependencies]
tokio-dstip = "0.1"

Usage

TCP

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(())
}

UDP

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(())
}

Examples

cargo run --example udp
cargo run --example tcp

License

MIT

Dependencies

~4–15MB
~138K SLoC