|
| 1 | +// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |
| 2 | +// Version 2, December 2004 |
| 3 | +// |
| 4 | +// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co |
| 5 | +// |
| 6 | +// Everyone is permitted to copy and distribute verbatim or modified |
| 7 | +// copies of this license document, and changing it is allowed as long |
| 8 | +// as the name is changed. |
| 9 | +// |
| 10 | +// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |
| 11 | +// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION |
| 12 | +// |
| 13 | +// 0. You just DO WHAT THE FUCK YOU WANT TO. |
| 14 | +#![allow(unused_variables)] |
| 15 | + |
| 16 | +use std::io::{Read, Write}; |
| 17 | +use std::net::IpAddr; |
| 18 | +use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; |
| 19 | + |
| 20 | +use crate::configuration::Configuration; |
| 21 | +use crate::device::AbstractDevice; |
| 22 | +use crate::error::{Error, Result}; |
| 23 | +use crate::platform::posix::{self, Fd, Tun}; |
| 24 | + |
| 25 | +/// A TUN device for OpenHarmony. |
| 26 | +pub struct Device { |
| 27 | + pub(crate) tun: Tun, |
| 28 | +} |
| 29 | + |
| 30 | +impl AsRef<dyn AbstractDevice + 'static> for Device { |
| 31 | + fn as_ref(&self) -> &(dyn AbstractDevice + 'static) { |
| 32 | + self |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl AsMut<dyn AbstractDevice + 'static> for Device { |
| 37 | + fn as_mut(&mut self) -> &mut (dyn AbstractDevice + 'static) { |
| 38 | + self |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Device { |
| 43 | + /// Create a new `Device` for the given `Configuration`. |
| 44 | + pub fn new(config: &Configuration) -> Result<Self> { |
| 45 | + let close_fd_on_drop = config.close_fd_on_drop.unwrap_or(true); |
| 46 | + let fd = match config.raw_fd { |
| 47 | + Some(raw_fd) => raw_fd, |
| 48 | + _ => return Err(Error::InvalidConfig), |
| 49 | + }; |
| 50 | + let device = { |
| 51 | + let mtu = config.mtu.unwrap_or(crate::DEFAULT_MTU); |
| 52 | + let tun = Fd::new(fd, close_fd_on_drop).map_err(|_| std::io::Error::last_os_error())?; |
| 53 | + |
| 54 | + Device { |
| 55 | + tun: Tun::new(tun, mtu, false), |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + Ok(device) |
| 60 | + } |
| 61 | + |
| 62 | + /// Split the interface into a `Reader` and `Writer`. |
| 63 | + pub fn split(self) -> (posix::Reader, posix::Writer) { |
| 64 | + (self.tun.reader, self.tun.writer) |
| 65 | + } |
| 66 | + |
| 67 | + /// Set non-blocking mode |
| 68 | + pub fn set_nonblock(&self) -> std::io::Result<()> { |
| 69 | + self.tun.set_nonblock() |
| 70 | + } |
| 71 | + |
| 72 | + /// Recv a packet from tun device |
| 73 | + pub fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 74 | + self.tun.recv(buf) |
| 75 | + } |
| 76 | + |
| 77 | + /// Send a packet to tun device |
| 78 | + pub fn send(&self, buf: &[u8]) -> std::io::Result<usize> { |
| 79 | + self.tun.send(buf) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Read for Device { |
| 84 | + fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 85 | + self.tun.read(buf) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Write for Device { |
| 90 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 91 | + self.tun.write(buf) |
| 92 | + } |
| 93 | + |
| 94 | + fn flush(&mut self) -> std::io::Result<()> { |
| 95 | + self.tun.flush() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl AbstractDevice for Device { |
| 100 | + fn tun_name(&self) -> Result<String> { |
| 101 | + Ok("vpn-tun".to_string()) |
| 102 | + } |
| 103 | + |
| 104 | + fn set_tun_name(&mut self, value: &str) -> Result<()> { |
| 105 | + Err(Error::NotImplemented) |
| 106 | + } |
| 107 | + |
| 108 | + fn enabled(&mut self, value: bool) -> Result<()> { |
| 109 | + Ok(()) |
| 110 | + } |
| 111 | + |
| 112 | + fn address(&self) -> Result<IpAddr> { |
| 113 | + Err(Error::NotImplemented) |
| 114 | + } |
| 115 | + |
| 116 | + fn set_address(&mut self, _value: IpAddr) -> Result<()> { |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | + |
| 120 | + fn destination(&self) -> Result<IpAddr> { |
| 121 | + Err(Error::NotImplemented) |
| 122 | + } |
| 123 | + |
| 124 | + fn set_destination(&mut self, _value: IpAddr) -> Result<()> { |
| 125 | + Ok(()) |
| 126 | + } |
| 127 | + |
| 128 | + fn broadcast(&self) -> Result<IpAddr> { |
| 129 | + Err(Error::NotImplemented) |
| 130 | + } |
| 131 | + |
| 132 | + fn set_broadcast(&mut self, _value: IpAddr) -> Result<()> { |
| 133 | + Ok(()) |
| 134 | + } |
| 135 | + |
| 136 | + fn netmask(&self) -> Result<IpAddr> { |
| 137 | + Err(Error::NotImplemented) |
| 138 | + } |
| 139 | + |
| 140 | + fn set_netmask(&mut self, _value: IpAddr) -> Result<()> { |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + fn mtu(&self) -> Result<u16> { |
| 145 | + // TODO: must get the mtu from the underlying device driver |
| 146 | + Ok(self.tun.mtu()) |
| 147 | + } |
| 148 | + |
| 149 | + fn set_mtu(&mut self, value: u16) -> Result<()> { |
| 150 | + // TODO: must set the mtu to the underlying device driver |
| 151 | + self.tun.set_mtu(value); |
| 152 | + Ok(()) |
| 153 | + } |
| 154 | + |
| 155 | + fn packet_information(&self) -> bool { |
| 156 | + self.tun.packet_information() |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl AsRawFd for Device { |
| 161 | + fn as_raw_fd(&self) -> RawFd { |
| 162 | + self.tun.as_raw_fd() |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl IntoRawFd for Device { |
| 167 | + fn into_raw_fd(self) -> RawFd { |
| 168 | + self.tun.into_raw_fd() |
| 169 | + } |
| 170 | +} |
0 commit comments