1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct NetworkPacket {
6 pub timestamp: DateTime<Utc>,
7 pub ethernet_layer: EthernetFrame,
8 pub ip_layer: IPv4Packet,
9 pub tcp_layer: TCPSegment,
10 pub application_layer: ApplicationData,
11}
12
13#[derive(Debug, Serialize, Deserialize, Clone)]
15pub struct EthernetFrame {
16 pub source_mac: [u8; 6],
17 pub destination_mac: [u8; 6],
18 pub ethertype: u16, pub frame_check_sequence: u32,
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone)]
24pub struct IPv4Packet {
25 pub version: u8, pub ihl: u8, pub dscp: u8, pub ecn: u8, pub total_length: u16,
30 pub identification: u16,
31 pub flags: IPv4Flags,
32 pub fragment_offset: u16,
33 pub ttl: u8, pub protocol: u8, pub header_checksum: u16,
36 pub source_ip: [u8; 4],
37 pub destination_ip: [u8; 4],
38 pub options: Vec<u8>, }
40
41#[derive(Debug, Serialize, Deserialize, Clone)]
43pub struct IPv4Flags {
44 pub reserved: bool, pub dont_fragment: bool,
46 pub more_fragments: bool,
47}
48
49#[derive(Debug, Serialize, Deserialize, Clone)]
51pub struct TCPSegment {
52 pub source_port: u16,
53 pub destination_port: u16,
54 pub sequence_number: u32,
55 pub acknowledgment_number: u32,
56 pub data_offset: u8, pub flags: TCPFlags,
58 pub window_size: u16,
59 pub checksum: u16,
60 pub urgent_pointer: u16,
61 pub options: Vec<TCPOption>,
62}
63
64#[derive(Debug, Serialize, Deserialize, Clone)]
66pub struct TCPFlags {
67 pub fin: bool, pub syn: bool, pub rst: bool, pub psh: bool, pub ack: bool, pub urg: bool, pub ece: bool, pub cwr: bool, }
76
77#[derive(Debug, Serialize, Deserialize, Clone)]
79pub struct TCPOption {
80 pub kind: u8,
81 pub length: u8,
82 pub data: Vec<u8>,
83}
84
85#[derive(Debug, Serialize, Deserialize, Clone)]
87pub struct ApplicationData {
88 pub protocol: ApplicationProtocol,
89 pub payload: Vec<u8>,
90}
91
92#[derive(Debug, Serialize, Deserialize, Clone)]
94pub enum ApplicationProtocol {
95 HTTP,
96 HTTPS,
97 FTP,
98 SSH,
99 SMTP,
100 DNS,
101 NTP,
102 NetBIOS,
103 IMAP,
104 SNMP,
105 LDAP,
106 SMB,
107 Kerberos,
108 Custom(String),
109}
110
111impl NetworkPacket {
113 pub fn total_size(&self) -> usize {
115 14 + 20 + self.ip_layer.options.len() + 20 + self.tcp_layer.options.iter().map(|opt| opt.length as usize).sum::<usize>() + self.application_layer.payload.len() }
120
121 pub fn is_handshake(&self) -> bool {
123 self.tcp_layer.flags.syn || self.tcp_layer.flags.fin
124 }
125
126 pub fn get_protocol_string(&self) -> String {
128 match &self.application_layer.protocol {
129 ApplicationProtocol::HTTP => "HTTP".to_string(),
130 ApplicationProtocol::HTTPS => "HTTPS".to_string(),
131 ApplicationProtocol::FTP => "FTP".to_string(),
132 ApplicationProtocol::SSH => "SSH".to_string(),
133 ApplicationProtocol::SMTP => "SMTP".to_string(),
134 ApplicationProtocol::DNS => "DNS".to_string(),
135 ApplicationProtocol::NTP => "NTP".to_string(),
136 ApplicationProtocol::NetBIOS => "NetBIOS".to_string(),
137 ApplicationProtocol::IMAP => "IMAP".to_string(),
138 ApplicationProtocol::SNMP => "SNMP".to_string(),
139 ApplicationProtocol::LDAP => "LDAP".to_string(),
140 ApplicationProtocol::SMB => "SMB".to_string(),
141 ApplicationProtocol::Kerberos => "Kerberos".to_string(),
142 ApplicationProtocol::Custom(proto) => proto.clone(),
143 }
144 }
145}