Remove C code

This commit is contained in:
Dennis Schwerdel 2020-07-14 00:03:08 +02:00
parent 29fe0911f2
commit 12c899b04d
6 changed files with 32 additions and 69 deletions

8
Cargo.lock generated
View File

@ -259,11 +259,6 @@ name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.8" version = "0.2.8"
@ -704,13 +699,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "vpncloud" name = "vpncloud"
version = "1.4.0" version = "1.4.0"
dependencies = [ 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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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 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 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 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 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 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" "checksum proc-macro-error 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fc175e9777c3116627248584e8f8b3e2987405cabe1c0adf7d1dd28f09dc7880"

View File

@ -29,10 +29,6 @@ daemonize = "0.4"
ring = "0.16" ring = "0.16"
privdrop = "0.3" privdrop = "0.3"
[build-dependencies]
cc = "^1"
pkg-config = "0.3"
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"

View File

@ -2,17 +2,11 @@
// Copyright (C) 2015-2020 Dennis Schwerdel // Copyright (C) 2015-2020 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
extern crate cc;
use std::{env, fs, path::Path, process::Command}; use std::{env, fs, path::Path, process::Command};
fn main() { fn main() {
let out_dir = env::var("OUT_DIR").unwrap(); 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 // Process manpage using asciidoctor command
println!("cargo:rerun-if-changed=vpncloud.adoc"); println!("cargo:rerun-if-changed=vpncloud.adoc");
fs::copy("vpncloud.adoc", Path::new(&out_dir).join("vpncloud.adoc")).unwrap(); fs::copy("vpncloud.adoc", Path::new(&out_dir).join("vpncloud.adoc")).unwrap();

View File

@ -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

View File

@ -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 <stdint.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <string.h>
#include <sys/ioctl.h>
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);
}

View File

@ -2,19 +2,40 @@
// Copyright (C) 2015-2020 Dennis Schwerdel // Copyright (C) 2015-2020 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md) // 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::{ use std::{
collections::VecDeque, collections::VecDeque,
fmt, fs, fmt, fs,
io::{self, Error as IoError, ErrorKind, Read, Write}, io::{self, Error as IoError, ErrorKind, Read, Write},
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd},
str,
str::FromStr str::FromStr
}; };
use super::types::Error; use super::types::Error;
extern "C" { static TUNSETIFF: c_ulong = 1074025674;
fn setup_tap_device(fd: i32, ifname: *mut u8) -> i32;
fn setup_tun_device(fd: i32, ifname: *mut u8) -> i32;
#[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_) return Self::dummy(ifname, path, type_)
} }
let fd = fs::OpenOptions::new().read(true).write(true).open(path)?; let fd = fs::OpenOptions::new().read(true).write(true).open(path)?;
// Add trailing \0 to interface name let flags = match type_ {
let mut ifname_string = String::with_capacity(32); Type::Tun => IFF_TUN | IFF_NO_PI,
ifname_string.push_str(ifname); Type::Tap => IFF_TAP | IFF_NO_PI,
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()) },
Type::Dummy => unreachable!() 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 { match res {
0 => { 0 => {
// Remove trailing \0 from name let nul_range_end = ifreq.ifr_name.iter().position(|&c| c == b'\0').unwrap_or(ifreq.ifr_name.len());
while ifname_c.last() == Some(&0) { let ifname = unsafe { str::from_utf8_unchecked(&ifreq.ifr_name[0..nul_range_end]) }.to_string();
ifname_c.pop(); Ok(Self { fd, ifname, type_ })
}
Ok(Self { fd, ifname: String::from_utf8(ifname_c).unwrap(), type_ })
} }
_ => Err(IoError::last_os_error()) _ => Err(IoError::last_os_error())
} }