Skip to content

Commit

Permalink
refactor bindgen script
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Aug 22, 2024
1 parent aac55b8 commit 29742c5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 255 deletions.
48 changes: 48 additions & 0 deletions bindgen.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Description: Generate Rust bindings for Wintun using bindgen
# Usage: Run this script from the root of the wintun-bindings crate

bindgen `
--allowlist-function "Wintun.*" `
--allowlist-type "WINTUN_.*" `
--allowlist-var "WINTUN_.*" `
--blocklist-type "_GUID" `
--blocklist-type "BOOL" `
--blocklist-type "BYTE" `
--blocklist-type "DWORD" `
--blocklist-type "DWORD64" `
--blocklist-type "GUID" `
--blocklist-type "HANDLE" `
--blocklist-type "LPCWSTR" `
--blocklist-type "NET_LUID" `
--blocklist-type "WCHAR" `
--blocklist-type "wchar_t" `
--opaque-type "NET_LUID" `
--dynamic-loading wintun `
--dynamic-link-require-all wintun/include/wintun_functions.h > src/wintun_raw.rs `
-- --target=i686-pc-windows-msvc

# Insert prelude to generated file
$prelude = @'
#![allow(non_snake_case, non_camel_case_types)]
#![cfg(target_os = "windows")]
use windows_sys::core::GUID;
use windows_sys::core::PCWSTR as LPCWSTR;
use windows_sys::Win32::Foundation::BOOL;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH as NET_LUID;
pub type DWORD = core::ffi::c_ulong;
pub type BYTE = core::ffi::c_uchar;
pub type DWORD64 = core::ffi::c_ulonglong;
'@

# Write prelude to a tmp file
$tmpFile = "src/wintun_raw.rs.tmp"
$prelude | Out-File -FilePath $tmpFile -Encoding utf8

# Append generated file to tmp file
Get-Content src/wintun_raw.rs | Add-Content -Path $tmpFile

# Replace generated file with tmp file
Move-Item -Path $tmpFile -Destination src/wintun_raw.rs -Force
8 changes: 0 additions & 8 deletions bindgen.sh

This file was deleted.

8 changes: 4 additions & 4 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub struct Adapter {
}

fn get_adapter_luid(wintun: &Wintun, adapter: wintun_raw::WINTUN_ADAPTER_HANDLE) -> NET_LUID_LH {
let mut luid: wintun_raw::NET_LUID = unsafe { std::mem::zeroed() };
unsafe { wintun.WintunGetAdapterLUID(adapter, &mut luid as *mut wintun_raw::NET_LUID) };
let mut luid: NET_LUID_LH = unsafe { std::mem::zeroed() };
unsafe { wintun.WintunGetAdapterLUID(adapter, &mut luid as *mut NET_LUID_LH) };
unsafe { std::mem::transmute(luid) }

Check failure on line 43 in src/adapter.rs

View workflow job for this annotation

GitHub Actions / Clippy Check & Build

transmute from a type (`windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH`) to itself
}

