From 12c899b04d3e05bed095a16290169c435a5c0d43 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Tue, 14 Jul 2020 00:03:08 +0200 Subject: [PATCH] Remove C code --- Cargo.lock | 8 -------- Cargo.toml | 4 ---- build.rs | 6 ------ src/c/Makefile | 7 ------- src/c/tuntap.c | 27 --------------------------- src/device.rs | 49 ++++++++++++++++++++++++++++++++----------------- 6 files changed, 32 insertions(+), 69 deletions(-) delete mode 100644 src/c/Makefile delete mode 100644 src/c/tuntap.c diff --git a/Cargo.lock b/Cargo.lock index 212eba3..68a77d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -259,11 +259,6 @@ name = "percent-encoding" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "pkg-config" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "ppv-lite86" version = "0.2.8" @@ -704,13 +699,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "vpncloud" version = "1.4.0" dependencies = [ - "cc 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)", "daemonize 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "igd 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.72 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", "privdrop 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "ring 0.16.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -863,7 +856,6 @@ dependencies = [ "checksum nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" "checksum once_cell 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" -"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" "checksum privdrop 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "939fa7cbfef9c15c65cf2fb3ed57f3f2a14dca1757a556aa1ba4a7f998b2b479" "checksum proc-macro-error 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fc175e9777c3116627248584e8f8b3e2987405cabe1c0adf7d1dd28f09dc7880" diff --git a/Cargo.toml b/Cargo.toml index e8ff589..bb660e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,10 +29,6 @@ daemonize = "0.4" ring = "0.16" privdrop = "0.3" -[build-dependencies] -cc = "^1" -pkg-config = "0.3" - [dev-dependencies] tempfile = "3" diff --git a/build.rs b/build.rs index a7e9439..e15db69 100644 --- a/build.rs +++ b/build.rs @@ -2,17 +2,11 @@ // Copyright (C) 2015-2020 Dennis Schwerdel // This software is licensed under GPL-3 or newer (see LICENSE.md) -extern crate cc; - use std::{env, fs, path::Path, process::Command}; fn main() { let out_dir = env::var("OUT_DIR").unwrap(); - // Compile tun/tap C code - println!("cargo:rerun-if-changed=src/c/tuntap.c"); - cc::Build::new().file("src/c/tuntap.c").include("src").compile("libtuntap.a"); - // Process manpage using asciidoctor command println!("cargo:rerun-if-changed=vpncloud.adoc"); fs::copy("vpncloud.adoc", Path::new(&out_dir).join("vpncloud.adoc")).unwrap(); diff --git a/src/c/Makefile b/src/c/Makefile deleted file mode 100644 index 2bce4c6..0000000 --- a/src/c/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -default: libtuntap.a - -tapdev.o: tuntap.c - gcc -Os -c tuntap.c - -libtapdev.a: tuntap.o - ar rcs libtuntap.a tuntap.o diff --git a/src/c/tuntap.c b/src/c/tuntap.c deleted file mode 100644 index dae22dc..0000000 --- a/src/c/tuntap.c +++ /dev/null @@ -1,27 +0,0 @@ -// VpnCloud - Peer-to-Peer VPN -// Copyright (C) 2015-2020 Dennis Schwerdel -// This software is licensed under GPL-3 or newer (see LICENSE.md) - -#include -#include -#include -#include -#include - -int32_t setup_device(int32_t fd, char *ifname, int32_t flags) { - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = flags; - strncpy(ifr.ifr_name, ifname, IFNAMSIZ); - if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) return 1; - strncpy(ifname, ifr.ifr_name, IFNAMSIZ); - return 0; -} - -int32_t setup_tap_device(int32_t fd, char *ifname) { - return setup_device(fd, ifname, IFF_TAP | IFF_NO_PI); -} - -int32_t setup_tun_device(int32_t fd, char *ifname) { - return setup_device(fd, ifname, IFF_TUN | IFF_NO_PI); -} diff --git a/src/device.rs b/src/device.rs index 8c9cfb0..da72b86 100644 --- a/src/device.rs +++ b/src/device.rs @@ -2,19 +2,40 @@ // Copyright (C) 2015-2020 Dennis Schwerdel // This software is licensed under GPL-3 or newer (see LICENSE.md) +use libc::{c_short, c_ulong, ioctl, IFF_NO_PI, IFF_TAP, IFF_TUN, IF_NAMESIZE}; use std::{ collections::VecDeque, fmt, fs, io::{self, Error as IoError, ErrorKind, Read, Write}, os::unix::io::{AsRawFd, RawFd}, + str, str::FromStr }; use super::types::Error; -extern "C" { - fn setup_tap_device(fd: i32, ifname: *mut u8) -> i32; - fn setup_tun_device(fd: i32, ifname: *mut u8) -> i32; +static TUNSETIFF: c_ulong = 1074025674; + + +#[repr(C)] +union IfReqData { + flags: c_short, + _dummy: [u8; 24] +} + +#[repr(C)] +struct IfReq { + ifr_name: [u8; IF_NAMESIZE], + data: IfReqData +} + +impl IfReq { + fn new(name: &str, flags: c_short) -> Self { + assert!(name.len() < IF_NAMESIZE); + let mut ifr_name = [0 as u8; IF_NAMESIZE]; + ifr_name[..name.len()].clone_from_slice(name.as_bytes()); + Self { ifr_name, data: IfReqData { flags } } + } } @@ -120,24 +141,18 @@ impl TunTapDevice { return Self::dummy(ifname, path, type_) } let fd = fs::OpenOptions::new().read(true).write(true).open(path)?; - // Add trailing \0 to interface name - let mut ifname_string = String::with_capacity(32); - ifname_string.push_str(ifname); - ifname_string.push('\0'); - assert!(ifname_string.len() <= 32); - let mut ifname_c = ifname_string.into_bytes(); - let res = match type_ { - Type::Tun => unsafe { setup_tun_device(fd.as_raw_fd(), ifname_c.as_mut_ptr()) }, - Type::Tap => unsafe { setup_tap_device(fd.as_raw_fd(), ifname_c.as_mut_ptr()) }, + let flags = match type_ { + Type::Tun => IFF_TUN | IFF_NO_PI, + Type::Tap => IFF_TAP | IFF_NO_PI, Type::Dummy => unreachable!() }; + let mut ifreq = IfReq::new(ifname, flags as c_short); + let res = unsafe { ioctl(fd.as_raw_fd(), TUNSETIFF, &mut ifreq) }; match res { 0 => { - // Remove trailing \0 from name - while ifname_c.last() == Some(&0) { - ifname_c.pop(); - } - Ok(Self { fd, ifname: String::from_utf8(ifname_c).unwrap(), type_ }) + let nul_range_end = ifreq.ifr_name.iter().position(|&c| c == b'\0').unwrap_or(ifreq.ifr_name.len()); + let ifname = unsafe { str::from_utf8_unchecked(&ifreq.ifr_name[0..nul_range_end]) }.to_string(); + Ok(Self { fd, ifname, type_ }) } _ => Err(IoError::last_os_error()) }