Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
change the type of mtu to u16 (#38)
Browse files Browse the repository at this point in the history
* change the type of mtu to u16
  • Loading branch information
xmh0511 authored Feb 1, 2024
1 parent 847b641 commit f34d8fb
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main_entry(
});

let mut dev = tun2::create_as_async(&config)?;
let size = dev.as_ref().mtu()? + tun2::PACKET_INFORMATION_LENGTH;
let size = dev.as_ref().mtu()? as usize + tun2::PACKET_INFORMATION_LENGTH;
let mut buf = vec![0; size];
loop {
tokio::select! {
Expand Down
4 changes: 2 additions & 2 deletions src/async/unix_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl AsyncDevice {
/// Consumes this AsyncDevice and return a Framed object (unified Stream and Sink interface)
pub fn into_framed(self) -> Framed<Self, TunPacketCodec> {
let mtu = self.as_ref().mtu().unwrap_or(crate::DEFAULT_MTU);
let codec = TunPacketCodec::new(mtu);
let codec = TunPacketCodec::new(mtu as usize);
// associate mtu with the capacity of ReadBuf
Framed::with_capacity(self, codec, mtu)
Framed::with_capacity(self, codec, mtu as usize)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/async/win_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl AsyncDevice {
/// Consumes this AsyncDevice and return a Framed object (unified Stream and Sink interface)
pub fn into_framed(self) -> Framed<Self, TunPacketCodec> {
let mtu = self.as_ref().mtu().unwrap_or(crate::DEFAULT_MTU);
let codec = TunPacketCodec::new(mtu);
// guarantee to avoid the mtu of wintun may far away larger than the default provided capacity of RedBuff of Framed
Framed::with_capacity(self, codec, mtu)
let codec = TunPacketCodec::new(mtu as usize);
// guarantee to avoid the mtu of wintun may far away larger than the default provided capacity of ReadBuf of Framed
Framed::with_capacity(self, codec, mtu as usize)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Configuration {
pub(crate) destination: Option<IpAddr>,
pub(crate) broadcast: Option<IpAddr>,
pub(crate) netmask: Option<IpAddr>,
pub(crate) mtu: Option<usize>,
pub(crate) mtu: Option<u16>,
pub(crate) enabled: Option<bool>,
pub(crate) layer: Option<Layer>,
pub(crate) queues: Option<usize>,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Configuration {
/// Set the MTU.
///
/// [Note: mtu on the Windows platform is always 65535 due to wintun -- end note]
pub fn mtu(&mut self, value: usize) -> &mut Self {
pub fn mtu(&mut self, value: u16) -> &mut Self {
// mtu on windows platform is always 65535 due to wintun
if cfg!(target_family = "unix") {
self.mtu = Some(value);
Expand Down
4 changes: 2 additions & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ pub trait AbstractDevice: Read + Write {
fn set_netmask(&mut self, value: IpAddr) -> Result<()>;

/// Get the MTU.
fn mtu(&self) -> Result<usize>;
fn mtu(&self) -> Result<u16>;

/// Set the MTU.
///
/// [Note: This setting has no effect on the Windows platform due to the mtu of wintun is always 65535. --end note]
fn set_mtu(&mut self, value: usize) -> Result<()>;
fn set_mtu(&mut self, value: u16) -> Result<()>;

/// Return whether the underlying tun device on the platform has packet information
///
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum Error {
#[error("invalid queues number")]
InvalidQueuesNumber,

#[error("out of range integral type conversion attempted")]
TryFromIntError,

#[error(transparent)]
Io(#[from] std::io::Error),

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn configure() -> Configuration {
}

#[cfg(unix)]
pub const DEFAULT_MTU: usize = 1500;
pub const DEFAULT_MTU: u16 = 1500;
#[cfg(windows)]
pub const DEFAULT_MTU: usize = 0xFFFF; // 65535
pub const DEFAULT_MTU: u16 = 0xFFFF; // 65535

pub const PACKET_INFORMATION_LENGTH: usize = 4;
4 changes: 2 additions & 2 deletions src/platform/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ impl AbstractDevice for Device {
Ok(())
}

fn mtu(&self) -> Result<usize> {
fn mtu(&self) -> Result<u16> {
// TODO: must get the mtu from the underlying device driver
Ok(self.tun.mtu())
}

fn set_mtu(&mut self, value: usize) -> Result<()> {
fn set_mtu(&mut self, value: u16) -> Result<()> {
// TODO: must set the mtu to the underlying device driver
self.tun.set_mtu(value);
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/platform/ios/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ impl AbstractDevice for Device {
Ok(())
}

fn mtu(&self) -> Result<usize> {
fn mtu(&self) -> Result<u16> {
// TODO: must get the mtu from the underlying device driver
Ok(self.tun.mtu())
}

fn set_mtu(&mut self, value: usize) -> Result<()> {
fn set_mtu(&mut self, value: u16) -> Result<()> {
// TODO: must set the mtu to the underlying device driver
self.tun.set_mtu(value);
Ok(())
Expand Down
9 changes: 6 additions & 3 deletions src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,19 +380,22 @@ impl AbstractDevice for Device {
}
}

fn mtu(&self) -> Result<usize> {
fn mtu(&self) -> Result<u16> {
unsafe {
let mut req = self.request();

if siocgifmtu(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
}

Ok(req.ifr_ifru.ifru_mtu as usize)
req.ifr_ifru
.ifru_mtu
.try_into()
.map_err(|_| Error::TryFromIntError)
}
}

fn set_mtu(&mut self, value: usize) -> Result<()> {
fn set_mtu(&mut self, value: u16) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifr_ifru.ifru_mtu = value as i32;
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct PlatformConfig {
pub(crate) ensure_root_privileges: bool,
}

/// `packet_information` is default to be `false` and `ensure_root_privileges` is default to be `true`.
impl Default for PlatformConfig {
fn default() -> Self {
PlatformConfig {
Expand Down
9 changes: 6 additions & 3 deletions src/platform/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl AbstractDevice for Device {
}
}

fn mtu(&self) -> Result<usize> {
fn mtu(&self) -> Result<u16> {
let ctl = self.ctl.as_ref().ok_or(Error::InvalidConfig)?;
unsafe {
let mut req = self.request()?;
Expand All @@ -406,11 +406,14 @@ impl AbstractDevice for Device {
return Err(io::Error::last_os_error().into());
}

Ok(req.ifr_ifru.ifru_mtu as usize)
req.ifr_ifru
.ifru_mtu
.try_into()
.map_err(|_| Error::TryFromIntError)
}
}

fn set_mtu(&mut self, value: usize) -> Result<()> {
fn set_mtu(&mut self, value: u16) -> Result<()> {
let ctl = self.ctl.as_ref().ok_or(Error::InvalidConfig)?;
unsafe {
let mut req = self.request()?;
Expand Down
24 changes: 12 additions & 12 deletions src/platform/posix/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub struct Reader {
}

impl Reader {
pub(crate) fn set_mtu(&mut self, value: usize) {
self.buf.resize(value + self.offset, 0);
pub(crate) fn set_mtu(&mut self, value: u16) {
self.buf.resize(value as usize + self.offset, 0);
}
}

Expand Down Expand Up @@ -115,8 +115,8 @@ pub struct Writer {
}

impl Writer {
pub(crate) fn set_mtu(&mut self, value: usize) {
self.buf.resize(value + self.offset, 0);
pub(crate) fn set_mtu(&mut self, value: u16) {
self.buf.resize(value as usize + self.offset, 0);
}
}

Expand Down Expand Up @@ -164,22 +164,22 @@ pub struct Tun {
}

impl Tun {
pub(crate) fn new(fd: Fd, mtu: usize, packet_information: bool) -> Self {
pub(crate) fn new(fd: Fd, mtu: u16, packet_information: bool) -> Self {
let fd = Arc::new(fd);
let offset = if packet_information { PIL } else { 0 };
Self {
reader: Reader {
fd: fd.clone(),
offset,
buf: vec![0; mtu + offset],
buf: vec![0; mtu as usize + offset],
},
writer: Writer {
fd,
offset,
buf: vec![0; mtu + offset],
buf: vec![0; mtu as usize + offset],
},
info: TunInfo {
mtu,
mtu: mtu as usize,
packet_information,
},
}
Expand All @@ -189,14 +189,14 @@ impl Tun {
self.reader.fd.set_nonblock()
}

pub fn set_mtu(&mut self, value: usize) {
self.info.mtu = value;
pub fn set_mtu(&mut self, value: u16) {
self.info.mtu = value as usize;
self.reader.set_mtu(value);
self.writer.set_mtu(value);
}

pub fn mtu(&self) -> usize {
self.info.mtu
pub fn mtu(&self) -> u16 {
self.info.mtu as u16
}

pub fn packet_information(&self) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/windows/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::error::{Error, Result};
/// A TUN device using the wintun driver.
pub struct Device {
pub(crate) tun: Tun,
mtu: usize,
mtu: u16,
}

impl Device {
Expand Down Expand Up @@ -181,13 +181,13 @@ impl AbstractDevice for Device {
}

/// The return value is always `Ok(65535)` due to wintun
fn mtu(&self) -> Result<usize> {
fn mtu(&self) -> Result<u16> {
// Note: wintun mtu is always 65535
Ok(self.mtu)
}

/// This setting has no effect since the mtu of wintun is always 65535
fn set_mtu(&mut self, _: usize) -> Result<()> {
fn set_mtu(&mut self, _: u16) -> Result<()> {
// Note: no-op due to mtu of wintun is always 65535
Ok(())
}
Expand Down

0 comments on commit f34d8fb

Please sign in to comment.