Skip to content

SentryPeer/tokio-dstip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages