From 400f4b06c37c5e59562fc032b74bdcc9ee0a0f53 Mon Sep 17 00:00:00 2001 From: "Zach N." Date: Fri, 19 Jan 2024 11:25:10 -0800 Subject: [PATCH] Fix linux build --- src/dynamic_win_raw.rs | 36 ++++++++++++++++++++++++------------ src/lib.rs | 7 ++++--- src/raw.rs | 5 +++-- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/dynamic_win_raw.rs b/src/dynamic_win_raw.rs index 8368fbc86..101eb30f3 100644 --- a/src/dynamic_win_raw.rs +++ b/src/dynamic_win_raw.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] -use std::env; use crate::raw_common::{ bpf_program, pcap_direction_t, pcap_dumper_t, pcap_if_t, pcap_pkthdr, pcap_send_queue, pcap_stat, pcap_t, }; use libc::{c_char, c_int, c_uchar, c_uint, FILE}; use libloading::Library; -use std::path::PathBuf; use once_cell::sync::Lazy; +use std::env; +use std::path::PathBuf; use windows_sys::Win32::Foundation::HANDLE; type PcapCreate = unsafe extern "C" fn(arg1: *const c_char, arg2: *mut c_char) -> *mut pcap_t; @@ -90,9 +90,7 @@ type PcapSendqueueQueue = unsafe extern "C" fn( type PcapSendqueueTransmit = unsafe extern "C" fn(p: *mut pcap_t, queue: *mut pcap_send_queue, sync: c_int) -> c_uint; -static mut LIBRARY: Lazy = Lazy::new(|| { - unsafe { load_library() } -}); +static mut LIBRARY: Lazy = Lazy::new(|| unsafe { load_library() }); unsafe fn load_library() -> Library { let mut libfile = PathBuf::from("wpcap.dll"); @@ -141,11 +139,15 @@ pub unsafe fn pcap_open_dead(arg1: c_int, arg2: c_int) -> *mut pcap_t { func(arg1, arg2) } pub unsafe fn pcap_open_offline(arg1: *const c_char, arg2: *mut c_char) -> *mut pcap_t { - let func = LIBRARY.get::(b"pcap_open_offline").unwrap(); + let func = LIBRARY + .get::(b"pcap_open_offline") + .unwrap(); func(arg1, arg2) } pub unsafe fn pcap_fopen_offline(arg1: *mut FILE, arg2: *mut c_char) -> *mut pcap_t { - let func = LIBRARY.get::(b"pcap_fopen_offline").unwrap(); + let func = LIBRARY + .get::(b"pcap_fopen_offline") + .unwrap(); func(arg1, arg2) } pub unsafe fn pcap_close(arg1: *mut pcap_t) { @@ -169,7 +171,9 @@ pub unsafe fn pcap_setfilter(arg1: *mut pcap_t, arg2: *mut bpf_program) -> c_int func(arg1, arg2) } pub unsafe fn pcap_setdirection(arg1: *mut pcap_t, arg2: pcap_direction_t) -> c_int { - let func = LIBRARY.get::(b"pcap_setdirection").unwrap(); + let func = LIBRARY + .get::(b"pcap_setdirection") + .unwrap(); func(arg1, arg2) } pub unsafe fn pcap_setnonblock(arg1: *mut pcap_t, arg2: c_int, arg3: *mut c_char) -> c_int { @@ -219,7 +223,9 @@ pub unsafe fn pcap_list_datalinks(arg1: *mut pcap_t, arg2: *mut *mut c_int) -> c func(arg1, arg2) } pub unsafe fn pcap_set_datalink(arg1: *mut pcap_t, arg2: c_int) -> c_int { - let func = LIBRARY.get::(b"pcap_set_datalink").unwrap(); + let func = LIBRARY + .get::(b"pcap_set_datalink") + .unwrap(); func(arg1, arg2) } pub unsafe fn pcap_free_datalinks(arg1: *mut c_int) { @@ -247,11 +253,15 @@ pub unsafe fn pcap_datalink_val_to_description(arg1: c_int) -> *const c_char { func(arg1) } pub unsafe fn pcap_major_version(arg1: *mut pcap_t) -> c_int { - let func = LIBRARY.get::(b"pcap_major_version").unwrap(); + let func = LIBRARY + .get::(b"pcap_major_version") + .unwrap(); func(arg1) } pub unsafe fn pcap_minor_version(arg1: *mut pcap_t) -> c_int { - let func = LIBRARY.get::(b"pcap_minor_version").unwrap(); + let func = LIBRARY + .get::(b"pcap_minor_version") + .unwrap(); func(arg1) } pub unsafe fn pcap_fileno(arg1: *mut pcap_t) -> c_int { @@ -354,7 +364,9 @@ pub fn pcap_dump_open_append(arg1: *mut pcap_t, arg2: *const c_char) -> *mut pca } pub unsafe fn pcap_setmintocopy(arg1: *mut pcap_t, arg2: c_int) -> c_int { - let func = LIBRARY.get::(b"pcap_setmintocopy").unwrap(); + let func = LIBRARY + .get::(b"pcap_setmintocopy") + .unwrap(); func(arg1, arg2) } pub unsafe fn pcap_getevent(p: *mut pcap_t) -> HANDLE { diff --git a/src/lib.rs b/src/lib.rs index f8fa89889..f89af841f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,7 @@ mod raw; mod raw_common; #[cfg(all(target_os = "windows", feature = "dynamic-load"))] use dynamic_win_raw as raw; +#[cfg(windows)] pub mod sendqueue; #[cfg(feature = "capture-stream")] #[cfg_attr(windows, path = "stream_windows.rs")] @@ -1193,7 +1194,7 @@ impl Capture { #[cfg(not(windows))] pub unsafe fn savefile_raw_fd(&self, fd: RawFd) -> Result { open_raw_fd(fd, b'w').and_then(|file| { - let handle_opt = NonNull::::new(raw::pcap_dump_fopen( + let handle_opt = NonNull::::new(raw::pcap_dump_fopen( self.handle.as_ptr(), file, )); @@ -1213,7 +1214,7 @@ impl Capture { #[cfg(libpcap_1_7_2)] pub fn savefile_append>(&self, path: P) -> Result { let name = CString::new(path.as_ref().to_str().unwrap())?; - let handle_opt = NonNull::::new(unsafe { + let handle_opt = NonNull::::new(unsafe { raw::pcap_dump_open_append(self.handle.as_ptr(), name.as_ptr()) }); let handle = self @@ -1368,7 +1369,7 @@ impl Capture { raw::pcap_open_dead_with_tstamp_precision(linktype.0, 65535, precision as u32) }; Ok(Capture::from( - NonNull::::new(handle).ok_or(InsufficientMemory)?, + NonNull::::new(handle).ok_or(InsufficientMemory)?, )) } diff --git a/src/raw.rs b/src/raw.rs index 278046fda..3e734067a 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -2,9 +2,10 @@ use libc::{c_char, c_int, c_uchar, c_uint, FILE}; +#[cfg(windows)] +use crate::raw_common::pcap_send_queue; use crate::raw_common::{ - bpf_program, pcap_direction_t, pcap_dumper_t, pcap_if_t, pcap_pkthdr, pcap_send_queue, - pcap_stat, pcap_t, + bpf_program, pcap_direction_t, pcap_dumper_t, pcap_if_t, pcap_pkthdr, pcap_stat, pcap_t, }; #[cfg(windows)] use windows_sys::Win32::Foundation::HANDLE;