cross_socket/pcap/
mod.rs

1mod capture;
2pub mod listener;
3
4use crate::packet::ethernet::EtherType;
5use crate::packet::ip::IpNextLevelProtocol;
6use std::collections::HashSet;
7use std::net::IpAddr;
8use std::time::Duration;
9
10/// Packet capture options
11#[derive(Clone, Debug)]
12pub struct PacketCaptureOptions {
13    /// Interface index
14    pub interface_index: u32,
15    /// Interface name
16    pub interface_name: String,
17    /// Source IP addresses to filter. If empty, all source IP addresses will be captured
18    pub src_ips: HashSet<IpAddr>,
19    /// Destination IP addresses to filter. If empty, all destination IP addresses will be captured
20    pub dst_ips: HashSet<IpAddr>,
21    /// Source ports to filter. If empty, all source ports will be captured
22    pub src_ports: HashSet<u16>,
23    /// Destination ports to filter. If empty, all destination ports will be captured
24    pub dst_ports: HashSet<u16>,
25    /// Ether types to filter. If empty, all ether types will be captured
26    pub ether_types: HashSet<EtherType>,
27    /// IP protocols to filter. If empty, all IP protocols will be captured
28    pub ip_protocols: HashSet<IpNextLevelProtocol>,
29    /// Capture duration limit
30    pub duration: Duration,
31    /// Read Timeout for read next packet (Linux, BPF, Netmap only)
32    pub read_timeout: Duration,
33    /// Capture in promiscuous mode
34    pub promiscuous: bool,
35    /// Store captured packets in memory
36    pub store: bool,
37    /// Store limit
38    pub store_limit: u32,
39    /// Receive undefined packets
40    pub receive_undefined: bool,
41    /// Use TUN interface
42    pub use_tun: bool,
43    /// Loopback interface
44    pub loopback: bool,
45}
46
47impl PacketCaptureOptions {
48    /// Constructs a new PacketCaptureOptions
49    pub fn new() -> PacketCaptureOptions {
50        PacketCaptureOptions {
51            interface_index: 0,
52            interface_name: String::new(),
53            src_ips: HashSet::new(),
54            dst_ips: HashSet::new(),
55            src_ports: HashSet::new(),
56            dst_ports: HashSet::new(),
57            ether_types: HashSet::new(),
58            ip_protocols: HashSet::new(),
59            duration: Duration::from_secs(30),
60            read_timeout: Duration::from_secs(2),
61            promiscuous: false,
62            store: false,
63            store_limit: u32::MAX,
64            receive_undefined: false,
65            use_tun: false,
66            loopback: false,
67        }
68    }
69    /// Set interface index
70    pub fn with_interface_index(mut self, interface_index: u32) -> PacketCaptureOptions {
71        self.interface_index = interface_index;
72        self
73    }
74    /// Set source IP addresses to filter
75    pub fn set_src_ips(&mut self, ips: Vec<IpAddr>) {
76        for ip in ips {
77            self.src_ips.insert(ip);
78        }
79    }
80    /// Set destination IP addresses to filter
81    pub fn set_dst_ips(&mut self, ips: Vec<IpAddr>) {
82        for ip in ips {
83            self.dst_ips.insert(ip);
84        }
85    }
86    /// Set source ports to filter
87    pub fn set_src_ports(&mut self, ports: Vec<u16>) {
88        for port in ports {
89            self.src_ports.insert(port);
90        }
91    }
92    /// Set destination ports to filter
93    pub fn set_dst_ports(&mut self, ports: Vec<u16>) {
94        for port in ports {
95            self.dst_ports.insert(port);
96        }
97    }
98    /// Set ether types to filter
99    pub fn set_ether_types(&mut self, ether_types: Vec<EtherType>) {
100        for ether_type in ether_types {
101            self.ether_types.insert(ether_type);
102        }
103    }
104    /// Set IP protocols to filter
105    pub fn set_ip_protocols(&mut self, ip_protocols: Vec<IpNextLevelProtocol>) {
106        for ip_protocol in ip_protocols {
107            self.ip_protocols.insert(ip_protocol);
108        }
109    }
110}