rawsock/common/
mod.rs

1mod packet;
2mod lib_version;
3mod interf_desc;
4mod data_link;
5mod err;
6
7use crate::traits::Library;
8use crate::{wpcap, pcap, pfring};
9use std::sync::Arc;
10
11
12pub use self::lib_version::LibraryVersion;
13pub use self::packet::{Packet, OwnedPacket, BorrowedPacket};
14pub use self::interf_desc::InterfaceDescription;
15pub use self::data_link::DataLink;
16pub use self::err::Error;
17
18
19
20/**
21Opens optimal library available on the platform.
22
23# Example
24
25```no_run
26extern crate rawsock;
27use rawsock::open_best_library;
28
29fn main(){
30    let lib = open_best_library().expect("Could not open any library.");
31
32    // do something with the library
33}
34```
35*/
36pub fn open_best_library() -> Result<Box<dyn Library>, Error> {
37    if let Ok(l) = pfring::Library::open_default_paths() {
38        return Ok(Box::new(l));
39    }
40    if let Ok(l) = wpcap::Library::open_default_paths() {
41        return Ok(Box::new(l));
42    }
43    match pcap::Library::open_default_paths() {
44        Ok(l) => Ok(Box::new(l)),
45        Err(e) => Err(e)
46    }
47}
48
49/// Multi-thread version of open_best_library()
50pub fn open_best_library_arc() -> Result<Arc<dyn Library>, Error> {
51    if let Ok(l) = pfring::Library::open_default_paths() {
52        return Ok(Arc::new(l));
53    }
54    if let Ok(l) = wpcap::Library::open_default_paths() {
55        return Ok(Arc::new(l));
56    }
57    match pcap::Library::open_default_paths() {
58        Ok(l) => Ok(Arc::new(l)),
59        Err(e) => Err(e)
60    }
61}
62
63///Provides library statistics
64#[derive(Copy, Clone, Debug)]
65pub struct Stats {
66    /// Received frames
67    pub received: u64,
68    ///Dropped frames
69    pub dropped: u64
70}
71