fix merge conflict
This commit is contained in:
commit
cd25dd1d0e
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
pub mod drivers;
|
||||
pub mod drivers; // Baremetal network drivers for various platforms
|
||||
pub use cidr;
|
||||
pub mod crypto;
|
Loading…
Reference in New Issue