common_data_model/data_models.rs
1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4
5use crate::custom_traits::CustomSerializer;
6
7/// Represents a host discovered on the network.
8///
9/// This struct contains details such as the host's IP address, MAC address,
10/// vendor name, and open ports.
11#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Serialize, Deserialize, FromRow)]
12pub struct DiscoveredHost {
13 pub id: Option<i32>,
14 pub ip: String,
15 pub mac_address: String,
16 pub vendor_name: String,
17 pub discovered_at: DateTime<Utc>,
18 pub open_ports: Vec<ServicePort>,
19}
20
21impl DiscoveredHost {
22 /// Creates a new instance of `DiscoveredHost`.
23 ///
24 /// # Arguments
25 ///
26 /// * `ip` - The IP address of the host.
27 /// * `mac_address` - The MAC address of the host.
28 /// * `vendor_name` - The vendor name associated with the MAC address.
29 /// * `open_ports` - A vector of open service ports on the host.
30 pub fn new(
31 ip: String,
32 mac_address: String,
33 vendor_name: String,
34 open_ports: Vec<ServicePort>,
35 ) -> Self {
36 let discovered_at = Utc::now();
37 DiscoveredHost {
38 id: None,
39 ip,
40 mac_address,
41 vendor_name,
42 discovered_at,
43 open_ports,
44 }
45 }
46}
47/// Represents an open service port on a discovered host.
48///
49/// This struct contains the port number, the name of the service running on that port,
50/// and an optional ID which can be used when storing in a database.
51#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Serialize, Deserialize, FromRow)]
52pub struct ServicePort {
53 pub id: Option<i32>,
54 pub port: i32,
55 pub name: String,
56}
57
58impl ServicePort {
59 /// Creates a new instance of `ServicePort` without an ID.
60 ///
61 /// This constructor is useful for newly discovered service ports that
62 /// haven't been persisted to a database yet.
63 ///
64 /// # Arguments
65 ///
66 /// * `port` - The port number of the service.
67 /// * `name` - The name of the service running on the port.
68 ///
69 /// # Returns
70 /// A new instance of `ServicePort` with the specified port and service name.
71 pub fn new(port: i32, name: String) -> Self {
72 ServicePort {
73 id: None,
74 port,
75 name,
76 }
77 }
78 /// Creates a new instance of `ServicePort` with a specified ID.
79 ///
80 /// This constructor is useful for service ports that have been persisted
81 /// to a database and have a corresponding ID.
82 ///
83 /// # Arguments
84 ///
85 /// * `id` - The database ID of the service port.
86 /// * `port` - The port number of the service.
87 /// * `name` - The name of the service running on the port.
88 ///
89 /// # Returns
90 /// A new instance of `ServicePort` with the specified ID, port, and service name.
91 pub fn new_with_id(id: i32, port: i32, name: String) -> Self {
92 ServicePort {
93 id: Some(id),
94 port,
95 name,
96 }
97 }
98}
99
100/// Represents the association between a `ServicePort` and a `DiscoveredHost`.
101///
102/// This struct is used to link a discovered host with the service ports
103/// it has open, typically for database associations.
104#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Serialize, Deserialize, FromRow)]
105pub struct ServiceDiscoveredHost {
106 pub service_port_id: Option<i32>,
107 pub discovered_host_id: Option<i32>,
108}
109
110impl ServiceDiscoveredHost {
111 /// Creates a new instance of `ServiceDiscoveredHost`.
112 ///
113 /// This constructor links a service port with a discovered host.
114 ///
115 /// # Arguments
116 ///
117 /// * `service_port_id` - The ID of the associated service port.
118 /// * `discovered_host_id` - The ID of the associated discovered host.
119 ///
120 /// # Returns
121 /// A new instance of `ServiceDiscoveredHost` with the specified associations.
122 pub fn new(service_port_id: Option<i32>, discovered_host_id: Option<i32>) -> Self {
123 ServiceDiscoveredHost {
124 service_port_id,
125 discovered_host_id,
126 }
127 }
128}
129
130impl CustomSerializer<ServiceDiscoveredHost> for ServiceDiscoveredHost {}
131
132impl CustomSerializer<ServicePort> for ServicePort {}
133
134impl CustomSerializer<DiscoveredHost> for DiscoveredHost {}