1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::wintun_raw;
use winapi::{
shared::ntdef::{LANG_NEUTRAL, SUBLANG_DEFAULT},
um::{winbase, winnt::MAKELANGID},
};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Arc;
use widestring::U16Str;
pub(crate) struct UnsafeHandle<T>(pub T);
unsafe impl<T> Send for UnsafeHandle<T> {}
unsafe impl<T> Sync for UnsafeHandle<T> {}
pub fn get_error_message(err_code: u32) -> String {
const LEN: usize = 256;
let mut buf = MaybeUninit::<[u16; LEN]>::uninit();
let first = unsafe { *buf.as_mut_ptr() }.as_mut_ptr();
unsafe { first.write(0u16) };
let chars_written = unsafe {
winbase::FormatMessageW(
winbase::FORMAT_MESSAGE_FROM_SYSTEM | winbase::FORMAT_MESSAGE_IGNORE_INSERTS,
ptr::null(),
err_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) as u32,
first,
LEN as u32,
ptr::null_mut(),
)
};
format!(
"{} ({})",
unsafe { U16Str::from_ptr(first, chars_written as usize) }.to_string_lossy(),
err_code
)
}
pub fn get_running_driver_version(wintun: &Arc<wintun_raw::wintun>) -> (u16, u16) {
let version = unsafe { wintun.WintunGetRunningDriverVersion() };
(((version >> 16) & 0xFF) as u16, (version & 0xFF) as u16)
}