use wdk::println;
use wdk_sys::{
call_unsafe_wdf_function_binding, NTSTATUS,
STATUS_INVALID_PARAMETER, STATUS_SUCCESS, ULONG, WDFQUEUE, WDFREQUEST,
};
pub mod codes {
use wdk_sys::ULONG;
pub const FILE_DEVICE_LEVIATHAN: ULONG = 0x8000;
pub const IOCTL_LEVIATHAN_GET_VERSION: ULONG =
(FILE_DEVICE_LEVIATHAN << 16) | (0x800 << 2) | 0 | 0;
pub const IOCTL_LEVIATHAN_ECHO: ULONG =
(FILE_DEVICE_LEVIATHAN << 16) | (0x801 << 2) | 0 | 0;
pub const IOCTL_LEVIATHAN_GET_STATS: ULONG =
(FILE_DEVICE_LEVIATHAN << 16) | (0x802 << 2) | 0 | 0;
}
#[repr(C)]
#[derive(Default)]
pub struct DriverStats {
pub read_count: u64,
pub write_count: u64,
pub ioctl_count: u64,
pub bytes_read: u64,
pub bytes_written: u64,
}
static mut STATS: DriverStats = DriverStats {
read_count: 0,
write_count: 0,
ioctl_count: 0,
bytes_read: 0,
bytes_written: 0,
};
#[unsafe(no_mangle)]
pub unsafe extern "C" fn evt_io_read(
_queue: WDFQUEUE,
request: WDFREQUEST,
length: usize,
) {
println!("[Leviathan] Read request received, length: {}", length);
unsafe {
STATS.read_count += 1;
}
unsafe {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
STATUS_SUCCESS,
0
);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn evt_io_write(
_queue: WDFQUEUE,
request: WDFREQUEST,
length: usize,
) {
println!("[Leviathan] Write request received, length: {}", length);
unsafe {
STATS.write_count += 1;
STATS.bytes_written += length as u64;
}
unsafe {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
STATUS_SUCCESS,
length as u64
);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn evt_io_device_control(
_queue: WDFQUEUE,
request: WDFREQUEST,
_output_buffer_length: usize,
_input_buffer_length: usize,
io_control_code: ULONG,
) {
println!("[Leviathan] IOCTL received: {:#x}", io_control_code);
unsafe {
STATS.ioctl_count += 1;
}
let status = match io_control_code {
codes::IOCTL_LEVIATHAN_GET_VERSION => {
unsafe { handle_get_version(request) }
}
codes::IOCTL_LEVIATHAN_ECHO => {
unsafe { handle_echo(request) }
}
codes::IOCTL_LEVIATHAN_GET_STATS => {
unsafe { handle_get_stats(request) }
}
_ => {
println!("[Leviathan] Unknown IOCTL code: {:#x}", io_control_code);
STATUS_INVALID_PARAMETER
}
};
unsafe {
call_unsafe_wdf_function_binding!(
WdfRequestComplete,
request,
status
);
}
}
unsafe fn handle_get_version(_request: WDFREQUEST) -> NTSTATUS {
let version = crate::DRIVER_VERSION;
println!("[Leviathan] Returning version: {}", version);
STATUS_SUCCESS
}
unsafe fn handle_echo(_request: WDFREQUEST) -> NTSTATUS {
println!("[Leviathan] Echo IOCTL");
STATUS_SUCCESS
}
unsafe fn handle_get_stats(_request: WDFREQUEST) -> NTSTATUS {
println!("[Leviathan] Get stats IOCTL");
STATUS_SUCCESS
}