Skip to content

Instantly share code, notes, and snippets.

@mnogu
Created November 12, 2022 13:29
Show Gist options
  • Save mnogu/4c04d3083f1fdcf513eda452562061eb to your computer and use it in GitHub Desktop.
Save mnogu/4c04d3083f1fdcf513eda452562061eb to your computer and use it in GitHub Desktop.
Make the state of tap0 up 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::{
ioctl_write_ptr_bad, libc,
sys::socket::{socket, AddressFamily, SockFlag, SockType},
};
ioctl_write_ptr_bad!(siocsifflags, libc::SIOCSIFFLAGS, libc::ifreq);
fn main() {
let sock = socket(
AddressFamily::Inet,
SockType::Datagram,
SockFlag::empty(),
None,
)
.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_UP as i16;
let ifr_ifru = libc::__c_anonymous_ifr_ifru { ifru_flags };
let ifreq = libc::ifreq { ifr_name, ifr_ifru };
unsafe { siocsifflags(sock, &ifreq) }.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment