Skip to content

Commit

Permalink
Merge branch 'v3' of https://github.com/xmh0511/rust-tun into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
xmh0511 committed Aug 29, 2024
2 parents c44d753 + e6ecdc6 commit 4e986d3
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 305 deletions.
21 changes: 0 additions & 21 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use crate::platform::PlatformConfig;
use crate::AbstractDevice;
#[allow(unused_imports)]
use std::net::IpAddr;
#[cfg(unix)]
use std::os::unix::io::RawFd;

cfg_if::cfg_if! {
if #[cfg(windows)] {
Expand Down Expand Up @@ -94,16 +92,12 @@ pub struct Configuration {
pub(crate) enabled: Option<bool>,
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "freebsd",))]
pub(crate) layer: Option<Layer>,
#[cfg(unix)]
pub(crate) raw_fd: Option<RawFd>,
#[cfg(windows)]
pub(crate) raw_handle: Option<WinHandle>,
#[cfg(windows)]
pub(crate) ring_capacity: Option<u32>,
#[cfg(windows)]
pub(crate) metric: Option<u16>,
#[cfg(unix)]
pub(crate) close_fd_on_drop: Option<bool>,
}

impl Configuration {
Expand Down Expand Up @@ -211,12 +205,6 @@ impl Configuration {
self
}

/// Set the raw fd.
#[cfg(unix)]
pub fn raw_fd(&mut self, fd: RawFd) -> &mut Self {
self.raw_fd = Some(fd);
self
}
#[cfg(windows)]
pub fn raw_handle(&mut self, handle: std::os::windows::raw::HANDLE) -> &mut Self {
self.raw_handle = Some(WinHandle(handle));
Expand All @@ -232,15 +220,6 @@ impl Configuration {
self.metric = Some(metric);
self
}
/// Set whether to close the received raw file descriptor on drop or not.
/// The default behaviour is to close the received or tun2 generated file descriptor.
/// Note: If this is set to false, it is up to the caller to ensure the
/// file descriptor that they pass via [Configuration::raw_fd] is properly closed.
#[cfg(unix)]
pub fn close_fd_on_drop(&mut self, value: bool) -> &mut Self {
self.close_fd_on_drop = Some(value);
self
}
}

/// Reconfigure the device.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub use crate::configuration::{Configuration, Layer};

pub mod platform;
pub use crate::platform::create;
#[cfg(unix)]
pub use crate::platform::create_with_fd;

#[cfg(feature = "async")]
pub mod r#async;
Expand Down
36 changes: 9 additions & 27 deletions src/platform/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,25 @@
// 0. You just DO WHAT THE FUCK YOU WANT TO.
#![allow(unused_variables)]

use std::os::fd::FromRawFd;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};

use crate::configuration::Configuration;
use crate::device::AbstractDevice;
use crate::error::{Error, Result};
use crate::platform::posix::{Fd, Tun};

/// A TUN device for Android.
pub struct Device {
tun: Tun,
}

impl Device {
/// Create a new `Device` for the given `Configuration`.
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 tun = Fd::new(fd, close_fd_on_drop).map_err(|_| std::io::Error::last_os_error())?;

Device {
tun: Tun::new(tun, false),
}
};

Ok(device)
impl FromRawFd for Device {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let tun = Fd::new(fd, true).unwrap();
Device {
tun: Tun::new(tun, false),
}
}
}

