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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::net::{IpAddr, Ipv4Addr};
use serde::{Deserialize, Serialize};

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

impl PortStatus {
    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"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Port {
    pub port_number: u16,
    pub port_status: PortStatus,
    pub service_name: String,
}

/// The host
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Host {
    pub ip_addr: IpAddr,
    pub host_name: String,
    pub ports: Vec<Port>,
}

impl Host {
    pub fn new() -> Host {
        Host {
            ip_addr: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
            host_name: String::new(),
            ports: Vec::new(),
        }
    }
    pub fn add_open_port(&mut self, port_number: u16, service_name: String) {
        let port: Port = Port {
            port_number: port_number,
            port_status: PortStatus::Open,
            service_name: service_name,
        };
        self.ports.push(port);
    }
    pub fn add_closed_port(&mut self, port_number: u16) {
        let port: Port = Port {
            port_number: port_number,
            port_status: PortStatus::Closed,
            service_name: String::new(),
        };
        self.ports.push(port);
    }
    pub fn get_open_ports(&self) -> Vec<u16> {
        let mut open_ports: Vec<u16> = Vec::new();
        for port in &self.ports {
            if port.port_status == PortStatus::Open {
                open_ports.push(port.port_number);
            }
        }
        open_ports
    }
    pub fn get_closed_ports(&self) -> Vec<u16> {
        let mut closed_ports: Vec<u16> = Vec::new();
        for port in &self.ports {
            if port.port_status == PortStatus::Closed {
                closed_ports.push(port.port_number);
            }
        }
        closed_ports
    }
}

/// Port Information
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ServiceInfo {
    pub port_number: u16,
    pub port_status: PortStatus,
    pub service_name: String,
    pub service_version: String,
    pub cpe: String,
    pub remark: String,
}

impl ServiceInfo {
    pub fn new() -> ServiceInfo {
        ServiceInfo {
            port_number: 0,
            port_status: PortStatus::Unknown,
            service_name: String::new(),
            service_version: String::new(),
            cpe: String::new(),
            remark: String::new(),
        }
    }
}

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

impl NodeType {
    pub fn name(&self) -> String {
        match *self {
            NodeType::DefaultGateway => String::from("DefaultGateway"),
            NodeType::Relay => String::from("Relay"),
            NodeType::Destination => String::from("Destination"),
        }
    }
    pub fn from_tracert_type(node_type: tracert::node::NodeType) -> NodeType {
        match node_type {
            tracert::node::NodeType::DefaultGateway => NodeType::DefaultGateway,
            tracert::node::NodeType::Relay => NodeType::Relay,
            tracert::node::NodeType::Destination => NodeType::Destination,
        }
    }
}

/// Host Information
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NodeInfo {
    pub ip_addr: IpAddr,
    pub host_name: String,
    pub ttl: u8,
    pub mac_addr: String,
    pub vendor_info: String,
    pub os_name: String,
    pub cpe: String,
    pub services: Vec<ServiceInfo>,
    pub node_type: NodeType,
}

impl NodeInfo {
    pub fn new() -> NodeInfo {
        NodeInfo {
            ip_addr: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
            host_name: String::new(),
            ttl: 0,
            mac_addr: String::new(),
            vendor_info: String::new(),
            os_name: String::new(),
            cpe: String::new(),
            services: Vec::new(),
            node_type: NodeType::Destination,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Oui {
    pub mac_prefix: String,
    pub vendor_name: String,
    pub vendor_name_detail: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TcpService {
    pub port: u16,
    pub service_name: String,
    pub service_description: String,
    pub wellknown_flag: u32,
    pub default_flag: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UdpService {
    pub port: u16,
    pub service_name: String,
    pub service_description: String,
    pub wellknown_flag: u32,
    pub default_flag: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OsFingerprint {
    pub cpe: String,
    pub os_name: String,
    pub os_vendor: String,
    pub os_family: String,
    pub os_generation: String,
    pub device_type: String,
    pub tcp_window_size: u16,
    pub tcp_option_pattern: String,
}

impl OsFingerprint {
    pub fn new() -> OsFingerprint {
        OsFingerprint {
            cpe: String::new(),
            os_name: String::new(),
            os_vendor: String::new(),
            os_family: String::new(),
            os_generation: String::new(),
            device_type: String::new(),
            tcp_window_size: 0,
            tcp_option_pattern: String::new(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OsFamilyFingerprint {
    pub os_family: String,
    pub tcp_window_size: u16,
    pub tcp_option_pattern: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OsTtl {
    pub os_family: String,
    pub os_description: String,
    pub initial_ttl: u8,
}