Skip to content

Commit

Permalink
Fix linux build
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachNo committed Jan 19, 2024
1 parent 3520d16 commit 400f4b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
36 changes: 24 additions & 12 deletions src/dynamic_win_raw.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Library> = Lazy::new(|| {
unsafe { load_library() }
});
static mut LIBRARY: Lazy<Library> = Lazy::new(|| unsafe { load_library() });

unsafe fn load_library() -> Library {
let mut libfile = PathBuf::from("wpcap.dll");
Expand Down Expand Up @@ -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::<PcapOpenOffline>(b"pcap_open_offline").unwrap();
let func = LIBRARY
.get::<PcapOpenOffline>(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::<PcapFopenOffline>(b"pcap_fopen_offline").unwrap();
let func = LIBRARY
.get::<PcapFopenOffline>(b"pcap_fopen_offline")
.unwrap();
func(arg1, arg2)
}
pub unsafe fn pcap_close(arg1: *mut pcap_t) {
Expand All @@ -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::<PcapSetdirection>(b"pcap_setdirection").unwrap();
let func = LIBRARY
.get::<PcapSetdirection>(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 {
Expand Down Expand Up @@ -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::<PcapSetDatalink>(b"pcap_set_datalink").unwrap();
let func = LIBRARY
.get::<PcapSetDatalink>(b"pcap_set_datalink")
.unwrap();
func(arg1, arg2)
}
pub unsafe fn pcap_free_datalinks(arg1: *mut c_int) {
Expand Down Expand Up @@ -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::<PcapMajorVersion>(b"pcap_major_version").unwrap();
let func = LIBRARY
.get::<PcapMajorVersion>(b"pcap_major_version")
.unwrap();
func(arg1)
}
pub unsafe fn pcap_minor_version(arg1: *mut pcap_t) -> c_int {
let func = LIBRARY.get::<PcapMinorVersion>(b"pcap_minor_version").unwrap();
let func = LIBRARY
.get::<PcapMinorVersion>(b"pcap_minor_version")
.unwrap();
func(arg1)
}
pub unsafe fn pcap_fileno(arg1: *mut pcap_t) -> c_int {
Expand Down Expand Up @@ -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::<PcapSetmintocopy>(b"pcap_setmintocopy").unwrap();
let func = LIBRARY
.get::<PcapSetmintocopy>(b"pcap_setmintocopy")
.unwrap();
func(arg1, arg2)
}
pub unsafe fn pcap_getevent(p: *mut pcap_t) -> HANDLE {
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -1193,7 +1194,7 @@ impl<T: Activated + ?Sized> Capture<T> {
#[cfg(not(windows))]
pub unsafe fn savefile_raw_fd(&self, fd: RawFd) -> Result<Savefile, Error> {
open_raw_fd(fd, b'w').and_then(|file| {
let handle_opt = NonNull::<raw::pcap_dumper_t>::new(raw::pcap_dump_fopen(
let handle_opt = NonNull::<raw_common::pcap_dumper_t>::new(raw::pcap_dump_fopen(
self.handle.as_ptr(),
file,
));
Expand All @@ -1213,7 +1214,7 @@ impl<T: Activated + ?Sized> Capture<T> {
#[cfg(libpcap_1_7_2)]
pub fn savefile_append<P: AsRef<Path>>(&self, path: P) -> Result<Savefile, Error> {
let name = CString::new(path.as_ref().to_str().unwrap())?;
let handle_opt = NonNull::<raw::pcap_dumper_t>::new(unsafe {
let handle_opt = NonNull::<raw_common::pcap_dumper_t>::new(unsafe {
raw::pcap_dump_open_append(self.handle.as_ptr(), name.as_ptr())
});
let handle = self
Expand Down Expand Up @@ -1368,7 +1369,7 @@ impl Capture<Dead> {
raw::pcap_open_dead_with_tstamp_precision(linktype.0, 65535, precision as u32)
};
Ok(Capture::from(
NonNull::<raw::pcap_t>::new(handle).ok_or(InsufficientMemory)?,
NonNull::<raw_common::pcap_t>::new(handle).ok_or(InsufficientMemory)?,
))
}

Expand Down
5 changes: 3 additions & 2 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 400f4b0

Please sign in to comment.