#![allow(unused_variables)]
use crate::{
configuration::Configuration,
device::AbstractDevice,
error::{Error, Result},
platform::posix::{self, Fd, Tun},
};
use std::{
io::{Read, Write},
net::IpAddr,
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
};
pub struct Device {
pub(crate) tun: Tun,
pub(crate) address: Option<IpAddr>,
pub(crate) netmask: Option<IpAddr>,
pub(crate) tun_name: Option<String>,
}
impl AsRef<dyn AbstractDevice + 'static> for Device {
fn as_ref(&self) -> &(dyn AbstractDevice + 'static) {
self
}
}
impl AsMut<dyn AbstractDevice + 'static> for Device {
fn as_mut(&mut self) -> &mut (dyn AbstractDevice + 'static) {
self
}
}
impl Device {
pub fn new(config: &Configuration) -> Result<Self> {
let close_fd_on_drop = config.close_fd_on_drop.unwrap_or(true);
let fd = match config.raw_fd {
Some(raw_fd) => raw_fd,
_ => return Err(Error::InvalidConfig),
};
let device = {
let mtu = config.mtu.unwrap_or(crate::DEFAULT_MTU);
let fd = Fd::new(fd, close_fd_on_drop).map_err(|_| std::io::Error::last_os_error())?;
Device {
tun: Tun::new(fd, mtu, config.platform_config.packet_information),
address: config.address,
netmask: config.netmask,
tun_name: config.tun_name.clone(),
}
};
Ok(device)
}
pub fn split(self) -> (posix::Reader, posix::Writer) {
(self.tun.reader, self.tun.writer)
}
#[allow(dead_code)]
pub(crate) fn set_nonblock(&self) -> std::io::Result<()> {
self.tun.set_nonblock()
}
pub fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
self.tun.recv(buf)
}
pub fn send(&self, buf: &[u8]) -> std::io::Result<usize> {
self.tun.send(buf)
}
}
impl Read for Device {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.tun.read(buf)
}
fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> {
self.tun.read_vectored(bufs)
}
}
impl Write for Device {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.tun.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.tun.flush()
}
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
self.tun.write_vectored(bufs)
}
}
impl AbstractDevice for Device {
fn tun_index(&self) -> Result<i32> {
Err("no tun_index".into())
}
fn tun_name(&self) -> Result<String> {
Ok(self.tun_name.clone().unwrap_or("".to_string()))
}
fn set_tun_name(&mut self, value: &str) -> Result<()> {
Err("set_tun_name".into())
}
fn enabled(&mut self, value: bool) -> Result<()> {
Ok(())
}
fn address(&self) -> Result<IpAddr> {
self.address.ok_or("no address".into())
}
fn set_address(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn destination(&self) -> Result<IpAddr> {
Err("no destination".into())
}
fn set_destination(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn broadcast(&self) -> Result<IpAddr> {
Err("no broadcast".into())
}
fn set_broadcast(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn netmask(&self) -> Result<IpAddr> {
self.netmask.ok_or("no netmask".into())
}
fn set_netmask(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn mtu(&self) -> Result<u16> {
Ok(self.tun.mtu())
}
fn set_mtu(&mut self, value: u16) -> Result<()> {
self.tun.set_mtu(value);
Ok(())
}
fn packet_information(&self) -> bool {
self.tun.packet_information()
}
}
impl AsRawFd for Device {
fn as_raw_fd(&self) -> RawFd {
self.tun.as_raw_fd()
}
}
impl IntoRawFd for Device {
fn into_raw_fd(self) -> RawFd {
self.tun.into_raw_fd()
}
}