use std::fmt;
use std::str::FromStr;
use crate::model::technique::TcpScanTechnique;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SendMode {
#[default]
Auto,
RawSocket,
Ethernet,
}
impl SendMode {
pub const ALL: [SendMode; 3] = [SendMode::Auto, SendMode::RawSocket, SendMode::Ethernet];
pub const fn name(self) -> &'static str {
match self {
SendMode::Auto => "auto",
SendMode::RawSocket => "raw_socket",
SendMode::Ethernet => "ethernet",
}
}
}
impl fmt::Display for SendMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unknown send mode '{input}', expected one of: auto, raw_socket, ethernet")]
pub struct UnknownSendMode {
pub input: String,
}
impl FromStr for SendMode {
type Err = UnknownSendMode;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let name = s.trim().to_ascii_lowercase();
Self::ALL
.into_iter()
.find(|mode| mode.name() == name)
.ok_or_else(|| UnknownSendMode {
input: s.to_string(),
})
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ScanEffort {
Single,
Fast,
#[default]
Balanced,
Thorough,
}
impl ScanEffort {
pub const ALL: [ScanEffort; 4] = [
ScanEffort::Single,
ScanEffort::Fast,
ScanEffort::Balanced,
ScanEffort::Thorough,
];
pub const fn name(self) -> &'static str {
match self {
ScanEffort::Single => "single",
ScanEffort::Fast => "fast",
ScanEffort::Balanced => "balanced",
ScanEffort::Thorough => "thorough",
}
}
}
impl std::fmt::Display for ScanEffort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unknown scan effort '{input}', expected one of: single, fast, balanced, thorough")]
pub struct UnknownScanEffort {
pub input: String,
}
impl std::str::FromStr for ScanEffort {
type Err = UnknownScanEffort;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let name = s.trim().to_ascii_lowercase();
Self::ALL
.into_iter()
.find(|effort| effort.name() == name)
.ok_or_else(|| UnknownScanEffort {
input: s.to_string(),
})
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OsDetection {
Off,
#[default]
Passive,
Active,
Aggressive,
}
impl OsDetection {
pub const ALL: [OsDetection; 4] = [
OsDetection::Off,
OsDetection::Passive,
OsDetection::Active,
OsDetection::Aggressive,
];
pub const fn name(self) -> &'static str {
match self {
OsDetection::Off => "off",
OsDetection::Passive => "passive",
OsDetection::Active => "active",
OsDetection::Aggressive => "aggressive",
}
}
pub const fn level(self) -> u8 {
match self {
OsDetection::Off => 0,
OsDetection::Passive => 1,
OsDetection::Active => 2,
OsDetection::Aggressive => 3,
}
}
pub const fn from_level(level: u8) -> Option<Self> {
match level {
0 => Some(OsDetection::Off),
1 => Some(OsDetection::Passive),
2 => Some(OsDetection::Active),
3 => Some(OsDetection::Aggressive),
_ => None,
}
}
pub const fn is_enabled(self) -> bool {
!matches!(self, OsDetection::Off)
}
pub const fn is_active(self) -> bool {
matches!(self, OsDetection::Active | OsDetection::Aggressive)
}
}
impl fmt::Display for OsDetection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error(
"unknown OS detection level '{input}', expected one of: off, passive, active, aggressive \
(or 0, 1, 2, 3)"
)]
pub struct UnknownOsDetection {
pub input: String,
}
impl FromStr for OsDetection {
type Err = UnknownOsDetection;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let written = s.trim().to_ascii_lowercase();
if let Ok(level) = written.parse::<u8>()
&& let Some(detection) = Self::from_level(level)
{
return Ok(detection);
}
Self::ALL
.into_iter()
.find(|detection| detection.name() == written)
.ok_or_else(|| UnknownOsDetection {
input: s.to_string(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RetryConfig {
pub effort: ScanEffort,
pub max_attempts: Option<u8>,
pub timeout_scale: Option<f64>,
pub dampen_silent_hosts: bool,
}
impl Default for RetryConfig {
fn default() -> Self {
Self {
effort: ScanEffort::default(),
max_attempts: None,
timeout_scale: None,
dampen_silent_hosts: true,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ProbeTuning {
pub send_mode: SendMode,
pub retry: RetryConfig,
pub max_probe_rate: Option<u32>,
pub tcp_technique: TcpScanTechnique,
pub os_detection: OsDetection,
}
#[derive(Debug, Clone, Default)]
pub struct ZondConfig {
pub no_dns: bool,
pub segment_sweep: bool,
pub assume_up: bool,
pub redact: bool,
pub send_mode: SendMode,
pub max_probe_rate: Option<u32>,
pub tcp_technique: TcpScanTechnique,
pub retry: RetryConfig,
pub os_detection: OsDetection,
}
impl ZondConfig {
pub fn probe_tuning(&self) -> ProbeTuning {
ProbeTuning {
send_mode: self.send_mode,
retry: self.retry,
max_probe_rate: self.max_probe_rate,
tcp_technique: self.tcp_technique,
os_detection: self.os_detection,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_os_detection_level_agrees_with_its_number() {
for (index, detection) in OsDetection::ALL.into_iter().enumerate() {
assert_eq!(usize::from(detection.level()), index);
assert_eq!(OsDetection::from_level(detection.level()), Some(detection));
}
let past_the_end = OsDetection::ALL.len() as u8;
assert_eq!(
OsDetection::from_level(past_the_end),
None,
"a level this engine does not offer is refused, not rounded down to the highest"
);
}
#[test]
fn os_detection_sends_nothing_below_the_active_level() {
assert!(!OsDetection::Off.is_active());
assert!(!OsDetection::Passive.is_active());
assert!(OsDetection::Active.is_active());
assert!(OsDetection::Aggressive.is_active());
assert!(!OsDetection::default().is_active(), "the default is silent");
assert!(OsDetection::default().is_enabled(), "and it is still on");
}
#[test]
fn an_os_detection_level_parses_the_same_by_name_and_by_number() {
for detection in OsDetection::ALL {
assert_eq!(detection.name().parse(), Ok(detection));
assert_eq!(detection.level().to_string().parse(), Ok(detection));
}
assert!("4".parse::<OsDetection>().is_err());
assert!("passive-ish".parse::<OsDetection>().is_err());
}
}