netscan_pcap/
lib.rs

1mod capture;
2pub mod listener;
3
4use std::collections::HashSet;
5use std::net::IpAddr;
6use std::time::Duration;
7pub use xenet::net::mac::MacAddr;
8pub use xenet::packet;
9use xenet::packet::arp::ArpHeader;
10use xenet::packet::ethernet::EtherType;
11use xenet::packet::ethernet::EthernetHeader;
12use xenet::packet::icmp::IcmpHeader;
13use xenet::packet::icmpv6::Icmpv6Header;
14use xenet::packet::ip::IpNextLevelProtocol;
15use xenet::packet::ipv4::Ipv4Header;
16use xenet::packet::ipv6::Ipv6Header;
17use xenet::packet::tcp::TcpHeader;
18use xenet::packet::udp::UdpHeader;
19
20/// Packet capture options
21#[derive(Clone, Debug)]
22pub struct PacketCaptureOptions {
23    /// Interface index
24    pub interface_index: u32,
25    /// Interface name
26    #[allow(dead_code)]
27    pub interface_name: String,
28    /// Source IP addresses to filter. If empty, all source IP addresses will be captured
29    pub src_ips: HashSet<IpAddr>,
30    /// Destination IP addresses to filter. If empty, all destination IP addresses will be captured
31    pub dst_ips: HashSet<IpAddr>,
32    /// Source ports to filter. If empty, all source ports will be captured
33    pub src_ports: HashSet<u16>,
34    /// Destination ports to filter. If empty, all destination ports will be captured
35    pub dst_ports: HashSet<u16>,
36    /// Ether types to filter. If empty, all ether types will be captured
37    pub ether_types: HashSet<EtherType>,
38    /// IP protocols to filter. If empty, all IP protocols will be captured
39    pub ip_protocols: HashSet<IpNextLevelProtocol>,
40    /// Capture duration limit
41    pub duration: Duration,
42    /// Read Timeout for read next packet (Linux, BPF, Netmap only)
43    pub read_timeout: Duration,
44    /// Capture in promiscuous mode
45    pub promiscuous: bool,
46    /// Store captured packets in memory
47    pub store: bool,
48    /// Store limit
49    pub store_limit: u32,
50    /// Receive undefined packets
51    pub receive_undefined: bool,
52    /// Use TUN interface
53    pub tunnel: bool,
54    /// Loopback interface
55    pub loopback: bool,
56}
57
58/// Packet Frame. Contains all the possible packet types
59#[derive(Clone, Debug)]
60pub struct PacketFrame {
61    pub ethernet_header: Option<EthernetHeader>,
62    pub arp_header: Option<ArpHeader>,
63    pub ipv4_header: Option<Ipv4Header>,
64    pub ipv6_header: Option<Ipv6Header>,
65    pub icmp_header: Option<IcmpHeader>,
66    pub icmpv6_header: Option<Icmpv6Header>,
67    pub tcp_header: Option<TcpHeader>,
68    pub udp_header: Option<UdpHeader>,
69    pub payload: Vec<u8>,
70}
71
72impl PacketFrame {
73    /// Constructs a new PacketFrame
74    pub fn new() -> PacketFrame {
75        PacketFrame {
76            ethernet_header: None,
77            arp_header: None,
78            ipv4_header: None,
79            ipv6_header: None,
80            icmp_header: None,
81            icmpv6_header: None,
82            tcp_header: None,
83            udp_header: None,
84            payload: vec![],
85        }
86    }
87    pub fn from_xenet_frame(frame: xenet::packet::frame::Frame) -> PacketFrame {
88        let mut packet_frame = PacketFrame::new();
89        if let Some(datalink) = frame.datalink {
90            if let Some(ethernet_header) = datalink.ethernet {
91                packet_frame.ethernet_header = Some(ethernet_header);
92            }
93            if let Some(arp_header) = datalink.arp {
94                packet_frame.arp_header = Some(arp_header);
95            }
96        }
97        if let Some(ip) = frame.ip {
98            if let Some(ipv4_header) = ip.ipv4 {
99                packet_frame.ipv4_header = Some(ipv4_header);
100            }
101            if let Some(ipv6_header) = ip.ipv6 {
102                packet_frame.ipv6_header = Some(ipv6_header);
103            }
104            if let Some(icmp_header) = ip.icmp {
105                packet_frame.icmp_header = Some(icmp_header);
106            }
107            if let Some(icmpv6_header) = ip.icmpv6 {
108                packet_frame.icmpv6_header = Some(icmpv6_header);
109            }
110        }
111        if let Some(transport) = frame.transport {
112            if let Some(tcp_header) = transport.tcp {
113                packet_frame.tcp_header = Some(tcp_header);
114            }
115            if let Some(udp_header) = transport.udp {
116                packet_frame.udp_header = Some(udp_header);
117            }
118        }
119        packet_frame.payload = frame.payload;
120        packet_frame
121    }
122}