#![allow(dead_code)]
use std::{
io::{self, Read, Write},
net::IpAddr,
ops::{Deref, DerefMut},
pin::Pin,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub enum Layer {
L2,
#[default]
L3,
}
#[derive(Clone, Default, Debug)]
pub struct Configuration;
impl Configuration {
pub fn tun_name<S: AsRef<str>>(&mut self, _tun_name: S) -> &mut Self {
self
}
pub fn address(&mut self, _value: IpAddr) -> &mut Self {
self
}
pub fn destination(&mut self, _value: IpAddr) -> &mut Self {
self
}
pub fn broadcast(&mut self, _value: IpAddr) -> &mut Self {
self
}
pub fn netmask(&mut self, _value: IpAddr) -> &mut Self {
self
}
pub fn mtu(&mut self, _value: u16) -> &mut Self {
self
}
pub fn up(&mut self) -> &mut Self {
self
}
pub fn down(&mut self) -> &mut Self {
self
}
pub fn layer(&mut self, _value: Layer) -> &mut Self {
self
}
#[cfg(unix)]
pub fn raw_fd(&mut self, _fd: ::std::os::fd::RawFd) -> &mut Self {
self
}
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("not implementated")]
NotImplemented,
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub type Result<T, E = Error> = ::std::result::Result<T, E>;
pub trait AbstractDevice: Read + Write {
fn configure(&mut self, _config: &Configuration) -> Result<()> {
Ok(())
}
fn tun_index(&self) -> Result<i32>;
fn tun_name(&self) -> Result<String>;
fn set_tun_name(&mut self, tun_name: &str) -> Result<()>;
fn enabled(&mut self, value: bool) -> Result<()>;
fn address(&self) -> Result<IpAddr>;
fn set_address(&mut self, value: IpAddr) -> Result<()>;
fn destination(&self) -> Result<IpAddr>;
fn set_destination(&mut self, value: IpAddr) -> Result<()>;
fn broadcast(&self) -> Result<IpAddr>;
fn set_broadcast(&mut self, value: IpAddr) -> Result<()>;
fn netmask(&self) -> Result<IpAddr>;
fn set_netmask(&mut self, value: IpAddr) -> Result<()>;
fn mtu(&self) -> Result<u16>;
fn set_mtu(&mut self, value: u16) -> Result<()>;
fn packet_information(&self) -> bool;
}
pub struct FakeQueue;
impl Read for FakeQueue {
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
}
impl Write for FakeQueue {
fn write(&mut self, _: &[u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
fn flush(&mut self) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
}
pub struct FakeDevice;
impl AbstractDevice for FakeDevice {
fn tun_name(&self) -> Result<String> {
Err(Error::NotImplemented)
}
fn tun_index(&self) -> Result<i32> {
Err(Error::NotImplemented)
}
fn set_tun_name(&mut self, _: &str) -> Result<()> {
Err(Error::NotImplemented)
}
fn enabled(&mut self, _: bool) -> Result<()> {
Err(Error::NotImplemented)
}
fn address(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_address(&mut self, _: IpAddr) -> Result<()> {
Err(Error::NotImplemented)
}
fn destination(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_destination(&mut self, _: IpAddr) -> Result<()> {
Err(Error::NotImplemented)
}
fn broadcast(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_broadcast(&mut self, _: IpAddr) -> Result<()> {
Err(Error::NotImplemented)
}
fn netmask(&self) -> Result<IpAddr> {
Err(Error::NotImplemented)
}
fn set_netmask(&mut self, _: IpAddr) -> Result<()> {
Err(Error::NotImplemented)
}
fn mtu(&self) -> Result<u16> {
Err(Error::NotImplemented)
}
fn set_mtu(&mut self, _: u16) -> Result<()> {
Err(Error::NotImplemented)
}
fn packet_information(&self) -> bool {
false
}
}
impl Read for FakeDevice {
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
}
impl Write for FakeDevice {
fn write(&mut self, _: &[u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
fn flush(&mut self) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
}
}
pub struct AsyncDevice(FakeDevice);
impl AsRef<FakeDevice> for AsyncDevice {
fn as_ref(&self) -> &FakeDevice {
&self.0
}
}
impl AsMut<FakeDevice> for AsyncDevice {
fn as_mut(&mut self) -> &mut FakeDevice {
&mut self.0
}
}
impl Deref for AsyncDevice {
type Target = FakeDevice;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for AsyncDevice {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsyncRead for AsyncDevice {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented")).into()
}
}
impl AsyncWrite for AsyncDevice {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &[u8],
) -> Poll<io::Result<usize>> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented")).into()
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented")).into()
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Err(io::Error::new(io::ErrorKind::Other, "not implemented")).into()
}
}
pub fn create_as_async(_: &Configuration) -> Result<AsyncDevice, Error> {
Err(Error::NotImplemented)
}