1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! 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.
//!
//! Library implements a cross-platform wrapper [`crate::PktInfoUdpSocket`] around [`socket2::Socket`] which returns data extracted from
//! the IP_PKTINFO and IPV6_PKTINFO control messages. Compatible with Windows, Linux and macOS.
//!
//! # Examples
//!
//! ```
//! 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(())
//! # }
//! ```

#[cfg(windows)]
mod win;
#[cfg(windows)]
pub use win::*;

#[cfg(not(windows))]
mod unix;
#[cfg(not(windows))]
pub use unix::*;

///
#[derive(Debug, Clone)]
pub struct PktInfo {
    /// Interface index
    pub if_index: u64,
    /// Source address
    pub addr_src: std::net::SocketAddr,
    /// Header destination address
    pub addr_dst: std::net::IpAddr,
}