1#[cfg(test)]
9use mockall::{automock, predicate::*};
10
11use serde;
12use serde::{Deserialize, Serialize};
13use std::collections::HashSet;
14use std::error::Error;
15use std::fmt;
16use std::thread::JoinHandle;
17
18pub const IDLE_TIMEOUT: u16 = 10000;
20
21#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub struct Port {
24 pub id: u16,
26 pub service: String,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
32pub struct Device {
34 pub hostname: String,
36 pub ip: String,
38 pub mac: String,
40 pub vendor: String,
42 pub is_current_host: bool,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
48pub struct DeviceWithPorts {
50 pub ip: String,
52 pub mac: String,
54 pub hostname: String,
56 pub vendor: String,
58 pub is_current_host: bool,
60 pub open_ports: HashSet<Port>,
62}
63
64impl From<DeviceWithPorts> for Device {
65 fn from(value: DeviceWithPorts) -> Self {
66 Self {
67 ip: value.ip.clone(),
68 mac: value.mac.clone(),
69 hostname: value.hostname.clone(),
70 vendor: value.vendor.clone(),
71 is_current_host: value.is_current_host,
72 }
73 }
74}
75
76#[derive(Debug)]
77pub struct Scanning {
79 pub ip: String,
81 pub port: Option<String>,
83}
84
85#[derive(Debug)]
86pub struct ScanError {
88 pub ip: Option<String>,
90 pub port: Option<String>,
92 pub error: Box<dyn Error>,
94}
95
96impl fmt::Display for ScanError {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 let ip = self.ip.clone().unwrap_or(String::from(""));
99 let port = self.port.clone().unwrap_or(String::from(""));
100 let msg = format!(
101 "scanning error: ip {ip}, port: {port}, msg: {0}",
102 self.error
103 );
104 write!(f, "{msg}")
105 }
106}
107
108impl Error for ScanError {}
109unsafe impl Send for ScanError {}
110unsafe impl Sync for ScanError {}
111
112#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
113pub struct SYNScanResult {
115 pub device: Device,
117 pub open_port: Port,
119}
120
121#[derive(Debug)]
122pub enum ScanMessage {
125 Done,
127 Info(Scanning),
129 ARPScanResult(Device),
131 SYNScanResult(SYNScanResult),
133}
134
135#[cfg_attr(test, automock)]
136pub trait Scanner: Sync + Send {
138 fn scan(&self) -> JoinHandle<Result<(), ScanError>>;
140}
141
142pub mod arp_scanner;
143pub mod full_scanner;
144mod heartbeat;
145pub mod syn_scanner;
146
147#[cfg(test)]
148#[path = "./scanners_tests.rs"]
149mod tests;