summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/log.rs28
-rw-r--r--src/opt.rs20
-rw-r--r--src/platform.rs10
-rw-r--r--src/platform/linux.rs12
-rw-r--r--src/platform/windows.rs19
5 files changed, 43 insertions, 46 deletions
diff --git a/src/log.rs b/src/log.rs
index a9d831f..90d47ac 100644
--- a/src/log.rs
+++ b/src/log.rs
@@ -61,37 +61,11 @@ impl std::str::FromStr for LogLevel {
}
}
-#[cfg(unix)]
-pub fn local_time() -> (i32, u8, u8, u8, u8, u8) {
- unsafe {
- let t = libc::time(std::ptr::null_mut());
- let mut tm: libc::tm = std::mem::zeroed();
- if t == -1 || libc::localtime_r(&t, &mut tm).is_null() {
- return (0, 0, 0, 0, 0, 0);
- };
- (tm.tm_year + 1900, (tm.tm_mon + 1) as u8, tm.tm_mday as u8,
- tm.tm_hour as u8, tm.tm_min as u8, tm.tm_sec as u8)
- }
-}
-
-#[cfg(windows)]
-pub fn local_time() -> (i32, u8, u8, u8, u8, u8) {
- use std::mem::zeroed;
- #[repr(C)]
- struct SYSTEMTIME { y: u16, m: u16, _dow: u16, d: u16, h: u16, min: u16, s: u16, _ms: u16 }
- unsafe extern "system" { fn GetLocalTime(st: *mut SYSTEMTIME); }
- unsafe {
- let mut st: SYSTEMTIME = zeroed();
- GetLocalTime(&mut st);
- (st.y as i32, st.m as u8, st.d as u8, st.h as u8, st.min as u8, st.s as u8)
- }
-}
-
#[macro_export]
macro_rules! log_println {
($level:expr, $($arg:tt)*) => {{
if $level >= crate::opt::log_level() {
- let (y, mo, d, h, mi, s) = crate::log::local_time();
+ let (y, mo, d, h, mi, s) = crate::platform::local_time();
println!("{y:04}-{mo:02}-{d:02} {h:02}:{mi:02}:{s:02} {} {}",
$level, format_args!($($arg)*));
}
diff --git a/src/opt.rs b/src/opt.rs
index db3a574..caf59e9 100644
--- a/src/opt.rs
+++ b/src/opt.rs
@@ -5,6 +5,7 @@ use anyhow::{Result, anyhow, Context};
use std::sync::OnceLock;
use crate::log;
+use crate::platform;
use log::LogLevel;
@@ -86,21 +87,6 @@ impl std::fmt::Display for SegmentOrder {
}
}
-#[cfg(windows)]
-fn pause() {
- println!("Press any key to exit...");
-
- unsafe extern "C" { fn _getch() -> i32; }
- unsafe { _getch(); }
-}
-
-/// pause before exit on windows to print information in
-/// console before it is closed.
-fn paexit(code: i32) {
- #[cfg(windows)] pause();
- std::process::exit(code);
-}
-
static OPT_DAEMON: OnceLock<bool> = OnceLock::new();
static OPT_LOG_LEVEL: OnceLock<LogLevel> = OnceLock::new();
static OPT_NO_SPLASH: OnceLock<bool> = OnceLock::new();
@@ -166,7 +152,7 @@ impl Opt {
let argv = arg.as_str();
match argv {
- "-h" | "--help" => { usage(); paexit(0); }
+ "-h" | "--help" => { usage(); platform::paexit(0); }
"-d" | "-D" | "--daemon" => {
if argv == "-D" && !warned_daemon_deprecated {
// FIXME(on release): remove this on v1.0.0
@@ -207,7 +193,7 @@ impl Opt {
#[cfg(target_os = "linux")]
"--nft-command" => { nft_command = take_value(&mut args, argv)?; }
- _ => { return Err(anyhow!("argument: unknown: {}", arg)); }
+ _ => { return Err(anyhow!("unknown argument: {}", arg)); }
}
}
diff --git a/src/platform.rs b/src/platform.rs
index eca2df5..5b74eb9 100644
--- a/src/platform.rs
+++ b/src/platform.rs
@@ -24,10 +24,16 @@ Press Ctrl+C or close this window to stop.
pub mod windows;
#[cfg(windows)]
-pub use windows::*;
+pub use windows::{bootstrap, run, local_time, send_to_raw};
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(target_os = "linux")]
-pub use linux::*;
+pub use linux::{bootstrap, run, local_time, send_to_raw, IS_U32_SUPPORTED};
+
+/// pause before exit on windows to print information in console before it is closed.
+pub fn paexit(code: i32) {
+ #[cfg(windows)] pause();
+ std::process::exit(code);
+}
diff --git a/src/platform/linux.rs b/src/platform/linux.rs
index 331e81c..00d4729 100644
--- a/src/platform/linux.rs
+++ b/src/platform/linux.rs
@@ -364,3 +364,15 @@ fn daemonize() {
}
}
}
+
+pub fn local_time() -> (i32, u8, u8, u8, u8, u8) {
+ unsafe {
+ let t = libc::time(std::ptr::null_mut());
+ let mut tm: libc::tm = std::mem::zeroed();
+ if t == -1 || libc::localtime_r(&t, &mut tm).is_null() {
+ return (0, 0, 0, 0, 0, 0);
+ };
+ (tm.tm_year + 1900, (tm.tm_mon + 1) as u8, tm.tm_mday as u8,
+ tm.tm_hour as u8, tm.tm_min as u8, tm.tm_sec as u8)
+ }
+}
diff --git a/src/platform/windows.rs b/src/platform/windows.rs
index 94fc185..c947b6a 100644
--- a/src/platform/windows.rs
+++ b/src/platform/windows.rs
@@ -25,6 +25,13 @@ use std::sync::{LazyLock, Mutex};
use std::thread;
use crate::{opt, pkt};
+fn pause() {
+ println!("Press any key to exit...");
+
+ unsafe extern "C" { fn _getch() -> i32; }
+ unsafe { _getch(); }
+}
+
fn open_handle(filter: &str, flags: prelude::WinDivertFlags) -> WinDivert<NetworkLayer> {
use windivert::*;
@@ -148,3 +155,15 @@ fn service_main() {
}
};
}
+
+pub fn local_time() -> (i32, u8, u8, u8, u8, u8) {
+ use std::mem::zeroed;
+ #[repr(C)]
+ struct SYSTEMTIME { y: u16, m: u16, _dow: u16, d: u16, h: u16, min: u16, s: u16, _ms: u16 }
+ unsafe extern "system" { fn GetLocalTime(st: *mut SYSTEMTIME); }
+ unsafe {
+ let mut st: SYSTEMTIME = zeroed();
+ GetLocalTime(&mut st);
+ (st.y as i32, st.m as u8, st.d as u8, st.h as u8, st.min as u8, st.s as u8)
+ }
+}