From 1f5a8122621e6bfa8e629a19c3d474685c4de1e4 Mon Sep 17 00:00:00 2001 From: core Date: Fri, 30 Dec 2022 19:20:43 -0500 Subject: [PATCH] [cli] work on cli --- quicktap-cli/src/command.rs | 11 ++++++- quicktap-cli/src/main.rs | 61 +++++++++++++++---------------------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/quicktap-cli/src/command.rs b/quicktap-cli/src/command.rs index 894806f..e15f923 100644 --- a/quicktap-cli/src/command.rs +++ b/quicktap-cli/src/command.rs @@ -1,6 +1,15 @@ +use std::error::Error; +use std::os::unix::net::UnixListener; + pub enum PacketThreadCommand { UpdateConfig(UpdateConfigCommand), Stop } -pub struct UpdateConfigCommand {} \ No newline at end of file +pub struct UpdateConfigCommand {} + +pub fn command_handler(listener: UnixListener) -> Result<(), Box> { + listener.set_nonblocking(true)?; + + Ok(()) +} \ No newline at end of file diff --git a/quicktap-cli/src/main.rs b/quicktap-cli/src/main.rs index 4845594..d3911e7 100644 --- a/quicktap-cli/src/main.rs +++ b/quicktap-cli/src/main.rs @@ -1,24 +1,20 @@ -use std::collections::HashMap; + use std::fs::File; -use std::io; -use std::io::Read; -use std::net::{Ipv4Addr, SocketAddr}; -use std::os::unix::net::{UnixListener, UnixStream}; -use std::path::Path; + +use std::os::unix::net::{UnixListener}; + use std::sync::mpsc::channel; -use std::thread::{sleep, spawn}; -use std::time::Duration; +use std::thread::{spawn}; use clap::Parser; use daemonize::Daemonize; use log::{error, info}; -use quicktap::cidr::{IpInet, Ipv4Inet}; use quicktap::drivers::tun::TunDevice; use quicktap::drivers::tungeneric::{GenericDriver, GenericInterface}; use crate::cli::Cli; -use crate::command::PacketThreadCommand; +use crate::command::{command_handler, PacketThreadCommand}; use crate::pproc::packet_thread; pub mod cli; @@ -65,7 +61,7 @@ fn main() { info!("Creating unconfigured device"); - let mut device = match TunDevice::new(&GenericInterface { + let device = match TunDevice::new(&GenericInterface { addresses: vec![], mtu: None, name: args.interface_name.clone(), @@ -77,23 +73,6 @@ fn main() { } }; - info!("Device created; opening listening socket"); - - let mut config_listener = match UnixListener::bind(format!("/var/run/{}.sock", args.interface_name)) { - Ok(s) => s, - Err(e) => { - error!("Failed to open listen socket /var/run/{}.sock: {}", args.interface_name, e); - std::process::exit(3); - } - }; - match config_listener.set_nonblocking(true) { - Ok(_) => (), - Err(e) => { - error!("Failed to set stream as nonblocking: {}", e); - std::process::exit(4); - } - }; - info!("Spawning packet processor thread"); let (tx, rx) = channel::(); @@ -107,7 +86,19 @@ fn main() { } }); - sleep(Duration::from_secs(10)); + info!("Creating command listener"); + match UnixListener::bind(format!("/var/run/{}.sock", args.interface_name)) { + Ok(l) => { + // handle l + match command_handler(l) { + Ok(_) => (), + Err(e) => error!("Error in command handler: {}", e) + } + }, + Err(e) => { + error!("Error creating command listener {}: {}", format!("/var/run/{}.sock", args.interface_name), e); + } + }; match tx.send(PacketThreadCommand::Stop) { Ok(_) => (), @@ -128,13 +119,11 @@ fn main() { info!("Cleaning up"); - if Path::new(&format!("/var/run/{}.sock", args.interface_name)).exists() { - match std::fs::remove_file(format!("/var/run/{}.sock", args.interface_name)) { - Ok(_) => (), - Err(e) => { - error!("Failed to remove existing socket: {}", e); - std::process::exit(7); - } + match std::fs::remove_file(format!("/var/run/{}.sock", args.interface_name)) { + Ok(_) => (), + Err(e) => { + error!("Failed to remove existing socket: {}", e); + std::process::exit(7); } }