Skip to content

Commit

Permalink
set_ignore_packet_info
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Aug 30, 2024
1 parent 3cd51b5 commit a68c8ab
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 152 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let mut cfg = tun_rs::Configuration::default();
#[cfg(target_os = "ios")]
cfg.platform_config(|p_cfg| {
p_cfg.packet_information(true);
});
// This is safe if the provided fd is valid
let tun = unsafe{tun_rs::AsyncDevice::from_raw_fd(fd)};
let mut buf = [0u8;1500];
Expand Down
6 changes: 2 additions & 4 deletions examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {
config.device_guid(9099482345783245345345_u128);
});

let dev = tun_rs::create_as_async(&config)?;
#[cfg(target_os = "macos")]
config.platform_config(|config| {
config.packet_information(false);
});
dev.set_ignore_packet_info(true);

let dev = tun_rs::create_as_async(&config)?;
let size = dev.mtu()? as usize + tun_rs::PACKET_INFORMATION_LENGTH;
let mut buf = vec![0; size];
loop {
Expand Down
9 changes: 3 additions & 6 deletions examples/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use packet::{builder::Builder, icmp, ip, Packet};
#[allow(unused_imports)]
use std::sync::{mpsc::Receiver, Arc};
use tun_rs::BoxError;
use tun_rs::{AbstractDevice, BoxError};

fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand Down Expand Up @@ -36,12 +36,9 @@ fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
// .destination((10, 0, 0, 1))
.up();

#[cfg(target_os = "macos")]
config.platform_config(|config| {
config.packet_information(false);
});

let dev = Arc::new(tun_rs::create(&config)?);
#[cfg(target_os = "macos")]
dev.set_ignore_packet_info(true);
let mut buf = [0; 4096];

#[cfg(feature = "experimental")]
Expand Down
10 changes: 7 additions & 3 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ pub trait AbstractDevice {
))]
fn set_mtu(&self, value: u16) -> Result<()>;

/// Return whether the underlying tun device on the platform has packet information
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios",))]
fn packet_information(&self) -> bool;
/// Ignore packet-information during reading and writing
#[cfg(any(target_os = "macos", target_os = "ios"))]
fn ignore_packet_info(&self) -> bool;

/// Ignore packet-information during reading and writing
#[cfg(any(target_os = "macos", target_os = "ios"))]
fn set_ignore_packet_info(&self, ign: bool);

/// Set mac address
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "freebsd",))]
Expand Down
4 changes: 1 addition & 3 deletions src/platform/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub struct 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),
}
Device { tun: Tun::new(tun) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/freebsd/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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),
tun: Tun::new(tun),
ctl: unsafe { ctl().unwrap() },
alias_lock: Mutex::new(()),
}
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Device {
};

Device {
tun: Tun::new(tun, false),
tun: Tun::new(tun),
ctl,
alias_lock: Mutex::new(()),
}
Expand Down
12 changes: 7 additions & 5 deletions src/platform/ios/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub struct 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, true),
}
Device { tun: Tun::new(tun) }
}
}

Expand All @@ -45,8 +43,12 @@ impl Device {
}

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

fn set_ignore_packet_info(&self, ign: bool) {
self.tun.set_ignore_packet_info(ign)
}
}

Expand Down
33 changes: 2 additions & 31 deletions src/platform/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,8 @@ use crate::configuration::Configuration;
use crate::error::Result;

/// iOS-only interface configuration.
#[derive(Copy, Clone, Debug)]
pub struct PlatformConfig {
/// switch of Enable/Disable packet information for network driver
pub(crate) packet_information: bool,
}

impl Default for PlatformConfig {
fn default() -> Self {
PlatformConfig {
packet_information: true, // default is true in iOS
}
}
}

