pub mod discovery;
pub mod security;
pub mod service;
pub mod set;
pub use discovery::{Discovery, ScanResponse};
pub use security::{CertificateInfo, Security};
pub use service::Service;
pub use set::{PortSet, PortSetParseError};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Protocol {
Tcp,
Udp,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PortState {
ClosedFiltered,
Filtered,
Unfiltered,
Closed,
OpenFiltered,
Open,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Port {
number: u16,
protocol: Protocol,
state: PortState,
service: Option<Service>,
security: Option<Security>,
discovery: Option<Discovery>,
}
impl Port {
pub fn new(number: u16, protocol: Protocol, state: PortState) -> Self {
Self {
number,
protocol,
state,
service: None,
security: None,
discovery: None,
}
}
pub fn number(&self) -> u16 {
self.number
}
pub fn protocol(&self) -> Protocol {
self.protocol
}
pub fn state(&self) -> PortState {
self.state
}
pub fn set_state(&mut self, state: PortState) {
self.state = std::cmp::max(self.state, state);
}
pub fn service(&self) -> Option<&Service> {
self.service.as_ref()
}
pub fn service_name(&self) -> Option<&str> {
self.service.as_ref().map(|s| s.name())
}
pub fn set_service(&mut self, service: Service) {
self.service = Some(service);
}
pub fn security(&self) -> Option<&Security> {
self.security.as_ref()
}
pub fn set_security(&mut self, security: Security) {
self.security = Some(security);
}
pub fn discovery(&self) -> Option<&Discovery> {
self.discovery.as_ref()
}
pub fn with_service(mut self, service: Service) -> Self {
self.service = Some(service);
self
}
pub fn with_security(mut self, security: Security) -> Self {
self.security = Some(security);
self
}
pub fn with_discovery(mut self, discovery: Discovery) -> Self {
self.discovery = Some(discovery);
self
}
pub fn merge(&mut self, other: Port) {
debug_assert_eq!(
(self.number, self.protocol),
(other.number, other.protocol),
"merging two different endpoints into one record"
);
let previous_state = self.state;
self.state = std::cmp::max(self.state, other.state);
if let Some(other_service) = other.service {
if let Some(ref mut self_service) = self.service {
self_service.merge(other_service);
} else {
self.service = Some(other_service);
}
}
if let Some(other_security) = other.security {
if let Some(ref mut self_security) = self.security {
self_security.merge(other_security);
} else {
self.security = Some(other_security);
}
}
if other.discovery.is_some() && (other.state > previous_state || self.discovery.is_none()) {
self.discovery = other.discovery;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_probe_that_learned_more_raises_the_verdict() {
let mut p1 = Port::new(80, Protocol::Tcp, PortState::Filtered);
p1.merge(Port::new(80, Protocol::Tcp, PortState::Open));
assert_eq!(p1.state(), PortState::Open);
let mut p2 = Port::new(53, Protocol::Udp, PortState::OpenFiltered);
p2.merge(Port::new(53, Protocol::Udp, PortState::Open));
assert_eq!(p2.state(), PortState::Open);
let mut p3 = Port::new(443, Protocol::Tcp, PortState::Unfiltered);
p3.merge(Port::new(443, Protocol::Tcp, PortState::Closed));
assert_eq!(p3.state(), PortState::Closed);
}
#[test]
fn a_tie_keeps_the_telemetry_already_on_record() {
let mut first = Port::new(22, Protocol::Tcp, PortState::Open)
.with_discovery(Discovery::new(ScanResponse::TcpSynAck).with_ttl(64));
let second = Port::new(22, Protocol::Tcp, PortState::Open)
.with_discovery(Discovery::new(ScanResponse::TcpSynAck).with_ttl(128));
first.merge(second);
assert_eq!(
first.discovery().expect("telemetry survives").ttl(),
Some(64)
);
}
#[test]
fn a_port_with_no_telemetry_adopts_a_weaker_probes() {
let mut open = Port::new(22, Protocol::Tcp, PortState::Open);
let filtered = Port::new(22, Protocol::Tcp, PortState::Filtered)
.with_discovery(Discovery::new(ScanResponse::NoResponse));
open.merge(filtered);
assert_eq!(open.state(), PortState::Open, "the weaker state loses");
assert_eq!(
open.discovery().expect("adopted").reason(),
&ScanResponse::NoResponse,
"but its account of itself is better than none"
);
}
#[test]
fn telemetry_follows_the_verdict_it_explains() {
let disc_filtered = Discovery::new(ScanResponse::NoResponse);
let mut p_filtered =
Port::new(22, Protocol::Tcp, PortState::Filtered).with_discovery(disc_filtered.clone());
let disc_open = Discovery::new(ScanResponse::TcpSynAck);
let p_open =
Port::new(22, Protocol::Tcp, PortState::Open).with_discovery(disc_open.clone());
p_filtered.merge(p_open);
assert_eq!(p_filtered.state(), PortState::Open);
assert_eq!(
p_filtered.discovery().unwrap().reason(),
&ScanResponse::TcpSynAck
);
}
}