1
//! pcap is a packet capture library available on Linux, Windows and Mac. This
2
//! crate supports creating and configuring capture contexts, sniffing packets,
3
//! sending packets to interfaces, listing devices, and recording packet captures
4
//! to pcap-format dump files.
5
//!
6
//! # Capturing packets
7
//! The easiest way to open an active capture handle and begin sniffing is to
8
//! use `.open()` on a `Device`. You can obtain the "default" device using
9
//! `Device::lookup()`, or you can obtain the device(s) you need via `Device::list()`.
10
//!
11
//! ```no_run
12
//! use pcap::Device;
13
//!
14
//! let mut cap = Device::lookup().unwrap().unwrap().open().unwrap();
15
//!
16
//! while let Ok(packet) = cap.next_packet() {
17
//!     println!("received packet! {:?}", packet);
18
//! }
19
//!
20
//! ```
21
//!
22
//! `Capture`'s `.next_packet()` will produce a `Packet` which can be dereferenced to access the
23
//! `&[u8]` packet contents.
24
//!
25
//! # Custom configuration
26
//!
27
//! You may want to configure the `timeout`, `snaplen` or other parameters for the capture
28
//! handle. In this case, use `Capture::from_device()` to obtain a `Capture<Inactive>`, and
29
//! proceed to configure the capture handle. When you're finished, run `.open()` on it to
30
//! turn it into a `Capture<Active>`.
31
//!
32
//! ```no_run
33
//! use pcap::{Device, Capture};
34
//!
35
//! let main_device = Device::lookup().unwrap().unwrap();
36
//! let mut cap = Capture::from_device(main_device).unwrap()
37
//!                   .promisc(true)
38
//!                   .snaplen(5000)
39
//!                   .open().unwrap();
40
//!
41
//! while let Ok(packet) = cap.next_packet() {
42
//!     println!("received packet! {:?}", packet);
43
//! }
44
//! ```
45
//!
46
//! # Abstracting over different capture types
47
//!
48
//! You can abstract over live captures (`Capture<Active>`) and file captures
49
//! (`Capture<Offline>`) using generics and the [`Activated`] trait, for example:
50
//!
51
//! ```
52
//! use pcap::{Activated, Capture};
53
//!
54
//! fn read_packets<T: Activated>(mut capture: Capture<T>) {
55
//!     while let Ok(packet) = capture.next_packet() {
56
//!         println!("received packet! {:?}", packet);
57
//!     }
58
//! }
59
//! ```
60

            
61
#![cfg_attr(docsrs, feature(doc_cfg))]
62

            
63
use std::ffi::{self, CStr};
64
use std::fmt;
65

            
66
use self::Error::*;
67

            
68
mod capture;
69
mod codec;
70
mod device;
71
mod linktype;
72
mod packet;
73

            
74
#[cfg(not(windows))]
75
pub use capture::activated::open_raw_fd;
76
pub use capture::{
77
    activated::{
78
        iterator::PacketIter, BpfInstruction, BpfProgram, BreakLoop, Direction, Savefile, Stat,
79
    },
80
    inactive::TimestampType,
81
    {Activated, Active, Capture, Dead, Inactive, Offline, Precision, State},
82
};
83
pub use codec::PacketCodec;
84
pub use device::{Address, ConnectionStatus, Device, DeviceFlags, IfFlags};
85
pub use linktype::Linktype;
86
pub use packet::{Packet, PacketHeader};
87

            
88
#[deprecated(note = "Renamed to TimestampType")]
89
/// An old name for `TimestampType`, kept around for backward-compatibility.
90
pub type TstampType = TimestampType;
91

            
92
mod raw;
93

            
94
#[cfg(windows)]
95
#[cfg_attr(docsrs, doc(cfg(windows)))]
96
pub mod sendqueue;
97

            
98
#[cfg(feature = "capture-stream")]
99
mod stream;
100
#[cfg(feature = "capture-stream")]
101
#[cfg_attr(docsrs, doc(cfg(feature = "capture-stream")))]
102
pub use stream::PacketStream;
103

            
104
/// An error received from pcap
105
#[derive(Debug, PartialEq, Eq)]
106
pub enum Error {
107
    /// The underlying library returned invalid UTF-8
108
    MalformedError(std::str::Utf8Error),
109
    /// The underlying library returned a null string
110
    InvalidString,
111
    /// The unerlying library returned an error
112
    PcapError(String),
113
    /// The linktype was invalid or unknown
114
    InvalidLinktype,
115
    /// The timeout expired while reading from a live capture
116
    TimeoutExpired,
117
    /// No more packets to read from the file
118
    NoMorePackets,
119
    /// Must be in non-blocking mode to function
120
    NonNonBlock,
121
    /// There is not sufficent memory to create a dead capture
122
    InsufficientMemory,
123
    /// An invalid input string (internal null)
124
    InvalidInputString,
125
    /// An IO error occurred
126
    IoError(std::io::ErrorKind),
127
    #[cfg(not(windows))]
128
    /// An invalid raw file descriptor was provided
129
    InvalidRawFd,
130
    /// Errno error
131
    ErrnoError(errno::Errno),
132
    /// Buffer size overflows capacity
133
    BufferOverflow,
134
}
135

            
136
impl Error {
137
47
    unsafe fn new(ptr: *const libc::c_char) -> Error {
138
47
        match cstr_to_string(ptr) {
139
2
            Err(e) => e as Error,
140
45
            Ok(string) => PcapError(string.unwrap_or_default()),
141
        }
142
47
    }
143

            
144
54
    fn with_errbuf<T, F>(func: F) -> Result<T, Error>
145
54
    where
146
54
        F: FnOnce(*mut libc::c_char) -> Result<T, Error>,
147
    {
148
54
        let mut errbuf = [0i8; 256];
149
54
        func(errbuf.as_mut_ptr() as _)
150
54
    }
151
}
152

            
153
72
unsafe fn cstr_to_string(ptr: *const libc::c_char) -> Result<Option<String>, Error> {
154
72
    let string = if ptr.is_null() {
155
2
        None
156
    } else {
157
70
        Some(CStr::from_ptr(ptr as _).to_str()?.to_owned())
158
    };
159
70
    Ok(string)
160
72
}
161

            
162
impl fmt::Display for Error {
163
26
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164
26
        match *self {
165
2
            MalformedError(ref e) => write!(f, "libpcap returned invalid UTF-8: {e}"),
166
2
            InvalidString => write!(f, "libpcap returned a null string"),
167
2
            PcapError(ref e) => write!(f, "libpcap error: {e}"),
168
2
            InvalidLinktype => write!(f, "invalid or unknown linktype"),
169
2
            TimeoutExpired => write!(f, "timeout expired while reading from a live capture"),
170
2
            NonNonBlock => write!(f, "must be in non-blocking mode to function"),
171
2
            NoMorePackets => write!(f, "no more packets to read from the file"),
172
2
            InsufficientMemory => write!(f, "insufficient memory"),
173
2
            InvalidInputString => write!(f, "invalid input string (internal null)"),
174
2
            IoError(ref e) => write!(f, "io error occurred: {e:?}"),
175
            #[cfg(not(windows))]
176
2
            InvalidRawFd => write!(f, "invalid raw file descriptor provided"),
177
2
            ErrnoError(ref e) => write!(f, "libpcap os errno: {e}"),
178
2
            BufferOverflow => write!(f, "buffer size too large"),
179
        }
180
26
    }
