Skip to content

Commit

Permalink
feat: refine code and testing script
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Nov 2, 2023
1 parent a35761b commit 76be7f3
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 206 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Push or PR

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build_n_test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: rustfmt
run: cargo fmt --all -- --check
- name: check
run: cargo check --verbose
- name: clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Build
run: cargo build --verbose --examples --tests --all-features --features="async tokio/rt-multi-thread"
3 changes: 2 additions & 1 deletion examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = Configuration::default();

config
.address((10, 0, 0, 1))
.address((10, 0, 0, 9))
.netmask((255, 255, 255, 0))
.destination((10, 0, 0, 1))
.up();

#[cfg(target_os = "linux")]
Expand Down
2 changes: 1 addition & 1 deletion src/platform/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Device {
let tun = Fd::new(fd).map_err(|_| io::Error::last_os_error())?;

Device {
queue: Queue { tun: tun },
queue: Queue { tun },
}
};
Ok(device)
Expand Down
2 changes: 1 addition & 1 deletion src/platform/ios/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Device {
let tun = Fd::new(fd).map_err(|_| io::Error::last_os_error())?;

Device {
queue: Queue { tun: tun },
queue: Queue { tun },
}
};
Ok(device)
Expand Down
101 changes: 50 additions & 51 deletions src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

use std::ffi::{CStr, CString};
use std::io::{self, Read, Write};
use std::mem;
use std::net::Ipv4Addr;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::ptr;
use std::sync::Arc;
use std::vec::Vec;

use libc;
use libc::{c_char, c_short};
use libc::{AF_INET, O_RDWR, SOCK_DGRAM};

use crate::configuration::{Configuration, Layer};
use crate::device::Device as D;
use crate::error::*;
use crate::platform::linux::sys::*;
use crate::platform::posix::{self, Fd, SockAddr};
use libc::{
self, c_char, c_short, ifreq, AF_INET, IFF_MULTI_QUEUE, IFF_NO_PI, IFF_RUNNING, IFF_TAP,
IFF_TUN, IFF_UP, IFNAMSIZ, O_RDWR, SOCK_DGRAM,
};
use std::{
ffi::{CStr, CString},
io::{self, Read, Write},
mem,
net::Ipv4Addr,
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
ptr,
sync::Arc,
vec::Vec,
};

use crate::{
configuration::{Configuration, Layer},
device::Device as D,
error::*,
platform::linux::sys::*,
platform::posix::{self, Fd, SockAddr},
};

/// A TUN device using the TUN/TAP Linux driver.
pub struct Device {
Expand Down Expand Up @@ -63,7 +67,7 @@ impl Device {
if let Some(dev) = dev.as_ref() {
ptr::copy_nonoverlapping(
dev.as_ptr() as *const c_char,
req.ifrn.name.as_mut_ptr(),
req.ifr_name.as_mut_ptr(),
dev.as_bytes().len(),
);
}
Expand All @@ -75,13 +79,12 @@ impl Device {
return Err(Error::InvalidQueuesNumber);
}

req.ifru.flags = device_type
| if config.platform.packet_information {
0
} else {
IFF_NO_PI
}
| if queues_num > 1 { IFF_MULTI_QUEUE } else { 0 };
let iff_no_pi = IFF_NO_PI as c_short;
let iff_multi_queue = IFF_MULTI_QUEUE as c_short;
let packet_information = config.platform.packet_information;
req.ifr_ifru.ifru_flags = device_type
| if packet_information { 0 } else { iff_no_pi }
| if queues_num > 1 { iff_multi_queue } else { 0 };

for _ in 0..queues_num {
let tun = Fd::new(libc::open(b"/dev/net/tun\0".as_ptr() as *const _, O_RDWR))
Expand All @@ -97,16 +100,12 @@ impl Device {
});
}

let ctl = Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0))
.map_err(|_| io::Error::last_os_error())?;
let ctl = Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0))?;

Device {
name: CStr::from_ptr(req.ifrn.name.as_ptr())
.to_string_lossy()
.into(),
queues,
ctl,
}
let name = CStr::from_ptr(req.ifr_name.as_ptr())
.to_string_lossy()
.to_string();
Device { name, queues, ctl }
};

