Skip to content

Instantly share code, notes, and snippets.

@mnogu
Created November 12, 2022 12:03
Show Gist options
  • Save mnogu/bebf9deedcb38045b4169aed4ade85c7 to your computer and use it in GitHub Desktop.
Save mnogu/bebf9deedcb38045b4169aed4ade85c7 to your computer and use it in GitHub Desktop.
Create a TAP interface on Ubuntu 22.04.1 LTS with Rust (mkdir src && mv main.rs src && sudo cargo run)
[package]
name = "pareiodon"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nix = "0.25.0"
use std::os::raw::c_char;
use nix::{
fcntl::{open, OFlag},
ioctl_write_int, libc,
sys::stat::Mode,
};
ioctl_write_int!(tunsetiff, b'T', 202);
ioctl_write_int!(tunsetpersist, b'T', 203);
fn main() {
let fd = open("/dev/net/tun", OFlag::O_RDWR, Mode::empty()).unwrap();
let mut ifr_name: [c_char; libc::IF_NAMESIZE] = [0; libc::IF_NAMESIZE];
let name = b"tap0\0";
ifr_name[..name.len()].copy_from_slice(&name.map(|c| c as i8)[..]);
let ifru_flags = libc::IFF_TAP as i16;
let ifr_ifru = libc::__c_anonymous_ifr_ifru { ifru_flags };
let ifreq = libc::ifreq { ifr_name, ifr_ifru };
unsafe { tunsetiff(fd, &ifreq as *const _ as _) }.unwrap();
unsafe { tunsetpersist(fd, 1) }.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment