Crates.io | socket-pktinfo |
lib.rs | socket-pktinfo |
version | 0.2.1 |
source | src |
created_at | 2024-03-19 15:21:58.45251 |
updated_at | 2024-03-27 17:02:24.527658 |
description | Small library to allow cross-platform handling of IP_PKTINFO and IPV6_PKTINFO with socket2 crate |
homepage | https://github.com/pixsper/socket-pktinfo |
repository | https://github.com/pixsper/socket-pktinfo |
max_upload_size | |
id | 1179370 |
size | 30,739 |
Small library to allow cross-platform handling of IP_PKTINFO and IPV6_PKTINFO with socket2 crate. Primary use case for this crate is to determine if a UDP packet was sent to a unicast, broadcast or multicast IP address. Compatible with Windows, Linux and macOS.
use std::net::{Ipv4Addr, SocketAddrV4};
use socket2::{Domain, SockAddr};
use socket_pktinfo::PktInfoUdpSocket;
fn main() -> std::io::Result<()> {
let mut buf = [0; 1024];
let mut socket = PktInfoUdpSocket::new(Domain::IPV4)?;
socket.bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 8000).into())?;
match socket.recv(&mut buf) {//!
Ok((bytes_received, info)) => {
println!("{} bytes received on interface index {} from src {} with destination ip {}",
bytes_received, info.if_index, info.addr_src, info.addr_dst);
}
Err(e) => {
eprintln!("Error receiving packet - {}", e);
}
}
Ok(())
}