Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial CI breaks #2

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/full_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
matrix:
toolchain:
- stable
- 1.66.0
- 1.74.0
steps:
- uses: actions/checkout@v4
- name: Setup Rust toolchain
Expand All @@ -45,7 +45,7 @@ jobs:
matrix:
target:
- { toolchain: stable, os: macos-14 }
- { toolchain: 1.66.0, os: macos-14 }
- { toolchain: 1.74.0, os: macos-14 }
runs-on: ${{ matrix.target.os }}
steps:
- uses: actions/checkout@v4
Expand All @@ -63,7 +63,7 @@ jobs:
fail-fast: true
matrix:
toolchain:
- 1.66.0
- 1.74.0
- stable
target:
- x86_64-pc-windows-msvc
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
name = "tappers"
authors = ["Nathaniel Bennett <me[at]nathanielbennett[dotcom]>"]
description = "Cross-platform TUN and TAP interfaces"
rust-version = "1.66" # `std::os::fd` stabilized
# 1.66 - `std::os::fd` stabilized
# 1.74 - `OsStr::as_encoded_bytes` stabilized
rust-version = "1.74"
version = "0.1.0"
license = "MIT OR Apache-2.0"
edition = "2021"
Expand All @@ -21,7 +23,7 @@ wintun-runtime = ["wintun"]
# Enables fallible run-time loading of tap-windows6 (default is load-time)
tapwin6-runtime = ["tapwin6"]

[target.'cfg(unix)'.dependencies]
[dependencies]
libc = { version = "0.2" }

[target.'cfg(windows)'.dependencies]
Expand Down
27 changes: 21 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ pub mod tapwin6;
#[cfg(all(target_os = "windows", feature = "wintun"))]
pub mod wintun;

#[cfg(not(target_os = "windows"))]
mod tap;
#[cfg(any(
not(target_os = "windows"),
all(target_os = "windows", feature = "wintun")
))]
mod tun;

#[cfg(not(target_os = "windows"))]
pub use tap::Tap;
#[cfg(any(
not(target_os = "windows"),
all(target_os = "windows", feature = "wintun")
))]
pub use tun::Tun;

use std::ffi::{CStr, OsStr, OsString};
#[cfg(not(target_os = "windows"))]
use std::ffi::CStr;
use std::ffi::{OsStr, OsString};
use std::fmt::{Debug, Display};
#[cfg(not(target_os = "windows"))]
use std::mem;
#[cfg(target_os = "windows")]
use std::ptr;
use std::str::FromStr;
use std::{array, io, mem};
use std::{array, io};

#[cfg(target_os = "windows")]
use windows_sys::Win32::Foundation::{ERROR_DEV_NOT_EXIST, ERROR_NO_DATA};
Expand Down Expand Up @@ -80,12 +96,11 @@ impl Interface {
pub const MAX_INTERFACE_NAME_LEN: usize = INTERNAL_MAX_INTERFACE_NAME_LEN;

/// A special catch-all interface identifier that specifies all operational interfaces.
#[cfg(not(target_os = "windows"))]
pub fn any() -> io::Result<Self> {
#[cfg(not(target_os = "windows"))]
let name = [0; Self::MAX_INTERFACE_NAME_LEN + 1];

// Leave the interface name blank since this is the catch-all identifier

Ok(Self {
name,
is_catchall: true,
Expand Down Expand Up @@ -115,7 +130,7 @@ impl Interface {
#[cfg(target_os = "windows")]
#[inline]
fn new_inner(if_name: &impl AsRef<OsStr>) -> io::Result<Self> {
let utf16 = if_name.as_ref().encode_wide();
let mut utf16 = if_name.as_ref().encode_wide();
let name = array::from_fn(|_| utf16.next().unwrap_or(0));

let interface = Interface {
Expand All @@ -129,6 +144,7 @@ impl Interface {
#[cfg(not(target_os = "windows"))]
#[inline]
fn new_inner(if_name: &impl AsRef<OsStr>) -> io::Result<Self> {
// Note: `as_encoded_bytes()` is the only think keeping MSRV as high as 1.74
Self::new_raw(if_name.as_ref().as_encoded_bytes())
}

Expand Down Expand Up @@ -182,7 +198,6 @@ impl Interface {
}

/// Retrieves the associated index of the network interface.
#[cfg(not(target_os = "windows"))]
#[inline]
pub fn index(&self) -> io::Result<u32> {
self.index_impl()
Expand Down
8 changes: 4 additions & 4 deletions src/linux/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,19 @@ impl Tun {
}

/// Reads a single packet from the TUN device.
pub fn recv(&self, data: &mut [u8]) -> io::Result<usize> {
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
unsafe {
match libc::read(self.fd, data.as_mut_ptr() as *mut libc::c_void, data.len()) {
match libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) {
r @ 0.. => Ok(r as usize),
_ => Err(io::Error::last_os_error()),
}
}
}

/// Writes a single packet to the TUN device.
pub fn send(&self, data: &[u8]) -> io::Result<usize> {
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
unsafe {
match libc::write(self.fd, data.as_ptr() as *const libc::c_void, data.len()) {
match libc::write(self.fd, buf.as_ptr() as *const libc::c_void, buf.len()) {
r @ 0.. => Ok(r as usize),
_ => Err(io::Error::last_os_error()),
}
Expand Down
68 changes: 67 additions & 1 deletion src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,70 @@ impl TunImpl {
}
}

pub(crate) struct TapImpl {}
pub(crate) struct TapImpl {
tap: FethTap,
}

impl TapImpl {
/// Creates a new, unique TUN device.
#[inline]
pub fn new() -> io::Result<Self> {
Ok(Self {
tap: FethTap::new()?,
})
}

/// Opens or creates a TUN device of the given name.
#[inline]
pub fn new_named(if_name: Interface) -> io::Result<Self> {
Ok(Self {
tap: FethTap::new_named(Some(if_name), None)?,
})
}

/// Retrieves the interface name of the TUN device.
#[inline]
pub fn name(&self) -> io::Result<Interface> {
self.tap.name()
}

#[inline]
pub fn set_state(&mut self, state: DeviceState) -> io::Result<()> {
self.tap.set_state(state)
}

#[inline]
pub fn set_up(&mut self) -> io::Result<()> {
self.tap.set_state(DeviceState::Up)
}

#[inline]
pub fn set_down(&mut self) -> io::Result<()> {
self.tap.set_state(DeviceState::Down)
}

#[inline]
pub fn mtu(&self) -> io::Result<usize> {
self.tap.mtu()
}

#[inline]
pub fn set_nonblocking(&mut self, nonblocking: bool) -> io::Result<()> {
self.tap.set_nonblocking(nonblocking)
}

#[inline]
pub fn nonblocking(&self) -> io::Result<bool> {
self.tap.nonblocking()
}

#[inline]
pub fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
self.tap.send(buf)
}

#[inline]
pub fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.tap.recv(buf)
}
}
Loading