fix merge conflict

This commit is contained in:
c0repwn3r 2022-12-12 09:30:45 -05:00
commit cd25dd1d0e
Signed by: core
GPG Key ID: FDBF740DADDCEECF
4 changed files with 40 additions and 37 deletions

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() { 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::error::Error;
use std::io; use std::io;
use std::io::Read; use std::io::{Read, Write};
use etherparse::IpHeader; use etherparse::IpHeader;
use tun::platform::Device; use tun::platform::Device;
use crate::drivers::tungeneric::{GenericDriver, GenericInterface, TunPacket}; 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 /// Create a new TunDevice with the provided generic interface configuration
fn new(config: &GenericInterface) -> Result<Self, Box<dyn Error>> { fn new(config: &GenericInterface) -> Result<Self, Box<dyn Error>> {
let mut device_config = tun::Configuration::default(); let mut device_config = tun::Configuration::default();
device_config.address(config.address.address())?;
device_config.netmask(config.address.mask())?; for address in &config.addresses {
device_config.name(&config.name)?; device_config.address(address.address()).netmask(address.mask());
if let Some(mtu) = config.mtu { }
device_config.mtu(mtu)?;
device_config.name(&config.name);
if let Some(mtu) = config.mtu {
device_config.mtu(mtu);
} }
//config.platform(|config| {
// config.packet_information(true);
//});
Ok(Self { Ok(Self {
device: tun::create(&device_config)?, device: tun::create(&device_config)?,
read_buf: [0u8; 4096], read_buf: [0u8; 4096],
@ -42,30 +42,13 @@ impl GenericDriver for TunDevice {
// Found a packet. Clear read buffers // Found a packet. Clear read buffers
self.read_offset = 0; self.read_offset = 0;
self.read_buf = [0u8; 4096]; self.read_buf = [0u8; 4096];
Ok(TunPacket { return Ok(TunPacket {
header: packet_header, header: packet_header,
packet: *self.packet_buf, 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,
}) })
} }
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) { fn clear(&mut self) {
@ -73,7 +56,13 @@ impl GenericDriver for TunDevice {
self.read_offset = 0; self.read_offset = 0;
} }
fn write(&mut self, packet: Box<TunPacket>) -> Result<(), Box<dyn Error>> { fn write(&mut self, packet: TunPacket) -> Result<(), Box<dyn Error>> {
todo!() 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 cidr::IpInet;
use etherparse::{IpHeader, SlicedPacket}; use etherparse::{IpHeader, SlicedPacket};
#[derive(Debug)]
pub struct TunPacket { pub struct TunPacket {
pub header: IpHeader, pub header: IpHeader,
pub packet: [u8] pub packet: [u8; 4096]
} }
pub trait GenericDriver { 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 read(&mut self) -> Result<TunPacket, Box<dyn Error>>;
fn clear(&mut self); fn clear(&mut self);
@ -16,7 +17,7 @@ pub trait GenericDriver {
} }
pub struct GenericInterface { pub struct GenericInterface {
pub address: IpInet, pub addresses: Vec<IpInet>,
pub mtu: Option<i32>, pub mtu: Option<i32>,
pub name: String pub name: String
} }

View File

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