Expand Down Expand Up @@ -103,8 +103,8 @@ impl Adapter {

crate::log::set_default_logger_if_unset(wintun);

let guid_struct: wintun_raw::GUID = unsafe { std::mem::transmute(GUID::from_u128(guid)) };
let guid_ptr = &guid_struct as *const wintun_raw::GUID;
let guid_struct: GUID = unsafe { std::mem::transmute(GUID::from_u128(guid)) };

Check failure on line 106 in src/adapter.rs

View workflow job for this annotation

GitHub Actions / Clippy Check & Build

transmute from a type (`windows_sys::core::GUID`) to itself
let guid_ptr = &guid_struct as *const GUID;

let result = unsafe { wintun.WintunCreateAdapter(name_utf16.as_ptr(), tunnel_type_utf16.as_ptr(), guid_ptr) };

Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static SET_LOGGER: AtomicBool = AtomicBool::new(false);
pub unsafe extern "stdcall" fn default_logger(
level: wintun_raw::WINTUN_LOGGER_LEVEL,
_timestamp: wintun_raw::DWORD64,
message: *const wintun_raw::WCHAR,
message: windows_sys::core::PCWSTR,
) {
//Wintun will always give us a valid UTF16 null termineted string
let utf8_msg = util::win_pwstr_to_string(message as *mut u16).unwrap_or_else(|e| e.to_string());
Expand Down
257 changes: 15 additions & 242 deletions src/wintun_raw.rs
Original file line number Diff line number Diff line change
@@ -1,247 +1,20 @@
#![allow(non_snake_case, non_camel_case_types)]
#![cfg(target_os = "windows")]

use windows_sys::core::GUID;
use windows_sys::core::PCWSTR as LPCWSTR;
use windows_sys::Win32::Foundation::BOOL;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH as NET_LUID;
pub type DWORD = core::ffi::c_ulong;
pub type BYTE = core::ffi::c_uchar;
pub type DWORD64 = core::ffi::c_ulonglong;

/* automatically generated by rust-bindgen 0.66.1 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage> {
storage: Storage,
}
impl<Storage> __BindgenBitfieldUnit<Storage> {
#[inline]
pub const fn new(storage: Storage) -> Self {
Self { storage }
}
}
impl<Storage> __BindgenBitfieldUnit<Storage>
where
Storage: AsRef<[u8]> + AsMut<[u8]>,
{
#[inline]
pub fn get_bit(&self, index: usize) -> bool {
debug_assert!(index / 8 < self.storage.as_ref().len());
let byte_index = index / 8;
let byte = self.storage.as_ref()[byte_index];
let bit_index = if cfg!(target_endian = "big") {
7 - (index % 8)
} else {
index % 8
};
let mask = 1 << bit_index;
byte & mask == mask
}
#[inline]
pub fn set_bit(&mut self, index: usize, val: bool) {
debug_assert!(index / 8 < self.storage.as_ref().len());
let byte_index = index / 8;
let byte = &mut self.storage.as_mut()[byte_index];
let bit_index = if cfg!(target_endian = "big") {
7 - (index % 8)
} else {
index % 8
};
let mask = 1 << bit_index;
if val {
*byte |= mask;
} else {
*byte &= !mask;
}
}
#[inline]
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
debug_assert!(bit_width <= 64);
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
let mut val = 0;
for i in 0..(bit_width as usize) {
if self.get_bit(i + bit_offset) {
let index = if cfg!(target_endian = "big") {
bit_width as usize - 1 - i
} else {
i
};
val |= 1 << index;
}
}
val
}
#[inline]
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
debug_assert!(bit_width <= 64);
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
for i in 0..(bit_width as usize) {
let mask = 1 << i;
let val_bit_is_set = val & mask == mask;
let index = if cfg!(target_endian = "big") {
bit_width as usize - 1 - i
} else {
i
};
self.set_bit(index + bit_offset, val_bit_is_set);
}
}
}
pub type wchar_t = ::std::os::raw::c_ushort;
pub type DWORD = ::std::os::raw::c_ulong;
pub type BOOL = ::std::os::raw::c_int;
pub type BYTE = ::std::os::raw::c_uchar;
pub type ULONG64 = ::std::os::raw::c_ulonglong;
pub type DWORD64 = ::std::os::raw::c_ulonglong;
pub type WCHAR = wchar_t;
pub type LPCWSTR = *const WCHAR;
pub type HANDLE = *mut ::std::os::raw::c_void;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _GUID {
pub Data1: ::std::os::raw::c_ulong,
pub Data2: ::std::os::raw::c_ushort,
pub Data3: ::std::os::raw::c_ushort,
pub Data4: [::std::os::raw::c_uchar; 8usize],
}
#[test]
fn bindgen_test_layout__GUID() {
const UNINIT: ::std::mem::MaybeUninit<_GUID> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<_GUID>(),
16usize,
concat!("Size of: ", stringify!(_GUID))
);
assert_eq!(
::std::mem::align_of::<_GUID>(),
4usize,
concat!("Alignment of ", stringify!(_GUID))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Data1) as usize - ptr as usize },
0usize,
concat!("Offset of field: ", stringify!(_GUID), "::", stringify!(Data1))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Data2) as usize - ptr as usize },
4usize,
concat!("Offset of field: ", stringify!(_GUID), "::", stringify!(Data2))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Data3) as usize - ptr as usize },
6usize,
concat!("Offset of field: ", stringify!(_GUID), "::", stringify!(Data3))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Data4) as usize - ptr as usize },
8usize,
concat!("Offset of field: ", stringify!(_GUID), "::", stringify!(Data4))
);
}
pub type GUID = _GUID;
#[repr(C)]
#[derive(Copy, Clone)]
pub union _NET_LUID_LH {
pub Value: ULONG64,
pub Info: _NET_LUID_LH__bindgen_ty_1,
}
#[repr(C)]
#[repr(align(8))]
#[derive(Debug, Copy, Clone)]
pub struct _NET_LUID_LH__bindgen_ty_1 {
pub _bitfield_align_1: [u32; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 8usize]>,
}
#[test]
fn bindgen_test_layout__NET_LUID_LH__bindgen_ty_1() {
assert_eq!(
::std::mem::size_of::<_NET_LUID_LH__bindgen_ty_1>(),
8usize,
concat!("Size of: ", stringify!(_NET_LUID_LH__bindgen_ty_1))
);
assert_eq!(
::std::mem::align_of::<_NET_LUID_LH__bindgen_ty_1>(),
8usize,
concat!("Alignment of ", stringify!(_NET_LUID_LH__bindgen_ty_1))
);
}
impl _NET_LUID_LH__bindgen_ty_1 {
#[inline]
pub fn Reserved(&self) -> ULONG64 {
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 24u8) as u64) }
}
#[inline]
pub fn set_Reserved(&mut self, val: ULONG64) {
unsafe {
let val: u64 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 24u8, val as u64)
}
}
#[inline]
pub fn NetLuidIndex(&self) -> ULONG64 {
unsafe { ::std::mem::transmute(self._bitfield_1.get(24usize, 24u8) as u64) }
}
#[inline]
pub fn set_NetLuidIndex(&mut self, val: ULONG64) {
unsafe {
let val: u64 = ::std::mem::transmute(val);
self._bitfield_1.set(24usize, 24u8, val as u64)
}
}
#[inline]
pub fn IfType(&self) -> ULONG64 {
unsafe { ::std::mem::transmute(self._bitfield_1.get(48usize, 16u8) as u64) }
}
#[inline]
pub fn set_IfType(&mut self, val: ULONG64) {
unsafe {
let val: u64 = ::std::mem::transmute(val);
self._bitfield_1.set(48usize, 16u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
Reserved: ULONG64,
NetLuidIndex: ULONG64,
IfType: ULONG64,
) -> __BindgenBitfieldUnit<[u8; 8usize]> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 8usize]> = Default::default();
__bindgen_bitfield_unit.set(0usize, 24u8, {
let Reserved: u64 = unsafe { ::std::mem::transmute(Reserved) };
Reserved as u64
});
__bindgen_bitfield_unit.set(24usize, 24u8, {
let NetLuidIndex: u64 = unsafe { ::std::mem::transmute(NetLuidIndex) };
NetLuidIndex as u64
});
__bindgen_bitfield_unit.set(48usize, 16u8, {
let IfType: u64 = unsafe { ::std::mem::transmute(IfType) };
IfType as u64
});
__bindgen_bitfield_unit
}
}
#[test]
fn bindgen_test_layout__NET_LUID_LH() {
const UNINIT: ::std::mem::MaybeUninit<_NET_LUID_LH> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<_NET_LUID_LH>(),
8usize,
concat!("Size of: ", stringify!(_NET_LUID_LH))
);
assert_eq!(
::std::mem::align_of::<_NET_LUID_LH>(),
8usize,
concat!("Alignment of ", stringify!(_NET_LUID_LH))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Value) as usize - ptr as usize },
0usize,
concat!("Offset of field: ", stringify!(_NET_LUID_LH), "::", stringify!(Value))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).Info) as usize - ptr as usize },
0usize,
concat!("Offset of field: ", stringify!(_NET_LUID_LH), "::", stringify!(Info))
);
}
pub type NET_LUID_LH = _NET_LUID_LH;
pub type NET_LUID = NET_LUID_LH;
pub const WINTUN_MIN_RING_CAPACITY: u32 = 131072;
pub const WINTUN_MAX_RING_CAPACITY: u32 = 67108864;
pub const WINTUN_MAX_IP_PACKET_SIZE: u32 = 65535;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _WINTUN_ADAPTER {
Expand Down

0 comments on commit 29742c5

Please sign in to comment.