impl Device {
/// Set non-blocking mode
pub fn set_nonblock(&self) -> std::io::Result<()> {
self.tun.set_nonblock()
Expand All @@ -65,12 +53,6 @@ impl Device {
}
}

impl AbstractDevice for Device {
// fn packet_information(&self) -> bool {
// self.tun.packet_information()
// }
}

impl AsRawFd for Device {
fn as_raw_fd(&self) -> RawFd {
self.tun.as_raw_fd()
Expand Down
11 changes: 9 additions & 2 deletions src/platform/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
//! Android specific functionality.

mod device;

pub use self::device::Device;
use std::os::fd::{FromRawFd, RawFd};

use crate::configuration::Configuration;
use crate::error::Result;
Expand All @@ -26,6 +28,11 @@ pub struct PlatformConfig;

use super::Device as DeviceWrapper;
/// Create a TUN device with the given name.
pub fn create(configuration: &Configuration) -> Result<DeviceWrapper> {
Ok(DeviceWrapper(Device::new(configuration)?))
pub fn create(_configuration: &Configuration) -> Result<DeviceWrapper> {
unimplemented!()
}
/// # Safety
/// The fd passed in must be an owned file descriptor; in particular, it must be open.
pub unsafe fn create_with_fd(fd: RawFd) -> Result<DeviceWrapper> {
Ok(DeviceWrapper(Device::from_raw_fd(fd)))
}
36 changes: 25 additions & 11 deletions src/platform/freebsd/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

use crate::{
configuration::{Configuration, Layer},
device::AbstractDevice,
error::{Error, Result},
platform::freebsd::sys::*,
platform::posix::{self, sockaddr_union, Fd, Tun},
IntoAddress,
};
use libc::{
self, c_char, c_short, fcntl, ifreq, kinfo_file, AF_INET, F_KINFO, IFF_RUNNING, IFF_UP,
IFNAMSIZ, KINFO_FILE_SIZE, O_RDWR, SOCK_DGRAM,
};
use std::os::fd::FromRawFd;
use std::{
// ffi::{CStr, CString},
io,
Expand All @@ -26,15 +35,6 @@ use std::{
sync::{Mutex, RwLock},
};

use crate::{
configuration::{Configuration, Layer},
device::AbstractDevice,
error::{Error, Result},
platform::freebsd::sys::*,
platform::posix::{self, sockaddr_union, Fd, Tun},
IntoAddress,
};

#[derive(Clone, Copy, Debug)]
struct Route {
addr: IpAddr,
Expand All @@ -49,14 +49,28 @@ pub struct Device {
alias_lock: Mutex<()>,
}

impl FromRawFd for Device {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let tun = Fd::new(fd, true).unwrap();
Device {
tun_name: Default::default(),
tun: Tun::new(tun, false),
ctl: unsafe { ctl().unwrap() },
alias_lock: Mutex::new(()),
}
}
}
unsafe fn ctl() -> io::Result<Fd> {
Ok(Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0), true)?)
}
impl Device {
/// Create a new `Device` for the given `Configuration`.
pub fn new(config: &Configuration) -> Result<Self> {
let layer = config.layer.unwrap_or(Layer::L3);
let device_prefix = if layer == Layer::L3 {
format!("tun")
"tun".to_string()
} else {
format!("tap")
"tap".to_string()
};
let device = unsafe {
let dev_index = match config.name.as_ref() {
Expand Down
73 changes: 40 additions & 33 deletions src/platform/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

//! Linux specific functionality.

pub mod sys;

mod device;
pub use self::device::Device;

use crate::configuration::Configuration;
use crate::error::Result;

/// FreeBSD-only interface configuration.
#[derive(Copy, Clone, Default, Debug)]
pub struct PlatformConfig;

use super::Device as DeviceWrapper;
/// Create a TUN device with the given name.
pub fn create(configuration: &Configuration) -> Result<DeviceWrapper> {
Ok(DeviceWrapper(Device::new(configuration)?))
}
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

//! Linux specific functionality.

pub mod sys;

mod device;

pub use self::device::Device;
use std::os::fd::{FromRawFd, RawFd};

use crate::configuration::Configuration;
use crate::error::Result;

/// FreeBSD-only interface configuration.
#[derive(Copy, Clone, Default, Debug)]
pub struct PlatformConfig;

use super::Device as DeviceWrapper;
/// Create a TUN device with the given name.
pub fn create(configuration: &Configuration) -> Result<DeviceWrapper> {
Ok(DeviceWrapper(Device::new(configuration)?))
}
/// # Safety
/// The fd passed in must be an owned file descriptor; in particular, it must be open.
pub unsafe fn create_with_fd(fd: RawFd) -> Result<DeviceWrapper> {
Ok(DeviceWrapper(Device::from_raw_fd(fd)))
}
38 changes: 9 additions & 29 deletions src/platform/ios/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,29 @@
// 0. You just DO WHAT THE FUCK YOU WANT TO.
#![allow(unused_variables)]

use std::os::fd::FromRawFd;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};

use crate::{
configuration::Configuration,
device::AbstractDevice,
error::{Error, Result},
platform::posix::{Fd, Tun},
};
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};

/// A TUN device for iOS.
pub struct Device {
tun: Tun,
}

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 FromRawFd for Device {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let tun = Fd::new(fd, true).unwrap();
Device {
tun: Tun::new(tun, true),
}
}
}

impl Device {
/// Create a new `Device` for the given `Configuration`.
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 fd = Fd::new(fd, close_fd_on_drop).map_err(|_| std::io::Error::last_os_error())?;
Device {
tun: Tun::new(fd, config.platform_config.packet_information),
}
};

Ok(device)
}

/// Set non-blocking mode
pub fn set_nonblock(&self) -> std::io::Result<()> {
self.tun.set_nonblock()
Expand Down
Loading

0 comments on commit 4e986d3

Please sign in to comment.