[−][src]Struct pcap::Capture
This is a pcap capture handle which is an abstraction over the pcap_t
provided by pcap.
There are many ways to instantiate and interact with a pcap handle, so phantom types are
used to express these behaviors.
Capture<Inactive>
is created via Capture::from_device()
. This handle is inactive,
so you cannot (yet) obtain packets from it. However, you can configure things like the
buffer size, snaplen, timeout, and promiscuity before you activate it.
Capture<Active>
is created by calling .open()
on a Capture<Inactive>
. This
activates the capture handle, allowing you to get packets with .next()
or apply filters
with .filter()
.
Capture<Offline>
is created via Capture::from_file()
. This allows you to read a
pcap format dump file as if you were opening an interface -- very useful for testing or
analysis.
Capture<Dead>
is created via Capture::dead()
. This allows you to create a pcap
format dump file without needing an active capture.
Example:
let cap = Capture::from_device(Device::lookup().unwrap()) // open the "default" interface .unwrap() // assume the device exists and we are authorized to open it .open() // activate the handle .unwrap(); // assume activation worked while let Ok(packet) = cap.next() { println!("received packet! {:?}", packet); }
Implementations
impl Capture<Offline>
[src]
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Capture<Offline>, Error>
[src]
Opens an offline capture handle from a pcap dump file, given a path.
pub fn from_file_with_precision<P: AsRef<Path>>(
path: P,
precision: Precision
) -> Result<Capture<Offline>, Error>
[src]
path: P,
precision: Precision
) -> Result<Capture<Offline>, Error>
Opens an offline capture handle from a pcap dump file, given a path. Takes an additional precision argument specifying the time stamp precision desired.
pub fn from_raw_fd(fd: RawFd) -> Result<Capture<Offline>, Error>
[src]
Opens an offline capture handle from a pcap dump file, given a file descriptor.
impl Capture<Inactive>
[src]
pub fn from_device<D: Into<Device>>(
device: D
) -> Result<Capture<Inactive>, Error>
[src]
device: D
) -> Result<Capture<Inactive>, Error>
Opens a capture handle for a device. You can pass a Device
or an &str
device
name here. The handle is inactive, but can be activated via .open()
.
pub fn open(self) -> Result<Capture<Active>, Error>
[src]
Activates an inactive capture created from Capture::from_device()
or returns
an error.
pub fn timeout(self, ms: i32) -> Capture<Inactive>
[src]
Set the read timeout for the Capture. By default, this is 0, so it will block indefinitely.
pub fn tstamp_type(
self,
tstamp_type: TimestampType
) -> Result<Capture<Inactive>, Error>
[src]
self,
tstamp_type: TimestampType
) -> Result<Capture<Inactive>, Error>
Set the time stamp type to be used by a capture device.
pub fn promisc(self, to: bool) -> Capture<Inactive>
[src]
Set promiscuous mode on or off. By default, this is off.
pub fn rfmon(self, to: bool) -> Capture<Inactive>
[src]
Set rfmon mode on or off. The default is maintained by pcap.
pub fn buffer_size(self, to: i32) -> Capture<Inactive>
[src]
Set the buffer size for incoming packet data.
The default is 1000000. This should always be larger than the snaplen.
pub fn precision(self, precision: Precision) -> Result<Capture<Inactive>, Error>
[src]
Set the time stamp precision returned in captures.
pub fn snaplen(self, to: i32) -> Capture<Inactive>
[src]
Set the snaplen size (the maximum length of a packet captured into the buffer). Useful if you only want certain headers, but not the entire packet.
The default is 65535.
pub fn immediate_mode(self, to: bool) -> Capture<Inactive>
[src]
impl<T: Activated + ?Sized> Capture<T>
[src]
pub fn list_datalinks(&self) -> Result<Vec<Linktype>, Error>
[src]
List the datalink types that this captured device supports.
pub fn get_datalink(&self) -> Linktype
[src]
Get the current datalink type for this capture handle.
pub fn set_datalink(&mut self, linktype: Linktype) -> Result<(), Error>
[src]
Set the datalink type for the current capture handle.
pub fn savefile<P: AsRef<Path>>(&self, path: P) -> Result<Savefile, Error>
[src]
Create a Savefile
context for recording captured packets using this Capture
's
configurations.
pub fn savefile_raw_fd(&self, fd: c_int) -> Result<Savefile, Error>
[src]
Create a Savefile
context for recording captured packets using this Capture
's
configurations. The output is written to a raw file descriptor which is opened
pub fn direction(&self, direction: Direction) -> Result<(), Error>
[src]
Set the direction of the capture
pub fn next(&mut self) -> Result<Packet<'_>, Error>
[src]
Blocks until a packet is returned from the capture handle or an error occurs.
pcap captures packets and places them into a buffer which this function reads from. This buffer has a finite length, so if the buffer fills completely new packets will be discarded temporarily. This means that in realtime situations, you probably want to minimize the time between calls of this next() method.
pub fn filter(&mut self, program: &str) -> Result<(), Error>
[src]
Adds a filter to the capture using the given BPF program string. Internally
this is compiled using pcap_compile()
.
See http://biot.com/capstats/bpf.html for more information about this syntax.
pub fn stats(&mut self) -> Result<Stat, Error>
[src]
impl Capture<Active>
[src]
pub fn sendpacket<B: Borrow<[u8]>>(&mut self, buf: B) -> Result<(), Error>
[src]
Sends a packet over this capture handle's interface.
pub fn setnonblock(self) -> Result<Capture<Active>, Error>
[src]
impl Capture<Dead>
[src]
pub fn dead(linktype: Linktype) -> Result<Capture<Dead>, Error>
[src]
Creates a "fake" capture handle for the given link type.
Trait Implementations
impl AsRawFd for Capture<Active>
[src]
impl<T: State + ?Sized> Drop for Capture<T>
[src]
impl<T: Activated> From<Capture<T>> for Capture<dyn Activated>
[src]
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for Capture<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T: ?Sized> Send for Capture<T>
impl<T: ?Sized> Sync for Capture<T>
impl<T: ?Sized> Unpin for Capture<T> where
T: Unpin,
T: Unpin,
impl<T: ?Sized> UnwindSafe for Capture<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,