#![allow(unused_variables)]
use crate::configuration::Configuration;
use crate::device::AbstractDevice;
use crate::error::{Error, Result};
use crate::platform::posix::{self, Fd, Tun};
use std::io::{Read, Write};
use std::net::IpAddr;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
pub struct Device {
pub(crate) tun: Tun,
pub(crate) address: Option<IpAddr>,
pub(crate) netmask: Option<IpAddr>,
}
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 tun = Fd::new(fd, close_fd_on_drop).map_err(|_| std::io::Error::last_os_error())?;
Device {
tun: Tun::new(tun, mtu, false),
address: config.address,
netmask: config.netmask,
}
};
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)
}
}
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()
}
}
impl AbstractDevice for Device {
fn tun_index(&self) -> Result<i32> {
Err(Error::NotImplemented)
}
fn tun_name(&self) -> Result<String> {
Ok("".to_string())
}
fn set_tun_name(&mut self, value: &str) -> Result<()> {
Err(Error::NotImplemented)
}
fn enabled(&mut self, value: bool) -> Result<()> {
Ok(())
}
fn address(&self) -> Result<IpAddr> {
self.address.ok_or(Error::NotImplemented)
}
fn set_address(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn destination(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_destination(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn broadcast(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_broadcast(&mut self, _value: IpAddr) -> Result<()> {
Ok(())
}
fn netmask(&self) -> Result<IpAddr> {
self.netmask.ok_or(Error::NotImplemented)
}
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()
}
}