|
| 1 | +/* |
| 2 | +* Copyright 2019 Comcast Cable Communications Management, LLC |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +* SPDX-License-Identifier: Apache-2.0 |
| 17 | +*/ |
| 18 | + |
| 19 | +use super::{AsStr, EasyPtr, ToCString, ToResult}; |
| 20 | +use crate::ffi::dpdk::MbufPtr; |
| 21 | +use anyhow::Result; |
| 22 | +use capsule_ffi as cffi; |
| 23 | +use libc; |
| 24 | +use std::ops::DerefMut; |
| 25 | +use std::os::raw; |
| 26 | +use std::ptr; |
| 27 | +use thiserror::Error; |
| 28 | + |
| 29 | +// Ethernet (10Mb, 100Mb, 1000Mb, and up); the 10MB in the DLT_ name is historical. |
| 30 | +const DLT_EN10MB: raw::c_int = 1; |
| 31 | + |
| 32 | +// https://github.com/the-tcpdump-group/libpcap/blob/master/pcap/pcap.h#L152 |
| 33 | +#[allow(dead_code)] |
| 34 | +const PCAP_ERRBUF_SIZE: usize = 256; |
| 35 | + |
| 36 | +/// A `pcap_t` pointer. |
| 37 | +pub(crate) type PcapPtr = EasyPtr<cffi::pcap_t>; |
| 38 | + |
| 39 | +/// Creates a `libpcap` handle needed to call other functions. |
| 40 | +pub(crate) fn open_dead() -> Result<PcapPtr> { |
| 41 | + let ptr = unsafe { |
| 42 | + cffi::pcap_open_dead(DLT_EN10MB, cffi::RTE_MBUF_DEFAULT_BUF_SIZE as raw::c_int) |
| 43 | + .into_result(|_| PcapError::new("Cannot create libpcap handle."))? |
| 44 | + }; |
| 45 | + |
| 46 | + Ok(EasyPtr(ptr)) |
| 47 | +} |
| 48 | + |
| 49 | +/// A `pcap_dumper_t` pointer. |
| 50 | +pub(crate) type DumperPtr = EasyPtr<cffi::pcap_dumper_t>; |
| 51 | + |
| 52 | +/// Opens a file to which to write packets. |
| 53 | +pub(crate) fn dump_open<S: Into<String>>(handle: &mut PcapPtr, filename: S) -> Result<DumperPtr> { |
| 54 | + let filename: String = filename.into(); |
| 55 | + let ptr = unsafe { |
| 56 | + cffi::pcap_dump_open(handle.deref_mut(), filename.into_cstring().as_ptr()) |
| 57 | + .into_result(|_| PcapError::get_error(handle))? |
| 58 | + }; |
| 59 | + |
| 60 | + Ok(EasyPtr(ptr)) |
| 61 | +} |
| 62 | + |
| 63 | +/// Writes a packet to a capture file. |
| 64 | +pub(crate) fn dump(dumper: &mut DumperPtr, mbuf: &MbufPtr) { |
| 65 | + let mut pkthdr = cffi::pcap_pkthdr::default(); |
| 66 | + pkthdr.len = mbuf.data_len as u32; |
| 67 | + pkthdr.caplen = pkthdr.len; |
| 68 | + |
| 69 | + unsafe { |
| 70 | + // If this errors, we'll still want to write packet(s) to the pcap, |
| 71 | + let _ = libc::gettimeofday( |
| 72 | + &mut pkthdr.ts as *mut cffi::timeval as *mut libc::timeval, |
| 73 | + ptr::null_mut(), |
| 74 | + ); |
| 75 | + |
| 76 | + cffi::pcap_dump( |
| 77 | + dumper.deref_mut() as *mut cffi::pcap_dumper_t as *mut raw::c_uchar, |
| 78 | + &pkthdr, |
| 79 | + (mbuf.buf_addr as *mut u8).offset(mbuf.data_off as isize), |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Flushes to a savefile packets dumped. |
| 85 | +pub(crate) fn dump_flush(dumper: &mut DumperPtr) -> Result<()> { |
| 86 | + unsafe { |
| 87 | + cffi::pcap_dump_flush(dumper.deref_mut()) |
| 88 | + .into_result(|_| PcapError::new("Cannot flush packets to capture file.")) |
| 89 | + .map(|_| ()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Closes a savefile being written to. |
| 94 | +pub(crate) fn dump_close(dumper: &mut DumperPtr) { |
| 95 | + unsafe { |
| 96 | + cffi::pcap_dump_close(dumper.deref_mut()); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Closes a capture device or savefile |
| 101 | +pub(crate) fn close(handle: &mut PcapPtr) { |
| 102 | + unsafe { |
| 103 | + cffi::pcap_close(handle.deref_mut()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Opens a saved capture file for reading. |
| 108 | +#[cfg(test)] |
| 109 | +pub(crate) fn open_offline<S: Into<String>>(filename: S) -> Result<PcapPtr> { |
| 110 | + let filename: String = filename.into(); |
| 111 | + let mut errbuf: [raw::c_char; PCAP_ERRBUF_SIZE] = [0; PCAP_ERRBUF_SIZE]; |
| 112 | + |
| 113 | + let ptr = unsafe { |
| 114 | + cffi::pcap_open_offline(filename.into_cstring().as_ptr(), errbuf.as_mut_ptr()) |
| 115 | + .into_result(|_| PcapError::new(errbuf.as_str()))? |
| 116 | + }; |
| 117 | + |
| 118 | + Ok(EasyPtr(ptr)) |
| 119 | +} |
| 120 | + |
| 121 | +/// Reads the next packet from a `pcap_t` handle. |
| 122 | +#[cfg(test)] |
| 123 | +pub(crate) fn next(handle: &mut PcapPtr) -> Result<&[u8]> { |
| 124 | + let mut pkthdr: *mut cffi::pcap_pkthdr = ptr::null_mut(); |
| 125 | + let mut pktdata: *const raw::c_uchar = ptr::null(); |
| 126 | + |
| 127 | + unsafe { |
| 128 | + match cffi::pcap_next_ex(handle.deref_mut(), &mut pkthdr, &mut pktdata) { |
| 129 | + 1 => Ok(std::slice::from_raw_parts( |
| 130 | + pktdata, |
| 131 | + (*pkthdr).caplen as usize, |
| 132 | + )), |
| 133 | + _ => Err(PcapError::get_error(handle).into()), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// An error generated in `libpcap`. |
| 139 | +#[derive(Debug, Error)] |
| 140 | +#[error("{0}")] |
| 141 | +pub(crate) struct PcapError(String); |
| 142 | + |
| 143 | +impl PcapError { |
| 144 | + /// Returns the `PcapError` with the given error message. |
| 145 | + #[inline] |
| 146 | + fn new(msg: &str) -> Self { |
| 147 | + PcapError(msg.into()) |
| 148 | + } |
| 149 | + |
| 150 | + /// Returns the `PcapError` pertaining to the last `libpcap` error. |
| 151 | + #[inline] |
| 152 | + fn get_error(handle: &mut PcapPtr) -> Self { |
| 153 | + let msg = unsafe { cffi::pcap_geterr(handle.deref_mut()) }; |
| 154 | + PcapError::new((msg as *const raw::c_char).as_str()) |
| 155 | + } |
| 156 | +} |
0 commit comments