impl PlatformConfig {
/// Enable or disable packet information, the first 4 bytes of
/// each packet delivered from/to iOS underlying API is a header with flags and protocol type when enabled.
///
/// - If get the fd from
/// ```Objective-C
/// int32_t tunFd = [[NEPacketTunnelProvider::packetFlow valueForKeyPath:@"socket.fileDescriptor"] intValue];
/// ```
/// there exist PI.
///
/// - But if get packet from `[NEPacketTunnelProvider::packetFlow readPacketsWithCompletionHandler:]`
/// and write packet via `[NEPacketTunnelProvider::packetFlow writePackets:withProtocols:]`, there is no PI.
pub fn packet_information(&mut self, value: bool) -> &mut Self {
self.packet_information = value;
self
}
}
#[derive(Copy, Clone, Debug, Default)]
pub struct PlatformConfig;

use super::Device as DeviceWrapper;
/// Create a TUN device with the given name.
Expand Down
10 changes: 2 additions & 8 deletions src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub struct 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),
}
Device { tun: Tun::new(tun) }
}
}

Expand Down Expand Up @@ -80,7 +78,7 @@ impl Device {
}

let device = Device {
tun: Tun::new(tun_fd, config.platform_config.packet_information),
tun: Tun::new(tun_fd),
};
configure(&device, config)?;
Ok(device)
Expand Down Expand Up @@ -345,10 +343,6 @@ impl AbstractDevice for Device {
}
}

fn packet_information(&self) -> bool {
self.tun.packet_information()
}

fn set_mac_address(&self, eth_addr: [u8; ETHER_ADDR_LEN as usize]) -> Result<()> {
unsafe {
let mut req = self.request()?;
Expand Down
12 changes: 8 additions & 4 deletions src/platform/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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),
tun: Tun::new(tun),
alias_lock: Mutex::new(()),
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Device {
let ctl = Some(posix::Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0), true)?);

Device {
tun: posix::Tun::new(tun, config.platform_config.packet_information),
tun: posix::Tun::new(tun),
alias_lock: Mutex::new(()),
}
};
Expand Down Expand Up @@ -474,8 +474,12 @@ impl AbstractDevice for Device {
Ok(())
}

fn packet_information(&self) -> bool {
self.tun.packet_information()
fn ignore_packet_info(&self) -> bool {
self.tun.ignore_packet_info()
}

fn set_ignore_packet_info(&self, ign: bool) {
self.tun.set_ignore_packet_info(ign)
}
}

Expand Down
36 changes: 2 additions & 34 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,8 @@ use crate::configuration::Configuration;
use crate::error::Result;

/// macOS-only interface configuration.
#[derive(Copy, Clone, Debug)]
pub struct PlatformConfig {
pub(crate) packet_information: bool,
}

impl Default for PlatformConfig {
fn default() -> Self {
PlatformConfig {
packet_information: true, // default is true in macOS
}
}
}

impl PlatformConfig {
/// Enable or disable packet information, the first 4 bytes of
/// each packet delivered from/to macOS underlying API is a header with flags and protocol type when enabled.
///
/// - If we open an `utun` device, there always exist PI.
///
/// - If we use `Network Extension` to build our App:
///
/// - If get the fd from
/// ```Objective-C
/// int32_t tunFd = [[NEPacketTunnelProvider::packetFlow valueForKeyPath:@"socket.fileDescriptor"] intValue];
/// ```
/// there exist PI.
///
/// - But if get packet from `[NEPacketTunnelProvider::packetFlow readPacketsWithCompletionHandler:]`
/// and write packet via `[NEPacketTunnelProvider::packetFlow writePackets:withProtocols:]`, there is no PI.
pub fn packet_information(&mut self, value: bool) -> &mut Self {
self.packet_information = value;
self
}
}
#[derive(Copy, Clone, Debug, Default)]
pub struct PlatformConfig;

use super::Device as DeviceWrapper;
/// Create a TUN device with the given name.
Expand Down
1 change: 1 addition & 0 deletions src/platform/posix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Fd {
Ok(amount as usize)
}

#[inline]
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let fd = self.as_raw_fd();
let amount = unsafe { libc::write(fd, buf.as_ptr() as *const _, buf.len()) };
Expand Down
Loading

0 comments on commit a68c8ab

Please sign in to comment.