fix some bugz

This commit is contained in:
core 2022-12-12 09:30:40 -05:00
parent bd9e953352
commit f8efcc3d26
Signed by: core
GPG Key ID: FDBF740DADDCEECF
5 changed files with 39 additions and 37 deletions

View File

@ -2,6 +2,5 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/wginterface" vcs="Git" />
</component>
</project>

View File

@ -1,3 +1,15 @@
use std::net::{IpAddr, Ipv4Addr};
use quicktap::cidr::{IpInet, Ipv4Inet};
use quicktap::drivers::tun::TunDevice;
use quicktap::drivers::tungeneric::{GenericDriver, GenericInterface};
fn main() {
println!("Hello, world!");
let mut device = TunDevice::new(&GenericInterface {
addresses: vec![IpInet::V4(Ipv4Inet::new(Ipv4Addr::new(10, 19, 2, 2), 16).unwrap())],
mtu: None,
name: "ela1".to_string(),
}).unwrap();
loop {
println!("{:?}", device.read().unwrap().header)
}
}

View File

@ -1,6 +1,6 @@
use std::error::Error;
use std::io;
use std::io::Read;
use std::io::{Read, Write};
use etherparse::IpHeader;
use tun::platform::Device;
use crate::drivers::tungeneric::{GenericDriver, GenericInterface, TunPacket};
@ -15,15 +15,15 @@ impl GenericDriver for TunDevice {
/// Create a new TunDevice with the provided generic interface configuration
fn new(config: &GenericInterface) -> Result<Self, Box<dyn Error>> {
let mut device_config = tun::Configuration::default();
device_config.address(config.address.address())?;
device_config.netmask(config.address.mask())?;
device_config.name(&config.name)?;
if let Some(mtu) = config.mtu {
device_config.mtu(mtu)?;
for address in &config.addresses {
device_config.address(address.address()).netmask(address.mask());
}
device_config.name(&config.name);
if let Some(mtu) = config.mtu {
device_config.mtu(mtu);
}
//config.platform(|config| {
// config.packet_information(true);
//});
Ok(Self {
device: tun::create(&device_config)?,
read_buf: [0u8; 4096],
@ -42,30 +42,13 @@ impl GenericDriver for TunDevice {
// Found a packet. Clear read buffers
self.read_offset = 0;
self.read_buf = [0u8; 4096];
Ok(TunPacket {
return Ok(TunPacket {
header: packet_header,
packet: *self.packet_buf,
})
}
if &self.read_offset + read_amt > 4096 {
// packet is too big
io::Error::new(io::ErrorKind::InvalidData, "Too much non-packet data recieved on network")?;
}
self.read_buf[&self.read_offset..] = &self.packet_buf;
&self.read_offset += read_amt;
let ip_header = IpHeader::from_slice(&self.read_buf);
if let Ok((packet_header, _, _)) = ip_header {
// Found a packet. Clear read buffers
self.read_offset = 0;
self.read_buf = [0u8; 4096];
Ok(TunPacket {
header: packet_header,
packet: *self.packet_buf,
packet: self.packet_buf,
})
}
io::Error::new(io::ErrorKind::WouldBlock, "No packets detected yet")?
Err(io::Error::new(io::ErrorKind::WouldBlock, "No packets detected yet").into())
}
fn clear(&mut self) {
@ -73,7 +56,13 @@ impl GenericDriver for TunDevice {
self.read_offset = 0;
}
fn write(&mut self, packet: Box<TunPacket>) -> Result<(), Box<dyn Error>> {
todo!()
fn write(&mut self, packet: TunPacket) -> Result<(), Box<dyn Error>> {
loop {
let written = self.device.write(&packet.packet)?;
if written == 0 {
break;
}
}
Ok(())
}
}

View File

@ -2,13 +2,14 @@ use std::error::Error;
use cidr::IpInet;
use etherparse::{IpHeader, SlicedPacket};
#[derive(Debug)]
pub struct TunPacket {
pub header: IpHeader,
pub packet: [u8]
pub packet: [u8; 4096]
}
pub trait GenericDriver {
fn new(config: &GenericInterface) -> Result<Self, Box<dyn Error>>;
fn new(config: &GenericInterface) -> Result<Self, Box<dyn Error>> where Self: Sized;
fn read(&mut self) -> Result<TunPacket, Box<dyn Error>>;
fn clear(&mut self);
@ -16,7 +17,7 @@ pub trait GenericDriver {
}
pub struct GenericInterface {
pub address: IpInet,
pub addresses: Vec<IpInet>,
pub mtu: Option<i32>,
pub name: String
}

View File

@ -1 +1,2 @@
pub mod drivers; // Baremetal network drivers for various platforms
pub mod drivers; // Baremetal network drivers for various platforms
pub use cidr;