Skip to content

Commit 1237883

Browse files
authored
Merge pull request #2 from FrankHan052176/v2
support ohos
2 parents e3bec10 + c47184c commit 1237883

File tree

3 files changed

+209
-2
lines changed

3 files changed

+209
-2
lines changed

src/platform/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
#[cfg(unix)]
1818
pub mod posix;
1919

20-
#[cfg(target_os = "linux")]
20+
#[cfg(target_env = "ohos")]
21+
mod ohos;
22+
#[cfg(target_env = "ohos")]
23+
pub use self::ohos::{create, Device, PlatformConfig};
24+
25+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
2126
pub mod linux;
22-
#[cfg(target_os = "linux")]
27+
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
2328
pub use self::linux::{create, Device, PlatformConfig};
2429

2530
#[cfg(target_os = "freebsd")]
@@ -47,6 +52,7 @@ pub use crate::platform::posix::Tun;
4752

4853
#[cfg(target_os = "windows")]
4954
pub mod windows;
55+
5056
#[cfg(target_os = "windows")]
5157
pub use self::windows::{create, Device, PlatformConfig, Tun};
5258

src/platform/ohos/device.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}

src/platform/ohos/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
15+
//! OpenHarmony specific functionality.
16+
#![allow(unused_variables)]
17+
#![allow(dead_code)]
18+
mod device;
19+
pub use self::device::Device;
20+
21+
use crate::configuration::Configuration;
22+
use crate::error::Result;
23+
24+
/// OpenHarmony-only interface configuration.
25+
#[derive(Copy, Clone, Default, Debug)]
26+
pub struct PlatformConfig;
27+
28+
/// Create a TUN device with the given name.
29+
pub fn create(configuration: &Configuration) -> Result<Device> {
30+
Device::new(configuration)
31+
}

0 commit comments

Comments
 (0)