1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::net::IpAddr;
use netdev::mac::MacAddr;
use crate::dns;

/// Status of the scanned port
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PortStatus {
    Open,
    Closed,
    Filtered,
    Unknown,
}

impl PortStatus {
    pub fn id(&self) -> String {
        match *self {
            PortStatus::Open => String::from("open"),
            PortStatus::Closed => String::from("closed"),
            PortStatus::Filtered => String::from("filtered"),
            PortStatus::Unknown => String::from("unknown"),
        }
    }
    pub fn name(&self) -> String {
        match *self {
            PortStatus::Open => String::from("Open"),
            PortStatus::Closed => String::from("Closed"),
            PortStatus::Filtered => String::from("Filtered"),
            PortStatus::Unknown => String::from("Unknown"),
        }
    }
}

/// Port Information
#[derive(Clone, Debug, PartialEq)]
pub struct Port {
    /// Port number
    pub number: u16,
    /// Port status
    pub status: PortStatus,
    /// Service name
    pub service_name: String,
    /// Service version
    pub service_version: String,
}

impl Port {
    pub fn new(number: u16) -> Self {
        Self {
            number: number,
            status: PortStatus::Unknown,
            service_name: String::new(),
            service_version: String::new(),
        }
    }
}

/// Host Information
#[derive(Clone, Debug, PartialEq)]
pub struct Host {
    /// IP address of the host
    pub ip_addr: IpAddr,
    /// Host name
    pub hostname: String,
    /// List of ports
    pub ports: Vec<Port>,
    /// MAC address of the host
    pub mac_addr: MacAddr,
    /// TTL
    pub ttl: u8,
}

impl Host {
    pub fn new(ip_addr: IpAddr, hostname: String) -> Self {
        Self {
            ip_addr: ip_addr,
            hostname: hostname,
            ports: Vec::new(),
            mac_addr: MacAddr::zero(),
            ttl: 0,
        }
    }
    pub fn with_port_range(mut self, start: u16, end: u16) -> Self {
        for port in start..end {
            self.ports.push(Port::new(port));
        }
        self
    }
    pub fn with_ports(mut self, ports: Vec<u16>) -> Self {
        for port in ports {
            self.ports.push(Port::new(port));
        }
        self
    }
    pub fn get_ports(&self) -> Vec<u16> {
        self.ports.iter().map(|port| port.number).collect()
    }
    pub fn get_open_port_numbers(&self) -> Vec<u16> {
        self.ports
            .iter()
            .filter(|port| port.status == PortStatus::Open)
            .map(|port| port.number)
            .collect()
    }
    pub fn get_open_ports(&self) -> Vec<Port> {
        self.ports
            .iter()
            .filter(|port| port.status == PortStatus::Open)
            .map(|port| port.clone())
            .collect()
    }
}

/// Node type
#[derive(Clone, Debug, PartialEq)]
pub enum NodeType {
    DefaultGateway,
    Relay,
    Destination,
}

impl NodeType {
    pub fn id(&self) -> String {
        match *self {
            NodeType::DefaultGateway => String::from("default_gateway"),
            NodeType::Relay => String::from("relay"),
            NodeType::Destination => String::from("destination"),
        }
    }
    pub fn name(&self) -> String {
        match *self {
            NodeType::DefaultGateway => String::from("DefaultGateway"),
            NodeType::Relay => String::from("Relay"),
            NodeType::Destination => String::from("Destination"),
        }
    }
}

// Check if the target is an IP address
pub fn is_valid_ip_addr(target: &str) -> bool {
    match target.parse::<IpAddr>() {
        Ok(_) => true,
        Err(_) => false,
    }
}

// Check if the target is a valid hostname
pub fn is_valid_hostname(target: &str) -> bool {
    dns::lookup_host_name(target).is_some()
}

// Check if the target is valid
pub fn is_valid_target(target: &str) -> bool {
    is_valid_ip_addr(target) || is_valid_hostname(target)
}