diff --git a/quicktap-cli/src/main.rs b/quicktap-cli/src/main.rs index e7a11a9..ab3699e 100644 --- a/quicktap-cli/src/main.rs +++ b/quicktap-cli/src/main.rs @@ -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) + } } diff --git a/quicktap/src/drivers/linux.rs b/quicktap/src/drivers/linux.rs index c0496f2..510f79a 100644 --- a/quicktap/src/drivers/linux.rs +++ b/quicktap/src/drivers/linux.rs @@ -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> { 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) -> Result<(), Box> { - todo!() + fn write(&mut self, packet: TunPacket) -> Result<(), Box> { + loop { + let written = self.device.write(&packet.packet)?; + if written == 0 { + break; + } + } + Ok(()) } } \ No newline at end of file diff --git a/quicktap/src/drivers/tungeneric.rs b/quicktap/src/drivers/tungeneric.rs index 60bd0cd..df9b209 100644 --- a/quicktap/src/drivers/tungeneric.rs +++ b/quicktap/src/drivers/tungeneric.rs @@ -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>; + fn new(config: &GenericInterface) -> Result> where Self: Sized; fn read(&mut self) -> Result>; fn clear(&mut self); @@ -16,7 +17,7 @@ pub trait GenericDriver { } pub struct GenericInterface { - pub address: IpInet, + pub addresses: Vec, pub mtu: Option, pub name: String } \ No newline at end of file diff --git a/quicktap/src/lib.rs b/quicktap/src/lib.rs index a41b036..0ef9924 100644 --- a/quicktap/src/lib.rs +++ b/quicktap/src/lib.rs @@ -1,2 +1,3 @@ -pub mod drivers; -pub mod crypto; \ No newline at end of file +pub mod drivers; // Baremetal network drivers for various platforms +pub use cidr; +pub mod crypto;