Initial commit

This commit is contained in:
Conrad Kramer 2023-04-10 16:49:23 -04:00
commit c1e7415871
56 changed files with 3225 additions and 0 deletions

44
tun/src/windows/mod.rs Normal file
View file

@ -0,0 +1,44 @@
use std::io::Result;
use std::ptr;
use widestring::{u16cstr, U16CString};
pub struct TunInterface {
wintun: sys::wintun,
handle: sys::WINTUN_ADAPTER_HANDLE,
name: String,
}
impl TunInterface {
pub fn new() -> Result<TunInterface> {
let name = U16CString::from(u16cstr!("ConradNet"));
let wintun = sys::wintun::default();
let handle =
unsafe { wintun.WintunCreateAdapter(name.as_ptr(), name.as_ptr(), ptr::null()) };
Ok(TunInterface {
wintun,
handle,
name: String::from("ConradNet"),
})
}
pub fn name(&self) -> String {
self.name.clone()
}
}
impl Drop for TunInterface {
fn drop(&mut self) {
unsafe { self.wintun.WintunCloseAdapter(self.handle) }
}
}
pub(crate) mod sys {
#![allow(dead_code, non_camel_case_types, non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/wintun.rs"));
impl Default for wintun {
fn default() -> Self {
unsafe { wintun::new(format!("{}/wintun.dll", env!("OUT_DIR"))).unwrap() }
}
}
}