device.configure(config)?;
Expand All @@ -119,7 +118,7 @@ impl Device {
let mut req: ifreq = mem::zeroed();
ptr::copy_nonoverlapping(
self.name.as_ptr() as *const c_char,
req.ifrn.name.as_mut_ptr(),
req.ifr_name.as_mut_ptr(),
self.name.len(),
);

Expand Down Expand Up @@ -218,7 +217,7 @@ impl D for Device {
let mut req = self.request();
ptr::copy_nonoverlapping(
name.as_ptr() as *const c_char,
req.ifru.newname.as_mut_ptr(),
req.ifr_ifru.ifru_newname.as_mut_ptr(),
value.len(),
);

Expand All @@ -241,9 +240,9 @@ impl D for Device {
}

if value {
req.ifru.flags |= IFF_UP | IFF_RUNNING;
req.ifr_ifru.ifru_flags |= (IFF_UP | IFF_RUNNING) as c_short;
} else {
req.ifru.flags &= !IFF_UP;
req.ifr_ifru.ifru_flags &= !(IFF_UP as c_short);
}

if siocsifflags(self.ctl.as_raw_fd(), &req) < 0 {
Expand All @@ -262,14 +261,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.addr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_addr).map(Into::into)
}
}

fn set_address(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.addr = SockAddr::from(value).into();
req.ifr_ifru.ifru_addr = SockAddr::from(value).into();

if siocsifaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -287,14 +286,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.dstaddr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_dstaddr).map(Into::into)
}
}

fn set_destination(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.dstaddr = SockAddr::from(value).into();
req.ifr_ifru.ifru_dstaddr = SockAddr::from(value).into();

if siocsifdstaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -312,14 +311,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.broadaddr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_broadaddr).map(Into::into)
}
}

fn set_broadcast(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.broadaddr = SockAddr::from(value).into();
req.ifr_ifru.ifru_broadaddr = SockAddr::from(value).into();

if siocsifbrdaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -337,14 +336,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.netmask).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_netmask).map(Into::into)
}
}

fn set_netmask(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.netmask = SockAddr::from(value).into();
req.ifr_ifru.ifru_netmask = SockAddr::from(value).into();

if siocsifnetmask(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -362,14 +361,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

Ok(req.ifru.mtu)
Ok(req.ifr_ifru.ifru_mtu)
}
}

fn set_mtu(&mut self, value: i32) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.mtu = value;
req.ifr_ifru.ifru_mtu = value;

if siocsifmtu(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand Down Expand Up @@ -452,8 +451,8 @@ impl IntoRawFd for Queue {
impl From<Layer> for c_short {
fn from(layer: Layer) -> Self {
match layer {
Layer::L2 => IFF_TAP,
Layer::L3 => IFF_TUN,
Layer::L2 => IFF_TAP as c_short,
Layer::L3 => IFF_TUN as c_short,
}
}
}
76 changes: 1 addition & 75 deletions src/platform/linux/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,7 @@
//! Bindings to internal Linux stuff.

use ioctl::*;
use libc::sockaddr;
use libc::{c_char, c_int, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void};

pub const IFNAMSIZ: usize = 16;

pub const IFF_UP: c_short = 0x1;
pub const IFF_RUNNING: c_short = 0x40;

pub const IFF_TUN: c_short = 0x0001;
pub const IFF_TAP: c_short = 0x0002;
pub const IFF_NO_PI: c_short = 0x1000;
pub const IFF_MULTI_QUEUE: c_short = 0x0100;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifmap {
pub mem_start: c_ulong,
pub mem_end: c_ulong,
pub base_addr: c_ushort,
pub irq: c_uchar,
pub dma: c_uchar,
pub port: c_uchar,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifsu {
pub raw_hdlc_proto: *mut c_void,
pub cisco: *mut c_void,
pub fr: *mut c_void,
pub fr_pvc: *mut c_void,
pub fr_pvc_info: *mut c_void,
pub sync: *mut c_void,
pub te1: *mut c_void,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct if_settings {
pub type_: c_uint,
pub size: c_uint,
pub ifsu: ifsu,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifrn {
pub name: [c_char; IFNAMSIZ],
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifru {
pub addr: sockaddr,
pub dstaddr: sockaddr,
pub broadaddr: sockaddr,
pub netmask: sockaddr,
pub hwaddr: sockaddr,

pub flags: c_short,
pub ivalue: c_int,
pub mtu: c_int,
pub map: ifmap,
pub slave: [c_char; IFNAMSIZ],
pub newname: [c_char; IFNAMSIZ],
pub data: *mut c_void,
pub settings: if_settings,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifreq {
pub ifrn: ifrn,
pub ifru: ifru,
}
use libc::{c_int, ifreq};

ioctl!(bad read siocgifflags with 0x8913; ifreq);
ioctl!(bad write siocsifflags with 0x8914; ifreq);
Expand Down
Loading

0 comments on commit 76be7f3

Please sign in to comment.