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
use crate::{MacAddr, Packet, PrimitiveValues};
use std::fmt;
#[derive(Debug, Packet)]
pub struct Ethernet {
#[construct_with(u8, u8, u8, u8, u8, u8)]
pub destination: MacAddr,
#[construct_with(u8, u8, u8, u8, u8, u8)]
pub source: MacAddr,
#[construct_with(u16)]
pub ethertype: EtherType,
#[payload]
pub payload: Vec<u8>,
}
#[test]
fn ethernet_header_test() {
let mut packet = [0u8; 14];
{
let mut ethernet_header = MutableEthernetPacket::new(&mut packet[..]).unwrap();
let source = MacAddr(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc);
ethernet_header.set_source(source);
assert_eq!(ethernet_header.get_source(), source);
let dest = MacAddr(0xde, 0xf0, 0x12, 0x34, 0x45, 0x67);
ethernet_header.set_destination(dest);
assert_eq!(ethernet_header.get_destination(), dest);
ethernet_header.set_ethertype(EtherTypes::Ipv6);
assert_eq!(ethernet_header.get_ethertype(), EtherTypes::Ipv6);
}
let ref_packet = [
0xde, 0xf0, 0x12, 0x34, 0x45, 0x67,
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
0x86, 0xdd,
];
assert_eq!(&ref_packet[..], &packet[..]);
}
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod EtherTypes {
use super::EtherType;
pub const Ipv4: EtherType = EtherType(0x0800);
pub const Arp: EtherType = EtherType(0x0806);
pub const WakeOnLan: EtherType = EtherType(0x0842);
pub const Trill: EtherType = EtherType(0x22F3);
pub const DECnet: EtherType = EtherType(0x6003);
pub const Rarp: EtherType = EtherType(0x8035);
pub const AppleTalk: EtherType = EtherType(0x809B);
pub const Aarp: EtherType = EtherType(0x80F3);
pub const Ipx: EtherType = EtherType(0x8137);
pub const Qnx: EtherType = EtherType(0x8204);
pub const Ipv6: EtherType = EtherType(0x86DD);
pub const FlowControl: EtherType = EtherType(0x8808);
pub const CobraNet: EtherType = EtherType(0x8819);
pub const Mpls: EtherType = EtherType(0x8847);
pub const MplsMcast: EtherType = EtherType(0x8848);
pub const PppoeDiscovery: EtherType = EtherType(0x8863);
pub const PppoeSession: EtherType = EtherType(0x8864);
pub const Vlan: EtherType = EtherType(0x8100);
pub const PBridge: EtherType = EtherType(0x88a8);
pub const Lldp: EtherType = EtherType(0x88cc);
pub const Ptp: EtherType = EtherType(0x88f7);
pub const Cfm: EtherType = EtherType(0x8902);
pub const QinQ: EtherType = EtherType(0x9100);
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EtherType(pub u16);
impl EtherType {
pub fn new(val: u16) -> EtherType {
EtherType(val)
}
}
impl PrimitiveValues for EtherType {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
impl fmt::Display for EtherType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
&EtherTypes::Ipv4 => "Ipv4",
&EtherTypes::Arp => "Arp",
&EtherTypes::WakeOnLan => "WakeOnLan",
&EtherTypes::Trill => "Trill",
&EtherTypes::DECnet => "DECnet",
&EtherTypes::Rarp => "Rarp",
&EtherTypes::AppleTalk => "AppleTalk",
&EtherTypes::Aarp => "Aarp",
&EtherTypes::Ipx => "Ipx",
&EtherTypes::Qnx => "Qnx",
&EtherTypes::Ipv6 => "Ipv6",
&EtherTypes::FlowControl => "FlowControl",
&EtherTypes::CobraNet => "CobraNet",
&EtherTypes::Mpls => "Mpls",
&EtherTypes::MplsMcast => "MplsMcast",
&EtherTypes::PppoeDiscovery => "PppoeDiscovery",
&EtherTypes::PppoeSession => "PppoeSession",
&EtherTypes::Vlan => "Vlan",
&EtherTypes::PBridge => "PBridge",
&EtherTypes::Lldp => "Lldp",
&EtherTypes::Ptp => "Ptp",
&EtherTypes::Cfm => "Cfm",
&EtherTypes::QinQ => "QinQ",
_ => "unknown",
}
)
}
}
#[test]
fn ether_type_to_str() {
let ipv4 = EtherType(0x0800);
assert_eq!(format!("{}", ipv4), "Ipv4");
let arp = EtherType(0x0806);
assert_eq!(format!("{}", arp), "Arp");
let unknown = EtherType(0x0666);
assert_eq!(format!("{}", unknown), "unknown");
}
#[cfg(all(test, feature = "benchmark"))]
mod packet_benchmarks {
use super::*;
use test::{black_box, Bencher};
use util::MacAddr;
#[bench]
fn bench_packet_new_constructor(b: &mut Bencher) {
let buffer = vec![0; 20];
b.iter(|| EthernetPacket::new(black_box(&buffer)).unwrap());
}
#[bench]
fn bench_packet_get_source(b: &mut Bencher) {
let buffer = vec![0; 20];
let packet = EthernetPacket::new(&buffer).unwrap();
b.iter(|| black_box(packet.get_source()));
}
#[bench]
fn bench_packet_set_source_black_box(b: &mut Bencher) {
let mut buffer = vec![0; 20];
let mut packet = MutableEthernetPacket::new(&mut buffer).unwrap();
let mac = MacAddr::new(1, 2, 3, 4, 5, 6);
b.iter(|| packet.set_source(black_box(mac)));
}
#[bench]
fn bench_packet_mutable_to_immutable(b: &mut Bencher) {
let mut buffer = vec![0; 20];
let mut packet = MutableEthernetPacket::new(&mut buffer).unwrap();
b.iter(|| black_box(packet.to_immutable()));
}
#[bench]
fn bench_packet_immutable_to_immutable(b: &mut Bencher) {
let mut buffer = vec![0; 20];
let mut packet = EthernetPacket::new(&mut buffer).unwrap();
b.iter(|| black_box(packet.to_immutable()));
}
}