181
}
182

            
183
// Using description is deprecated. Remove in next version.
184
impl std::error::Error for Error {
185
26
    fn description(&self) -> &str {
186
26
        match *self {
187
2
            MalformedError(..) => "libpcap returned invalid UTF-8",
188
2
            PcapError(..) => "libpcap FFI error",
189
2
            InvalidString => "libpcap returned a null string",
190
2
            InvalidLinktype => "invalid or unknown linktype",
191
2
            TimeoutExpired => "timeout expired while reading from a live capture",
192
2
            NonNonBlock => "must be in non-blocking mode to function",
193
2
            NoMorePackets => "no more packets to read from the file",
194
2
            InsufficientMemory => "insufficient memory",
195
2
            InvalidInputString => "invalid input string (internal null)",
196
2
            IoError(..) => "io error occurred",
197
            #[cfg(not(windows))]
198
2
            InvalidRawFd => "invalid raw file descriptor provided",
199
2
            ErrnoError(..) => "internal error, providing errno",
200
2
            BufferOverflow => "buffer size too large",
201
        }
202
26
    }
203

            
204
26
    fn cause(&self) -> Option<&dyn std::error::Error> {
205
26
        match *self {
206
2
            MalformedError(ref e) => Some(e),
207
24
            _ => None,
208
        }
209
26
    }
210
}
211

            
212
impl From<ffi::NulError> for Error {
213
2
    fn from(_: ffi::NulError) -> Error {
214
2
        InvalidInputString
215
2
    }
216
}
217

            
218
impl From<std::str::Utf8Error> for Error {
219
4
    fn from(obj: std::str::Utf8Error) -> Error {
220
4
        MalformedError(obj)
221
4
    }
222
}
223

            
224
impl From<std::io::Error> for Error {
225
2
    fn from(obj: std::io::Error) -> Error {
226
2
        obj.kind().into()
227
2
    }
228
}
229

            
230
impl From<std::io::ErrorKind> for Error {
231
2
    fn from(obj: std::io::ErrorKind) -> Error {
232
2
        IoError(obj)
233
2
    }
234
}
235

            
236
/// Return size of a commonly used packet header.
237
///
238
/// On Windows this packet header is implicitly added to send queues, so this size must be known
239
/// if an application needs to precalculate the exact send queue buffer size.
240
2
pub const fn packet_header_size() -> usize {
241
2
    std::mem::size_of::<raw::pcap_pkthdr>()
242
2
}
243

            
244
#[cfg(test)]
245
mod tests {
246
    use std::error::Error as StdError;
247
    use std::{ffi::CString, io};
248

            
249
    use super::*;
250

            
251
    #[test]
252
    fn test_error_invalid_utf8() {
253
        let bytes: [u8; 8] = [0x78, 0xfe, 0xe9, 0x89, 0x00, 0x00, 0xed, 0x4f];
254
        let error = unsafe { Error::new(&bytes as *const _ as _) };
255
        assert!(matches!(error, Error::MalformedError(_)));
256
    }
257

            
258
    #[test]
259
    fn test_error_null() {
260
        let error = unsafe { Error::new(std::ptr::null()) };
261
        assert_eq!(error, Error::PcapError("".to_string()));
262
    }
263

            
264
    #[test]
265
    #[allow(deprecated)]
266
    fn test_errors() {
267
        let mut errors: Vec<Error> = vec![];
268

            
269
        let bytes: [u8; 8] = [0x78, 0xfe, 0xe9, 0x89, 0x00, 0x00, 0xed, 0x4f];
270
        let cstr = unsafe { CStr::from_ptr(&bytes as *const _ as _) };
271

            
272
        errors.push(cstr.to_str().unwrap_err().into());
273
        errors.push(Error::InvalidString);
274
        errors.push(Error::PcapError("git rekt".to_string()));
275
        errors.push(Error::InvalidLinktype);
276
        errors.push(Error::TimeoutExpired);
277
        errors.push(Error::NoMorePackets);
278
        errors.push(Error::NonNonBlock);
279
        errors.push(Error::InsufficientMemory);
280
        errors.push(CString::new(b"f\0oo".to_vec()).unwrap_err().into());
281
        errors.push(io::Error::new(io::ErrorKind::Interrupted, "error").into());
282
        #[cfg(not(windows))]
283
        errors.push(Error::InvalidRawFd);
284
        errors.push(Error::ErrnoError(errno::Errno(125)));
285
        errors.push(Error::BufferOverflow);
286

            
287
        for error in errors.iter() {
288
            assert!(!error.to_string().is_empty());
289
            assert!(!error.description().is_empty());
290
            match error {
291
                Error::MalformedError(_) => assert!(error.cause().is_some()),
292
                _ => assert!(error.cause().is_none()),
293
            }
294
        }
295
    }
296

            
297
    #[test]
298
    fn test_packet_size() {
299
        assert_eq!(
300
            packet_header_size(),
301
            std::mem::size_of::<raw::pcap_pkthdr>()
302
        );
303
    }
304
}