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() }
}
}
}

19
tun/src/windows/queue.rs Normal file
View file

@ -0,0 +1,19 @@
use crate::TunInterface;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
pub struct TunQueue {
inner: crate::windows::sys::
inner: socket2::Socket,
}
impl AsRawFd for TunQueue {
fn as_raw_fd(&self) -> RawFd {
self.socket.as_raw_fd()
}
}
impl IntoRawFd for TunQueue {
fn into_raw_fd(self) -> RawFd {
self.socket.into_raw_fd()
}
}