Device

Struct Device 

Source
pub struct Device {
    pub name: String,
    pub desc: Option<String>,
}
Expand description

A network device name and (potentially) pcap’s description of it.

Fields§

§name: String§desc: Option<String>

Implementations§

Source§

impl Device

Source

pub fn open(self) -> Result<Capture<Active>, Error>

Opens a Capture<Active> on this device.

Examples found in repository?
examples/easylisten.rs (line 5)
3fn main() {
4    // get the default Device
5    let mut cap = pcap::Device::lookup().unwrap().open().unwrap();
6
7    // get a packet and print its bytes
8    println!("{:?}", cap.next());
9}
More examples
Hide additional examples
examples/getstatistics.rs (line 5)
3fn main() {
4    // get the default Device
5    let mut cap = pcap::Device::lookup().unwrap().open().unwrap();
6
7    // get 10 packets
8    for _ in 0..10 {
9      cap.next().ok();
10    }
11    let stats = cap.stats().unwrap();
12    println!("Received: {}, dropped: {}, if_dropped: {}", stats.received, stats.dropped, stats.if_dropped);
13}
examples/getdevices.rs (line 9)
3fn main() {
4    // list all of the devices pcap tells us are available
5    for device in pcap::Device::list().unwrap() {
6        println!("Found device! {:?}", device);
7
8        // now you can create a Capture with this Device if you want.
9        let mut cap = device.open().unwrap();
10
11        // get a packet from this capture
12        let packet = cap.next();
13
14        println!("got a packet! {:?}", packet);
15    }
16}
Source

pub fn lookup() -> Result<Device, Error>

Returns the default Device suitable for captures according to pcap_lookupdev, or an error from pcap.

Examples found in repository?
examples/easylisten.rs (line 5)
3fn main() {
4    // get the default Device
5    let mut cap = pcap::Device::lookup().unwrap().open().unwrap();
6
7    // get a packet and print its bytes
8    println!("{:?}", cap.next());
9}
More examples
Hide additional examples
examples/getstatistics.rs (line 5)
3fn main() {
4    // get the default Device
5    let mut cap = pcap::Device::lookup().unwrap().open().unwrap();
6
7    // get 10 packets
8    for _ in 0..10 {
9      cap.next().ok();
10    }
11    let stats = cap.stats().unwrap();
12    println!("Received: {}, dropped: {}, if_dropped: {}", stats.received, stats.dropped, stats.if_dropped);
13}
examples/savefile.rs (line 8)
5fn main() {
6	{
7		// open capture from default device
8		let mut cap = Capture::from_device(Device::lookup().unwrap()).unwrap().open().unwrap();
9
10		// open savefile using the capture
11		let mut savefile = cap.savefile("test.pcap").unwrap();
12
13		// get a packet from the interface
14		let p = cap.next().unwrap();
15
16		// print the packet out
17		println!("packet received on network: {:?}", p);
18
19		// write the packet to the savefile
20		savefile.write(&p);
21	}
22
23	// open a new capture from the test.pcap file we wrote to above
24	let mut cap = Capture::from_file("test.pcap").unwrap();
25
26	// get a packet
27	let p = cap.next().unwrap();
28
29	// print that packet out -- it should be the same as the one we printed above
30	println!("packet obtained from file: {:?}", p);
31}
Source

pub fn list() -> Result<Vec<Device>, Error>

Returns a vector of Devices known by pcap via pcap_findalldevs.

Examples found in repository?
examples/getdevices.rs (line 5)
3fn main() {
4    // list all of the devices pcap tells us are available
5    for device in pcap::Device::list().unwrap() {
6        println!("Found device! {:?}", device);
7
8        // now you can create a Capture with this Device if you want.
9        let mut cap = device.open().unwrap();
10
11        // get a packet from this capture
12        let packet = cap.next();
13
14        println!("got a packet! {:?}", packet);
15    }
16}

Trait Implementations§

Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Into<Device> for &'a str

Source§

fn into(self) -> Device

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.