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
use super::{
protocol_numbers,
Header,
EthernetHeader,
ArpHeader,
IcmpHeader,
IpHeader,
TcpHeader
};
use std::fmt;
type ProtocolNumber = u8;
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub enum Protocol {
ETH,
ARP,
ICMP,
TCP,
UDP,
IP,
}
impl Protocol {
pub fn min_header_len(&self) -> u8 {
match self {
Self::ETH => EthernetHeader::get_min_length(),
Self::ARP => ArpHeader::get_min_length(),
Self::ICMP => IcmpHeader::get_min_length(),
Self::TCP => TcpHeader::get_min_length(),
Self::UDP => 0,
Self::IP => IpHeader::get_min_length(),
}
}
pub fn protocol_number(&self) -> ProtocolNumber {
match self {
Self::ETH => protocol_numbers::IPPROTO_ETHERIP,
Self::ICMP => protocol_numbers::IPPROTO_ICMP,
Self::TCP => protocol_numbers::IPPROTO_TCP,
Self::UDP => protocol_numbers::IPPROTO_UDP,
Self::IP => protocol_numbers::IPPROTO_IPV4,
Self::ARP => panic!("ARP does not have an assigned ip protocol number"),
}
}
}
impl From<ProtocolNumber> for Protocol {
fn from(p: ProtocolNumber) -> Protocol {
match p {
protocol_numbers::IPPROTO_ETHERIP => Protocol::ETH,
protocol_numbers::IPPROTO_ICMP => Protocol::ICMP,
protocol_numbers::IPPROTO_TCP => Protocol::TCP,
protocol_numbers::IPPROTO_UDP => Protocol::UDP,
protocol_numbers::IPPROTO_IPV4 => Protocol::IP,
_ => panic!("Could not convert to Protocol enum"),
}
}
}
impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Protocol::ETH => write!(f, "Ethernet"),
Protocol::ARP => write!(f, "Arp"),
Protocol::ICMP => write!(f, "ICMP"),
Protocol::TCP => write!(f, "TCP"),
Protocol::UDP => write!(f, "UDP"),
Protocol::IP => write!(f, "IP"),
}
}
}